(function($){$.widget("lws.lac_checklist",{options:{classe:'',name:'',placeholder:'',shared:!1,addlabel:'Add',delay:300,minlength:0,minoptions:2,minsearch:1,rows:3},_create:function(){this.checkedList=new Map();this.preventNext=!1;this.initList=undefined;if(this.element.data('value')!=undefined&&this.element.data('value').trim()!='')
this.initList=lwsBase64.toObj(this.element.data('value'));this.name=this.element.prop("name");this.element.data('lw_name',this.name).prop('name','');this.showMore=!1;this._setOptions();this.container=this.element.next();this.topLine=this.container.find('.top-line');this.selectList=this.container.find('.result-list');this.tagsList=this.container.find('.values-list');this.textInput=this.container.find('.search-input');this.resLoad={'target':this,'fn':this._ajaxLoading};this.resChange={'target':this,'fn':this._resultChanged};this.searchOriginalWidth=this.container.find('.search-div').css('width');this._manageModel();this.resList=$(this.model).lac_model('getSource');this._setResList(this.resList);this.container.addClass(this.options.classe);this.container.on('click','.tag-remove',this._bind(this._delTag,this));this.container.on('click','.resicon',this._bind(this._toggleCheckList,this));this.container.on('click','.selector',this._bind(this._openCheckList,this));this.container.on('click','.reset-btn',this._bind(this._resetSearch,this));this.container.on('click','.showmore',this._bind(this._showMore,this));this.container.on('click','.close-btn',this._bind(this._closeCheckList,this));this.container.on('click','.item-expand',this._bind(this._optGroupDisplay,this));this.container.on('click','.item',this._bind(this._selectItem,this));this.container.on('keydown',this._bind(this._manageKeys,this));this.element.on('change',this._bind(this._changeElement,this));$(document).on('click',this._bind(this._manageFocus,this));var me=this;var timer;var keyData=[];this.container.on('keyup',function(event,data){keyData[0]=event.key;keyData[1]=event.keyCode;var sentData=[keyData];timer&&clearTimeout(timer);timer=setTimeout(me._bindD(me._manageSearch,me,sentData),me.options.delay)})},_bind:function(fn,me){return function(){return fn.apply(me,arguments)}},_bindD:function(fn,me,data){return function(){return fn.apply(me,data)}},_setOptions:function(){if(this.element.data('placeholder')!=undefined)
this.options.placeholder=this.element.data('placeholder');if(this.element.data('delay')!=undefined)
this.options.delay=this.element.data('delay');if(this.element.data('class')!=undefined)
this.options.classe=this.element.data('class');if(this.element.data('shared')!=undefined)
this.options.shared=this.element.data('shared');if(this.element.data('name')!=undefined)
this.options.name=this.element.data('name');},_manageModel:function(){if(this.options.shared){if($('#sha-'+this.options.shared).length){this.model=$('#sha-'+this.options.shared)}else{this.model=$('<input>',{'id':'sha-'+this.options.shared,'type':'hidden'}).appendTo($('body'))}}else{this.model=this.element}
$(this.model).lac_model({'mode':'research','origin':this});if(this.initList!=undefined)
$(this.model).lac_model("returnLabels",this.initList,this);},_changeElement:function(event,internal){if(undefined!=internal&&('self'==internal||'ignore'==internal))
return;this.checkedList.clear();this.selectList.empty();this.tagsList.empty();this.initList=undefined;if(this.element.data('value')!=undefined&&this.element.data('value').trim()!='')
this.initList=lwsBase64.toObj(this.element.data('value'));if(this.initList==undefined||!this.initList.length)
this._updateList(!0);$(this.model).lac_model("returnLabels",this.initList,this)},addToSource:function(valueToAdd,checked){if(checked==undefined)checked=!1;var newVal=[];newVal[valueToAdd]=valueToAdd;$(this.model).lac_model("addToSource",newVal,this);if(checked!=!1&&checked!=undefined){this.resList=$(this.model).lac_model('getSource');this.checkedList.set(valueToAdd.toString(),valueToAdd);this._setResList(this.resList);this._fillTagList();this._updateList()}},_openCheckList:function(){this.resList=$(this.model).lac_model("getSource");if(jQuery.isEmptyObject(this.resList)){this._ajaxLoading();$(this.model).lac_model("research",'',this,!0)}else{this._setResList(this.resList)}
this.topLine.addClass('open')
this.selectList.data('open',!0);this.selectList.css('display','grid')},_closeCheckList:function(){if(this.selectList.data('open')==!0){this.topLine.removeClass('open')
this.selectList.data('open',!1);this.selectList.css('display','none');this.textInput.text('');this.resList=$(this.model).lac_model('getSource');this._setResList(this.resList)}},_resetSearch:function(){this.textInput.text('');this.resList=$(this.model).lac_model('getSource');this._setResList(this.resList);this.textInput.trigger('focus')},_recursiveList:function(resList){var resultList=[];for(var key in resList){if(resList[key].group!=undefined){let retour=this._recursiveList(resList[key].group);if(retour.length>0){resultList.push($("<div>",{'class':'item expanded lac-optgroup','data-value':resList[key].value}).append($("<div>",{'class':'item-label','html':resList[key].label})).append($("<div>",{'class':'item-expand lws-icon-minus'})));var item=$("<div>",{'class':'optgroup-grid',});for(var i=0;i<retour.length;i++){item.append(retour[i])}
resultList.push(item)}}else{let checked='';let icon=' lws-icon-checkbox-unchecked';if(this.checkedList.has(resList[key].value)){checked=' item-selected';icon=' lws-icon-checkbox-checked'}
resultList.push($("<div>",{'class':'item '+checked,'data-value':resList[key].value}).append($("<div>",{'class':'item-cb'+icon})).append($("<div>",{'class':'item-label','html':resList[key].label})))}}
return resultList},_setResList:function(resList){this.selectList.empty();var results=this._recursiveList(resList);var hasOptGroups=!1;for(var i=0;i<results.length;i++){if(results[i].hasClass('lac-optgroup'))
hasOptGroups=!0;results[i].appendTo(this.selectList)}
$(this.selectList).toggleClass('has-optgroups',hasOptGroups);if(this.showMore)
this._addShowMoreButton();},_optGroupDisplay:function(event){let button=$(event.currentTarget);if($(button).hasClass('lws-icon-minus')){$(button).removeClass('lws-icon-minus').addClass('lws-icon-plus');$(button).parent().next('.optgroup-grid').css('display','none')}else{$(button).removeClass('lws-icon-plus').addClass('lws-icon-minus');$(button).parent().next('.optgroup-grid').css('display','grid')}},_selectItem:function(event,data){this.preventNext=!0;var item=$(event.currentTarget);if(item.hasClass("item-selected")){item.removeClass('item-selected');item.find('.item-cb').removeClass('lws-icon-checkbox-checked').addClass('lws-icon-checkbox-unchecked')
this.checkedList.delete(item.data('value').toString())}else{item.addClass('item-selected');item.find('.item-cb').removeClass('lws-icon-checkbox-unchecked').addClass('lws-icon-checkbox-checked')
this.checkedList.set(item.data('value').toString(),item[0].innerText)}
this._fillTagList();this._updateList();this.element.trigger('change',['self'])},_manageSearch:function(data){if(this.textInput.text()!=''){$(this.model).lac_model("research",this.textInput.text())}else{if(data[0]!="Tab")
this._resetSearch();}},_manageKeys:function(event,data){if(event.key=='Backspace'||event.key=="Delete"||event.key=="Suppr"){}
if(event.key=="Escape"||event.key=="Enter"||event.key=="Tab"){this._closeCheckList()}},_manageFocus:function(event){if(this.selectList.data('open')==!0){if($(event.originalEvent.target).closest('.lac-checklist-wrapper').length==0){this._closeCheckList()}}},_showError:function(msg){this.container.find(".lac-checklist-error").html(msg).show().delay(1000).fadeOut(500)},_addShowMoreButton:function(){this.showMoreButton=$("<div>",{'class':'showmore',}).append($("<div>",{'class':'smicon lws-icon lws-icon-plus'})).append($("<div>",{'class':'smtext','text':'Show More'})).appendTo(this.selectList)},_showMore:function(event,data){$(this.model).lac_model("showMoreResults",this)},_ajaxLoading:function(){this.textInput.find(".lac-loading").append($("<div>",{'class':'animation'}))},_resultChanged:function(data){this.textInput.find(".lac-loading").find(".animation").remove();if(data[1]=='ok'){if(!jQuery.isEmptyObject(data[0])){this.resList=data[0];this.showMore=data[2];this._setResList(this.resList);this.selectList.data('open',!0);this.selectList.css('display','grid')}else{this.resList=[];this.selectList.empty();this.selectList.data('open',!1);this.selectList.hide()}}else if(data[1]=='init'){if(!jQuery.isEmptyObject(data[0])){var me=this;$.each(data[0],function(index,obj){me.checkedList.set(obj.value.toString(),obj.label)});this.resList=$(this.model).lac_model('getSource');this.showMore=data[2];this._setResList(this.resList);this._fillTagList();this._updateList()}}else if(data[1]=='force'){}else{this.resList=data[0];this._setResList(this.resList);this._showError(data[1])}},_fillTagList:function(){this.tagsList.empty();var me=this;this.checkedList.forEach(function(value,key){$("<div>",{'class':'tag-wrapper','data-value':key}).append($("<div>",{'class':'tag-text','html':value})).append($("<a>",{'class':'tag-remove lws-icon-cross'})).appendTo(me.tagsList)})},_delTag:function(event){var theTag=$(event.currentTarget).parent();this.checkedList.delete(theTag.data('value').toString());theTag.remove();this._updateList();this._setResList(this.resList);this.element.trigger('change',['self'])},_updateList:function(silent){var valuesContainer=this.container.find('.values');valuesContainer.empty();var me=this;var keyTable=[];this.checkedList.forEach(function(value,key){$("<input>",{'type':'hidden','name':me.name+'[]','value':key,'data-lw_dependant':me.name}).appendTo(valuesContainer);keyTable.push(key)});this.element.data('value',lwsBase64.fromObj(keyTable));if(undefined==silent||!1==silent)
this.element.trigger('change',['ignore']);}})})(jQuery)
jQuery(function($){$(".lac_checklist").lac_checklist()})