Skip to content

Commit 9b26826

Browse files
author
Friedrich W. H. Kossebau
committed
Compile everything ViewerJS into a single HTML file
Support for running ViewerJS from source to be added later again
1 parent 41746bf commit 9b26826

10 files changed

+456
-131
lines changed

CMakeLists.txt

Lines changed: 88 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -90,19 +90,21 @@ set( PDFJS_SOURCE_DIR ${CMAKE_BINARY_DIR}/PDFJS-source )
9090

9191
set( WEBODF_SOURCE_DIR ${CMAKE_BINARY_DIR}/WebODF-source )
9292
set( WEBODF_BUILD_DIR ${CMAKE_BINARY_DIR}/WebODF-build )
93+
set( TOOLS_DIR ${WEBODF_SOURCE_DIR}/webodf/tools )
94+
set( RUNTIMEJS ${WEBODF_SOURCE_DIR}/webodf/lib/runtime.js )
9395

9496
set( WORDPRESS_ZIP_DIR ${CMAKE_CURRENT_BINARY_DIR}/viewerjs-wordpress-${VIEWERJS_VERSION})
9597
set( WORDPRESSZIPNAME viewerjs-wordpress-${VIEWERJS_VERSION}.zip)
9698
set( WORDPRESSZIP ${CMAKE_BINARY_DIR}/${WORDPRESSZIPNAME})
9799

98-
# HEADERCOMPILED_FILE defines the file to use as header for the compiled ViewerJS files.
99-
# Per default that is HeaderCompiled.js
100-
# For release builds it can be overwritten by passing -DHEADERCOMPILED_FILE=/path/to/file
100+
# HTMLHEADERCOMPILED_FILE defines the file to use as header for the compiled ViewerJS HTML files.
101+
# Per default that is HeaderCompiled.html
102+
# For release builds it can be overwritten by passing -DHTMLHEADERCOMPILED_FILE=/path/to/file
101103
# to cmake.
102-
if(NOT HEADERCOMPILED_FILE)
103-
set(HEADERCOMPILED_FILE "${CMAKE_SOURCE_DIR}/HeaderCompiled.js")
104-
elseif(NOT IS_ABSOLUTE ${HEADERCOMPILED_FILE})
105-
set(HEADERCOMPILED_FILE ${CMAKE_BINARY_DIR}/${HEADERCOMPILED_FILE})
104+
if(NOT HTMLHEADERCOMPILED_FILE)
105+
set(HTMLHEADERCOMPILED_FILE "${CMAKE_SOURCE_DIR}/HeaderCompiled.html")
106+
elseif(NOT IS_ABSOLUTE ${HTMLHEADERCOMPILED_FILE})
107+
set(HTMLHEADERCOMPILED_FILE ${CMAKE_BINARY_DIR}/${HTMLHEADERCOMPILED_FILE})
106108
endif()
107109

108110

