-
Notifications
You must be signed in to change notification settings - Fork 0
/
counter.js
110 lines (91 loc) · 2.59 KB
/
counter.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
/*!
* Flickity counter v0.0.1
* Enable slide counter for Flickity
*/
/*jshint browser: true, undef: true, unused: true, strict: true*/
( function( window, factory ) {
// universal module definition
/*jshint strict: false */ /*globals define, module, require */
if ( typeof define == 'function' && define.amd ) {
// AMD
define( [
'flickity/js/index',
], factory );
} else if ( typeof module == 'object' && module.exports ) {
// CommonJS
module.exports = factory(
require('flickity')
);
} else {
// browser global
factory(
window.Flickity
);
}
}( window, function factory( Flickity ) {
'use strict';
Flickity.createMethods.push('_createCounterDiv');
var proto = Flickity.prototype;
proto._createCounterDiv = function() {
if ( !this.options.counter ) {
return;
}
this.CounterDiv = new CounterDiv( this );
this.on( 'activate', this.activateCounter );
};
proto.activateCounter = function() {
this.CounterDiv.activate();
this.on( 'deactivate', this.deactivateCounter );
}
proto.deactivateCounter = function() {
this.CounterDiv.deactivate();
this.off( 'deactivate', this.deactivateCounter );
}
// ----- Counter Div ----- //
function CounterDiv( flickity ) {
this.parent = flickity;
this.divisor = this.parent.options.counterDivisor || ' / ';
this.create();
this.disable();
}
CounterDiv.prototype.activate = function() {
this.update();
this.parent.on( 'select', this.update.bind( this ) );
this.parent.element.appendChild( this.element );
}
CounterDiv.prototype.deactivate = function() {
this.parent.element.removeChild( this.element );
}
CounterDiv.prototype.enable = function() {
if(this.isEnabled) {
return;
}
this.isEnabled = true;
this.element.classList.remove('flickity-counter-disabled');
this.element.classList.add('flickity-counter-enabled');
}
CounterDiv.prototype.disable = function() {
if(this.isEnabled === false) {
return;
}
this.isEnabled = false;
this.element.classList.remove('flickity-counter-enabled');
this.element.classList.add('flickity-counter-disabled');
}
CounterDiv.prototype.create = function() {
var element = this.element = document.createElement('div');
element.className = 'flickity-button flickity-counter';
};
CounterDiv.prototype.update = function() {
var totalSlides = this.parent.slides.length;
var currentSlide = this.parent.selectedIndex + 1;
if( totalSlides > 1 ) {
this.enable();
this.element.innerHTML = currentSlide + this.divisor + totalSlides;
} else {
this.disable();
}
}
Flickity.CounterDiv = CounterDiv;
return Flickity;
}));