Welcome to mirror list, hosted at ThFree Co, Russian Federation.

viewer.js « viewer « js - github.com/nextcloud/richdocuments.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 15db07824e0ed9bef6b96a2f849f084b3ee7fd6b (plain)
1
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
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
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
329
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/* globals FileList, OCA.Files.fileActions, oc_debug */

function getSearchParam(name){
	var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
	if (results === null){
		return null;
	}
	return decodeURI(results[1]) || 0;
}

var preload_type = getSearchParam('richdocuments_create');
var preload_filename = getSearchParam('richdocuments_filename');
var Preload = {
	create: {
		type: preload_type,
		filename: preload_filename,
	}
};

var odfViewer = {
	isDocuments : false,
	nextcloudVersion: 0,
	receivedLoading: false,
	supportedMimes: OC.getCapabilities().richdocuments.mimetypes.concat(OC.getCapabilities().richdocuments.mimetypesNoDefaultOpen),
	excludeMimeFromDefaultOpen: OC.getCapabilities().richdocuments.mimetypesNoDefaultOpen,
	register : function() {
		odfViewer.nextcloudVersion = parseInt(OC.config.version.split('.')[0]);
		var i,
		    mime;
		var editActionName = 'Edit with ' + OC.getCapabilities().richdocuments.productName;
		for (i = 0; i < odfViewer.supportedMimes.length; ++i) {
			mime = odfViewer.supportedMimes[i];
			OCA.Files.fileActions.register(
				    mime,
				    editActionName,
				    OC.PERMISSION_UPDATE | OC.PERMISSION_READ,
					OC.imagePath('core', 'actions/rename'),
					odfViewer.onEdit,
					t('richdocuments', 'Edit with {productName}', { productName: OC.getCapabilities().richdocuments.productName })
			);
			if (odfViewer.excludeMimeFromDefaultOpen.indexOf(mime) === -1) {
				OCA.Files.fileActions.setDefault(mime, editActionName);
			}
		}
	},

	dispatch : function(filename){
		odfViewer.onEdit(filename);
	},

	getNewDocumentFromTemplateUrl: function(templateId, fileName, fileDir, fillWithTemplate) {
		return OC.generateUrl(
			'apps/richdocuments/indexTemplate?templateId={templateId}&fileName={fileName}&dir={dir}&requesttoken={requesttoken}',
			{
				templateId: templateId,
				fileName: fileName,
				dir: fileDir,
				requesttoken: OC.requestToken
			}
		);
	},

	onEdit : function(fileName, context) {
		if(context) {
			var fileDir = context.dir;
			var fileId = context.fileId || context.$file.attr('data-id');
			var templateId = context.templateId;
		}
		odfViewer.receivedLoading = false;

		var viewer;
		if($('#isPublic').val() === '1') {
			viewer = OC.generateUrl(
				'apps/richdocuments/public?shareToken={shareToken}&fileName={fileName}&requesttoken={requesttoken}&fileId={fileId}',
				{
					shareToken: $('#sharingToken').val(),
					fileName: fileName,
					fileId: fileId,
					requesttoken: OC.requestToken
				}
			);
		} else {
			// We are dealing with a template
			if (typeof(templateId) !== 'undefined') {
				viewer = this.getNewDocumentFromTemplateUrl(templateId, fileName, fileDir);
			} else {
				viewer = OC.generateUrl(
					'apps/richdocuments/index?fileId={fileId}&requesttoken={requesttoken}',
					{
						fileId: fileId,
						dir: fileDir,
						requesttoken: OC.requestToken
					}
				);
			}
		}

		if(context) {
			FileList.setViewerMode(true);
			FileList.setPageTitle(fileName);
			FileList.showMask();
		}

		OC.addStyle('richdocuments', 'mobile');

		var $iframe = $('<iframe id="richdocumentsframe" nonce="' + btoa(OC.requestToken) + '" scrolling="no" allowfullscreen src="'+viewer+'" />');
		odfViewer.loadingTimeout = setTimeout(function() {
			if (!odfViewer.receivedLoading) {
				odfViewer.onClose();
				OC.Notification.showTemporary(t('richdocuments', 'Failed to load {productName} - please try again later', {productName: OC.getCapabilities().richdocuments.productName || 'Collabora Online'}));
			}
		}, 15000);
		$iframe.src = viewer;

		$('body').css('overscroll-behavior-y', 'none');
		var viewport = document.querySelector("meta[name=viewport]");
		viewport.setAttribute('content', 'width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no');
		if ($('#isPublic').val()) {
			// force the preview to adjust its height
			$('#preview').append($iframe).css({height: '100%'});
			$('body').css({height: '100%'});
			$('#content').addClass('full-height');
			$('footer').addClass('hidden');
			$('#imgframe').addClass('hidden');
			$('.directLink').addClass('hidden');
			$('.directDownload').addClass('hidden');
			$('#controls').addClass('hidden');
			$('#content').addClass('loading');
		} else {
			$('body').css('overflow', 'hidden');
			$('#app-content').append($iframe);
			$iframe.hide();
			if ($('header').length) {
				var $button = $('<div class="richdocuments-sharing"><a class="icon-shared icon-white"></a></div>');
				$('.header-right').prepend($button);
				$button.on('click', function() {
					if ($('#app-sidebar').is(':visible')) {
						OC.Apps.hideAppSidebar();
						return;
					}
					var frameFilename = $('#richdocumentsframe')[0].contentWindow.documentsMain.fileName;
					FileList.showDetailsView(frameFilename ? frameFilename : fileName, 'shareTabView');
					OC.Apps.showAppSidebar();
				});
				$('.searchbox').hide();
			}
		}

		$('#app-content #controls').addClass('hidden');
	},

	onClose: function() {
		clearTimeout(odfViewer.loadingTimeout)
		if(typeof FileList !== "undefined") {
			FileList.setViewerMode(false);
			FileList.reload();
			FileList.setPageTitle();
		}
		odfViewer.receivedLoading = false;
		$('link[href*="richdocuments/css/mobile"]').remove();
		$('#app-content #controls').removeClass('hidden');
		$('#richdocumentsframe').remove();
		$('.richdocuments-sharing').remove();
		$('#richdocuments-avatars').remove();
		$('#richdocuments-actions').remove();
		$('.searchbox').show();
		$('body').css('overflow', 'auto');

		if ($('#isPublic').val()) {
			$('#content').removeClass('full-height');
			$('footer').removeClass('hidden');
			$('#imgframe').removeClass('hidden');
			$('.directLink').removeClass('hidden');
			$('.directDownload').removeClass('hidden');
		}

		OC.Util.History.replaceState();
	},

	registerFilesMenu: function(response) {
		var ooxml = response.doc_format === 'ooxml';

		var docExt, spreadsheetExt, presentationExt;
		var docMime, spreadsheetMime, presentationMime;
		if (ooxml) {
			docExt = 'docx';
			spreadsheetExt = 'xlsx';
			presentationExt = 'pptx';
			docMime = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
			spreadsheetMime = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
			presentationMime =	'application/vnd.openxmlformats-officedocument.presentationml.presentation';
		} else {
			docExt = 'odt';
			spreadsheetExt = 'ods';
			presentationExt = 'odp';
			docMime = 'application/vnd.oasis.opendocument.text';
			spreadsheetMime = 'application/vnd.oasis.opendocument.spreadsheet';
			presentationMime = 'application/vnd.oasis.opendocument.presentation';
		}

		(function(OCA){
			OCA.FilesLOMenu = {
				attach: function(newFileMenu) {
					var self = this;

					newFileMenu.addMenuEntry({
						id: 'add-' + docExt,
						displayName: t('richdocuments', 'New Document'),
						templateName: t('richdocuments', 'New Document') + '.' + docExt,
						iconClass: 'icon-filetype-document',
						fileType: 'x-office-document',
						actionHandler: function(filename) {
							if (OC.getCapabilities().richdocuments.templates) {
								self._openTemplatePicker('document', docMime, filename);
							} else {
								self._createDocument(docMime, filename);
							}
						}
					});

					newFileMenu.addMenuEntry({
						id: 'add-' + spreadsheetExt,
						displayName: t('richdocuments', 'New Spreadsheet'),
						templateName: t('richdocuments', 'New Spreadsheet') + '.' + spreadsheetExt,
						iconClass: 'icon-filetype-spreadsheet',
						fileType: 'x-office-spreadsheet',
						actionHandler: function(filename) {
							if (OC.getCapabilities().richdocuments.templates) {
								self._openTemplatePicker('spreadsheet', spreadsheetMime, filename);
							} else {
								self._createDocument(spreadsheetMime, filename);
							}
						}
					});

					newFileMenu.addMenuEntry({
						id: 'add-' + presentationExt,
						displayName: t('richdocuments', 'New Presentation'),
						templateName: t('richdocuments', 'New Presentation') + '.' + presentationExt,
						iconClass: 'icon-filetype-presentation',
						fileType: 'x-office-presentation',
						actionHandler: function(filename) {
							if (OC.getCapabilities().richdocuments.templates) {
								self._openTemplatePicker('presentation', presentationMime, filename);
							} else {
								self._createDocument(presentationMime, filename);
							}
						}
					});
				},

				_createDocument: function(mimetype, filename) {
					OCA.Files.Files.isFileNameValid(filename);
					filename = FileList.getUniqueName(filename);

					$.post(
						OC.generateUrl('apps/richdocuments/ajax/documents/create'),
						{ mimetype : mimetype, filename: filename, dir: $('#dir').val() },
						function(response){
							if (response && response.status === 'success'){
								FileList.add(response.data, {animate: true, scrollTo: true});
							} else {
								OC.dialogs.alert(response.data.message, t('core', 'Could not create file'));
							}
						}
					);
				},

				_createDocumentFromTemplate: function(templateId, mimetype, filename) {
					OCA.Files.Files.isFileNameValid(filename);
					filename = FileList.getUniqueName(filename);
					odfViewer.onEdit(filename, {
						fileId: -1,
						dir: $('#dir').val(),
						templateId: templateId
					});
				},

				_openTemplatePicker: function(type, mimetype, filename) {
					var self = this;
					$.ajax({
						url: OC.linkToOCS('apps/richdocuments/api/v1/templates', 2)  + type,
						dataType: 'json'
					}).then(function(response) {
						self._buildTemplatePicker(response.ocs.data)
							.then(function() {
								var buttonlist = [{
									text: t('core', 'Cancel'),
									classes: 'cancel',
									click: function() {
										$(this).ocdialog('close');
									}
								}, {
									text: t('richdocuments', 'Create'),
									classes: 'primary',
									click: function() {
										var templateId = this.dataset.templateId;
										self._createDocumentFromTemplate(templateId, mimetype, filename);
										$(this).ocdialog('close');
									}
								}];

								$('#template-picker').ocdialog({
									closeOnEscape: true,
									modal: true,
									buttons: buttonlist
								});
							})
					})
				},

				_buildTemplatePicker: function(data) {
					var self = this;
					return $.get(OC.filePath('richdocuments', 'templates', 'templatePicker.html'), function(tmpl) {
						$tmpl = $(tmpl);
						// init template picker
						var $dlg = $tmpl.octemplate({
							dialog_name: 'template-picker',
							dialog_title: t('richdocuments','Select template'),
						});

						// create templates list
						var templates = _.values(data)
						templates.forEach(function(template) {
							self._appendTemplateFromData($dlg[0], template);
						})

						$('body').append($dlg);
					})
				},

				_appendTemplateFromData: function(dlg, data) {
					var self = this;
					var template = dlg.querySelector('.template-model').cloneNode(true);
					template.className = '';
					template.querySelector('img').src = OC.generateUrl('apps/richdocuments/template/preview/'+data.id);
					template.querySelector('h2').textContent = data.name;
					template.onclick = function() {
						dlg.dataset.templateId = data.id;
					};
					if (!dlg.dataset.templateId) {
						dlg.dataset.templateId = data.id;
					}

					dlg.querySelector('.template-container').appendChild(template);
				}
			};
		})(OCA);

		OC.Plugins.register('OCA.Files.NewFileMenu', OCA.FilesLOMenu);

		// Open the template picker if there was a create parameter detected on load
		if (!!(Preload.create && Preload.create.type && Preload.create.filename)) {
			var mimetype;
			var ext;
			switch (Preload.create.type) {
				case 'document':
					mimetype = docMime;
					ext = docExt;
					break;
				case 'spreadsheet':
					mimetype = spreadsheetMime;
					ext = spreadsheetExt;
					break;
				case 'presentation':
					mimetype = presentationMime;
					ext = presentationExt;
					break;
			}
			OCA.FilesLOMenu._openTemplatePicker(Preload.create.type, mimetype, Preload.create.filename + '.' + ext);
		}

	}
};

