-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathviewer-base.js
466 lines (409 loc) · 14.7 KB
/
viewer-base.js
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/**
* @fileoverview viewer-base component
* @author lakenen
*/
Crocodoc.addComponent('viewer-base', function (scope) {
'use strict';
//--------------------------------------------------------------------------
// Private
//--------------------------------------------------------------------------
var util = scope.getUtility('common'),
browser = scope.getUtility('browser'),
support = scope.getUtility('support');
var api, // the viewer API object
config,
$el,
stylesheetEl,
layout,
scroller,
resizer,
dragger,
$assetsPromise;
/**
* Add CSS classes to the element for necessary feature/support flags
* @returns {void}
* @private
*/
function setCSSFlags() {
// add SVG version number flag
$el.attr(ATTR_SVG_VERSION, config.metadata.version || '0.0.0');
//add CSS flags
if (browser.mobile) {
$el.addClass(CSS_CLASS_MOBILE); //Mobile?
}
if (browser.ielt9) {
$el.addClass(CSS_CLASS_IELT9); //IE7 or IE8?
}
if (support.svg && config.useSVG) {
$el.addClass(CSS_CLASS_SUPPORTS_SVG);
}
}
/**
* Create and insert basic viewer DOM structure
* @returns {void}
* @private
*/
function initViewerHTML() {
// create viewer HTML
$el.html(Crocodoc.viewerTemplate);
if (config.useWindowAsViewport) {
config.$viewport = $(window);
$el.addClass(CSS_CLASS_WINDOW_AS_VIEWPORT);
} else {
config.$viewport = $el.find('.' + CSS_CLASS_VIEWPORT);
}
config.$doc = $el.find('.' + CSS_CLASS_DOC);
}
/**
* Initialize all plugins specified for this viewer instance
* @returns {void}
* @private
*/
function initPlugins() {
var name,
plugin,
plugins = config.plugins || {};
for (name in plugins) {
plugin = scope.createComponent('plugin-' + name);
if (plugin && util.isFn(plugin.init)) {
plugin.init(config.plugins[name]);
}
}
}
/**
* Complete intialization after document metadata has been loaded;
* ie., bind events, init lazyloader and layout, broadcast ready message
* @returns {void}
* @private
*/
function completeInit() {
setCSSFlags();
// initialize scroller and resizer components
scroller = scope.createComponent('scroller');
scroller.init(config.$viewport);
resizer = scope.createComponent('resizer');
resizer.init(config.$viewport);
var controller;
switch (config.metadata.type) {
case 'text':
// load text-based viewer
controller = scope.createComponent('controller-text');
// force the text layout only
// @TODO: allow overriding the layout eventually
config.layout = LAYOUT_TEXT;
break;
case 'paged':
/* falls through */
default:
controller = scope.createComponent('controller-paged');
break;
}
controller.init();
// disable text selection if necessary
if (config.metadata.type === 'text') {
if (!config.enableTextSelection) {
api.disableTextSelection();
}
} else if (browser.ielt9) {
api.disableTextSelection();
}
// disable links if necessary
// @NOTE: links are disabled in IE < 9
if (!config.enableLinks || browser.ielt9) {
api.disableLinks();
}
// set the initial layout
api.setLayout(config.layout);
// broadcast ready message
scope.broadcast('ready', {
page: config.page || 1,
numPages: config.numPages
});
scope.ready();
}
/**
* Handler for linkclick messages
* @returns {void}
* @private
*/
function handleLinkClick(data) {
var event = api.fire('linkclick', data);
if (!event.isDefaultPrevented()) {
if (data.uri) {
window.open(data.uri);
} else if (data.destination) {
api.scrollTo(data.destination.pagenum);
}
}
}
/**
* Enable or disable the dragger given the `isDraggable` flag
* @param {Boolean} isDraggable Whether or not the layout is draggable
* @returns {void}
* @private
*/
function updateDragger(isDraggable) {
if (isDraggable) {
if (!dragger) {
$el.addClass(CSS_CLASS_DRAGGABLE);
dragger = scope.createComponent('dragger');
dragger.init(config.$viewport);
}
} else {
if (dragger) {
$el.removeClass(CSS_CLASS_DRAGGABLE);
scope.destroyComponent(dragger);
dragger = null;
}
}
}
/**
* Validates and normalizes queryParams config option
* @returns {void}
*/
function validateQueryParams() {
var queryString;
if (config.queryParams) {
if (typeof config.queryParams === 'string') {
// strip '?' if it's there, because we add it below
queryString = config.queryParams.replace(/^\?/, '');
} else {
queryString = util.param(config.queryParams);
}
}
config.queryString = queryString ? '?' + queryString : '';
}
//--------------------------------------------------------------------------
// Public
//--------------------------------------------------------------------------
return {
messages: [
'asseterror',
'destroy',
'dragend',
'dragstart',
'fail',
'layoutchange',
'linkclick',
'pagefail',
'pagefocus',
'pageload',
'pageunload',
'ready',
'resize',
'scrollstart',
'scrollend',
'zoom'
],
/**
* Handle framework messages
* @param {string} name The name of the message
* @param {any} data The related data for the message
* @returns {void}
*/
onmessage: function (name, data) {
switch (name) {
case 'layoutchange':
api.updateLayout();
break;
case 'linkclick':
handleLinkClick(data);
break;
case 'zoom':
// artificially adjust the reported zoom to be accuate given the page scale
data.zoom *= config.pageScale;
data.prevZoom *= config.pageScale;
if (config.enableDragging) {
updateDragger(data.isDraggable);
}
// forward zoom event to external event handlers
api.fire(name, data);
break;
case 'dragstart':
if (!$el.hasClass(CSS_CLASS_DRAGGING)) {
$el.addClass(CSS_CLASS_DRAGGING);
}
// forward zoom event to external event handlers
api.fire(name, data);
break;
case 'dragend':
if ($el.hasClass(CSS_CLASS_DRAGGING)) {
$el.removeClass(CSS_CLASS_DRAGGING);
}
// forward zoom event to external event handlers
api.fire(name, data);
break;
default:
// forward subscribed framework messages to external event handlers
api.fire(name, data);
break;
}
},
/**
* Initialize the viewer api
* @returns {void}
*/
init: function () {
config = scope.getConfig();
api = config.api;
// create a unique CSS namespace for this viewer instance
config.namespace = CSS_CLASS_VIEWER + '-' + config.id;
// Setup container
$el = config.$el;
// add crocodoc viewer and namespace classes
$el.addClass(CSS_CLASS_VIEWER);
$el.addClass(config.namespace);
// add a / to the end of the base url if necessary
if (config.url) {
if (!/\/$/.test(config.url)) {
config.url += '/';
}
} else {
throw new Error('no URL given for viewer assets');
}
// make the url absolute
config.url = scope.getUtility('url').makeAbsolute(config.url);
//if useSVG hasn't been set, default to true
if (config.useSVG === undefined) {
config.useSVG = true;
}
validateQueryParams();
initViewerHTML();
initPlugins();
},
/**
* Destroy the viewer-base component
* @returns {void}
*/
destroy: function () {
// empty container and remove all class names that contain "crocodoc"
$el.empty().removeClass(function (i, cls) {
var match = cls.match(new RegExp('crocodoc\\S+', 'g'));
return match && match.join(' ');
});
// remove the stylesheet
$(stylesheetEl).remove();
if ($assetsPromise) {
$assetsPromise.abort();
}
},
/**
* Set the layout to the given mode, destroying and cleaning up the current
* layout if there is one
* @param {string} layoutMode The layout mode
* @returns {Layout} The layout object
*/
setLayout: function (layoutMode) {
// create a layout component with the new layout config
var lastPage = config.page,
lastZoom = config.zoom || 1,
previousLayoutMode = config.layout,
newLayout;
// if there is already a layout, save some state
if (layout) {
// ignore this if we already have the specified layout
if (layoutMode === previousLayoutMode) {
return layout;
}
lastPage = layout.state.currentPage;
lastZoom = layout.state.zoomState;
}
newLayout = scope.createComponent('layout-' + layoutMode);
if (!newLayout) {
throw new Error('Invalid layout ' + layoutMode);
}
// remove and destroy the existing layout component
// @NOTE: this must be done after we decide if the
// new layout exists!
if (layout) {
scope.destroyComponent(layout);
}
config.layout = layoutMode;
layout = newLayout;
layout.init();
layout.setZoom(lastZoom.zoomMode || lastZoom.zoom || lastZoom);
if (util.isFn(layout.scrollTo)) {
layout.scrollTo(lastPage);
}
config.currentLayout = layout;
scope.broadcast('layoutchange', {
// in the context of event data, `layout` and `previousLayout`
// are actually the name of those layouts, and not the layout
// objects themselves
previousLayout: previousLayoutMode,
layout: layoutMode
});
return layout;
},
/**
* Load the metadata and css for this document
* @returns {void}
*/
loadAssets: function () {
var $loadStylesheetPromise,
$loadMetadataPromise,
$pageOneContentPromise,
$pageOneTextPromise;
if ($assetsPromise) {
return;
}
$loadMetadataPromise = scope.get('metadata');
$loadMetadataPromise.then(function handleMetadataResponse(metadata) {
config.metadata = metadata;
});
// don't load the stylesheet for IE < 9
if (browser.ielt9) {
stylesheetEl = util.insertCSS('');
config.stylesheet = stylesheetEl.styleSheet;
$loadStylesheetPromise = $.when('').promise({
abort: function () {}
});
} else {
$loadStylesheetPromise = scope.get('stylesheet');
$loadStylesheetPromise.then(function handleStylesheetResponse(cssText) {
stylesheetEl = util.insertCSS(cssText);
config.stylesheet = stylesheetEl.sheet;
});
}
// load page 1 assets immediately if necessary
if (config.autoloadFirstPage &&
(!config.pageStart || config.pageStart === 1)) {
if (support.svg && config.useSVG) {
$pageOneContentPromise = scope.get('page-svg', 1);
} else if (config.conversionIsComplete) {
// unfortunately, page-1.png is not necessarily available
// on View API's document.viewable event, so we can only
// prefetch page-1.png if conversion is complete
$pageOneContentPromise = scope.get('page-img', 1);
}
if (config.enableTextSelection) {
$pageOneTextPromise = scope.get('page-text', 1);
}
}
// when both metatadata and stylesheet are done or if either fails...
$assetsPromise = $.when($loadMetadataPromise, $loadStylesheetPromise)
.fail(function (error) {
if ($assetsPromise) {
$assetsPromise.abort();
}
scope.ready();
scope.broadcast('asseterror', error);
scope.broadcast('fail', error);
})
.then(completeInit)
.promise({
abort: function () {
$assetsPromise = null;
$loadMetadataPromise.abort();
$loadStylesheetPromise.abort();
if ($pageOneContentPromise) {
$pageOneContentPromise.abort();
}
if ($pageOneTextPromise) {
$pageOneTextPromise.abort();
}
}
});
}
};
});