Issues (4868)

api/js/etemplate/vfsSelectUI.js (2 issues)

1
/**
2
 * EGroupware - VFS SELECT Widget UI
3
 *
4
 * @link http://www.egroupware.org
5
 * @package et2_vfsSelect
6
 * @author Hadi Nategh <[email protected]>
7
 * @copyright (c) 2013-2017 by Hadi Nategh <[email protected]>
8
 * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
9
 * @version $Id$
10
 */
11
12
/**
13
 * UI for VFS Select widget
14
 *
15
 * @augments AppJS
16
 */
17
app.classes.vfsSelectUI = (function(){ "use strict"; return AppJS.extend(
18
{
19
	appname: 'filemanager',
20
	dirContent: {},
21
	vfsSelectWidget: {},
22
	path_widget: {},
23
	/**
24
	 * Constructor
25
	 *
26
	 * @memberOf app.filemanager
27
	 */
28
	init: function()
29
	{
30
		// call parent
31
		this._super.apply(this, arguments);
32
33
		this.egw.langRequireApp(this.egw.window, 'filemanager');
34
	},
35
36
	/**
37
	 * Destructor
38
	 */
39
	destroy: function()
40
	{
41
		delete this.path_widget;
42
		delete this.vfsSelectWidget;
43
		// call parent
44
		this._super.apply(this, arguments);
45
	},
46
47
	/**
48
	 * This function is called when the etemplate2 object is loaded
49
	 * and ready.  If you must store a reference to the et2 object,
50
	 * make sure to clean it up in destroy().
51
	 *
52
	 * @param et2 etemplate2 Newly ready object
53
	 * @param {string} name template name
54
	 */
55
	et2_ready: function(et2,name)
56
	{
57
		this.path_widget = this.et2.getWidgetById('path');
58
		this.dirContent = this.et2.getArrayMgr('content').data.dir;
59
	},
60
61
	/**
62
	 * Get directory of a path
63
	 *
64
	 * @param {string} _path
65
	 * @returns string
66
	 */
67
	dirname: function(_path)
68
	{
69
		var parts = _path.split('/');
70
		parts.pop();
71
		return parts.join('/') || '/';
72
	},
73
74
	/**
75
	 * Get name of a path
76
	 *
77
	 * @param {string} _path
78
	 * @returns string
79
	 */
80
	basename: function(_path)
81
	{
82
		return _path.split('/').pop();
83
	},
84
85
	/**
86
	 * Get current working directory
87
	 *
88
	 * @return string
89
	 */
90
	get_path: function()
91
	{
92
		return this.path_widget.get_value();
93
	},
94
95
	/**
96
	 * Send names of uploaded files (again) to server,
97
	 * to process them: either copy to vfs or ask overwrite/rename
98
	 *
99
	 * @param {event} _event
100
	 */
101
	storeFile: function(_event)
102
	{
103
		var path = this.get_path();
104
105
		if (!jQuery.isEmptyObject(_event.data.getValue()))
106
		{
107
			var widget = _event.data;
108
			egw(window).json('EGroupware\\Api\\Etemplate\\Widget\\Vfs::ajax_vfsSelect_storeFile', [widget.getValue(), path],
109
				this._storeFile_callback, this, true, this
110
			).sendRequest(true);
111
			widget.set_value('');
112
		}
113
	},
114
115
	/**
116
	 * Callback for server response to storeFile request:
117
	 * - display message and refresh list
118
	 * - ask use to confirm overwritting existing files or rename upload
119
	 *
120
	 * @param {object} _data values for attributes msg, files, ...
121
	 */
122
	_storeFile_callback: function(_data)
123
	{
124
		if (_data.msg || _data.uploaded) egw(window).message(_data.msg);
125
126
		var that = this;
127
		for(var file in _data.uploaded)
128
		{
129
			if (_data.uploaded[file].confirm && !_data.uploaded[file].confirmed)
130
			{
131
				var buttons = [
132
					{text: this.egw.lang("Yes"), id: "overwrite", class: "ui-priority-primary", "default": true, image: 'check'},
133
					{text: this.egw.lang("Rename"), id:"rename", image: 'edit'},
134
					{text: this.egw.lang("Cancel"), id:"cancel"}
135
				];
136
				if (_data.uploaded[file].confirm === "is_dir")
137
					buttons.shift();
138
				var dialog = et2_dialog.show_prompt(function(_button_id, _value) {
139
					var uploaded = {};
140
					uploaded[this.my_data.file] = this.my_data.data;
141
					switch (_button_id)
142
					{
143
						case "overwrite":
144
							uploaded[this.my_data.file].confirmed = true;
0 ignored issues
show
This node falls through to the next case due to this statement. Please add a comment either directly below this line or between the cases to explain.
Loading history...
145
							// fall through
146
						case "rename":
147
							uploaded[this.my_data.file].name = _value;
148
							delete uploaded[this.my_data.file].confirm;
149
							// send overwrite-confirmation and/or rename request to server
150
							egw.json('EGroupware\\Api\\Etemplate\\Widget\\Vfs::ajax_vfsSelect_storeFile', [uploaded, this.my_data.path],
151
								that._storeFile_callback, that, true, that
152
							).sendRequest();
153
							return;
154
						case "cancel":
155
							// Remove that file from every file widget...
156
							that.et2.iterateOver(function(_widget) {
157
								_widget.remove_file(this.my_data.data.name);
158
							}, this, et2_file);
159
					}
160
				},
161
				_data.uploaded[file].confirm === "is_dir" ?
162
					this.egw.lang("There's already a directory with that name!") :
163
					this.egw.lang('Do you want to overwrite existing file %1 in directory %2?', _data.uploaded[file].name, _data.path),
164
				this.egw.lang('File %1 already exists', _data.uploaded[file].name),
165
				_data.uploaded[file].name, buttons, file);
166
				// setting required data for callback in as my_data
167
				dialog.my_data = {
168
					file: file,
169
					path: _data.path,
170
					data: _data.uploaded[file],
171
				};
172
			}
173
			else
174
			{
175
				this.submit();
176
			}
177
		}
178
	},
179
180
	/**
181
	 * Prompt user for directory to create
182
	 *
183
	 * @param {egwAction|undefined|jQuery.Event} action Action, event or undefined if called directly
184
	 * @param {egwActionObject[] | undefined} selected Selected row, or undefined if called directly
185
	 */
186
	createdir: function(action, selected)
187
	{
188
		var self = this;
189
		et2_dialog.show_prompt(function(button, dir){
190
			if (button && dir)
191
			{
192
				var path = self.get_path();
193
				self.egw.json('EGroupware\\Api\\Etemplate\\Widget\\Vfs::ajax_create_dir', [dir, path], function(msg){
194
					self.egw.message(msg);
195
					self.change_dir((path == '/' ? '' : path)+'/'+ dir);
196
				}).sendRequest(false);
197
			}
198
		},this.egw.lang('New directory'),this.egw.lang('Create directory'));
199
	},
200
201
	/**
202
	 * Change directory
203
	 *
204
	 * @param {string} _dir directory to change to incl. '..' for one up
205
	 * @param {et2_widget} widget
206
	 */
207
	change_dir: function(_dir, widget)
208
	{
209
		switch (_dir)
210
		{
211
			case '..':
212
				_dir = this.dirname(this.get_path());
213
				break;
214
215
		}
216
217
		this.path_widget.set_value(_dir);
218
	},
219
220
	/**
221
	 * Row or filename in select-file dialog clicked
222
	 *
223
	 * @param {jQuery.event} event
224
	 * @param {et2_widget} widget
225
	 */
226
	select_clicked: function(event, widget)
227
	{
228
		if (!widget || typeof widget.value != 'object')
229
		{
0 ignored issues
show
Comprehensibility Documentation Best Practice introduced by
This code block is empty. Consider removing it or adding a comment to explain.
Loading history...
230
231
		}
232
		else if (widget.value.is_dir)	// true for "httpd/unix-directory" and "egw/*"
233
		{
234
			var path = null;
235
			// Cannot do this, there are multiple widgets named path
236
			// widget.getRoot().getWidgetById("path");
237
			widget.getRoot().iterateOver(function(widget) {
238
				if(widget.id == "path") path = widget;
239
			},null, et2_textbox);
240
			if(path)
241
			{
242
				path.set_value(widget.value.path);
243
			}
244
		}
245
		else if (this.et2 && this.et2.getArrayMgr('content').getEntry('mode') != 'open-multiple')
246
		{
247
			var editfield = this.et2.getWidgetById('name');
248
			if(editfield)
249
			{
250
				editfield.set_value(widget.value.name);
251
			}
252
		}
253
		else
254
		{
255
			var file = widget.value.name;
256
			widget.getParent().iterateOver(function(widget)
257
			{
258
				if(widget.options.selected_value == file)
259
				{
260
					widget.set_value(widget.get_value() == file ? widget.options.unselected_value : file);
261
				}
262
			}, null, et2_checkbox);
263
264
		}
265
		// Stop event or it will toggle back off
266
		event.preventDefault();
267
		event.stopPropagation();
268
		return false;
269
	},
270
271
	/**
272
	 * Handles action and offer it to the submit
273
	 *
274
	 * @param {string} action action name
275
	 * @param {object} widget widget which action was called from
276
	 */
277
	do_action: function (action, widget)
278
	{
279
		if (!action) return;
280
		var field = '', value = '';
281
		switch (action)
282
		{
283
			case 'path': field = 'path'; value = widget.getValue(); break;
284
			case 'home': field = 'action'; value = 'home'; break;
285
			case 'app': field = 'app'; value = widget.getValue(); break;
286
			case 'mime': field = 'mime'; value = widget.getValue(); break;
287
		}
288
		this.submit(field, value);
289
	},
290
291
	/**
292
	 * Sumbits content value after modification
293
	 *
294
	 * @param {string} _field content field to be modified
295
	 * @param {any} _val value of field
296
	 * @param {function} _callback
297
	 */
298
	submit: function(_field, _val, _callback)
299
	{
300
		var arrMgrs = this.et2.getArrayMgrs();
301
		if (_field)
302
		{
303
			arrMgrs.content.data[_field] = _val;
304
			jQuery.extend(arrMgrs.content.data, arrMgrs.modifications.data);
305
			this.et2.setArrayMgrs(arrMgrs);
306
		}
307
308
		// preserve value of the name
309
		if (arrMgrs && this.et2.getWidgetById('name'))
310
		{
311
			arrMgrs.content.data['name'] = this.et2.getWidgetById('name').get_value();
312
		}
313
314
		this.vfsSelectWidget._content(arrMgrs.content.data, _callback);
315
	},
316
317
	/**
318
	 * search through dir content and set its content base on searched query
319
	 * @returns
320
	 */
321
	search: function(_widget)
322
	{
323
		var dir = this.et2.getWidgetById('dir');
324
		var query = _widget.get_value();
325
		if (query == "")
326
		{
327
			dir.set_value({content: this.dirContent});
328
			return;
329
		}
330
		var self = this;
331
		var searchQuery = function (_query)
332
		{
333
			var result = {};
334
			var reg = RegExp(_query, 'ig');
335
			var key = 0;
336
			for (var i in self.dirContent)
337
			{
338
				if (typeof self.dirContent[i]['name'] != 'undefined' && self.dirContent[i]['name'].match(reg))
339
				{
340
					result[key] = self.dirContent[i];
341
					key++;
342
				}
343
				else if (typeof self.dirContent[i]['name'] == 'undefined' && isNaN(i))
344
				{
345
					result[i] = self.dirContent[i];
346
				}
347
			}
348
			return result;
349
		};
350
		dir.set_value({content: searchQuery(query)});
351
	}
352
});}).call(this);