forked from fancyapps/fancybox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash.js
210 lines (173 loc) · 6.13 KB
/
hash.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
// ==========================================================================
//
// Hash
// Enables linking to each modal
//
// ==========================================================================
(function (window, document, $) {
"use strict";
// Simple $.escapeSelector polyfill (for jQuery prior v3)
if (!$.escapeSelector) {
$.escapeSelector = function (sel) {
var rcssescape = /([\0-\x1f\x7f]|^-?\d)|^-$|[^\x80-\uFFFF\w-]/g;
var fcssescape = function (ch, asCodePoint) {
if (asCodePoint) {
// U+0000 NULL becomes U+FFFD REPLACEMENT CHARACTER
if (ch === "\0") {
return "\uFFFD";
}
// Control characters and (dependent upon position) numbers get escaped as code points
return ch.slice(0, -1) + "\\" + ch.charCodeAt(ch.length - 1).toString(16) + " ";
}
// Other potentially-special ASCII characters get backslash-escaped
return "\\" + ch;
};
return (sel + "").replace(rcssescape, fcssescape);
};
}
// Get info about gallery name and current index from url
function parseUrl() {
var hash = window.location.hash.substr(1),
rez = hash.split("-"),
index = rez.length > 1 && /^\+?\d+$/.test(rez[rez.length - 1]) ? parseInt(rez.pop(-1), 10) || 1 : 1,
gallery = rez.join("-");
return {
hash: hash,
/* Index is starting from 1 */
index: index < 1 ? 1 : index,
gallery: gallery
};
}
// Trigger click evnt on links to open new fancyBox instance
function triggerFromUrl(url) {
if (url.gallery !== "") {
// If we can find element matching 'data-fancybox' atribute,
// then triggering click event should start fancyBox
$("[data-fancybox='" + $.escapeSelector(url.gallery) + "']")
.eq(url.index - 1)
.focus()
.trigger("click.fb-start");
}
}
// Get gallery name from current instance
function getGalleryID(instance) {
var opts, ret;
if (!instance) {
return false;
}
opts = instance.current ? instance.current.opts : instance.opts;
ret = opts.hash || (opts.$orig ? opts.$orig.data("fancybox") || opts.$orig.data("fancybox-trigger") : "");
return ret === "" ? false : ret;
}
// Start when DOM becomes ready
$(function () {
// Check if user has disabled this module
if ($.fancybox.defaults.hash === false) {
return;
}
// Update hash when opening/closing fancyBox
$(document).on({
"onInit.fb": function (e, instance) {
var url, gallery;
if (instance.group[instance.currIndex].opts.hash === false) {
return;
}
url = parseUrl();
gallery = getGalleryID(instance);
// Make sure gallery start index matches index from hash
if (gallery && url.gallery && gallery == url.gallery) {
instance.currIndex = url.index - 1;
}
},
"beforeShow.fb": function (e, instance, current, firstRun) {
var gallery;
if (!current || current.opts.hash === false) {
return;
}
// Check if need to update window hash
gallery = getGalleryID(instance);
if (!gallery) {
return;
}
// Variable containing last hash value set by fancyBox
// It will be used to determine if fancyBox needs to close after hash change is detected
instance.currentHash = gallery + (instance.group.length > 1 ? "-" + (current.index + 1) : "");
// If current hash is the same (this instance most likely is opened by hashchange), then do nothing
if (window.location.hash === "#" + instance.currentHash) {
return;
}
if (firstRun && !instance.origHash) {
instance.origHash = window.location.hash;
}
if (instance.hashTimer) {
clearTimeout(instance.hashTimer);
}
// Update hash
instance.hashTimer = setTimeout(function () {
if ("replaceState" in window.history) {
window.history[firstRun ? "pushState" : "replaceState"]({},
document.title,
window.location.pathname + window.location.search + "#" + instance.currentHash
);
if (firstRun) {
instance.hasCreatedHistory = true;
}
} else {
window.location.hash = instance.currentHash;
}
instance.hashTimer = null;
}, 300);
},
"beforeClose.fb": function (e, instance, current) {
if (!current || current.opts.hash === false) {
return;
}
clearTimeout(instance.hashTimer);
// Goto previous history entry
if (instance.currentHash && instance.hasCreatedHistory) {
window.history.back();
} else if (instance.currentHash) {
if ("replaceState" in window.history) {
window.history.replaceState({}, document.title, window.location.pathname + window.location.search + (instance.origHash || ""));
} else {
window.location.hash = instance.origHash;
}
}
instance.currentHash = null;
}
});
// Check if need to start/close after url has changed
$(window).on("hashchange.fb", function () {
var url = parseUrl(),
fb = null;
// Find last fancyBox instance that has "hash"
$.each(
$(".fancybox-container")
.get()
.reverse(),
function (index, value) {
var tmp = $(value).data("FancyBox");
if (tmp && tmp.currentHash) {
fb = tmp;
return false;
}
}
);
if (fb) {
// Now, compare hash values
if (fb.currentHash !== url.gallery + "-" + url.index && !(url.index === 1 && fb.currentHash == url.gallery)) {
fb.currentHash = null;
fb.close();
}
} else if (url.gallery !== "") {
triggerFromUrl(url);
}
});
// Check current hash and trigger click event on matching element to start fancyBox, if needed
setTimeout(function () {
if (!$.fancybox.getInstance()) {
triggerFromUrl(parseUrl());
}
}, 50);
});
})(window, document, jQuery);