1 /*
  2  * ***** BEGIN LICENSE BLOCK *****
  3  * Zimbra Collaboration Suite Web Client
  4  * Copyright (C) 2009, 2010, 2012, 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) 2009, 2010, 2012, 2013, 2014, 2015, 2016 Synacor, Inc. All Rights Reserved.
 21  * ***** END LICENSE BLOCK *****
 22  */
 23 
 24 /**
 25  * Creates the mobile devices page.
 26  * @class
 27  * This class represents the mobile devices page.
 28  * 
 29  * @param {DwtControl}	parent			the containing widget
 30  * @param {Object}	section			the page
 31  * @param {ZmPrefController}	controller		the prefs controller
 32  * 
 33  * @extends	ZmPreferencesPage
 34  * 
 35  * @private
 36  */
 37 ZmMobileDevicesPage = function(parent, section, controller) {
 38 	ZmPreferencesPage.apply(this, arguments);
 39 
 40 	this._deviceController = controller.getMobileDevicesController();
 41 };
 42 
 43 ZmMobileDevicesPage.prototype = new ZmPreferencesPage;
 44 ZmMobileDevicesPage.prototype.constructor = ZmMobileDevicesPage;
 45 
 46 ZmMobileDevicesPage.prototype.toString =
 47 function () {
 48     return "ZmMobileDevicesPage";
 49 };
 50 
 51 
 52 // ZmPreferencesPage methods
 53 
 54 /**
 55  * @private
 56  */
 57 ZmMobileDevicesPage.prototype.showMe =
 58 function() {
 59 	ZmPreferencesPage.prototype.showMe.apply(this, arguments);
 60 
 61 	if (!this._rendered) {
 62 		var params = {
 63 			parent:this,
 64 			buttons:this._deviceController.getToolbarButtons(),
 65 			posStyle:Dwt.STATIC_STYLE,
 66 			context:ZmId.VIEW_MOBILE_DEVICES,
 67 			parentElement:(this._htmlElId+"_deviceToolbar")
 68 		};
 69 		this._toolbar = new ZmButtonToolBar(params);
 70 		params = {
 71 			parent:this,
 72 			parentElement:(this._htmlElId+"_deviceList")
 73 		};
 74 		this.listView = new ZmMobileDeviceListView(params);
 75 		this.listView.setMultiSelect(false);
 76 
 77 		this._deviceController.initialize(this._toolbar, this.listView);
 78 
 79         var params1 = {
 80             parent: this,
 81             parentElement: (this._htmlElId + "_oauthconsumerapps"),
 82             type: ZmMobileDevice.TYPE_OAUTH
 83         };
 84         this.oAuthAppsListView = new ZmMobileDeviceListView(params1);
 85         this._deviceController.initializeOAuthAppListView(this.oAuthAppsListView);
 86 
 87         var pageId = appCtxt.getApp(ZmApp.PREFERENCES).getPrefController().getPrefsView().getView("MOBILE").getHTMLElId();
 88         this._addListView(this.oAuthAppsListView, pageId + "_oauthconsumerapps");
 89         this._rendered = true;
 90 	}
 91 	this._deviceController.loadDeviceInfo();
 92     this._deviceController.loadOAuthConsumerAppInfo();
 93 };
 94 
 95 ZmMobileDevicesPage.prototype.reset = function(useDefaults) {
 96     var deviceController = this._deviceController;
 97     ZmPreferencesPage.prototype.reset.apply(this, arguments);
 98 
 99     if (this._controller.getTabView().getActiveView() == this) {
100         deviceController.loadDeviceInfo();
101         deviceController.loadOAuthConsumerAppInfo();
102     }
103 };
104 
105 ZmMobileDevicesPage.prototype.hasResetButton =
106 function() {
107 	return false;
108 };
109 
110 ZmMobileDevicesPage.prototype._addListView = function(listView, listViewDivId) {
111         var listDiv = document.getElementById(listViewDivId);
112         listDiv.appendChild(listView.getHtmlElement());
113         listView.setUI(null, true);
114         listView._initialized = true;
115 };
116 
117 
118 /**
119  * Creates a mobile device list.
120  * @class
121  * A list view that displays user mobile devices. The data is in the form of a
122  * list of {@link ZmMobileDevice} objects.
123  *
124  * @param {Hash}	params	a hash of parameters
125  * 
126  * 
127  * @extends		DwtListView
128  * 
129  * @private
130  */
131 ZmMobileDeviceListView = function(params) {
132     this.type = params.type;
133 	params.headerList = this._getHeaderList();
134 	DwtListView.call(this, params);
135 };
136 
137 ZmMobileDeviceListView.prototype = new DwtListView;
138 ZmMobileDeviceListView.prototype.constructor = ZmMobileDeviceListView;
139 
140 
141 // Consts
142 
143 ZmMobileDeviceListView.F_DEVICE			= "de";
144 ZmMobileDeviceListView.F_STATUS			= "st";
145 ZmMobileDeviceListView.F_ID				= "id";
146 ZmMobileDeviceListView.F_PROTOCOL		= "pr";
147 ZmMobileDeviceListView.F_PROVISIONABLE	= "pv";
148 
149 ZmMobileDeviceListView.F_APP		    = "ap";
150 ZmMobileDeviceListView.F_APPDEVICE	    = "ad";
151 ZmMobileDeviceListView.F_APPROVED	    = "ar";
152 ZmMobileDeviceListView.F_ACTIONS	    = "ac";
153 
154 
155 // Public methods
156 
157 ZmMobileDeviceListView.prototype.toString =
158 function() {
159 	return "ZmMobileDeviceListView";
160 };
161 
162 ZmMobileDeviceListView.prototype._getHeaderList =
163 function() {
164 
165 	var headerList = [];
166     if (this.type === ZmMobileDevice.TYPE_OAUTH) {
167         headerList.push(new DwtListHeaderItem({field:ZmMobileDeviceListView.F_APP, text:ZmMsg.oAuthApp, width:ZmMsg.COLUMN_WIDTH_ID_MDL}));
168         headerList.push(new DwtListHeaderItem({field:ZmMobileDeviceListView.F_APPDEVICE, text:ZmMsg.oAuthAppDevice, width:ZmMsg.COLUMN_WIDTH_ID_MDL}));
169         headerList.push(new DwtListHeaderItem({field:ZmMobileDeviceListView.F_APPROVED, text:ZmMsg.oAuthAppApprovedDate, width:ZmMsg.COLUMN_WIDTH_STATUS_MDL}));
170         headerList.push(new DwtListHeaderItem({field:ZmMobileDeviceListView.F_ACTIONS, width:ZmMsg.COLUMN_WIDTH_PROTOCOL_MDL}));
171     }
172     else {
173         headerList.push(new DwtListHeaderItem({field: ZmMobileDeviceListView.F_DEVICE, text: ZmMsg.mobileDevice}));
174         headerList.push(new DwtListHeaderItem({field: ZmMobileDeviceListView.F_ID, text: ZmMsg.mobileDeviceId, width: ZmMsg.COLUMN_WIDTH_ID_MDL}));
175         headerList.push(new DwtListHeaderItem({field: ZmMobileDeviceListView.F_STATUS, text: ZmMsg.status, width: ZmMsg.COLUMN_WIDTH_STATUS_MDL}));
176         headerList.push(new DwtListHeaderItem({field: ZmMobileDeviceListView.F_PROTOCOL, text: ZmMsg.mobileProtocolVersion, width: ZmMsg.COLUMN_WIDTH_PROTOCOL_MDL}));
177         headerList.push(new DwtListHeaderItem({field: ZmMobileDeviceListView.F_PROVISIONABLE, text: ZmMsg.mobileProvisionable, width: ZmMsg.COLUMN_WIDTH_PROVISIONABLE_MDL}));
178     }
179     return headerList;
180 };
181 
182 ZmMobileDeviceListView.prototype._getCellContents =
183 function(html, idx, item, field, colIdx, params) {
184 
185         if (field == ZmMobileDeviceListView.F_DEVICE) {
186             html[idx++] = '<span style="white-space:nowrap">';
187             html[idx++] = item.type;
188             if (item.ua) {
189                 html[idx++] = " (";
190                 html[idx++] = item.ua;
191                 html[idx++] = ")";
192             }
193             html[idx++] = "</span>";
194         } else if (field == ZmMobileDeviceListView.F_STATUS) {
195             html[idx++] = item.getStatusString();
196         } else if (field == ZmMobileDeviceListView.F_ID) {
197             html[idx++] = item.id;
198         } else if (field == ZmMobileDeviceListView.F_PROTOCOL) {
199             html[idx++] = item.protocol;
200         } else if (field == ZmMobileDeviceListView.F_PROVISIONABLE) {
201             html[idx++] = item.provisionable ? AjxMsg.yes : AjxMsg.no;
202         } else if (field == ZmMobileDeviceListView.F_APP) {
203             html[idx++] = item.appName;
204         } else if (field == ZmMobileDeviceListView.F_APPDEVICE) {
205             html[idx++] = item.device;
206         } else if (field == ZmMobileDeviceListView.F_APPROVED) {
207             var approvedOn = item.approvedOn;
208             html[idx++] = AjxDateFormat.getDateInstance(AjxDateFormat.MEDIUM).format(new Date(parseInt(approvedOn)));
209         } else if (field == ZmMobileDeviceListView.F_ACTIONS) {
210             html[idx++] = "<a href = 'javascript:;' onclick = 'ZmMobileDevicesController.handleRemoveOauthConsumerApp(this, " + '"' + item.accessToken + '", "' + item.appName + '", "' + item.device + '" ' + " );'>" + ZmMsg.remove + "</a> ";
211         }
212 
213     return idx;
214 };
215