/
skin.js
787 lines (685 loc) · 19.9 KB
/
skin.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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
/**
* Keeps track of whether or not the overlay is activated.
*
* @type {Boolean}
* @private
*/
var overlayOn = false,
/**
* A cache of elements that are troublesome for modal overlays.
*
* @type {Array}
* @private
*/
visibilityCache = [],
/**
* Id's of elements that need transparent PNG support.
*
* @type {Array}
* @private
*/
pngIds = [
"sb-nav-close",
"sb-nav-next",
"sb-nav-play",
"sb-nav-pause",
"sb-nav-previous"
],
/**
* The container element.
*
* @type {HTMLElement}
* @private
*/
container,
/**
* The overlay element.
*
* @type {HTMLElement}
* @private
*/
overlay,
/**
* The wrapper element.
*
* @type {HTMLElement}
* @private
*/
wrapper,
/**
* True if the window resize event is allowed to fire.
*
* @type {Boolean}
* @private
*/
doWindowResize = true;
/**
* Animates the given property of el to the given value over a specified duration. If a
* callback is provided, it will be called when the animation is finished.
*
* @param {HTMLElement} el
* @param {String} property
* @param {mixed} to
* @param {Number} duration
* @param {Function} callback
* @private
*/
function animate(el, property, to, duration, callback) {
var isOpacity = (property == "opacity"),
anim = isOpacity ? S.setOpacity : function(el, value) {
// default unit is px for properties other than opacity
el.style[property] = "" +
value + "px";
};
if (duration == 0 || (!isOpacity && !S.options.animate) || (isOpacity && !S.options.animateFade)) {
anim(el, to);
if (callback)
callback();
return;
}
var from = parseFloat(S.getStyle(el, property)) || 0;
var delta = to - from;
if (delta == 0) {
if (callback)
callback();
return; // nothing to animate
}
duration *= 1000; // convert to milliseconds
var begin = now(),
ease = S.ease,
end = begin + duration,
time;
var interval = setInterval(function() {
time = now();
if (time >= end) {
clearInterval(interval);
interval = null;
anim(el, to);
if (callback)
callback();
} else {
anim(el, from + ease((time - begin) / duration) * delta);
}
}, 10); // 10 ms interval is minimum on WebKit
}
/**
* Sets the size of the container element.
*
* @private
*/
function setSize() {
container.style.height = S.getWindowSize("Height") + "px";
container.style.width = S.getWindowSize("Width") + "px";
}
/**
* Sets the top of the container element. This is only necessary in browsers that
* don't support fixed positioning, such as IE6.
*
* @private
*/
function setPosition() {
container.style.top = document.documentElement.scrollTop + "px";
container.style.left = document.documentElement.scrollLeft + "px";
}
/**
* Toggles the visibility of elements that are troublesome for overlays.
*
* @param {Boolean} on True to make visible, false to hide
* @private
*/
function toggleTroubleElements(on) {
if (on) {
each(visibilityCache, function(i, el){
el[0].style.visibility = el[1] || '';
});
} else {
visibilityCache = [];
each(S.options.troubleElements, function(i, tag) {
each(document.getElementsByTagName(tag), function(j, el) {
visibilityCache.push([el, el.style.visibility]);
el.style.visibility = "hidden";
});
});
}
}
/**
* Toggles the display of the nav control with the given id.
*
* @param {String} id The id of the navigation control
* @param {Boolean} on True to toggle on, false to toggle off
* @private
*/
function toggleNav(id, on) {
var el = get("sb-nav-" + id);
if (el)
el.style.display = on ? "" : "none";
}
/**
* Toggles the visibility of the loading layer.
*
* @param {Boolean} on True to toggle on, false to toggle off
* @param {Function} callback The callback to use when finished
* @private
*/
function toggleLoading(on, callback) {
var loading = get("sb-loading"),
playerName = S.getCurrent().player,
anim = (playerName == "img" || playerName == "html"); // fade on images & html
if (on) {
S.setOpacity(loading, 0);
loading.style.display = "block";
var wrapped = function() {
S.clearOpacity(loading);
if (callback)
callback();
}
if (anim) {
animate(loading, "opacity", 1, S.options.fadeDuration, wrapped);
} else {
wrapped();
}
} else {
var wrapped = function() {
loading.style.display = "none";
S.clearOpacity(loading);
if (callback)
callback();
}
if (anim) {
animate(loading, "opacity", 0, S.options.fadeDuration, wrapped);
} else {
wrapped();
}
}
}
/**
* Builds the content for the title and information bars.
*
* @param {Function} callback The callback to use when finished
* @private
*/
function buildBars(callback) {
var obj = S.getCurrent();
get("sb-title-inner").innerHTML = obj.title || "";
// build the nav
var close, next, play, pause, previous;
if (S.options.displayNav) {
close = true;
var len = S.gallery.length;
if (len > 1) {
if (S.options.continuous) {
next = previous = true;
} else {
next = (len - 1) > S.current; // not last in gallery, show next
previous = S.current > 0; // not first in gallery, show previous
}
}
// in a slideshow?
if (S.options.slideshowDelay > 0 && S.hasNext()) {
pause = !S.isPaused();
play = !pause;
}
} else {
close = next = play = pause = previous = false;
}
toggleNav("close", close);
toggleNav("next", next);
toggleNav("play", play);
toggleNav("pause", pause);
toggleNav("previous", previous);
// build the counter
var counter = "";
if (S.options.displayCounter && S.gallery.length > 1) {
var len = S.gallery.length;
if (S.options.counterType == "skip") {
// limit the counter?
var i = 0,
end = len,
limit = parseInt(S.options.counterLimit) || 0;
if (limit < len && limit > 2) { // support large galleries
var h = Math.floor(limit / 2);
i = S.current - h;
if (i < 0)
i += len;
end = S.current + (limit - h);
if (end > len)
end -= len;
}
while (i != end) {
if (i == len)
i = 0;
counter += '<a onclick="Shadowbox.change(' + i + ');"'
if (i == S.current)
counter += ' class="sb-counter-current"';
counter += ">" + (++i) + "</a>";
}
} else {
counter = [S.current + 1, S.lang.of, len].join(' ');
}
}
get("sb-counter").innerHTML = counter;
callback();
}
/**
* Shows the title and info bars.
*
* @param {Function} callback The callback to use when finished
* @private
*/
function showBars(callback) {
var titleInner = get("sb-title-inner"),
infoInner = get("sb-info-inner"),
duration = 0.35;
// clear visibility before animating into view
titleInner.style.visibility = infoInner.style.visibility = "";
if (titleInner.innerHTML != "")
animate(titleInner, "marginTop", 0, duration);
animate(infoInner, "marginTop", 0, duration, callback);
}
/**
* Hides the title and info bars.
*
* @param {Boolean} anim True to animate the transition
* @param {Function} callback The callback to use when finished
* @private
*/
function hideBars(anim, callback) {
var title = get("sb-title"),
info = get("sb-info"),
titleHeight = title.offsetHeight,
infoHeight = info.offsetHeight,
titleInner = get("sb-title-inner"),
infoInner = get("sb-info-inner"),
duration = (anim ? 0.35 : 0);
animate(titleInner, "marginTop", titleHeight, duration);
animate(infoInner, "marginTop", infoHeight * -1, duration, function() {
titleInner.style.visibility = infoInner.style.visibility = "hidden";
callback();
});
}
/**
* Adjusts the height of #sb-wrapper-inner and centers #sb-wrapper vertically
* in the viewport.
*
* @param {Number} height The height (in pixels)
* @param {Number} top The top (in pixels)
* @param {Boolean} anim True to animate the transition
* @param {Function} callback The callback to use when finished
* @private
*/
function adjustHeight(height, top, anim, callback) {
var wrapperInner = get("sb-wrapper-inner"),
duration = (anim ? S.options.resizeDuration : 0);
animate(wrapper, "top", top, duration);
animate(wrapperInner, "height", height, duration, callback);
}
/**
* Adjusts the width and left position of #sb-wrapper.
*
* @param {Number} width The width (in pixels)
* @param {Number} left The left (in pixels)
* @param {Boolean} anim True to animate the transition
* @param {Function} callback The callback to use when finished
* @private
*/
function adjustWidth(width, left, anim, callback) {
var duration = (anim ? S.options.resizeDuration : 0);
animate(wrapper, "left", left, duration);
animate(wrapper, "width", width, duration, callback);
}
/**
* Calculates the dimensions for Shadowbox.
*
* @param {Number} height The content height
* @param {Number} width The content width
* @return {Object} The new dimensions object
* @private
*/
function setDimensions(height, width) {
var bodyInner = get("sb-body-inner"),
height = parseInt(height),
width = parseInt(width),
topBottom = wrapper.offsetHeight - bodyInner.offsetHeight,
leftRight = wrapper.offsetWidth - bodyInner.offsetWidth,
// overlay should provide proper window dimensions here
maxHeight = overlay.offsetHeight,
maxWidth = overlay.offsetWidth,
// default to the default viewport padding
padding = parseInt(S.options.viewportPadding) || 20,
// only preserve aspect ratio if there is something to display and
// it's not draggable
preserveAspect = (S.player && S.options.handleOversize != "drag");
return S.setDimensions(height, width, maxHeight, maxWidth, topBottom, leftRight, padding, preserveAspect);
}
/**
* The Shadowbox.skin object.
*
* @type {Object}
* @public
*/
var K = {};
/**
* The HTML markup to use.
*
* @type {String}
* @public
*/
K.markup = "" +
'<div id="sb-container">' +
'<div id="sb-overlay"></div>' +
'<div id="sb-wrapper">' +
'<div id="sb-title">' +
'<div id="sb-title-inner"></div>' +
'</div>' +
'<div id="sb-wrapper-inner">' +
'<div id="sb-body">' +
'<div id="sb-body-inner"></div>' +
'<div id="sb-loading">' +
'<div id="sb-loading-inner"><span>{loading}</span></div>' +
'</div>' +
'</div>' +
'</div>' +
'<div id="sb-info">' +
'<div id="sb-info-inner">' +
'<div id="sb-counter"></div>' +
'<div id="sb-nav">' +
'<a id="sb-nav-close" title="{close}" onclick="Shadowbox.close()"></a>' +
'<a id="sb-nav-next" title="{next}" onclick="Shadowbox.next()"></a>' +
'<a id="sb-nav-play" title="{play}" onclick="Shadowbox.play()"></a>' +
'<a id="sb-nav-pause" title="{pause}" onclick="Shadowbox.pause()"></a>' +
'<a id="sb-nav-previous" title="{previous}" onclick="Shadowbox.previous()"></a>' +
'</div>' +
'</div>' +
'</div>' +
'</div>' +
'</div>';
/**
* Various options that control the behavior of Shadowbox' skin.
*
* @type {Object}
* @public
*/
K.options = {
/**
* The sequence of the resizing animations. "hw" will resize height, then width. "wh" resizes
* width, then height. "sync" resizes both simultaneously.
*
* @type {String}
*/
animSequence: "sync",
/**
* The limit to the number of counter links that are displayed in a "skip"-style counter.
*
* @type {Number}
*/
counterLimit: 10,
/**
* The counter type to use. May be either "default" or "skip". A skip counter displays a
* link for each object in the gallery.
*
* @type {String}
*/
counterType: "default",
/**
* True to display the gallery counter.
*
* @type {Boolean}
*/
displayCounter: true,
/**
* True to show the navigation controls.
*
* @type {Boolean}
*/
displayNav: true,
/**
* The duration (in seconds) of opacity animations.
*
* @type {Number}
*/
fadeDuration: 0.35,
/**
* The initial height (in pixels).
*
* @type {Number}
*/
initialHeight: 160,
/**
* The initial width (in pixels).
*
* @type {Number}
*/
initialWidth: 320,
/**
* True to trigger Shadowbox.close when the overlay is clicked.
*
* @type {Boolean}
*/
modal: false,
/**
* The color (in hex) to use for the overlay.
*
* @type {String}
*/
overlayColor: "#000",
/**
* The opacity to use for the overlay.
*
* @type {Number}
*/
overlayOpacity: 0.5,
/**
* The duration (in seconds) to use for resizing animations.
*
* @type {Number}
*/
resizeDuration: 0.35,
/**
* True to show the overlay, false to hide it.
*
* @type {Boolean}
*/
showOverlay: true,
/**
* Names of elements that should be hidden when the overlay is enabled.
*
* @type {String}
*/
troubleElements: ["select", "object", "embed", "canvas"]
};
/**
* Initialization function. Called immediately after this skin's markup has been
* appended to the document with all of the necessary language replacements done.
*
* @public
*/
K.init = function() {
S.appendHTML(document.body, sprintf(K.markup, S.lang));
K.body = get("sb-body-inner");
// cache oft-used elements
container = get("sb-container");
overlay = get("sb-overlay");
wrapper = get("sb-wrapper");
// use absolute positioning in browsers that don't support fixed
if (!supportsFixed)
container.style.position = "absolute";
if (!supportsOpacity) {
// support transparent PNG's via AlphaImageLoader
var el, m, re = /url\("(.*\.png)"\)/;
each(pngIds, function(i, id) {
el = get(id);
if (el) {
m = S.getStyle(el, "backgroundImage").match(re);
if (m) {
el.style.backgroundImage = "none";
el.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true,src=" +
m[1] + ",sizingMethod=scale);";
}
}
});
}
// add window resize event handler, use 10 ms buffer to prevent jerky resizing
var timer;
addEvent(window, "resize", function() {
if (timer) {
clearTimeout(timer);
timer = null;
}
if (open)
timer = setTimeout(K.onWindowResize, 10);
});
}
/**
* Called when Shadowbox opens.
*
* @param {Object} obj The object to open
* @param {Function} callback The callback to use when finished
* @public
*/
K.onOpen = function(obj, callback) {
// prevent window resize events from firing until we're finished
doWindowResize = false;
container.style.display = "block";
setSize();
var dims = setDimensions(S.options.initialHeight, S.options.initialWidth);
adjustHeight(dims.innerHeight, dims.top);
adjustWidth(dims.width, dims.left);
if (S.options.showOverlay) {
overlay.style.backgroundColor = S.options.overlayColor;
S.setOpacity(overlay, 0);
if (!S.options.modal)
addEvent(overlay, "click", S.close);
overlayOn = true;
}
if (!supportsFixed) {
setPosition();
addEvent(window, "scroll", setPosition);
}
toggleTroubleElements();
container.style.visibility = "visible";
if (overlayOn) {
animate(overlay, "opacity", S.options.overlayOpacity, S.options.fadeDuration, callback);
} else {
callback();
}
}
/**
* Called when a new object is being loaded.
*
* @param {Boolean} changing True if the content is changing from some
* previous object
* @param {Function} callback The callback to use when finished
* @public
*/
K.onLoad = function(changing, callback) {
toggleLoading(true);
// make sure the body doesn't have any children
while (K.body.firstChild)
remove(K.body.firstChild);
hideBars(changing, function() {
if (!open)
return;
if (!changing)
wrapper.style.visibility = "visible";
buildBars(callback);
});
}
/**
* Called when the content is ready to be loaded (e.g. when the image has finished
* loading). Should resize the content box and make any other necessary adjustments.
*
* @param {Function} callback The callback to use when finished
* @public
*/
K.onReady = function(callback) {
if (!open)
return;
var player = S.player,
dims = setDimensions(player.height, player.width);
var wrapped = function() {
showBars(callback);
}
switch (S.options.animSequence) {
case "hw":
adjustHeight(dims.innerHeight, dims.top, true, function() {
adjustWidth(dims.width, dims.left, true, wrapped);
});
break;
case "wh":
adjustWidth(dims.width, dims.left, true, function() {
adjustHeight(dims.innerHeight, dims.top, true, wrapped);
});
break;
default: // sync
adjustWidth(dims.width, dims.left, true);
adjustHeight(dims.innerHeight, dims.top, true, wrapped);
}
}
/**
* Called when the content is loaded into the box and is ready to be displayed.
*
* @param {Function} callback The callback to use when finished
* @public
*/
K.onShow = function(callback) {
toggleLoading(false, callback);
// re-enable window resize events
doWindowResize = true;
}
/**
* Called in Shadowbox.close.
*
* @public
*/
K.onClose = function() {
if (!supportsFixed)
removeEvent(window, "scroll", setPosition);
removeEvent(overlay, "click", S.close);
wrapper.style.visibility = "hidden";
var callback = function() {
container.style.visibility = "hidden";
container.style.display = "none";
toggleTroubleElements(true);
}
if (overlayOn) {
animate(overlay, "opacity", 0, S.options.fadeDuration, callback);
} else {
callback();
}
}
/**
* Called in Shadowbox.play.
*
* @public
*/
K.onPlay = function() {
toggleNav("play", false);
toggleNav("pause", true);
}
/**
* Called in Shadowbox.pause.
*
* @public
*/
K.onPause = function() {
toggleNav("pause", false);
toggleNav("play", true);
}
/**
* Called when the window is resized.
*
* @public
*/
K.onWindowResize = function() {
if (!doWindowResize)
return;
setSize();
var player = S.player,
dims = setDimensions(player.height, player.width);
// adjust width first to eliminate horizontal scroll bar
adjustWidth(dims.width, dims.left);
adjustHeight(dims.innerHeight, dims.top);
if (player.onWindowResize)
player.onWindowResize();
}
S.skin = K;