mirror of https://github.com/IoTcat/ushio-img.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
216 lines
6.2 KiB
216 lines
6.2 KiB
// ========================================================================== |
|
// |
|
// Hash |
|
// Enables linking to each modal |
|
// |
|
// ========================================================================== |
|
(function(document, window, $) { |
|
"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) { |
|
var $el; |
|
|
|
if (url.gallery !== "") { |
|
// If we can find element matching 'data-fancybox' atribute, then trigger click event for that. |
|
// It should start fancyBox |
|
$el = $("[data-fancybox='" + $.escapeSelector(url.gallery) + "']") |
|
.eq(url.index - 1) |
|
.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") : ""); |
|
|
|
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 (!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) { |
|
var gallery; |
|
|
|
if (current.opts.hash === false) { |
|
return; |
|
} |
|
|
|
gallery = getGalleryID(instance); |
|
|
|
// 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; |
|
|
|
clearTimeout(instance.hashTimer); |
|
} |
|
}); |
|
|
|
// Check if need to start/close after url has changed |
|
$(window).on("hashchange.fb", function() { |
|
var url = parseUrl(), |
|
fb; |
|
|
|
// Find last fancyBox instance that has "hash" |
|
$.each( |
|
$(".fancybox-container") |
|
.get() |
|
.reverse(), |
|
function(index, value) { |
|
var tmp = $(value).data("FancyBox"); |
|
//isClosing |
|
if (tmp.currentHash) { |
|
fb = tmp; |
|
return false; |
|
} |
|
} |
|
); |
|
|
|
if (fb) { |
|
// Now, compare hash values |
|
if (fb.currentHash && 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); |
|
}); |
|
})(document, window, window.jQuery || jQuery);
|
|
|