viewerjs-rails 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +14 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +65 -0
  6. data/Rakefile +2 -0
  7. data/app/assets/AGPL-3.0.txt +661 -0
  8. data/app/assets/images/kogmbh.png +0 -0
  9. data/app/assets/images/nlnet.png +0 -0
  10. data/app/assets/images/texture.png +0 -0
  11. data/app/assets/images/toolbarButton-download.png +0 -0
  12. data/app/assets/images/toolbarButton-fullscreen.png +0 -0
  13. data/app/assets/images/toolbarButton-menuArrows.png +0 -0
  14. data/app/assets/images/toolbarButton-pageDown.png +0 -0
  15. data/app/assets/images/toolbarButton-pageUp.png +0 -0
  16. data/app/assets/images/toolbarButton-presentation.png +0 -0
  17. data/app/assets/images/toolbarButton-zoomIn.png +0 -0
  18. data/app/assets/images/toolbarButton-zoomOut.png +0 -0
  19. data/app/assets/javascripts/pdf.worker.js +39900 -0
  20. data/app/assets/javascripts/viewerjs/ODFViewerPlugin.js +219 -0
  21. data/app/assets/javascripts/viewerjs/PDFViewerPlugin.js.erb +348 -0
  22. data/app/assets/javascripts/viewerjs/PluginLoader.js +47 -0
  23. data/app/assets/javascripts/viewerjs/compatibility.js +491 -0
  24. data/app/assets/javascripts/viewerjs/pdf.js +7651 -0
  25. data/app/assets/javascripts/viewerjs/pdf_find_bar.js +175 -0
  26. data/app/assets/javascripts/viewerjs/pdf_find_controller.js +355 -0
  27. data/app/assets/javascripts/viewerjs/pdfjsversion.js +1 -0
  28. data/app/assets/javascripts/viewerjs/text_layer_builder.js +385 -0
  29. data/app/assets/javascripts/viewerjs/ui_utils.js +270 -0
  30. data/app/assets/javascripts/viewerjs/viewer.js +230 -0
  31. data/app/assets/javascripts/viewerjs/webodf.js +631 -0
  32. data/app/assets/javascripts/viewerjs_rails.js +22 -0
  33. data/app/assets/stylesheets/ODFViewerPlugin.css.scss +29 -0
  34. data/app/assets/stylesheets/PDFViewerPlugin.css.scss +36 -0
  35. data/app/assets/stylesheets/example.local.css.scss +27 -0
  36. data/app/assets/stylesheets/viewer.css.scss +816 -0
  37. data/app/assets/stylesheets/viewerjs_rails.css +15 -0
  38. data/lib/viewerjs/rails.rb +19 -0
  39. data/lib/viewerjs/rails/version.rb +5 -0
  40. data/lib/viewerjs/view_helpers.rb +75 -0
  41. data/viewerjs-rails.gemspec +23 -0
  42. metadata +113 -0
