Ext.namespace("Zarafa.plugins.delayeddelivery");
Zarafa.plugins.delayeddelivery.DelayDeliveryPlugin=Ext.extend(Zarafa.core.Plugin,{initPlugin:function(){Zarafa.plugins.delayeddelivery.DelayDeliveryPlugin.superclass.initPlugin.apply(this,arguments);Zarafa.core.data.SharedComponentType.addProperty("common.dialog.delayeddelivery");this.registerInsertionPoint("context.mail.mailcreatecontentpanel.toolbar.aftersendbutton",this.createNewDelayDeliveryButton,this)},bidSharedComponent:function(a){var b=-1;switch(a){case Zarafa.core.data.SharedComponentType["common.dialog.delayeddelivery"]:b=1;
break}return b},getSharedComponent:function(){return Zarafa.plugins.delayeddelivery.dialogs.DelayDeliveryContentPanel},createNewDelayDeliveryButton:function(){return[{xtype:"button",text:_("Send Later"),tooltip:{title:_("Send Later"),text:_("Schedule your mail to be sent on a specific time.")},iconCls:"icon_delaydelivery",handler:this.showDelayDeliveryContentPanel,listeners:{afterrender:this.onAfterRender,scope:this},scope:this}]},onAfterRender:function(a){a=a.ownerCt;a.sendButton.on("click",this.onSend,
a)},onSend:function(){this.record.set("deferred_send_time",null)},showDelayDeliveryContentPanel:function(a){Zarafa.core.data.UIFactory.openLayerComponent(Zarafa.core.data.SharedComponentType["common.dialog.delayeddelivery"],a.ownerCt.record,{manager:Ext.WindowMgr,modal:true,resizable:false,scope:this})}});Zarafa.onReady(function(){container.registerPlugin(new Zarafa.core.PluginMetaData({name:"delayeddelivery",displayName:_("Delayed Delivery Plugin"),pluginConstructor:Zarafa.plugins.delayeddelivery.DelayDeliveryPlugin}))});
Ext.namespace("Zarafa.plugins.delayeddelivery.data");Zarafa.plugins.delayeddelivery.data.DelayDeliveryRecordFields=[{name:"deferred_send_time",type:"date",dateFormat:"timestamp",defaultValue:null}];Zarafa.core.data.RecordFactory.addFieldToMessageClass("IPM",Zarafa.plugins.delayeddelivery.data.DelayDeliveryRecordFields);Ext.namespace("Zarafa.plugins.delayeddelivery.dialogs");
Zarafa.plugins.delayeddelivery.dialogs.DelayDeliveryContentPanel=Ext.extend(Zarafa.core.ui.ContentPanel,{record:null,deferredSendTime:undefined,constructor:function(a){a=a||{};a=Ext.apply(a,{xtype:"zarafa.delayeddeliverycontentpanel",width:300,height:250,title:_("Schedule mail to be sent out")+"...",layout:"fit",items:[{xtype:"zarafa.delayeddeliverypanel",ref:"delayDeliveryPanel",buttonAlign:"left",buttons:[{text:_("Send"),cls:"zarafa-action",iconCls:"icon_delaydelivery_dialog",ref:"../sendButton",
disabled:true,handler:this.onSend,scope:this},"->",{xtype:"spacer"},{text:_("Cancel"),handler:this.onCancel,scope:this}]}]});Zarafa.plugins.delayeddelivery.dialogs.DelayDeliveryContentPanel.superclass.constructor.call(this,a)},setDisabled:function(a){this.delayDeliveryPanel.sendButton.setDisabled(a)},onCancel:function(){this.close()},onSend:function(){var a=container.getTabPanel().getActiveTab();this.deferredSendTime=this.delayDeliveryPanel.getMailDateTime();a.un("saverecord",this.onSaveRecord,this);
a.on("saverecord",this.onSaveRecord,this);a.sendValidationQueue.un("completequeue",this.onCompleteQueue.createDelegate(this,[a,this.record]),this);a.sendValidationQueue.on("completequeue",this.onCompleteQueue.createDelegate(this,[a,this.record]),this);this.close();a.sendRecord()},onCompleteQueue:function(a,b){if(Ext.isEmpty(b.get("deferred_send_time"))){a.un("saverecord",this.onSaveRecord,this);a.showInfoMask=true}a.sendValidationQueue.un("completequeue",this.onCompleteQueue,this)},onSaveRecord:function(a){if(a.isSending===
true&&a.showInfoMask===true){a.record.set("deferred_send_time",this.deferredSendTime);a.showInfoMask=false;this.showNotification();a.un("saverecord",this.onSaveRecord,this)}},showNotification:function(){var a=container.getNotifier().notify("info.saved","Scheduled Items",this.sendLaterMessage(),{persistent:true,listeners:{click:this.onNotifierClick,scope:this}});this.notificationTimer=new Ext.util.DelayedTask(this.clearNotification.createDelegate(this,[a],this));this.notificationTimer.delay(5E3)},
sendLaterMessage:function(){var a=this.deferredSendTime.format("d/m/y H:i"),b=_("Your message will be sent at "),c=_("Go to your <b>Outbox</b> to edit your email.");return String.format("{0}<b>{1}.</b> <br>{2}",b,a,c)},clearNotification:function(a){container.getNotifier().notify("info.saved",null,null,{reference:a,destroy:true});this.notificationTimer.cancel()},onNotifierClick:function(a){this.clearNotification(a)}});Ext.reg("zarafa.delayeddeliverycontentpanel",Zarafa.plugins.delayeddelivery.dialogs.DelayDeliveryContentPanel);
Ext.namespace("Zarafa.plugins.delayeddelivery.dialogs");
Zarafa.plugins.delayeddelivery.dialogs.DelayDeliveryPanel=Ext.extend(Ext.Panel,{scheduledMailDateTime:undefined,constructor:function(a){a=a||{};a=Ext.applyIf(a,{xtype:"zarafa.delayeddeliverypanel",border:false,bodyStyle:"padding: 15px; background-color: inherit;",defaults:{style:{margin:"0px 0px 10px 5px"}},items:[{xtype:"zarafa.delayeddeliveryspinnerfield",value:_("hour(s)"),listeners:{focus:this.onFieldFocus,change:this.onFieldChange,scope:this}},{xtype:"zarafa.delayeddeliveryspinnerfield",value:_("day(s)"),
listeners:{focus:this.onFieldFocus,change:this.onFieldChange,scope:this}},{xtype:"zarafa.delayeddeliveryspinnerfield",value:_("month(s)"),listeners:{focus:this.onFieldFocus,change:this.onFieldChange,scope:this}},{xtype:"displayfield",value:_("at a specific time"),style:"marginLeft : 10%;"},{xtype:"zarafa.delayeddeliverydatetimefield",ref:"delayDeliveryDateTimeField",listeners:{focus:this.onFieldFocus,change:this.onFieldChange,scope:this}},{xtype:"displayfield",ref:"timeInfofield"}],listeners:{afterrender:this.setDefaultValue}});
Zarafa.plugins.delayeddelivery.dialogs.DelayDeliveryPanel.superclass.constructor.call(this,a)},onFieldFocus:function(a){this.createMessage(a.getDateTime())},onFieldChange:function(a,b){this.createMessage(b)},createMessage:function(a){this.scheduledMailDateTime=a=new Date(a);this.showMessage(this.getMessageDate(a))},getMessageDate:function(a){var b="";this.ownerCt.setDisabled(true);if(!isNaN(a.getTime()))if(this.isPastDate(a)){b=_("Sorry,the time you entered is in past. Please reschedule your mail");
this.delayDeliveryDateTimeField.addClass("delayeddelivery_datetimefield_color")}else{b="";var c=a.format("H:i");b=this.isTodayDate(a)?_("today  at "):this.isTomorrowDate(a)?_("tomorrow  at "):this.isDayInCurrentWeek(a)?String.format(_("{0} at "),a.format("l ")):String.format(_("at {0}"),a.format("F jS Y ,"));b=String.format(_("Your email will be sent {0}{1}"),b,c);this.ownerCt.setDisabled(false);this.delayDeliveryDateTimeField.removeClass("delayeddelivery_datetimefield_color")}return b},isPastDate:function(a){var b=
(new Date).getTime();return a.getTime()<b-3E3},isTodayDate:function(a){var b=(new Date).clearTime(true);a=(new Date(a)).clearTime(true);return b.getTime()===a.getTime()},isTomorrowDate:function(a){var b=(new Date).clearTime(true);a=(new Date(a)).clearTime(true);return(new Date(b.add(Date.DAY,1))).getTime()===a.getTime()},isDayInCurrentWeek:function(a){currentDateTime=new Date;var b=currentDateTime.getDate()-currentDateTime.getDay(),c=b+7;b=new Date(currentDateTime.setDate(b));c=new Date(currentDateTime.setDate(c));
return(new Date(a)).clearTime(true).between(b,c)},showMessage:function(a){this.timeInfofield.setValue(a)},setDefaultValue:function(){var a=this.ownerCt.record.get("deferred_send_time");Ext.isEmpty(a)?this.delayDeliveryDateTimeField.setValue(new Date):this.delayDeliveryDateTimeField.setValue(a);this.createMessage(this.delayDeliveryDateTimeField.getDateTime())},getMailDateTime:function(){return this.scheduledMailDateTime}});Ext.reg("zarafa.delayeddeliverypanel",Zarafa.plugins.delayeddelivery.dialogs.DelayDeliveryPanel);
Ext.namespace("Zarafa.plugins.delayeddelivery.ui");
Zarafa.plugins.delayeddelivery.ui.DelayDeliveryDateTimeField=Ext.extend(Zarafa.common.ui.CompositeField,{constructor:function(a){a=a||{};Ext.apply(this,a);a=Ext.applyIf(a,{items:[{xtype:"radio",boxLabel:" ",ref:"sendLaterRadio",name:"sendLaterRadio",listeners:{check:this.onRadioCheck,scope:this}},{xtype:"zarafa.datetimefield",ref:"dateTimeField",minValue:new Date,width:200,listeners:{change:this.onDateTimeChange,afterrender:this.initEvent,scope:this}}]});Zarafa.plugins.delayeddelivery.ui.DelayDeliveryDateTimeField.superclass.constructor.call(this,
a)},onKeyUp:function(a,b){var c=b.getCharCode();if(c>=48&&c<=57||c>=96&&c<=105){c=a.startValue;this.dateTimeField.doDateChange(a.getValue(),c)}},onRadioCheck:function(a,b){b===true&&this.fireEvent("focus",this)},onDateTimeFocus:function(){this.sendLaterRadio.setValue(true);this.fireEvent("focus",this)},onDateTimeChange:function(){this.fireEvent("change",this,this.getDateTime())},initEvent:function(){var a=this.dateTimeField.timeField,b=this.dateTimeField.dateField;a.enableKeyEvents=true;b.enableKeyEvents=
true;a.initEvents();b.initEvents();a.on("keyup",this.onKeyUp,this);b.on("keyup",this.onKeyUp,this);a.on("focus",this.onDateTimeFocus,this);b.on("focus",this.onDateTimeFocus,this);a.un("change",this.dateTimeField.onTimeChange,this.dateTimeField);b.un("change",this.dateTimeField.onDateChange,this.dateTimeField)},getDateTime:function(){return this.dateTimeField.getValue()},setValue:function(a){this.sendLaterRadio.setValue(true);this.dateTimeField.setValue(a)},addClass:function(a){this.dateTimeField.timeField.addClass(a);
this.dateTimeField.dateField.addClass(a)},removeClass:function(a){this.dateTimeField.timeField.removeClass(a);this.dateTimeField.dateField.removeClass(a)}});Ext.reg("zarafa.delayeddeliverydatetimefield",Zarafa.plugins.delayeddelivery.ui.DelayDeliveryDateTimeField);Ext.namespace("Zarafa.plugins.delayeddelivery.ui");
Zarafa.plugins.delayeddelivery.ui.DelayDeliverySpinnerField=Ext.extend(Zarafa.common.ui.CompositeField,{value:undefined,constructor:function(a){a=a||{};Ext.apply(this,a);a=Ext.applyIf(a,{items:[{xtype:"radio",boxLabel:"in",ref:"sendLaterRadio",name:"sendLaterRadio",inputValue:this.value,listeners:{check:this.onRadioCheck,scope:this}},{xtype:"zarafa.spinnerfield",ref:"spinnerField",defaultValue:1,minValue:1,maxValue:999,incrementValue:1,enableKeyEvents:true,width:45,plugins:["zarafa.numberspinner"],
listeners:{spin:this.onValueChange,keyup:this.onValueChange,focus:this.onFocus,scope:this}},{xtype:"displayfield",value:this.value}]});Zarafa.plugins.delayeddelivery.ui.DelayDeliverySpinnerField.superclass.constructor.call(this,a)},onRadioCheck:function(a,b){b===true&&this.fireEvent("focus",this)},onFocus:function(){this.sendLaterRadio.setValue(true);this.fireEvent("focus",this)},onValueChange:function(){this.fireEvent("change",this,this.getDateTime())},getDateTime:function(){var a=this.sendLaterRadio.getGroupValue(),
b=this.spinnerField.getValue(),c=new Date;switch(a){case "hour(s)":return c.add(Date.HOUR,b);case "day(s)":return c.add(Date.DAY,b);case "month(s)":return c.add(Date.MONTH,b);default:return c}}});Ext.reg("zarafa.delayeddeliveryspinnerfield",Zarafa.plugins.delayeddelivery.ui.DelayDeliverySpinnerField);