@@ -154,57 +156,99 @@ configure_file(viewerjsversion.js.in ${CMAKE_CURRENT_BINARY_DIR}/viewerjsversion
154156
configure_file(pdfjsversion.js.in ${CMAKE_BINARY_DIR}/pdfjsversion.js)
155157
configure_file(viewerjs-plugin.php.in ${CMAKE_CURRENT_BINARY_DIR}/viewerjs-plugin.php)
156158

159+
add_custom_command(
160+
OUTPUT viewer.css.js
161+
COMMAND node ${RUNTIMEJS} ${TOOLS_DIR}/css2js.js
162+
viewer.css ${CMAKE_CURRENT_BINARY_DIR}/viewer.css.js
163+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
164+
DEPENDS
165+
WebODF
166+
${TOOLS_DIR}/css2js.js
167+
viewer.css
168+
)
169+
add_custom_target(viewer.css.js-target DEPENDS viewer.css.js)
170+
171+
add_custom_command(
172+
OUTPUT viewerTouch.css.js
173+
COMMAND node ${RUNTIMEJS} ${TOOLS_DIR}/css2js.js
174+
viewerTouch.css ${CMAKE_CURRENT_BINARY_DIR}/viewerTouch.css.js
175+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
176+
DEPENDS
177+
WebODF
178+
${TOOLS_DIR}/css2js.js
179+
viewer.css
180+
)
181+
add_custom_target(viewerTouch.css.js-target DEPENDS viewerTouch.css.js)
182+
183+
add_custom_command(
184+
OUTPUT ODFViewerPlugin.css.js
185+
COMMAND node ${RUNTIMEJS} ${TOOLS_DIR}/css2js.js
186+
ODFViewerPlugin.css ${CMAKE_CURRENT_BINARY_DIR}/ODFViewerPlugin.css.js
187+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
188+
DEPENDS
189+
WebODF
190+
${TOOLS_DIR}/css2js.js
191+
ODFViewerPlugin.css
192+
)
193+
add_custom_target(ODFViewerPlugin.css.js-target DEPENDS ODFViewerPlugin.css.js)
194+
195+
add_custom_command(
196+
OUTPUT PDFViewerPlugin.css.js
197+
COMMAND node ${RUNTIMEJS} ${TOOLS_DIR}/css2js.js
198+
PDFViewerPlugin.css ${CMAKE_CURRENT_BINARY_DIR}/PDFViewerPlugin.css.js
199+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
200+
DEPENDS
201+
WebODF
202+
${TOOLS_DIR}/css2js.js
203+
PDFViewerPlugin.css
204+
)
205+
add_custom_target(PDFViewerPlugin.css.js-target DEPENDS PDFViewerPlugin.css.js)
206+
157207
add_custom_command(
158208
OUTPUT viewer.min.js
159209
COMMENT "Creating viewer.min.js"
160210
COMMAND ${Java_JAVA_EXECUTABLE}
161211
-jar ${CLOSURE_JAR}
162-
--js ${HEADERCOMPILED_FILE}
212+
--js ${CMAKE_CURRENT_BINARY_DIR}/ODFViewerPlugin.css.js
213+
--js ${CMAKE_CURRENT_SOURCE_DIR}/ODFViewerPlugin.js
214+
--js ${CMAKE_CURRENT_BINARY_DIR}/PDFViewerPlugin.css.js
215+
--js ${CMAKE_CURRENT_SOURCE_DIR}/PDFViewerPlugin.js
163216
--js ${CMAKE_CURRENT_BINARY_DIR}/viewerjsversion.js
217+
--js ${CMAKE_CURRENT_BINARY_DIR}/viewer.css.js
218+
--js ${CMAKE_CURRENT_BINARY_DIR}/viewerTouch.css.js
164219
--js ${CMAKE_CURRENT_SOURCE_DIR}/viewer.js
165220
--js ${CMAKE_CURRENT_SOURCE_DIR}/PluginLoader.js
166221
--compilation_level SIMPLE_OPTIMIZATIONS
167222
--js_output_file ${CMAKE_CURRENT_BINARY_DIR}/viewer.min.js
168223
DEPENDS
169224
ClosureCompiler
170-
${HEADERCOMPILED_FILE}
225+
ODFViewerPlugin.css.js-target
226+
ODFViewerPlugin.js
227+
PDFViewerPlugin.css.js-target
228+
PDFViewerPlugin.js
171229
${CMAKE_CURRENT_BINARY_DIR}/viewerjsversion.js
230+
viewer.css.js-target
231+
viewerTouch.css.js-target
172232
viewer.js
173233
PluginLoader.js
174234
)
175235
add_custom_target(viewer.min.js-target DEPENDS viewer.min.js)
176236

177237
add_custom_command(
178-
OUTPUT ODFViewerPlugin.min.js
179-
COMMENT "Creating ODFViewerPlugin.min.js"
180-
COMMAND ${Java_JAVA_EXECUTABLE}
181-
-jar ${CLOSURE_JAR}
182-
--js ${HEADERCOMPILED_FILE}
183-
--js ${CMAKE_CURRENT_SOURCE_DIR}/ODFViewerPlugin.js
184-
--compilation_level SIMPLE_OPTIMIZATIONS
185-
--js_output_file ${CMAKE_CURRENT_BINARY_DIR}/ODFViewerPlugin.min.js
186-
DEPENDS
187-
ClosureCompiler
188-
${HEADERCOMPILED_FILE}
189-
ODFViewerPlugin.js
190-
)
191-
add_custom_target(ODFViewerPlugin.min.js-target DEPENDS ODFViewerPlugin.min.js)
192-
193-
add_custom_command(
194-
OUTPUT PDFViewerPlugin.min.js
195-
COMMENT "Creating PDFViewerPlugin.min.js"
196-
COMMAND ${Java_JAVA_EXECUTABLE}
197-
-jar ${CLOSURE_JAR}
198-
--js ${HEADERCOMPILED_FILE}
199-
--js ${CMAKE_CURRENT_SOURCE_DIR}/PDFViewerPlugin.js
200-
--compilation_level SIMPLE_OPTIMIZATIONS
201-
--js_output_file ${CMAKE_CURRENT_BINARY_DIR}/PDFViewerPlugin.min.js
238+
OUTPUT index.html
239+
COMMAND node ${RUNTIMEJS} ${CMAKE_SOURCE_DIR}/tools/replaceByFileContents.js
240+
${CMAKE_CURRENT_SOURCE_DIR}/index-template.html
241+
${CMAKE_CURRENT_BINARY_DIR}/index.html
242+
@VIEWERJSLICENSE_START@ @VIEWERJSLICENSE_END@ ${HTMLHEADERCOMPILED_FILE}
243+
@VIEWERJS_START@ @VIEWERJS_END@ ${CMAKE_CURRENT_BINARY_DIR}/viewer.min.js
202244
DEPENDS
203-
ClosureCompiler
204-
${HEADERCOMPILED_FILE}
205-
PDFViewerPlugin.js
206-
)
207-
add_custom_target(PDFViewerPlugin.min.js-target DEPENDS PDFViewerPlugin.min.js)
245+
WebODF
246+
${CMAKE_SOURCE_DIR}/tools/replaceByFileContents.js
247+
${HTMLHEADERCOMPILED_FILE}
248+
viewer.min.js-target
249+
index-template.html
250+
)
251+
add_custom_target(index.html-target DEPENDS index.html)
208252

