1 /*
  2  * ***** BEGIN LICENSE BLOCK *****
  3  * Zimbra Collaboration Suite Web Client
  4  * Copyright (C) 2011, 2014, 2016 Synacor, Inc.
  5  *
  6  * The contents of this file are subject to the Common Public Attribution License Version 1.0 (the "License");
  7  * you may not use this file except in compliance with the License.
  8  * You may obtain a copy of the License at: https://www.zimbra.com/license
  9  * The License is based on the Mozilla Public License Version 1.1 but Sections 14 and 15
 10  * have been added to cover use of software over a computer network and provide for limited attribution
 11  * for the Original Developer. In addition, Exhibit A has been modified to be consistent with Exhibit B.
 12  *
 13  * Software distributed under the License is distributed on an "AS IS" basis,
 14  * WITHOUT WARRANTY OF ANY KIND, either express or implied.
 15  * See the License for the specific language governing rights and limitations under the License.
 16  * The Original Code is Zimbra Open Source Web Client.
 17  * The Initial Developer of the Original Code is Zimbra, Inc.  All rights to the Original Code were
 18  * transferred by Zimbra, Inc. to Synacor, Inc. on September 14, 2015.
 19  *
 20  * All portions of the code are Copyright (C) 2011, 2014, 2016 Synacor, Inc. All Rights Reserved.
 21  * ***** END LICENSE BLOCK *****
 22  */
 23 /**
 24  * Priority Group divides a mail list into sections: Important & Unread, Important, Flagged, Everything else
 25  * This implemenation is experimental and based on Tag which will change in the future.
 26  * TODO:  Search needs to be batch request based. (implementation outside the scope of this object, but noting here)
 27  * @param group
 28  */
 29 ZmMailListPriorityGroup = function(group) {
 30     this.id = ZmId.GROUPBY_PRIORITY;
 31     ZmMailListGroup.call(this);
 32 };
 33 
 34 ZmMailListPriorityGroup.prototype = new ZmMailListGroup;
 35 ZmMailListPriorityGroup.prototype.constructor =  ZmMailListPriorityGroup;
 36 
 37 ZmMailListPriorityGroup.IMPORTANT_UNREAD = "IMPORTANT_UNREAD";
 38 ZmMailListPriorityGroup.IMPORTANT_READ = "IMPORTANT_READ";
 39 ZmMailListPriorityGroup.FLAGGED = "FLAGGED";
 40 ZmMailListPriorityGroup.EVERYTHING_ELSE = "EVERYTHING_ELSE";
 41 
 42 ZmMailListPriorityGroup.TAG = "Important";
 43 
 44 ZmMailListPriorityGroup.GROUP = [ZmMailListPriorityGroup.IMPORTANT_UNREAD, ZmMailListPriorityGroup.FLAGGED,
 45                                  ZmMailListPriorityGroup.IMPORTANT_READ, ZmMailListPriorityGroup.EVERYTHING_ELSE];
 46 
 47 ZmMailListPriorityGroup.SECTION_TITLE = {};
 48 ZmMailListPriorityGroup.SECTION_TITLE[ZmMailListPriorityGroup.IMPORTANT_READ] = ZmMsg.mailPriorityImportantRead;
 49 ZmMailListPriorityGroup.SECTION_TITLE[ZmMailListPriorityGroup.IMPORTANT_UNREAD] = ZmMsg.mailPriorityImportantUnread;
 50 ZmMailListPriorityGroup.SECTION_TITLE[ZmMailListPriorityGroup.FLAGGED] = ZmMsg.mailPriorityFlagged;
 51 ZmMailListPriorityGroup.SECTION_TITLE[ZmMailListPriorityGroup.EVERYTHING_ELSE] = ZmMsg.mailPriorityEverythingElse;
 52 
 53 ZmMailListPriorityGroup.prototype.getAllSections =
 54 function() {
 55     var htmlArr = [];
 56     for (var i = 0; i<ZmMailListPriorityGroup.GROUP.length; i++) {
 57         if (this._section[ZmMailListPriorityGroup.GROUP[i]].length > 0) {
 58             htmlArr.push(this.getSectionHeader(ZmMailListPriorityGroup.SECTION_TITLE[ZmMailListPriorityGroup.GROUP[i]]));
 59             htmlArr.push(this._section[ZmMailListPriorityGroup.GROUP[i]].join(""));
 60         }
 61         else if (this._showEmptySectionHeader) {
 62             htmlArr.push(this.getSectionHeader(ZmMailListPriorityGroup.SECTION_TITLE[ZmMailListPriorityGroup.GROUP[i]]));
 63         }
 64     }
 65     return htmlArr.join("");
 66 };
 67 
 68 ZmMailListPriorityGroup.prototype.addMsgToSection =
 69 function(msg, item){
 70    for (var i = 0; i<ZmMailListPriorityGroup.GROUP.length; i++) {
 71        if (this.isMsgInSection(ZmMailListPriorityGroup.GROUP[i], msg)) {
 72            this._section[ZmMailListPriorityGroup.GROUP[i]].push(item);
 73            return true;
 74        }
 75    }
 76    return false;
 77 };
 78 
 79 ZmMailListPriorityGroup.prototype.isMsgInSection =
 80 function(section, msg) {
 81     switch(section) {
 82         case ZmMailListPriorityGroup.IMPORTANT_UNREAD:
 83             return this._isImportantAndUnread(msg);
 84 
 85         case ZmMailListPriorityGroup.IMPORTANT_READ:
 86             return this._isImportantAndRead(msg);
 87 
 88         case ZmMailListPriorityGroup.FLAGGED:
 89             return this._isFlagged(msg);
 90 
 91         case ZmMailListPriorityGroup.EVERYTHING_ELSE:
 92            return this._noMatchingGroup(msg);
 93 
 94         default:
 95             return false;
 96     }
 97 };
 98 
 99 ZmMailListPriorityGroup.prototype._init =
