Blame view

sources/apps/documents/js/documents.js 23 KB
d1bafeea1   Kload   [fix] Upgrade to ...
1
  /*globals $,OC,fileDownloadPath,t,document,odf,webodfEditor,alert,require,dojo,runtime */
6d9380f96   Cédric Dupont   Update sources OC...
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
  
  $.widget('oc.documentGrid', {
  	options : {
  		context : '.documentslist',
  		documents : {},
  		sessions : {},
  		members : {}
  	},
  	
  	_create : function (){
  			
  	},
  	
  	render : function(){
  		var that = this;
  		jQuery.when(this._load())
  			.then(function(){
  				that._render();
  			});
  	},
  	
  	add : function(document) {
  		var docElem = $(this.options.context + ' .template').clone(),
  			a = docElem.find('a')
  		;
  
  		//Fill an element
  		docElem.removeClass('template').attr('data-id', document.fileid);
  		a.css('background-image', 'url("'+document.icon+'")')
  			.attr('href', OC.generateUrl('apps/files/download{file}',{file:document.path}))
  			.find('label').text(document.name)
  		;
  		
  		docElem.appendTo(this.options.context).show();
  		
  		//Preview
  		var previewURL,
  			urlSpec = {
  			file : document.path.replace(/^\/\//, '/'),
  			x : 200,
  			y : 200,
  			c : document.etag,
  			forceIcon : 0
  		};
  
  		if ( $('#isPublic').length ) {
  			urlSpec.t = $('#dirToken').val();
  		}
  		
  		if (!urlSpec.x) {
  			urlSpec.x = $('#filestable').data('preview-x');
  		}
  		if (!urlSpec.y) {
  			urlSpec.y = $('#filestable').data('preview-y');
  		}
  		urlSpec.y *= window.devicePixelRatio;
  		urlSpec.x *= window.devicePixelRatio;
  
  		previewURL = OC.generateUrl('/core/preview.png?') + $.param(urlSpec);
  		previewURL = previewURL.replace('(', '%28').replace(')', '%29');
  
  		var img = new Image();
  		img.onload = function(){
  			var ready = function (node){
  				return function(path){
  					node.css('background-image', 'url("'+ path +'")');
  				}; 
  			}(a);
  			ready(previewURL);
  		};
  		img.src = previewURL;
  	},
  	
  	_load : function (){
  		var that = this;
  		var def = new $.Deferred();
  		$.getJSON(OC.generateUrl('apps/documents/ajax/documents/list'))
  			.done(function (data) {
  				that.options.documents = data.documents;
  				that.options.sessions = data.sessions;
  				that.options.members = data.members;
  				def.resolve();
  			})
  			.fail(function(data){
  				console.log(t('documents','Failed to load documents.'));
  			});
  		return def;
  	},
  	
  	_render : function (data){
  		var that = this,
  			documents = data && data.documents || this.options.documents,
  			sessions = data && data.sessions || this.options.sessions,
  			members = data && data.members || this.options.members,
  			hasDocuments = false
  		;
  		
  		$(this.options.context + ' .document:not(.template,.progress)').remove();
  		
  		$.each(documents, function(i, document){
  			hasDocuments = true;
  			that.add(document);
  		});
  		
  		$.each(sessions, function(i, session){
  			if (members[session.es_id].length > 0) {
  				var docElem = $(that.options.context + ' .document[data-id="'+session.file_id+'"]');
  				if (docElem.length > 0) {
  					docElem.attr('data-esid', session.es_id);
  					docElem.find('label').after('<img class="svg session-active" src="'+OC.imagePath('core','places/contacts-dark')+'">');
  					docElem.addClass('session');
  				} else {
  					console.log('Could not find file '+session.file_id+' for session '+session.es_id);
  				}
  			}
  		});
  		
  		if (!hasDocuments){
  			$(this.options.context).before('<div id="emptycontent">'
  				+ t('documents', 'No documents were found. Upload or create a document to get started!')
  				+ '</div>'
  			);
  		} else {
  			$('#emptycontent').remove();
  		}
  	}
  });
  
  $.widget('oc.documentOverlay', {
  	options : {
  		parent : 'document.body'
  	},
  	_create : function (){
  		$(this.element).hide().appendTo(document.body);
  	},
  	show : function(){
  		$(this.element).fadeIn('fast');
  	},
  	hide : function(){
  		$(this.element).fadeOut('fast');
  	}
  });
  
  $.widget('oc.documentToolbar', {
  	options : {
  		innerhtml : '<div id="document-title">' +
  					'  <div id="header">' +
  					'    <div class="logo-wide"></div>' +
  					'    <div id="document-title-container">&nbsp;</div>' +
  			        '  </div>' +
  					'</div>' +
  					
  					'<span id="toolbar" class="claro">' +
  					'  <button id="odf-invite" class="drop hidden icon-share svg">' +
  					t('documents', 'Share') +
  					'  </button>' +
  					'  <button id="odf-close" class="icon-close svg"></button>' +
  					'  <img id="saving-document" alt=""' +
  					'    src="' + OC.imagePath('core', 'loading.gif') + '"' +
  					'  />' +
  					'</span>'
  	},
  	_create : function (){
  		$(this.element).html(this.options.innerhtml).hide().prependTo(document.body);
  	},
  	show : function (){
  		$(this.element).show();
  	},
  	hide : function(){
  		$(this.element).fadeOut('fast');
  		$(this.element).html(this.options.innerhtml);
  	}
  });
d1bafeea1   Kload   [fix] Upgrade to ...
175
  var documentsMain = {
d1bafeea1   Kload   [fix] Upgrade to ...
176
177
178
179
180
181
182
183
  	isEditormode : false,
  	useUnstable : false,
  	isGuest : false,
  	memberId : false,
  	esId : false,
  	ready :false,
  	fileName: null,
  	
6d9380f96   Cédric Dupont   Update sources OC...
184
  	UI : {		
d1bafeea1   Kload   [fix] Upgrade to ...
185
  		/* Editor wrapper HTML */
6d9380f96   Cédric Dupont   Update sources OC...
186
  		container : '<div id = "mainContainer" class="claro">' +
d1bafeea1   Kload   [fix] Upgrade to ...
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
  					'  <div id = "editor">' +
  					'    <div id = "container">' +
  					'      <div id="canvas"></div>' +
  					'    </div>' +
  					'  </div>' +
  					'  <div id = "collaboration">' +
  					'    <div id = "collabContainer">' +
  					'      <div id = "members">' +
  					'        <div id = "inviteButton"></div>' +
  					'        <div id = "memberList"></div>' +
  					'      </div>' +
  					'    </div>' +
  					'  </div>' +
  					'</div>',
  					
  		/* Previous window title */
  		mainTitle : '',
  				
  		init : function(){
d1bafeea1   Kload   [fix] Upgrade to ...
206
207
208
  			documentsMain.UI.mainTitle = $('title').text();
  		},
  		
d1bafeea1   Kload   [fix] Upgrade to ...
209
  		showEditor : function(title, canShare){
6d9380f96   Cédric Dupont   Update sources OC...
210
211
212
213
  			if (documentsMain.isGuest){
  				// !Login page mess wih WebODF toolbars
  				$(document.body).attr('id', 'body-user');
  			}
d1bafeea1   Kload   [fix] Upgrade to ...
214
215
216
217
  			$('#document-title-container').text(title);
  			if (!canShare){
  				$('#odf-invite').remove();
  			} else {
6d9380f96   Cédric Dupont   Update sources OC...
218
  				$('#odf-invite').show();
d1bafeea1   Kload   [fix] Upgrade to ...
219
220
221
222
223
224
225
226
227
  			}
  			$(document.body).addClass("claro");
  			$(document.body).prepend(documentsMain.UI.container);
  			// in case we are on the public sharing page we shall display the odf into the preview tag
  			$('#preview').html(container);
  			$('title').text(title + ' - ' + documentsMain.UI.mainTitle);
  		},
  		
  		hideEditor : function(){
6d9380f96   Cédric Dupont   Update sources OC...
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
  			if (documentsMain.isGuest){
  				// !Login page mess wih WebODF toolbars
  				$(document.body).attr('id', 'body-login');
  				$('header,footer,nav').show();
  			}
  			
  			documentsMain.toolbar.documentToolbar('hide');
  			
  			// Fade out editor
  			$('#mainContainer').fadeOut('fast', function() {
  				$('#mainContainer').remove();
  				$('#content').fadeIn('fast');
  				$(document.body).removeClass('claro');
  				$('title').text(documentsMain.UI.mainTitle);
  			});
d1bafeea1   Kload   [fix] Upgrade to ...
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
  		},
  		
  		showSave : function (){
  			$('#odf-close').hide();
  			$('#saving-document').show();
  		},
  		
  		hideSave : function(){
  			$('#saving-document').hide();
  			$('#odf-close').show();
  		},
  		
  		showProgress : function(message){
  			if (!message){
  				message = '&nbsp;';
  			}
  			$('.documentslist .progress div').text(message);
  			$('.documentslist .progress').show();
  		},
  		
  		hideProgress : function(){
  			$('.documentslist .progress').hide();
  		},
  		
  		showLostConnection : function(){
  			$('#memberList .memberListButton').css({opacity : 0.3});
  			$('#odf-toolbar').children(':not(#document-title)').hide();
  			$('<div id="connection-lost"></div>').prependTo('#memberList');
  			$('<div id="warning-connection-lost">' + t('documents', 'No connection to server. Trying to reconnect.') +'<img src="'+ OC.imagePath('core', 'loading-dark.gif') +'" alt="" /></div>').appendTo('#odf-toolbar');
  		},
  		
  		hideLostConnection : function() {
  			$('#connection-lost,#warning-connection-lost').remove();
  			$('#odf-toolbar').children(':not(#document-title,#saving-document)').show();
  			$('#memberList .memberListButton').css({opacity : 1});
  		},
  		
  		notify : function(message){
  			OC.Notification.show(message);
  			setTimeout(OC.Notification.hide, 10000);
  		}
  	},
  	
  	onStartup: function() {
  		var fileId;
  		"use strict";
  		documentsMain.useUnstable = $('#webodf-unstable').val()==='true';
  		documentsMain.UI.init();
  		
  		if (!OC.currentUser){
  			documentsMain.isGuest = true;
  			
d1bafeea1   Kload   [fix] Upgrade to ...
295
  			if ($("[name='document']").val()){
6d9380f96   Cédric Dupont   Update sources OC...
296
  				documentsMain.toolbar.documentToolbar('show');
d1bafeea1   Kload   [fix] Upgrade to ...
297
298
299
300
301
302
303
304
305
306
307
308
309
  				documentsMain.prepareSession();
  				documentsMain.joinSession(
  					$("[name='document']").val()
  				);
  			}
  
  		} else {
  			// Does anything indicate that we need to autostart a session?
  			fileId = parent.location.hash.replace(/\W*/g, '');
  		}
  		
  		documentsMain.show();
  		if (fileId){
6d9380f96   Cédric Dupont   Update sources OC...
310
  			documentsMain.overlay.documentOverlay('show');
d1bafeea1   Kload   [fix] Upgrade to ...
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
  		}
  		
  		var webodfSource = (oc_debug === true) ? 'webodf-debug' : 'webodf';
  		OC.addScript('documents', '3rdparty/webodf/' + webodfSource).done(function() {
  			// preload stuff in the background
  			require({}, ["dojo/ready"], function(ready) {
  				ready(function() {
  					require({}, ["webodf/editor/Editor"], function(Editor) {
  						runtime.setTranslator(function(s){return t('documents', s);});
  						documentsMain.ready = true;
  						if (fileId){
  							documentsMain.prepareSession();
  							documentsMain.joinSession(fileId);
  						}
  					});
  				});
  			});
  		});
  	},
  	
  	prepareSession : function(){
  		documentsMain.isEditorMode = true;
6d9380f96   Cédric Dupont   Update sources OC...
333
  		documentsMain.overlay.documentOverlay('show');
d1bafeea1   Kload   [fix] Upgrade to ...
334
335
336
337
338
339
340
341
  		$(window).on('beforeunload', function(){
  			return t('documents', "Leaving this page in Editor mode might cause unsaved data. It is recommended to use 'Close' button instead."); 
  		});
  		$(window).on("unload", documentsMain.onTerminate);
  	},
  	
  	prepareGrid : function(){
  		documentsMain.isEditorMode = false;
6d9380f96   Cédric Dupont   Update sources OC...
342
  		documentsMain.overlay.documentOverlay('hide');
d1bafeea1   Kload   [fix] Upgrade to ...
343
344
345
346
347
348
349
  	},
  	
  	initSession: function(response) {
  		"use strict";
  
  		if(response && (response.id && !response.es_id)){
  			return documentsMain.view(response.id);
6d9380f96   Cédric Dupont   Update sources OC...
350
351
352
353
354
  		}
  		
  		$('header,footer,nav').hide();
  		documentsMain.toolbar.documentToolbar('show');
  		
d1bafeea1   Kload   [fix] Upgrade to ...
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
  		if (!response || !response.status || response.status==='error'){
  			documentsMain.onEditorShutdown(t('documents', 'Failed to load this document. Please check if it can be opened with an external odt editor. This might also mean it has been unshared or deleted recently.'));
  			return;
  		}
  		
  		//Wait for 3 sec if editor is still loading 
  		if (!documentsMain.ready){
  			setTimeout(function(){ documentsMain.initSession(response); }, 3000);
  			console.log('Waiting for the editor to start...');
  			return;
  		}
  
  		require({ }, ["owncloud/ServerFactory", "webodf/editor/Editor"], function (ServerFactory, Editor) {
  			// fade out file list and show WebODF canvas
  			$('#content').fadeOut('fast').promise().done(function() {
  				
  				documentsMain.fileId = response.file_id;
6d9380f96   Cédric Dupont   Update sources OC...
372
  				documentsMain.fileName = response.title;
d1bafeea1   Kload   [fix] Upgrade to ...
373
374
375
376
377
378
  				documentsMain.UI.showEditor(
  						documentsMain.fileName || response.title,
  						typeof OC.Share !== 'undefined' && response.permissions & OC.PERMISSION_SHARE && !documentsMain.isGuest
  				);
  				if (documentsMain.isGuest){
  					$('#odf-close').text(t('documents', 'Save') );
6d9380f96   Cédric Dupont   Update sources OC...
379
  					$('#odf-close').removeClass('icon-close');
d1bafeea1   Kload   [fix] Upgrade to ...
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
  				}
  				var serverFactory = new ServerFactory();
  				documentsMain.esId = response.es_id;
  				documentsMain.memberId = response.member_id;
  
  				// TODO: set webodf translation system, by passing a proper function translate(!string):!string in "runtime.setTranslator(translate);"
  
  				documentsMain.webodfServerInstance = serverFactory.createServer();
  				documentsMain.webodfServerInstance.setToken(oc_requesttoken);
  				documentsMain.webodfEditorInstance = new Editor({unstableFeaturesEnabled: documentsMain.useUnstable}, documentsMain.webodfServerInstance, serverFactory);
  				documentsMain.webodfEditorInstance.addEventListener(Editor.EVENT_BEFORESAVETOFILE, documentsMain.UI.showSave);
  				documentsMain.webodfEditorInstance.addEventListener(Editor.EVENT_SAVEDTOFILE, documentsMain.UI.hideSave);
  				documentsMain.webodfEditorInstance.addEventListener(Editor.EVENT_ERROR, documentsMain.onEditorShutdown);
  				documentsMain.webodfEditorInstance.addEventListener(Editor.EVENT_HASSESSIONHOSTCONNECTIONCHANGED, function(has) {
  					if (has){
  						documentsMain.UI.hideLostConnection();
  					} else {
  						documentsMain.UI.showLostConnection();
  					}
  				});
  				// load the document and get called back when it's live
  				documentsMain.webodfEditorInstance.openSession(documentsMain.esId, documentsMain.memberId, function() {
  					documentsMain.webodfEditorInstance.startEditing();
6d9380f96   Cédric Dupont   Update sources OC...
403
  					documentsMain.overlay.documentOverlay('hide');
d1bafeea1   Kload   [fix] Upgrade to ...
404
405
406
407
408
409
410
411
412
413
414
  					parent.location.hash = response.file_id;
  				});
  			});
  		});
  	},
  	
  
  	joinSession: function(fileId) {
  		console.log('joining session '+fileId);
  		var url;
  		if (documentsMain.isGuest){
6d9380f96   Cédric Dupont   Update sources OC...
415
  			url = OC.generateUrl('apps/documents/ajax/session/joinasguest/{token}', {token: fileId});
d1bafeea1   Kload   [fix] Upgrade to ...
416
  		} else {
6d9380f96   Cédric Dupont   Update sources OC...
417
  			url = OC.generateUrl('apps/documents/ajax/session/joinasuser/{file_id}', {file_id: fileId});
d1bafeea1   Kload   [fix] Upgrade to ...
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
  		}
  		$.post(
  			url,
  			{ name : $("[name='memberName']").val() },
  			documentsMain.initSession
  		);
  	},
  	
  	view : function(id){
  		OC.addScript('documents', 'viewer/viewer', function() {
  			documentsMain.prepareGrid();
  			$(window).off('beforeunload');
  			$(window).off('unload');
  			var path = $('li[data-id='+ id +']>a').attr('href');
  			odfViewer.isDocuments = true;
  			odfViewer.onView(path);
  		});
  	},
  			
  	onCreate: function(event){
  		event.preventDefault();
  		var docElem = $('.documentslist .template').clone();
  		docElem.removeClass('template');
  		docElem.addClass('document');
  		docElem.insertAfter('.documentslist .template');
  		docElem.show();
  		$.post(
6d9380f96   Cédric Dupont   Update sources OC...
445
  			OC.generateUrl('apps/documents/ajax/documents/create'),
d1bafeea1   Kload   [fix] Upgrade to ...
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
  			{},
  			function(response){
  				if (response && response.fileid){
  					documentsMain.prepareSession();
  					documentsMain.joinSession(response.fileid);
  				} else {
  					if (response && response.message){
  						documentsMain.UI.notify(response.message);
  					}
  					documentsMain.show();
  				}
  			}
  			
  		);
  	},
  
  	onInvite: function(event) {
  		event.preventDefault();
  		if (OC.Share.droppedDown) {
  			OC.Share.hideDropDown();
  		} else {
  			(function() {
  				var target = OC.Share.showLink;
  				OC.Share.showLink = function() {
  					var r = target.apply( this, arguments );
  					$('#linkText').val( $('#linkText').val().replace('service=files', 'service=documents') );
  					return r;
  				};
  			})();
  
  			OC.Share.showDropDown(
  				'file', 
  				parent.location.hash.replace(/\W*/g, ''),
  				$("#odf-toolbar"),
  				true, 
  				OC.PERMISSION_READ | OC.PERMISSION_SHARE | OC.PERMISSION_UPDATE
  			);
  		}
  	},
  	
  	changeNick: function(memberId, name, node){
  		var url = OC.generateUrl('apps/documents/ajax/user/rename/{member_id}', {member_id: memberId});
  		$.post(
  			url,
  			{ name : name },
  			function(result) {
  				if (result && result.status === 'error') {
  					if (result.message){
  						documentsMain.UI.notify(result.message);
  					}
  					return;
  				}
  			}
  		);
  	},
  	
  	onNickChange: function(memberId, fullNameNode){
  		if (!documentsMain.isGuest || memberId !== documentsMain.memberId){
  			return;
  		}
  		if ($(fullNameNode.parentNode).children('input').length !== 0){
  			return;
  		}
  		
  		var input = $('<input type="text"/>').val($(fullNameNode).attr('fullname'));
  		$(fullNameNode.parentNode).append(input);
  		$(fullNameNode).hide();
  
  		input.on('blur', function(){
  			var newName = input.val();
  			if (!newName || newName === name) {
  				input.tipsy('hide');
  				input.remove();
  				$(fullNameNode).show();
  				return;
  			}
  			else {
  				try {
  					input.tipsy('hide');
  					input.removeClass('error');
  					input.tipsy('hide');
  					input.remove();
  					$(fullNameNode).show();
  					documentsMain.changeNick(memberId, newName, fullNameNode);
  				}
  				catch (error) {
  					input.attr('title', error);
  					input.tipsy({gravity: 'n', trigger: 'manual'});
  					input.tipsy('show');
  					input.addClass('error');
  				}
  			}
  		});
  		input.on('keyup', function(event){
  			if (event.keyCode === 27) {
  				// cancel by putting in an empty value
  				$(this).val('');
  				$(this).blur();
  				event.preventDefault();
  			}
  			if (event.keyCode === 13) {
  				$(this).blur();
  				event.preventDefault();
  			}
  		});
  		input.focus();
  		input.selectRange(0, name.length);
  	},
  
  	renameDocument: function(name) {
6d9380f96   Cédric Dupont   Update sources OC...
556
  		var url = OC.generateUrl('apps/documents/ajax/documents/rename/{file_id}', {file_id: documentsMain.fileId});
d1bafeea1   Kload   [fix] Upgrade to ...
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
  		$.post(
  			url,
  			{ name : name },
  			function(result) {
  				if (result && result.status === 'error') {
  					if (result.message){
  						documentsMain.IU.notify(result.message);
  					}
  					return;
  				}
  				documentsMain.fileName = name;
  				$('title').text(documentsMain.UI.mainTitle + '| ' + name);
  				$('#document-title-container').text(name);
  			}
  		);
  	},
  
  
  	onRenamePrompt: function() {
  		var name = documentsMain.fileName;
  		var lastPos = name.lastIndexOf('.');
  		var extension = name.substr(lastPos + 1);
  		name = name.substr(0, lastPos);
  		var input = $('<input type="text" class="filename"/>').val(name);
6d9380f96   Cédric Dupont   Update sources OC...
581
582
  		$('#header').append(input);
  		$('#document-title-container').hide();
d1bafeea1   Kload   [fix] Upgrade to ...
583
584
585
586
587
588
  
  		input.on('blur', function(){
  			var newName = input.val();
  			if (!newName || newName === name) {
  				input.tipsy('hide');
  				input.remove();
6d9380f96   Cédric Dupont   Update sources OC...
589
  				$('#document-title-container').show();
d1bafeea1   Kload   [fix] Upgrade to ...
590
591
592
593
594
595
596
597
598
599
  				return;
  			}
  			else {
  				newName = newName + '.' + extension;
  				try {
  					input.tipsy('hide');
  					input.removeClass('error');
  					if (Files.isFileNameValid(newName)) {
  						input.tipsy('hide');
  						input.remove();
6d9380f96   Cédric Dupont   Update sources OC...
600
  						$('#document-title-container').show();
d1bafeea1   Kload   [fix] Upgrade to ...
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
  						documentsMain.renameDocument(newName);
  					}
  				}
  				catch (error) {
  					input.attr('title', error);
  					input.tipsy({gravity: 'n', trigger: 'manual'});
  					input.tipsy('show');
  					input.addClass('error');
  				}
  			}
  		});
  		input.on('keyup', function(event){
  			if (event.keyCode === 27) {
  				// cancel by putting in an empty value
  				$(this).val('');
  				$(this).blur();
  				event.preventDefault();
  			}
  			if (event.keyCode === 13) {
  				$(this).blur();
  				event.preventDefault();
  			}
  		});
  		input.focus();
  		input.selectRange(0, name.length);
  	},
  
  	onEditorShutdown : function (message){
  			OC.Notification.show(message);
  
  			$(window).off('beforeunload');
  			$(window).off('unload');
  			if (documentsMain.isEditorMode){
  				documentsMain.isEditorMode = false;
  				parent.location.hash = "";
  			} else {
  				setTimeout(OC.Notification.hide, 7000);
  			}
  			documentsMain.prepareGrid();
  			try {
  				documentsMain.webodfEditorInstance.endEditing();
  				documentsMain.webodfEditorInstance.closeSession(function() {
  					documentsMain.webodfEditorInstance.destroy(documentsMain.UI.hideEditor);
  				});
  			} catch (e){
  				documentsMain.UI.hideEditor();
  			}
  			
  			documentsMain.show();
  	},
  		
  
  	onClose: function() {
  		"use strict";
  		
  		if (!documentsMain.isEditorMode){
  			return;
  		}
  		documentsMain.isEditorMode = false;
  		$(window).off('beforeunload');
  		$(window).off('unload');
  		parent.location.hash = "";
  
  		documentsMain.webodfEditorInstance.endEditing();
  		documentsMain.webodfEditorInstance.closeSession(function() {
  			// successfull shutdown - all is good.
  			// TODO: proper session leaving call to server, either by webodfServerInstance or custom
  // 			documentsMain.webodfServerInstance.leaveSession(sessionId, memberId, function() {
6d9380f96   Cédric Dupont   Update sources OC...
669
670
  
  			$('header,footer,nav').show();
d1bafeea1   Kload   [fix] Upgrade to ...
671
  			documentsMain.webodfEditorInstance.destroy(documentsMain.UI.hideEditor);
6d9380f96   Cédric Dupont   Update sources OC...
672
673
  
  			var url = '';
d1bafeea1   Kload   [fix] Upgrade to ...
674
  			if (documentsMain.isGuest){
6d9380f96   Cédric Dupont   Update sources OC...
675
  				url = OC.generateUrl('apps/documents/ajax/user/disconnectGuest/{member_id}', {member_id: documentsMain.memberId});
d1bafeea1   Kload   [fix] Upgrade to ...
676
  			} else {
6d9380f96   Cédric Dupont   Update sources OC...
677
  				url = OC.generateUrl('apps/documents/ajax/user/disconnect/{member_id}', {member_id: documentsMain.memberId});
d1bafeea1   Kload   [fix] Upgrade to ...
678
679
  			}
  			
6d9380f96   Cédric Dupont   Update sources OC...
680
  			$.post(url, {esId: documentsMain.esId});
d1bafeea1   Kload   [fix] Upgrade to ...
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
  			
  			documentsMain.show();
  // 			});
  		});
  	},
  	
  	onTerminate: function(){
  		var url = '';
  		if (documentsMain.isGuest){
  			url = OC.generateUrl('apps/documents/ajax/user/disconnectGuest/{member_id}', {member_id: documentsMain.memberId});
  		} else {
  			url = OC.generateUrl('apps/documents/ajax/user/disconnect/{member_id}', {member_id: documentsMain.memberId});
  		}
  		$.ajax({
  				type: "POST",
  				url: url,
  				data: {esId: documentsMain.esId},
  				dataType: "json",
  				async: false // Should be sync to complete before the page is closed
  		});
  
  		
  		documentsMain.webodfEditorInstance.endEditing();
  		documentsMain.webodfEditorInstance.closeSession(function() {
  			if (documentsMain.isGuest){
6d9380f96   Cédric Dupont   Update sources OC...
706
  				$('header,footer,nav').show();
d1bafeea1   Kload   [fix] Upgrade to ...
707
708
709
710
711
712
  			}
  			documentsMain.webodfEditorInstance.destroy(documentsMain.UI.hideEditor);
  		});
  
  	},
  	
d1bafeea1   Kload   [fix] Upgrade to ...
713
714
715
716
717
  	show: function(){
  		if (documentsMain.isGuest){
  			return;
  		}
  		documentsMain.UI.showProgress(t('documents', 'Loading documents...'));
6d9380f96   Cédric Dupont   Update sources OC...
718
719
  		documentsMain.docs.documentGrid('render');
  		documentsMain.UI.hideProgress();
d1bafeea1   Kload   [fix] Upgrade to ...
720
721
722
723
724
725
726
  	}
  };
  
  
  //web odf bootstrap code. Added here to reduce number of requests
  /*globals navigator,dojoConfig */
  var usedLocale = "C";
6d9380f96   Cédric Dupont   Update sources OC...
727
  if (navigator && navigator.language && navigator.language.match(/^(de)/)) {
d1bafeea1   Kload   [fix] Upgrade to ...
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
  	usedLocale = navigator.language.substr(0,2);
  }
  
  dojoConfig = {
  	locale: usedLocale,
  	paths: {
  		"webodf/editor": OC.appswebroots.documents + "/js/3rdparty/webodf/editor",
  		"dijit": OC.appswebroots.documents + "/js/3rdparty/resources/dijit",
  		"dojox": OC.appswebroots.documents + "/js/3rdparty/resources/dojox",
  		"dojo": OC.appswebroots.documents + "/js/3rdparty/resources/dojo",
  		"resources": OC.appswebroots.documents + "/js/3rdparty/resources",
  		"owncloud" : OC.appswebroots.documents + "/js"
  	}
  };
  
  
  //init
  var Files = Files || {
  	// FIXME: copy/pasted from Files.isFileNameValid, needs refactor into core
  	isFileNameValid:function (name) {
  		if (name === '.') {
  			throw t('files', '\'.\' is an invalid file name.');
  		} else if (name.length === 0) {
  			throw t('files', 'File name cannot be empty.');
  		}
  
  		// check for invalid characters
  		var invalid_characters = ['\\', '/', '<', '>', ':', '"', '|', '?', '*'];
  		for (var i = 0; i < invalid_characters.length; i++) {
  			if (name.indexOf(invalid_characters[i]) !== -1) {
  				throw t('files', "Invalid name, '\\', '/', '<', '>', ':', '\"', '|', '?' and '*' are not allowed.");
  			}
  		}
  		return true;
  	},
  	
d1bafeea1   Kload   [fix] Upgrade to ...
764
765
766
767
768
769
770
771
772
773
774
  	updateStorageStatistics: function(){}
  },
  FileList = FileList || {};
  
  FileList.getCurrentDirectory = function(){
  	return $('#dir').val() || '/';
  };
  
  $(document).ready(function() {
  	"use strict";
  	
6d9380f96   Cédric Dupont   Update sources OC...
775
776
777
778
  	documentsMain.docs = $('.documentslist').documentGrid();
  	documentsMain.overlay = $('<div id="documents-overlay" class="icon-loading"></div><div id="documents-overlay-below" class="icon-loading-dark"></div>').documentOverlay();
  	documentsMain.toolbar = $('<div id="odf-toolbar" class="dijitToolbar"></div>').documentToolbar();
  	
d1bafeea1   Kload   [fix] Upgrade to ...
779
780
781
782
783
784
785
786
787
788
789
790
791
  	$('.documentslist').on('click', 'li:not(.add-document)', function(event) {
  		event.preventDefault();
  
  		if (documentsMain.isEditorMode){
  			return;
  		}
  		
  		documentsMain.prepareSession();
  		if ($(this).attr('data-id')){
  			documentsMain.joinSession($(this).attr('data-id'));
  		}
  	});
  	
6d9380f96   Cédric Dupont   Update sources OC...
792
  	$(document.body).on('click', '#document-title-container', documentsMain.onRenamePrompt);
d1bafeea1   Kload   [fix] Upgrade to ...
793
794
795
796
797
798
799
800
801
802
  	$(document.body).on('click', '#odf-close', documentsMain.onClose);
  	$(document.body).on('click', '#odf-invite', documentsMain.onInvite);
  
  	$('.add-document').on('click', '.add', documentsMain.onCreate);
  
  
  	var file_upload_start = $('#file_upload_start');
  	if (typeof supportAjaxUploadWithProgress !== 'undefined' && supportAjaxUploadWithProgress()) {
  		file_upload_start.on('fileuploadstart', function(e, data) {
  			$('#upload').addClass('icon-loading');
6d9380f96   Cédric Dupont   Update sources OC...
803
  			$('.add-document .upload').css({opacity:0});
d1bafeea1   Kload   [fix] Upgrade to ...
804
805
806
807
  		});
  	}
  	file_upload_start.on('fileuploaddone', function(){
  		$('#upload').removeClass('icon-loading');
6d9380f96   Cédric Dupont   Update sources OC...
808
  		$('.add-document .upload').css({opacity:0.7});
d1bafeea1   Kload   [fix] Upgrade to ...
809
810
  		documentsMain.show();
  	});
d1bafeea1   Kload   [fix] Upgrade to ...
811
812
813
  	
  	OC.addScript('documents', '3rdparty/webodf/dojo-amalgamation', documentsMain.onStartup);
  });