(function(){"use strict";u8.Crm.Widgets.ExpandViewExtrasWidget=function(e,i){this.Widget(e,i);this._isCreated=false;this._$header=this.$('<div class="u8-expand-extrasHeader" />').appendTo(this.element);this._$container=this.$("<div/>").attr("tabindex",-1).appendTo(this.element);u8.widget("Header",this._$header,{parent:this,headerName:"ExpandOptions",headerGroupName:i.expandView.model.properties.headerGroupName,onLinkChange:this._onLinkChange.bind(this),onPreCreate:this._onHeaderPreCreate.bind(this),onCreate:this._onHeaderCreate.bind(this),onCollapsedChange:this._onHeaderCollapsedChange.bind(this)})};$.extend(u8.Crm.Widgets.ExpandViewExtrasWidget.prototype,{update:function(){var e=this.$;this._$container.css("visibility","hidden").children("[data-extrasid]").each(function(){var i=e(this).data("extras-widget");if(i){i.update()}});this._updateHeaderLinks();u8.tools.ready(true,this,function(){this._setCurrentWidget(this._header.linkMessageNr);this._$container.css("visibility","visible");this._updateVisibility()}.bind(this))},isEmpty:function(){var e=this._header,i=e&&e.model&&e.model.links;var t=(i||[]).filter(u8._.neq("visible",false));if(t.length<=0){return true}var s=true;for(var a=0;s&&a<t.length;a++){var n=this.getWidget(t[a].messageNr);if(!n||!n.isEmpty()){s=false}}return s},isModified:function(){var e=false,i=this.$;this._$container.children("[data-extrasid]").each(function(){var t=i(this).data("extras-widget");if(t&&t.isModified()){e=true}});return e},dispose:function(){if(this._isDisposed){return}this._$container=null;this._$header=null;this.Widget_dispose()},getWidget:function(e){var i=this._$container.children("[data-extrasid="+e+"]");if(i.length){return i.data("extras-widget")}return this.Widget_getWidget(e)},_updateVisibility:function(){var e=this.$(this.element),i=!this.isEmpty(),t=e.is(":visible");if(!i&&u8.session.webConfigValues.getBoolean("ExpandViewExtrasWidget.DoNotHideIfEmpty",false)){return}e.css("visibility",i?"inherit":"hidden");if(i&&t!=i){this.children.layout()}},_updateHeaderLinks:function(e){if(!e&&!this._header){return}var i=(e||this._header).model;if(!i||!i.links){return}var t=this.expandView.model,s=t&&t.mode=="New";var a=false;var n=s?function(e){return e.messageNr!=1&&e.messageNr!=2}:u8._.val(true);for(var r=0;r<i.links.length;r++){var d=i.links[r];var h=d.visible;d.visible=n(d);a=a||h!=d.visible}if(!e){if(a){this._header.linkMessageNr=-1}this._header.update({links:true})}},_setCurrentWidget:function(e,i){if(e==-1){return i&&i()}this._$container.children("[data-extrasid]").hide();var t=this._$container.children("[data-extrasid="+e+"]");if(t.length){t.show();return i&&i()}var s=this;var a=u8.Crm.Widgets.ExpandView.extras[e];if(typeof a=="string"){return u8.using({types:[a]},function(){var n=u8.services.factory.getType(a);if(n){a=n.constructor_;u8.Crm.Widgets.ExpandView.extras[e]=a;s._setCurrentWidget(e,i)}else{t.errorMessage("Failed to create extras widget for unknown type '"+a+"'!")}}.ifAlive(this))}t=this.$('<div class="u8-expand-extra" data-extrasid="'+e+'" />').appendTo(this._$container);if(a){var n=$.extend({},this.items&&this.items[e],{expandView:this.expandView,parent:this});if(e==1){n.options=$.extend(n.options,{dashboardPanel:{header:this._header,showButtonsOnLink:1}})}var r=new a(t,n);t.data("extras-widget",r);if(r.isRaising("IsModifiedChange")){r.bind("IsModifiedChange",this._onWidgetIsModifiedChange.bind(this))}r.create();u8.tools.ready(this.children,function(){s._updateVisibility();if(i){i()}}.ifAlive(this))}else{u8.log.error("Failed to create extras widget #"+e+"!");return i&&i()}},_onHeaderPreCreate:function(e){e.model.flags|=u8.Base.Widgets.Header.Flags.plain;this._updateHeaderLinks(e)},_onHeaderCreate:function(e){this._header=e;this._setCurrentWidget(e.linkMessageNr,u8.tools.created.curry(this,{}))},_onLinkChange:function(e){this._setCurrentWidget(e.linkMessageNr)},_onHeaderCollapsedChange:function(e,i){this._$container.toggle(!e.getCollapsed())},_onWidgetIsModifiedChange:function(){this.raise("IsModifiedChange")}});u8.Crm.Widgets.ExpandViewExtrasWidget.makeClass({name:"ExpandViewExtrasWidget",deriveFrom:u8.Base.Widgets.Widget,events:["Create","IsModifiedChange"]})})();