123 lines
2.8 KiB
JavaScript
123 lines
2.8 KiB
JavaScript
/**
|
|
* Hash Plugin
|
|
* @version 2.3.4
|
|
* @author Artus Kolanowski
|
|
* @author David Deutsch
|
|
* @license The MIT License (MIT)
|
|
*/
|
|
;(function($, window, document, undefined) {
|
|
'use strict';
|
|
|
|
/**
|
|
* Creates the hash plugin.
|
|
* @class The Hash Plugin
|
|
* @param {Owl} carousel - The Owl Carousel
|
|
*/
|
|
var Hash = function(carousel) {
|
|
/**
|
|
* Reference to the core.
|
|
* @protected
|
|
* @type {Owl}
|
|
*/
|
|
this._core = carousel;
|
|
|
|
/**
|
|
* Hash index for the items.
|
|
* @protected
|
|
* @type {Object}
|
|
*/
|
|
this._hashes = {};
|
|
|
|
/**
|
|
* The carousel element.
|
|
* @type {jQuery}
|
|
*/
|
|
this.$element = this._core.$element;
|
|
|
|
/**
|
|
* All event handlers.
|
|
* @protected
|
|
* @type {Object}
|
|
*/
|
|
this._handlers = {
|
|
'initialized.owl.carousel': $.proxy(function(e) {
|
|
if (e.namespace && this._core.settings.startPosition === 'URLHash') {
|
|
$(window).trigger('hashchange.owl.navigation');
|
|
}
|
|
}, this),
|
|
'prepared.owl.carousel': $.proxy(function(e) {
|
|
if (e.namespace) {
|
|
var hash = $(e.content).find('[data-hash]').addBack('[data-hash]').attr('data-hash');
|
|
|
|
if (!hash) {
|
|
return;
|
|
}
|
|
|
|
this._hashes[hash] = e.content;
|
|
}
|
|
}, this),
|
|
'changed.owl.carousel': $.proxy(function(e) {
|
|
if (e.namespace && e.property.name === 'position') {
|
|
var current = this._core.items(this._core.relative(this._core.current())),
|
|
hash = $.map(this._hashes, function(item, hash) {
|
|
return item === current ? hash : null;
|
|
}).join();
|
|
|
|
if (!hash || window.location.hash.slice(1) === hash) {
|
|
return;
|
|
}
|
|
|
|
window.location.hash = hash;
|
|
}
|
|
}, this)
|
|
};
|
|
|
|
// set default options
|
|
this._core.options = $.extend({}, Hash.Defaults, this._core.options);
|
|
|
|
// register the event handlers
|
|
this.$element.on(this._handlers);
|
|
|
|
// register event listener for hash navigation
|
|
$(window).on('hashchange.owl.navigation', $.proxy(function(e) {
|
|
var hash = window.location.hash.substring(1),
|
|
items = this._core.$stage.children(),
|
|
position = this._hashes[hash] && items.index(this._hashes[hash]);
|
|
|
|
if (position === undefined || position === this._core.current()) {
|
|
return;
|
|
}
|
|
|
|
this._core.to(this._core.relative(position), false, true);
|
|
}, this));
|
|
};
|
|
|
|
/**
|
|
* Default options.
|
|
* @public
|
|
*/
|
|
Hash.Defaults = {
|
|
URLhashListener: false
|
|
};
|
|
|
|
/**
|
|
* Destroys the plugin.
|
|
* @public
|
|
*/
|
|
Hash.prototype.destroy = function() {
|
|
var handler, property;
|
|
|
|
$(window).off('hashchange.owl.navigation');
|
|
|
|
for (handler in this._handlers) {
|
|
this._core.$element.off(handler, this._handlers[handler]);
|
|
}
|
|
for (property in Object.getOwnPropertyNames(this)) {
|
|
typeof this[property] != 'function' && (this[property] = null);
|
|
}
|
|
};
|
|
|
|
$.fn.owlCarousel.Constructor.Plugins.Hash = Hash;
|
|
|
|
})(window.Zepto || window.jQuery, window, document);
|