-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.clientPagination.js
626 lines (575 loc) · 28.1 KB
/
jquery.clientPagination.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
/*
if (typeof console == "undefined" || typeof console.log == "undefined") {
var console = {
log: function(){
//nitobi.Debug.log.call(arguments);
},
warn: function(){
},
info: function(){
},
error: function(){
}
};
}
*/
//addUrlParams() ?
$.addOtherLinks = function(hash){
var fragment = document.location.hash.split('#').reverse()[0];
var hrefQsObject = $.toQso(hash);
var qso = $.mergeQsParams(hrefQsObject, $.toQso(fragment));
var newHash = $.toQs(qso);
return newHash;
}
$.fn.addOtherLinks = function(){
$(this).each(function(){
if ($(this).is('a')) $(this).attr('href', '#'+$.addOtherLinks(decodeURIComponent($(this).attr('href').split('#')[1])));
if ($(this).is('select')){
$('option', this).each(function(){
$ (this).attr('value', '#'+$.addOtherLinks(decodeURIComponent($(this).attr('value').split('#')[1])));
}); l
}
});
return $(this);
}
$.fn.removeParam = function(param){
$(this).each(function(){
var hrefQs = $.toQso($(this).attr('href').split('#').reverse()[0]);
delete hrefQs[param];
hrefQs = decodeURIComponent($.toQs(hrefQs));
$(this).attr('href', $(this).attr('href').split('#')[0]+'#'+hrefQs);
});
return $(this);
}
$.fn.addParam = function(param){
$(this).each(function(){
var hrefQs = $(this).attr('href').split('#').reverse()[0];
hrefQs = $.mergeQsParams($.toQso(hrefQs), $.toQso(param));
$(this).attr('href', $(this).attr('href').split('#')[0]+'#'+$.toQs(hrefQs));
});
return $(this);
}
$.addToOthersLinks = function(action, $excluded){
var $links = $('.ui-history').not($excluded);
action = $.toQs(action);
var id = $.idsInQs(action)[0];
$.each($links, function(i, o){
if ($(this).is('a') == true){
var hrefQs = $(this).attr('href').split('#').reverse()[0] || (this).attr('href').split('?').reverse()[0] ;
var hrefQsObject = $.toQso(hrefQs);
var actionA = action.split('&');
hrefQs = $.toQs(hrefQsObject);
var hrefQsA = hrefQs.split('&');
var hrefQsAi = '', actionAi = '';
for (i in hrefQsA){
if (typeof hrefQsA[i] !== undefined) {
hrefQsAi = hrefQsA[i];
hrefQsAi = hrefQsAi.split('~')[0];
if (hrefQsAi == id) delete hrefQsA[i];
}
}
hrefQs = hrefQsA.join('&');
hrefQsObject = $.toQso(hrefQs);
var qso = $.mergeQsParams( action, hrefQsObject);
var newHash = $.toQs(qso, '#');
$(this).attr('href', newHash);
}
if ($(this).is('select')){
$('option', this).each(function(){
var hrefQs = $(this).val().split('#').reverse()[0];
var hrefQsObject = $.toQso(hrefQs);
var actionA = action.split('&');
hrefQS = $.toQs(hrefQsObject);
var hrefQsA = hrefQs.split('&');
var hrefQsAi = '', actionAj = '';
for (i in hrefQsA){
if (typeof hrefQsA[i] !== undefined) {
hrefQsAi = hrefQsA[i];
hrefQsAi = hrefQsAi.split('~')[0];
if (hrefQsAi == id) delete hrefQsA[i];
}
}
hrefQs = hrefQsA.join('&');
hrefQsObject = $.toQso(hrefQs);
var qso = $.mergeQsParams( action, hrefQsObject);
var newHash = $.toQs(qso);
$(this).val(newHash);
});
}
});
}
var isLoading = false;
$.fn.clientPagination = function(options){
var $tpl_pager_default = $('<div class="pagination">default'
+'<a href="index.html?previous=true" class="previous">previous</a>'
+'<span class="pages"></span>'
+'<a href="index.html?next=true" class="next">next</a>'
+'<span class="limit">'
+'<a href="index.html?limit=5">5</a>'
+'<a href="index.html?limit=10">10</a>'
+'<a href="index.html?limit=50">50</a>'
+'<a href="index.html?limit=100">100</a>'
+'</span>'
+'total post count<span class="totalCount"></span>'
+'</div>');
var defaults = {};
defaults.limit = 10; //todo : at init count number of rows provided serverside
defaults.offset = 0;
defaults.startpoints = 2;
defaults.endpoints = 2;
defaults.transition = 'undefined';
function updateNav(relevantHash, lastHash){
var id = $(this).attr('id');
var opts = $(this).data('opts');
var $nav = $('#'+id).next('.ui-paginated-nav');
var $pagers = $('.ui-pager', $('#'+id).next('.ui-paginated-nav'));
var relevantHashObj = $.toQso(relevantHash);
//what's the heck here ? it should be based on the relevantHash none of fragment or sfragment are needed anymore
//var fragment = $.parseQuery(window.location.hash.split('#')[1]);
//var sfragment = (typeof document.location.href.split('#')[1] == 'undefined')? {} : $.toQso(document.location.href.split('#')[1]);
var offset = (typeof relevantHashObj[id+'~offset'] !== 'undefined') ? parseInt(relevantHashObj[id+'~offset']) : opts.offset;
var limit = (typeof relevantHashObj[id+'~limit'] !== 'undefined') ? parseInt(relevantHashObj[id+'~limit']) : opts.limit;
var postId = (typeof relevantHashObj[id+'~postId'] !== 'undefined') ? relevantHashObj[id+'~postId'] : false;
var history = $(this).data('history') || [];
if (id+'~postId' in relevantHashObj) {
$pagers.hide();
if (typeof history[history.length-2] != 'undefined' && history[history.length-2] != '') var backParam = history[history.length-2];
else{
var backParam = {};
backParam[id+'~limit'] = opts.limit;
backParam[id+'~offset'] = opts.offset;
}
if ($('.ui-pager-back', $nav).length < 1) $('<a class="ui-pager-back ui-history" href="#">back</a>').addOtherLinks().removeParam(id+'~postId').addParam($.toQs(backParam)).data('action', $.toQs(backParam)).hide().appendTo($nav);//todo switch to transitions
$('.ui-pager-back', $nav).show();
$('.ui-pager-back', $nav).click(function(){
$.history.load($(this).attr('href').split('#')[1]);
$.transition.replace({'state': 'start', 'trigger': $.domToSelector($(this)), 'toReplace': '#'+id, 'action': $(this).data('action'), 'transition': opts.transition});
return false;
});
} else {
$pagers.show();
$('.ui-pager-back', $nav).hide();
}
var postcount = parseInt($('.count', $pagers).html());
var next = (offset + limit >= postcount) ? null : offset + limit;
var previous = (offset > 0) ? offset-limit+'' : null;
if (previous < 0) previous = 0;
var qsObject = $.decodeURIComponentArray($.parseUri()['queryKey']);
console.warn('function updateNav - relevantHash', relevantHash, ' - id :', id, ' - offset : ', offset, ' - limit : ', limit, ' - next : ', next, ' - previous : ', previous);
//startpoints and endpoints (not sure of the vocabulary I'm using here)
var startpoints = opts.startpoints, startpoint=[];
var chunks = Math.ceil(postcount/limit);
if (opts.allpoints) startpoints = chunks; //allpoints means that a full bar of links should be there. so lets ask for as much startpoints as chunks and call it allpoints.
if (startpoints > chunks) startpoints = chunks;
for (var i=0; i<startpoints; i++){
startpoint[i] = (limit*i >= postcount) ? false : limit*i;
}
var endpoints = opts.endpoints, endpoint=[];
var lastChunkSize = postcount - Math.floor(postcount/limit)*limit;
if (endpoints > (chunks-startpoints)) endpoints = (chunks-startpoints);//excludes endpoints already referenced as startpoints
for (var i=0; i<endpoints; i++){
endpoint[i] = (limit*(i-1) >= postcount-lastChunkSize) ? false : (postcount-lastChunkSize) - limit*i;
}
$pagers.each(function(){
var $pager = $(this), action = {}, thislimit = id+'~limit', thisoffset = id+'~offset';
action[thisoffset] = offset;
action[thislimit] = limit;
//needs refactoring for more unobtrusive behaviour (i.e let the user provide in the html his own wrappers for the links)
var $startpointsHolder = (opts.allpoints)? $('.allpoints', $pager) : $('.startpoints', $pager);
var startpointshtml = '';
for (var i=0; i<startpoints; i++){
action[thisoffset] = startpoint[i];
if (startpoint[i] == offset) startpointshtml += '<span class="ui-paginated-current">'+(i+1)+'</span>';
else startpointshtml += '<a href="#'+$.toQs(action)+'" class="ui-history">'+(i+1)+'</a>';
}
var $startpoints = $(startpointshtml);
$startpoints.each(function(i,o){
action[thisoffset] = startpoint[i];
if ($(this).is('a')){
$(this).addClass('ui-history').addOtherLinks().data('action', $.toQs(action)).unbind("click").click( function(){
$.history.load($(this).attr('href').split('#')[1]);
$.transition.replace({'state': 'start', 'trigger': $.domToSelector($(this)), 'toReplace': '#'+id, 'action': $(this).data('action'), 'transition': opts.transition});
return false;
});
}
});
var endpointshtml = '';
var remainingChunks = chunks;
for (var i=0; i<endpoints; i++){
console.warn('endpoint[i]', endpoint, endpoint[i]);
action[thisoffset] = endpoint[i];
if (endpoint[i] == offset) endpointshtml = '<span class="ui-paginated-current">'+remainingChunks+'</span>'+endpointshtml;
else endpointshtml = '<a href="#'+$.toQs(action)+'" class="ui-history">'+remainingChunks+'</a>'+endpointshtml;
remainingChunks--;
}
var $endpoints = $(endpointshtml);
if ($endpoints.get(0) != document) {
$endpoints.each(function(i,o){
action[thisoffset] = startpoint[i];
if ($(this).is('a')){
$(this).addClass('ui-history').addOtherLinks().data('action', $.toQs(action)).unbind("click").click( function(){
$.history.load($(this).attr('href').split('#')[1], '$endpoints btn');
console.log('click endpoint : action : ', $(this).data('action'));
$.transition.replace({'state': 'start', 'trigger': $.domToSelector($(this)), 'toReplace': '#'+id, 'action': $(this).data('action'), 'transition': opts.transition});
return false;
});
}
});
}
if ($startpoints.length) $startpointsHolder.empty().append($startpoints);
$('.endpoints', $pager).empty();
if ($endpoints.get(0) != document) $('.endpoints', $pager).empty().append($endpoints);//append only real nodes
var $next = $('a.next', $pager);
if (next !== null) {
if ($('a.next', $pager).length == 0){
$next = $('<a href="'+window.location.href.split('#')[0]+'" class="next ui-history">next</a>');
if ($('a.previous', $pager).length) $next.insertAfter($('a.previous', $pager));
else $next.prependTo($pager);
}
$('a.next:hidden', $pager).show();
action[thisoffset] = next;
var hrefQs = $next.attr('href').split('#')[1] || decodeURIComponent($next.attr('href').split('?')[1]);
var hrefQsObject = $.decodeURIComponentArray($.parseQuery(hrefQs));
var newHash = $.toQs($.mergeQsParams( action, hrefQsObject), '#');
$next.addClass('ui-history').attr('href', newHash).data('action', $.toQs(action)).unbind("click").click( function(){
console.warn('click (1) : $(this).attr(\'href\').split(\'#\')[1]',$(this).attr('href').split('#')[1],'newHash : ', newHash, $(this), ' - document.location.href.split(#)[1] : ', document.location.href.split('#')[1], ' - $.toQs(qsObject).split(?).reverse()[0] : ', $.toQs(qsObject).split('?').reverse()[0], 'qsObject : ', qsObject, '- $.parseUri()[queryKey] : ', $.parseUri()['queryKey'][0], $.parseUri()['queryKey'][1]);
$.history.load($(this).attr('href').split('#')[1]);
$.transition.replace({'state': 'start', 'trigger': $.domToSelector($(this)), 'toReplace': '#'+id, 'action': $(this).data('action'), 'transition': opts.transition});
return false;
});
}else $next.hide();
var $previous = $('a.previous', $pager);
if (previous !== null) {
console.warn(') previous', previous, thisoffset);
if ($('a.previous', $pager).length == 0) {
$previous = $('<a href="' +decodeURIComponent(window.location.href.split('#')[0])+'#" class="previous ui-history">previous</a>');
if ($('a.next', $pager).length) $previous.insertBefore($('a.next', $pager));
else $previous.prependTo($($pager));
}
action[thisoffset] = previous;
var phrefQs = $previous.attr('href').split('#')[1] || $previous.attr('href').split('?')[1] || '';
var phrefQsObject = $.toQso(phrefQs);
var pnewHash = $.toQs($.mergeQsParams( action, phrefQsObject), '#');
$previous.addClass('ui-history').attr('href', pnewHash).data('action', $.toQs(action)).unbind("click").click( function(e){
$.history.load($(this).attr('href').split('#')[1], 'previous');
$.transition.replace({'state': 'start', 'trigger': $.domToSelector($(this)), 'toReplace': '#'+id, 'action': $(this).data('action'), 'transition': opts.transition}); //need to pass an identifier linking start and ready states in the same event occurence. hmm toReplace: '#posts' ???
return false;
});
$('a.previous:hidden', $pager).show();
}else $previous.hide();
if (typeof offset != 'undefined') action[thisoffset] = offset;
else delete action[thisoffset];
$('form.limit select option', $pager).each(function(){
$(this).each(function(){
var lvalues = $(this).attr('value').split('?').reverse()[0];
lvalues = lvalues.split('#').reverse()[0];
var lvalueQsObject = $.decodeURIComponentArray($.parseQuery(lvalues));
action[thislimit] = lvalueQsObject[thislimit];
lvalueQsObject[thisoffset] = action[thisoffset];
$(this).attr('value', $.toQs(lvalueQsObject)).data('action', $.toQs(action));
});
});
$('form.limit select', $pager).addClass('ui-history').unbind("change").change(function(){
console.warn('form.limit select $(this).val()', $(this), $(this).val().split('#').reverse()[0], '$(option:selected, this).data(action)', $('option:selected', this).data('action'), '$.addOtherLinks($(option:selected, this).data(action)', $.addOtherLinks($('option:selected', this).data('action')));
$.history.load($(this).val().split('#').reverse()[0]);
window.location.hash = '#'+$.addOtherLinks($('option:selected', this).attr('value'));
$.transition.replace({'state': 'start', 'trigger': $.domToSelector($(this)), 'toReplace': '#'+id, 'action': $('option:selected', this).data('action'), 'transition': opts.transition});
return false;
});
$('.limit a', $pager).each(function(){
var lhrefQs = $(this).attr('href').split('#')[1] || decodeURIComponent($(this).attr('href').split('?')[1]);
var lhrefQsObject = $.decodeURIComponentArray($.parseQuery(lhrefQs));
action[thislimit] = lhrefQsObject[thislimit];
$(this).addClass('ui-history').attr('href', '#'+$.toQs(action) ).addOtherLinks().data('action', $.toQs(action)).unbind("click").click(function(){
$.history.load($(this).attr('href').split('#').reverse()[0]);
$.transition.replace({'state': 'start', 'trigger': $.domToSelector($(this)), 'toReplace': '#'+id, 'action': $(this).data('action'), 'transition': opts.transition});
return false;
});
});
console.log('END updateNav : next', next);
//$('.totalCount', $pager).empty().text(postcount);
});
}
function display(posts, action){
var $paginated = $(this);
var id = $paginated.attr('id');
//console.log('display : posts :;', posts.length, 'action : ', action, '$paginated : ', $paginated, '- toReplace : ', '#'+id);
var opts = $paginated.data('opts');
var $newPaginated = $paginated.clone(true), i = 0;
var $list = $newPaginated.clone(true);
$newPaginated.html('');
var $tpl = {};
var isPost = false;
var actionObj = $.toQso(action);
$.each(actionObj, function(k,v){
console.log('k', k);
if (k.split('~')[1] == 'postId'){
isPost = true;
}
});
$.each(posts, function(k, v){
$tpl = $('li:first', $list).clone();
i++;
$.each(opts.definition.fieldDefinition, function(key, val){
if (typeof v[key] !== 'undefined'){
//console.log('- display loop loop - key :', typeof key, key, ' - val :', typeof val, val, ' - v[key] : ', typeof v[key], v[key]);
$('.'+key, $tpl).html(v[key]);
}
});
$('a', $tpl).remove();
if (!isPost) $tpl.append('<a href="index.html?'+id+'~postId=' + v.id + '">more</a>');
$('.id', $tpl).html(v.id);
$('.i', $tpl).html(i);
$newPaginated.append($tpl);
//console.log('display loop end');
});
$newPaginated.data('opts', opts);
return $newPaginated;
}
//add defaults params of an element if this element is not represented in the string
function defaultize(qs){
if (typeof qs == 'string') qs = $.toQso(qs);
if (typeof qs == 'undefined' || $.objectIsEmpty(qs) ) qs = {};
var allIds = [];
$('.ui-paginated-enabled').each(function(){
allIds.push($(this).attr('id'));
});
var referencedIds = $.idsInQs(qs);
var missingIds = $.array_diff(allIds, referencedIds);
var opts = {}, newParams = {};
$.each(missingIds, function(i, id){
opts = $('#'+id).data('opts');
newParams[id+'~offset'] = opts.offset;
newParams[id+'~limit'] = opts.limit;
});
qs = $.mergeQsParams(qs, newParams);
return qs;
}
function onPageLoad(hash, state, pid, init){
//console.warn('onPageLoad - START - arguments : ', arguments, 'isLoading', isLoading, this, arguments.callee, arguments.caller);
if (state != 'start') if (isLoading) {
console.warn('KILLED !!!. prevented double loading after a click');
return; //prevents double loading after a click. dirty workaround. Ihis is a strange behavior from the history plugin.
}
isLoading = true;
var thisFunction = arguments.callee;
if (!thisFunction.count) thisFunction.count = 0;
++thisFunction.count;
console.warn('onPageLoad - START - thisFunction.count ', thisFunction.count, 'hash', hash );
if (thisFunction.count == 1 && state == 'start' && init == 'init'){
--thisFunction.count;
console.log('KILLED !!! - history init - break now');
isLoading = false;
return;
}
var ids = [], id = ''; // list of the ids of the element to be paginated - we deduce them from the hash, or if there is no hash yet (initial state) then it comes from the params of the history plugin
if (typeof hash == 'undefined') var hash = '';
var defaultHash = false;
var hashObj = $.toQso(hash);
hash = $.toQs(hashObj);
var historyLength = $.fn.clientPagination.data.history.length;
//console.info('onPageLoad - arguments ', arguments, 'this', this, 'historyLength : ', historyLength, '$.fn.clientPagination.data.history[historyLength-1]', $.fn.clientPagination.data.history[historyLength-1], $.fn.clientPagination.data.history);
var last = $.fn.clientPagination.data.history[historyLength-1] || {'hash':''};
if (thisFunction.count != 1){
var defaultizedLastHash = defaultize(last.hash);
var paramsInDLH = {};
$.each(defaultizedLastHash, function(k,v){
id = k.split('~')[0];
if (typeof defaultizedLastHash[id+'~postId'] !== 'undefined') {
delete defaultizedLastHash[id+'~limit'];
delete defaultizedLastHash[id+'~offset'];
}
});
var defaultizedHash = defaultize(hash);
console.log('defaultizedLastHash : ', defaultizedLastHash, $.toQs(defaultizedLastHash), '- defaultizedHash : ', defaultizedHash, $.toQs(defaultizedHash));
var relevantHashObj = $.objectDiff(defaultizedHash, defaultizedLastHash );
console.log('relevantHashObj is : ', relevantHashObj, $.toQs(relevantHashObj), 'instead of : ', $.objectDiff(hash, last.hash));
var idsInQs = $.idsInQs(relevantHashObj);
$.each(idsInQs, function(i, id){
if (typeof relevantHashObj[id+'~offset'] != 'undefined' && typeof relevantHashObj[id+'~limit'] == 'undefined'){
relevantHashObj[id+'~limit'] = defaultizedHash[id+'~limit'];
}
if (typeof relevantHashObj[id+'~limit'] != 'undefined' && typeof relevantHashObj[id+'~offset'] == 'undefined'){
relevantHashObj[id+'~offset'] = defaultizedHash[id+'~offset'];
}
});
console.log('relevantHashObj is : ', relevantHashObj, 'after adding "companion values"');
}
else var relevantHashObj = $.objectDiff(hash, last.hash);
var relevantHash = $.toQs(relevantHashObj);
$.each(relevantHashObj, function(k,v){
id = k.split('~')[0];
if (state == 'start' && pid == id) if ($.inArray(id, ids) == -1) ids[ids.length] = id;
if (state != 'start') {
if (state == 'check') if ($.inArray(id, ids) == -1) ids[ids.length] = id;
if (state == 'load') {
if ($.inArray(id, ids) == -1) {
ids[ids.length] = id;
}
}
else if (pid == id) if ($.inArray(id, ids) == -1) ids[ids.length] = id;
}
});
var idsLength = ids.length;
console.log('ids ---.->', ids, ids.length, relevantHash, 'thisFunction.count', thisFunction.count);
//initial load, lets rewrite links in the pagers
if (thisFunction.count == 1){
$('#'+pid).each(function(){
$.fn.clientPagination.data.ids.push($(this).attr('id'));
var id = $(this).attr('id'), $a = $('a', $(this)), $select = $('select', $(this));
//pas if hash est rien mais si
if (hash == ''){
updateNav.call(this, relevantHash);// $(this).next('.ui-pager'));
$a.addClass('ui-history').each(function(){
var ahref = $(this).attr('href').split('?')[1] || $(this).attr('href').split('#')[1];
ahref = decodeURIComponent(ahref);
$(this).attr('href', '#'+ahref).data('action', ahref).addOtherLinks().unbind("click").click( function(e){
$.history.load($(this).attr('href').split('#')[1]);
if ($.transition) $.transition.replace({'state': 'start', 'toReplace': '#'+id, 'action': $(this).data('action'), 'transition': opts.transition});
return false;
});
});
var history = [];
var action = $.paramsById(defaultize(relevantHashObj), id);
history.push(action);
$(this).data('history', history);
console.warn('onPageLoad - END 0 - isLoading', isLoading);
isLoading = false;
}
else{
console.warn('onPageLoad - NOT END 0 before loadeach ', $(this).attr('id'));
if (ids.length > 0) loadEachId();
else {
var $a = $('a', $(this));
$a.addClass('ui-history').each(function(){
var ahref = $(this).attr('href').split('?')[1] || $(this).attr('href').split('#')[1];
ahref = decodeURIComponent(ahref);
$(this).attr('href', '#'+ahref).data('action', ahref).addOtherLinks().unbind("click").click( function(e){
$.history.load($(this).attr('href').split('#')[1]);
if ($.transition) $.transition.replace({'state': 'start', 'toReplace': '#'+id, 'action': $(this).data('action'), 'transition': opts.transition});
return false;
});
});
updateNav.call($(this), relevantHash);
$.addToOthersLinks(relevantHash, $('#'+id+' a').add($('select, a', $('#'+id).next('.ui-paginated-nav')) ) );//$.paramsById(relevantHashObj, id));
}
console.warn('onPageLoad - NOT END 0 if (defaultHash) is false ', $(this).attr('id'));
}
});
}
else if (ids.length == 0){
console.warn('onPageLoad - END 1- ids.length == 0 - isLoading', isLoading);
isLoading = false;
}
else{
loadEachId();
}
function loadEachId(){
console.info('onPageLoad - $.each(ids, function(i, id){ hash', hash, hash.toString(), 'relevantHash', relevantHash, 'ids', ids);
$.each(ids, function(i, id){
var $paginated = $('#'+id);
var opts = $paginated.data('opts');
var postcount = parseInt($('.postcount', $paginated).html());
var postId = hashObj[id+'~postId'] || '';
var offset = hashObj[id+'~offset'] || 0;
var limit = hashObj[id+'~limit'] || 10;
var $toDisplay = '';
var action = $.paramsById(relevantHashObj, id);
var history = $paginated.data('history') || [];
$.each(history, function(i){
history[i] = $.cleanData(this);
});
if ($.toQs(action) == $.toQs(history[history.length-1])) {
isLoading = false;//SAME STATE AS BEFORE, DO NOT LOAD
}
else{
console.warn('DIFFERENT STATE go load it');
history.push(action);
if (postId != '') {
window[id+'ById'].async(function(_return){
$toDisplay = display.call($paginated, _return, relevantHash, $paginated.data('opts'));
history[history.length-1] = $.cleanData(history[history.length-1]);
$toDisplay.data('history', history);
var $a = $('a', $toDisplay);
$a.addClass('ui-history').each(function(){
var ahref = $(this).attr('href').split('?')[1] || $(this).attr('href').split('#')[1];
$(this).attr('href', '#'+ahref).data('action', ahref).addOtherLinks().unbind("click").click( function(e){
$.history.load($(this).attr('href').split('#')[1]);
if ($.transition) $.transition.replace({'state': 'start', 'toReplace': '#'+id, 'action': $(this).data('action'), 'transition': opts.transition});
return false;
});
});
updateNav.call($paginated, relevantHash);
$.addToOthersLinks(action, $('#'+id+' a, #'+id+' select').add($('select, a', $('#'+id).next('.ui-paginated-nav')) ));
if ($.transition) $.transition.replace({'state': 'ready', 'toReplace': '#'+id, '$replacement': $toDisplay, 'action': relevantHash, 'transition': opts.transition});
//else $paginated.replaceWith($newPaginated);
if (i == idsLength - 1) {
$.fn.clientPagination.data.history.push({'hash': hash, 'relevantHash':relevantHash});
isLoading = false;
}
},
postId);
}
else {
window[id].async(function(_return){
console.warn('$paginated', $paginated, '_return', _return, 'relevantHash', relevantHash,'$paginated.data(opts)', $paginated.data('opts'));
$toDisplay = display.call($paginated, _return, relevantHash, $paginated.data('opts'));
history[history.length-1] = $.cleanData(history[history.length-1] );
$toDisplay.data('history', history);
var $a = $('a', $toDisplay);
$a.addClass('ui-history').each(function(){
var ahref = $(this).attr('href').split('?')[1] || $(this).attr('href').split('#')[1];
$(this).attr('href', '#'+ahref).data('action', ahref).addOtherLinks().unbind("click").click( function(e){
$.history.load($(this).attr('href').split('#')[1]);
if ($.transition) $.transition.replace({'state': 'start', 'toReplace': '#'+id, 'action': $(this).data('action'), 'transition': opts.transition});
return false;
});
});
updateNav.call($paginated, relevantHash);
$.addToOthersLinks(action, $('#'+id+' a, #'+id+' select').add($('select, a', $('#'+id).next('.ui-paginated-nav')) ));//$.paramsById(relevantHashObj, id));
if (typeof opts != 'undefined'){
if ($.transition) $.transition.replace({'state': 'ready', 'toReplace': '#'+id, '$replacement': $toDisplay, 'action': relevantHash, 'transition': opts.transition});
else $paginated.replaceWith($toDisplay);
}
if (i == idsLength - 1) {
$.fn.clientPagination.data.history.push({'hash': hash, 'relevantHash':relevantHash});
isLoading = false;
}
console.warn('onPageLoad - END 2 - isLoading', isLoading, ' - i', i, 'id', id, 'idsLength', idsLength,'- relevantHashObj', $.toQs($.paramsById(relevantHashObj, id)));
},
offset, limit);
}
}
});
}
}
var thisPlugin = arguments.callee; //run at initial load of the page
if (!thisPlugin.count) thisPlugin.count = 0;
++thisPlugin.count;
$(this).addClass('ui-paginated').addClass('ui-paginated-enabled');
$(this).each(function(){
var id = $.domToSelector(this).replace(/#/, '');
if ($(this).attr('id') != id) $(this).attr('id', id);
var opts = $.extend(defaults, options);
//todo add code for tables, (and divs, spans, etc)
if ($(this).is('ul')){
opts.definition = {'tableName': id, 'fieldDefinition': {} };
$('li:first>*', this).each(function(i){
var klass = $(this).attr('class').split(' ').slice(-1);
if (klass != 'id' && klass != 'i' ) opts.definition.fieldDefinition[klass] = '';
});
}
$(this).data('opts', opts);
if (thisPlugin.count == 1 && $.history) { //
console.warn('history inited with $.history.init(onPageLoad)');
$.history.init(onPageLoad, 'start', $(this).attr('id'));
}
console.warn('plugin calls onPageLoad- hash : ', document.location.href.split('#')[1], 'state : start - id :', id);
onPageLoad(document.location.href.split('#')[1], 'start', id);
});
}
$.fn.clientPagination.data = {};
$.fn.clientPagination.data.history = [];
$.fn.clientPagination.data.ids = [];
$.fn.clientPagination.data.pagers = [];
$.fn.clientPagination.data.options = {};