1 /*
  2  * ***** BEGIN LICENSE BLOCK *****
  3  * Zimbra Collaboration Suite Web Client
  4  * Copyright (C) 2011, 2013, 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, 2013, 2014, 2016 Synacor, Inc. All Rights Reserved.
 21  * ***** END LICENSE BLOCK *****
 22  */
 23 
 24 /**
 25  * Size group divides messages into the following sections:
 26  * Enormous > 5MB
 27  * Huge 1-5MB
 28  * Very Large 500KB-1MB
 29  * Large 100KB-500KB
 30  * Medium 25KB-100KB
 31  * Small 10KB-25KB
 32  * Tiny < 10KB
 33  *
 34  */
 35 ZmMailListSizeGroup = function(){
 36     this.id = ZmId.GROUPBY_SIZE;
 37 	this.field = ZmItem.F_SIZE;
 38     ZmMailListGroup.call(this);
 39 };
 40 
 41 ZmMailListSizeGroup.prototype = new ZmMailListGroup;
 42 ZmMailListSizeGroup.prototype.constructor =  ZmMailListSizeGroup;
 43 
 44 ZmMailListSizeGroup.ENORMOUS = "ENORMOUS";
 45 ZmMailListSizeGroup.HUGE = "HUGE";
 46 ZmMailListSizeGroup.VERY_LARGE = "VERY_LARGE";
 47 ZmMailListSizeGroup.LARGE = "LARGE";
 48 ZmMailListSizeGroup.MEDIUM = "MEDIUM";
 49 ZmMailListSizeGroup.SMALL = "SMALL";
 50 ZmMailListSizeGroup.TINY = "TINY";
 51 
 52 ZmMailListSizeGroup.KILOBYTE = 1024;
 53 ZmMailListSizeGroup.MEGABYTE = 1024 * 1024;
 54 
 55 ZmMailListSizeGroup.GROUP = [ZmMailListSizeGroup.ENORMOUS, ZmMailListSizeGroup.HUGE, ZmMailListSizeGroup.VERY_LARGE,
 56                              ZmMailListSizeGroup.LARGE, ZmMailListSizeGroup.MEDIUM, ZmMailListSizeGroup.SMALL, ZmMailListSizeGroup.TINY];
 57 
 58 ZmMailListSizeGroup.SIZE = {};
 59 ZmMailListSizeGroup.SIZE[ZmMailListSizeGroup.ENORMOUS] = {min: 5 * ZmMailListSizeGroup.MEGABYTE - ZmMailListSizeGroup.MEGABYTE/2}; // > 4.5MB
 60 ZmMailListSizeGroup.SIZE[ZmMailListSizeGroup.HUGE] = {min: (ZmMailListSizeGroup.MEGABYTE) - 512, max: (5 * ZmMailListSizeGroup.MEGABYTE) - ZmMailListSizeGroup.MEGABYTE/2};    //1023.5KB - 4.5MB
 61 ZmMailListSizeGroup.SIZE[ZmMailListSizeGroup.VERY_LARGE] = {min: (500 * ZmMailListSizeGroup.KILOBYTE) - 512, max: ZmMailListSizeGroup.MEGABYTE - 512}; //499.5KB - 1023.5KB
 62 ZmMailListSizeGroup.SIZE[ZmMailListSizeGroup.LARGE] = {min: 100 * ZmMailListSizeGroup.KILOBYTE - 512, max: (500 * ZmMailListSizeGroup.KILOBYTE) - 512};//99.5KB - 499.5KB
 63 ZmMailListSizeGroup.SIZE[ZmMailListSizeGroup.MEDIUM] = {min: 25 * ZmMailListSizeGroup.KILOBYTE -512, max: (100 * ZmMailListSizeGroup.KILOBYTE)- 512};  //24.5KB - 99.5KB
 64 ZmMailListSizeGroup.SIZE[ZmMailListSizeGroup.SMALL] = {min: 10 * ZmMailListSizeGroup.KILOBYTE - 512, max: (25 * ZmMailListSizeGroup.KILOBYTE) - 512}; //9.5KB - 24.5KB
 65 ZmMailListSizeGroup.SIZE[ZmMailListSizeGroup.TINY] = {max: (10 * ZmMailListSizeGroup.KILOBYTE) - 512}; // < 9.5KB
 66 
 67 ZmMailListSizeGroup.SECTION_TITLE = {};
 68 ZmMailListSizeGroup.SECTION_TITLE[ZmMailListSizeGroup.ENORMOUS] = ZmMsg.mailSizeEnormousTitle;
 69 ZmMailListSizeGroup.SECTION_TITLE[ZmMailListSizeGroup.HUGE] = ZmMsg.mailSizeHugeTitle;
 70 ZmMailListSizeGroup.SECTION_TITLE[ZmMailListSizeGroup.VERY_LARGE] = ZmMsg.mailSizeVeryLargeTitle;
 71 ZmMailListSizeGroup.SECTION_TITLE[ZmMailListSizeGroup.LARGE] = ZmMsg.mailSizeLargeTitle;
 72 ZmMailListSizeGroup.SECTION_TITLE[ZmMailListSizeGroup.MEDIUM] = ZmMsg.mailSizeMediumTitle;
 73 ZmMailListSizeGroup.SECTION_TITLE[ZmMailListSizeGroup.SMALL] = ZmMsg.mailSizeSmallTitle;
 74 ZmMailListSizeGroup.SECTION_TITLE[ZmMailListSizeGroup.TINY] =  ZmMsg.mailSizeTinyTitle;
 75 
 76 /**
 77  *  returns HTML string for all sections.
 78  *  @param {boolean} sortAsc    true/false if sort ascending
 79  *  @return {String} HTML for all sections including section header
 80  * @param sortAsc
 81  */
 82 ZmMailListSizeGroup.prototype.getAllSections =
 83 function(sortAsc) {
 84     var keys = ZmMailListSizeGroup.GROUP.slice(0); //copy group into keys
 85     var htmlArr = [];
 86 
 87     if (sortAsc) {
 88         keys.reverse(); //sort ascending
 89     }
 90 
 91     for (var i=0; i<keys.length; i++) {
 92        if (this._section[keys[i]].length > 0) {
 93             htmlArr.push(this.getSectionHeader(ZmMailListSizeGroup.SECTION_TITLE[keys[i]]));
 94             htmlArr.push(this._section[keys[i]].join(""));
 95        }
 96        else if (this._showEmptySectionHeader) {
 97             htmlArr.push(this.getSectionHeader(ZmMailListSizeGroup.SECTION_TITLE[keys[i]]));
 98        }
 99     }
100 
101     return htmlArr.join("");
102 };
103 
104 /**
105  * Adds item to section
106  * @param {ZmMailMsg} msg   mail message
107  * @param {String} item  HTML to add to section
108  * @return {String} section returns section if successfully added, else returns null
109  */
110 ZmMailListSizeGroup.prototype.addMsgToSection =
111 function(msg, item){
112    for (var i = 0; i<ZmMailListSizeGroup.GROUP.length; i++) {
113        if (this.isMsgInSection(ZmMailListSizeGroup.GROUP[i], msg)) {
114            this._section[ZmMailListSizeGroup.GROUP[i]].push(item);
115            return ZmMailListSizeGroup.GROUP[i];
116        }
117    }
118 
119    return null;
120 };
121 
122 /**
123  * Determines if message is in group
124  * @param {String} section ID of section
125  * @param {ZmMailMsg} msg
126  * @return {boolean} true/false
127  */
128 ZmMailListSizeGroup.prototype.isMsgInSection =
129 function(section, msg) {
130     var size = msg.size;
131     if (!size && msg.type == ZmId.ITEM_CONV) {
132         size = msg.sf;
133     }
134     switch(section) {
135         case ZmMailListSizeGroup.ENORMOUS:
136             return this._isInSizeRange(size, section);
137 
138         case ZmMailListSizeGroup.HUGE:
139             return this._isInSizeRange(size, section);
140 
141         case ZmMailListSizeGroup.VERY_LARGE:
142             return this._isInSizeRange(size, section);
143 
144         case ZmMailListSizeGroup.LARGE:
145             return this._isInSizeRange(size, section);
146 
147         case ZmMailListSizeGroup.MEDIUM:
148            return this._isInSizeRange(size, section);
149 
150         case ZmMailListSizeGroup.SMALL:
151            return this._isInSizeRange(size, section);
152 
153         case ZmMailListSizeGroup.TINY:
154             return this._isInSizeRange(size, section);
155 
156         default:
157             return false;
158     }
159 
160 };
161 
162 /**
163  * Returns the sort by (ZmSearch.SIZE_ASC or ZmSearch.SIZE_DESC)
164  * @param {boolean} sortAsc
165  * @return {String} sortBy
166  */
167 ZmMailListSizeGroup.prototype.getSortBy =
168 function(sortAsc) {
169     if (sortAsc) {
170         return ZmSearch.SIZE_ASC;
171     }
172     return ZmSearch.SIZE_DESC;
173 };
174 
175 //PROTECTED METHODS
176 
177 ZmMailListSizeGroup.prototype._init =
178 function() {
179     this._section = {};
180     this._section[ZmMailListSizeGroup.ENORMOUS] = [];
181     this._section[ZmMailListSizeGroup.HUGE] = [];
182     this._section[ZmMailListSizeGroup.VERY_LARGE] = [];
183     this._section[ZmMailListSizeGroup.LARGE] = [];
184     this._section[ZmMailListSizeGroup.MEDIUM] = [];
185     this._section[ZmMailListSizeGroup.SMALL] = [];
186     this._section[ZmMailListSizeGroup.TINY] = [];
187 };
188 
189 ZmMailListSizeGroup.prototype._isInSizeRange =
190 function(size, section) {
191 	if (size >= 0 && section) {
192 		var min = ZmMailListSizeGroup.SIZE[section].min;
193 		var max = ZmMailListSizeGroup.SIZE[section].max;
194 		if (min && max) {
195 			return size >= ZmMailListSizeGroup.SIZE[section].min && size < ZmMailListSizeGroup.SIZE[section].max;
196 		}
197 		else if (max) {
198 			return size < ZmMailListSizeGroup.SIZE[section].max;
199 		}
200 		else if (min) {
201 			return size >= ZmMailListSizeGroup.SIZE[section].min;
202 		}
203 	}
204 	return false;
205 };
206 
207 ZmMailListSizeGroup.prototype._getSectionHeaderTitle =
208 function(section) {
209    if (ZmMailListSizeGroup.SECTION_TITLE[section]) {
210        return ZmMailListSizeGroup.SECTION_TITLE[section];
211    }
212 
213    return "";
214 };