Blame view

frontend/web/js/new-owl/owl.autorefresh.js 2.3 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
  /**
   * AutoRefresh Plugin
   * @version 2.1.0
   * @author Artus Kolanowski
   * @author David Deutsch
   * @license The MIT License (MIT)
   */
  ;(function($, window, document, undefined) {
  
  	/**
  	 * Creates the auto refresh plugin.
  	 * @class The Auto Refresh Plugin
  	 * @param {Owl} carousel - The Owl Carousel
  	 */
  	var AutoRefresh = function(carousel) {
  		/**
  		 * Reference to the core.
  		 * @protected
  		 * @type {Owl}
  		 */
  		this._core = carousel;
  
  		/**
  		 * Refresh interval.
  		 * @protected
  		 * @type {number}
  		 */
  		this._interval = null;
  
  		/**
  		 * Whether the element is currently visible or not.
  		 * @protected
  		 * @type {Boolean}
  		 */
  		this._visible = null;
  
  		/**
  		 * All event handlers.
  		 * @protected
  		 * @type {Object}
  		 */
  		this._handlers = {
  			'initialized.owl.carousel': $.proxy(function(e) {
  				if (e.namespace && this._core.settings.autoRefresh) {
  					this.watch();
  				}
  			}, this)
  		};
  
  		// set default options
  		this._core.options = $.extend({}, AutoRefresh.Defaults, this._core.options);
  
  		// register event handlers
  		this._core.$element.on(this._handlers);
  	};
  
  	/**
  	 * Default options.
  	 * @public
  	 */
  	AutoRefresh.Defaults = {
  		autoRefresh: true,
  		autoRefreshInterval: 500
  	};
  
  	/**
  	 * Watches the element.
  	 */
  	AutoRefresh.prototype.watch = function() {
  		if (this._interval) {
  			return;
  		}
  
  		this._visible = this._core.$element.is(':visible');
  		this._interval = window.setInterval($.proxy(this.refresh, this), this._core.settings.autoRefreshInterval);
  	};
  
  	/**
  	 * Refreshes the element.
  	 */
  	AutoRefresh.prototype.refresh = function() {
  		if (this._core.$element.is(':visible') === this._visible) {
  			return;
  		}
  
  		this._visible = !this._visible;
  
  		this._core.$element.toggleClass('owl-hidden', !this._visible);
  
  		this._visible && (this._core.invalidate('width') && this._core.refresh());
  	};
  
  	/**
  	 * Destroys the plugin.
  	 */
  	AutoRefresh.prototype.destroy = function() {
  		var handler, property;
  
  		window.clearInterval(this._interval);
  
  		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.AutoRefresh = AutoRefresh;
  
  })(window.Zepto || window.jQuery, window, document);