-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
162 lines (134 loc) · 2.46 KB
/
index.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/**
* Dependencies
*/
var Emitter = require('emitter');
/**
* Expose `Deck`
*/
module.exports = Deck;
/**
* Utils
*/
var array = [];
var slice = array.slice;
/**
* Deck
* create a deck
*
* @param {Object} [options] options
* @param {String} [options.selector] slide selector
* @api public
*/
function Deck (options) {
Emitter.call(this);
var options = options || {};
var defaults = this.defaults;
var selector = options.selector || defaults.selector;
var elements = document.querySelectorAll(selector);
this.selector = selector;
this.slides = slice.call(elements);
this.n = elements.length;
this.current = 0;
}
/**
* Inherit from `Emitter.prototype`
*/
Deck.prototype = Object.create(Emitter.prototype);
Deck.prototype.constructor = Deck;
/**
* Defaults
*/
Deck.prototype.defaults = {
"selector": "article.slide"
};
/**
* next
* Change to next slide.
*
* @return {Deck} this for chaining
* @api public
*/
Deck.prototype.next = function () {
return this.nth(this.current+1);
};
/**
* prev
* Change to the previous slide.
*
* @return {Deck} this for chaining
* @api public
*/
Deck.prototype.prev = function () {
return this.nth(this.current-1);
};
/**
* last
* Change to the last slide.
*
* @return {Deck} this for chaining
* @api public
*/
Deck.prototype.last = function () {
return this.nth(this.n-1);
};
/**
* first
* Change to the first slide.
*
* @return {Deck} this for chaining
* @api public
*/
Deck.prototype.first = function () {
return this.nth(0);
};
/**
* nth
* Change to the *n*-th slide.
*
* @param {Number} i slide index
* @return {Deck} this for chaining
* @api public
*/
Deck.prototype.nth = function (i) {
if (i === this.current) {
return this;
}
if (i < 0) {
return this;
}
if (i > this.n-1) {
return this;
}
this.past = this.current;
this.current = i;
this.goto(i);
this.show(i);
this.emit("slide", this.current, this.past);
return this;
};
/**
* goto
* Change location to the *i*-th slide
*
* @param {Number} i slide index
* @return {Deck} this for chaining
* @api public
*/
Deck.prototype.goto = function (i) {
window.location = "#" + i;
return this;
};
/**
* show
* Show the *i*-th slide
*
* @param {Number} i slide index
* @return {Deck} this for chaining
* @api public
*/
Deck.prototype.show = function (i) {
this.slides.forEach(function (slide, k) {
slide.style.display = (i === k) ? "block" : "none";
});
return this;
};