r111766 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r111765‎ | r111766 | r111767 >
Date:17:35, 17 February 2012
Author:jdlrobson
Status:ok
Tags:
Comment:
jslinted opensearch code
Modified paths:
  • /trunk/extensions/MobileFrontend/javascripts/beta_opensearch.js (modified) (history)
  • /trunk/extensions/MobileFrontend/javascripts/opensearch.js (modified) (history)

Diff [purge]

Index: trunk/extensions/MobileFrontend/javascripts/opensearch.js
@@ -1,21 +1,17 @@
2 -MobileFrontend.opensearch = function() {
 2+/*global document, window, MobileFrontend, navigator, placeholder */
 3+/*jslint sloppy: true, white:true, maxerr: 50, indent: 4, plusplus: true*/
 4+MobileFrontend.opensearch = (function() {
 5+ var apiUrl = '/api.php', timer = -1, typingDelay = 500,
 6+ numResults = 5, pixels = 'px',
 7+ results = document.getElementById( 'results' ),
 8+ search = document.getElementById( 'search' ),
 9+ sq = document.getElementById( 'sq' ),
 10+ sb = document.getElementById( 'searchbox' );
311
4 - var apiUrl = '/api.php';
5 -
612 if ( scriptPath ) {
713 apiUrl = scriptPath + apiUrl;
814 }
915
10 - var timer = -1;
11 - var typingDelay = 500;
12 - var numResults = 5;
13 - var pixels = 'px';
14 -
15 - var results = document.getElementById( 'results' );
16 - var search = document.getElementById( 'search' );
17 - var sq = document.getElementById( 'sq' );
18 - var sb = document.getElementById( 'searchbox' );
19 -
2016 function hideResults() {
2117 var results = document.getElementById( 'results' );
2218 results.style.display = 'none';
@@ -24,7 +20,7 @@
2521 function whichElement( e ) {
2622 var targ;
2723 if ( !e ) {
28 - var e = window.event;
 24+ e = window.event;
2925 }
3026 if ( e.target ) {
3127 targ = e.target;
@@ -32,25 +28,24 @@
3329 targ = e.srcElement;
3430 }
3531
36 - if ( targ.nodeType == 3 ) {
 32+ if ( targ.nodeType === 3 ) {
3733 targ = targ.parentNode;
3834 }
3935
4036 e.cancelBubble = true;
4137 e.stopPropagation();
4238
43 - if ( targ.className == "suggestion-result" ||
44 - targ.className == "search-result-item" ||
45 - targ.className == "suggestions-result" ||
46 - targ.className == "sq-val-update" ) {
47 - } else {
 39+ if (!( targ.className === "suggestion-result" ||
 40+ targ.className === "search-result-item" ||
 41+ targ.className === "suggestions-result" ||
 42+ targ.className === "sq-val-update" ) ) {
4843 hideResults();
4944 }
5045 }
5146
5247 function updateSearchWidth() {
5348 if ( sq && search && sb ) {
54 - var iw = ( document.documentElement.clientWidth ) ? document.documentElement.clientWidth : document.body.clientWidth;
 49+ var iw = document.documentElement.clientWidth || document.body.clientWidth;
5550 sb.style.width = ( iw - 30 ) + pixels;
5651 sq.style.width = ( iw - 110 ) + pixels;
5752 search.style.width = ( iw - 130 ) + pixels;
@@ -89,10 +84,10 @@
9085 timer = setTimeout( function () { searchApi( term ); }, typingDelay );
9186 }
9287 }, false );
93 - }
 88+ };
9489
9590 function searchApi( term ) {
96 - var xmlHttp;
 91+ var xmlHttp, url;
9792 if ( window.XMLHttpRequest ) {
9893 xmlHttp = new XMLHttpRequest();
9994 } else {
@@ -100,26 +95,26 @@
10196 }
10297 xmlHttp.overrideMimeType( 'text/xml' );
10398 xmlHttp.onreadystatechange = function() {
104 - if ( xmlHttp.readyState == 4 && xmlHttp.status == 200 ) {
 99+ if ( xmlHttp.readyState === 4 && xmlHttp.status === 200 ) {
105100 var sections = createObjectArray( xmlHttp.responseXML );
106101 writeResults( sections );
107102 }
108 - }
 103+ };
109104 term = encodeURIComponent( term );
110 - var url = apiUrl + '?action=opensearch&limit=' + numResults + '&namespace=0&format=xml&search=' + term;
 105+ url = apiUrl + '?action=opensearch&limit=' + numResults + '&namespace=0&format=xml&search=' + term;
111106 xmlHttp.open( 'GET', url, true );
112107 xmlHttp.send();
113108 }
114109
115110 function createObjectArray( responseXml ) {
116 - var sections = new Array();
117 - var items = responseXml.getElementsByTagName( 'Item' );
 111+ var sections = [], i, item, section,
 112+ items = responseXml.getElementsByTagName( 'Item' );
118113 for ( i = 0; i < items.length; i++ ) {
119 - var item = items[i];
120 - var section = {
 114+ item = items[i];
 115+ section = {
121116 label: item.getElementsByTagName( 'Text' )[0].textContent,
122 - value: item.getElementsByTagName( 'Url' )[0].textContent,
123 - }
 117+ value: item.getElementsByTagName( 'Url' )[0].textContent
 118+ };
124119 sections.push( section );
125120 }
126121 return sections;
@@ -135,7 +130,9 @@
136131 }
137132
138133 function writeResults( sections ) {
139 - var results = document.getElementById( 'results' );
 134+ var results = document.getElementById( 'results' ), suggestions, i,
 135+ suggestionListener, section, suggestionsResult, link, label;
 136+
140137 results.style.display = 'block';
141138 if ( !sections || sections.length < 1 ) {
142139 results.innerHTML = "No results";
@@ -143,21 +140,24 @@
144141 if( results.firstChild ) {
145142 results.removeChild( results.firstChild );
146143 }
147 - var suggestions = document.createElement( 'div' );
 144+ suggestions = document.createElement( 'div' );
148145 suggestions.className = 'suggestions-results';
149146 results.appendChild( suggestions );
 147+ suggestionListener = function() {
 148+ var title = this.parentNode.getAttribute( 'title' );
 149+ sqValUpdate( title );
 150+ };
 151+
150152 for ( i = 0; i < sections.length; i++ ) {
151 - var section = sections[i], suggestionsResult = document.createElement( 'div' ),
152 - link = document.createElement( 'a' ), label;
 153+ section = sections[i];
 154+ suggestionsResult = document.createElement( 'div' );
 155+ link = document.createElement( 'a' );
153156 suggestionsResult.setAttribute( 'title', section.label );
154157 suggestionsResult.className = 'suggestions-result';
155158 label = document.createTextNode( '+' );
156159 link.appendChild(label);
157160 link.className = 'sq-val-update';
158 - link.addEventListener( 'click', function() {
159 - var title = this.parentNode.getAttribute( 'title' );
160 - sqValUpdate( title );
161 - });
 161+ link.addEventListener( 'click', suggestionListener );
162162 suggestionsResult.appendChild( link );
163163
164164 link = document.createElement( 'a' );
@@ -194,4 +194,4 @@
195195 createObjectArray: createObjectArray
196196 };
197197
198 -}();
 198+}());
Index: trunk/extensions/MobileFrontend/javascripts/beta_opensearch.js
@@ -1,29 +1,25 @@
2 -MobileFrontend.opensearch = function() {
 2+/*global document, window, MobileFrontend, navigator, placeholder */
 3+/*jslint sloppy: true, white:true, maxerr: 50, indent: 4, plusplus: true*/
 4+MobileFrontend.opensearch = (function() {
 5+ var apiUrl = '/api.php', timer = -1, typingDelay = 500,
 6+ numResults = 15, pixels = 'px', term,
 7+ results = document.getElementById( 'results' ),
 8+ search = document.getElementById( 'search' ),
 9+ sq = document.getElementById( 'sq' ),
 10+ sb = document.getElementById( 'searchbox' ),
 11+ logo = document.getElementById( 'logo' ),
 12+ goButton = document.getElementById( 'goButton' ),
 13+ content = document.getElementById( 'content' ),
 14+ footer = document.getElementById( 'footer' ),
 15+ zeroRatedBanner = document.getElementById( 'zero-rated-banner' ) ||
 16+ document.getElementById( 'zero-rated-banner-red' ),
 17+ clearSearch = document.getElementById( 'clearsearch' ),
 18+ focused = false, ol = {};
319
4 - var apiUrl = '/api.php';
5 -
620 if ( scriptPath ) {
721 apiUrl = scriptPath + apiUrl;
822 }
923
10 - var timer = -1;
11 - var typingDelay = 500;
12 - var numResults = 15;
13 - var pixels = 'px';
14 - var term;
15 -
16 - var results = document.getElementById( 'results' );
17 - var search = document.getElementById( 'search' );
18 - var sq = document.getElementById( 'sq' );
19 - var sb = document.getElementById( 'searchbox' );
20 - var logo = document.getElementById( 'logo' );
21 - var goButton = document.getElementById( 'goButton' );
22 - var content = document.getElementById( 'content' );
23 - var footer = document.getElementById( 'footer' );
24 - var zeroRatedBanner = document.getElementById( 'zero-rated-banner' ) ||
25 - document.getElementById( 'zero-rated-banner-red' );
26 - var clearSearch = document.getElementById( 'clearsearch' );
27 -
2824 function hideResults() {
2925 results.style.display = 'none';
3026 }
@@ -41,10 +37,9 @@
4238
4339 resetViewPort();
4440
45 - var focused = false;
46 - var ol = new Object();
4741 search.onfocus = function() {
48 -
 42+ var pE, pT, pTT, rrd, rrdD,
 43+ removeResultsEl;
4944 resetViewPort();
5045
5146 if ( zeroRatedBanner ) {
@@ -67,8 +62,8 @@
6863 sq.style.top = sb.offsetTop + pixels;
6964 sq.style.height = sb.offsetHeight + pixels;
7065 sq.style.width = sb.offsetWidth + pixels;
71 - sq.style.left = 0 + pixels;
72 - sq.style.top = 0 + pixels;
 66+ sq.style.left = 0;
 67+ sq.style.top = 0;
7368 sq.style.height = 40 + pixels;
7469 sq.style.width = document.body.clientWidth + pixels;
7570 search.style.position = 'absolute';
@@ -76,7 +71,7 @@
7772 search.style.height = 34 + pixels;
7873 search.style.width = ( document.body.clientWidth - 90 ) + pixels;
7974 search.style.fontSize = 16 + pixels;
80 - results.style.left = 0 + pixels;
 75+ results.style.left = 0;
8176 results.style.top = ( sq.offsetTop + sq.offsetHeight ) + pixels;
8277 results.style.width = document.body.clientWidth + pixels;
8378 results.style.minHeight = '100%';
@@ -84,19 +79,19 @@
8580 results.style.backgroundColor = '#E6E6E6';
8681 results.style.paddingTop = 5 + pixels;
8782 results.style.display = 'block';
88 - sb.style.border = 0 + pixels;
 83+ sb.style.border = 0;
8984 logo.style.visibility = 'hidden';
9085 goButton.style.visibility = 'hidden';
9186
92 - var pE = document.getElementById( 'placeholder' );
 87+ pE = document.getElementById( 'placeholder' );
9388 if ( !pE ) {
9489 pT = document.createElement( 'span' );
95 - var pTT = document.createTextNode(placeholder);
 90+ pTT = document.createTextNode(placeholder);
9691 pT.setAttribute( 'id', 'placeholder' );
9792 pT.appendChild(pTT);
9893 sb.insertBefore( pT, sb.firstChild );
9994 }
100 - var pE = document.getElementById( 'placeholder' );
 95+ pE = document.getElementById( 'placeholder' );
10196 if ( pE ) {
10297 pE.style.position = 'absolute';
10398 pE.style.left = ( search.offsetLeft + 5 ) + pixels;
@@ -107,11 +102,11 @@
108103 search.style.backgroundColor = 'transparent';
109104 }
110105
111 - if ( pE && search.value != '' ) {
 106+ if ( pE && search.value !== '' ) {
112107 pE.style.display = 'none';
113108 }
114109
115 - var removeResultsEl = document.getElementById( 'remove-results' );
 110+ removeResultsEl = document.getElementById( 'remove-results' );
116111 if ( !removeResultsEl ) {
117112 rrd = document.createElement( 'a' );
118113 rrd.setAttribute( 'href', '#' );
@@ -126,9 +121,10 @@
127122 }
128123 focused = true;
129124 }
130 - }
 125+ };
131126
132127 function removeResults() {
 128+ var removeResultsEl, pE = document.getElementById( 'placeholder' );
133129 if ( content ) {
134130 content.style.display = 'block';
135131 }
@@ -136,7 +132,6 @@
137133 footer.style.display = 'block';
138134 }
139135
140 - var pE = document.getElementById( 'placeholder' );
141136 if ( pE ) {
142137 pE.style.display = 'none';
143138 }
@@ -164,9 +159,9 @@
165160 }
166161 if ( sb ) {
167162 sb.style.border = 'solid #CCC 1px';
168 - var removeResults = document.getElementById( 'remove-results' );
169 - if ( removeResults ) {
170 - removeResults.style.display = 'none';
 163+ removeResultsEl = document.getElementById( 'remove-results' );
 164+ if ( removeResultsEl ) {
 165+ removeResultsEl.style.display = 'none';
171166 }
172167 }
173168 if ( focused ) {
@@ -181,7 +176,7 @@
182177 function whichElement( e ) {
183178 var targ;
184179 if ( !e ) {
185 - var e = window.event;
 180+ e = window.event;
186181 }
187182 if ( e.target ) {
188183 targ = e.target;
@@ -189,24 +184,24 @@
190185 targ = e.srcElement;
191186 }
192187
193 - if ( targ.nodeType == 3 ) {
 188+ if ( targ.nodeType === 3 ) {
194189 targ = targ.parentNode;
195190 }
196191
197192 e.cancelBubble = true;
198193 e.stopPropagation();
199 - if ( targ.className == "suggestion-result" ||
200 - targ.className == "search-result-item" ||
201 - targ.className == "suggestions-result" ||
202 - targ.className == "sq-val-update" ||
203 - targ.id == 'results' ||
204 - targ.id == 'search' ||
205 - targ.id == 'searchbox' ||
206 - targ.id == 'sq' ||
207 - targ.id == 'placeholder' ||
208 - targ.id == 'clearsearch' ||
209 - targ.tagName == 'BODY' ) {
210 - if ( targ.id == 'clearsearch' && results ) {
 194+ if ( targ.className === "suggestion-result" ||
 195+ targ.className === "search-result-item" ||
 196+ targ.className === "suggestions-result" ||
 197+ targ.className === "sq-val-update" ||
 198+ targ.id === 'results' ||
 199+ targ.id === 'search' ||
 200+ targ.id === 'searchbox' ||
 201+ targ.id === 'sq' ||
 202+ targ.id === 'placeholder' ||
 203+ targ.id === 'clearsearch' ||
 204+ targ.tagName === 'BODY' ) {
 205+ if ( targ.id === 'clearsearch' && results ) {
211206 results.innerHTML = '';
212207 }
213208 } else {
@@ -216,7 +211,7 @@
217212
218213 function updateSearchWidth() {
219214 if ( sq && search && sb ) {
220 - var iw = ( document.documentElement.clientWidth ) ? document.documentElement.clientWidth : document.body.clientWidth;
 215+ var iw = document.documentElement.clientWidth || document.body.clientWidth;
221216 sb.style.width = ( iw - 30 ) + pixels;
222217 sq.style.width = ( iw - 110 ) + pixels;
223218 search.style.width = ( iw - 130 ) + pixels;
@@ -224,7 +219,7 @@
225220 results.style.width = ( sq.offsetWidth - 2 ) + pixels;
226221 results.style.left = sq.offsetLeft + pixels;
227222 results.style.top = ( sq.offsetTop + sq.offsetHeight ) + pixels;
228 - if ( results.style.display == 'block' ) {
 223+ if ( results.style.display === 'block' ) {
229224 focused = false;
230225 search.blur();
231226 search.focus();
@@ -241,7 +236,7 @@
242237 case -90:
243238 case 90:
244239 case 180:
245 - setTimeout( "updateSearchWidth()", 300 );
 240+ setTimeout( updateSearchWidth, 300 );
246241 break;
247242 }
248243 }
@@ -261,10 +256,10 @@
262257 timer = setTimeout( function () { searchApi( term ); }, typingDelay );
263258 }
264259 }, false );
265 - }
 260+ };
266261
267262 function searchApi( term ) {
268 - var xmlHttp;
 263+ var xmlHttp, url;
269264 if ( window.XMLHttpRequest ) {
270265 xmlHttp = new XMLHttpRequest();
271266 } else {
@@ -272,25 +267,25 @@
273268 }
274269 xmlHttp.overrideMimeType( 'text/xml' );
275270 xmlHttp.onreadystatechange = function() {
276 - if ( xmlHttp.readyState == 4 && xmlHttp.status == 200 ) {
 271+ if ( xmlHttp.readyState === 4 && xmlHttp.status === 200 ) {
277272 var sections = createObjectArray( xmlHttp.responseXML );
278273 writeResults( sections );
279274 }
280 - }
281 - var url = apiUrl + '?action=opensearch&limit=' + numResults + '&namespace=0&format=xml&search=' + term;
 275+ };
 276+ url = apiUrl + '?action=opensearch&limit=' + numResults + '&namespace=0&format=xml&search=' + term;
282277 xmlHttp.open( 'GET', url, true );
283278 xmlHttp.send();
284279 }
285280
286281 function createObjectArray( responseXml ) {
287 - var sections = new Array();
288 - var items = responseXml.getElementsByTagName( 'Item' );
 282+ var sections = [], i, item, section,
 283+ items = responseXml.getElementsByTagName( 'Item' );
289284 for ( i = 0; i < items.length; i++ ) {
290 - var item = items[i];
291 - var section = {
 285+ item = items[i];
 286+ section = {
292287 label: item.getElementsByTagName( 'Text' )[0].textContent,
293 - value: item.getElementsByTagName( 'Url' )[0].textContent,
294 - }
 288+ value: item.getElementsByTagName( 'Url' )[0].textContent
 289+ };
295290 sections.push( section );
296291 }
297292 return sections;
@@ -314,8 +309,10 @@
315310 }
316311
317312 function writeResults( sections ) {
318 - var results = document.getElementById( 'results' );
319 - var term = htmlEntities( document.getElementById( 'search' ).value );
 313+ var results = document.getElementById( 'results' ), suggestions, i,
 314+ term = htmlEntities( document.getElementById( 'search' ).value ),
 315+ suggestionListener, section, escapedTerm, suggestionsResult, link, label;
 316+
320317 results.style.display = 'block';
321318 if ( search ) {
322319 search.focus();
@@ -326,21 +323,24 @@
327324 if( results.firstChild ) {
328325 results.removeChild( results.firstChild );
329326 }
330 - var suggestions = document.createElement( 'div' );
 327+ suggestions = document.createElement( 'div' );
331328 suggestions.className = 'suggestions-results';
332329 results.appendChild( suggestions );
 330+ suggestionListener = function() {
 331+ var title = this.parentNode.getAttribute( 'title' );
 332+ sqValUpdate( title );
 333+ };
 334+
333335 for ( i = 0; i < sections.length; i++ ) {
334 - var section = sections[i], suggestionsResult = document.createElement( 'div' ),
335 - link = document.createElement( 'a' ), label;
 336+ section = sections[i];
 337+ suggestionsResult = document.createElement( 'div' );
 338+ link = document.createElement( 'a' );
336339 suggestionsResult.setAttribute( 'title', section.label );
337340 suggestionsResult.className = 'suggestions-result';
338341 label = document.createTextNode( '+' );
339342 link.appendChild(label);
340343 link.className = 'sq-val-update';
341 - link.addEventListener( 'click', function() {
342 - var title = this.parentNode.getAttribute( 'title' );
343 - sqValUpdate( title );
344 - });
 344+ link.addEventListener( 'click', suggestionListener );
345345 suggestionsResult.appendChild( link );
346346
347347 link = document.createElement( 'a' );
@@ -353,7 +353,7 @@
354354 suggestions.appendChild( suggestionsResult );
355355 // TODO: simplify the highlighting code to not use htmlEntities
356356 // highlight matched term
357 - var escapedTerm = escapeJsString( term );
 357+ escapedTerm = escapeJsString( term );
358358 link.innerHTML = link.innerHTML.replace( new RegExp( '(' + escapedTerm + ')' , 'ig'),
359359 '<strong>$1</strong>' );
360360 }
@@ -384,4 +384,4 @@
385385 removeResults: removeResults
386386 };
387387
388 -}();
 388+}());
\ No newline at end of file

Status & tagging log