');
// DORP DOWN AREA
this._elemDropDownContainer = $('#' + this.comboTreeId + 'DropDownContainer');
this._elemDropDownContainer.html(this.createSourceHTML());
this._elemFilterInput = this.options.isMultiple ? $('#' + this.comboTreeId + 'MultiFilter') : null;
this._elemSourceUl = $('#' + this.comboTreeId + 'ComboTreeSourceUl');
this._elemItems = this._elemDropDownContainer.find('li');
this._elemItemsTitle = this._elemDropDownContainer.find('span.comboTreeItemTitle');
// VARIABLES
this._selectedItem = {};
this._selectedItems = [];
this.processSelected();
this.bindings();
};
ComboTree.prototype.unbind = function () {
this._elemArrowBtn.off('click');
this._elemInput.off('click');
this._elemItems.off('click');
this._elemItemsTitle.off('click');
this._elemItemsTitle.off("mousemove");
this._elemInput.off('keyup');
this._elemInput.off('keydown');
this._elemInput.off('mouseup.' + this.comboTreeId);
$(document).off('mouseup.' + this.comboTreeId);
}
ComboTree.prototype.destroy = function () {
this.unbind();
this._elemWrapper.before(this._elemInput);
this._elemWrapper.remove();
//this._elemInput.removeData('plugin_' + comboTreePlugin);
}
// CREATE DOM HTMLs
ComboTree.prototype.removeSourceHTML = function () {
this._elemDropDownContainer.html('');
};
ComboTree.prototype.createSourceHTML = function () {
var sourceHTML = '';
if (this.options.isMultiple)
sourceHTML = this.createFilterHTMLForMultiSelect();
sourceHTML += this.createSourceSubItemsHTML(this.options.source);
return sourceHTML;
};
ComboTree.prototype.createFilterHTMLForMultiSelect = function (){
return '
';
}
ComboTree.prototype.createSourceSubItemsHTML = function (subItems, parentId) {
var subItemsHtml = '
';
for (var i=0; i'
return subItemsHtml;
}
ComboTree.prototype.createSourceItemHTML = function (sourceItem) {
var itemHtml = "",
isThereSubs = sourceItem.hasOwnProperty("subs");
itemHtml = '- ';
if (isThereSubs)
itemHtml += '' + (this.options.collapse ? '+' : '−') + '';
if (this.options.isMultiple)
itemHtml += '' + sourceItem.title + '';
else
itemHtml += '' + sourceItem.title + '';
if (isThereSubs)
itemHtml += this.createSourceSubItemsHTML(sourceItem.subs, sourceItem.id);
itemHtml += '
';
return itemHtml;
};
// BINDINGS
ComboTree.prototype.bindings = function () {
var _this = this;
$(this._elemInput).focus(function (e) {
if (!_this._elemDropDownContainer.is(':visible'))
$(_this._elemDropDownContainer).slideToggle(100);
});
this._elemArrowBtn.on('click', function(e){
e.stopPropagation();
_this.toggleDropDown();
});
this._elemInput.on('click', function(e){
e.stopPropagation();
if (!_this._elemDropDownContainer.is(':visible'))
_this.toggleDropDown();
});
this._elemItems.on('click', function(e){
e.stopPropagation();
if ($(this).hasClass('ComboTreeItemParent')){
_this.toggleSelectionTree(this);
}
});
this._elemItemsTitle.on('click', function(e){
e.stopPropagation();
if (_this.options.isMultiple)
_this.multiItemClick(this);
else
_this.singleItemClick(this);
});
this._elemItemsTitle.on("mousemove", function (e) {
e.stopPropagation();
_this.dropDownMenuHover(this);
});
// KEY BINDINGS
this._elemInput.on('keyup', function(e) {
e.stopPropagation();
switch (e.keyCode) {
case 27:
_this.closeDropDownMenu(); break;
case 13:
case 39: case 37: case 40: case 38:
e.preventDefault();
break;
default:
if (!_this.options.isMultiple)
_this.filterDropDownMenu();
break;
}
});
this._elemFilterInput && this._elemFilterInput.on('keyup', function (e) {
e.stopPropagation();
switch (e.keyCode) {
case 27:
if ($(this).val()) {
$(this).val('');
_this.filterDropDownMenu();
} else {
_this.closeDropDownMenu();
}
break;
case 40: case 38:
e.preventDefault();
_this.dropDownInputKeyControl(e.keyCode - 39); break;
case 37: case 39:
e.preventDefault();
_this.dropDownInputKeyToggleTreeControl(e.keyCode - 38);
break;
case 13:
_this.multiItemClick(_this._elemHoveredItem);
e.preventDefault();
break;
default:
_this.filterDropDownMenu();
break;
}
});
this._elemInput.on('keydown', function(e) {
e.stopPropagation();
switch (e.keyCode) {
case 9:
_this.closeDropDownMenu(); break;
case 40: case 38:
e.preventDefault();
_this.dropDownInputKeyControl(e.keyCode - 39); break;
case 37: case 39:
e.preventDefault();
_this.dropDownInputKeyToggleTreeControl(e.keyCode - 38);
break;
case 13:
if (_this.options.isMultiple)
_this.multiItemClick(_this._elemHoveredItem);
else
_this.singleItemClick(_this._elemHoveredItem);
e.preventDefault();
break;
default:
if (_this.options.isMultiple)
e.preventDefault();
}
});
// ON FOCUS OUT CLOSE DROPDOWN
$(document).on('mouseup.' + _this.comboTreeId, function (e){
if (!_this._elemWrapper.is(e.target) && _this._elemWrapper.has(e.target).length === 0 && _this._elemDropDownContainer.is(':visible'))
_this.closeDropDownMenu();
});
};
// EVENTS HERE
// DropDown Menu Open/Close
ComboTree.prototype.toggleDropDown = function () {
let _this = this;
$(this._elemDropDownContainer).slideToggle(100, function () {
if (_this._elemDropDownContainer.is(':visible'))
$(_this._elemInput).focus();
});
};
ComboTree.prototype.closeDropDownMenu = function () {
$(this._elemDropDownContainer).slideUp(100);
};
// Selection Tree Open/Close
ComboTree.prototype.toggleSelectionTree = function (item, direction) {
var subMenu = $(item).children('ul')[0];
if (direction === undefined){
if ($(subMenu).is(':visible'))
$(item).children('span.comboTreeParentPlus').html("+");
else
$(item).children('span.comboTreeParentPlus').html("−");
$(subMenu).slideToggle(50);
}
else if (direction == 1 && !$(subMenu).is(':visible')){
$(item).children('span.comboTreeParentPlus').html("−");
$(subMenu).slideDown(50);
}
else if (direction == -1){
if ($(subMenu).is(':visible')){
$(item).children('span.comboTreeParentPlus').html("+");
$(subMenu).slideUp(50);
}
else {
this.dropDownMenuHoverToParentItem(item);
}
}
};
// SELECTION FUNCTIONS
ComboTree.prototype.selectMultipleItem = function(ctItem){
this._selectedItem = {
id: $(ctItem).attr("data-id"),
title: $(ctItem).text()
};
let check = this.isItemInArray(this._selectedItem, this.options.source);
if (check) {
var index = this.isItemInArray(this._selectedItem, this._selectedItems);
if (index) {
this._selectedItems.splice(parseInt(index), 1);
$(ctItem).find("input").prop('checked', false);
} else {
this._selectedItems.push(this._selectedItem);
$(ctItem).find("input").prop('checked', true);
}
}
};
ComboTree.prototype.singleItemClick = function (ctItem) {
this._selectedItem = {
id: $(ctItem).attr("data-id"),
title: $(ctItem).text()
};
this.refreshInputVal();
this.closeDropDownMenu();
};
ComboTree.prototype.multiItemClick = function (ctItem) {
this.selectMultipleItem(ctItem);
if(this.options.cascadeSelect){
if ($(ctItem).parent('li').hasClass('ComboTreeItemParent')){
var subMenu = $(ctItem).parent('li').children('ul').first().find('input[type="checkbox"]');
subMenu.each(function() {
var $input = $(this)
if($(ctItem).children('input[type="checkbox"]').first().prop("checked")!==$input.prop('checked')){
$input.prop('checked', !$(ctItem).children('input[type="checkbox"]').first().prop("checked"));
$input.trigger('click');
}
});
}
}
this.refreshInputVal();
};
// recursive search for item in arr
ComboTree.prototype.isItemInArray = function (item, arr) {
for (var i=0; i {
return item.innerHTML.toLowerCase().indexOf(searchText.toLowerCase()) != -1;
})
.each(function (i, elem) {
$(this.children).show()
$(this).siblings("span.comboTreeParentPlus").show();
});
}
else{
this._elemItemsTitle.show();
this._elemItemsTitle.siblings("span.comboTreeParentPlus").show();
}
}
ComboTree.prototype.processSelected = function () {
let elements = this._elemItemsTitle;
let selectedItem = this._selectedItem;
let selectedItems = this._selectedItems;
this.options.selected.forEach(function(element) {
let selected = $(elements).filter(function(){
return $(this).data('id') == element;
});
if(selected.length > 0){
$(selected).find('input').attr('checked', true);
selectedItem = {
id: selected.data("id"),
title: selected.text()
};
selectedItems.push(selectedItem);
}
});
//Without this it doesn't work
this._selectedItem = selectedItem;
this.refreshInputVal();
};
// METHODS
ComboTree.prototype.findItembyId = function(itemId, source) {
if (itemId && source) {
for (let i=0; i0){
var tmpArr = [];
for (i=0; i0){
var tmpArr = [];
for (i=0; i 0) {
for (let i = 0; i < selectionIdList.length; i++) {
let selectedItem = this.findItembyId(selectionIdList[i], this.options.source);
if (selectedItem) {
let check = this.isItemInArray(selectedItem, this.options.source);
if (check) {
var index = this.isItemInArray(selectedItem, this._selectedItems);
if (!index) {
let selectedItemElem = $("#" + this.comboTreeId + 'Li' + selectionIdList[i]);
this._selectedItems.push(selectedItem);
$(selectedItemElem).find("input").prop('checked', true);
}
}
}
}
}
this.refreshInputVal();
};
// EVENTS
ComboTree.prototype.onChange = function(callBack) {
if (callBack && typeof callBack === "function")
this.changeHandler = callBack;
};
// -----
$.fn[comboTreePlugin] = function (options) {
var ctArr = [];
this.each(function () {
if (!$.data(this, 'plugin_' + comboTreePlugin)) {
$.data(this, 'plugin_' + comboTreePlugin, new ComboTree( this, options));
ctArr.push($(this).data()['plugin_' + comboTreePlugin]);
}
});
if (this.length == 1)
return ctArr[0];
else
return ctArr;
}
})( jQuery, window, document );