-
Notifications
You must be signed in to change notification settings - Fork 9
/
tools.overlay.gallery.js
212 lines (155 loc) · 5.51 KB
/
tools.overlay.gallery.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
/**
* Overlay Gallery plugin, version: 1.0.0
*
* Copyright (c) 2009 Tero Piirainen
* http://flowplayer.org/tools/overlay.html#gallery
*
* Dual licensed under MIT and GPL 2+ licenses
* http://www.opensource.org/licenses
*
* Since : July 2009
* Date: ${date}
* Revision: ${revision}
*/
(function($) {
// TODO: next(), prev(), getIndex(), onChange event
// version number
var t = $.tools.overlay;
t.plugins = t.plugins || {};
t.plugins.gallery = {
version: '1.0.0',
conf: {
imgId: 'img',
next: '.next',
prev: '.prev',
info: '.info',
progress: '.progress',
disabledClass: 'disabled',
activeClass: 'active',
opacity: 0.8,
speed: 'slow',
template: '<strong>${title}</strong> <span>Image ${index} of ${total}</span>',
autohide: true,
preload: true,
api: false
}
};
$.fn.gallery = function(opts) {
var conf = $.extend({}, t.plugins.gallery.conf), api;
$.extend(conf, opts);
// common variables for all gallery images
api = this.overlay();
var links = this,
overlay = api.getOverlay(),
next = overlay.find(conf.next),
prev = overlay.find(conf.prev),
info = overlay.find(conf.info),
progress = overlay.find(conf.progress),
els = prev.add(next).add(info).css({opacity: conf.opacity}),
close = api.getClosers(),
index;
//{{{ load
function load(el) {
progress.fadeIn();
els.hide(); close.hide();
var url = el.attr("href");
// download the image
var image = new Image();
image.onload = function() {
progress.fadeOut();
// find image inside overlay
var img = $("#" + conf.imgId, overlay);
// or append it to the overlay
if (!img.length) {
img = $("<img/>").attr("id", conf.imgId).css("visibility", "hidden");
overlay.prepend(img);
}
// make initially invisible to get it's dimensions
img.attr("src", url).css("visibility", "hidden");
// animate overlay to fit the image dimensions
var width = image.width;
var left = ($(window).width() - width) / 2;
// calculate index number
index = links.index(links.filter("[href=" +url+ "]"));
// activate trigger
links.removeClass(conf.activeClass).eq(index).addClass(conf.activeClass);
// enable/disable next/prev links
var cls = conf.disabledClass;
els.removeClass(cls);
if (index === 0) { prev.addClass(cls); }
if (index == links.length -1) { next.addClass(cls); }
// set info text & width
var text = conf.template
.replace("${title}", el.attr("title") || el.data("title"))
.replace("${index}", index + 1)
.replace("${total}", links.length);
var padd = parseInt(info.css("paddingLeft"), 10) + parseInt(info.css("paddingRight"), 10);
info.html(text).css({width: width - padd});
overlay.animate({
width: width, height: image.height, left: left}, conf.speed, function() {
// gradually show the image
img.hide().css("visibility", "visible").fadeIn(function() {
if (!conf.autohide) {
els.fadeIn(); close.show();
}
});
});
};
image.onerror = function() {
overlay.fadeIn().html("Cannot find image " + url);
};
image.src = url;
if (conf.preload) {
links.filter(":eq(" +(index-1)+ "), :eq(" +(index+1)+ ")").each(function() {
var img = new Image();
img.src = $(this).attr("href");
});
}
}
//}}}
// function to add click handlers to next/prev links
function addClick(el, isNext) {
el.click(function() {
if (el.hasClass(conf.disabledClass)) { return; }
// find the triggering link
var trigger = links.eq(i = index + (isNext ? 1 : -1));
// if found load it's href
if (trigger.length) { load(trigger); }
});
}
// assign next/prev click handlers
addClick(next, true);
addClick(prev);
// arrow keys
$(document).keydown(function(evt) {
if (!overlay.is(":visible") || evt.altKey || evt.ctrlKey) { return; }
if (evt.keyCode == 37 || evt.keyCode == 39) {
var btn = evt.keyCode == 37 ? prev : next;
btn.click();
return evt.preventDefault();
}
return true;
});
function showEls() {
if (!overlay.is(":animated")) {
els.show(); close.show();
}
}
// autohide functionality
if (conf.autohide) {
overlay.hover(showEls, function() { els.fadeOut(); close.hide(); }).mousemove(showEls);
}
// load a proper gallery image when overlay trigger is clicked
var ret;
this.each(function() {
var el = $(this), api = $(this).overlay(), ret = api;
api.onBeforeLoad(function() {
load(el);
});
api.onClose(function() {
links.removeClass(conf.activeClass);
});
});
return conf.api ? ret : this;
};
})(jQuery);