-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflexpager.js
294 lines (250 loc) · 7.83 KB
/
flexpager.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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/**
* Copyright (c) 2013 Mike Sendlakowski
* Author: Mike Sendlakowski
* Git: https://github.com/sendlo/FlexPager
*
* Released under the MIT License
* http://www.opensource.org/licenses/MIT
*
* FlexPager
* A jQuery plugin to create a horizontal list carousel with pagination that reconfigures to handle irregular sized elements and any container size.
*
* Version: 1.0.0
*
*
* TODO:
* 1) Resize when page (or container) resizes. Reset user to the same position.
*
*/
(function (window, $, undefined) {
"use strict";
$.flexpager = function flexp(options, callback, element) {
this.element = $(element);
this.ul = $("ul:first", this.element);
this.pgNotFit = "data-notfit";
// Flag the object in the event of a failed creation
if (!this._create(options, callback)) {
this.failed = true;
}
};
$.flexpager.defaults = {
setPagesCB: function(){}, // Callback called whenever items are configured into pages
movedCB: function(){}, // Callback called whenever the carousel is paginated
pgFlag: "data-flxpgrstart", // Flag to identify the first item in each page
pgTxt: {p:"Previous page",n:"Next page",pg:"Page {}"} // All text used. Can be overriden for localization
};
$.flexpager.prototype = {
/*
----------------------------
Private methods
----------------------------
*/
_create: function(options, callback){
var opts = $.extend(true, {}, $.flexpager.defaults, options);
this.options = opts;
this._attachEvents();
this._setPages();
return true;
},
_update: function(options){
var self = this;
var opts = $.extend(true, {}, $.flexpager.defaults, options);
self.options = opts;
self._setPages();
return true;
},
_attachEvents: function(){
var self = this;
self.element.delegate(".fpCtrls a","click",function(){self._move($(this).data("move"));});
},
_setPages: function() {
var self = this,
curPageWidth = 0,
previousItem,
itemWidth,
item,
previousRightMargin,
previousStartElement;
var maxPageWidth = self.element.width();
self._hidePaging();
$('li', self.ul).each(function(){
item = $(this);
item.attr(self.options.pgFlag, false);
item.attr(self.pgNotFit, false);
itemWidth = item.width();
if(!previousItem) {
item.attr(self.options.pgFlag, true); // mark this as a start to a page
previousStartElement = item;
curPageWidth = itemWidth;
} else {
previousRightMargin = previousItem.css("marginRight").replace('px', '')-0;
if((curPageWidth + previousRightMargin + itemWidth) > maxPageWidth) {
item.attr(self.options.pgFlag, true); // mark this as a start to a page
if(curPageWidth + previousRightMargin + 1 < maxPageWidth) {
// if the items on the prevous page cannot fit nicely, mark the previous start page so it can be handled in a customized way (fade away, etc)
previousStartElement.attr(self.pgNotFit, true);
}
previousStartElement = item;
curPageWidth = 0;
} else {
curPageWidth += previousRightMargin;
}
curPageWidth += itemWidth;
}
previousItem = item;
});
self._initPaging();
self.options.setPagesCB();
},
_hidePaging: function() {
var self = this;
if(self.controls) {
self.controls.hide();
}
},
_initPaging : function() {
var self = this,
pagination = "",
classVar;
self.curPage = 0;
self.pageItems = $("li["+self.options.pgFlag+"=true]", self.ul);
if(self.pageItems.size() < 2) {
self._testFit();
return; // no paging needed
}
if(!self.controls) {
self.controls = $('<div class="fpCtrls"></div>');
self.element.append(self.controls);
}
pagination += self._buildPageLink("p","flxP flxAlt",self.options.pgTxt.p);
self.pageItems.each(function(idx){
classVar = (idx === 0)?"flxD flxAlt":"flxD";
pagination += self._buildPageLink(idx+1, classVar ,self.options.pgTxt.pg.replace("{}",idx+1));
});
pagination += self._buildPageLink("n","flxN",self.options.pgTxt.n);
self.controls.html(pagination);
self.controls.show();
self._testFit();
self.pgCntrls = $("a", self.controls);
},
_buildPageLink: function(moveStr, classStr, title) {
return '<a href="javascript:;" data-move="'+moveStr+'" class="'+classStr+'" title="'+title+'"></a>';
},
_move: function(direction) {
var self = this,
newPage = self._getPage(direction),
pageItem = self.pageItems.eq(newPage),
position = 0-pageItem.position().left;
self.curPage = newPage;
self._updateControls(newPage);
self.ul.stop(true,true).animate({"left":position}, 750);
self._testFit();
self.options.movedCB();
},
_testFit: function(off) {
var self = this,
pageStartItem = self.pageItems.eq(self.curPage);
this.element.toggleClass("flxNoFit", (pageStartItem.attr(this.pgNotFit) === "true"));
},
_updateControls: function(newPage) {
var self = this,
lastPage = self.pgCntrls.size()-2, // adjusting for previous and next links
link,
addClass;
newPage++; // adjusting the 0 based index being passed in
self.pgCntrls.each(function(idx){
link = $(this);
addClass = false;
if(link.hasClass('flxP')) {
addClass = (newPage === 1);
} else if(link.hasClass('flxN')) {
addClass = (newPage === lastPage);
} else {
addClass = (newPage === idx);
}
link.toggleClass("flxAlt", addClass);
});
},
_getPage: function(direction) {
var self = this,
newPage = self.curPage;
switch(direction) {
case "p":
newPage--;
break;
case "n":
newPage++;
break;
default:
newPage = parseInt(direction) || 0;
newPage--; // adjusting for 0 based array
break;
}
if(newPage < 1) {
newPage = 0;
} else if(newPage > self.pageItems.size()-1) {
newPage = self.pageItems.size()-1;
}
return newPage;
},
/*
----------------------------
Public methods
----------------------------
*/
update: function(options) {
this._update(options);
}
};
/*
----------------------------
Function
----------------------------
*/
$.fn.flexpager = function flexp_init(options, callback) {
var thisCall = typeof options;
switch (thisCall) {
// allow users to call a specific public method
case 'string':
var args = Array.prototype.slice.call(arguments, 1);
try{
this.each(function () {
var instance = $.data(this, 'flexpager');
if (!instance) {
throw 'Method ' + options + ' cannot be called until Flex Pager is setup';
}
if (!$.isFunction(instance[options]) || options.charAt(0) === "_") {
throw 'No such public method ' + options + ' for Flex Pager';
}
// no errors!
instance[options].apply(instance, args);
});
} catch(err){
if(window.console){
console.log(err);
}
return false;
}
break;
// attempt to create
case 'undefined':
case 'object':
this.each(function () {
var instance = $.data(this, 'flexpager');
if (instance) {
// update options of current instance
instance.update(options);
} else {
// initialize new instance
instance = new $.flexpager(options, callback, this);
// don't attach if instantiation failed
if (!instance.failed) {
$.data(this, 'flexpager', instance);
}
}
});
break;
}
return this;
};
})(window, jQuery);