-
Notifications
You must be signed in to change notification settings - Fork 0
/
eirt.js
508 lines (462 loc) · 13.4 KB
/
eirt.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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
/**!
* Exame Informática Reader Tools
* https://github.com/pedrofsantoscom/exame-informatica-reader-tools
*
* Copyright 2015 Pedro F. Santos, http://pedrofsantos.com, [email protected]
* Released under the MIT license
* http://en.wikipedia.org/wiki/MIT_License
*
* Version: 2015.09.22
*/
var EIReaderTools =
{
version: "2015.09.22",
options:
{
fullscreen:
{
icon:
{
on: "iVBORw0KGgoAAAANSUhEUgAAACAAAAAgBAMAAACBVGfHAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAABVQTFRFAAAAAAAAAAAAAAAAAAAAAAAAAAAAEgEApAAAAAZ0Uk5TAA+fz9/vpTOW9gAAAJNJREFUKM/NkkEKwyAUBSeeQOoFDAX3duERcoQcQJN3/yN08RVLUui2fyE4Ph/IyPLiY56eoDj3TpmiOkFSI2lEPE6qtgSA3A+TtBaA9pAq4KRTANr77SQZUO93E1j9sg9wegDCTGQAygTN80fz+3FsAxx8T1w7nHQY2CySpJgAqpMq3QVANA3mwo/6eld5k339Dm89PDdxiEGVaQAAAABJRU5ErkJggg==",
off: "iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAI1JREFUeNrsV0EOwCAI6w/8/6lP8gk8ZTuPLUoirltCEy8mpY0iIAAcg0WsgxONreIRE4+bhnzYr05AkgO2wQQHV3wxwAmBieJe6xY8w0Q0BlcDZHOngXqA1zPziE68BTjNmWDGM4qKexNEoVAoJPVzWSGSlmJpM3q1HcsHEvlI9omhtL5m9TWT5cApwAD/IigEZttSgAAAAABJRU5ErkJggg=="
}
},
// backup of the original functions
savedFunctions:
{
"ResizeViewer":
{
"orig": ResizeViewer,
"mod": function()
{
var b = Math.max(400, document.documentElement.clientHeight), a = Math.round(pw * b / ph);
ResizeMenu();
if (zoom) {
a = Math.max(1000, Math.min(1502, document.documentElement.clientWidth));
$("#bvdPage div.pages").width(a).height(b).children(".panviewport").width(a).height(b)
}
if ((zoom || crop != null)) {
return;
}
RszImgs(a, b)
},
},
"CancelZoom":
{
"orig": CancelZoom,
"mod": function(e)
{
var $this = $(this).find("img");
$("#bvdMenu").show();
$this.css("transform", "");
EIReaderTools.options.savedFunctions.CancelZoom.orig();
},
},
"PageChange":
{
"orig": PageChange,
"mod": function()
{
CancelCut();
Tag();
TagAd();
if (!smode)
$(window).trigger("resize");
if (player)
$("#jqp").jPlayer("play");
// custom event: before sending request
$(document).trigger("endPageChanged.eirt");
$("#ctl00_cph_viewer1_bvdPages").append($("<div>").addClass("eirt-loading"));
}
}
},
},
// highlight current page on the side menu
highlightSidePage: function()
{
var currPage = GetPage().p;
var $container = $('#bvdMenuInner');
var $imgs = $("#bvdMenuImg img").removeClass("eirt-highlight");
var $el = $imgs.eq(currPage);
$el.addClass("eirt-highlight");
$imgs.eq(currPage + ((currPage % 2 == 0) ? 1 : -1)).addClass("eirt-highlight");
$container.stop().animate(
{
scrollTop: parseInt(currPage / 2) * 98
}, 1000);
},
// activates custom loading for the page "start ajax request"
requestPageLoading: function(e)
{
e = e || {type: "click", target: $(".crn.topright")[0]};
var containers = $("#bvNav td.bvNavLinkSec, #bvdMenuImg img, .crn.topleft, .crn.topright");
var loading = $(".eirt-loading");
if ((e.type === "click"
&& containers.find(e.target).length
|| containers.is(e.target))
&& loading.css("display") === "none")
{
loading.css("display", "block");
$(document).one("endPageChanged.eirt", function()
{
$(".eirt-loading").remove();
});
}
},
// "jump to page" init method
initPagination: function()
{
var el = $("<div>", {id: "eirt-pag-cont"})
.css(
{
"display": "inline-block",
"color": "#000",
"position": "relative",
"padding": "0 2px",
"border-left": "1px solid hsl(0, 0%, 70%)",
"border-right": "1px solid hsl(0, 0%, 70%)",
})
.insertAfter($("#eirt-state"));
// jump to page event handler
var goToPage = function(e)
{
e.stopPropagation();
e.preventDefault();
if (e.type !== "click")
{
var keycode = (e.keyCode ? e.keyCode : e.which);
if (keycode !== 13 || e.type !== "keyup")
return false;
}
var $this = $(this);
var input = $this;
if ($this.siblings('#eirt-pag-input').length > 0)
input = $this.siblings('#eirt-pag-input');
var inputVal = input.val();
var imgs = $("#bvdMenuImg img[onclick]");
input.val("");
var specialPages =
{
"i": 2,
"inicio": 0,
"fim": imgs.length - 1
};
var specialPagesEN =
{
"i": 2,
"begin": 0,
"end": imgs.length - 1
};
if (specialPages[inputVal] != null || specialPagesEN[inputVal] != null)
{
$(imgs[specialPages[inputVal] || specialPagesEN[inputVal]]).click();
return;
}
var num = parseInt(inputVal);
if (num == null || num === NaN)
return;
var numLimited = Math.max(Math.min(num, imgs.length), 0);
$(imgs[numLimited]).click();
EIReaderTools.requestPageLoading();
};
// shortcut to focus the input box, event handler
var focusInput = function(e)
{
if (e.which == 80 && e.altKey)
$("#eirt-pag-input").focus();
};
var focusOutInput = function() // input got focus
{
$(this).on("keyup.esc.eirt", function(e) // wait for esc key to unfocus
{
var $this = $(this);
if (e.which === 27 && $this.is(":focus"))
{
document.getElementByTagName().blur();
return false;
}
}).one("blur.esc.eirt", function() // on focus out, remove above event
{
$(this).off("keyup.esc.eirt");
});
};
$("<input>",
{
id: "eirt-pag-input",
type: "text",
placeholder: "Página"
})
.css(
{
"width": "40px",
"height": "14px",
"margin": "0px 3px 0 5px",
})
.appendTo(el);
$(document)
.on("keyup.eirt", focusInput)
.on("keyup.eirt", "#eirt-pag-input", goToPage)
.on("focus.eirt", "#eirt-pag-input", focusOutInput)
.on("click.eirt", "#eirt-pag-acc", goToPage);
},
// "previous (left arrow) and next (right arrow) page" init method
initNavigation: function()
{
$(document).on("keyup.eirt", function(e)
{
var imgs = $("#bvdMenuImg img[onclick]");
var $handlerL = $(".page.fleft");
var $handlerR = $(".page.fright");
// right arrow
if (e.which === 39)
{
if ($handlerR.length === 0)
$handlerR = $(".page:visible");
var src = $handlerR.attr("src");
var page = +src.split("/")[3].replace("f", "");
if (page < imgs.length - 1)
$(".crn.topright").click();
}
// left arrow
else if (e.which === 37)
{
if ($handlerL.length === 0)
$handlerL = $(".page:visible");
var src = $handlerL.attr("src");
var page = +src.split("/")[3].replace("f", "");
if (page > 1)
$(".crn.topleft").click();
}
else
return;
EIReaderTools.requestPageLoading();
});
},
// overriding the default mousewheel action to
// zoom in and out the page
initZoom: function()
{
var mathLimit = function(value, min, max)
{
return Math.max(Math.min(value, max), min);
};
// catch mousewheel action event handler
$(document).on("mousewheel.eirt", ".panviewport", function(e)
{
//$(this).css("overflow", "hidden");
var $this = $(this).find("img:visible");
var deltaY = -e.originalEvent.deltaY;
/*
var imgW = $this.width();
var imgH = $this.height();
var mouseX = mathLimit(e.clientX - $this.offset().left, 0, imgW);
var mouseY = mathLimit(e.clientY - $this.offset().top, 0, imgH);
var pointX = Math.abs(mouseX * 100 / imgW);
var pointY = Math.abs(mouseY * 100 / imgH);
*/
var scaleValue = $this.data("scale") || 1;
var scale = mathLimit((deltaY / 1000) + scaleValue, 0.1, 1);
var transform = "scale("+scale+")".replace("@par", scale);
/*
var transformOrigin = mathLimit(pointX, 0, 100)+"% "+mathLimit(pointY, 0, 100)+"%";
*/
// save scale value and apply transform to .panviewport
$this.data("scale", scale)
.css(
{
"transform": transform,
//"transform-origin": transformOrigin,
});
e.preventDefault();
});
},
// inits the fullscreen functionality
initFullscreen: function()
{
var icons = EIReaderTools.options.fullscreen.icon;
// fullscreen icon click, event handler
// toggles fullscreen on and off
var toggleFullscreen = function(e)
{
var $this = $(this);
// redefine $this when using ESC key
if (e.type === "keyup")
$this = $("#eirt-fs");
// keyup event triggered but not ESC key
if ((e.which !== 27 && e.type === "keyup")
|| e.which === 27 && !$this.data("mode"))
return;
e.data = e.data || {};
e.data.modeOverride = (e.data) ? e.data.modeOverride : null;
var mode = (e.data.modeOverride != null) ? e.data.modeOverride : !$this.data("mode");
var bgcolor = 'url("data:image/png;base64,'+icons.off+'")';
var fsLeft = "50%";
var fsTransform = "-219%";
var fsTop = "6px";
var func = "orig";
var holder = $("#zahirad192");
$("#bvdPage_css").remove();
// turn on fullscreen else the default is turn off
if (mode)
{
$("<style>", {id: "bvdPage_css"}).html(
"#bvdPage\
{\
position: absolute;\
top: 0;\
right: 0;\
left: 0;\
bottom: 0;\
margin: 0;\
z-index: 1000;\
background-color: #F5F5F5;\
}").appendTo($("head"));
func = "mod"
bgcolor = 'url("data:image/png;base64,'+icons.on+'")';
holder = $("body");
fsPosition = "absolute";
fsTop = "0";
fsLeft = "0";
fsTransform = "0%";
}
var style = $("#eirt-container")
.css(
{
"left": fsLeft,
"top": fsTop,
})
.attr("style");
$("#eirt-container").attr("style", style+";transform: translateX("+fsTransform+");");
$this.css("background-image", bgcolor);
$this.data("mode", mode);
ResizeViewer = EIReaderTools.options.savedFunctions["ResizeViewer"][func];
$(window).trigger("resize");
};
// fullscreen ui element
var $fs = $("<div>", {id: "eirt-fs"})
.css(
{
"display": "inline-block",
"width": "15px",
"height": "15px",
"background-image": 'url("data:image/png;base64,'+icons.off+'")',
"margin-left": "5px",
"position": "relative",
"top": "3px",
"background-size": "100%",
})
.data("mode", false)
.click(toggleFullscreen)
.insertAfter($("#eirt-pag-cont"));
$(document).on("keyup.fullscreen.eirt", {modeOverride: false}, toggleFullscreen);
},
// entry point for the tool
init: function()
{
// remove EIRT if already exists
EIReaderTools.reset();
// watch for specific elements clicks to trigger custom loading
$(document).on("click.eirt.requestStarted", EIReaderTools.requestPageLoading);
// ui container
$("<div>", {id: "eirt-container"})
.css(
{
"position": "absolute",
"left": "50%",
"transform": "translateX(-219%)",
"top": "5px",
"z-index": "1010",
"background": "#fff",
"padding": "4px",
"border-right": "1px solid hsl(0, 0%, 70%)",
"border-bottom": "1px solid hsl(0, 0%, 70%)",
})
.appendTo($("body"));
// tool loading state and its name
$("<span>", {id: "eirt-state"})
.text("EIReaderTools")
.css(
{
"color": "#f00",
"margin-right": "5px",
"position": "relative",
"top": "-4px",
}).appendTo($("#eirt-container"));
//
$("head").append($("<style>", {id: "eirt-style"})
.text(
'#eirt-state:after{'+
'content: "v'+EIReaderTools.version+'";'+
'color: black;'+
'font-size: 8px;'+
'position: absolute;'+
'left: 0;'+
'bottom: -8px;'+
'margin-left: 32px;}'+
'#bvdMenuImg img.eirt-highlight{'+
'border: 4px solid rgb(0, 173, 239);'+
'margin: 0;}'+
// http://stephanwagner.me/only-css-loading-spinner
'@keyframes loading{'+
'to {transform: rotate(360deg);}}'+
'@-webkit-keyframes loading{'+
'to {-webkit-transform: rotate(360deg);}}'+
'.eirt-loading{'+
'display: none;'+
'min-width: 100%;'+
'min-height: 100%;'+
'position: absolute;'+
'background-color: hsla(100, 0%, 0%, 0.5);}'+
'.eirt-loading:before{'+
'content: "A carregar...";'+
'position: absolute;'+
'top: 50%;'+
'left: 50%;'+
'width: 150px;'+
'height: 150px;'+
'margin-top: -75px;'+
'margin-left: -75px;}'+
'.eirt-loading:not(:required):before{'+
'content: "";'+
'border-radius: 50%;'+
'border-top: 10px solid #03ade0;'+
'border-right: 0px solid transparent;'+
'animation: loading 1s linear infinite;'+
'-webkit-animation: loading 1s linear infinite;}'
)
);
$("#ctl00_cph_viewer1_bvdPages").append($("<div>").addClass("eirt-loading"));
$(document).on("endPageChanged.eirt", EIReaderTools.highlightSidePage);
// apply a moded function
CancelZoom = EIReaderTools.options.savedFunctions.CancelZoom.mod;
PageChange = EIReaderTools.options.savedFunctions.PageChange.mod;
EIReaderTools.initPagination();
EIReaderTools.initNavigation();
EIReaderTools.initFullscreen();
EIReaderTools.initZoom();
EIReaderTools.highlightSidePage();
// load success, change color of #eirt-state
$("#eirt-state").css("color", "#32CD32");
},
// remove the EIRT
reset: function()
{
// remove the ui elements
$("#eirt-container,#eirt-style").remove();
$("#bvdMenuImg img").removeClass("eirt-highlight")
// set the original functions in place
ResizeViewer = EIReaderTools.options.savedFunctions.ResizeViewer.orig;
CancelZoom = EIReaderTools.options.savedFunctions.CancelZoom.orig;
PageChange = EIReaderTools.options.savedFunctions.PageChange.orig;
// unbind events
$(document)
.off("click.eirt.requestStarted")
.off("click.eirt")
.off("dblclick.eirt")
.off("mousewheel.eirt")
.off("keyup.eirt")
.off("keyup.fullscreen.eirt")
.off("endPageChanged.eirt");
},
}
$(document).ready(EIReaderTools.init);