(function(){"use strict";var e={CalendarOneDay:{view:"singleDay"},CalendarWorkWeek:{view:"workWeek"},CalendarWeek:{view:"week"},CalendarMonth:{view:"month"}};u8.Crm.Widgets.CalendarHeader=function(e,t){this.Widget(e,$.extend({},u8.Crm.Widgets.CalendarHeader.defaults,t));this._isCreated=false;if(!t.model){$error("CalendarHeader requires a model!")}this.setModel(t.model);this._create()};u8.Crm.Widgets.CalendarHeader.makeClass({name:"CalendarHeader",deriveFrom:u8.Base.Widgets.Widget,events:["Create"]});u8.Crm.Widgets.CalendarHeader.defaults={headerOptions:{headerGroupName:"Special",headerName:"CalendarHeader"},showNewButton:true};$.extend(u8.Crm.Widgets.CalendarHeader.prototype,{dispose:function(){if(this._isDisposed){return}this.setModel(null);this.$(this.element).unbind();this.Widget_dispose()},setModel:function(e){if(this._model){this._model.unbind("Update",this.id)}this._model=e;if(this._model){this._model.bind("Update",this._onModelUpdate.bind(this),this.id)}},getModel:function(){return this._model},_create:function(){var e=Function.chain.onDisposedAbort;Function.chain(e,this._load,this._updateTitle.async(),this._draw.curry(e)).call(this,this._onCreated)},_draw:function(e,t){Function.chain(e,this._createViewModel.async(),this._createView).call(this,t)},_load:function(e){u8.using({texts:["names_of_months","search_text","net_core_texts"],templates:[{name:"u8-calendar-header",url:"~/crm/CalendarHeader.utml.htm"}]},e)},_createViewModel:function(){var e={id:this.id+"_header",isDefault:this.isDefault,view:{showButtons:true,showCaption:false,showImage:false,showCollapse:false},context:this._model,onOwnerDraw:this._onHeaderOwnerDraw.bind(this)};this._viewModel={id:this.id,texts:{next:$t("TXT_search_text_8"),previous:$t("TXT_search_text_10")},label:this._getRangeLabel(),headerOptions:$.extend({},this.headerOptions,e),buttonOptions:this.showNewButton!==false&&{text:$t("TXT_net_core_texts_114"),command:"CalendarNewEntry"}}},_createView:function(e){var t=this.$(this.element);t.addClass("u8-calendarheader").toggleClass("page",!!this.isDefault).empty().utml({name:"u8-calendar-header",data:this._viewModel,itemOptions:{parent:this}},e)},_updateRange:function(){if(!this._viewModel){return}this._viewModel.label=this._getRangeLabel();this.$(this.element).find("#"+this._viewModel.id+"_range").text(this._viewModel.label)},_updateView:function(){var t=this.getWidget("header");if(!t){return}var i=this._model.getView();for(var a in e){var n=t.getItem(a);if(n&&n.__button){n.__button.setGroupValue(i);break}}},_getRangeLabel:function(){if(this._model.view=="month"){var e="";if(this._model.skipMonths<12){e+=$t("TXT_names_of_months_"+this._model.range.start.getMonth().toString())+" "}e+=this._model.range.start.getFullYear();return e}else{var t=this._model.days;var i={start:t[0],end:t.peek().clone()};return new u8.Base.DateRange(i).toDateString()}},_updateTitle:function(){if(this.isDefault){var e=$t("TXT_net_core_texts_322");if(e){this.page.setTitle(e)}}},_onCreated:function(){this.$(this.element).on("click",this._onClick.bind(this));u8.tools.createdWhenReady(this)},_onClick:function(e){var t=this.$(e.target).closest(".u8-calendarheader-button").attr("data-command");if(t=="next"||t=="previous"){this._model.setView(t)}},_onModelUpdate:function(e,t){if(this._isDisposed){return}if(t.reason=="range"){this._updateRange()}else if(t.reason=="view"){this._updateRange();this._updateView()}},_onHeaderOwnerDraw:function(t,i){var a=i.item;var n=e[a.name];if(n){var s=this._model.getView();var r=n.view==s;var d=this.$("<button/>").appendTo(a.element);a.__button=u8.widget("ToggleButton",d,{parent:a,text:a.label,image:a.image,pressed:r,value:n.view,command:a.name,groupName:this.id+"_views"})}}})})();