1
|
|
|
/* |
2
|
|
|
* Copyright (c) 2013-2014 Lukas Reschke <[email protected]> |
3
|
|
|
* |
4
|
|
|
* This file is licensed under the Affero General Public License version 3 |
5
|
|
|
* or later. |
6
|
|
|
* |
7
|
|
|
* See the COPYING-README file. |
8
|
|
|
* |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
(function(OCA) { |
12
|
|
|
|
13
|
|
|
OCA.FilesPdfViewer = OCA.FilesPdfViewer || {}; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @namespace OCA.FilesPdfViewer.PreviewPlugin |
17
|
|
|
*/ |
18
|
|
|
OCA.FilesPdfViewer.PreviewPlugin = { |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param fileList |
22
|
|
|
*/ |
23
|
|
|
attach: function(fileList) { |
24
|
|
|
this._extendFileActions(fileList.fileActions); |
25
|
|
|
}, |
26
|
|
|
|
27
|
|
|
hide: function() { |
28
|
|
|
$('#pdframe').remove(); |
29
|
|
|
if ($('#isPublic').val() && $('#filesApp').val()){ |
30
|
|
|
$('#controls').removeClass('hidden'); |
31
|
|
|
$('#content').removeClass('full-height'); |
32
|
|
|
$('footer').removeClass('hidden'); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
FileList.setViewerMode(false); |
36
|
|
|
// replace the controls with our own |
37
|
|
|
$('#app-content #controls').removeClass('hidden'); |
38
|
|
|
}, |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param downloadUrl |
42
|
|
|
* @param isFileList |
43
|
|
|
*/ |
44
|
|
|
show: function(downloadUrl, isFileList) { |
45
|
|
|
var self = this; |
46
|
|
|
var $iframe; |
47
|
|
|
var isPdfVisible; |
48
|
|
|
var viewer = OC.generateUrl('/apps/files_pdfviewer/?file={file}', {file: downloadUrl}); |
49
|
|
|
$iframe = $('<iframe id="pdframe" style="width:100%;height:100%;display:block;position:absolute;top:0;z-index:10;" src="'+viewer+'" sandbox="allow-scripts allow-same-origin allow-popups allow-modals allow-top-navigation" />'); |
50
|
|
|
|
51
|
|
|
if(isFileList === true) { |
52
|
|
|
FileList.setViewerMode(true); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if ($('#isPublic').val()) { |
56
|
|
|
// force the preview to adjust its height |
57
|
|
|
$('#preview').append($iframe).css({height: '100%'}); |
58
|
|
|
$('body').css({height: '100%'}); |
59
|
|
|
$('#content').addClass('full-height'); |
60
|
|
|
$('footer').addClass('hidden'); |
61
|
|
|
$('#imgframe').addClass('hidden'); |
62
|
|
|
$('.directLink').addClass('hidden'); |
63
|
|
|
$('.directDownload').addClass('hidden'); |
64
|
|
|
$('#controls').addClass('hidden'); |
65
|
|
|
} else { |
66
|
|
|
$('#app-content').append($iframe); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$("#pageWidthOption").attr("selected","selected"); |
70
|
|
|
// replace the controls with our own |
71
|
|
|
$('#app-content #controls').addClass('hidden'); |
72
|
|
|
|
73
|
|
|
// if a filelist is present, the PDF viewer can be closed to go back there |
74
|
|
|
$('#pdframe').load(function(){ |
75
|
|
|
var iframe = $('#pdframe').contents(); |
76
|
|
|
if ($('#fileList').length) { |
77
|
|
|
isPdfVisible = true; |
78
|
|
|
history.pushState({}, '', '#pdfviewer'); |
79
|
|
|
iframe.find('#secondaryToolbarClose').click(function(){history.back()}); |
|
|
|
|
80
|
|
|
} else { |
81
|
|
|
iframe.find("#secondaryToolbarClose").addClass('hidden'); |
82
|
|
|
} |
83
|
|
|
}); |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
$(document).keyup(function(e) { |
87
|
|
|
if (isPdfVisible && e.keyCode == 27) { |
88
|
|
|
isPdfVisible = false; |
89
|
|
|
history.back(); |
90
|
|
|
} |
91
|
|
|
}); |
92
|
|
|
|
93
|
|
|
setTimeout(function () { |
94
|
|
|
$(window).one('popstate', function (e) { |
95
|
|
|
self.hide(); |
96
|
|
|
}); |
97
|
|
|
}, 0); |
98
|
|
|
}, |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param fileActions |
102
|
|
|
* @private |
103
|
|
|
*/ |
104
|
|
|
_extendFileActions: function(fileActions) { |
105
|
|
|
var self = this; |
106
|
|
|
fileActions.registerAction({ |
107
|
|
|
name: 'view', |
108
|
|
|
displayName: 'Favorite', |
109
|
|
|
mime: 'application/pdf', |
110
|
|
|
permissions: OC.PERMISSION_READ, |
111
|
|
|
actionHandler: function(fileName, context) { |
112
|
|
|
var downloadUrl = ''; |
113
|
|
|
if($('#isPublic').val()) { |
114
|
|
|
var sharingToken = $('#sharingToken').val(); |
115
|
|
|
downloadUrl = OC.generateUrl('/s/{token}/download?files={files}&path={path}', { |
116
|
|
|
token: sharingToken, |
117
|
|
|
files: fileName, |
118
|
|
|
path: context.dir |
119
|
|
|
}); |
120
|
|
|
} else { |
121
|
|
|
downloadUrl = Files.getDownloadUrl(fileName, context.dir); |
122
|
|
|
} |
123
|
|
|
self.show(downloadUrl, true); |
124
|
|
|
} |
125
|
|
|
}); |
126
|
|
|
fileActions.setDefault('application/pdf', 'view'); |
127
|
|
|
} |
128
|
|
|
}; |
129
|
|
|
|
130
|
|
|
})(OCA); |
131
|
|
|
|
132
|
|
|
OC.Plugins.register('OCA.Files.FileList', OCA.FilesPdfViewer.PreviewPlugin); |
133
|
|
|
|
134
|
|
|
// FIXME: Hack for single public file view since it is not attached to the fileslist |
135
|
|
|
$(document).ready(function(){ |
136
|
|
|
if ($('#isPublic').val() && $('#mimetype').val() === 'application/pdf') { |
137
|
|
|
var sharingToken = $('#sharingToken').val(); |
138
|
|
|
var downloadUrl = OC.generateUrl('/s/{token}/download', {token: sharingToken}); |
139
|
|
|
var viewer = OCA.FilesPdfViewer.PreviewPlugin; |
140
|
|
|
viewer.show(downloadUrl, false); |
141
|
|
|
} |
142
|
|
|
}); |
143
|
|
|
|
Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.
Further Readings: