(function () { 'use strict'; var _ = require('_'), $ = require('$'), ko = require('ko'), key = require('key'), PhotoSwipe = require('PhotoSwipe'), PhotoSwipeUI_Default = require('PhotoSwipeUI_Default'), Consts = require('Common/Consts'), Enums = require('Common/Enums'), Globals = require('Common/Globals'), Utils = require('Common/Utils'), Events = require('Common/Events'), Translator = require('Common/Translator'), SettingsStore = require('Stores/User/Settings'), AccountStore = require('Stores/User/Account'), FolderStore = require('Stores/User/Folder'), Local = require('Storage/Client'), Cache = require('Storage/User/Cache'), Data = require('Storage/User/Data'), Remote = require('Storage/User/Remote'), kn = require('Knoin/Knoin'), AbstractView = require('Knoin/AbstractView') ; /** * @constructor * @extends AbstractView */ function MessageViewMailBoxUserView() { AbstractView.call(this, 'Right', 'MailMessageView'); var self = this, sLastEmail = '', createCommandHelper = function (sType) { return Utils.createCommand(self, function () { this.lastReplyAction(sType); this.replyOrforward(sType); }, self.canBeRepliedOrForwarded); } ; this.oHeaderDom = null; this.oMessageScrollerDom = null; this.pswp = null; this.message = Data.message; this.currentMessage = Data.currentMessage; this.messageListChecked = Data.messageListChecked; this.hasCheckedMessages = Data.hasCheckedMessages; this.messageListCheckedOrSelectedUidsWithSubMails = Data.messageListCheckedOrSelectedUidsWithSubMails; this.messageLoading = Data.messageLoading; this.messageLoadingThrottle = Data.messageLoadingThrottle; this.messagesBodiesDom = Data.messagesBodiesDom; this.useThreads = SettingsStore.useThreads; this.replySameFolder = SettingsStore.replySameFolder; this.layout = SettingsStore.layout; this.usePreviewPane = SettingsStore.usePreviewPane; this.isMessageSelected = Data.isMessageSelected; this.messageActiveDom = Data.messageActiveDom; this.messageError = Data.messageError; this.fullScreenMode = Data.messageFullScreenMode; this.lastReplyAction_ = ko.observable(''); this.lastReplyAction = ko.computed({ read: this.lastReplyAction_, write: function (sValue) { sValue = -1 === Utils.inArray(sValue, [ Enums.ComposeType.Reply, Enums.ComposeType.ReplyAll, Enums.ComposeType.Forward ]) ? Enums.ComposeType.Reply : sValue; this.lastReplyAction_(sValue); }, owner: this }); this.lastReplyAction(Local.get(Enums.ClientSideKeyName.LastReplyAction) || Enums.ComposeType.Reply); this.lastReplyAction_.subscribe(function (sValue) { Local.set(Enums.ClientSideKeyName.LastReplyAction, sValue); }); this.showFullInfo = ko.observable(false); this.moreDropdownTrigger = ko.observable(false); this.messageDomFocused = ko.observable(false).extend({'rateLimit': 0}); this.messageVisibility = ko.computed(function () { return !this.messageLoadingThrottle() && !!this.message(); }, this); this.message.subscribe(function (oMessage) { if (!oMessage) { this.currentMessage(null); } }, this); this.canBeRepliedOrForwarded = ko.computed(function () { var bV = this.messageVisibility(); return !this.isDraftFolder() && bV; }, this); // commands this.closeMessage = Utils.createCommand(this, function () { Data.message(null); }); this.replyCommand = createCommandHelper(Enums.ComposeType.Reply); this.replyAllCommand = createCommandHelper(Enums.ComposeType.ReplyAll); this.forwardCommand = createCommandHelper(Enums.ComposeType.Forward); this.forwardAsAttachmentCommand = createCommandHelper(Enums.ComposeType.ForwardAsAttachment); this.editAsNewCommand = createCommandHelper(Enums.ComposeType.EditAsNew); this.messageVisibilityCommand = Utils.createCommand(this, Utils.emptyFunction, this.messageVisibility); this.messageEditCommand = Utils.createCommand(this, function () { this.editMessage(); }, this.messageVisibility); this.deleteCommand = Utils.createCommand(this, function () { if (this.message()) { require('App/User').deleteMessagesFromFolder(Enums.FolderType.Trash, this.message().folderFullNameRaw, [this.message().uid], true); } }, this.messageVisibility); this.deleteWithoutMoveCommand = Utils.createCommand(this, function () { if (this.message()) { require('App/User').deleteMessagesFromFolder(Enums.FolderType.Trash, Data.currentFolderFullNameRaw(), [this.message().uid], false); } }, this.messageVisibility); this.archiveCommand = Utils.createCommand(this, function () { if (this.message()) { require('App/User').deleteMessagesFromFolder(Enums.FolderType.Archive, this.message().folderFullNameRaw, [this.message().uid], true); } }, this.messageVisibility); this.spamCommand = Utils.createCommand(this, function () { if (this.message()) { require('App/User').deleteMessagesFromFolder(Enums.FolderType.Spam, this.message().folderFullNameRaw, [this.message().uid], true); } }, this.messageVisibility); this.notSpamCommand = Utils.createCommand(this, function () { if (this.message()) { require('App/User').deleteMessagesFromFolder(Enums.FolderType.NotSpam, this.message().folderFullNameRaw, [this.message().uid], true); } }, this.messageVisibility); // viewer this.viewBodyTopValue = ko.observable(0); this.viewHash = ''; this.viewSubject = ko.observable(''); this.viewFromShort = ko.observable(''); this.viewFromDkimData = ko.observable(['none', '']); this.viewToShort = ko.observable(''); this.viewFrom = ko.observable(''); this.viewTo = ko.observable(''); this.viewCc = ko.observable(''); this.viewBcc = ko.observable(''); this.viewReplyTo = ko.observable(''); this.viewDate = ko.observable(''); this.viewSize = ko.observable(''); this.viewMoment = ko.observable(''); this.viewLineAsCss = ko.observable(''); this.viewViewLink = ko.observable(''); this.viewDownloadLink = ko.observable(''); this.viewUserPic = ko.observable(Consts.DataImages.UserDotPic); this.viewUserPicVisible = ko.observable(false); this.viewIsImportant = ko.observable(false); this.viewPgpPassword = ko.observable(''); this.viewPgpSignedVerifyStatus = ko.computed(function () { return this.message() ? this.message().pgpSignedVerifyStatus() : Enums.SignedVerifyStatus.None; }, this); this.viewPgpSignedVerifyUser = ko.computed(function () { return this.message() ? this.message().pgpSignedVerifyUser() : ''; }, this); this.viewFromDkimStatusIconClass = ko.computed(function () { var sResult = 'icon-none iconcolor-display-none'; // var sResult = 'icon-warning-alt iconcolor-grey'; switch (this.viewFromDkimData()[0]) { case 'none': break; case 'pass': sResult = 'icon-ok iconcolor-green'; // sResult = 'icon-warning-alt iconcolor-green'; break; default: sResult = 'icon-warning-alt iconcolor-red'; break; } return sResult; }, this); this.viewFromDkimStatusTitle = ko.computed(function () { var aStatus = this.viewFromDkimData(); if (Utils.isNonEmptyArray(aStatus)) { if (aStatus[0] && aStatus[1]) { return aStatus[1]; } else if (aStatus[0]) { return 'DKIM: ' + aStatus[0]; } } return ''; }, this); this.message.subscribe(function (oMessage) { this.messageActiveDom(null); this.viewPgpPassword(''); if (oMessage) { if (this.viewHash !== oMessage.hash) { this.scrollMessageToTop(); } this.viewHash = oMessage.hash; this.viewSubject(oMessage.subject()); this.viewFromShort(oMessage.fromToLine(true, true)); this.viewFromDkimData(oMessage.fromDkimData()); this.viewToShort(oMessage.toToLine(true, true)); this.viewFrom(oMessage.fromToLine(false)); this.viewTo(oMessage.toToLine(false)); this.viewCc(oMessage.ccToLine(false)); this.viewBcc(oMessage.bccToLine(false)); this.viewReplyTo(oMessage.replyToToLine(false)); this.viewDate(oMessage.fullFormatDateValue()); this.viewSize(oMessage.friendlySize()); this.viewMoment(oMessage.momentDate()); this.viewLineAsCss(oMessage.lineAsCss()); this.viewViewLink(oMessage.viewLink()); this.viewDownloadLink(oMessage.downloadLink()); this.viewIsImportant(oMessage.isImportant()); sLastEmail = oMessage.fromAsSingleEmail(); Cache.getUserPic(sLastEmail, function (sPic, $sEmail) { if (sPic !== self.viewUserPic() && sLastEmail === $sEmail) { self.viewUserPicVisible(false); self.viewUserPic(Consts.DataImages.UserDotPic); if ('' !== sPic) { self.viewUserPicVisible(true); self.viewUserPic(sPic); } } }); } else { this.viewHash = ''; this.scrollMessageToTop(); } }, this); this.fullScreenMode.subscribe(function (bValue) { if (bValue) { Globals.$html.addClass('rl-message-fullscreen'); } else { Globals.$html.removeClass('rl-message-fullscreen'); } Utils.windowResize(); }); this.messageLoadingThrottle.subscribe(Utils.windowResizeCallback); this.goUpCommand = Utils.createCommand(this, function () { Events.pub('mailbox.message-list.selector.go-up'); }); this.goDownCommand = Utils.createCommand(this, function () { Events.pub('mailbox.message-list.selector.go-down'); }); kn.constructorEnd(this); } kn.extendAsViewModel(['View/User/MailBox/MessageView', 'View/App/MailBox/MessageView', 'MailBoxMessageViewViewModel'], MessageViewMailBoxUserView); _.extend(MessageViewMailBoxUserView.prototype, AbstractView.prototype); MessageViewMailBoxUserView.prototype.isPgpActionVisible = function () { return Enums.SignedVerifyStatus.Success !== this.viewPgpSignedVerifyStatus(); }; MessageViewMailBoxUserView.prototype.isPgpStatusVerifyVisible = function () { return Enums.SignedVerifyStatus.None !== this.viewPgpSignedVerifyStatus(); }; MessageViewMailBoxUserView.prototype.isPgpStatusVerifySuccess = function () { return Enums.SignedVerifyStatus.Success === this.viewPgpSignedVerifyStatus(); }; MessageViewMailBoxUserView.prototype.pgpStatusVerifyMessage = function () { var sResult = ''; switch (this.viewPgpSignedVerifyStatus()) { case Enums.SignedVerifyStatus.UnknownPublicKeys: sResult = Translator.i18n('PGP_NOTIFICATIONS/NO_PUBLIC_KEYS_FOUND'); break; case Enums.SignedVerifyStatus.UnknownPrivateKey: sResult = Translator.i18n('PGP_NOTIFICATIONS/NO_PRIVATE_KEY_FOUND'); break; case Enums.SignedVerifyStatus.Unverified: sResult = Translator.i18n('PGP_NOTIFICATIONS/UNVERIFIRED_SIGNATURE'); break; case Enums.SignedVerifyStatus.Error: sResult = Translator.i18n('PGP_NOTIFICATIONS/DECRYPTION_ERROR'); break; case Enums.SignedVerifyStatus.Success: sResult = Translator.i18n('PGP_NOTIFICATIONS/GOOD_SIGNATURE', { 'USER': this.viewPgpSignedVerifyUser() }); break; } return sResult; }; MessageViewMailBoxUserView.prototype.fullScreen = function () { this.fullScreenMode(true); Utils.windowResize(); }; MessageViewMailBoxUserView.prototype.unFullScreen = function () { this.fullScreenMode(false); Utils.windowResize(); }; MessageViewMailBoxUserView.prototype.toggleFullScreen = function () { Utils.removeSelection(); this.fullScreenMode(!this.fullScreenMode()); Utils.windowResize(); }; /** * @param {string} sType */ MessageViewMailBoxUserView.prototype.replyOrforward = function (sType) { kn.showScreenPopup(require('View/Popup/Compose'), [sType, Data.message()]); }; MessageViewMailBoxUserView.prototype.onBuild = function (oDom) { var self = this, sErrorMessage = Translator.i18n('PREVIEW_POPUP/IMAGE_ERROR'), fCheckHeaderHeight = function () { if (self.oHeaderDom) { self.viewBodyTopValue(self.message() ? self.oHeaderDom.height() + 20 /* padding-(top/bottom): 20px */ + 1 /* borded-bottom: 1px */ : 0); } } ; this.fullScreenMode.subscribe(function (bValue) { if (bValue) { self.message.focused(true); } }, this); this.fullScreenMode.subscribe(fCheckHeaderHeight); this.showFullInfo.subscribe(fCheckHeaderHeight); this.message.subscribe(fCheckHeaderHeight); Events.sub('window.resize', _.throttle(function () { _.delay(fCheckHeaderHeight, 1); _.delay(fCheckHeaderHeight, 200); _.delay(fCheckHeaderHeight, 500); }, 50)); this.showFullInfo.subscribe(function () { Utils.windowResize(); Utils.windowResize(250); }); this.oHeaderDom = $('.messageItemHeader', oDom); this.oHeaderDom = this.oHeaderDom[0] ? this.oHeaderDom : null; this.pswpDom = $('.pswp', oDom)[0]; if (this.pswpDom) { oDom .on('click', '.attachmentImagePreview[data-index]', function (oEvent) { var oPs = null, oEl = oEvent.currentTarget || null, aItems = [] // fThumbBoundsFn = function (index) { // var oRes = null, oEl = aItems[index], oPos = null; // if (oEl && oEl.el) // { // oPos = oEl.el.find('.iconBG').offset(); // oRes = oPos && oPos.top && oPos.left ? // {x: oPos.left, y: oPos.top, w: 60} : null; // } // // return oRes; // } ; oDom.find('.attachmentImagePreview[data-index]').each(function (index, oSubElement) { var $oItem = $(oSubElement) ; aItems.push({ // 'el': $oItem, 'w': 600, 'h': 400, 'src': $oItem.attr('href'), 'title': $oItem.attr('title') || '' }); }); if (aItems && 0 < aItems.length) { Globals.useKeyboardShortcuts(false); oPs = new PhotoSwipe(self.pswpDom, PhotoSwipeUI_Default, aItems, { 'index': Utils.pInt($(oEl).data('index')), 'bgOpacity': 0.85, 'loadingIndicatorDelay': 500, 'errorMsg': '