Blame view

sources/core/js/oc-dialogs.js 20.9 KB
03e52840d   Kload   Init
1
2
3
  /**
   * ownCloud
   *
31b7f2792   Kload   Upgrade to ownclo...
4
   * @author Bartek Przybylski, Christopher Schäpers, Thomas Tanghus
03e52840d   Kload   Init
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
   * @copyright 2012 Bartek Przybylski bartek@alefzero.eu
   *
   * This library is free software; you can redistribute it and/or
   * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
   * License as published by the Free Software Foundation; either
   * version 3 of the License, or any later version.
   *
   * This library is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
   *
   * You should have received a copy of the GNU Affero General Public
   * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
   *
   */
6d9380f96   Cédric Dupont   Update sources OC...
21
  /* global alert */
03e52840d   Kload   Init
22
23
24
25
  /**
   * this class to ease the usage of jquery dialogs
   */
  var OCdialogs = {
31b7f2792   Kload   Upgrade to ownclo...
26
27
28
29
  	// dialog button types
  	YES_NO_BUTTONS:		70,
  	OK_BUTTONS:		71,
  	// used to name each dialog
6d9380f96   Cédric Dupont   Update sources OC...
30
  	dialogsCounter: 0,
03e52840d   Kload   Init
31
32
33
34
  	/**
  	* displays alert dialog
  	* @param text content of dialog
  	* @param title dialog title
31b7f2792   Kload   Upgrade to ownclo...
35
36
  	* @param callback which will be triggered when user presses OK
  	* @param modal make the dialog modal
03e52840d   Kload   Init
37
38
  	*/
  	alert:function(text, title, callback, modal) {
6d9380f96   Cédric Dupont   Update sources OC...
39
40
41
42
43
44
45
46
  		this.message(
  			text,
  			title,
  			'alert',
  			OCdialogs.OK_BUTTON,
  			callback,
  			modal
  		);
03e52840d   Kload   Init
47
48
49
50
51
  	},
  	/**
  	* displays info dialog
  	* @param text content of dialog
  	* @param title dialog title
31b7f2792   Kload   Upgrade to ownclo...
52
53
  	* @param callback which will be triggered when user presses OK
  	* @param modal make the dialog modal
03e52840d   Kload   Init
54
55
  	*/
  	info:function(text, title, callback, modal) {
31b7f2792   Kload   Upgrade to ownclo...
56
  		this.message(text, title, 'info', OCdialogs.OK_BUTTON, callback, modal);
03e52840d   Kload   Init
57
58
59
60
61
  	},
  	/**
  	* displays confirmation dialog
  	* @param text content of dialog
  	* @param title dialog title
6d9380f96   Cédric Dupont   Update sources OC...
62
63
  	* @param callback which will be triggered when user presses YES or NO
  	*        (true or false would be passed to callback respectively)
31b7f2792   Kload   Upgrade to ownclo...
64
  	* @param modal make the dialog modal
03e52840d   Kload   Init
65
66
  	*/
  	confirm:function(text, title, callback, modal) {
6d9380f96   Cédric Dupont   Update sources OC...
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
  		return this.message(
  			text,
  			title,
  			'notice',
  			OCdialogs.YES_NO_BUTTONS,
  			callback,
  			modal
  		);
  	},
  	/**
  	 * displays prompt dialog
  	 * @param text content of dialog
  	 * @param title dialog title
  	 * @param callback which will be triggered when user presses YES or NO
  	 *        (true or false would be passed to callback respectively)
  	 * @param modal make the dialog modal
  	 * @param name name of the input field
  	 * @param password whether the input should be a password input
  	 */
  	prompt: function (text, title, callback, modal, name, password) {
  		return $.when(this._getMessageTemplate()).then(function ($tmpl) {
  			var dialogName = 'oc-dialog-' + OCdialogs.dialogsCounter + '-content';
  			var dialogId = '#' + dialogName;
  			var $dlg = $tmpl.octemplate({
  				dialog_name: dialogName,
  				title      : title,
  				message    : text,
  				type       : 'notice'
  			});
  			var input = $('<input/>');
  			input.attr('type', password ? 'password' : 'text').attr('id', dialogName + '-input');
  			var label = $('<label/>').attr('for', dialogName + '-input').text(name + ': ');
  			$dlg.append(label);
  			$dlg.append(input);
  			if (modal === undefined) {
  				modal = false;
  			}
  			$('body').append($dlg);
  			var buttonlist = [{
  					text : t('core', 'No'),
  					click: function () {
  						if (callback !== undefined) {
  							callback(false, input.val());
  						}
  						$(dialogId).ocdialog('close');
  					}
  				}, {
  					text         : t('core', 'Yes'),
  					click        : function () {
  						if (callback !== undefined) {
  							callback(true, input.val());
  						}
  						$(dialogId).ocdialog('close');
  					},
  					defaultButton: true
  				}
  			];
  
  			$(dialogId).ocdialog({
  				closeOnEscape: true,
  				modal        : modal,
  				buttons      : buttonlist
  			});
  			OCdialogs.dialogsCounter++;
  		});
03e52840d   Kload   Init
132
133
  	},
  	/**
31b7f2792   Kload   Upgrade to ownclo...
134
135
136
137
  	 * show a file picker to pick a file from
  	 * @param title dialog title
  	 * @param callback which will be triggered when user presses Choose
  	 * @param multiselect whether it should be possible to select multiple files
6d9380f96   Cédric Dupont   Update sources OC...
138
  	 * @param mimetypeFilter mimetype to filter by
31b7f2792   Kload   Upgrade to ownclo...
139
  	 * @param modal make the dialog modal
03e52840d   Kload   Init
140
  	*/
6d9380f96   Cédric Dupont   Update sources OC...
141
  	filepicker:function(title, callback, multiselect, mimetypeFilter, modal) {
31b7f2792   Kload   Upgrade to ownclo...
142
  		var self = this;
6d9380f96   Cédric Dupont   Update sources OC...
143
144
145
146
147
  		// avoid opening the picker twice
  		if (this.filepicker.loading) {
  			return;
  		}
  		this.filepicker.loading = true;
31b7f2792   Kload   Upgrade to ownclo...
148
  		$.when(this._getFilePickerTemplate()).then(function($tmpl) {
6d9380f96   Cédric Dupont   Update sources OC...
149
150
  			self.filepicker.loading = false;
  			var dialogName = 'oc-dialog-filepicker-content';
31b7f2792   Kload   Upgrade to ownclo...
151
152
153
154
  			if(self.$filePicker) {
  				self.$filePicker.ocdialog('close');
  			}
  			self.$filePicker = $tmpl.octemplate({
6d9380f96   Cédric Dupont   Update sources OC...
155
  				dialog_name: dialogName,
31b7f2792   Kload   Upgrade to ownclo...
156
  				title: title
6d9380f96   Cédric Dupont   Update sources OC...
157
  			}).data('path', '').data('multiselect', multiselect).data('mimetype', mimetypeFilter);
31b7f2792   Kload   Upgrade to ownclo...
158
159
160
161
162
163
164
  
  			if (modal === undefined) {
  				modal = false;
  			}
  			if (multiselect === undefined) {
  				multiselect = false;
  			}
6d9380f96   Cédric Dupont   Update sources OC...
165
166
  			if (mimetypeFilter === undefined) {
  				mimetypeFilter = '';
31b7f2792   Kload   Upgrade to ownclo...
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
  			}
  
  			$('body').append(self.$filePicker);
  
  
  			self.$filePicker.ready(function() {
  				self.$filelist = self.$filePicker.find('.filelist');
  				self.$dirTree = self.$filePicker.find('.dirtree');
  				self.$dirTree.on('click', 'span:not(:last-child)', self, self._handleTreeListSelect);
  				self.$filelist.on('click', 'li', function(event) {
  					self._handlePickerClick(event, $(this));
  				});
  				self._fillFilePicker('');
  			});
  
  			// build buttons
  			var functionToCall = function() {
  				if (callback !== undefined) {
  					var datapath;
  					if (multiselect === true) {
  						datapath = [];
  						self.$filelist.find('.filepicker_element_selected .filename').each(function(index, element) {
  							datapath.push(self.$filePicker.data('path') + '/' + $(element).text());
  						});
03e52840d   Kload   Init
191
  					} else {
31b7f2792   Kload   Upgrade to ownclo...
192
193
  						datapath = self.$filePicker.data('path');
  						datapath += '/' + self.$filelist.find('.filepicker_element_selected .filename').text();
03e52840d   Kload   Init
194
  					}
31b7f2792   Kload   Upgrade to ownclo...
195
196
  					callback(datapath);
  					self.$filePicker.ocdialog('close');
03e52840d   Kload   Init
197
  				}
31b7f2792   Kload   Upgrade to ownclo...
198
199
200
201
202
203
204
205
206
  			};
  			var buttonlist = [{
  				text: t('core', 'Choose'),
  				click: functionToCall,
  				defaultButton: true
  			}];
  
  			self.$filePicker.ocdialog({
  				closeOnEscape: true,
f7d878ff1   kload   [enh] Update to 7...
207
  				width: (4/5)*$(document).width(),
31b7f2792   Kload   Upgrade to ownclo...
208
209
210
  				height: 420,
  				modal: modal,
  				buttons: buttonlist,
6d9380f96   Cédric Dupont   Update sources OC...
211
  				close: function() {
31b7f2792   Kload   Upgrade to ownclo...
212
213
214
215
  					try {
  						$(this).ocdialog('destroy').remove();
  					} catch(e) {}
  					self.$filePicker = null;
03e52840d   Kload   Init
216
  				}
31b7f2792   Kload   Upgrade to ownclo...
217
  			});
6d9380f96   Cédric Dupont   Update sources OC...
218
219
220
  			if (!OC.Util.hasSVGSupport()) {
  				OC.Util.replaceSVG(self.$filePicker.parent());
  			}
31b7f2792   Kload   Upgrade to ownclo...
221
222
223
224
  		})
  		.fail(function(status, error) {
  			// If the method is called while navigating away
  			// from the page, it is probably not needed ;)
6d9380f96   Cédric Dupont   Update sources OC...
225
  			self.filepicker.loading = false;
31b7f2792   Kload   Upgrade to ownclo...
226
227
  			if(status !== 0) {
  				alert(t('core', 'Error loading file picker template: {error}', {error: error}));
03e52840d   Kload   Init
228
  			}
03e52840d   Kload   Init
229
  		});
03e52840d   Kload   Init
230
  	},
31b7f2792   Kload   Upgrade to ownclo...
231
232
233
234
  	/**
  	 * Displays raw dialog
  	 * You better use a wrapper instead ...
  	*/
6d9380f96   Cédric Dupont   Update sources OC...
235
236
237
238
  	message:function(content, title, dialogType, buttons, callback, modal) {
  		return $.when(this._getMessageTemplate()).then(function($tmpl) {
  			var dialogName = 'oc-dialog-' + OCdialogs.dialogsCounter + '-content';
  			var dialogId = '#' + dialogName;
31b7f2792   Kload   Upgrade to ownclo...
239
  			var $dlg = $tmpl.octemplate({
6d9380f96   Cédric Dupont   Update sources OC...
240
  				dialog_name: dialogName,
31b7f2792   Kload   Upgrade to ownclo...
241
242
  				title: title,
  				message: content,
6d9380f96   Cédric Dupont   Update sources OC...
243
  				type: dialogType
03e52840d   Kload   Init
244
  			});
31b7f2792   Kload   Upgrade to ownclo...
245
246
247
248
249
250
  			if (modal === undefined) {
  				modal = false;
  			}
  			$('body').append($dlg);
  			var buttonlist = [];
  			switch (buttons) {
6d9380f96   Cédric Dupont   Update sources OC...
251
252
253
254
255
256
  			case OCdialogs.YES_NO_BUTTONS:
  				buttonlist = [{
  					text: t('core', 'No'),
  					click: function(){
  						if (callback !== undefined) {
  							callback(false);
31b7f2792   Kload   Upgrade to ownclo...
257
  						}
6d9380f96   Cédric Dupont   Update sources OC...
258
259
260
261
262
263
264
265
  						$(dialogId).ocdialog('close');
  					}
  				},
  				{
  					text: t('core', 'Yes'),
  					click: function(){
  						if (callback !== undefined) {
  							callback(true);
31b7f2792   Kload   Upgrade to ownclo...
266
  						}
6d9380f96   Cédric Dupont   Update sources OC...
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
  						$(dialogId).ocdialog('close');
  					},
  					defaultButton: true
  				}];
  				break;
  			case OCdialogs.OK_BUTTON:
  				var functionToCall = function() {
  					$(dialogId).ocdialog('close');
  					if(callback !== undefined) {
  						callback();
  					}
  				};
  				buttonlist[0] = {
  					text: t('core', 'Ok'),
  					click: functionToCall,
  					defaultButton: true
  				};
31b7f2792   Kload   Upgrade to ownclo...
284
285
  				break;
  			}
6d9380f96   Cédric Dupont   Update sources OC...
286
  			$(dialogId).ocdialog({
31b7f2792   Kload   Upgrade to ownclo...
287
288
289
290
  				closeOnEscape: true,
  				modal: modal,
  				buttons: buttonlist
  			});
6d9380f96   Cédric Dupont   Update sources OC...
291
  			OCdialogs.dialogsCounter++;
31b7f2792   Kload   Upgrade to ownclo...
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
  		})
  		.fail(function(status, error) {
  			// If the method is called while navigating away from
  			// the page, we still want to deliver the message.
  			if(status === 0) {
  				alert(title + ': ' + content);
  			} else {
  				alert(t('core', 'Error loading message template: {error}', {error: error}));
  			}
  		});
  	},
  	_fileexistsshown: false,
  	/**
  	 * Displays file exists dialog
  	 * @param {object} data upload object
  	 * @param {object} original file with name, size and mtime
  	 * @param {object} replacement file with name, size and mtime
  	 * @param {object} controller with onCancel, onSkip, onReplace and onRename methods
  	*/
  	fileexists:function(data, original, replacement, controller) {
  		var self = this;
  
  		var getCroppedPreview = function(file) {
  			var deferred = new $.Deferred();
  			// Only process image files.
  			var type = file.type && file.type.split('/').shift();
  			if (window.FileReader && type === 'image') {
  				var reader = new FileReader();
  				reader.onload = function (e) {
  					var blob = new Blob([e.target.result]);
  					window.URL = window.URL || window.webkitURL;
  					var originalUrl = window.URL.createObjectURL(blob);
  					var image = new Image();
  					image.src = originalUrl;
  					image.onload = function () {
  						var url = crop(image);
  						deferred.resolve(url);
6d9380f96   Cédric Dupont   Update sources OC...
329
  					};
31b7f2792   Kload   Upgrade to ownclo...
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
  				};
  				reader.readAsArrayBuffer(file);
  			} else {
  				deferred.reject();
  			}
  			return deferred;
  		};
  
  		var crop = function(img) {
  			var canvas = document.createElement('canvas'),
  				width = img.width,
  				height = img.height,
  				x, y, size;
  
  			// calculate the width and height, constraining the proportions
  			if (width > height) {
  				y = 0;
  				x = (width - height) / 2;
  			} else {
  				y = (height - width) / 2;
  				x = 0;
  			}
  			size = Math.min(width, height);
  
  			// resize the canvas and draw the image data into it
  			canvas.width = 64;
  			canvas.height = 64;
  			var ctx = canvas.getContext("2d");
  			ctx.drawImage(img, x, y, size, size, 0, 0, 64, 64);
  			return canvas.toDataURL("image/png", 0.7);
  		};
  
  		var addConflict = function(conflicts, original, replacement) {
  
  			var conflict = conflicts.find('.template').clone().removeClass('template').addClass('conflict');
  
  			conflict.data('data',data);
  
  			conflict.find('.filename').text(original.name);
  			conflict.find('.original .size').text(humanFileSize(original.size));
6d9380f96   Cédric Dupont   Update sources OC...
370
  			conflict.find('.original .mtime').text(formatDate(original.mtime));
31b7f2792   Kload   Upgrade to ownclo...
371
372
373
374
375
  			// ie sucks
  			if (replacement.size && replacement.lastModifiedDate) {
  				conflict.find('.replacement .size').text(humanFileSize(replacement.size));
  				conflict.find('.replacement .mtime').text(formatDate(replacement.lastModifiedDate));
  			}
6d9380f96   Cédric Dupont   Update sources OC...
376
377
  			var path = original.directory + '/' +original.name;
  			Files.lazyLoadPreview(path, original.mimetype, function(previewpath){
31b7f2792   Kload   Upgrade to ownclo...
378
379
380
381
382
383
384
385
386
  				conflict.find('.original .icon').css('background-image','url('+previewpath+')');
  			}, 96, 96, original.etag);
  			getCroppedPreview(replacement).then(
  				function(path){
  					conflict.find('.replacement .icon').css('background-image','url(' + path + ')');
  				}, function(){
  					Files.getMimeIcon(replacement.type,function(path){
  						conflict.find('.replacement .icon').css('background-image','url(' + path + ')');
  					});
03e52840d   Kload   Init
387
  				}
31b7f2792   Kload   Upgrade to ownclo...
388
389
390
391
392
  			);
  			conflicts.append(conflict);
  
  			//set more recent mtime bold
  			// ie sucks
6d9380f96   Cédric Dupont   Update sources OC...
393
  			if (replacement.lastModifiedDate && replacement.lastModifiedDate.getTime() > original.mtime) {
31b7f2792   Kload   Upgrade to ownclo...
394
  				conflict.find('.replacement .mtime').css('font-weight', 'bold');
6d9380f96   Cédric Dupont   Update sources OC...
395
  			} else if (replacement.lastModifiedDate && replacement.lastModifiedDate.getTime() < original.mtime) {
31b7f2792   Kload   Upgrade to ownclo...
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
  				conflict.find('.original .mtime').css('font-weight', 'bold');
  			} else {
  				//TODO add to same mtime collection?
  			}
  
  			// set bigger size bold
  			if (replacement.size && replacement.size > original.size) {
  				conflict.find('.replacement .size').css('font-weight', 'bold');
  			} else if (replacement.size && replacement.size < original.size) {
  				conflict.find('.original .size').css('font-weight', 'bold');
  			} else {
  				//TODO add to same size collection?
  			}
  
  			//TODO show skip action for files with same size and mtime in bottom row
  
  		};
  		//var selection = controller.getSelection(data.originalFiles);
  		//if (selection.defaultAction) {
  		//	controller[selection.defaultAction](data);
  		//} else {
6d9380f96   Cédric Dupont   Update sources OC...
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
  		var dialogName = 'oc-dialog-fileexists-content';
  		var dialogId = '#' + dialogName;
  		if (this._fileexistsshown) {
  			// add conflict
  
  			var conflicts = $(dialogId+ ' .conflicts');
  			addConflict(conflicts, original, replacement);
  
  			var count = $(dialogId+ ' .conflict').length;
  			var title = n('core',
  							'{count} file conflict',
  							'{count} file conflicts',
  							count,
  							{count:count}
  						);
  			$(dialogId).parent().children('.oc-dialog-title').text(title);
  
  			//recalculate dimensions
  			$(window).trigger('resize');
31b7f2792   Kload   Upgrade to ownclo...
436

6d9380f96   Cédric Dupont   Update sources OC...
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
  		} else {
  			//create dialog
  			this._fileexistsshown = true;
  			$.when(this._getFileExistsTemplate()).then(function($tmpl) {
  				var title = t('core','One file conflict');
  				var $dlg = $tmpl.octemplate({
  					dialog_name: dialogName,
  					title: title,
  					type: 'fileexists',
  
  					allnewfiles: t('core','New Files'),
  					allexistingfiles: t('core','Already existing files'),
  
  					why: t('core','Which files do you want to keep?'),
  					what: t('core','If you select both versions, the copied file will have a number added to its name.')
  				});
  				$('body').append($dlg);
31b7f2792   Kload   Upgrade to ownclo...
454

6d9380f96   Cédric Dupont   Update sources OC...
455
456
  				var conflicts = $($dlg).find('.conflicts');
  				addConflict(conflicts, original, replacement);
31b7f2792   Kload   Upgrade to ownclo...
457

6d9380f96   Cédric Dupont   Update sources OC...
458
459
460
461
462
463
  				var buttonlist = [{
  						text: t('core', 'Cancel'),
  						classes: 'cancel',
  						click: function(){
  							if ( typeof controller.onCancel !== 'undefined') {
  								controller.onCancel(data);
31b7f2792   Kload   Upgrade to ownclo...
464
  							}
6d9380f96   Cédric Dupont   Update sources OC...
465
466
467
468
469
470
471
472
473
  							$(dialogId).ocdialog('close');
  						}
  					},
  					{
  						text: t('core', 'Continue'),
  						classes: 'continue',
  						click: function(){
  							if ( typeof controller.onContinue !== 'undefined') {
  								controller.onContinue($(dialogId + ' .conflict'));
31b7f2792   Kload   Upgrade to ownclo...
474
  							}
6d9380f96   Cédric Dupont   Update sources OC...
475
476
477
478
479
480
481
482
483
484
485
486
  							$(dialogId).ocdialog('close');
  						}
  					}];
  
  				$(dialogId).ocdialog({
  					width: 500,
  					closeOnEscape: true,
  					modal: true,
  					buttons: buttonlist,
  					closeButton: null,
  					close: function() {
  							self._fileexistsshown = false;
31b7f2792   Kload   Upgrade to ownclo...
487
488
  							$(this).ocdialog('destroy').remove();
  						}
6d9380f96   Cédric Dupont   Update sources OC...
489
  				});
31b7f2792   Kload   Upgrade to ownclo...
490

6d9380f96   Cédric Dupont   Update sources OC...
491
  				$(dialogId).css('height','auto');
31b7f2792   Kload   Upgrade to ownclo...
492

6d9380f96   Cédric Dupont   Update sources OC...
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
  				//add checkbox toggling actions
  				$(dialogId).find('.allnewfiles').on('click', function() {
  					var checkboxes = $(dialogId).find('.conflict .replacement input[type="checkbox"]');
  					checkboxes.prop('checked', $(this).prop('checked'));
  				});
  				$(dialogId).find('.allexistingfiles').on('click', function() {
  					var checkboxes = $(dialogId).find('.conflict .original input[type="checkbox"]');
  					checkboxes.prop('checked', $(this).prop('checked'));
  				});
  				$(dialogId).find('.conflicts').on('click', '.replacement,.original', function() {
  					var checkbox = $(this).find('input[type="checkbox"]');
  					checkbox.prop('checked', !checkbox.prop('checked'));
  				});
  				$(dialogId).find('.conflicts').on('click', 'input[type="checkbox"]', function() {
  					var checkbox = $(this);
  					checkbox.prop('checked', !checkbox.prop('checked'));
  				});
31b7f2792   Kload   Upgrade to ownclo...
510

6d9380f96   Cédric Dupont   Update sources OC...
511
512
513
514
515
516
517
518
519
520
521
522
523
  				//update counters
  				$(dialogId).on('click', '.replacement,.allnewfiles', function() {
  					var count = $(dialogId).find('.conflict .replacement input[type="checkbox"]:checked').length;
  					if (count === $(dialogId+ ' .conflict').length) {
  						$(dialogId).find('.allnewfiles').prop('checked', true);
  						$(dialogId).find('.allnewfiles + .count').text(t('core','(all selected)'));
  					} else if (count > 0) {
  						$(dialogId).find('.allnewfiles').prop('checked', false);
  						$(dialogId).find('.allnewfiles + .count').text(t('core','({count} selected)',{count:count}));
  					} else {
  						$(dialogId).find('.allnewfiles').prop('checked', false);
  						$(dialogId).find('.allnewfiles + .count').text('');
  					}
31b7f2792   Kload   Upgrade to ownclo...
524
  				});
6d9380f96   Cédric Dupont   Update sources OC...
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
  				$(dialogId).on('click', '.original,.allexistingfiles', function(){
  					var count = $(dialogId).find('.conflict .original input[type="checkbox"]:checked').length;
  					if (count === $(dialogId+ ' .conflict').length) {
  						$(dialogId).find('.allexistingfiles').prop('checked', true);
  						$(dialogId).find('.allexistingfiles + .count').text(t('core','(all selected)'));
  					} else if (count > 0) {
  						$(dialogId).find('.allexistingfiles').prop('checked', false);
  						$(dialogId).find('.allexistingfiles + .count')
  							.text(t('core','({count} selected)',{count:count}));
  					} else {
  						$(dialogId).find('.allexistingfiles').prop('checked', false);
  						$(dialogId).find('.allexistingfiles + .count').text('');
  					}
  				});
  			})
  			.fail(function() {
  				alert(t('core', 'Error loading file exists template'));
  			});
  		}
31b7f2792   Kload   Upgrade to ownclo...
544
  		//}
03e52840d   Kload   Init
545
  	},
31b7f2792   Kload   Upgrade to ownclo...
546
547
548
549
550
551
552
553
554
555
556
557
558
559
  	_getFilePickerTemplate: function() {
  		var defer = $.Deferred();
  		if(!this.$filePickerTemplate) {
  			var self = this;
  			$.get(OC.filePath('core', 'templates', 'filepicker.html'), function(tmpl) {
  				self.$filePickerTemplate = $(tmpl);
  				self.$listTmpl = self.$filePickerTemplate.find('.filelist li:first-child').detach();
  				defer.resolve(self.$filePickerTemplate);
  			})
  			.fail(function(jqXHR, textStatus, errorThrown) {
  				defer.reject(jqXHR.status, errorThrown);
  			});
  		} else {
  			defer.resolve(this.$filePickerTemplate);
03e52840d   Kload   Init
560
  		}
31b7f2792   Kload   Upgrade to ownclo...
561
  		return defer.promise();
03e52840d   Kload   Init
562
  	},
31b7f2792   Kload   Upgrade to ownclo...
563
564
565
566
567
568
569
570
571
572
573
574
575
  	_getMessageTemplate: function() {
  		var defer = $.Deferred();
  		if(!this.$messageTemplate) {
  			var self = this;
  			$.get(OC.filePath('core', 'templates', 'message.html'), function(tmpl) {
  				self.$messageTemplate = $(tmpl);
  				defer.resolve(self.$messageTemplate);
  			})
  			.fail(function(jqXHR, textStatus, errorThrown) {
  				defer.reject(jqXHR.status, errorThrown);
  			});
  		} else {
  			defer.resolve(this.$messageTemplate);
03e52840d   Kload   Init
576
  		}
31b7f2792   Kload   Upgrade to ownclo...
577
  		return defer.promise();
03e52840d   Kload   Init
578
  	},
31b7f2792   Kload   Upgrade to ownclo...
579
580
581
582
583
584
585
586
587
588
  	_getFileExistsTemplate: function () {
  		var defer = $.Deferred();
  		if (!this.$fileexistsTemplate) {
  			var self = this;
  			$.get(OC.filePath('files', 'templates', 'fileexists.html'), function (tmpl) {
  				self.$fileexistsTemplate = $(tmpl);
  				defer.resolve(self.$fileexistsTemplate);
  			})
  			.fail(function () {
  				defer.reject();
03e52840d   Kload   Init
589
  			});
03e52840d   Kload   Init
590
  		} else {
31b7f2792   Kload   Upgrade to ownclo...
591
  			defer.resolve(this.$fileexistsTemplate);
03e52840d   Kload   Init
592
  		}
31b7f2792   Kload   Upgrade to ownclo...
593
  		return defer.promise();
03e52840d   Kload   Init
594
  	},
31b7f2792   Kload   Upgrade to ownclo...
595
596
597
  	_getFileList: function(dir, mimeType) {
  		if (typeof(mimeType) === "string") {
  			mimeType = [mimeType];
03e52840d   Kload   Init
598
  		}
31b7f2792   Kload   Upgrade to ownclo...
599
600
  
  		return $.getJSON(
6d9380f96   Cédric Dupont   Update sources OC...
601
  			OC.filePath('files', 'ajax', 'list.php'),
31b7f2792   Kload   Upgrade to ownclo...
602
603
604
  			{
  				dir: dir,
  				mimetypes: JSON.stringify(mimeType)
03e52840d   Kload   Init
605
  			}
31b7f2792   Kload   Upgrade to ownclo...
606
607
  		);
  	},
31b7f2792   Kload   Upgrade to ownclo...
608
609
610
611
612
613
614
615
616
617
618
  
  	/**
  	 * fills the filepicker with files
  	*/
  	_fillFilePicker:function(dir) {
  		var dirs = [];
  		var others = [];
  		var self = this;
  		this.$filelist.empty().addClass('loading');
  		this.$filePicker.data('path', dir);
  		$.when(this._getFileList(dir, this.$filePicker.data('mimetype'))).then(function(response) {
6d9380f96   Cédric Dupont   Update sources OC...
619
  			$.each(response.data.files, function(index, file) {
31b7f2792   Kload   Upgrade to ownclo...
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
  				if (file.type === 'dir') {
  					dirs.push(file);
  				} else {
  					others.push(file);
  				}
  			});
  
  			self._fillSlug();
  			var sorted = dirs.concat(others);
  
  			$.each(sorted, function(idx, entry) {
  				var $li = self.$listTmpl.octemplate({
  					type: entry.type,
  					dir: dir,
  					filename: entry.name,
f7d878ff1   kload   [enh] Update to 7...
635
  					date: relative_modified_date(entry.mtime/1000)
31b7f2792   Kload   Upgrade to ownclo...
636
  				});
6d9380f96   Cédric Dupont   Update sources OC...
637
638
639
640
641
642
643
644
645
646
  				if (entry.isPreviewAvailable) {
  					var urlSpec = {
  						file: dir + '/' + entry.name
  					};
  					var previewUrl = OC.generateUrl('/core/preview.png?') + $.param(urlSpec);
  					$li.find('img').attr('src', previewUrl);
  				}
  				else {
  					$li.find('img').attr('src', OC.Util.replaceSVGIcon(entry.icon));
  				}
31b7f2792   Kload   Upgrade to ownclo...
647
648
649
650
  				self.$filelist.append($li);
  			});
  
  			self.$filelist.removeClass('loading');
6d9380f96   Cédric Dupont   Update sources OC...
651
652
653
  			if (!OC.Util.hasSVGSupport()) {
  				OC.Util.replaceSVG(self.$filePicker.find('.dirtree'));
  			}
03e52840d   Kload   Init
654
  		});
03e52840d   Kload   Init
655
  	},
31b7f2792   Kload   Upgrade to ownclo...
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
  	/**
  	 * fills the tree list with directories
  	*/
  	_fillSlug: function() {
  		this.$dirTree.empty();
  		var self = this;
  		var path = this.$filePicker.data('path');
  		var $template = $('<span data-dir="{dir}">{name}</span>');
  		if(path) {
  			var paths = path.split('/');
  			$.each(paths, function(index, dir) {
  				dir = paths.pop();
  				if(dir === '') {
  					return false;
  				}
  				self.$dirTree.prepend($template.octemplate({
  					dir: paths.join('/') + '/' + dir,
  					name: dir
  				}));
  			});
  		}
  		$template.octemplate({
  			dir: '',
  			name: '&nbsp;&nbsp;&nbsp;&nbsp;' // Ugly but works ;)
  		}, {escapeFunction: null}).addClass('home svg').prependTo(this.$dirTree);
  	},
  	/**
  	 * handle selection made in the tree list
  	*/
  	_handleTreeListSelect:function(event) {
  		var self = event.data;
  		var dir = $(event.target).data('dir');
  		self._fillFilePicker(dir);
  	},
  	/**
  	 * handle clicks made in the filepicker
  	*/
  	_handlePickerClick:function(event, $element) {
  		if ($element.data('type') === 'file') {
  			if (this.$filePicker.data('multiselect') !== true || !event.ctrlKey) {
  				this.$filelist.find('.filepicker_element_selected').removeClass('filepicker_element_selected');
03e52840d   Kload   Init
697
  			}
31b7f2792   Kload   Upgrade to ownclo...
698
  			$element.toggleClass('filepicker_element_selected');
31b7f2792   Kload   Upgrade to ownclo...
699
700
  		} else if ( $element.data('type') === 'dir' ) {
  			this._fillFilePicker(this.$filePicker.data('path') + '/' + $element.data('entryname'));
03e52840d   Kload   Init
701
  		}
03e52840d   Kload   Init
702
703
  	}
  };