209253
add_custom_command(
210254
OUTPUT ${VIEWER_BUILD_DIR}/ViewerJS
@@ -213,15 +257,9 @@ add_custom_command(
213257
COMMAND ${CMAKE_COMMAND} -E make_directory ${VIEWER_BUILD_DIR}
214258
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_CURRENT_SOURCE_DIR}/AGPL-3.0.txt ${VIEWER_BUILD_DIR}/AGPL-3.0.txt
215259
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/images ${VIEWER_BUILD_DIR}/ViewerJS/images
216-
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_CURRENT_SOURCE_DIR}/index.html ${VIEWER_BUILD_DIR}/ViewerJS/index.html
217-
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_CURRENT_BINARY_DIR}/viewer.min.js ${VIEWER_BUILD_DIR}/ViewerJS/viewer.js
218-
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_CURRENT_SOURCE_DIR}/viewer.css ${VIEWER_BUILD_DIR}/ViewerJS/viewer.css
260+
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_CURRENT_BINARY_DIR}/index.html ${VIEWER_BUILD_DIR}/ViewerJS/index.html
219261
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_CURRENT_SOURCE_DIR}/example.local.css ${VIEWER_BUILD_DIR}/ViewerJS/example.local.css
220-
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_CURRENT_BINARY_DIR}/ODFViewerPlugin.min.js ${VIEWER_BUILD_DIR}/ViewerJS/ODFViewerPlugin.js
221-
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_CURRENT_SOURCE_DIR}/ODFViewerPlugin.css ${VIEWER_BUILD_DIR}/ViewerJS/ODFViewerPlugin.css
222262
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${WEBODF_BUILD_DIR}/webodf/webodf.js ${VIEWER_BUILD_DIR}/ViewerJS
223-
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_CURRENT_BINARY_DIR}/PDFViewerPlugin.min.js ${VIEWER_BUILD_DIR}/ViewerJS/PDFViewerPlugin.js
224-
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_CURRENT_SOURCE_DIR}/PDFViewerPlugin.css ${VIEWER_BUILD_DIR}/ViewerJS/PDFViewerPlugin.css
225263
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_BINARY_DIR}/pdfjsversion.js ${VIEWER_BUILD_DIR}/ViewerJS/pdfjsversion.js
226264
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${PDFJS_SOURCE_DIR}/build/pdf.js ${VIEWER_BUILD_DIR}/ViewerJS/pdf.js
227265
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${PDFJS_SOURCE_DIR}/build/pdf.worker.js ${VIEWER_BUILD_DIR}/ViewerJS/pdf.worker.js
@@ -231,21 +269,16 @@ add_custom_command(
231269
DEPENDS
232270
WebODF
233271
PDFjs
234-
viewer.min.js-target
272+
index.html-target
235273
${VIEWER_IMAGES}
236274
index.html
237-
viewer.css
238275
example.local.css
239-
ODFViewerPlugin.min.js-target
240-
ODFViewerPlugin.css
241-
PDFViewerPlugin.min.js-target
242-
PDFViewerPlugin.css
243276
)
244277

