(function($){$.widget("lws.lac_select",{options:{classe:'',name:'',placeholder:'',shared:!1,delay:300,maxwidth:'',minlength:1,minoptions:2,minsearch:1,showMore:!1,mode:'autocomplete',required:!1,allownew:!1,},_create:function(){this._setOptions();this._createStructure();this.currentIndex=-1;this.flagAutocomplete=!1;this.inHouse=!1;this.initValue=(this.element.val()!=undefined)?[this.element.val()]:undefined;this.resLoad={'target':this,'fn':this._ajaxLoading};this.resChange={'target':this,'fn':this._resultChanged};this._manageModel();if(this.element.val()==undefined||this.element.val().length==0){this.resList=$(this.model).lac_model("getSource");if(!jQuery.isEmptyObject(this.resList))
this._setResList(this.resList);}
this._manageEvents();var me=this;var timer;var keyData=[];this.container.on('keyup',function(event,data){keyData[0]=event.key;keyData[1]=event.keyCode;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('required')!=undefined)
this.options.required=this.element.data('required');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('name')!=undefined)
this.options.name=this.element.data('name');if(this.element.data('maxwidth')!=undefined)
this.options.maxwidth=this.element.data('maxwidth');if(this.element.data('shared')!=undefined)
this.options.shared=this.element.data('shared');if(this.element.data('mode')!=undefined)
this.options.mode=this.element.data('mode');if(this.element.data('allownew')!=undefined)
this.options.allownew=(this.element.data('allownew').trim().length>0);},_destroy:function(){if(this.container.css('display')!='none')
this.element.show();this.container.remove()},_createStructure:function(){var ignoreConfirm='';if(this.element.hasClass('lws-ignore-confirm'))
ignoreConfirm='lws-ignore-confirm ';let reqborder=(this.options.required)?'lws-reqborder':'';let maxwidth=(this.options.maxwidth)?'max-width:'+this.options.maxwidth:'';this.container=$("<div>",{'class':'lac-select-wrapper '+this.options.classe,'style':maxwidth,'tabIndex':'-1','data-uuid':this.uuid,}).append($("<div>",{'class':'lac-select-combo '+reqborder,}).append($("<div>",{'class':ignoreConfirm+'lac-select-input','contenteditable':'true','tabIndex':'0','data-placeholder':this.options.placeholder,'name':this.options.name})).append($("<div>",{'class':'lac-loading'})).append($("<div>",{'class':'lac-select-showmore lws-icon lws-icon-show-more'})).append($("<div>",{'class':'lac-select-ddbutton lws-icon lws-icon-nav-down'}))).append($("<div>",{'class':'lac-select-list','tabIndex':'-1','data-open':!1})).append($("<div>",{'class':'lac-select-error'})).insertAfter(this.element);if(this.element.css('display')=='none')
this.container.hide();this.element.hide();this.showMoreButton=this.container.find('.lac-select-showmore');this.selectList=this.container.find('.lac-select-list');this.textInput=this.container.find('.lac-select-input');this.textInput.prop('autocomplete','off');if(this.options.mode==='select'){this.textInput.css('user-select','none');this.textInput.prop('contenteditable','false')}
if(this.options.required)this.textInput.lws_required();},_manageEvents:function(){if(this.element.prop('disabled')){this.textInput.prop('disabled',!0);this.container.find('.lac-select-ddbutton').addClass('lws-disabled');this.container.off()}else{this.container.find('.lac-select-ddbutton').removeClass('lws-disabled');this.container.on('click','.lac-select-ddbutton',this._bind(this._showHide,this));this.container.on('click','.lac-select-showmore',this._bind(this._showMore,this));this.container.on('click','.lac-select-item',this._bind(this._selectItem,this));this.container.on('focusout',this._bind(this._manageFocus,this));this.container.on('click','.lac-select-input',this._bind(this._manageInputFocus,this));this.container.on('keydown',this._bind(this._manageKeys,this));this.element.on('change',this._bind(this._changeElement,this))}},currentValue:function(){return this.element.val()},currentText:function(){return this.textInput.html()},_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':this.options.mode,'origin':this});if(this.initValue!=undefined){$(this.model).lac_model("returnLabels",this.initValue,this,this.options.mode)}},_recursiveList:function(resList,depth){var resultList=[];var me=this;$.each(resList,function(key,selItem){if(selItem.group!=undefined){let retour=me._recursiveList(selItem.group,depth+1)
if(retour.length>0){if(retour[0][0].className!='lac-select-optgroup'){var item=$("<div>",{'class':'lac-select-optgroup','data-value':selItem.value,'html':selItem.label,});resultList.push(item)}
resultList=$.merge(resultList,retour)}}else{me.selectIndex+=1;var item=$("<div>",{'class':'lac-select-item lac-item-'+me.selectIndex,'data-value':selItem.value,'data-label':selItem.label,'data-index':me.selectIndex,});if(selItem.html!=undefined&&selItem.html!=''){item.html(selItem.html)}else{item.html(selItem.label)}
if(depth>0){arrow=$("<div>",{'class':'lac-item-arrow lws-icon lws-icon-arrow-right'});item.prepend(arrow)}
resultList.push(item)}})
return resultList},_setResList:function(resList){this.selectList.empty();this.currentIndex=-1;this.selectIndex=-1;var retour=this._recursiveList(resList,0);for(var i=0;i<retour.length;i++){retour[i].appendTo(this.selectList)}
this.container.find('.lac-select-item').removeClass("lac-highlighted")},_selectItem:function(event){let item=$(event.currentTarget);this._changeValue(item.data('value'));this.textInput.text($('<span>').html(item.data('label')).text());this.currentIndex=item.data('index');this._closeList();this.element.trigger('updated')},_changeElement:function(event){if(this.inHouse===!0){this.inHouse=!1;this.textInput.trigger('focus')}else{{$(this.model).lac_model("returnLabels",[this.element.val()],this)}}},_changeValue:function(value){this.inHouse=!0;this.element.val(value);this.element.trigger("change")},_showHide:function(event,data){if(!this.selectList.data('open')){if(this.options.mode!='select'){this.flagAutocomplete=!0}
$(this.model).lac_model("returnSource")}else{this._closeList();return!1}},_showMore:function(event,data){$(this.model).lac_model("showMoreResults",this)},_openList:function(){if(this.showMore)this.showMoreButton.show();this.selectList.data('open',!0);this.selectList.show()},_closeList:function(){this.showMoreButton.hide();this.selectList.data('open',!1);this.selectList.hide()},_manageInputFocus:function(event){if(this.options.mode=='select'){if(!this.selectList.data('open')){$(this.model).lac_model("returnSource")}else{this._closeList()}}},_manageFocus:function(event,data){if(!event.originalEvent)return;var target=$(event.originalEvent.relatedTarget).closest('.lac-select-wrapper');if(target.length>0&&target.data('uuid')==this.uuid)return;this._handleNewOrIncorrectValue();this._closeList()},_handleNewOrIncorrectValue:function(){if(this.textInput.text()!=''&&!this._isInResList(this.element.val(),this.textInput.text())){if(this.options.allownew!=undefined&&this.options.allownew==!0){var key=this.textInput.text();var newVal=[];newVal[key]=key;$(this.model).lac_model("addToSource",newVal,this);this._changeValue(key)}else{this.textInput.text('');this._showError('Incorrect Value');this.element.trigger('updated')}}},_isInResList:function(val,label){var sanitizer=$('<span>');for(const selItem of this.resList){if(selItem.value==val&&sanitizer.html(selItem.label).text()==label){return!0}else if(selItem.group!=undefined){for(const groupItem of selItem.group){if(groupItem.value==val&&sanitizer.html(groupItem.label).text()==label)
return!0}}}
return!1},_manageSearch:function(data){if(data[0].length==1&&this.options.mode!='select'){this.currentIndex=-1;$(this.model).lac_model("research",this.textInput.text(),this)}},_showError:function(msg){this.container.find(".lac-select-error").html(msg).show().delay(1000).fadeOut(500)},_manageKeys:function(event,data){if(event.key=='ArrowDown'){this.container.find('.lac-item-'+this.currentIndex).removeClass("lac-highlighted");this.currentIndex=(this.currentIndex+1>this.selectIndex)?0:this.currentIndex+1;var item=this.container.find('.lac-item-'+this.currentIndex);item.addClass("lac-highlighted");this.textInput.text(item.data('label'));this.selectList.scrollTop(Math.floor(item.position().top)+Math.floor(this.selectList.scrollTop()));this._changeValue(item.data('value'));if(!this.selectList.data('open')){this._openList()}
this.element.trigger('updated');return!1}
if(event.key=='ArrowUp'){this.container.find('.lac-item-'+this.currentIndex).removeClass("lac-highlighted");this.currentIndex=(this.currentIndex-1<0)?this.selectIndex:this.currentIndex-1;var item=this.container.find('.lac-item-'+this.currentIndex);item.addClass("lac-highlighted");this.textInput.text(item.data('label'));this.selectList.scrollTop(Math.floor(item.position().top)+Math.floor(this.selectList.scrollTop()));this._changeValue(item.data('value'));if(!this.selectList.data('open')){this._openList()}
this.element.trigger('updated');return!1}
if(event.key=='Enter'||event.key=="Escape"||event.key=="Tab"){this._handleNewOrIncorrectValue();this._closeList()}
if(event.key=='Backspace'||event.key=="Delete"||event.key=="Suppr"){if(this.options.mode=='select')return;this.currentIndex=-1;this._changeValue('');var me=this;setTimeout(function(){if(me.textInput.text().length==0){me.resList=$(me.model).lac_model("getSource");me._setResList(me.resList)}else{me.flagAutocomplete=!0;$(me.model).lac_model("research",me.textInput.text(),me)}},50)}},_inputSelectRange:function(){if(this.textInput[0].setSelectionRange){var searchString=this.textInput.text();this.textInput.trigger('focus');if(this.options.mode!='research'){var item=this.container.find('.lac-item-'+this.currentIndex);this.textInput.text(item.data('label'));this.textInput[0].setSelectionRange(searchString.length,this.textInput.text().length);this._changeValue(item.data('value'));this.element.trigger('updated')}}},_ajaxLoading:function(){this.container.find(".lac-loading").append($("<div>",{'class':'animation'}))},_resultChanged:function(data){this.container.find(".lac-loading").find(".animation").remove();if(data[1]=='ok'){if(!jQuery.isEmptyObject(data[0])){this.resList=data[0];this._setResList(this.resList);if(!this.flagAutocomplete){this.currentIndex=0;this._inputSelectRange();this.container.find('.lac-item-'+this.currentIndex).addClass("lac-highlighted")}
this.flagAutocomplete=!1;this.showMore=data[2];if(!this.showMore)this.showMoreButton.hide();this._openList()}else{this._closeList();this.selectList.empty()}}else if(data[1]=='init'){if(!jQuery.isEmptyObject(data[0])){this.resList=data[0];this._setResList(this.resList);this.currentIndex=0;this.showMore=data[2];if(!this.showMore)this.showMoreButton.hide();this.textInput.text(this.selectList.find('.lac-item-0').data('label'));if(this.options.mode=='select'){this.resList=$(this.model).lac_model("getSource");this._setResList(this.resList)}
this.element.trigger('init')}}else if(data[1]=='force'){}else{if(!jQuery.isEmptyObject(data[0])){this.resList=data[0];this._setResList(this.resList);this._showError(data[1])}else{this._closeList();this.selectList.empty();this._showError(data[1])}}},enableControl:function(){if(this.element.prop('disabled')==!0){this.element.prop('disabled',!1);this._manageEvents()}},disableControl:function(){if(this.element.prop('disabled')==!1){this.element.prop('disabled',!0);this._manageEvents()}},reInitSource:function(){if(!this.options.shared){let value=this.element.val();$(this.model).lac_model("reInit",this);if(undefined!=value){$(this.model).lac_model("returnLabels",[value],this,this.options.mode)}}}})})(jQuery)
jQuery(function($){$(".lac_select").lac_select()})