(function(){"use strict";var e={Edit:["Save","Cancel"],Read:["Edit","ShowMenu"]};var t=e.Edit.concat(e.Read);function i(){return 1e3+Math.random()*1e3}u8.Crm.Widgets.RecordItemView=function(e,i){this.Widget(e,$.extend({},u8.Crm.Widgets.RecordItemView.defaults,i));this._isCreated=false;this._selected=false;var s=this._onCommandQueryState.bind(this);this.commandBindings=t.reduce(function(e,t){e[t]={queryState:s};return e},{});this._create()};u8.Crm.Widgets.RecordItemView.defaults={theme:{css:{root:"u8-recorditemview",selected:"selected",loading:"loading"}}};u8.Crm.Widgets.RecordItemView.makeClass({name:"RecordItemView",events:["Create","Click"],deriveFrom:u8.Base.Widgets.Widget});$.extend(u8.Crm.Widgets.RecordItemView.prototype,{dispose:function(){if(this._isDisposed){return this}var e=this.theme.css;this.$(this.element).removeClass(e.root+" "+e.selected+" "+e.loading).off("."+this.id);this.Widget_dispose()},apply:function(e){if(!this._isCreated){if(e){e()}return}var t=this;var i=this.getWidget("expand");if(!i.isModified()){i.setViewMode("Read");if(e){e(t,{})}return}i.model.save(function(i,s){if(e){e(t,s)}})},setSelected:function(e){if(this._selected===e){return}this._selected=e;this.element.classList.toggle(this.theme.css.selected,this._selected)},getSelected:function(){return this._selected},_create:function(){this._createModel();this._createView();this._updateDraggability();u8.tools.createdWhenReady(this)},_createModel:function(){this._model={expandView:{id:this.id+"_expand",model:{uid:this.uid,expandName:this.expandName},header:{level:3},showExtras:false,showBreadCrumbs:false,allowReorder:false,parent:this,onPreViewModeChange:this._onExpandPreViewModeChange.bind(this),onViewModeChange:this._onExpandViewModeChange.bind(this)}}},_createView:function(){var e=this.theme.css;this.$(this.element).addClass(e.root+" "+e.loading).on("click."+this.id,this._onClick.bind(this));if(this.deferExpandView){setTimeout(this._createExpandView.ifAlive(this),i())}else{this._createExpandView()}},_createExpandView:function(){var e=this;var t=this.$("<div />").appendTo(this.element);var i=t.widget("ExpandView",this._model.expandView)[0];u8.tools.ready(i,function(){e.$(e.element).removeClass(e.theme.css.loading)})},_updateDraggability:function(){this.$(this.element).attr("draggable",this._getViewMode()!="Edit")},_getViewMode:function(){var e=this.getWidget("expand");return e&&e.getViewMode()},_onClick:function(e){if(this._getViewMode()==="Edit"){return}this.raise("Click",{elEvent:e})},_onExpandPreViewModeChange:function(e,t){if(t.viewMode==="Edit"&&!this.getSelected()){t.cancel=true}},_onExpandViewModeChange:function(){this._updateDraggability()},_onCommandQueryState:function(i,s,d){if(!t.contains(s)){return}var n=this._getViewMode();var a=e[n];d.visible=a.contains(s)}})})();