/
core.js
1358 lines (1175 loc) · 32.5 KB
/
core.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
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* The Shadowbox object.
*
* @type {Object}
* @public
*/
var S = {
/**
* The current version of Shadowbox.
*
* @type {String}
* @public
*/
version: "3.0.3"
}
Array.prototype.contains = Array.prototype.contains || function (obj) {
for (var i = 0; i < this.length; ++i) {
if (this[i] === obj) {
return true;
}
}
return false;
}
var galleryName = /^(light|shadow)box\[(.*?)\]/i,
inlineParam = /\s*([a-z_]*?)\s*=\s*(.+)\s*/,
fileExtension = /\.([0-9a-z]+)(?:[\?#]|$)/i,
scriptPath = /(.+\/)shadowbox\.js/i,
root = document.documentElement,
/**
* True if Shadowbox is currently open, false otherwise.
*
* @type {Boolean}
* @private
*/
open = false,
/**
* True if Shadowbox has been initialized, false otherwise.
*
* @type {Boolean}
* @private
*/
initialized = false,
/**
* The previous set of options that were used before Shadowbox.applyOptions
* was called.
*
* @type {Object}
* @private
*/
lastOptions = {},
/**
* The delay in milliseconds that the current gallery uses.
*
* @type {Number}
* @private
*/
slideDelay = 0,
/**
* The time at which the current slideshow frame appeared.
*
* @type {Number}
* @private
*/
slideStart,
/**
* The timeout id for the slideshow transition function.
*
* @type {Number}
* @private
*/
slideTimer,
/**
* True if this browser supports opacity.
*
* @type {Boolean}
* @private
*/
supportsOpacity = "opacity" in root.style && typeof root.style.opacity === "string",
/**
* True if the browser supports fixed positioning.
*
* @type {Boolean}
* @private
*/
supportsFixed = false;
(function () {
var div = document.createElement("div");
div.style.position = "fixed";
div.style.margin = 0;
div.style.top = "20px";
root.appendChild(div, root.firstChild);
supportsFixed = div.offsetTop == 20;
root.removeChild(div);
})();
/**
* The index of the current object in the gallery array.
*
* @type {Number}
* @public
*/
S.current = -1;
/**
* The current dimensions of Shadowbox.
*
* @type {Object}
* @public
*/
S.dimensions = null;
/**
* Easing function used for animations. Based on a cubic polynomial.
*
* @param {Number} state The state of the animation (% complete)
* @return {Number} The adjusted easing value
* @public
*/
S.ease = function(state) {
return 1 + Math.pow(state - 1, 3);
}
/**
* An object containing names of plugins and links to their respective download pages.
*
* @type {Object}
* @public
*/
S.errorInfo = {
fla: {
name: "Flash",
url: "http://www.adobe.com/products/flashplayer/"
},
qt: {
name: "QuickTime",
url: "http://www.apple.com/quicktime/download/"
}
};
/**
* The content objects in the current set.
*
* @type {Array}
* @public
*/
S.gallery = [];
/**
* A function that will be called as soon as the DOM is ready.
*
* @type {Function}
* @public
*/
S.onReady = noop;
/**
* The URL path to the Shadowbox script.
*
* @type {String}
* @public
*/
S.path = null;
/**
* The current player object.
*
* @type {Object}
* @public
*/
S.player = null;
/**
* The id to use for the Shadowbox player element.
*
* @type {String}
* @public
*/
S.playerId = "sb-player";
/**
* Various options that control Shadowbox' behavior.
*
* @type {Object}
* @public
*/
S.options = {
/**
* True to enable animations.
*
* @type {Boolean}
*/
animate: true,
/**
* True to enable opacity animations.
*
* @type {Boolean}
*/
animateFade: true,
/**
* True to automatically play movies when the load.
*
* @type {Boolean}
*/
autoplayMovies: true,
/**
* True to enable the user to skip to the first item in a gallery from the last using
* next.
*
* @type {Boolean}
*/
continuous: false,
/**
* True to enable keyboard navigation.
*
* @type {Boolean}
*/
enableKeys: true,
/**
* Parameters to pass to flash <object>'s.
*
* @type {Object}
*/
flashParams: {
bgcolor: "#000000",
allowfullscreen: true
},
/**
* Variables to pass to flash <object>'s.
*
* @type {Object}
*/
flashVars: {},
/**
* The minimum required Flash version.
*
* Note: The default is 9.0.115. This is the minimum version suggested by
* the JW FLV player.
*
* @type {String}
*/
flashVersion: "9.0.115",
/**
* Determines how oversized content is handled. If set to "resize" the
* content will be resized while preserving aspect ratio. If "drag" will display
* the image at its original resolution but it will be draggable. If "none" will
* display the content at its original resolution but it may be cropped.
*
* @type {String}
*/
handleOversize: "resize",
/**
* Determines how unsupported content is handled. If set to "remove" will
* remove the content from the gallery. If "link" will display a helpful
* link to a page where the necessary browser plugin can be installed.
*
* @type {String}
*/
handleUnsupported: "link",
/**
* A hook function to be fired when changing from one gallery item to the
* next. Is passed the item that is about to be displayed as its only argument.
*
* @type {Function}
*/
onChange: noop,
/**
* A hook function to be fired when closing. Is passed the most recent item
* as its only argument.
*
* @type {Function}
*/
onClose: noop,
/**
* A hook funciton to be fires when content is finished loading. Is passed the
* current gallery item as its only argument.
*
* @type {Function}
*/
onFinish: noop,
/**
* A hook function to be fired when opening. Is passed the current gallery item
* as its only argument.
*
* @type {Function}
*/
onOpen: noop,
/**
* True to enable movie controls on movie players.
*
* @type {Boolean}
*/
showMovieControls: true,
/**
* True to skip calling setup during init.
*
* @type {Boolean}
*/
skipSetup: false,
/**
* The delay (in seconds) to use when displaying a gallery in slideshow mode. Setting
* this option to any value other than 0 will trigger slideshow mode.
*
* @type {Number}
*/
slideshowDelay: 0,
/**
* The ammount of padding (in pixels) to maintain around the edge of the viewport at all
* times.
*
* @type {Number}
*/
viewportPadding: 20
};
/**
* Gets the object that is currently being displayed.
*
* @return {Object}
* @public
*/
S.getCurrent = function() {
return S.current > -1 ? S.gallery[S.current] : null;
}
/**
* Returns true if there is another object to display after the current.
*
* @return {Boolean}
* @public
*/
S.hasNext = function() {
return S.gallery.length > 1 && (S.current != S.gallery.length - 1 || S.options.continuous);
}
/**
* Returns true if Shadowbox is currently open.
*
* @return {Boolean}
* @public
*/
S.isOpen = function() {
return open;
}
/**
* Returns true if Shadowbox is currently paused.
*
* @return {Boolean}
* @public
*/
S.isPaused = function() {
return slideTimer == "pause";
}
/**
* Applies the given set of options to Shadowbox' options. May be undone with revertOptions().
*
* @param {Object} options
* @public
*/
S.applyOptions = function(options) {
lastOptions = apply({}, S.options);
apply(S.options, options);
}
/**
* Reverts to whatever the options were before applyOptions() was called.
*
* @public
*/
S.revertOptions = function() {
apply(S.options, lastOptions);
}
/**
* Initializes the Shadowbox environment. If options are given here, they
* will override the defaults. A callback may be provided that will be called
* when the document is ready. This function can be used for setting up links
* using Shadowbox.setup.
*
* @param {Object} options
* @param {Function} callback
* @public
*/
S.init = function(options, callback) {
if (initialized)
return;
initialized = true;
if (S.skin.options)
apply(S.options, S.skin.options);
if (options)
apply(S.options, options);
if (!S.path) {
// determine script path automatically
var path, scripts = document.getElementsByTagName("script");
for (var i = 0, len = scripts.length; i < len; ++i) {
path = scriptPath.exec(scripts[i].src);
if (path) {
S.path = path[1];
break;
}
}
}
if (callback)
S.onReady = callback;
bindLoad();
}
/**
* Opens the given object in Shadowbox. This object may be any of the following:
*
* - A URL specifying the location of some content to display
* - An HTML link object (A or AREA tag) that links to some content
* - A custom object similar to one produced by Shadowbox.makeObject
* - An array of any of the above
*
* Note: When a single link object is given, Shadowbox will automatically search
* for other cached link objects that have been set up in the same gallery and
* display them all together.
*
* @param {mixed} obj
* @public
*/
S.open = function(obj) {
if (open)
return;
var gc = S.makeGallery(obj);
S.gallery = gc[0];
S.current = gc[1];
obj = S.getCurrent();
if (obj == null)
return;
S.applyOptions(obj.options || {});
filterGallery();
// anything left to display?
if (S.gallery.length) {
obj = S.getCurrent();
if (S.options.onOpen(obj) === false)
return;
open = true;
S.skin.onOpen(obj, load);
}
}
/**
* Closes Shadowbox.
*
* @public
*/
S.close = function() {
if (!open)
return;
open = false;
if (S.player) {
S.player.remove();
S.player = null;
}
if (typeof slideTimer == "number") {
clearTimeout(slideTimer);
slideTimer = null;
}
slideDelay = 0;
listenKeys(false);
S.options.onClose(S.getCurrent());
S.skin.onClose();
S.revertOptions();
}
/**
* Starts a slideshow when a gallery is being displayed. Is called automatically
* when the slideshowDelay option is set to anything other than 0.
*
* @public
*/
S.play = function() {
if (!S.hasNext())
return;
if (!slideDelay)
slideDelay = S.options.slideshowDelay * 1000;
if (slideDelay) {
slideStart = now();
slideTimer = setTimeout(function(){
slideDelay = slideStart = 0; // reset slideshow
S.next();
}, slideDelay);
if(S.skin.onPlay)
S.skin.onPlay();
}
}
/**
* Pauses a slideshow on the current object.
*
* @public
*/
S.pause = function() {
if (typeof slideTimer != "number")
return;
slideDelay = Math.max(0, slideDelay - (now() - slideStart));
// if there's any time left on current slide, pause the timer
if (slideDelay) {
clearTimeout(slideTimer);
slideTimer = "pause";
if(S.skin.onPause)
S.skin.onPause();
}
}
/**
* Changes Shadowbox to display the item in the gallery specified by index.
*
* @param {Number} index
* @public
*/
S.change = function(index) {
if (!(index in S.gallery)) {
if (S.options.continuous) {
index = (index < 0 ? S.gallery.length + index : 0); // loop
if (!(index in S.gallery))
return;
} else {
return;
}
}
S.current = index;
if (typeof slideTimer == "number") {
clearTimeout(slideTimer);
slideTimer = null;
slideDelay = slideStart = 0;
}
S.options.onChange(S.getCurrent());
load(true);
}
/**
* Advances to the next item in the gallery.
*
* @public
*/
S.next = function() {
S.change(S.current + 1);
}
/**
* Rewinds to the previous gallery item.
*
* @public
*/
S.previous = function() {
S.change(S.current - 1);
}
/**
* Calculates the dimensions for Shadowbox.
*
* @param {Number} height The height of the object
* @param {Number} width The width of the object
* @param {Number} maxHeight The maximum available height
* @param {Number} maxWidth The maximum available width
* @param {Number} topBottom The extra top/bottom required for borders/toolbars
* @param {Number} leftRight The extra left/right required for borders/toolbars
* @param {Number} padding The amount of padding (in pixels) to maintain around
* the edge of the viewport
* @param {Boolean} preserveAspect True to preserve the original aspect ratio when the
* given dimensions are too large
* @return {Object} The new dimensions object
* @public
*/
S.setDimensions = function(height, width, maxHeight, maxWidth, topBottom, leftRight, padding, preserveAspect) {
var originalHeight = height,
originalWidth = width;
// constrain height/width to max
var extraHeight = 2 * padding + topBottom;
if (height + extraHeight > maxHeight)
height = maxHeight - extraHeight;
var extraWidth = 2 * padding + leftRight;
if (width + extraWidth > maxWidth)
width = maxWidth - extraWidth;
// determine if object is oversized
var changeHeight = (originalHeight - height) / originalHeight,
changeWidth = (originalWidth - width) / originalWidth,
oversized = (changeHeight > 0 || changeWidth > 0);
// adjust height/width if too large
if (preserveAspect && oversized) {
// preserve aspect ratio according to greatest change
if (changeHeight > changeWidth) {
width = Math.round((originalWidth / originalHeight) * height);
} else if (changeWidth > changeHeight) {
height = Math.round((originalHeight / originalWidth) * width);
}
}
S.dimensions = {
height: height + topBottom,
width: width + leftRight,
innerHeight: height,
innerWidth: width,
top: Math.floor((maxHeight - (height + extraHeight)) / 2 + padding),
left: Math.floor((maxWidth - (width + extraWidth)) / 2 + padding),
oversized: oversized
};
return S.dimensions;
}
/**
* Returns an array with two elements. The first is an array of objects that
* constitutes the gallery, and the second is the index of the given object in
* that array.
*
* @param {mixed} obj
* @return {Array} An array containing the gallery and current index
* @public
*/
S.makeGallery = function(obj) {
var gallery = [], current = -1;
if (typeof obj == "string")
obj = [obj];
if (typeof obj.length == "number") {
each(obj, function(i, o) {
if (o.content) {
gallery[i] = o;
} else {
gallery[i] = {content: o};
}
});
current = 0;
} else {
if (obj.tagName) {
// check the cache for this object before building one on the fly
var cacheObj = S.getCache(obj);
obj = cacheObj ? cacheObj : S.makeObject(obj);
}
if (obj.gallery) {
// gallery object, build gallery from cached gallery objects
gallery = [];
var o;
for (var key in S.cache) {
o = S.cache[key];
if (o.gallery && o.gallery == obj.gallery) {
if (current == -1 && o.content == obj.content)
current = gallery.length;
gallery.push(o);
}
}
if (current == -1) {
gallery.unshift(obj);
current = 0;
}
} else {
// single object, no gallery
gallery = [obj];
current = 0;
}
}
// use apply to break references to each gallery object here because
// the code may modify certain properties of these objects from here
// on out and we want to preserve the original in case the same object
// is used again in a future call
each(gallery, function(i, o) {
gallery[i] = apply({}, o);
});
return [gallery, current];
}
/**
* Extracts parameters from a link element and returns an object containing
* (most of) the following keys:
*
* - content: The URL of the linked to content
* - player: The abbreviated name of the player to use for the object (can automatically
* be determined in most cases)
* - title: The title to use for the object (optional)
* - gallery: The name of the gallery the object belongs to (optional)
* - height: The height of the object (in pixels, only required for movies and Flash)
* - width: The width of the object (in pixels, only required for movies and Flash)
* - options: A set of options to use for this object (optional)
* - link: A reference to the original link element
*
* A custom set of options may be passed in here that will be applied when
* this object is displayed. However, any options that are specified in
* the link's HTML markup will trump options given here.
*
* @param {HTMLElement} link
* @param {Object} options
* @return {Object} An object representing the link
* @public
*/
S.makeObject = function(link, options) {
var obj = {
// accessing the href attribute directly here (instead of using
// getAttribute) should give a full URL instead of a relative one
content: link.href,
title: link.getAttribute("title") || "",
link: link
};
// remove link-level options from top-level options
if (options) {
options = apply({}, options);
each(["player", "title", "height", "width", "gallery"], function(i, o) {
if (typeof options[o] != "undefined") {
obj[o] = options[o];
delete options[o];
}
});
obj.options = options;
} else {
obj.options = {};
}
if (!obj.player)
obj.player = S.getPlayer(obj.content);
// HTML options always trump JavaScript options, so do these last
var rel = link.getAttribute("rel");
if (rel) {
// extract gallery name from shadowbox[name] format
var match = rel.match(galleryName);
if (match)
obj.gallery = escape(match[2]);
// extract any other parameters
each(rel.split(';'), function(i, p) {
match = p.match(inlineParam);
if (match)
obj[match[1]] = match[2];
});
}
return obj;
}
/**
* Attempts to automatically determine the correct player to use for an object based
* on its content attribute. Defaults to "iframe" when the content type cannot
* automatically be determined.
*
* @param {String} content The content attribute of the object
* @return {String} The name of the player to use
* @public
*/
S.getPlayer = function(content) {
// strip query string for player detection purposes
var q = content.indexOf("?");
if (q > -1) {
content = content.substring(0, q);
}
// get file extension
var ext, m = content.match(fileExtension);
if (m) {
ext = m[1].toLowerCase();
}
if (ext) {
if (S.img && S.img.ext.contains(ext)) {
return "img";
}
if (S.swf && S.swf.ext.contains(ext)) {
return "swf";
}
if (S.flv && S.flv.ext.contains(ext)) {
return "flv";
}
if (S.qt && S.qt.ext.contains(ext)) {
return "qt";
}
}
return "iframe";
}
/**
* Filters the current gallery for unsupported objects.
*
* @private
*/
function filterGallery() {
var err = S.errorInfo, plugins = S.plugins, obj, remove, needed,
m, flashVersion;
for (var i = 0; i < S.gallery.length; ++i) {
obj = S.gallery[i]
remove = false; // remove the object?
needed = null; // what plugins are needed?
switch (obj.player) {
case "flv":
case "swf":
if (!plugins.fla) {
needed = "fla";
}
break;
case "qt":
if (!plugins.qt) {
needed = "qt";
}
break;
}
// handle unsupported elements
if (needed) {
if (S.options.handleUnsupported == "link") {
// generate a link to the appropriate plugin download page
obj.player = "html";
obj.content = '<div class="sb-message">Please download <a href="' + err[needed].url + '">' + err[needed].name + '</a> in order to view this content.</div>';
} else {
remove = true;
}
} else if (obj.player == "swf" || obj.player == "flv") {
flashVersion = (obj.options && obj.options.flashVersion) || S.options.flashVersion;
if (S.flash && !S.flash.hasFlashPlayerVersion(flashVersion)) {
// express install will be triggered because the client does not meet the
// minimum required version of Flash. set height and width to those of expressInstall.swf
obj.width = 310;
// minimum height is 127, but +20 pixels on top and bottom looks better
obj.height = 177;
}
}
if (remove) {
S.gallery.splice(i, 1);
if (i < S.current) {
--S.current; // maintain integrity of S.current
} else if (i == S.current) {
S.current = i > 0 ? i - 1 : i; // look for supported neighbor
}
// decrement index for next loop
--i;
}
}
}
/**
* Sets up a listener on the document for keydown events.
*
* @param {Boolean} on True to enable the listener, false to disable
* @private
*/
function listenKeys(on) {
if (!S.options.enableKeys)
return;
(on ? addEvent : removeEvent)(document, "keydown", handleKey);
}
/**
* A listener function that is fired when a key is pressed.
*
* @param {Event} e The keydown event
* @private
*/
function handleKey(e) {
// don't handle events with modifier keys
if (e.metaKey || e.shiftKey || e.altKey || e.ctrlKey) {
return;
}
var handler;
switch (e.keyCode) {
case 81: // q
case 88: // x
case 27: // esc
handler = S.close;
break;
case 37: // left
handler = S.previous;
break;
case 39: // right
handler = S.next;
break;
case 32: // space
handler = typeof slideTimer == "number" ? S.pause : S.play;
break;
}
if (handler) {
e.preventDefault();
handler();
}
}
/**
* Loads the current object.
*
* @param {Boolean} True if changing from a previous object
* @private
*/
function load(changing) {
listenKeys(false);
var obj = S.getCurrent(),
player = obj.player;
if (typeof S[player] !== "function") {
throw "unknown player " + player;
}
if (changing) {
S.player.remove();
S.revertOptions();
S.applyOptions(obj.options || {});
}
S.player = new S[player](obj, S.playerId);
// preload neighboring gallery images
if (S.gallery.length > 1) {
var next = S.gallery[S.current + 1] || S.gallery[0];
if (next.player === "img") {
var a = new Image();
a.src = next.content;
}
var prev = S.gallery[S.current - 1] || S.gallery[S.gallery.length - 1];
if (prev.player === "img") {
var b = new Image();
b.src = prev.content;
}