1 /*
  2  * ***** BEGIN LICENSE BLOCK *****
  3  * Zimbra Collaboration Suite Web Client
  4  * Copyright (C) 2008, 2009, 2010, 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) 2008, 2009, 2010, 2011, 2013, 2014, 2016 Synacor, Inc. All Rights Reserved.
 21  * ***** END LICENSE BLOCK *****
 22  */
 23 
 24 /**
 25  * This class represents the import view.
 26  * 
 27  * @extends		ZmImportExportBaseView
 28  * @private
 29  */
 30 ZmImportView = function(params) {
 31 	if (arguments.length == 0) { return; }
 32 
 33 	// setup form
 34 	params.form = {
 35 		items: [
 36 			// default items
 37 			{ id: "TYPE", type: "DwtRadioButtonGroup", value: ZmImportExportController.TYPE_TGZ,
 38 				items: [
 39 					{ id: "TYPE_TGZ", label: ZmMsg.importExportTypeTGZ, value: ZmImportExportController.TYPE_TGZ },
 40 					{ id: "TYPE_ICS", label: ZmMsg.importExportTypeICS, value: ZmImportExportController.TYPE_ICS },
 41 					{ id: "TYPE_CSV", label: ZmMsg.importExportTypeCSV, value: ZmImportExportController.TYPE_CSV }
 42 				],
 43 				onclick: this._type_onclick
 44 			},
 45 			{ id: "TYPE_HINT", type: "DwtText" },
 46 			{ id: "SUBTYPE", type: "DwtSelect",
 47 				visible: "get('TYPE') == ZmImportExportController.TYPE_CSV && String(get('FILE')).match(/\\.csv$/i)"
 48 			},
 49 			{ id: "FOLDER_BUTTON", type: "DwtButton", label: ZmMsg.browse,
 50 				enabled: "get('FILE')",
 51 				onclick: this._folderButton_onclick
 52 			},
 53 			{ id: "FORM" },
 54 			{ id: "FILE",
 55 				setter: new Function() // no-op -- can't set a file value
 56 			},
 57 			{ id: "RESOLVE", type: "DwtRadioButtonGroup", value: "ignore",
 58 				items: [
 59 					{ id: "RESOLVE_IGNORE", label: ZmMsg.resolveDuplicateIgnore, value: "ignore" },
 60 					{ id: "RESOLVE_MODIFY", label: ZmMsg.resolveDuplicateModify, value: "modify" },
 61 					{ id: "RESOLVE_REPLACE", label: ZmMsg.resolveDuplicateReplace, value: "replace" },
 62 					{ id: "RESOLVE_RESET", label: ZmMsg.resolveDuplicateReset, value: "reset" }
 63 				],
 64 				visible: "get('FILE') && get('TYPE') == ZmImportExportController.TYPE_TGZ"
 65 			},
 66 			{ id: "ADVANCED", type: "DwtCheckbox", label: ZmMsg.advancedSettings,
 67 				visible: "get('FILE') && get('TYPE') == ZmImportExportController.TYPE_TGZ"
 68 			},
 69 			// advanced
 70 			{ id: "DATA_TYPES", type: "ZmImportExportDataTypes",
 71 				visible: "get('ADVANCED')"
 72 			}
 73 		]
 74 	};
 75 	params.id = "ZmImportView";
 76 	ZmImportExportBaseView.call(this, params);
 77 
 78 	// add change listener to file input
 79 	var form = this.getControl("FORM");
 80 	var file = form && form.elements["file"];
 81 	if (file) {
 82 		file.onchange = AjxCallback.simpleClosure(this._handleFileChange, this, file);
 83 	}
 84 };
 85 ZmImportView.prototype = new ZmImportExportBaseView;
 86 ZmImportView.prototype.constructor = ZmImportView;
 87 
 88 ZmImportView.prototype.toString = function() {
 89 	return "ZmImportView";
 90 };
 91 
 92 //
 93 // Constants
 94 //
 95 
 96 ZmImportView.prototype.TYPE_HINTS = {};
 97 ZmImportView.prototype.TYPE_HINTS[ZmImportExportController.TYPE_CSV] = ZmMsg.importFromCSVHint;
 98 ZmImportView.prototype.TYPE_HINTS[ZmImportExportController.TYPE_ICS] = ZmMsg.importFromICSHint;
 99 ZmImportView.prototype.TYPE_HINTS[ZmImportExportController.TYPE_TGZ] = ZmMsg.importFromTGZHint;
100 
101 //
102 // Data
103 //
104 
105 ZmImportView.prototype.TEMPLATE = "data.ImportExport#ImportView";
106 
107 //
108 // Public methods
109 //
110 
111 /**
112  * Returns a params object that can be used to directly call
113  * ZmImportExportController#exportData.
114  */
115 ZmImportView.prototype.getParams = function() {
116 	var form = this.getControl("FORM");
117 	var filename = form && form.elements["file"].value;
118 	var ext = filename && filename.replace(/^.*\./,"").toLowerCase();
119 	var type = ext || this.getValue("TYPE") || ZmImportExportController.TYPE_TGZ;
120 	var isTGZ = type == ZmImportExportController.TYPE_TGZ;
121 	var params = {
122 		// required
123 		form:		form,
124 		// optional -- ignore if not relevant
125 		type:		type,
126 		subType:	this.isRelevant("SUBTYPE") ? this.getValue("SUBTYPE") : null,
127 		views:		this.isRelevant("DATA_TYPES") ? this.getValue("DATA_TYPES") : null,
128 		resolve:	this.isRelevant("RESOLVE") && isTGZ ? this.getValue("RESOLVE") : null,
129 		folderId:	this._folderId,
130 		dataTypes:	this.isRelevant("DATA_TYPES") ? this.getValue("DATA_TYPES") : null
131 	};
132 	if (params.resolve == "ignore") {
133 		delete params.resolve;
134 	}
135 	return params;
136 };
137 
138 //
139 // Protected methods
140 //
141 
142 ZmImportView.prototype._getSubTypeOptions = function(type) {
143 	var options = ZmImportExportBaseView.prototype._getSubTypeOptions.apply(this, arguments);
144 	if (type == ZmImportExportController.TYPE_CSV) {
145 		options = [].concat({ displayValue: ZmMsg.importAutoDetect, value: "" }, options);
146 	}
147 	return options;
148 };
149 
150 ZmImportView.prototype._handleFileChange = function(file) {
151 	var filename = file.value;
152 	var ext = filename.replace(/^.*\./,"").toLowerCase();
153 	var type = ZmImportExportController.EXTS_TYPE[ext];
154 	if (type) {
155 		this.set("TYPE", type);
156 	}
157 };
158