245278
add_custom_command(
246279
OUTPUT ${VIEWERZIP}
247280
COMMENT "Creating ${VIEWERZIPNAME}"
248-
COMMAND node ARGS ${WEBODF_SOURCE_DIR}/webodf/lib/runtime.js ${WEBODF_SOURCE_DIR}/webodf/tools/zipdir.js
281+
COMMAND node ${RUNTIMEJS} ${TOOLS_DIR}/zipdir.js
249282
${VIEWER_BUILD_DIR}
250283
${VIEWERZIP}
251284
DEPENDS
@@ -264,9 +297,9 @@ add_custom_command(
264297
COMMAND ${CMAKE_COMMAND} -E copy_directory ${VIEWER_BUILD_DIR}/ViewerJS ${WORDPRESS_ZIP_DIR}
265298
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/viewerjs-plugin-README.txt ${WORDPRESS_ZIP_DIR}
266299
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_BINARY_DIR}/viewerjs-plugin.php ${WORDPRESS_ZIP_DIR}
267-
COMMAND node ARGS ${WEBODF_SOURCE_DIR}/webodf/lib/runtime.js ${WEBODF_SOURCE_DIR}/webodf/tools/zipdir.js
268-
${WORDPRESS_ZIP_DIR}
269-
${WORDPRESSZIP}
300+
COMMAND node ${RUNTIMEJS} ${TOOLS_DIR}/zipdir.js
301+
${WORDPRESS_ZIP_DIR}
302+
${WORDPRESSZIP}
270303
DEPENDS
271304
${VIEWER_BUILD_DIR}/ViewerJS
272305
)

HeaderCompiled.html

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
This is a generated file. DO NOT EDIT.
2+
3+
Copyright (C) 2012-2015 KO GmbH <copyright@kogmbh.com>
4+
5+
@licstart
6+
This file is the compiled version of the ViewerJS module.
7+
8+
ViewerJS is free software: you can redistribute it and/or modify it
9+
under the terms of the GNU Affero General Public License (GNU AGPL)
10+
as published by the Free Software Foundation, either version 3 of
11+
the License, or (at your option) any later version.
12+
13+
ViewerJS is distributed in the hope that it will be useful, but
14+
WITHOUT ANY WARRANTY; without even the implied warranty of
15+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
GNU Affero General Public License for more details.
17+
18+
You should have received a copy of the GNU Affero General Public License
19+
along with ViewerJS. If not, see <http://www.gnu.org/licenses/>.
20+
@licend
21+
22+
@source: http://viewerjs.org/
23+
@source: http://github.com/kogmbh/ViewerJS

ODFViewerPlugin.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ function ODFViewerPlugin() {
4949
callback();
5050
};
5151

52-
document.getElementsByTagName('head')[0].appendChild(lib);
52+
document.head.appendChild(lib);
5353

