(function () { 'use strict'; var window = require('window'), _ = require('_'), $ = require('$'), ko = require('ko'), key = require('key'), Consts = require('Common/Consts'), Enums = require('Common/Enums'), Globals = require('Common/Globals'), Utils = require('Common/Utils'), Events = require('Common/Events'), Translator = require('Common/Translator'), Audio = require('Common/Audio'), Links = require('Common/Links'), Cache = require('Common/Cache'), SocialStore = require('Stores/Social'), AppStore = require('Stores/User/App'), SettingsStore = require('Stores/User/Settings'), AccountStore = require('Stores/User/Account'), FolderStore = require('Stores/User/Folder'), MessageStore = require('Stores/User/Message'), Local = require('Storage/Client'), Settings = require('Storage/Settings'), Remote = require('Remote/User/Ajax'), Promises = require('Promises/User/Ajax'), 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.oDom = null; this.oHeaderDom = null; this.oMessageScrollerDom = null; this.bodyBackgroundColor = ko.observable(''); this.pswp = null; this.allowComposer = !!Settings.capa(Enums.Capa.Composer); this.allowMessageActions = !!Settings.capa(Enums.Capa.MessageActions); this.allowMessageListActions = !!Settings.capa(Enums.Capa.MessageListActions); this.logoImg = Utils.trim(Settings.settingsGet('UserLogoMessage')); this.logoIframe = Utils.trim(Settings.settingsGet('UserIframeMessage')); this.mobile = !!Settings.appSettingsGet('mobile'); this.attachmentsActions = AppStore.attachmentsActions; this.message = MessageStore.message; this.messageListChecked = MessageStore.messageListChecked; this.hasCheckedMessages = MessageStore.hasCheckedMessages; this.messageListCheckedOrSelectedUidsWithSubMails = MessageStore.messageListCheckedOrSelectedUidsWithSubMails; this.messageLoadingThrottle = MessageStore.messageLoadingThrottle; this.messagesBodiesDom = MessageStore.messagesBodiesDom; this.useThreads = SettingsStore.useThreads; this.replySameFolder = SettingsStore.replySameFolder; this.layout = SettingsStore.layout; this.usePreviewPane = SettingsStore.usePreviewPane; this.isMessageSelected = MessageStore.isMessageSelected; this.messageActiveDom = MessageStore.messageActiveDom; this.messageError = MessageStore.messageError; this.fullScreenMode = MessageStore.messageFullScreenMode; this.messageListOfThreadsLoading = ko.observable(false).extend({'rateLimit': 1}); this.highlightUnselectedAttachments = ko.observable(false).extend({'falseTimeout': 2000}); this.showAttachmnetControls = ko.observable(false); this.allowAttachmnetControls = ko.computed(function () { return 0 < this.attachmentsActions().length && Settings.capa(Enums.Capa.AttachmentsActions); }, this); this.downloadAsZipAllowed = ko.computed(function () { return -1 < Utils.inArray('zip', this.attachmentsActions()) && this.allowAttachmnetControls(); }, this); this.downloadAsZipLoading = ko.observable(false); this.downloadAsZipError = ko.observable(false).extend({'falseTimeout': 7000}); this.saveToOwnCloudAllowed = ko.computed(function () { return -1 < Utils.inArray('owncloud', this.attachmentsActions()) && this.allowAttachmnetControls(); }, this); this.saveToOwnCloudLoading = ko.observable(false); this.saveToOwnCloudSuccess = ko.observable(false).extend({'falseTimeout': 2000}); this.saveToOwnCloudError = ko.observable(false).extend({'falseTimeout': 7000}); this.saveToOwnCloudSuccess.subscribe(function (bV) { if (bV) { this.saveToOwnCloudError(false); } }, this); this.saveToOwnCloudError.subscribe(function (bV) { if (bV) { this.saveToOwnCloudSuccess(false); } }, this); this.saveToDropboxAllowed = ko.computed(function () { return -1 < Utils.inArray('dropbox', this.attachmentsActions()) && this.allowAttachmnetControls(); }, this); this.saveToDropboxLoading = ko.observable(false); this.saveToDropboxSuccess = ko.observable(false).extend({'falseTimeout': 2000}); this.saveToDropboxError = ko.observable(false).extend({'falseTimeout': 7000}); this.saveToDropboxSuccess.subscribe(function (bV) { if (bV) { this.saveToDropboxError(false); } }, this); this.saveToDropboxError.subscribe(function (bV) { if (bV) { this.saveToDropboxSuccess(false); } }, this); this.showAttachmnetControls.subscribe(function (bV) { if (this.message()) { _.each(this.message().attachments(), function (oItem) { if (oItem) { oItem.checked(!!bV); } }); } }, this); 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) { MessageStore.selectorMessageSelected(null); } }, this); this.canBeRepliedOrForwarded = ko.computed(function () { var bV = this.messageVisibility(); return !this.isDraftFolder() && bV; }, this); // commands this.closeMessage = Utils.createCommand(this, function () { MessageStore.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 () { var oMessage = this.message(); if (oMessage && this.allowMessageListActions) { this.message(null); require('App/User').default.deleteMessagesFromFolder(Enums.FolderType.Trash, oMessage.folderFullNameRaw, [oMessage.uid], true); } }, this.messageVisibility); this.deleteWithoutMoveCommand = Utils.createCommand(this, function () { var oMessage = this.message(); if (oMessage && this.allowMessageListActions) { this.message(null); require('App/User').default.deleteMessagesFromFolder(Enums.FolderType.Trash, oMessage.folderFullNameRaw, [oMessage.uid], false); } }, this.messageVisibility); this.archiveCommand = Utils.createCommand(this, function () { var oMessage = this.message(); if (oMessage && this.allowMessageListActions) { this.message(null); require('App/User').default.deleteMessagesFromFolder(Enums.FolderType.Archive, oMessage.folderFullNameRaw, [oMessage.uid], true); } }, this.messageVisibility); this.spamCommand = Utils.createCommand(this, function () { var oMessage = this.message(); if (oMessage && this.allowMessageListActions) { this.message(null); require('App/User').default.deleteMessagesFromFolder(Enums.FolderType.Spam, oMessage.folderFullNameRaw, [oMessage.uid], true); } }, this.messageVisibility); this.notSpamCommand = Utils.createCommand(this, function () { var oMessage = this.message(); if (oMessage && this.allowMessageListActions) { this.message(null); require('App/User').default.deleteMessagesFromFolder(Enums.FolderType.NotSpam, oMessage.folderFullNameRaw, [oMessage.uid], true); } }, this.messageVisibility); this.dropboxEnabled = SocialStore.dropbox.enabled; this.dropboxApiKey = SocialStore.dropbox.apiKey; // viewer this.viewBodyTopValue = ko.observable(0); this.viewFolder = ''; this.viewUid = ''; 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.viewTimeStamp = ko.observable(0); this.viewSize = ko.observable(''); this.viewLineAsCss = ko.observable(''); this.viewViewLink = ko.observable(''); this.viewDownloadLink = ko.observable(''); this.viewUserPic = ko.observable(Consts.DATA_IMAGE_USER_DOT_PIC); this.viewUserPicVisible = ko.observable(false); this.viewIsImportant = ko.observable(false); this.viewIsFlagged = ko.observable(false); 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.messageActiveDom.subscribe(function (oDom) { this.bodyBackgroundColor(oDom ? this.detectDomBackgroundColor(oDom): ''); }, this); this.message.subscribe(function (oMessage) { this.messageActiveDom(null); if (oMessage) { this.showAttachmnetControls(false); if (this.viewHash !== oMessage.hash) { this.scrollMessageToTop(); } this.viewFolder = oMessage.folderFullNameRaw; this.viewUid = oMessage.uid; 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.viewTimeStamp(oMessage.dateTimeStampInUTC()); this.viewSize(oMessage.friendlySize()); this.viewLineAsCss(oMessage.lineAsCss()); this.viewViewLink(oMessage.viewLink()); this.viewDownloadLink(oMessage.downloadLink()); this.viewIsImportant(oMessage.isImportant()); this.viewIsFlagged(oMessage.flagged()); sLastEmail = oMessage.fromAsSingleEmail(); Cache.getUserPic(sLastEmail, function (sPic, sEmail) { if (sPic !== self.viewUserPic() && sLastEmail === sEmail) { self.viewUserPicVisible(false); self.viewUserPic(Consts.DATA_IMAGE_USER_DOT_PIC); if ('' !== sPic) { self.viewUserPicVisible(true); self.viewUserPic(sPic); } } }); } else { this.viewFolder = ''; this.viewUid = ''; this.viewHash = ''; this.scrollMessageToTop(); } }, this); this.message.viewTrigger.subscribe(function () { var oMessage = this.message(); if (oMessage) { this.viewIsFlagged(oMessage.flagged()); } else { this.viewIsFlagged(false); } }, this); this.fullScreenMode.subscribe(function (bValue) { Globals.$html.toggleClass('rl-message-fullscreen', bValue); Utils.windowResize(); }); this.messageLoadingThrottle.subscribe(Utils.windowResizeCallback); this.messageFocused = ko.computed(function () { return Enums.Focused.MessageView === AppStore.focusedState(); }); this.messageListAndMessageViewLoading = ko.computed(function () { return MessageStore.messageListCompleteLoadingThrottle() || MessageStore.messageLoadingThrottle(); }); this.goUpCommand = Utils.createCommand(this, function () { Events.pub('mailbox.message-list.selector.go-up', [ Enums.Layout.NoPreview === this.layout() ? !!this.message() : true ]); }, function () { return !this.messageListAndMessageViewLoading(); }); this.goDownCommand = Utils.createCommand(this, function () { Events.pub('mailbox.message-list.selector.go-down', [ Enums.Layout.NoPreview === this.layout() ? !!this.message() : true ]); }, function () { return !this.messageListAndMessageViewLoading(); }); Events.sub('mailbox.message-view.toggle-full-screen', function () { this.toggleFullScreen(); }, this); this.attachmentPreview = _.bind(this.attachmentPreview, this); kn.constructorEnd(this); } kn.extendAsViewModel(['View/User/MailBox/MessageView', 'View/App/MailBox/MessageView', 'MailBoxMessageViewViewModel'], MessageViewMailBoxUserView); _.extend(MessageViewMailBoxUserView.prototype, AbstractView.prototype); MessageViewMailBoxUserView.prototype.detectDomBackgroundColor = function (oDom) { var iLimit = 5, sResult = '', aC = null, fFindDom = function (oDom) { var aC = oDom ? oDom.children() : null; return (aC && 1 === aC.length && aC.is('table,div,center')) ? aC : null; }, fFindColor = function (oDom) { var sResult = ''; if (oDom) { sResult = oDom.css('background-color') || ''; if (!oDom.is('table')) { sResult = 'rgba(0, 0, 0, 0)' === sResult || 'transparent' === sResult ? '' : sResult; } } return sResult; } ; if (oDom && 1 === oDom.length) { aC = oDom; while ('' === sResult) { iLimit--; if (0 >= iLimit) { break; } aC = fFindDom(aC); if (aC) { sResult = fFindColor(aC); } else { break; } } sResult = 'rgba(0, 0, 0, 0)' === sResult || 'transparent' === sResult ? '' : sResult; } 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) { if (Settings.capa(Enums.Capa.Composer)) { kn.showScreenPopup(require('View/Popup/Compose'), [sType, MessageStore.message()]); } }; MessageViewMailBoxUserView.prototype.checkHeaderHeight = function () { if (this.oHeaderDom) { this.viewBodyTopValue(this.message() ? this.oHeaderDom.height() + 20 /* padding-(top/bottom): 20px */ + 1 /* borded-bottom: 1px */ : 0); } }; /** * @todo * @param {string} sEmail */ // MessageViewMailBoxUserView.prototype.displayMailToPopup = function (sMailToUrl) // { // sMailToUrl = sMailToUrl.replace(/\?.+$/, ''); // // var // sResult = '', // aTo = [], // EmailModel = require('Model/Email'), // fParseEmailLine = function (sLine) { // return sLine ? _.compact(_.map([window.decodeURIComponent(sLine)], function (sItem) { // var oEmailModel = new EmailModel(); // oEmailModel.mailsoParse(sItem); // return '' !== oEmailModel.email ? oEmailModel : null; // })) : null; // } // ; // // aTo = fParseEmailLine(sMailToUrl); // sResult = aTo && aTo[0] ? aTo[0].email : ''; // // return sResult; // }; /** * @param {Object} oAttachment * @returns {boolean} */ MessageViewMailBoxUserView.prototype.attachmentPreview = function (oAttachment) { if (oAttachment && oAttachment.isImage() && !oAttachment.isLinked && this.message() && this.message().attachments()) { var oDiv = $('