-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathjquery.mobile.lazyloader.dev.min.js
30 lines (30 loc) · 14.7 KB
/
jquery.mobile.lazyloader.dev.min.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
(function(b,r){b.widget("mobile.lazyloader",b.mobile.widget,{_defaultOptions:{threshold:b(window).height(),retrieve:20,retrieved:20,bubbles:!1,offset:0,limit:0},_defaultParameters:{retrieve:20,retrieved:20,offset:0},_defaultSettings:{pageId:"",templateType:"",templatePrecompiled:!1,templateId:"",template:"",mainId:"",progressDivId:"",moreUrl:"",clearUrl:"",JSONP:!1,JSONPCallback:""},_defaultSelectors:{main:"ul",single:"ul li",bottom:'[data-role="list-divider"]'},_handleScrollStartJustFired:!1,_handleScrollStopJustFired:!1,
_mouseWheelEventJustFired:!1,_handleScrollStartTimeoutId:null,_handleScrollStopTimeoutId:null,_mouseWheelTimeoutId:null,_instances:{},_moreOutstandingPageId:null,_parameters:null,_settings:null,_selectors:null,timeoutOptions:{mousewheel:350,scrollstart:500,scrollstop:50,showprogress:200,scrolldown:400,immediately:0},_widgetName:"lazyloader",_widgetState:{busy:!1,done:!1,limit:!1},_create:function(){this._initialize(this._defaultOptions,this._defaultSettings,this._defaultParameters,this._defaultSelectors);
this._bind()},_init:function(){},_initialize:function(a,d,c,e){if("undefined"!=typeof a&&""!=a&&(this._widgetState.busy=!1,this._widgetState.done=!1,this._widgetState.limit=!1,this._settings=b.extend(!0,this._settings,this._defaultSettings),this._settings=b.extend(!0,this._settings,d),"undefined"!==typeof this._settings.mainId&&""!==this._settings.mainId&&(this._defaultSelectors.main="#"+this._settings.mainId,this._defaultSelectors.single="#"+this._settings.mainId+" li",this._defaultSelectors.bottom=
'[data-role="list-divider"]'),"undefined"!==typeof this._settings.pageId&&""!==this._settings.pageId&&(this._settings.totalHeight=b("#"+this._settings.pageId).height()),this._selectors=b.extend(!0,this._selectors,this._defaultSelectors),this._selectors=b.extend(!0,this._selectors,e),this._parameters=b.extend(!0,this._parameters,this._defaultParameters),this._parameters=b.extend(!0,this._parameters,c),this.options=b.extend(!0,this.options,this._defaultOptions),this.options=b.extend(!0,this.options,
a),a=d.pageId,"undefined "!=typeof a&&""!=a&&!this._instances[a])){if(("undefined"==typeof this._settings.template||""==this._settings.template)&&"undefined"!=typeof this._settings.templateId&&""!=this._settings.templateId)d=b("#"+this._settings.templateId).html(),c="",e=this._settings.templatePrecompiled,"undefined"!=typeof this._settings.templateType&&""!=this._settings.templateType&&(c=this._settings.templateType),this._settings.template="dust"===c&&""!==d&&!e?dust.compile(d,this._settings.templateId):
d;this._instances[a]=[];this._instances[a].options=b.extend(!0,{},this.options);this._instances[a].settings=b.extend(!0,{},this._settings);this._instances[a].selectors=b.extend(!0,{},this._selectors);0<this._instances[a].options.limit&&this._instances[a].options.limit<this._instances[a].options.retrieved&&(this._widgetState.done=!0,this._widgetState.limit=!0,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(){b("body").bind("scrollstart",b.proxy(this._handleScrollStart,this));b("body").bind("scrollstop",b.proxy(this._handleScrollStop,this));/Firefox/i.test(navigator.userAgent)?b(window).bind("DOMMouseScroll",b.proxy(this._handleMouseWheelEvent,this)):"undefined"!=typeof this._selectors&&null!=this._selectors&&""!=this._selectors&&"undefined"!=typeof this._selectors.main&&(b(this._selectors.main).attachEvent?b(window).bind("onmousewheel",b.proxy(this._handleMouseWheelEvent,
this)):b(window).bind("mousewheel",b.proxy(this._handleMouseWheelEvent,this)))},_unbind:function(){b("body").unbind("scrollstart",this._handleScrollStart);b("body").unbind("scrollstop",this._handleScrollStop);/Firefox/i.test(navigator.userAgent)?b(window).unbind("DOMMouseScroll",this._handleMouseWheelEvent):"undefined"!=typeof this._selectors&&null!=this._selectors&&""!=this._selectors&&"undefined"!=typeof this._selectors.main&&(b(this._selectors.main).attachEvent?b(window).unbind("onmousewheel",
this._handleMouseWheelEvent):b(window).unbind("mousewheel",this._handleMouseWheelEvent))},destroy:function(){this._unbind();this._defaultParameters=this._defaultSettings=this._defaultOptions=this._widgetState=this._mouseWheelTimeoutId=this._handleScrollStopTimeoutId=this._handleScrollStartTimeoutId=this._mouseWheelEventJustFired=this._handleScrollStopJustFired=this._handleScrollStartJustFired=this._instances=this._parameters=this._settings=this.timeoutOptions=this.options=null;b.Widget.prototype.destroy.apply(this)},
_check:function(a){a=this.options.threshold||a;var d,c,e;c=document.documentElement.scrollTop?document.documentElement.scrollTop:document.body.scrollTop;d=this._instances[this._settings.pageId]?this._instances[this._settings.pageId].settings.totalHeight:this._settings.totalHeight;e=b(window).height();return d-a<=c+e},_load:function(a){typeof this._settings.pageId!=r&&""!=this._settings.pageId&&(b(".ui-page-active").attr("id")==this._settings.pageId?(this._moreOutstandingPageId=this._settings.pageId,
$that=this,setTimeout(function(){!$that._widgetState.busy&&!$that._widgetState.done&&!$that._widgetState.limit?$that._moreOutstandingPageId==$that._settings.pageId&&($that._check($that.options.threshold)||0===a)&&b("#"+$that._settings.progressDivId).show($that.timeoutOptions.showprogress,function(){moreUrl=$that._settings.moreUrl;var a="POST",c="json",e="",n=!1,p="",h=0;$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,0<$that._instances[$that._settings.pageId].options.limit&&$that._instances[$that._settings.pageId].options.retrieved+$that._instances[$that._settings.pageId].options.retrieve>=$that._instances[$that._settings.pageId].options.limit&&($that._parameters.retrieve=$that._instances[$that._settings.pageId].options.limit>=$that._instances[$that._settings.pageId].options.retrieved?
$that._instances[$that._settings.pageId].options.limit-$that._instances[$that._settings.pageId].options.retrieved:0),$that._instances[$that._settings.pageId].settings.JSONP&&(n=!0,p=$that._instances[$that._settings.pageId].settings.JSONPCallback)):($that._parameters.retrieve=$that.options.retrieve,$that._parameters.retrieved=$that.options.retrieved,$that._parameters.offset=$that.options.offset,0<$that.options.limit&&$that.options.retrieve+$that.options.retrieved>=$that.options.limit&&($that._parameters.retrieve=
$that.options.limit>=$that.options.retrieve?$that.options.limit-$that.options.retrieve:0));if("undefined"!=typeof $that._settings.pageId&&""!=$that._settings.pageId){var q=b("#"+$that._settings.pageId).find('[type="hidden"]');for(i=0;i<q.length;i++){var k=b(q).get(i);"undefined"!=typeof b(k).attr("id")&&""!=b(k).attr("id")&&($that._parameters[b(k).attr("id")]=escape(b(k).val()))}}if(n){a="GET";c="jsonp";e="";for(f in $that._parameters)e=0==h?e+('"'+f+'": "'+$that._parameters[f]+'"'):e+(', "'+f+'": "'+
$that._parameters[f]+'"'),h+=1;e=b.parseJSON("{ "+e+" }")}else for(var f in $that._parameters)e=0==h?e+(f+"="+$that._parameters[f]):e+("&"+f+"="+$that._parameters[f]),h+=1;b.ajax({type:a,url:moreUrl,dataType:c,jsonpCallback:p,data:e,success:function(a){more=a;if("object"!==typeof a)try{more=b.parseJSON(a)}catch(c){return $that._triggerEvent("error","_load",c.message),b("#"+$that._settings.progressDivId).hide(250,function(){$that._widgetState.busy=!1}),!1}try{var d=more.data[0].count,e="",g="",j="",
f="",h="",k=!1,m="",n=a="",l="";if(0<d){m=$that._selectors.main;a=$that._selectors.single;n=$that._selectors.bottom;l=$that._getBottomElement(m,n);if("undefined"!=typeof more.data[0].html&&""!=more.data[0].html)e=more.data[0].html,l?b(a).last().before(e):b(m).append(e);else if($that._instances[$that._settings.pageId]?("undefined"!=typeof $that._instances[$that._settings.pageId].settings.templateId&&""!=$that._instances[$that._settings.pageId].settings.templateId&&(f=$that._instances[$that._settings.pageId].settings.templateId,
"undefined"!=typeof $that._instances[$that._settings.pageId].settings.templateType&&""!=$that._instances[$that._settings.pageId].settings.templateType&&(h=$that._instances[$that._settings.pageId].settings.templateType),"undefined"!=typeof $that._instances[$that._settings.pageId].settings.template&&""!=$that._instances[$that._settings.pageId].settings.template&&(j=$that._instances[$that._settings.pageId].settings.template)),k=$that._instances[$that._settings.pageId].settings.templatePrecompiled):("undefined"!=
typeof $that._settings.templateId&&""!=$that._settings.templateId&&(f=$that._settings.templateId,"undefined"!=typeof $that._settings.templateType&&""!=$that._settings.templateType&&(h=$that._settings.templateType),"undefined"!=typeof $that._settings.template&&""!=$that._settings.template&&(j=$that._settings.template)),k=$that._settings.templatePrecompiled),""!==h&&""!==f&&""!==j)if("json2html"===h)g=more.data[0].json,l&&l.remove(),b(m).json2html(g,j),l&&b(a).last().append(l);else{g=more.data[0];switch(h){case "handlebars":j=
k?Handlebars.templates[f+".tmpl"]:Handlebars.compile(j);e=j(g);break;case "icanhaz":ich.addTemplate("listitem",j);e=ich.listitem(g,!0);ich.clearAll();break;case "dust":k?dust.render(f,g,function(a,b){e=b}):(dust.loadSource(j),dust.render(f,g,function(a,b){e=b}));break;case "dot":j=doT.template(j),e=j(g)}l?b(a).last().before(e):b(m).append(e)}b(m).listview("refresh");g=0;d=parseInt(d);if($that._instances[$that._settings.pageId]){var p=$that._instances[$that._settings.pageId].settings.totalHeight;"undefined"!==
typeof $that._instances[$that._settings.pageId].settings.singleItemHeight?g=$that._instances[$that._settings.pageId].settings.singleItemHeight:(g=b(a).first().next().height(),$that._instances[$that._settings.pageId].settings.singleItemHeight=g);$that._instances[$that._settings.pageId].settings.totalHeight=p+g*d}else"undefined"!==typeof $that._settings.singleItemHeight?g=$that._settings.singleItemHeight:(g=b(a).first().next().height(),$that._settings.singleItemHeight=g),$that._settings.totalHeight+=
$that._settings.singleItemHeight*d;$that._instances[$that._settings.pageId].options.retrieved+=d;0<$that._instances[$that._settings.pageId].options.limit&&$that._instances[$that._settings.pageId].options.retrieved==$that._instances[$that._settings.pageId].options.limit&&($that._widgetState.limit=!0,$that._triggerEvent("limitreached","_load"));if(d<$that._instances[$that._settings.pageId].options.retrieve||"all"==$that._instances[$that._settings.pageId].options.retrieve)$that._widgetState.done=!0,
$that._triggerEvent("alldone","_load")}else $that._widgetState.done=!0,$that._triggerEvent("alldone","_load");b("#"+$that._settings.progressDivId).hide(250,function(){$that._widgetState.busy=!1});$that._triggerEvent("doneloading","_load")}catch(q){return $that._triggerEvent("error","_load",q.message),b("#"+$that._settings.progressDivId).hide(250,function(){$that._widgetState.busy=!1}),!1}},error:function(a){$that._triggerEvent("error","_load",a);b("#"+$that._settings.progressDivId).hide(250,function(){$that._widgetState.busy=
!1})},complete:function(){}})}):$that._widgetState.done?($that._triggerEvent("alldone","_load"),$that._widgetState.limit&&$that._triggerEvent("limited","_load")):$that._widgetState.busy&&$that._triggerEvent("busy","_load")},a)):b("#"+this._settings.progressDivId).hide(250,function(){"undefined"!=typeof this._widgetState&&(this._widgetState.busy=!1)}))},_getBottomElement:function(a,d){var c=b(a).last().find(d);switch(c.length){case 2:c=c.last();break;default:c=null}return"undefined"!=typeof c&&null!=
c&&""!=c&&"null"!=c?c:!1},_handleMouseWheelEvent:function(){if(!this._mouseWheelEventJustFired&&!this._handleScrollStopJustFired&&!this._handleScrollStartJustFired){this._mouseWheelEventJustFired=!0;this._load(this.timeoutOptions.mousewheel);var a=this;this._mouseWheelTimeoutId=setTimeout(function(){a._mouseWheelEventJustFired=!1},1E3)}},_handleScrollStart:function(){if(!this._mouseWheelEventJustFired&&!this._handleScrollStopJustFired&&!this._handleScrollStartJustFired){this._handleScrollStartJustFired=
!0;this._load(this.timeoutOptions.scrollstart);var a=this;this._handleScrollStartTimeoutId=setTimeout(function(){a._handleScrollStartJustFired=!1},1200)}},_handleScrollStop:function(){if(!this._mouseWheelEventJustFired&&!this._handleScrollStopJustFired&&!this._handleScrollStartJustFired){this._handleScrollStopJustFired=!0;this._load(this.timeoutOptions.scrollstop);var a=this;this._handleScrollStopTimeoutId=setTimeout(function(){a._handleScrollStopJustFired=!1},1200)}},loadMore:function(a){0===a?this._load(this.timeoutOptions.immediately):
this._load(this.timeoutOptions.scrolldown)},_setOption:function(a,d){this._instances[this._settings.pageId]&&this._instances[this._settings.pageId].options[a]&&(this._instances[this._settings.pageId].options[a]=d);b.Widget.prototype._setOption.apply(this,arguments)},refresh:function(a,d){if("parameters"==a){if("undefined"!=typeof this.options)for(var c in this._parameters)"undefined"!=typeof this.options[c]&&(this._parameters[c]=this.options[c])}else"parameter"==a&&(c=d,"undefined"!=typeof this.options[c]&&
(this._parameters[c]=this.options[c]));c=JSON.stringify(this._parameters);this._parameters=b.parseJSON(c)},reInitialize:function(a,b,c,e){this._initialize(a,b,c,e)},reset:function(a){var d=this;b.ajax({type:"POST",url:d._settings.clearUrl,async:!0,data:"section="+a,success:function(b){parseInt(b)&&(d.options.retrieved=d._defaultOptions.retrieved,d._widgetState.done=!1,d._widgetState.limit=!1,"undefined"!=typeof d._instances[a]&&delete d._instances[a],d._triggerEvent("reset","reset","All session variables for the '"+
a+"' page and the lazyloader instance variables have been cleared."))},error:function(a){d._triggerEvent("error","reset",a);b("#"+d._settings.progressDivId).hide(250,function(){d._widgetState.busy=!1})}})},resetAll:function(){var a=this;b.ajax({type:"POST",url:a._settings.clearUrl,async:!0,data:"",success:function(b){if(parseInt(b)){for(pageId in a._instances)delete a._instances[pageId];a.options.retrieved=a._defaultOptions.retrieved;a._widgetState.done=!1;a._widgetState.busy=!1;a._widgetState.limit=
!1;a._triggerEvent("resetall","resetAll","All session variables for all pages currently being tracked by the lazyloader have been cleared.")}}})},_triggerEvent:function(a,b,c){c=c||"";switch(a){case "error":case "resetall":this._trigger(a,{type:"lazyloader"+a,"function":b,message:c,settings:this._settings,options:this.options,parameters:this._parameters});break;default:this._trigger(a,{type:"lazyloader"+a,"function":b,message:c,pageId:this._settings.pageId,mainId:this._settings.mainId,loaded:this.options.retrieved})}}});
b(document).bind("pagecreate create",function(a){b.mobile.lazyloader.prototype.enhanceWithin(a.target)})})(jQuery);