100 function() {
101 	this._section = {};
102     this._section[ZmMailListPriorityGroup.IMPORTANT_UNREAD] = [];
103     this._section[ZmMailListPriorityGroup.IMPORTANT_READ] = [];
104     this._section[ZmMailListPriorityGroup.FLAGGED] = [];
105     this._section[ZmMailListPriorityGroup.EVERYTHING_ELSE] = [];
106 	if (!this._importantTag) {
107 		this._importantTag = this._getImportantTag();
108 	}
109 };
110 
111 ZmMailListPriorityGroup.prototype._isImportantAndUnread =
112 function(msg){
113    if (msg && this._importantTag) {
114        if (msg.hasTag(this._importantTag.id) && msg.isUnread) {
115            return true;
116        }
117    }
118    return false;
119 };
120 
121 ZmMailListPriorityGroup.prototype._isImportantAndRead =
122 function(msg) {
123   if (msg && this._importantTag) {
124       if (msg.hasTag(this._importantTag.id) && !msg.isUnread) {
125           return true;
126       }
127   }
128   return false;
129 };
130 
131 ZmMailListPriorityGroup.prototype._isFlagged =
132 function(msg) {
133    if (msg) {
134        return msg.isFlagged;
135    }
136    return false;
137 };
138 
139 
140 ZmMailListPriorityGroup.prototype._noMatchingGroup =
141 function(msg) {
142     if (!this._isImportantAndUnread(msg) && !this._isImportantAndRead(msg) &&
143         !this._isFlagged(msg)) {
144         return true;
145     }
146     return false;
147 };
148 
149 ZmMailListPriorityGroup.prototype._getImportantTag =
150 function() {
151 	var tagList = appCtxt.getTagTree();
152 	if (tagList) {
153 	   return tagList.getByName("Important");
154 	}
155 	return null;
156 };