Blame view

sources/apps/documents/js/viewer/viewer.js 3.51 KB
6d9380f96   Cédric Dupont   Update sources OC...
1
  /* globals FileList, OCA.Files.fileActions, oc_debug */
d1bafeea1   Kload   [fix] Upgrade to ...
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  var odfViewer = {
  	isDocuments : false,
  	supportedMimesRead: [
  		'application/vnd.oasis.opendocument.text',
  		'application/vnd.oasis.opendocument.spreadsheet',
  		'application/vnd.oasis.opendocument.graphics',
  		'application/vnd.oasis.opendocument.presentation'
  	],
  			
  	supportedMimesUpdate: [
  		'application/vnd.oasis.opendocument.text'
  	],
  			
  	register : function(response){
  		if (response && response.mimes){
  			jQuery.each(response.mimes, function(i, mime){
  				odfViewer.supportedMimesRead.push(mime);
  				odfViewer.supportedMimesUpdate.push(mime);
  			});
  		}
  		for (var i = 0; i < odfViewer.supportedMimesRead.length; ++i) {
  			var mime = odfViewer.supportedMimesRead[i];
6d9380f96   Cédric Dupont   Update sources OC...
24
25
  			OCA.Files.fileActions.register(mime, 'View', OC.PERMISSION_READ, '', odfViewer.onView);
  			OCA.Files.fileActions.setDefault(mime, 'View');
d1bafeea1   Kload   [fix] Upgrade to ...
26
27
28
  		}
  		for (var i = 0; i < odfViewer.supportedMimesUpdate.length; ++i) {
  			var mime = odfViewer.supportedMimesUpdate[i];
6d9380f96   Cédric Dupont   Update sources OC...
29
  			OCA.Files.fileActions.register(
d1bafeea1   Kload   [fix] Upgrade to ...
30
31
32
33
34
35
36
37
38
39
  					mime, 
  					t('documents', 'Edit'), 
  					OC.PERMISSION_UPDATE, 
  					OC.imagePath('core', 'actions/rename'), 
  					odfViewer.onEdit
  			);
  		}
  	},
  	
  	dispatch : function(filename){
6d9380f96   Cédric Dupont   Update sources OC...
40
41
  		if (odfViewer.supportedMimesUpdate.indexOf(OCA.Files.fileActions.getCurrentMimeType()) !== -1
  		 && OCA.Files.fileActions.getCurrentPermissions() & OC.PERMISSION_UPDATE
d1bafeea1   Kload   [fix] Upgrade to ...
42
43
44
45
46
47
48
49
  		){
  			odfViewer.onEdit(filename);
  		} else {
  			odfViewer.onView(filename);
  		}
  	},
  	
  	onEdit : function(){
6d9380f96   Cédric Dupont   Update sources OC...
50
51
  		var fileId = OCA.Files.fileActions.currentFile.parent().attr('data-id');
  		window.location = OC.linkTo('documents', 'index.php') + '#' + fileId;
d1bafeea1   Kload   [fix] Upgrade to ...
52
53
54
55
  	},
  			
  	onView: function(filename) {
  		var webodfSource = (oc_debug === true) ? 'webodf-debug' : 'webodf',
6d9380f96   Cédric Dupont   Update sources OC...
56
  		attachTo = odfViewer.isDocuments ? '#documents-content' : '#controls',
d1bafeea1   Kload   [fix] Upgrade to ...
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
  		attachToolbarTo = odfViewer.isDocuments ? '#content-wrapper' : '#controls';
  
  		if (odfViewer.isDocuments){
  			//Documents view
  			var location = filename;
  		} else {
  			//Public page, files app, etc
  			var dirName = $('#dir').val()!='/' ? $('#dir').val() + '/' : '/';
  			var location = OC.filePath('documents', 'ajax', 'download.php') + '?path=' + dirName + encodeURIComponent(filename);
  			OC.addStyle('documents', '3rdparty/webodf/editor');
  		}
  		
  		OC.addStyle('documents', 'viewer/odfviewer');
  		
  		OC.addScript('documents', '3rdparty/webodf/' + webodfSource, function() {
6d9380f96   Cédric Dupont   Update sources OC...
72
  			FileList.setViewerMode(true);
d1bafeea1   Kload   [fix] Upgrade to ...
73

6d9380f96   Cédric Dupont   Update sources OC...
74
75
76
77
78
79
80
81
82
83
84
  			// odf action toolbar
  			var odfToolbarHtml =
  					'<div id="odf-toolbar">' +
  					'<button id="odf_close">' + t('documents', 'Close') +
  					'</button></div>';
  			if (odfViewer.isDocuments){
  				$(attachToolbarTo).prepend(odfToolbarHtml);
  				$('#odf-toolbar').css({position:'fixed'});
  			} else {
  				$(attachToolbarTo).append(odfToolbarHtml);
  			}
d1bafeea1   Kload   [fix] Upgrade to ...
85

6d9380f96   Cédric Dupont   Update sources OC...
86
87
88
89
90
91
92
93
  			var canvashtml = '<div id="odf-canvas"></div>';
  			$(attachTo).after(canvashtml);
  			// in case we are on the public sharing page we shall display the odf into the preview tag
  			$('#preview').html(canvashtml);
  
  			var odfelement = document.getElementById("odf-canvas");
  			var odfcanvas = new odf.OdfCanvas(odfelement);
  			odfcanvas.load(location);
d1bafeea1   Kload   [fix] Upgrade to ...
94
95
96
97
  		});
  	},
  	
  	onClose: function() {
6d9380f96   Cédric Dupont   Update sources OC...
98
99
100
  		FileList.setViewerMode(false);
  		$('#odf-toolbar').remove();
  		$('#odf-canvas').remove();
d1bafeea1   Kload   [fix] Upgrade to ...
101
102
103
104
  	}
  };
  
  $(document).ready(function() {
6d9380f96   Cédric Dupont   Update sources OC...
105
106
107
108
  	if ( typeof OCA !== 'undefined' 
  		&& typeof OCA.Files !== 'undefined'
  		&& typeof OCA.Files.fileActions !== 'undefined'
  	) {
d1bafeea1   Kload   [fix] Upgrade to ...
109
110
111
112
113
114
115
116
117
  		$.post(
  			OC.filePath('documents', 'ajax', 'mimes.php'),
  			{},
  			odfViewer.register
  		);
  	}
  
  	$('#odf_close').live('click', odfViewer.onClose);
  });