r63942 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r63941‎ | r63942 | r63943 >
Date:04:51, 19 March 2010
Author:neilk
Status:deferred
Tags:
Comment:
Destination checking with delay
Modified paths:
  • /branches/js2-work/phase3/js/mwEmbed/modules/UploadWizard/mw.DestinationChecker.js (modified) (history)
  • /branches/js2-work/phase3/js/mwEmbed/modules/UploadWizard/mw.UploadWizard.js (modified) (history)

Diff [purge]

Index: branches/js2-work/phase3/js/mwEmbed/modules/UploadWizard/mw.UploadWizard.js
@@ -469,9 +469,9 @@
470470 $j( _this.filenameInput ).val( mw.UploadWizardUtil.titleToPath( $j(_this.titleInput).val() ) );
471471 });
472472 $j(_this.titleInput).destinationChecked( {
473 - spinner: _this.toggleDestinationBusy,
474 - preprocess: mw.UploadWizardUtil.titleToPath,
475 - processResult: _this.processDestinationCheck
 473+ spinner: function(bool) { _this.toggleDestinationBusy(bool) },
 474+ preprocess: mw.UploadWizardUtil.pathToTitle, // stateless, so we don't need the object
 475+ processResult: function(result) { _this.processDestinationCheck(result) }
476476 } );
477477
478478 _this.titleErrorDiv = $j('<div></div>');
@@ -597,10 +597,11 @@
598598 * @param busy boolean true = show busy-ness, false = remove
599599 */
600600 toggleDestinationBusy: function ( busy ) {
 601+ var _this = this;
601602 if (busy) {
602 - _this.titleInput.addClass( busy );
 603+ _this.titleInput.addClass( "busy" );
603604 } else {
604 - _this.titleInput.removeClass( busy );
 605+ _this.titleInput.removeClass( "busy" );
605606 }
606607 },
607608
@@ -609,6 +610,7 @@
610611 * See mw.DestinationChecker.js for documentation of result format
611612 */
612613 processDestinationCheck: function( result ) {
 614+ var _this = this;
613615
614616 if ( result.unique ) {
615617 // do nothing
@@ -687,7 +689,7 @@
688690 )
689691 );
690692
691 - }
 693+ },
692694
693695 /**
694696 * Do anything related to a change in the number of descriptions
Index: branches/js2-work/phase3/js/mwEmbed/modules/UploadWizard/mw.DestinationChecker.js
@@ -19,6 +19,7 @@
2020 var _this = this;
2121 _this.selector = options.selector;
2222 _this.spinner = options.spinner;
 23+ _this.cachedResult = {};
2324 _this.processResult = options.processResult;
2425
2526 if (options.apiUrl) {
@@ -39,7 +40,8 @@
4041 _this.delay = 500; // ms;
4142 }
4243
43 - $j( selector ).change( _this.change ).keypress( _this.change );
 44+ var check = _this.getDelayedChecker();
 45+ $j( _this.selector ).change( check ).keypress( check );
4446
4547 }
4648
@@ -53,20 +55,22 @@
5456 * fire when the input changes value or keypress
5557 * will trigger a check of the name if the field has been idle for delay ms.
5658 */
57 - change: function() {
58 - var _this = this;
 59+ getDelayedChecker: function() {
 60+ var checker = this;
 61+ return function() {
 62+ var el = this; // but we don't use it...
5963
60 - // if we changed before the old timeout ran, clear that timeout.
61 - if ( _this.timeoutId ) {
62 - window.clearTimeout( this.timeoutId );
 64+ // if we changed before the old timeout ran, clear that timeout.
 65+ if ( checker.timeoutId ) {
 66+ window.clearTimeout( checker.timeoutId );
 67+ }
 68+
 69+ // and start another, hoping this time we'll be idle for delay ms.
 70+ checker.timeoutId = window.setTimeout(
 71+ function() { checker.checkUnique(); },
 72+ checker.delay
 73+ );
6374 }
64 -
65 - // and start another, hoping this time we'll be idle for delay ms.
66 - _this.timeoutId = window.setTimeout(
67 - function() { _this.checkUnique() },
68 - _this.delay
69 - );
70 -
7175 },
7276
7377 /**
@@ -74,9 +78,9 @@
7579 * This is a more abstract version of AddMedia/UploadHandler.js::doDestCheck
7680 */
7781 checkUnique: function() {
78 -
 82+ var _this = this;
7983 var found = false;
80 - var name = _this.preprocess( $j(_this.input).val() );
 84+ var name = _this.preprocess( $j( _this.selector ).val() );
8185
8286 if ( _this.responseCache[name] !== undefined ) {
8387 _this.doResult( name, _this.responseCache[name] );
@@ -101,7 +105,7 @@
102106
103107 if ( !data || !data.query || !data.query.pages ) {
104108 // Ignore a null result
105 - mw.log(" No data in checkUnique result")
 109+ mw.log(" No data in checkUnique result");
106110 return;
107111 }
108112
@@ -128,9 +132,11 @@
129133 var ntitle = data.query.pages[ page_id ].title
130134 }
131135
 136+ var img = data.query.pages[ page_id ].imageinfo[0];
 137+
132138 result = {
133139 unique: false,
134 - img: data.query.pages[ page_id ].imageinfo[0],
 140+ img: img,
135141 title: ntitle,
136142 href : img.descriptionurl
137143 };
@@ -139,11 +145,11 @@
140146 }
141147 }
142148
143 - if ( result !== undefined) {
 149+ if ( result !== undefined ) {
144150 _this.cachedResult[name] = result;
145151 _this.processResult( result );
146152 }
147 - }
 153+ } );
148154 }
149155
150156 };
@@ -156,7 +162,7 @@
157163 $.fn.destinationChecked = function( options ) {
158164 var _this = this;
159165 options.selector = _this;
160 - new UploadFileNameChecker( options );
 166+ new mw.DestinationChecker( options );
161167 return _this;
162168 };
163169 } )( jQuery );

Status & tagging log