!function(t){"use strict";function e(e,i){this.el=e,this.$el=t(e),this.options=t.extend({},t.fn[s].defaults,i),this.init()}var s="jqListbox";t.fn[s]=function(i){var n,o=arguments;if(void 0===i||"object"==typeof i)return this.each(function(){t.data(this,"plugin_"+s)||t.data(this,"plugin_"+s,new e(t(this),i))});if(1===this.length&&(n=t.data(this[0],"plugin_"+s))instanceof e){if("function"==typeof n[i])return n[i].apply(n,Array.prototype.slice.call(o,1));if(n.options.hasOwnProperty(i)){if(1===o.length)return n.options[i];n.options[i]=o[1]}}},t.fn[s].defaults={itemSelector:"li",targetInput:"#listbox-value",initialValues:!1,initialEncodedValues:!1,selectedClass:"selected",autoSelectOnClick:!0,multiselect:!0,itemRenderer:function(t,e,s){return"
  • "+t+"
  • "},listboxValueEncoder:function(t){return JSON.stringify(t)},listboxValueDecoder:function(t){return t.length>0?JSON.parse(t):[]},onBeforeInit:!1,onAfterInit:!1,onBeforeItemInsert:!1,onAfterItemInsert:!1,onBeforeItemUpdate:!1,onAfterItemUpdate:!1,onBeforeItemRemove:!1,onAfterItemRemove:!1,onBeforeClear:!1,onAfterClear:!1,onAfterDataChanged:!1,onChanged:!1,onBeforeSelect:!1,onBeforeDeSelect:!1,onBeforeRender:!1,onAfterRender:!1},e.prototype={items:[],selectedPositions:[],init:function(){"function"==typeof this.options.onBeforeInit&&this.options.onBeforeInit.apply(this.$el,[this]),this.items=[],this.selectedPositions=[],this.initItems(),this.options.autoSelectOnClick&&this.setupAutoClickHandler(),this.render(),"function"==typeof this.options.onAfterInit&&this.options.onAfterInit.apply(this.$el,[this])},setupAutoClickHandler:function(){var e=this;this.$el.on("click",this.options.itemSelector,function(s){var i=t(this).index();return e.isSelected(i)?e.deselect(i):e.select(i),s.preventDefault(),!1})},reset:function(){this.clear(),this.initItems(),this.render()},initItems:function(){!1!==this.options.initialValues?this.setFromArray(this.options.initialValues):!1!==this.options.initialEncodedValues?this.setFromTargetValue(this.options.initialEncodedValues):!1!==this.options.targetInput&&1===t(this.options.targetInput).length&&this.setFromTargetValue(t(this.options.targetInput).val())},getItemByIndex:function(t){return this.items[t]},testOnAfterDataChanged:function(t){if("function"==typeof this.options.onAfterDataChanged){for(var e=this.items,s=[],i=0;i0&&!1!==(t=this.testOnBeforeItemUpdate(this.getSelectedItems(),[t]))){for(t=t[0],e=0;e0&&t.length>0&&!1!==(t=this.testOnBeforeItemUpdate(this.getSelectedItems(),t))){for(e=0;e=t.length&&(s=0);this.testOnAfterDataChanged("update"),this.render(),this.testOnAfterItemUpdate(this.getSelectedItems(),t),this.testOnChanged("update")}return this.selectedPositions.length},updateAt:function(t,e){if(e<0&&(e=this.items.length+e),e>=0&&e0&&!1!==(t=this.testOnBeforeItemRemove(this.selectedPositions))&&0!==t.length){for(n=0;n-1&&this.selectedPositions.splice(e,-1),this.items.splice(t,1),this.testOnAfterDataChanged("remove"),this.render(),this.testOnAfterItemRemove(),this.testOnChanged("remove")),this.items.length},clear:function(){if("function"==typeof this.options.onBeforeClear&&!1===this.options.onBeforeClear.apply(this.$el,[this]))return!1;this.items=[],this.selectedPositions=[],this.render(),"function"==typeof this.options.onAfterClear&&this.options.onAfterClear.apply(this.$el,[this]),this.testOnChanged("clear")},moveup:function(){var t,e;if(this.selectedPositions.length>0){if(0===this.selectedPositions[0])this.shiftElementsUp();else if(1===this.selectedPositions.length)this.switchElements(this.selectedPositions[0],this.selectedPositions[0]-1);else for(e=this.selectedPositions.slice(),t=0;t0){if(this.selectedPositions[this.selectedPositions.length-1]===this.items.length-1)this.shiftElementsDown();else if(1===this.selectedPositions.length)this.switchElements(this.selectedPositions[0],this.selectedPositions[0]+1);else for((e=this.selectedPositions.slice()).reverse(),t=0;t0&&((t=this.items.slice(1)).push(this.items[0]),this.items=t.slice(),this.selectedPositions.length>0)){for(e=[],s=0;s0&&(t=[this.items.pop()].concat(this.items.slice()),this.items=t.slice(),this.selectedPositions.length>0)){for(e=[],this.selectedPositions[this.selectedPositions.length-1]===this.items.length-1?e.push(0):e.push(this.selectedPositions[this.selectedPositions.length-1]+1),s=0;s0){var s=this.getSelectedItems(),i=0;for(i=0;i=0&&e0){e.sort(function(t,e){return t-e});var i;if(!0!==s)for(i=0;i0&&(this.selectedPositions=[]),this.selectedPositions.push(t),this.selectedPositions.sort(),this.render()}},deselect:function(t){var e=this.selectedPositions.indexOf(t);if(e>-1){if("function"==typeof this.options.onBeforeDeSelect&&!1===this.options.onBeforeDeSelect.apply(this.$el,[t,this]))return!1;this.selectedPositions.splice(e,1),this.render()}},selectAll:function(){var t;if(!0===this.options.multiselect){for(this.selectedPositions=[],t=0;t-1},this)},getSelectedJQueryItems:function(){var t=this;return this.$el.find(this.options.itemSelector).filter(function(e,s){return t.selectedPositions.indexOf(e)>-1})},getJQueryItemByIndex:function(t){return this.$el.find(this.options.itemSelector).eq(t)},render:function(){var e,s,i,n;if("function"==typeof this.options.onBeforeRender&&!1===this.options.onBeforeRender.apply(this.$el,[this]))return!1;for(e=this.getTargetValue(),!1!==this.options.targetInput&&t(this.options.targetInput).val(e),this.$el.empty(),s=0;s0)for(e=0;e0)for(e=this.$el.find(this.options.itemSelector),s=0;s