r65771 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r65770‎ | r65771 | r65772 >
Date:23:28, 1 May 2010
Author:jeroendedauw
Status:deferred
Tags:
Comment:
Refactoring ajaxscroll JS
Modified paths:
  • /trunk/extensions/Storyboard/jquery/jquery.ajaxscroll.js (modified) (history)
  • /trunk/extensions/Storyboard/tags/Storyboard/storyboard.js (modified) (history)

Diff [purge]

Index: trunk/extensions/Storyboard/jquery/jquery.ajaxscroll.js
@@ -8,34 +8,35 @@
99 * @version 0.2
1010 */
1111 (function($) {
12 - $.fn.ajaxScroll=function(opt){
13 - opt=jQuery.extend(
 12+ $.fn.ajaxScroll = function( opt ) {
 13+ opt = jQuery.extend(
1414 {
15 - batchNum:5,
16 - batchSize:30,
17 - batchTemplate:null,
18 - boxTemplate:null,
19 - batchClass:"storyboard-batch",
20 - boxClass:"storyboard-box",
21 - emptyBatchClass:"storyboard-empty",
22 - scrollPaneClass:"scrollpane",
23 - lBound:"auto",
24 - uBound:"auto",
25 - eBound:"auto",
26 - maxOffset:1000,
27 - scrollDelay:600,
28 - endDelay:100,
29 - updateBatch:null,
30 - updateEnd:null
 15+ batchNum: 5,
 16+ batchSize: 30,
 17+ batchTemplate: null,
 18+ boxTemplate: null,
 19+ batchClass: "storyboard-batch",
 20+ boxClass: "storyboard-box",
 21+ emptyBatchClass: "storyboard-empty",
 22+ scrollPaneClass: "scrollpane",
 23+ lBound: "auto",
 24+ uBound: "auto",
 25+ eBound: "auto",
 26+ maxOffset: 1000,
 27+ scrollDelay: 600, // The interval for checking if the user scrolled, in ms.
 28+ endDelay: 100,
 29+ updateBatch: null,
 30+ updateEnd: null,
 31+ busy: false
3132 },
3233 opt
3334 );
 35+
3436 return this.each( function() {
3537 var ele = this;
3638 var $me = jQuery( this );
3739 var $sp;
3840 var fnEnd;
39 - var fnScroll;
4041 var offset = 0;
4142 var lsp = -1;
4243
@@ -52,12 +53,11 @@
5354 _ab();
5455
5556 fnEnd = vEnd;
56 - fnScroll = vScroll;
5757
5858 setTimeout( monEnd, opt.endDelay );
5959
6060 if( typeof opt.updateBatch == 'function' ){
61 - setTimeout( monScroll, opt.scrollDelay );
 61+ setTimeout( handleScrolling, opt.scrollDelay );
6262 }
6363
6464 function _css(){
@@ -105,30 +105,40 @@
106106 $b.append( opt.boxTemplate );
107107 }
108108
109 - $s.append($b);
 109+ $s.append( $b );
110110 }
111111
112112 return offset;
113113 };
114114
115115 function vScroll() {
 116+ // If a batcnh is currently being loaded, we can't start another one yet.
 117+ if ( opt.busy ) {
 118+ return;
 119+ }
 120+
116121 var so = $me.scrollTop();
117122
118 - if( lsp != so){
 123+ if( lsp != so) {
119124 lsp = so;
120125 var co = $me.offset().top;
121126
122127 $sp.find( '> .' + opt.emptyBatchClass ).each( function( i, obj ) {
123128 var $b = jQuery( obj );
124129 var p = $b.position().top - co;
125 - if ( opt.lBound > p || p > opt.uBound ) { return; }
126 - opt.updateBatch( $b.removeClass( opt.emptyBatchClass ) );
 130+
 131+ if ( opt.lBound > p || p > opt.uBound ) {
 132+ return;
 133+ }
 134+
 135+ opt.busy = true;
 136+ opt.updateBatch( $b.removeClass( opt.emptyBatchClass ), opt );
127137 });
128138 }
129139 };
130140
131141 function vEnd() {
132 - if( ele.scrollTop > 0 && ele.scrollHeight-ele.scrollTop < opt.eBound ) {
 142+ if ( ele.scrollTop > 0 && ele.scrollHeight - ele.scrollTop < opt.eBound ) {
133143 offset = batch( $sp, offset, opt );
134144 return 1;
135145 }
@@ -136,14 +146,18 @@
137147 return opt.endDelay;
138148 };
139149
140 - function monScroll(){
141 - fnScroll();
142 - setTimeout(monScroll,opt.scrollDelay);
 150+ /**
 151+ * This function emulates a scroll event handler by firing itself every so many ms, and
 152+ * then calling a function checking if the user has scrolled, and if any batches should be loaded.
 153+ */
 154+ function handleScrolling() {
 155+ vScroll();
 156+ setTimeout( handleScrolling, opt.scrollDelay );
143157 };
144158
145 - function monEnd(){
146 - if(offset < opt.maxOffset){
147 - setTimeout(monEnd,fnEnd());
 159+ function monEnd() {
 160+ if ( offset < opt.maxOffset ) {
 161+ setTimeout( monEnd, fnEnd() );
148162 }
149163 }
150164
Index: trunk/extensions/Storyboard/tags/Storyboard/storyboard.js
@@ -19,7 +19,7 @@
2020 } );
2121 } );
2222
23 - function updateStoryboard( $storyboard ) {
 23+ function updateStoryboard( $storyboard, ajaxScrollObj ) {
2424 $.getJSON( wgScriptPath + '/api.php',
2525 {
2626 'action': 'query',
@@ -31,7 +31,7 @@
3232 },
3333 function( data ) {
3434 if ( data.query ) {
35 - addStories( $storyboard, data.query );
 35+ addStories( $storyboard, data.query, ajaxScrollObj );
3636 } else {
3737 alert( 'An error occured:\n' + data.error.info ); // TODO: i18n
3838 }
@@ -39,7 +39,7 @@
4040 );
4141 }
4242
43 - function addStories( $storyboard, query ) {
 43+ function addStories( $storyboard, query, ajaxScrollObj ) {
4444 // Remove the empty boxes.
4545 $storyboard.html('');
4646
@@ -115,6 +115,8 @@
116116
117117 $storyboard.append( $storyBody );
118118 }
 119+
 120+ ajaxScrollObj.busy = false;
119121 }
120122
121123 })(jQuery);
\ No newline at end of file

Status & tagging log