Blame view

frontend/web/js/new-owl/owl.hash.js 2.77 KB
b60a88b8   Anastasia   - main page
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
  /**
   * Hash Plugin
   * @version 2.1.0
   * @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);