(function(){"use strict";u8.Base.Widgets.ListInputItem=function(t,e){this.Widget(t,$.extend({},u8.Base.Widgets.ListInputItem.defaults,e));this[this.imageProperty]=this.image||"";this[this.textProperty]=this.text||"";this[this.keyProperty]=this.key||"";this._isCreated=false;this._create()};u8.Base.Widgets.ListInputItem.defaults={tabIndex:0,actions:[{name:"itemDelete",image:"u8-img-delete-xs"}],theme:{css:{item:"u8-listinput-item",actions:"u8-listinput-item-actions",action:"u8-listinput-item-action"}},separator:","};u8.Base.Widgets.ListInputItem.makeClass({name:"ListInputItem",events:["Create","Change","Click","Delete","KeyDown","PreChange","PreEdit","Select"],deriveFrom:u8.Base.Widgets.Widget,mixins:[u8.Base.Widgets.ActionsMixIn],attributes:{supportsActions:true}});$.extend(u8.Base.Widgets.ListInputItem.prototype,{dispose:function(){if(this._isDisposed){return}if(this._$input){this._$input.off(".ListInputItemInput")}this.$(this.element).off(".ListInputItem");this.Widget_dispose()},edit:function(t,e){var i=this;var s=this.parent.getSearchProvider();if(!this.allowInput||this.disabled||!this.allowNewValues&&!s||!this.$(this.element).attr("data-selected")||this._isEditing()){return}this._$label.hide();if(!this._input){this._$input.on("keyup.ListInputItemInput",this._onKeyUp.bind(this));this._input=u8.create("SearchInput",this._$input,{parent:this,textIsValue:false,searchProvider:s,alignDropDownTo:this.alignDropDownTo,autoGrow:{ignoreAutoMinWidth:true},allowNewValues:this.allowNewValues,keepInputValueOnce:true,tabIndex:this.tabIndex,onValueChange:this._onSearchValueChange.bind(this)})}else{this._input.keepInputValueOnce=true;this._input.searchProvider=s;this._$input.css("display","")}this._input.focus();this._$input.find("input").trigger(e);if(t){var n=this[this.textProperty];this._input.withoutEvents(function(){i._input.setText(n)})}},_create:function(){var t=this.$(this.element).addClass(this.theme.css.item).toggleClass("readonly",this.readonly).toggleClass("disabled",!this.readonly&&this.disabled).attr("tabindex",this.tabIndex);if(!this.disabled){t.on("click.ListInputItem",this._onClick.bind(this)).on("keydown.ListInputItem",this._onKeyDown.bind(this))}this._$image=this.$("<div/>").appendTo(t);this._$label=this.$("<div/>").appendTo(t);if(this.allowInput&&!this.readonly&&!this.disabled){this._$input=this.$("<div/>").appendTo(t)}if(!this[this.keyProperty]){this[this.keyProperty]=this.id}if(this[this.imageProperty]){this._image=u8.create("Image",this._$image,{parent:this,source:this[this.imageProperty]})}this._label=u8.create("Label",this._$label,{parent:this,text:this[this.textProperty],title:true,isLink:!!this.asLink&&this.readonly,onClick:this._onLinkClick.bind(this),forInputId:this.forInputId});if(!this.readonly&&!this.disabled){t.append(this["Actions.createView"]());t.find("."+this.theme.css.action).removeAttr("tabindex")}var e=this._label.$(this._label.element);var i=t.find("."+this.theme.css.actions).outerWidth(true);var s=e.outerWidth(true);var n="calc(100% - "+(i+s)+"px)";var a="max-width: -webkit-"+n+"; max-width: -moz-"+n+"; max-width: "+n+";";e.attr("style",(e.attr("style")||"")+a);if(this.readonly){t.append("<span class='u8-label'>"+this.separator+"</span>")}u8.tools.createdWhenReady(this)},_isEditing:function(){return this._$input&&this._$input.is(":visible")},_onAction:function(t){if(!t.elEvent||!t.elEvent.ctrlKey){this.raise("Delete");t.cancelEvent=true}},_onClick:function(t){var e=this.$(t.delegateTarget);if(!this._input){e.focus()}else{this._input.focus()}var i={multiSelect:t.ctrlKey};i[this.keyProperty]=this[this.keyProperty];this.raise("Select",i);t.stopPropagation()},_onKeyDown:function(t){var e=false;if(u8.Base.Dom.Keys.isFunctional(t.keyCode)){if(t.keyCode==9&&this.$(t.target).is("input")){t.stopPropagation();t.preventDefault();return}if(!this._isEditing()){if(t.keyCode==113){e=true}else{this.raise("KeyDown",t);return}}if(t.keyCode==27){t.stopPropagation();this._input.setText("");this._$input.hide();this._$label.css("display","");this.raise("Select");return}}var i={event:t};this.raise("PreEdit",i);if(!i.cancel){this.edit(e,t)}t.stopPropagation()},_onKeyUp:function(t){if(this._input&&this._input.getInputText().length>0&&t.keyCode==13){t.preventDefault();t.stopPropagation()}},_onLinkClick:function(t,e){this.raise("Click",e)},_onSearchValueChange:function(t){var e=t.getValue();if(e&&e[this.textProperty]){var i={};i[this.textProperty]=e[this.textProperty];this.raise("PreChange",i);if(i.cancel){return}var s=this[this.keyProperty];this[this.keyProperty]=e[this.keyProperty];this[this.imagePoperty]=e[this.imageProperty];this[this.textProperty]=e[this.textProperty];this._label.setText(e[this.textProperty]);this._$label.css("display","");this._$input.hide();this.raise("Change",{oldKey:s})}}})})();