54-
pluginCSS = document.createElement('link');
55-
pluginCSS.setAttribute("rel", "stylesheet");
56-
pluginCSS.setAttribute("type", "text/css");
57-
pluginCSS.setAttribute("href", "./ODFViewerPlugin.css");
54+
pluginCSS = /**@type{!HTMLStyleElement}*/(document.createElementNS(document.head.namespaceURI, 'style'));
55+
pluginCSS.setAttribute('media', 'screen, print, handheld, projection');
56+
pluginCSS.setAttribute('type', 'text/css');
57+
pluginCSS.appendChild(document.createTextNode(ODFViewerPlugin_css));
5858
document.head.appendChild(pluginCSS);
5959
}
6060

PDFViewerPlugin.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ function PDFViewerPlugin() {
4747
loadScript('./pdfjsversion.js', callback);
4848
});
4949

50-
pluginCSS = document.createElement('link');
51-
pluginCSS.setAttribute("rel", "stylesheet");
52-
pluginCSS.setAttribute("type", "text/css");
53-
pluginCSS.setAttribute("href", "./PDFViewerPlugin.css");
50+
pluginCSS = /**@type{!HTMLStyleElement}*/(document.createElementNS(document.head.namespaceURI, 'style'));
51+
pluginCSS.setAttribute('media', 'screen, print, handheld, projection');
52+
pluginCSS.setAttribute('type', 'text/css');
53+
pluginCSS.appendChild(document.createTextNode(PDFViewerPlugin_css));
5454
document.head.appendChild(pluginCSS);
5555
}
5656

PluginLoader.js

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@
2424

2525
/*global document, window, Viewer, ODFViewerPlugin, PDFViewerPlugin*/
2626

27-
function loadDocument() {
27+
(function () {
2828
"use strict";
2929

30-
var pluginRegistry = [
30+
var css,
31+
pluginRegistry = [
3132
(function() {
3233
var odfMimetypes = [
3334
'application/vnd.oasis.opendocument.text',
@@ -73,19 +74,6 @@ function loadDocument() {
7374
}
7475
];
7576

76-
function loadPlugin(pluginName, callback) {
77-
"use strict";
78-
var script, style;
79-
80-
// Load script
81-
script = document.createElement('script');
82-
script.async = false;
83-
script.onload = callback;
84-
script.src = pluginName + '.js';
85-
script.type = 'text/javascript';
86-
document.getElementsByTagName('head')[0].appendChild(script);
87-
}
88-
8977

9078
function estimateTypeByHeaderContentType(documentUrl, cb) {
9179
var xhr = new XMLHttpRequest();
@@ -198,10 +186,15 @@ function loadDocument() {
198186
}
199187

200188
if (pluginData) {
201-
loadPlugin(pluginData.path, function () {
189+
if (String(typeof loadPlugin) !== "undefined") {
190+
loadPlugin(pluginData.path, function () {
191+
Plugin = pluginData.getClass();
192+
viewer = new Viewer(new Plugin(), parameters);
193+
});
194+
} else {
202195
Plugin = pluginData.getClass();
203196
viewer = new Viewer(new Plugin(), parameters);
204-
});
197+
}
205198
} else {
206199
viewer = new Viewer();
207200
}
@@ -210,4 +203,18 @@ function loadDocument() {
210203
viewer = new Viewer();
211204
}
212205
};
213-
}
206+
207+
css = /**@type{!HTMLStyleElement}*/(document.createElementNS(document.head.namespaceURI, 'style'));
208+
css.setAttribute('media', 'screen');
209+
css.setAttribute('type', 'text/css');
210+
css.appendChild(document.createTextNode(viewer_css));
211+
document.head.appendChild(css);
212+
213+
css = /**@type{!HTMLStyleElement}*/(document.createElementNS(document.head.namespaceURI, 'style'));
214+
css.setAttribute('media', 'only screen and (max-device-width: 800px) and (max-device-height: 800px)');
215+
css.setAttribute('type', 'text/css');
216+
css.setAttribute('viewerTouch', '1');
217+
css.appendChild(document.createTextNode(viewerTouch_css));
218+
document.head.appendChild(css);
219+
220+
}());

0 commit comments

Comments
 (0)