2016-05-20 23:13:28 +02:00
|
|
|
/**
|
|
|
|
* Copyright 2016 IBM Corp.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
**/
|
|
|
|
(function($) {
|
|
|
|
|
2016-05-29 23:25:30 +02:00
|
|
|
/**
|
|
|
|
* options:
|
2016-06-16 01:08:53 +02:00
|
|
|
* - addButton : boolean|string - text for add label, default 'add'
|
2016-05-29 23:25:30 +02:00
|
|
|
* - height : number|'auto'
|
|
|
|
* - resize : function - called when list as a whole is resized
|
|
|
|
* - resizeItem : function(item) - called to resize individual item
|
|
|
|
* - sortable : boolean|string - string is the css selector for handle
|
|
|
|
* - sortItems : function(items) - when order of items changes
|
|
|
|
* - connectWith : css selector of other sortables
|
|
|
|
* - removable : boolean - whether to display delete button on items
|
|
|
|
* - addItem : function(row,index,itemData) - when an item is added
|
|
|
|
* - removeItem : function(itemData) - called when an item is removed
|
2016-11-23 11:58:19 +01:00
|
|
|
* - filter : function(itemData) - called for each item to determine if it should be shown
|
|
|
|
* - sort : function(itemDataA,itemDataB) - called to sort items
|
|
|
|
* - scrollOnAdd : boolean - whether to scroll to newly added items
|
2016-05-29 23:25:30 +02:00
|
|
|
* methods:
|
|
|
|
* - addItem(itemData)
|
|
|
|
* - removeItem(itemData)
|
|
|
|
* - width(width)
|
|
|
|
* - height(height)
|
|
|
|
* - items()
|
|
|
|
* - empty()
|
2016-11-23 11:58:19 +01:00
|
|
|
* - filter(filter)
|
|
|
|
* - sort(sort)
|
|
|
|
* - length()
|
2016-05-29 23:25:30 +02:00
|
|
|
*/
|
2016-05-20 23:13:28 +02:00
|
|
|
$.widget( "nodered.editableList", {
|
|
|
|
_create: function() {
|
|
|
|
var that = this;
|
|
|
|
|
|
|
|
this.element.addClass('red-ui-editableList-list');
|
|
|
|
this.uiWidth = this.element.width();
|
|
|
|
this.uiContainer = this.element
|
|
|
|
.wrap( "<div>" )
|
|
|
|
.parent();
|
|
|
|
this.topContainer = this.uiContainer.wrap("<div>").parent();
|
|
|
|
|
|
|
|
this.topContainer.addClass('red-ui-editableList');
|
|
|
|
|
2016-05-29 23:25:30 +02:00
|
|
|
if (this.options.addButton !== false) {
|
|
|
|
var addLabel;
|
|
|
|
if (typeof this.options.addButton === 'string') {
|
|
|
|
addLabel = this.options.addButton
|
|
|
|
} else {
|
|
|
|
if (RED && RED._) {
|
|
|
|
addLabel = RED._("editableList.add");
|
|
|
|
} else {
|
|
|
|
addLabel = 'add';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$('<a href="#" class="editor-button editor-button-small" style="margin-top: 4px;"><i class="fa fa-plus"></i> '+addLabel+'</a>')
|
|
|
|
.appendTo(this.topContainer)
|
|
|
|
.click(function(evt) {
|
|
|
|
evt.preventDefault();
|
|
|
|
that.addItem({});
|
|
|
|
});
|
2016-05-20 23:13:28 +02:00
|
|
|
}
|
2016-08-04 17:49:36 +02:00
|
|
|
if (this.element.css("position") === "absolute") {
|
|
|
|
["top","left","bottom","right"].forEach(function(s) {
|
|
|
|
var v = that.element.css(s);
|
|
|
|
if (s!=="auto" && s!=="") {
|
|
|
|
that.topContainer.css(s,v);
|
|
|
|
that.uiContainer.css(s,"0");
|
|
|
|
that.element.css(s,'auto');
|
|
|
|
}
|
|
|
|
})
|
2016-09-30 11:46:13 +02:00
|
|
|
this.element.css("position","static");
|
|
|
|
this.topContainer.css("position","absolute");
|
|
|
|
this.uiContainer.css("position","absolute");
|
2016-05-20 23:13:28 +02:00
|
|
|
|
2016-08-04 17:49:36 +02:00
|
|
|
}
|
2016-05-20 23:13:28 +02:00
|
|
|
this.uiContainer.addClass("red-ui-editableList-container");
|
|
|
|
|
|
|
|
this.uiHeight = this.element.height();
|
|
|
|
|
2016-08-05 17:39:41 +02:00
|
|
|
this.activeFilter = this.options.filter||null;
|
2016-08-13 00:00:28 +02:00
|
|
|
this.activeSort = this.options.sort||null;
|
2016-09-30 00:44:49 +02:00
|
|
|
this.scrollOnAdd = this.options.scrollOnAdd;
|
|
|
|
if (this.scrollOnAdd === undefined) {
|
|
|
|
this.scrollOnAdd = true;
|
|
|
|
}
|
2016-05-20 23:13:28 +02:00
|
|
|
var minHeight = this.element.css("minHeight");
|
|
|
|
if (minHeight !== '0px') {
|
|
|
|
this.uiContainer.css("minHeight",minHeight);
|
|
|
|
this.element.css("minHeight",0);
|
|
|
|
}
|
2016-05-29 23:25:30 +02:00
|
|
|
if (this.options.height !== 'auto') {
|
|
|
|
this.uiContainer.css("overflow-y","scroll");
|
|
|
|
if (!isNaN(this.options.height)) {
|
|
|
|
this.uiHeight = this.options.height;
|
|
|
|
}
|
|
|
|
}
|
2016-06-28 11:56:03 +02:00
|
|
|
this.element.height('auto');
|
|
|
|
|
|
|
|
var attrStyle = this.element.attr('style');
|
|
|
|
var m;
|
|
|
|
if ((m = /width\s*:\s*(\d+%)/i.exec(attrStyle)) !== null) {
|
|
|
|
this.element.width('100%');
|
|
|
|
this.uiContainer.width(m[1]);
|
|
|
|
}
|
2016-05-20 23:13:28 +02:00
|
|
|
if (this.options.sortable) {
|
2016-05-29 23:25:30 +02:00
|
|
|
var handle = (typeof this.options.sortable === 'string')?
|
|
|
|
this.options.sortable :
|
|
|
|
".red-ui-editableList-item-handle";
|
|
|
|
var sortOptions = {
|
2016-05-20 23:13:28 +02:00
|
|
|
axis: "y",
|
|
|
|
update: function( event, ui ) {
|
2016-05-29 23:25:30 +02:00
|
|
|
if (that.options.sortItems) {
|
|
|
|
that.options.sortItems(that.items());
|
2016-05-20 23:13:28 +02:00
|
|
|
}
|
|
|
|
},
|
2016-05-29 23:25:30 +02:00
|
|
|
handle:handle,
|
|
|
|
cursor: "move",
|
|
|
|
tolerance: "pointer",
|
|
|
|
forcePlaceholderSize:true,
|
|
|
|
placeholder: "red-ui-editabelList-item-placeholder",
|
|
|
|
start: function(e, ui){
|
|
|
|
ui.placeholder.height(ui.item.height()-4);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if (this.options.connectWith) {
|
|
|
|
sortOptions.connectWith = this.options.connectWith;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.element.sortable(sortOptions);
|
2016-05-20 23:13:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
this._resize();
|
|
|
|
|
|
|
|
// this.menu = this._createMenu(this.types, function(v) { that.type(v) });
|
|
|
|
// this.type(this.options.default||this.types[0].value);
|
|
|
|
},
|
|
|
|
_resize: function() {
|
|
|
|
var currentFullHeight = this.topContainer.height();
|
|
|
|
var innerHeight = this.uiContainer.height();
|
|
|
|
var delta = currentFullHeight - innerHeight;
|
2016-05-29 23:41:25 +02:00
|
|
|
if (this.uiHeight !== 0) {
|
2016-05-29 23:25:30 +02:00
|
|
|
this.uiContainer.height(this.uiHeight-delta);
|
|
|
|
}
|
2016-05-20 23:13:28 +02:00
|
|
|
if (this.options.resize) {
|
|
|
|
this.options.resize();
|
|
|
|
}
|
|
|
|
if (this.options.resizeItem) {
|
|
|
|
var that = this;
|
|
|
|
this.element.children().each(function(i) {
|
2016-06-16 01:08:53 +02:00
|
|
|
that.options.resizeItem($(this).find(".red-ui-editableList-item-content"),i);
|
2016-05-20 23:13:28 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_destroy: function() {
|
|
|
|
},
|
2016-08-05 17:39:41 +02:00
|
|
|
_refreshFilter: function() {
|
|
|
|
var that = this;
|
2016-08-09 11:41:26 +02:00
|
|
|
var count = 0;
|
2016-08-05 17:39:41 +02:00
|
|
|
if (!this.activeFilter) {
|
|
|
|
this.element.children().show();
|
|
|
|
}
|
|
|
|
var items = this.items();
|
|
|
|
items.each(function (i,el) {
|
|
|
|
var data = el.data('data');
|
|
|
|
try {
|
|
|
|
if (that.activeFilter(data)) {
|
|
|
|
el.parent().show();
|
2016-08-09 11:41:26 +02:00
|
|
|
count++;
|
2016-08-05 17:39:41 +02:00
|
|
|
} else {
|
|
|
|
el.parent().hide();
|
|
|
|
}
|
|
|
|
} catch(err) {
|
|
|
|
console.log(err);
|
|
|
|
el.parent().show();
|
2016-08-09 11:41:26 +02:00
|
|
|
count++;
|
2016-08-05 17:39:41 +02:00
|
|
|
}
|
|
|
|
});
|
2016-08-09 11:41:26 +02:00
|
|
|
return count;
|
2016-08-05 17:39:41 +02:00
|
|
|
},
|
2016-08-13 00:00:28 +02:00
|
|
|
_refreshSort: function() {
|
|
|
|
if (this.activeSort) {
|
|
|
|
var items = this.element.children();
|
|
|
|
var that = this;
|
|
|
|
items.sort(function(A,B) {
|
|
|
|
return that.activeSort($(A).find(".red-ui-editableList-item-content").data('data'),$(B).find(".red-ui-editableList-item-content").data('data'));
|
|
|
|
});
|
|
|
|
$.each(items,function(idx,li) {
|
|
|
|
that.element.append(li);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
},
|
2016-05-20 23:13:28 +02:00
|
|
|
width: function(desiredWidth) {
|
|
|
|
this.uiWidth = desiredWidth;
|
|
|
|
this._resize();
|
|
|
|
},
|
|
|
|
height: function(desiredHeight) {
|
|
|
|
this.uiHeight = desiredHeight;
|
|
|
|
this._resize();
|
|
|
|
},
|
|
|
|
addItem: function(data) {
|
|
|
|
var that = this;
|
|
|
|
data = data || {};
|
2016-08-04 17:49:36 +02:00
|
|
|
var li = $('<li>');
|
|
|
|
var added = false;
|
2016-08-13 00:00:28 +02:00
|
|
|
if (this.activeSort) {
|
2016-08-04 17:49:36 +02:00
|
|
|
var items = this.items();
|
|
|
|
var skip = false;
|
|
|
|
items.each(function(i,el) {
|
|
|
|
if (added) { return }
|
|
|
|
var itemData = el.data('data');
|
2016-08-13 00:00:28 +02:00
|
|
|
if (that.activeSort(data,itemData) < 0) {
|
2016-08-04 17:49:36 +02:00
|
|
|
li.insertBefore(el.closest("li"));
|
|
|
|
added = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (!added) {
|
|
|
|
li.appendTo(this.element);
|
|
|
|
}
|
2016-05-20 23:13:28 +02:00
|
|
|
var row = $('<div/>').addClass("red-ui-editableList-item-content").appendTo(li);
|
2016-05-29 23:25:30 +02:00
|
|
|
row.data('data',data);
|
|
|
|
if (this.options.sortable === true) {
|
2016-05-20 23:13:28 +02:00
|
|
|
$('<i class="red-ui-editableList-item-handle fa fa-bars"></i>').appendTo(li);
|
|
|
|
li.addClass("red-ui-editableList-item-sortable");
|
|
|
|
}
|
2016-05-29 23:25:30 +02:00
|
|
|
if (this.options.removable) {
|
|
|
|
var deleteButton = $('<a/>',{href:"#",class:"red-ui-editableList-item-remove editor-button editor-button-small"}).appendTo(li);
|
2016-05-20 23:13:28 +02:00
|
|
|
$('<i/>',{class:"fa fa-remove"}).appendTo(deleteButton);
|
2016-05-29 23:25:30 +02:00
|
|
|
li.addClass("red-ui-editableList-item-removable");
|
2016-11-08 18:01:21 +01:00
|
|
|
deleteButton.click(function(evt) {
|
|
|
|
evt.preventDefault();
|
2016-08-22 00:05:53 +02:00
|
|
|
var data = row.data('data');
|
2016-05-20 23:13:28 +02:00
|
|
|
li.addClass("red-ui-editableList-item-deleting")
|
|
|
|
li.fadeOut(300, function() {
|
|
|
|
$(this).remove();
|
2016-05-29 23:25:30 +02:00
|
|
|
if (that.options.removeItem) {
|
2016-08-22 00:05:53 +02:00
|
|
|
that.options.removeItem(data);
|
2016-05-20 23:13:28 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (this.options.addItem) {
|
|
|
|
var index = that.element.children().length-1;
|
|
|
|
setTimeout(function() {
|
|
|
|
that.options.addItem(row,index,data);
|
2016-08-05 17:39:41 +02:00
|
|
|
if (that.activeFilter) {
|
|
|
|
try {
|
|
|
|
if (!that.activeFilter(data)) {
|
|
|
|
li.hide();
|
|
|
|
}
|
|
|
|
} catch(err) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-30 00:44:49 +02:00
|
|
|
if (!that.activeSort && that.scrollOnAdd) {
|
2016-08-05 14:39:14 +02:00
|
|
|
setTimeout(function() {
|
|
|
|
that.uiContainer.scrollTop(that.element.height());
|
|
|
|
},0);
|
|
|
|
}
|
2016-05-20 23:13:28 +02:00
|
|
|
},0);
|
|
|
|
}
|
|
|
|
},
|
2016-05-29 23:25:30 +02:00
|
|
|
removeItem: function(data) {
|
|
|
|
var items = this.element.children().filter(function(f) {
|
|
|
|
return data === $(this).find(".red-ui-editableList-item-content").data('data');
|
|
|
|
});
|
|
|
|
items.remove();
|
|
|
|
if (this.options.removeItem) {
|
|
|
|
this.options.removeItem(data);
|
|
|
|
}
|
|
|
|
},
|
2016-05-20 23:13:28 +02:00
|
|
|
items: function() {
|
|
|
|
return this.element.children().map(function(i) { return $(this).find(".red-ui-editableList-item-content"); });
|
2016-05-29 23:25:30 +02:00
|
|
|
},
|
|
|
|
empty: function() {
|
|
|
|
this.element.empty();
|
2016-08-05 17:39:41 +02:00
|
|
|
},
|
|
|
|
filter: function(filter) {
|
|
|
|
if (filter !== undefined) {
|
|
|
|
this.activeFilter = filter;
|
|
|
|
}
|
2016-08-09 11:41:26 +02:00
|
|
|
return this._refreshFilter();
|
|
|
|
},
|
2016-08-13 00:00:28 +02:00
|
|
|
sort: function(sort) {
|
|
|
|
if (sort !== undefined) {
|
|
|
|
this.activeSort = sort;
|
|
|
|
}
|
|
|
|
return this._refreshSort();
|
|
|
|
},
|
2016-08-09 11:41:26 +02:00
|
|
|
length: function() {
|
|
|
|
return this.element.children().length;
|
2016-05-20 23:13:28 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
})(jQuery);
|