1 /*
  2  * ***** BEGIN LICENSE BLOCK *****
  3  * Zimbra Collaboration Suite Web Client
  4  * Copyright (C) 2008, 2009, 2010, 2011, 2013, 2014, 2015, 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) 2008, 2009, 2010, 2011, 2013, 2014, 2015, 2016 Synacor, Inc. All Rights Reserved.
 21  * ***** END LICENSE BLOCK *****
 22  */
 23 
 24 /**
 25  * Creates the briefcase tab view.
 26  * @class
 27  * This class represents the briefcase tab view.
 28  * 
 29  * @param	{ZmControl}		parent	the parent
 30  * @param	{String}		className		the class name
 31  * @param	{constant}		posStyle		the position style
 32  * 
 33  * @extends		DwtTabViewPage
 34  * 
 35  * @see			Dwt.STATIC_STYLE
 36  */
 37 ZmBriefcaseTabView = function(parent,className,posStyle){
 38 	this._app = appCtxt.getApp(ZmApp.BRIEFCASE);
 39 	this.view = ZmId.VIEW_BRIEFCASE_ICON;
 40 	DwtComposite.call(this,parent,className,Dwt.STATIC_STYLE);
 41     this._createHtml();
 42     this.showMe();
 43 };
 44 
 45 ZmBriefcaseTabView.prototype = new DwtComposite;
 46 ZmBriefcaseTabView.prototype.constructor = ZmBriefcaseTabView;
 47 
 48 /**
 49  * Returns a string representation of the object.
 50  * 
 51  * @return		{String}		a string representation of the object
 52  */
 53 ZmBriefcaseTabView.prototype.toString = function(){
 54     return "ZmBriefcaseTabView";
 55 };
 56 
 57 /**
 58  * Shows this view.
 59  * 
 60  */
 61 ZmBriefcaseTabView.prototype.showMe =
 62 function() {
 63     this.setSize(500, 295);
 64     this.showFolder(this._folderId || ZmOrganizer.ID_BRIEFCASE);
 65 };
 66 
 67 /**
 68  * Hides this view.
 69  * 
 70  */
 71 ZmBriefcaseTabView.prototype.hideMe =
 72 function() {
 73 	this._itemCountText.setVisible(false);
 74 };
 75 
 76 //Create UI for Briefcase Tab UI
 77 ZmBriefcaseTabView.prototype._createHtml =
 78 function() {
 79 
 80     this._tableID = Dwt.getNextId();
 81     this._folderTreeCellId = Dwt.getNextId();
 82     this._folderListId = Dwt.getNextId();
 83     var html = [];
 84     var idx = 0;
 85     html[idx++] = ['<table class="ZmBriefcaseTabView_Table" id="', this._tableID, '" cellspacing="0" cellpadding="0" border="0">'].join("");
 86     html[idx++] = '<tr>';
 87     html[idx++] = ['<td width="30%" valign="top"  id="', this._folderTreeCellId, '">'].join("");
 88     html[idx++] = '</td>';
 89     html[idx++] = ['<td width="70%" valign="top" id="', this._folderListId, '">'].join("");
 90     html[idx++] = '</td>';
 91     html[idx++] = '</tr>';
 92     html[idx++] = '</table>';
 93     this.setContent(html.join(""));
 94     
 95     this.showBriefcaseTreeView();
 96 
 97 	var loadCallback = new AjxCallback(this, this._createHtml1);
 98 	AjxDispatcher.require(["BriefcaseCore", "Briefcase"], false, loadCallback);
 99 };
