forked from kaelzhang/neuron.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathendless.js
More file actions
250 lines (191 loc) · 6.47 KB
/
endless.js
File metadata and controls
250 lines (191 loc) · 6.47 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/**
* Switch Plugin: endless
*/
function returnFalse(){
return false;
};
/**
* generate a circlewize result of numbers by the range(length) and passed value(value)
* offset relative to initial pos -> index relative to items
* circlewizeInt is much more usefull than operator '%' when dealing negative numbers
* suppose:
* length: 3
* cases:
* values: -4 -3 -2 -1 0 1 2 3 4 5
* returns: 2 0 1 2 0 1 2 0 1 2
* @param {number} (int)value
* @param {number} (int)length
*/
function circlewizeInt(value, length){
return value - Math.floor(value / length) * length;
};
function realInit(self, EVENTS){
var move = self.get('move'),
stage = self.get('stage'),
direction = self.get('direction'),
offset_direction = self.offsetDirection;
/**
* deal with the situation data
/////////////////////////////////////////////////////////////////////////////
| curOffset |
| leftItems | stage | rightItems |
stage: ---------
train: -----------------------------------------
initial: |
/////////////////////////////////////////////////////////////////////////////
after the initialization, the order of the items might be changed
*/
// @type {number} this.leftItems items aside on the left of the 'stage'
self.leftItems =
// @type {number} this.curOffset real offset relative to the initial position of the 'container'
self.curOffset = self.activeIndex;
// @type {number} this.leftItems items aside on the right of the stage
self.rightItems = self.length - stage - self.leftItems;
// override methods
NR.mix(self, METHODS_OVERRIDEN);
self.on(EVENTS.NEXT, function(){
var self = this,
move_needed = move - self.rightItems;
move_needed > 0 && self._moveItems(move_needed);
self._dealOffset(move);
});
self.on(EVENTS.PREV, function(){
var self = this,
move_needed = move - self.leftItems;
move_needed > 0 && self._moveItems(- move_needed);
self._dealOffset(- move);
});
};
var
METHODS_OVERRIDEN = {
noprev: false,
nonext: false,
/**
* @override
* limit the range of this.activeIndex
*/
_limit: function(index){
return circlewizeInt(index, this.length);
},
/**
* @override
* if plugin::endless attached,
* there will be no left end or right end
*/
_isNoprev: returnFalse,
_isNonext: returnFalse,
/**
* method to move items
* @private
* @param {number} amount
if amount > 0, move <amount> items from the left to the right
if amount < 0, move <amount> items from the right to the left
*/
_moveItems: function(amount){
var self = this,
where,
stage = self.get('stage'),
direction = self.get('direction'),
space = self.get('itemSpace'),
length = self.length,
offset = self.curOffset,
lastOffset = offset + stage + self.rightItems - 1,
start,
step,
check,
// relative offset for the value of start
relative;
if(amount > 0){
start = 1;
step = 1;
check = function(){
return start <= amount;
};
relative = lastOffset;
}else{
start = 0;
step = -1;
check = function(){
return start > amount;
};
relative = offset - self.leftItems - 1;
}
for(; check(); start += step){
// when moving items,
// we don't change any data relevant to position
self._getItem( circlewizeInt(lastOffset + start, length), true).css(direction, (relative + start) * space);
}
// change position data at the end
self.leftItems -= amount;
self.rightItems += amount;
},
/**
* @param {number} move
*/
_dealOffset: function(move){
var self = this;
self.curOffset += move;
self.rightItems -= move;
self.leftItems += move;
return self;
},
/**
* @override
* method to get the real offset relative to the container
* after plugin::endless attached, the index of items and the offset to the initial pos might be different
*
* @param {number} index
*/
_getOffset: function(index){
var self = this,
stage = self.get('stage'),
// the changing of this.curOffset is always ahead of this.activeIndex
// so we use this.expectIndex to be compared with
expect = self.expectIndex,
delta = index - expect,
max_right,
max_left;
if(delta !== 0){
max_right = stage + self.rightItems - 1;
max_left = - self.leftItems;
if(delta > 0 && delta > max_right){
delta += max_left - max_right;
}else if(delta < 0 && delta < max_left){
delta += max_right - max_left;
}
}
return self.curOffset + delta;
}
};
module.exports = {
name: 'endless',
ATTRS: {
itemSpace: null
},
init: function(self){
var EVENTS = self.get('EVENTS');
self.on(EVENTS.AFTER_INIT, function(){
var t = self;
/**
if it doesn't meet the condition, there would be blanks
when moving the items during switching. Thus,
plugin::endless will dothing, and the Switcher
will fallback to normal carousel
*/
if(t.length >= t.get('stage') + t.get('move')){
realInit(t, EVENTS);
}
});
}
};
/**
change log:
2012-04-18 Kael:
TODO:
A. new _moveItems method to deal with nonlinear switching.
2011-11-02 Kael:
- add ._getOffset method to get the real offset relative to the container
- refractor, so it could work well with plugin::step
2011-10-30 Kael:
[issue]: blanks may occur during switching
*/