-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathjquery.mobile.lazyloader.dev.js
1196 lines (804 loc) · 61.2 KB
/
jquery.mobile.lazyloader.dev.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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
( function( $, undefined ) {
$.widget( "mobile.lazyloader", $.mobile.widget, {
// Create some default options that can be extended in reinitialization
_defaultOptions : {
// threshold for how close to the bottom should we trigger a load of more items - default to height of viewport
'threshold' : $( window ).height(),
// this is the number of items to retrieve from server by default
'retrieve' : 20,
// this is the number of items retrieved so far
'retrieved' : 20,
// this is whether or not to display count bubbles in the retrieved list items
'bubbles' : false,
// this is for specifying an offset into the list in the case where the client is not in sync with server
'offset' : 0,
// this is for setting a limit on how many items can be lazy loaded
'limit' : 0
},
// the parameters enable user defined server variables to be posted along with the ajax call to get more items
_defaultParameters : {
// this is the number of items to retrieve from server by default
'retrieve' : 20,
// this is the number of items retrieved so far
'retrieved' : 20,
// this is for specifying an offset into the list in the case where the client is not in sync with server
'offset' : 0
},
// Create some default settings that can be extended in reinitialization
_defaultSettings : {
// The page id of the page on which the lazyloader widget instance will be running
"pageId" : "",
// The type of template to be used for transforming JSON to HTML
"templateType" : "",
// Flag to indicate whether or not the template has been pre-compiled
"templatePrecompiled" : false,
// The id of the script element for the current page on which the lazyloader has been initialized
"templateId" : "",
// The template to use for transformation to HTML
"template" : "",
// The id of the main wrapper element which the lazyloader widget instance will be lazy loading
"mainId" : "",
// The id of the DIV that should contain the animated gif to indicate more items are being loaded
"progressDivId" : "",
// The url of the server side resource to which the lazy loader AJAX call should be directed
"moreUrl" : "",
// The url of the server side resource responsible for clearing server-side session variables to maintain state
"clearUrl" : "",
// This will allow for cross-domain loading with JSONP - if false, lazyloader will use $.ajax POST
"JSONP" : false,
// This is the callback that the server resourse needs for wrapping the returned JSON
"JSONPCallback" : ""
},
// Create the default selectors that can be overridden during reinitialization
_defaultSelectors : {
// This is the selector of the main element (e.g. the <ul> in the case of a listview)
"main" : 'ul',
// This is the selector for a single element of things that are being lazyloaded (e.g. the <li> in the case of a listview)
"single" : 'ul li',
// This is the selector for the bottom element that may need to be removed and added back post lazyloading in certain cases
"bottom" : '[data-role="list-divider"]'
},
// Short circuit event toggles to prevent the handling of multiple events at the same time
_handleScrollStartJustFired : false,
_handleScrollStopJustFired : false,
_mouseWheelEventJustFired : false,
// Variables to store the id of the setTimeout of the short circuit event toggle that sets toggle back to false
_handleScrollStartTimeoutId : null,
_handleScrollStopTimeoutId : null,
_mouseWheelTimeoutId : null,
// This stores the _settings for the last time the widget instance was used by a particular page (keyed by pageId)
_instances : {},
// This stores the merged object containing _defaultOptions and any overriding options passed in
//options : null,
_moreOutstandingPageId : null,
// This stores the merged object containing _defaultParameters and any overriding options passed in
_parameters : null,
// This stores the merged object containing _defaultSettings and any overriding settings passed in
_settings : null,
// This stores the merged object containing _defaultSelectors and any overriding selectors passed in
_selectors : null,
// Timeout values used for varying some of the setTimeout values used in the widget
timeoutOptions : {
// Timeout to pass to load when it's called from the mousewheel handler
'mousewheel' : 350,
// Timeout to pass to load when it's called from the scrollstart handler
'scrollstart' : 500,
// Timeout to pass to load when it's called from the scrollstop handler
'scrollstop' : 50,
// this is the timeout for how quickly to show the loading more items progress indicator at bottom
'showprogress' : 200,
// this is the timeout for when there's a button to scroll down manually
'scrolldown' : 400,
// this is the timeout used for when user clicks into search filter or something
'immediately' : 0
},
// The name of the widget
_widgetName : "lazyloader",
// Object to contain two possible widget states
_widgetState : {
// whether or not we are already retrieving items from server
'busy' : false,
// this is to specify whether lazy loading is probably done, so we don't need to try anymore
'done' : false,
// this is to specify that the lazy loader has reached the limit of items that can be loaded
'limit' : false
},
// Runs automatically the first time this widget is called. Put the initial widget set-up code here.
_create : function( ) {
// Initialize the widget using the options passed in by the widget constructor
this._initialize( this._defaultOptions, this._defaultSettings, this._defaultParameters, this._defaultSelectors );
// Bind events that are needed by this widget
this._bind();
},
_init : function () {
// not used
},
_initialize : function( options, settings, parameters, selectors ) {
if ( ( typeof options != 'undefined' ) && ( options != '' ) ) {
this._widgetState.busy = false;
this._widgetState.done = false;
this._widgetState.limit = false;
// Get the defaultSettings and extend / merge / override them with user defined settings
this._settings = $.extend( true, this._settings, this._defaultSettings );
this._settings = $.extend( true, this._settings, settings );
if ( ( typeof this._settings.mainId !== 'undefined' ) && ( this._settings.mainId !== "") ) {
this._defaultSelectors.main = '#'+this._settings.mainId;
this._defaultSelectors.single = '#'+this._settings.mainId+' li';
this._defaultSelectors.bottom = '[data-role="list-divider"]';
}
if ( ( typeof this._settings.pageId !== 'undefined' ) && ( this._settings.pageId !== "") ) {
this._settings.totalHeight = $( "#"+this._settings.pageId ).height();
}
// Get the defaultSelectors and extend / merge / override them with user defined selectors
this._selectors = $.extend( true, this._selectors, this._defaultSelectors );
this._selectors = $.extend( true, this._selectors, selectors );
// Get the defaultParameters and extend / merge / override them with user defined parameters
this._parameters = $.extend( true, this._parameters, this._defaultParameters );
this._parameters = $.extend( true, this._parameters, parameters );
// Get any user defined settings and extend / merge / override them with defaultSettings
this.options = $.extend( true, this.options, this._defaultOptions );
this.options = $.extend( true, this.options, options );
// Get the pageId for the settings that were passed in by the user
var newPageId = settings.pageId;
// Make sure a pageId was passed in
if ( ( typeof newPageId != 'undefined ') && ( newPageId != '' ) ) {
// First check to see if we are already tracking an instance for the page being re-initialized before storing the defaults
if ( !this._instances[newPageId] ) {
// Only try to retrieve the template from the DOM if it has not already been set externally by the user
if ( ( typeof this._settings.template == 'undefined' ) || ( this._settings.template == '' ) ) {
// retrieve the template from the DOM so we can store it along with the instance
if ( ( typeof this._settings.templateId != 'undefined' ) && ( this._settings.templateId != '') ) {
// retrieve the template from the DOM
var template = $( "#"+this._settings.templateId ).html();
var templateType = "";
var templatePrecompiled = this._settings.templatePrecompiled;
if (( typeof this._settings.templateType != 'undefined' ) && ( this._settings.templateType != '') ) {
templateType = this._settings.templateType;
}
// Dust templates seem to be the only ones that can be pre-compiled at initialization and then loaded when needed at runtime
if ( ( templateType === "dust" ) && ( template !== "" ) && ( !templatePrecompiled ) ) {
// add the pre-compiled template to the settings object
this._settings.template = dust.compile( template, this._settings.templateId );
} else {
// add it to the settings object
this._settings.template = template;
}
}
}
// initialize a new object for this newPageId
this._instances[newPageId] = [];
// Store the merged options object as a new instance for later modifications and retrieval
this._instances[newPageId]['options'] = $.extend( true, {}, this.options );
// Store the merged settings object as a new instance for later retrieval
this._instances[newPageId]['settings'] = $.extend( true, {}, this._settings );
// Store the merged selectors object as a new instance for later retrieval
this._instances[newPageId]['selectors'] = $.extend( true, {}, this._selectors );
if ( this._instances[newPageId]['options'].limit > 0 ) {
// Check to make sure retrieved is less than limit, if it isn't then set limit to true and trigger an error
if ( this._instances[newPageId]['options'].limit < this._instances[newPageId]['options'].retrieved ) {
this._widgetState.done = true;
this._widgetState.limit = true;
message = "Limit must be greater than the number of items listed by default (i.e. 'retrieved' by default)";
this._triggerEvent( "error", "_load", message );
}
}
}
}
}
},
_bind : function () {
$( 'body' ).bind( "scrollstart", $.proxy( this._handleScrollStart, this ) );
$( 'body' ).bind( "scrollstop", $.proxy( this._handleScrollStop, this ) );
if ( /Firefox/i.test( navigator.userAgent ) ) {
$( window ).bind( "DOMMouseScroll", $.proxy( this._handleMouseWheelEvent, this ) );
} else {
if ( ( typeof this._selectors != 'undefined' ) && ( this._selectors != null) && ( this._selectors != '' ) ) {
if ( typeof this._selectors.main != 'undefined' ) {
if ( $( this._selectors.main ).attachEvent ) {
$( window ).bind( "onmousewheel", $.proxy( this._handleMouseWheelEvent, this ) );
} else {
$( window ).bind( "mousewheel", $.proxy( this._handleMouseWheelEvent, this ) );
}
}
}
}
// bind if the element is destroyed
//this.$element.bind( "destroyed", $.proxy( this._teardown, this ) );
},
_unbind : function () {
$( 'body' ).unbind( "scrollstart", this._handleScrollStart );
$( 'body' ).unbind( "scrollstop", this._handleScrollStop );
if ( /Firefox/i.test( navigator.userAgent ) ) {
$( window ).unbind( "DOMMouseScroll", this._handleMouseWheelEvent );
} else {
if ( ( typeof this._selectors != 'undefined' ) && ( this._selectors != null ) && ( this._selectors != '' ) ) {
if ( typeof this._selectors.main != 'undefined' ) {
if ( $( this._selectors.main ).attachEvent ) {
$( window ).unbind( "onmousewheel", this._handleMouseWheelEvent );
} else {
$( window ).unbind( "mousewheel", this._handleMouseWheelEvent );
}
}
}
}
},
destroy : function () {
// Unbind any events that were bound at _create
this._unbind();
// Null out all properties of this widget
this.options = null;
this.timeoutOptions = null;
this._settings = null;
this._parameters = null;
this._instances = null;
this._handleScrollStartJustFired = null;
this._handleScrollStopJustFired = null;
this._mouseWheelEventJustFired = null;
this._handleScrollStartTimeoutId = null;
this._handleScrollStopTimeoutId = null;
this._mouseWheelTimeoutId = null;
this._widgetState = null;
this._defaultOptions = null;
this._defaultSettings = null;
this._defaultParameters = null;
// For jQuery UI 1.8, destroy must be invoked from the base widget
// For jQuery UI 1.9, define _destroy instead and don't worry about calling the base widget
$.Widget.prototype.destroy.apply( this );
},
_check : function( threshold ) {
threshold = this.options.threshold || threshold;
var totalHeight, singleItemHeight, currentScroll, visibleHeight;
if ( document.documentElement.scrollTop ) {
currentScroll = document.documentElement.scrollTop;
} else {
currentScroll = document.body.scrollTop;
}
if ( this._instances[this._settings.pageId] ) {
totalHeight = this._instances[this._settings.pageId]['settings'].totalHeight;
singleItemHeight = this._instances[this._settings.pageId]['settings'].singleItemHeight;
} else {
totalHeight = this._settings.totalHeight;
singleItemHeight = this._settings.singleItemHeight;
}
// Uses the height of browser viewport
visibleHeight = $( window ).height();
return ( ( totalHeight - threshold ) <= ( currentScroll + visibleHeight ) );
},
// Main lazy loader function
_load : function( timeout ) {
if ( ( typeof this._settings.pageId != undefined ) && ( this._settings.pageId != '' ) ) {
// we only want to proceed with this function logic if the lazyloader is currently initialized for the active page
if ( $( '.ui-page-active' ).attr( 'id' ) == this._settings.pageId ) {
// Set the variable that can be used to make sure the outstanding request for more is for the same instance of the lazyloader
this._moreOutstandingPageId = this._settings.pageId;
// Save a reference to this that can be used inside the setTimeout callback
$that = this;
// Don't try to load anything until the scroll is given some time to get closer to the bottom
setTimeout( function() {
// make sure the plugin is not already lazy loading some items
if ( ( !$that._widgetState.busy ) && ( !$that._widgetState.done ) && ( !$that._widgetState.limit ) ) {
// Make sure the request for more is still for the current page instance of the lazyloader
// before wasting any time building the _parameters and query string and then making the request
if ( $that._moreOutstandingPageId == $that._settings.pageId ) {
// if the page scroll location is close to the bottom
if ( $that._check( $that.options.threshold ) || ( timeout === 0 ) ) {
$( "#"+$that._settings.progressDivId ).show( $that.timeoutOptions.showprogress, function() {
// Default the moreUrl to be the current instance
moreUrl = $that._settings.moreUrl;
var requestType = "POST";
var dataType = "json";
var postData = "";
// JSONP parameters
var JSONP = false;
var JSONPCallback = "";
var count = 0;
if ( $that._instances[ $that._settings.pageId ] ) {
$that._parameters.retrieve = $that._instances[ $that._settings.pageId ][ 'options' ].retrieve;
$that._parameters.retrieved = $that._instances[ $that._settings.pageId ][ 'options' ].retrieved;
$that._parameters.offset = $that._instances[ $that._settings.pageId ][ 'options' ].offset;
//alert( "retrieved + retrieve = "+( $that._instances[$that._settings.pageId]['options'].retrieved + $that._instances[ $that._settings.pageId ][ 'options' ].retrieve )+"\nlimit: "+$that._instances[$that._settings.pageId]['options'].limit );
if ( $that._instances[$that._settings.pageId]['options'].limit > 0 ) {
// If there is a limit set on the number of items that can be lazy loaded, we need to enforce it here
if ( ( $that._instances[$that._settings.pageId]['options'].retrieved + $that._instances[ $that._settings.pageId ][ 'options' ].retrieve ) >= $that._instances[$that._settings.pageId]['options'].limit ) {
if ( $that._instances[$that._settings.pageId]['options'].limit >= $that._instances[$that._settings.pageId]['options'].retrieved ) {
$that._parameters.retrieve = ( $that._instances[$that._settings.pageId]['options'].limit - $that._instances[$that._settings.pageId]['options'].retrieved );
} else {
$that._parameters.retrieve = 0;
}
}
}
if ( ( $that._instances[ $that._settings.pageId ][ 'settings' ].JSONP ) ) {
JSONP = true;
JSONPCallback = $that._instances[ $that._settings.pageId ][ 'settings' ].JSONPCallback;
}
} else {
$that._parameters.retrieve = $that.options.retrieve;
$that._parameters.retrieved = $that.options.retrieved;
$that._parameters.offset = $that.options.offset;
if ( $that.options.limit > 0 ) {
// If there is a limit set on the number of items that can be lazy loaded, we need to enforce it here
if ( ( $that.options.retrieve + $that.options.retrieved ) >= $that.options.limit ) {
if ( $that.options.limit >= $that.options.retrieve ) {
$that._parameters.retrieve = ( $that.options.limit - $that.options.retrieve );
} else {
$that._parameters.retrieve = 0;
}
}
}
}
if ( ( typeof $that._settings.pageId != 'undefined' ) && ( $that._settings.pageId != '' ) ) {
var hidden_inputs = $( "#"+$that._settings.pageId ).find( '[type="hidden"]' );
for( i=0; i<hidden_inputs.length; i++ ) {
var hidden_input = $(hidden_inputs).get(i);
if ( (typeof $( hidden_input ).attr( 'id' ) != 'undefined' ) && ( $( hidden_input ).attr( 'id' ) != '' ) ) {
$that._parameters[$( hidden_input ).attr( 'id' )] = escape( $( hidden_input ).val() );
}
}
}
if ( !JSONP ) {
for ( var key in $that._parameters ) {
if ( count == 0 ) {
postData += ( key + "=" + $that._parameters[key] );
} else {
postData += ( "&" + key + "=" + $that._parameters[key] );
}
count = count+1;
}
} else {
requestType = "GET";
dataType = "jsonp";
var JSONPParameters = "";
for ( var key in $that._parameters ) {
if (count == 0) {
JSONPParameters += '"' + key + '"' + ': "'+$that._parameters[key]+'"';
} else {
JSONPParameters += ', ' + '"' + key + '"' + ': "'+$that._parameters[key]+'"';
}
count = count+1;
}
// Create a JSON object out of the JSONParameters string
postData = $.parseJSON( "{ "+JSONPParameters+" }" );
}
//alert(JSON.stringify(postData));
$.ajax( {
type: requestType,
url: moreUrl,
dataType: dataType,
jsonpCallback: JSONPCallback,
data: postData,
success: function( data ){
more = data;
if ( typeof data === 'object' ) {
// we should be good then
} else {
try {
// it seems the response can also be received as a string even though we specified json as dataType
more = $.parseJSON( data );
} catch ( err ) {
// trigger an event to announce that an error occurred during the _load
$that._triggerEvent( "error", "_load", err.message );
$( "#"+$that._settings.progressDivId ).hide( 250, function() {
$that._widgetState.busy = false;
} );
return false;
}
}
try {
var count = more.data[0].count;
var html = "";
var json = "";
var template = "";
var templateId = "";
var templateType = "";
var templatePrecompiled = false;
var mainElementSelector = "";
var singleItemElementSelector = "";
var bottomElementSelector = "";
var $bottomElement = "";
if ( count > 0 ) {
mainElementSelector = $that._selectors.main;
singleItemElementSelector = $that._selectors.single;
bottomElementSelector = $that._selectors.bottom;
$bottomElement = $that._getBottomElement( mainElementSelector, bottomElementSelector );
if ( ( typeof more.data[0].html != 'undefined' ) && ( more.data[0].html != '' ) ) {
html = more.data[0].html;
if ( $bottomElement ) {
$( singleItemElementSelector ).last().before( html );
} else {
$( mainElementSelector ).append( html );
}
} else {
// Check to see if there is already an instance of this page in memory
if ( $that._instances[$that._settings.pageId] ) {
// If a templateId isn't set, then there's no need to do the other two checks
if ( ( typeof $that._instances[$that._settings.pageId]['settings'].templateId != 'undefined' ) && ( $that._instances[$that._settings.pageId]['settings'].templateId != '' ) ) {
templateId = $that._instances[$that._settings.pageId]['settings'].templateId;
if ( ( typeof $that._instances[$that._settings.pageId]['settings'].templateType != 'undefined' ) && ( $that._instances[$that._settings.pageId]['settings'].templateType != '' ) ) {
templateType = $that._instances[$that._settings.pageId]['settings'].templateType;
}
if ( ( typeof $that._instances[$that._settings.pageId]['settings'].template != 'undefined' ) && ( $that._instances[$that._settings.pageId]['settings'].template != '' ) ) {
template = $that._instances[$that._settings.pageId]['settings'].template;
}
}
templatePrecompiled = $that._instances[$that._settings.pageId]['settings'].templatePrecompiled;
} else { // This should never happen ... but, just in case
if ( ( typeof $that._settings.templateId != 'undefined' ) && ( $that._settings.templateId != '') ) {
templateId = $that._settings.templateId;
if ( ( typeof $that._settings.templateType != 'undefined' ) && ( $that._settings.templateType != '') ) {
templateType = $that._settings.templateType;
}
if ( ( typeof $that._settings.template != 'undefined' ) && ( $that._settings.template != '') ) {
template = $that._settings.template;
}
}
templatePrecompiled = $that._settings.templatePrecompiled;
}
// Just to make sure we got something
if ( ( templateType !== "" ) && ( templateId !== "" ) && ( template !== "" ) ) {
// First check to see if json2html is being used since it needs special handling
if ( templateType === "json2html" ) {
json = more.data[0].json;
// first make sure there was a bottom element to work around
if ( $bottomElement ) {
// we need to remove the last li if it's a divider so we can append the retrieved li items
$bottomElement.remove();
}
// Transform the retrieved json data into HTML using the transform template that was set at re-initialization for this page
$( mainElementSelector ).json2html( json, template );
// first make sure there was a list-divider
if ( $bottomElement ) {
// put the last li item back if it exists (it will exist if it was an list-divider)
$( singleItemElementSelector ).last().append( $bottomElement );
}
} else {
json = more.data[0];
switch( templateType ) {
case 'handlebars' :
if ( templatePrecompiled ) {
template = Handlebars.templates[templateId + '.tmpl']; // your template minus the .js
html = template( json );
} else {
template = Handlebars.compile( template );
html = template( json );
}
break;
case 'icanhaz' :
// Add the icanhaz template for this page
ich.addTemplate( "listitem", template );
// Convert the json record to HTML with icanhaz
html = ich.listitem( json, true );
// Clear the icanhaz cache
ich.clearAll();
break;
case 'dust' :
if ( templatePrecompiled ) {
// Should be no need to load the template source here since it's pre-compiled externally
dust.render( templateId, json, function( err, result ) {
// Append the item HTML onto the main HTML string
html = result;
} );
} else {
// Even if Dust templates aren't pre-compiled in an external script, they are still pre-compiled during initialization
dust.loadSource( template );
dust.render( templateId, json, function( err, result ) {
// Append the item HTML onto the main HTML string
html = result;
} );
}
break;
case 'dot' :
template = doT.template( template );
// Convert the json data to html with doT.js
html = template( json );
break;
default :
// Not sure if it makes sense to have a default here - we should probably raise an error instead
break;
}
// First check for the bottom element to see if we need to insert the html before it
if ( $bottomElement ) {
$( singleItemElementSelector ).last().before( html );
} else { // we can just append it
$( mainElementSelector ).append( html );
}
}
} else {
// raise an error
}
}
// Refresh the listview so it is re-enhanced by JQM
$( mainElementSelector ).listview( 'refresh' );
// initialize this to zero for now
var singleItemHeight = 0;
count = parseInt( count );
if ( $that._instances[$that._settings.pageId] ) {
var totalHeight = $that._instances[$that._settings.pageId]['settings'].totalHeight;
if ( typeof $that._instances[$that._settings.pageId]['settings'].singleItemHeight !== 'undefined' ) {
// retrieve the value of singleItemHeight for the current instance of the lazyloader
singleItemHeight = $that._instances[$that._settings.pageId]['settings'].singleItemHeight;
} else {
// We only need to calculate the singleItemHeight for the current instance of the lazyloader once
singleItemHeight = $( singleItemElementSelector ).first().next().height();
// let's store the singleItemHeight for later so we don't have to recalculate it every time
$that._instances[$that._settings.pageId]['settings'].singleItemHeight = singleItemHeight;
}
// Adjust the total height based on the number of items that were just lazyloaded
$that._instances[$that._settings.pageId]['settings'].totalHeight = ( totalHeight + ( singleItemHeight * count ) );
} else {
if ( typeof $that._settings.singleItemHeight !== 'undefined' ) {
// retrieve the value of singleItemHeight for the current instance of the lazyloader
singleItemHeight = $that._settings.singleItemHeight;
} else {
// We only need to calculate the singleItemHeight for the current instance of the lazyloader once
singleItemHeight = $( singleItemElementSelector ).first().next().height();
// let's store the singleItemHeight for later so we don't have to recalculate it every time
$that._settings.singleItemHeight = singleItemHeight;
}
$that._settings.totalHeight = ( $that._settings.totalHeight + ( $that._settings.singleItemHeight * count ) );
}
// Increment the stored retrieved count only by the number of items retrieved
$that._instances[$that._settings.pageId]['options'].retrieved += count;
//alert( "retrieved: "+$that._instances[$that._settings.pageId]['options'].retrieved+"\nlimit: "+$that._instances[$that._settings.pageId]['options'].limit );
if ( $that._instances[$that._settings.pageId]['options'].limit > 0 ) {
if ( $that._instances[$that._settings.pageId]['options'].retrieved == $that._instances[$that._settings.pageId]['options'].limit ) {
$that._widgetState.limit = true;
// trigger an event to announce that the lazyloader has reached the set limit
$that._triggerEvent( "limitreached", "_load" );
}
}
if ( ( count < $that._instances[$that._settings.pageId]['options'].retrieve ) || ( $that._instances[$that._settings.pageId]['options'].retrieve == "all" ) ) {
$that._widgetState.done = true;
// trigger an event to announce that the lazyloader is done loading this page
$that._triggerEvent( "alldone", "_load" );
}
} else {
$that._widgetState.done = true;
// trigger an event to announce that the lazyloader is done loading this page
$that._triggerEvent( "alldone", "_load" );
}
$( "#"+$that._settings.progressDivId ).hide( 250, function() {
$that._widgetState.busy = false;
} );
// trigger an event to announce that the lazyloader is done loading that chunk
$that._triggerEvent( "doneloading", "_load" );
} catch ( err ) {
// trigger an event to announce that an error occurred during the _load
$that._triggerEvent( "error", "_load", err.message );
$( "#"+$that._settings.progressDivId ).hide( 250, function() {
$that._widgetState.busy = false;
} );
return false;
}
},
error: function( msg ) {
// trigger an event to announce that an error occurred during the _load
$that._triggerEvent( "error", "_load", msg );
$( "#"+$that._settings.progressDivId ).hide( 250, function() {
$that._widgetState.busy = false;
} );
},
complete: function( msg ) {
// this might be useful for something someday
}
});
});
}
}
} else {
if ( $that._widgetState.done ) {
// trigger an event to announce that the lazyloader is done loading this page
$that._triggerEvent( "alldone", "_load" );
if ( $that._widgetState.limit ) {
// trigger an event to announce that the lazyloader has been limited and that limit has been reached
$that._triggerEvent( "limited", "_load" );
}
} else if ( $that._widgetState.busy ) {
// trigger an event to announce that the lazyloader is currently busy loading
$that._triggerEvent( "busy", "_load" );
} else {
// what happened?
}
}
}, timeout );
} else {
$( "#"+this._settings.progressDivId ).hide( 250, function() {
if ( typeof this._widgetState != 'undefined' ) {
this._widgetState.busy = false;
}
} );
}
}
},
_getBottomElement : function ( mainElementSelector, bottomElementSelector ) {
// we will be removing the last li if it's a divider, so we need to store it for later
var $bottomElement = $( mainElementSelector ).last().find( bottomElementSelector );
switch ( $bottomElement.length ) {
case 2 :
$bottomElement = $bottomElement.last();
break;
case 1 : // the assumption is that this must be the heading list-divider
case 0 : // if there aren't any list-dividers, then nothing to remove
default : // so, null it out so we know we don't need to remove anything
$bottomElement = null;
break;
}
// determine if there is a bottom element we need to worry about when appending the retrieved items
if ( ( typeof $bottomElement != 'undefined' ) &&
( $bottomElement != null ) &&
( $bottomElement != '' ) &&
( $bottomElement != 'null' ) ) {
return $bottomElement;
} else {
return false;
}
},
// Event Handlers
_handleMouseWheelEvent : function() {
if ( ( !this._mouseWheelEventJustFired ) && ( !this._handleScrollStopJustFired ) && ( !this._handleScrollStartJustFired ) ) {
this._mouseWheelEventJustFired = true;
this._load( this.timeoutOptions.mousewheel );
var $that = this;
this._mouseWheelTimeoutId = setTimeout( function() {
$that._mouseWheelEventJustFired = false;
}, 1000 );
}
},
_handleScrollStart : function() {
if ( ( !this._mouseWheelEventJustFired ) && ( !this._handleScrollStopJustFired ) && ( !this._handleScrollStartJustFired ) ) {
this._handleScrollStartJustFired = true;
this._load( this.timeoutOptions.scrollstart );
var $that = this;
this._handleScrollStartTimeoutId = setTimeout( function() {
$that._handleScrollStartJustFired = false;
}, 1200 );
}
},
_handleScrollStop : function() {
if ( ( !this._mouseWheelEventJustFired ) && ( !this._handleScrollStopJustFired ) && ( !this._handleScrollStartJustFired ) ) {
this._handleScrollStopJustFired = true;
this._load( this.timeoutOptions.scrollstop );
var $that = this;
this._handleScrollStopTimeoutId = setTimeout( function() {
$that._handleScrollStopJustFired = false;
}, 1200 );
}
},
loadMore : function ( timeout ) {
if ( timeout === 0 ) {