100 
101 ZmBriefcaseTabView.prototype._createHtml1 =
102 function() {
103 
104  	this._app = appCtxt.getApp(ZmApp.BRIEFCASE);
105 	var bc = this._controller = new ZmBriefcaseController(this._app._container, this._app);
106 
107     var params = {parent:bc._container, className:"BriefcaseTabBox BriefcaseList", view:this.view,
108 				  controller:bc};
109     var lv = this._listView = this._controller._listView[this.view] = new ZmBriefcaseIconView(params);
110 	this._controller._currentViewId = this.view;
111     lv.reparentHtmlElement(this._folderListId);
112     Dwt.setPosition(lv.getHtmlElement(),Dwt.RELATIVE_STYLE);
113 };
114 
115 ZmBriefcaseTabView.prototype.setSize =
116 function(width, height) {
117 
118     var treeWidth = width * 0.40;
119     var listWidth = width - treeWidth;
120     var newHeight = height - 15;
121 	if (this._overview) {
122 		this._overview.setSize(treeWidth, newHeight);
123 		this._listView.setSize(listWidth - 11, newHeight);
124 	}
125 	
126     return this;
127 };
128 
129 /**
130  * Shows the folder.
131  * 
132  * @param	{String}	folderId		the folder id
133  */
134 ZmBriefcaseTabView.prototype.showFolder =
135 function(folderId) {
136     this._folderId = folderId;
137     var callback = new AjxCallback(this, this.showFolderContents, [folderId]);
138     var params = {
139         folderId:folderId,
140         callback:callback,
141         noRender:true
142     };
143     if (appCtxt.multiAccounts) {
144         params.accountName = appCtxt.getAppViewMgr().getCurrentView().getFromAccount().name;
145     }
146     this._app.search(params);
147 };
148 
149 /**
150  * Shows the folder contents.
151  * 
152  * @param	{String}	folderId		the folder id
153  * @param	{Object}	results			the results
154  */
155 ZmBriefcaseTabView.prototype.showFolderContents =
156 function(folderId, results) {
157 	var searchResult = results.getResponse();
158 	if (searchResult) {
159 		var list = this._controller._list = searchResult.getResults(ZmItem.BRIEFCASE_ITEM);
160 		this._controller._list.setHasMore(searchResult.getAttribute("more"));
161 		ZmListController.prototype.show.call(this._controller, searchResult, ZmId.VIEW_BRIEFCASE_ICON);
162 		this._listView.set(list);
163         this._listView.focus();
164 		this._controller._setItemCountText();
165 	}
166 };
167 
168 ZmBriefcaseTabView.prototype._handleKeys =
169 function(ev){
170     var key = DwtKeyEvent.getCharCode(ev);
171     return !DwtKeyEvent.IS_RETURN[key];
172 };
173 
174 ZmBriefcaseTabView.prototype.gotAttachments =
175 function() {
176     return false;
177 };
178 
179 ZmBriefcaseTabView.prototype.uploadFiles =
180 function(attachDialog, docIds) {
181 
182     if (!docIds) {
183         docIds = [];
184         var items = this._listView.getSelection();
185         if (!items || (items.length == 0)) {
186             var attachDialog = appCtxt.getAttachDialog();
187             attachDialog.setFooter(ZmMsg.attachSelectMessage);
188             return;
189         }
190         for (var i in items) {
191             docIds.push({id: items[i].id, ct: items[i].contentType, s: items[i].size});
192         }
193     }
194 
195 	docIds = AjxUtil.toArray(docIds);
196 
197     var callback = attachDialog.getUploadCallback();
198     if (callback) {
199         callback.run(AjxPost.SC_OK, null, docIds);
200     }
201 };
202 
203 ZmBriefcaseTabView.prototype.showBriefcaseTreeView =
204 function() {
205 
206     //Force create deferred folders if not created
207     var aCtxt = appCtxt.isChildWindow ? parentAppCtxt : appCtxt;
208     var briefcaseApp = aCtxt.getApp(ZmApp.BRIEFCASE);
209     briefcaseApp._createDeferredFolders();
210 
211     var base = this.toString();
212     var acct = appCtxt.getActiveAccount();
213     var params = {
214         treeIds: [ZmOrganizer.BRIEFCASE],
215         fieldId: this._folderTreeCellId,
216         overviewId: (appCtxt.multiAccounts) ? ([base, acct.name].join(":")) : base,
217         account: acct
218     };
219     this._setOverview(params);
220 
221 };
222 
223 ZmBriefcaseTabView.prototype._setOverview =
224 function(params) {
225     var overviewId = params.overviewId;
226     var opc = appCtxt.getOverviewController();
227     var overview = opc.getOverview(overviewId);
228     if (!overview) {
229         var ovParams = {
230             overviewId: overviewId,
231             overviewClass: "BriefcaseTabBox",
232             headerClass: "DwtTreeItem",
233             noTooltips: true,
234             treeIds: params.treeIds
235         };
236         overview = this._overview = opc.createOverview(ovParams);
237         overview.set(params.treeIds);
238         document.getElementById(params.fieldId).appendChild(overview.getHtmlElement());
239         var treeView = overview.getTreeView(ZmOrganizer.BRIEFCASE);
240         treeView.addSelectionListener(new AjxListener(this, this._treeListener));
241         this._hideRoot(treeView);
242     } else if (params.account) {
243         overview.account = params.account;
244     }
245 };
246 
247 ZmBriefcaseTabView.prototype._treeListener =
248 function(ev) {
249     if (ev.detail == DwtTree.ITEM_SELECTED) {
250         var ti = ev.item;
251         var folder = ti.getData(Dwt.KEY_OBJECT);
252         if (folder) {
253             this.showFolder(folder.id);
254         }
255     }
256 };
257 
258 ZmBriefcaseTabView.prototype._hideRoot =
259 function(treeView) {
260     var ti = treeView.getTreeItemById(ZmOrganizer.ID_ROOT);
261     if (!ti) {
262         var rootId = ZmOrganizer.getSystemId(ZmOrganizer.ID_ROOT)
263         ti = treeView.getTreeItemById(rootId);
264     }
265     ti.showCheckBox(false);
266     ti.setExpanded(true);
267     ti.setVisible(false, true);
268 };
269