-
Notifications
You must be signed in to change notification settings - Fork 0
/
flipscroll.js
354 lines (316 loc) · 11.9 KB
/
flipscroll.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/**
* Flipscroll is a responsive table building class that includes a paging system.
*
* Author: Jeff Walter
* Date: Jan. 26, 2012
*/
/**
* Constructor
*
* @param action - url to get the table data from (json).
* @param page - page number of the data to fetch.
* @param limit - number of rows in each page.
* @param template - optional, html table row template.
* @param data_key - optional, key in json where data is found.
*/
function FlipScroll(flipscroll_container_selector, action, page, limit, template, data_key)
{
this.flipscroll_container_selector = flipscroll_container_selector || '#flip-scroll';
this.data_key = data_key || null;
this.action = action;
this.page = page || 1;
this.default_page = this.page;
this.limit = limit || 25;
this.default_limit = this.limit;
this.template = template;
this.event_handlers = [];
this.pager_length = 5;
this.start = null;
this.end = null;
this.last_page = this.page;
this.last_limit = this.limit;
}
/**
* Sets the data source and paging options.
*
* @param action - url to get the table data from (json).
* @param page - page number of the data to fetch.
* @param limit - number of rows in each page.
*/
FlipScroll.prototype.set_paging_options = function(action, page, limit)
{
this.action = action;
this.last_page = this.page;
this.last_limit = this.limit;
this.page = page || this.default_page;
this.limit = limit || this.default_limit;
};
/**
* Sets the row template to use during rendering
*
* You can either define a callback function that takes a row object, or define the html string to use as a row template.
*
* If you choose to pass an html string, this template should use {key} for the placeholder of the value to be replaced.
* Given that your data looked like => { key: 'value' }, key would be replaced by value.
*
* @param template - template to use.
*/
FlipScroll.prototype.set_row_template = function(template)
{
this.row_template = template;
};
/**
* Sets the row header template to use during rendering
*
* You can either define a callback function that takes a row header object, or define the html string to use as a row header template.
*
* If you choose to pass an html string, this template should use {key} for the placeholder of the value to be replaced.
* Given that your data looked like => { key: 'value' }, key would be replaced by value.
*
* @param template - template to use.
*/
FlipScroll.prototype.set_row_header_template = function(template)
{
this.header_template = template;
};
/**
* Sets the paging limit. Also resets the pager back to start.
*
* @param limit - number of rows in each page.
*/
FlipScroll.prototype.set_limit = function(limit)
{
this.page = 1;
this.limit = limit;
};
/**
* Sets the current page to fetch.
*
* @param page - page number of the data to fetch.
*/
FlipScroll.prototype.set_current_page = function(page)
{
this.page = page;
};
/**
* Calculates and builds the html pager controls for the table.
*
* @param paging - object of the following format: (comes from .json urls).
*
* paging: {
* total_rows: 31,
* last_page: 2,
* current_page: 1,
* limit: 25
* }
*/
FlipScroll.prototype.build_pager = function(paging)
{
var pages_remaining = paging.last_page - paging.current_page;
var step = Math.floor(this.pager_length/2);
this.start = 1;
this.end = this.pager_length;
// If we're at the first page.
if( paging.current_page == this.start ) {
if( pages_remaining >= this.pager_length ) {
this.end = this.pager_length;
}
else if( pages_remaining ) {
this.end = paging.current_page + pages_remaining;
}
else {
this.end = paging.current_page;
}
}
// If we're at the last page.
else if( pages_remaining == 0 ) {
this.end = paging.last_page;
this.start = this.end - (this.pager_length-1);
}
// If we're stuck in the middle.
else {
if( pages_remaining >= step ) {
this.end = paging.current_page + step;
}
else {
this.end = paging.last_page;
step += pages_remaining;
}
if( paging.current_page <= step ) {
this.start = 1;
this.end = paging.current_page + this.start + step;
}
else {
this.start = paging.current_page - step;
if( this.start == 0 ) {
this.start++;
this.end++;
}
}
}
// We shouldn't need this, but just to be safe let's make sure we don't show an invalid index.
if( this.start < 1 ) {
this.start = 1;
}
if( this.end > paging.last_page ) {
this.end = paging.last_page;
}
var showing_all = false;
if( !this.limit || this.limit == paging.total_rows ) {
showing_all = true;
this.limit = this.default_limit;
}
// Build our pager.
var active_css = 'active';
var pager = '<div class="pagination"><ul>';
if( !showing_all ) {
active_css = ( showing_all || this.start == this.end ) ? 'active' : '';
pager += '<li data-page="1" data-limit="'+paging.total_rows+'" class="all '+active_css+'"><a href="">All</a></li>';
active_css = ( showing_all || paging.current_page == 1 ) ? 'active' : '';
pager += '<li data-page="1" data-limit="'+this.limit+'" class="first '+active_css+'"><a href="">First</a></li>';
active_css = ( !showing_all && paging.current_page > 1 ) ? '' : 'active';
pager += '<li data-page="'+(paging.current_page-1)+'" data-limit="'+this.limit+'" class="prev '+active_css+'"><a href="">Prev</a></li>';
for( var i=this.start; i <= this.end; i++ ) {
active_css = ( !showing_all && i == paging.current_page ) ? 'active' : '';
pager += '<li data-page="'+i+'" data-limit="'+this.limit+'" class="numeric '+active_css+'"><a href="">'+i+'</a></li>';
}
active_css = ( !showing_all && paging.current_page != paging.last_page ) ? '' : 'active';
pager += '<li data-page="'+(paging.current_page+1)+'" data-limit="'+this.limit+'" class="next '+active_css+'"><a href="">Next</a></li>';
active_css = ( showing_all || (paging.current_page == paging.last_page) ) ? 'active' : '';
pager += '<li data-page="'+paging.last_page+'" data-limit="'+this.limit+'" class="last '+active_css+'"><a href="">Last</a></li>';
}
else {
pager += '<li data-page="'+this.last_page+'" data-limit="'+this.last_limit+'" class="back"><a href="">Back</a></li>';
}
pager += '</ul>'+
'</div>';
return pager;
};
/**
* Makes the ajax request to get the data and renders the table and paging controls.
*
*/
FlipScroll.prototype.load = function()
{
var self = this;
$.ajax({
url: 'https://'+location.hostname+self.action+'.json?page='+self.page+'&limit='+self.limit,
dataType: 'json',
accepts: 'json',
type: 'GET',
timeout: 60000,
error: function(xhr, ajaxOptions, throwError) {
//TODO: Figure out what to do in case we fail to load data.
$('#dialog').html('<p>' + $.parseJSON(xhr.responseText).errors.join('<br/>') + '</p>');
$('#dialog').dialog('open');
},
success: function(data, textStatus, jqXHR) {
// Build the pager.
var html = self.build_pager(data.paging);
// Build the table.
html += '<table class="table-bordered table-striped table-condensed cf">';
html += '<thead class="cf">';
html += '<tr>';
// Build table header.
// If we got a row header template callback use it.
if( self.header_template && (self.header_template instanceof Function) ) {
html += self.header_template(data.metadata);
}
// Else if we got a row template callback use it.
else if( self.header_template ) {
var current_header_template = self.header_template;
Object.keys(data.metadata).forEach(function(column) {
var regex = new RegExp('{'+column+'}' ,'g');
current_header_template = current_header_template.replace(regex, data.metadata[column]);
});
html += current_header_template;
}
// Otherwise, default to plain'ole table rows.
else {
html += '<tr>';
Object.keys(data.metadata).forEach(function(key) {
html += '<th>'+data.metadata[key]+'</th>';
});
html += '</tr>';
}
html += '</thead>';
html += '<tbody>';
// Get the row data so we can build our table body.
var rows = {};
if( self.data_key ) {
rows = data[self.data_key];
}
// Determine which collection holds our data.
else {
Object.keys(data).forEach(function(key) {
if( $.inArray(key, ['paging', 'metadata']) === -1 ) {
rows = data[key];
self.data_key = key;
}
});
}
var i = 0;
// Loop through the rows and build the table body.
$(rows).each(function() {
var row = this;
// If we got a row template callback use it.
if( self.row_template && (self.row_template instanceof Function) ) {
html += self.row_template(row, i);
}
// else if we got a row template callback use it.
else if( self.row_template ) {
var current_row_template = self.row_template;
var regex = null;
Object.keys(row).forEach(function(column) {
regex = new RegExp('{'+column+'}' ,'g');
current_row_template = current_row_template.replace(regex, row[column]);
});
html += current_row_template;
}
// Otherwise, default to plain'ole table rows.
else {
html += '<tr>';
Object.keys(row).forEach(function(column) {
html += '<td>'+row[column]+'</td>';
});
html += '</tr>';
}
i++;
});
html += '</tbody>';
html += '</table>';
// Update the table container.
$(self.flipscroll_container_selector).html(html);
}
});
};
FlipScroll.prototype.push_eventhandler = function(hander)
{
this.event_handlers.push(hander);
}
/**
* Binds the jQuery click event handler to the paging controls that it needs to.
*/
FlipScroll.prototype.bind = function()
{
var self = this;
// Make sure our inactive elements don't submit when clicked.
$(this.flipscroll_container_selector).on('click', '.pagination li.active', function(event) { return false; });
// Make sure our active elements submit when clicked.
$(this.flipscroll_container_selector).on('click', '.pagination li:not(.active)', function(event) {
var link = $(event.target).parent();
self.set_paging_options( window.location.pathname, link.data('page'), link.data('limit') );
self.load();
return false;
});
if( this.event_handlers.length ) {
for( var i=0, len=this.event_handlers.length; i < len; i++ ) {
if( this.event_handlers[i] instanceof Function ) {
this.event_handlers[i]();
}
else {
throw 'Event-handlers MUST be functions.';
}
}
}
};