Index: branches/js2-work/phase3/js/mwEmbed/modules/UploadWizard/mw.UploadWizard.js |
— | — | @@ -469,9 +469,9 @@ |
470 | 470 | $j( _this.filenameInput ).val( mw.UploadWizardUtil.titleToPath( $j(_this.titleInput).val() ) ); |
471 | 471 | }); |
472 | 472 | $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) } |
476 | 476 | } ); |
477 | 477 | |
478 | 478 | _this.titleErrorDiv = $j('<div></div>'); |
— | — | @@ -597,10 +597,11 @@ |
598 | 598 | * @param busy boolean true = show busy-ness, false = remove |
599 | 599 | */ |
600 | 600 | toggleDestinationBusy: function ( busy ) { |
| 601 | + var _this = this; |
601 | 602 | if (busy) { |
602 | | - _this.titleInput.addClass( busy ); |
| 603 | + _this.titleInput.addClass( "busy" ); |
603 | 604 | } else { |
604 | | - _this.titleInput.removeClass( busy ); |
| 605 | + _this.titleInput.removeClass( "busy" ); |
605 | 606 | } |
606 | 607 | }, |
607 | 608 | |
— | — | @@ -609,6 +610,7 @@ |
610 | 611 | * See mw.DestinationChecker.js for documentation of result format |
611 | 612 | */ |
612 | 613 | processDestinationCheck: function( result ) { |
| 614 | + var _this = this; |
613 | 615 | |
614 | 616 | if ( result.unique ) { |
615 | 617 | // do nothing |
— | — | @@ -687,7 +689,7 @@ |
688 | 690 | ) |
689 | 691 | ); |
690 | 692 | |
691 | | - } |
| 693 | + }, |
692 | 694 | |
693 | 695 | /** |
694 | 696 | * 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 @@ |
20 | 20 | var _this = this; |
21 | 21 | _this.selector = options.selector; |
22 | 22 | _this.spinner = options.spinner; |
| 23 | + _this.cachedResult = {}; |
23 | 24 | _this.processResult = options.processResult; |
24 | 25 | |
25 | 26 | if (options.apiUrl) { |
— | — | @@ -39,7 +40,8 @@ |
40 | 41 | _this.delay = 500; // ms; |
41 | 42 | } |
42 | 43 | |
43 | | - $j( selector ).change( _this.change ).keypress( _this.change ); |
| 44 | + var check = _this.getDelayedChecker(); |
| 45 | + $j( _this.selector ).change( check ).keypress( check ); |
44 | 46 | |
45 | 47 | } |
46 | 48 | |
— | — | @@ -53,20 +55,22 @@ |
54 | 56 | * fire when the input changes value or keypress |
55 | 57 | * will trigger a check of the name if the field has been idle for delay ms. |
56 | 58 | */ |
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... |
59 | 63 | |
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 | + ); |
63 | 74 | } |
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 | | - |
71 | 75 | }, |
72 | 76 | |
73 | 77 | /** |
— | — | @@ -74,9 +78,9 @@ |
75 | 79 | * This is a more abstract version of AddMedia/UploadHandler.js::doDestCheck |
76 | 80 | */ |
77 | 81 | checkUnique: function() { |
78 | | - |
| 82 | + var _this = this; |
79 | 83 | var found = false; |
80 | | - var name = _this.preprocess( $j(_this.input).val() ); |
| 84 | + var name = _this.preprocess( $j( _this.selector ).val() ); |
81 | 85 | |
82 | 86 | if ( _this.responseCache[name] !== undefined ) { |
83 | 87 | _this.doResult( name, _this.responseCache[name] ); |
— | — | @@ -101,7 +105,7 @@ |
102 | 106 | |
103 | 107 | if ( !data || !data.query || !data.query.pages ) { |
104 | 108 | // Ignore a null result |
105 | | - mw.log(" No data in checkUnique result") |
| 109 | + mw.log(" No data in checkUnique result"); |
106 | 110 | return; |
107 | 111 | } |
108 | 112 | |
— | — | @@ -128,9 +132,11 @@ |
129 | 133 | var ntitle = data.query.pages[ page_id ].title |
130 | 134 | } |
131 | 135 | |
| 136 | + var img = data.query.pages[ page_id ].imageinfo[0]; |
| 137 | + |
132 | 138 | result = { |
133 | 139 | unique: false, |
134 | | - img: data.query.pages[ page_id ].imageinfo[0], |
| 140 | + img: img, |
135 | 141 | title: ntitle, |
136 | 142 | href : img.descriptionurl |
137 | 143 | }; |
— | — | @@ -139,11 +145,11 @@ |
140 | 146 | } |
141 | 147 | } |
142 | 148 | |
143 | | - if ( result !== undefined) { |
| 149 | + if ( result !== undefined ) { |
144 | 150 | _this.cachedResult[name] = result; |
145 | 151 | _this.processResult( result ); |
146 | 152 | } |
147 | | - } |
| 153 | + } ); |
148 | 154 | } |
149 | 155 | |
150 | 156 | }; |
— | — | @@ -156,7 +162,7 @@ |
157 | 163 | $.fn.destinationChecked = function( options ) { |
158 | 164 | var _this = this; |
159 | 165 | options.selector = _this; |
160 | | - new UploadFileNameChecker( options ); |
| 166 | + new mw.DestinationChecker( options ); |
161 | 167 | return _this; |
162 | 168 | }; |
163 | 169 | } )( jQuery ); |