2024-04-04 14:37:20 +02:00
|
|
|
(function($){$.fn.asmSelect=function(customOptions){var options={listType:"ol",sortable:false,addable:true,deletable:true,highlight:false,fieldset:false,animate:false,addItemTarget:"bottom",hideWhenAdded:false,hideWhenEmpty:false,debugMode:false,jQueryUI:true,hideDeleted:true,deletedOpacity:.5,deletedPrepend:"-",useSelect2:true,removeWhenAdded:false,highlightTag:"<span></span>",sortLabel:'<span class="asmIcon asmIconSort">↕</span>',removeLabel:'<span class="asmIcon asmIconRemove">×</span>',highlightAddedLabel:"Added: ",highlightRemovedLabel:"Removed: ",containerClass:"asmContainer",selectClass:"asmSelect",optionDisabledClass:"asmOptionDisabled",listClass:"asmList",listSortableClass:"asmListSortable",listItemClass:"asmListItem",listItemLabelClass:"asmListItemLabel",listItemDescClass:"asmListItemDesc",listItemStatusClass:"asmListItemStatus",listItemHandleClass:"asmListItemHandle",removeClass:"asmListItemRemove",editClass:"asmListItemEdit",highlightClass:"asmHighlight",deletedClass:"asmListItemDeleted",editLink:"",editLabel:'<span class="ui-icon ui-icon-extlink"></span>',editLinkOnlySelected:true,editLinkModal:true,editLinkButtonSelector:"form button.ui-button:visible",optionParentClass:"asmParent",optionParentIcon:"⬇",optionChildAttr:"data-asmParent",optionParentOpenClass:"asmParentOpen",optionChildClass:"asmChild",optionChildIndent:" ",optionParentLabel:"← Click again to make selection"};$.extend(options,customOptions);return this.each(function(index){var $original=$(this);var $container;var $select;var $select2=null;var $ol;var buildingSelect=false;var ieClick=false;var ignoreOriginalChangeEvent=false;var fieldsetCloseItems={};var msie=0;var $highlightSpan=null;function init(){if(options.deletable&&!options.addable)options.hideDeleted=false;$original.find("option[selected]").addClass("asmOriginalSelected");while($("#"+options.containerClass+index).length>0)index++;$select=$("<select></select>").addClass(options.selectClass).addClass($original.attr("class")).attr("name",options.selectClass+index).attr("id",options.selectClass+index);if(!options.addable)$select.hide();$selectRemoved=$("<select></select>");$ol=$("<"+options.listType+"></"+options.listType+">").addClass(options.listClass).attr("id",options.listClass+index);$container=$("<div></div>").addClass(options.containerClass).attr("id",options.containerClass+index);buildSelect();$select.on("change",selectChangeEvent).on("click",selectClickEvent);$original.on("change",originalChangeEvent).wrap($container).before($select).before($ol);if(options.sortable)makeSortable();if(options.fieldset){setupFieldsets();findFieldsetCloseItems($original);$original.on("rebuild",function(e){console.log("asmSelect REBUILD");findFieldsetCloseItems($(this))})}$original.trigger("init");if(options.editLinkModal==="longclick"){$ol.on("longclick","a.asmEditLinkModalLongclick",clickEditLink)}if($select2&&$select2.length){$select2.addClass($select.attr("class")).removeClass("asmSelect").attr("id",$select.attr("id")+"-helper").hide();$select.after($select2)}}function makeSortable(){var fieldsetItems=[];var sortableUpdate=function($ul,e,data){var $option=$("#"+data.item.attr("rel"));var updatedOptionId=$option.attr("id");$ul.children("li").each(function(n){$option=$("#"+$(this).attr("rel"));$original.append($option)});if(updatedOptionId){triggerOriginalChange(updatedOptionId,"sort")}};$ol.sortable({items:"li."+options.listItemClass,axis:"y",cancel:"a.asmEditLinkModalLongclick",update:function(e,data){if(data.item.hasClass("asmFieldsetStart"))return;sortableUpdate(jQuery(this),e,data);$ol.trigger("sorted",[data.item])},start:function(e,data){if(options.jQueryUI)data.item.addClass("ui-state-highlight");if(data.item.hasClass("asmFieldsetStart")){var $next=data.item;var stopName=data.item.find("."+options.listItemLabelClass).text()+"_END";do{if($next.find("."+options.listItemLabelClass).text()==stopName)break;$next=$next.next("li");if($next.length&&!$next.hasClass("ui-sortable-placeholder")){$next.fadeTo(50,.7).slideUp("fast");fieldsetItems.push($next)}}while($next.length)}
|