@@ -0,0 +1,219 @@
1
+ /**
2
+ * Copyright (C) 2012 KO GmbH <copyright@kogmbh.com>
3
+ *
4
+ * @licstart
5
+ * This file is part of WebODF.
6
+ *
7
+ * WebODF is free software: you can redistribute it and/or modify it
8
+ * under the terms of the GNU Affero General Public License (GNU AGPL)
9
+ * as published by the Free Software Foundation, either version 3 of
10
+ * the License, or (at your option) any later version.
11
+ *
12
+ * WebODF is distributed in the hope that it will be useful, but
13
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ * GNU Affero General Public License for more details.
16
+ *
17
+ * You should have received a copy of the GNU Affero General Public License
18
+ * along with WebODF. If not, see <http://www.gnu.org/licenses/>.
19
+ * @licend
20
+ *
21
+ * @source: http://www.webodf.org/
22
+ * @source: https://github.com/kogmbh/WebODF/
23
+ */
24
+
25
+ /*global runtime, document, odf, gui, console, webodf*/
26
+
27
+ function ODFViewerPlugin() {
28
+ "use strict";
29
+
30
+ function init(callback) {
31
+ var lib = document.createElement('script'),
32
+ pluginCSS;
33
+
34
+ lib.async = false;
35
+ lib.src = './webodf.js';
36
+ lib.type = 'text/javascript';
37
+ lib.onload = function () {
38
+ runtime.loadClass('gui.HyperlinkClickHandler');
39
+ runtime.loadClass('odf.OdfCanvas');
40
+ runtime.loadClass('ops.Session');
41
+ runtime.loadClass('gui.CaretManager');
42
+ runtime.loadClass("gui.HyperlinkTooltipView");
43
+ runtime.loadClass('gui.SessionController');
44
+ runtime.loadClass('gui.SvgSelectionView');
45
+ runtime.loadClass('gui.SelectionViewManager');
46
+ runtime.loadClass('gui.ShadowCursor');
47
+ runtime.loadClass('gui.SessionView');
48
+
49
+ callback();
50
+ };
51
+
52
+ document.getElementsByTagName('head')[0].appendChild(lib);
53
+
54
+ pluginCSS = document.createElement('link');
55
+ pluginCSS.setAttribute("rel", "stylesheet");
56
+ pluginCSS.setAttribute("type", "text/css");
57
+ pluginCSS.setAttribute("href", "./ODFViewerPlugin.css");
58
+ document.head.appendChild(pluginCSS);
59
+ }
60
+
61
+ // that should probably be provided by webodf
62
+ function nsResolver(prefix) {
63
+ var ns = {
64
+ 'draw' : "urn:oasis:names:tc:opendocument:xmlns:drawing:1.0",
65
+ 'presentation' : "urn:oasis:names:tc:opendocument:xmlns:presentation:1.0",
66
+ 'text' : "urn:oasis:names:tc:opendocument:xmlns:text:1.0",
67
+ 'office' : "urn:oasis:names:tc:opendocument:xmlns:office:1.0"
68
+ };
69
+ return ns[prefix] || console.log('prefix [' + prefix + '] unknown.');
70
+ }
71
+
72
+ var self = this,
73
+ pluginName = "WebODF",
74
+ pluginURL = "http://webodf.org",
75
+ odfCanvas = null,
76
+ odfElement = null,
77
+ initialized = false,
78
+ root = null,
79
+ documentType = null,
80
+ pages = [],
81
+ currentPage = null;
82
+
83
+ this.initialize = function (viewerElement, documentUrl) {
84
+ // If the URL has a fragment (#...), try to load the file it represents
85
+ init(function () {
86
+ var session,
87
+ sessionController,
88
+ sessionView,
89
+ odtDocument,
90
+ shadowCursor,
91
+ selectionViewManager,
92
+ caretManager,
93
+ localMemberId = 'localuser',
94
+ hyperlinkTooltipView,
95
+ eventManager;
96
+
97
+ odfElement = document.getElementById('canvas');
98
+ odfCanvas = new odf.OdfCanvas(odfElement);
99
+ odfCanvas.load(documentUrl);
100
+
101
+ odfCanvas.addListener('statereadychange', function () {
102
+ root = odfCanvas.odfContainer().rootElement;
103
+ initialized = true;
104
+ documentType = odfCanvas.odfContainer().getDocumentType(root);
105
+ if (documentType === 'text') {
106
+ odfCanvas.enableAnnotations(true, false);
107
+
108
+ session = new ops.Session(odfCanvas);
109
+ odtDocument = session.getOdtDocument();
110
+ shadowCursor = new gui.ShadowCursor(odtDocument);
111
+ sessionController = new gui.SessionController(session, localMemberId, shadowCursor, {});
112
+ eventManager = sessionController.getEventManager();
113
+ caretManager = new gui.CaretManager(sessionController);
114
+ selectionViewManager = new gui.SelectionViewManager(gui.SvgSelectionView);
115
+ sessionView = new gui.SessionView({
116
+ caretAvatarsInitiallyVisible: false
117
+ }, localMemberId, session, sessionController.getSessionConstraints(), caretManager, selectionViewManager);
118
+ selectionViewManager.registerCursor(shadowCursor);
119
+ hyperlinkTooltipView = new gui.HyperlinkTooltipView(odfCanvas,
120
+ sessionController.getHyperlinkClickHandler().getModifier);
121
+ eventManager.subscribe("mousemove", hyperlinkTooltipView.showTooltip);
122
+ eventManager.subscribe("mouseout", hyperlinkTooltipView.hideTooltip);
123
+
124
+ var op = new ops.OpAddMember();
125
+ op.init({
126
+ memberid: localMemberId,
127
+ setProperties: {
128
+ fillName: runtime.tr("Unknown Author"),
129
+ color: "blue"
130
+ }
131
+ });
132
+ session.enqueue([op]);
133
+ sessionController.insertLocalCursor();
134
+ }
135
+
136
+ self.onLoad();
137
+ });
138
+ });
139
+ };
140
+
141
+ this.isSlideshow = function () {
142
+ return documentType === 'presentation';
143
+ };
144
+
145
+ this.onLoad = function () {};
146
+
147
+ this.getWidth = function () {
148
+ return odfElement.clientWidth;
149
+ };
150
+
151
+ this.getHeight = function () {
152
+ return odfElement.clientHeight;
153
+ };
154
+
155
+ this.fitToWidth = function (width) {
156
+ odfCanvas.fitToWidth(width);
157
+ };
158
+
159
+ this.fitToHeight = function (height) {
160
+ odfCanvas.fitToHeight(height);
161
+ };
162
+
163
+ this.fitToPage = function (width, height) {
164
+ odfCanvas.fitToContainingElement(width, height);
165
+ };
166
+
167
+ this.fitSmart = function (width) {
168
+ odfCanvas.fitSmart(width);
169
+ };
170
+
171
+ this.getZoomLevel = function () {
172
+ return odfCanvas.getZoomLevel();
173
+ };
174
+
175
+ this.setZoomLevel = function (value) {
176
+ odfCanvas.setZoomLevel(value);
177
+ };
178
+
179
+ // return a list of tuples (pagename, pagenode)
180
+ this.getPages = function () {
181
+ var pageNodes = Array.prototype.slice.call(root.getElementsByTagNameNS(nsResolver('draw'), 'page')),
182
+ pages = [],
183
+ i,
184
+ tuple;
185
+
186
+ for (i = 0; i < pageNodes.length; i += 1) {
187
+ tuple = [
188
+ pageNodes[i].getAttribute('draw:name'),
189
+ pageNodes[i]
190
+ ];
191
+ pages.push(tuple);
192
+ }
193
+ return pages;
194
+ };
195
+
196
+ this.showPage = function (n) {
197
+ odfCanvas.showPage(n);
198
+ };
199
+
200
+ this.getPluginName = function () {
201
+ return pluginName;
202
+ };
203
+
204
+ this.getPluginVersion = function () {
205
+ var version;
206
+
207
+ if (String(typeof webodf) !== "undefined") {
208
+ version = webodf.Version;
209
+ } else {
210
+ version = "Unknown";
211
+ }
212
+
213
+ return version;
214
+ };
215
+
216
+ this.getPluginURL = function () {
217
+ return pluginURL;
218
+ };
219
+ }
@@ -0,0 +1,348 @@
1
+ /**
2
+ * @license
3
+ * Copyright (C) 2013-2014 KO GmbH <copyright@kogmbh.com>
4
+ *
5
+ * @licstart
6
+ * The JavaScript code in this page is free software: you can redistribute it
7
+ * and/or modify it under the terms of the GNU Affero General Public License
8
+ * (GNU AGPL) as published by the Free Software Foundation, either version 3 of
9
+ * the License, or (at your option) any later version. The code is distributed
10
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU AGPL for more details.
12
+ *
13
+ * You should have received a copy of the GNU Affero General Public License
14
+ * along with this code. If not, see <http://www.gnu.org/licenses/>.
15
+ *
16
+ * As additional permission under GNU AGPL version 3 section 7, you
17
+ * may distribute non-source (e.g., minimized or compacted) forms of
18
+ * that code without the copy of the GNU GPL normally required by
19
+ * section 4, provided you include this license notice and a URL
20
+ * through which recipients can access the Corresponding Source.
21
+ *
22
+ * As a special exception to the AGPL, any HTML file which merely makes function
23
+ * calls to this code, and for that purpose includes it by reference shall be
24
+ * deemed a separate work for copyright law purposes. In addition, the copyright
25
+ * holders of this code give you permission to combine this code with free
26
+ * software libraries that are released under the GNU LGPL. You may copy and
27
+ * distribute such a system following the terms of the GNU AGPL for this code
28
+ * and the LGPL for the libraries. If you modify this code, you may extend this
29
+ * exception to your version of the code, but you are not obligated to do so.
30
+ * If you do not wish to do so, delete this exception statement from your
31
+ * version.
32
+ *
33
+ * This license applies to this entire compilation.
34
+ * @licend
35
+ * @source: http://viewerjs.org/
36
+ * @source: http://github.com/kogmbh/ViewerJS
37
+ */
38
+
39
+ /*global document, PDFJS, console, TextLayerBuilder*/
40
+
41
+
42
+ function PDFViewerPlugin() {
43
+ "use strict";
44
+
45
+ function init(callback) {
46
+ var pdfLib, textLayerLib, pluginCSS;
47
+ callback();
48
+ }
49
+
50
+ var self = this,
51
+ pages = [],
52
+ domPages = [],
53
+ pageText = [],
54
+ renderingStates = [],
55
+ RENDERING = {
56
+ BLANK: 0,
57
+ RUNNING: 1,
58
+ FINISHED: 2
59
+ },
60
+ startedTextExtraction = false,
61
+ container = null,
62
+ initialized = false,
63
+ pdfDocument = null,
64
+ pageViewScroll = null,
65
+ isPresentationMode = false,
66
+ scale = 1,
67
+ currentPage = 1,
68
+ pageWidth,
69
+ pageHeight,
70
+ createdPageCount = 0;
71
+
72
+ function scrollIntoView(elem) {
73
+ elem.parentNode.scrollTop = elem.offsetTop;
74
+ }
75
+
76
+ function isScrolledIntoView(elem) {
77
+ var docViewTop = container.scrollTop,
78
+ docViewBottom = docViewTop + container.clientHeight,
79
+ elemTop = elem.offsetTop,
80
+ elemBottom = elemTop + elem.clientHeight;
81
+
82
+ // Is in view if either the top or the bottom of the page is between the
83
+ // document viewport bounds,
84
+ // or if the top is above the viewport and the bottom is below it.
85
+ return (elemTop >= docViewTop && elemTop < docViewBottom)
86
+ || (elemBottom >= docViewTop && elemBottom < docViewBottom)
87
+ || (elemTop < docViewTop && elemBottom >= docViewBottom);
88
+ }
89
+
90
+ function getDomPage(page) {
91
+ return domPages[page.pageInfo.pageIndex];
92
+ }
93
+
94
+ function getPageText(page) {
95
+ return pageText[page.pageInfo.pageIndex];
96
+ }
97
+
98
+ function getRenderingStatus(page) {
99
+ return renderingStates[page.pageInfo.pageIndex];
100
+ }
101
+
102
+ function setRenderingStatus(page, renderStatus) {
103
+ renderingStates[page.pageInfo.pageIndex] = renderStatus;
104
+ }
105
+
106
+ function updatePageDimensions(page, width, height) {
107
+ var domPage = getDomPage(page),
108
+ canvas = domPage.getElementsByTagName('canvas')[0],
109
+ textLayer = domPage.getElementsByTagName('div')[0],
110
+ cssScale = 'scale(' + scale + ', ' + scale + ')';
111
+
112
+ domPage.style.width = width + "px";
113
+ domPage.style.height = height + "px";
114
+
115
+ canvas.width = width;
116
+ canvas.height = height;
117
+
118
+ textLayer.style.width = width + "px";
119
+ textLayer.style.height = height + "px";
120
+
121
+ CustomStyle.setProp('transform', textLayer, cssScale);
122
+ CustomStyle.setProp('transformOrigin', textLayer, '0% 0%');
123
+
124
+ // Once the page dimension is updated, the rendering state is blank.
125
+ setRenderingStatus(page, RENDERING.BLANK);
126
+ }
127
+
128
+ function renderPage(page) {
129
+ var domPage = getDomPage(page),
130
+ textLayer = getPageText(page),
131
+ canvas = domPage.getElementsByTagName('canvas')[0];
132
+
133
+ if (getRenderingStatus(page) === RENDERING.BLANK) {
134
+ setRenderingStatus(page, RENDERING.RUNNING);
135
+ page.render({
136
+ canvasContext: canvas.getContext('2d'),
137
+ textLayer: textLayer,
138
+ viewport: page.getViewport(scale)
139
+ }).promise.then(function () {
140
+ setRenderingStatus(page, RENDERING.FINISHED);
141
+ });
142
+ }
143
+ }
144
+
145
+ function createPage(page) {
146
+ var pageNumber,
147
+ textLayerDiv,
148
+ textLayer,
149
+ canvas,
150
+ domPage,
151
+ viewport;
152
+
153
+ pageNumber = page.pageInfo.pageIndex + 1;
154
+
155
+ viewport = page.getViewport(scale);
156
+
157
+ domPage = document.createElement('div');
158
+ domPage.id = 'pageContainer' + pageNumber;
159
+ domPage.className = 'page';
160
+
161
+ canvas = document.createElement('canvas');
162
+ canvas.id = 'canvas' + pageNumber;
163
+
164
+ textLayerDiv = document.createElement('div');
165
+ textLayerDiv.className = 'textLayer';
166
+ textLayerDiv.id = 'textLayer' + pageNumber;
167
+
168
+ container.appendChild(domPage);
169
+ domPage.appendChild(canvas);
170
+ domPage.appendChild(textLayerDiv);
171
+
172
+ pages.push(page);
173
+ domPages.push(domPage);
174
+ renderingStates.push(RENDERING.BLANK);
175
+
176
+ updatePageDimensions(page, viewport.width, viewport.height);
177
+ pageWidth = viewport.width;
178
+ pageHeight = viewport.height;
179
+
180
+ textLayer = new TextLayerBuilder({
181
+ textLayerDiv: textLayerDiv,
182
+ pageIndex: pageNumber - 1
183
+ });
184
+ page.getTextContent().then(function (textContent) {
185
+ textLayer.setTextContent(textContent);
186
+ });
187
+ pageText.push(textLayer);
188
+
189
+ createdPageCount += 1;
190
+ if (createdPageCount === (pdfDocument.numPages)) {
191
+ if (self.isSlideshow()) {
192
+ domPages.forEach(function (pageElement) {
193
+ pageElement.style.display = "none";
194
+ });
195
+ self.showPage(1);
196
+ }
197
+ self.onLoad();
198
+ }
199
+ }
200
+
201
+ this.initialize = function (viewContainer, location) {
202
+ var self = this,
203
+ i,
204
+ pluginCSS;
205
+
206
+ init(function () {
207
+ //TODO: Make it available from asset pipeline
208
+ // PDFJS.workerSrc = "../viewer_js/javascripts/pdf.worker.js";
209
+ PDFJS.workerSrc = "<%=javascript_path('pdf.worker.js') %>";
210
+ console.log(location);
211
+ PDFJS.getDocument(location).then(function loadPDF(doc) {
212
+ pdfDocument = doc;
213
+ container = viewContainer;
214
+
215
+ for (i = 0; i < pdfDocument.numPages; i += 1) {
216
+ pdfDocument.getPage(i + 1).then(createPage);
217
+ }
218
+
219
+ initialized = true;
220
+ });
221
+ });
222
+ };
223
+
224
+ this.isSlideshow = function () {
225
+ // A very simple but generally true guess - if the width is greater than the height, treat it as a slideshow
226
+ return pageWidth > pageHeight;
227
+ };
228
+
229
+ this.onLoad = function () {
230
+ };
231
+
232
+ this.getPages = function () {
233
+ return domPages;
234
+ };
235
+
236
+ this.getWidth = function () {
237
+ return pageWidth;
238
+ };
239
+
240
+ this.getHeight = function () {
241
+ return pageHeight;
242
+ };
243
+
244
+ this.fitToWidth = function (width) {
245
+ var zoomLevel;
246
+
247
+ if (self.getWidth() === width) {
248
+ return;
249
+ }
250
+ zoomLevel = width / pageWidth;
251
+ self.setZoomLevel(zoomLevel);
252
+ };
253
+
254
+ this.fitToHeight = function (height) {
255
+ var zoomLevel;
256
+
257
+ if (self.getHeight() === height) {
258
+ return;
259
+ }
260
+ zoomLevel = height / pageHeight;
261
+ self.setZoomLevel(zoomLevel);
262
+ };
263
+
264
+ this.fitToPage = function (width, height) {
265
+ var zoomLevel = width / pageWidth;
266
+ if (height / pageHeight < zoomLevel) {
267
+ zoomLevel = height / pageHeight;
268
+ }
269
+ self.setZoomLevel(zoomLevel);
270
+ };
271
+
272
+ this.fitSmart = function (width, height) {
273
+ var zoomLevel = width / pageWidth;
274
+ if (height && (height / pageHeight) < zoomLevel) {
275
+ zoomLevel = height / pageHeight;
276
+ }
277
+ zoomLevel = Math.min(1.0, zoomLevel);
278
+ self.setZoomLevel(zoomLevel);
279
+ };
280
+
281
+ this.setZoomLevel = function (zoomLevel) {
282
+ var i;
283
+
284
+ if (scale !== zoomLevel) {
285
+ scale = zoomLevel;
286
+
287
+ for (i = 0; i < pages.length; i += 1) {
288
+ updatePageDimensions(pages[i], pageWidth * scale, pageHeight * scale);
289
+ }
290
+ }
291
+ };
292
+
293
+ this.getZoomLevel = function () {
294
+ return scale;
295
+ };
296
+
297
+ this.onScroll = function () {
298
+ var i;
299
+
300
+ for (i = 0; i < domPages.length; i += 1) {
301
+ if (isScrolledIntoView(domPages[i])) {
302
+ if (getRenderingStatus(pages[i]) === RENDERING.BLANK) {
303
+ renderPage(pages[i]);
304
+ }
305
+ }
306
+ }
307
+ };
308
+
309
+ this.getPageInView = function () {
310
+ var i;
311
+
312
+ if (self.isSlideshow()) {
313
+ return currentPage;
314
+ } else {
315
+ for (i = 0; i < domPages.length; i += 1) {
316
+ if (isScrolledIntoView(domPages[i])) {
317
+ return i + 1;
318
+ }
319
+ }
320
+ }
321
+ };
322
+
323
+ this.showPage = function (n) {
324
+ if (self.isSlideshow()) {
325
+ domPages[currentPage - 1].style.display = "none";
326
+ currentPage = n;
327
+ domPages[n - 1].style.display = "block";
328
+ } else {
329
+ scrollIntoView(domPages[n - 1]);
330
+ }
331
+ };
332
+
333
+ this.getPluginName = function () {
334
+ return "PDF.js"
335
+ };
336
+
337
+ this.getPluginVersion = function () {
338
+ var version = (String(typeof pdfjs_version) !== "undefined"
339
+ ? pdfjs_version
340
+ : "From Source"
341
+ );
342
+ return version;
343
+ };
344
+
345
+ this.getPluginURL = function () {
346
+ return "https://github.com/mozilla/pdf.js/";
347
+ };
348
+ }