$(document).ready(function() {
	// register file actions and menu
	if ( typeof OCA !== 'undefined'
		&& typeof OCA.Files !== 'undefined'
		&& typeof OCA.Files.fileActions !== 'undefined'
	) {
		// check if texteditor app is enabled and loaded...
		if (_.isUndefined(OCA.Files_Texteditor)) {
			// it is not, so we do open text files with this app too.
			odfViewer.supportedMimes.push('text/plain');
		}

		// notice: when changing 'supportedMimes' interactively (e.g. dev console),
		// register() needs to be re-run to re-register the fileActions.
		odfViewer.register();

		$.get(OC.filePath('richdocuments', 'ajax', 'settings.php')).done(function(settings) {
			odfViewer.registerFilesMenu(settings);
		})

	}

	// Open documents if a public page is opened for a supported mimetype
	if ($('#isPublic').val() && odfViewer.supportedMimes.indexOf($('#mimetype').val()) !== -1) {
		odfViewer.onEdit($('#filename').val());
	}

	// listen to message from the viewer for closing/loading actions
	window.addEventListener('message', function(e) {
		if (e.data === 'close') {
			odfViewer.onClose();
		} else if(e.data === 'loading') {
			odfViewer.receivedLoading = true;
			$('#richdocumentsframe').show();
			$('#content').removeClass('loading');
			FileList.hideMask();
		}
	}, false);
});