r111761 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r111760‎ | r111761 | r111762 >
Date:16:25, 17 February 2012
Author:jdlrobson
Status:ok
Tags:
Comment:
encapsulate opensearch.js

this encapsulates the opensearch javascript code
a few tweaks are necessary -
less reliance on caching so writing tests is possible
attaching click event to #remove-results element via
addEventListener and the renaming of the removeResults
variable so it can be distinguished from the removeResults
function
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,3 +1,5 @@
 2+MobileFrontend.opensearch = function() {
 3+
24 var apiUrl = '/api.php';
35
46 if ( scriptPath ) {
@@ -15,23 +17,10 @@
1618 var sb = document.getElementById( 'searchbox' );
1719
1820 function hideResults() {
 21+ var results = document.getElementById( 'results' );
1922 results.style.display = 'none';
2023 }
2124
22 -document.body.onmousedown = function( event ) {
23 - whichElement(event);
24 -}
25 -results.onmousedown = function( event ) {
26 - whichElement(event);
27 -}
28 -
29 -document.body.ontouchstart = function( event ) {
30 - whichElement(event);
31 -}
32 -results.ontouchstart = function( event ) {
33 - whichElement(event);
34 -}
35 -
3625 function whichElement( e ) {
3726 var targ;
3827 if ( !e ) {
@@ -137,6 +126,7 @@
138127 }
139128
140129 function sqValUpdate( sqValue ) {
 130+ var search = document.getElementById( 'search' );
141131 if ( search ) {
142132 search.value = sqValue + ' ';
143133 search.focus();
@@ -145,6 +135,7 @@
146136 }
147137
148138 function writeResults( sections ) {
 139+ var results = document.getElementById( 'results' );
149140 results.style.display = 'block';
150141 if ( !sections || sections.length < 1 ) {
151142 results.innerHTML = "No results";
@@ -178,4 +169,29 @@
179170 suggestions.appendChild( suggestionsResult );
180171 }
181172 }
182 -}
\ No newline at end of file
 173+}
 174+
 175+function init() {
 176+ var results = document.getElementById( 'results' );
 177+ results.onmousedown = function( event ) {
 178+ whichElement( event );
 179+ };
 180+ document.body.onmousedown = function( event ) {
 181+ whichElement( event );
 182+ };
 183+ document.body.ontouchstart = function( event ) {
 184+ whichElement( event );
 185+ };
 186+ results.ontouchstart = function( event ) {
 187+ whichElement( event );
 188+ };
 189+}
 190+init();
 191+
 192+return {
 193+ init: init,
 194+ writeResults: writeResults,
 195+ createObjectArray: createObjectArray
 196+};
 197+
 198+}();
Index: trunk/extensions/MobileFrontend/javascripts/beta_opensearch.js
@@ -1,3 +1,5 @@
 2+MobileFrontend.opensearch = function() {
 3+
24 var apiUrl = '/api.php';
35
46 if ( scriptPath ) {
@@ -109,18 +111,18 @@
110112 pE.style.display = 'none';
111113 }
112114
113 - var removeResults = document.getElementById( 'remove-results' );
114 - if ( !removeResults ) {
 115+ var removeResultsEl = document.getElementById( 'remove-results' );
 116+ if ( !removeResultsEl ) {
115117 rrd = document.createElement( 'a' );
116 - rrd.setAttribute( 'href', '#' );
 118+ rrd.setAttribute( 'href', '#' );
117119 rrd.setAttribute( 'id', 'remove-results' );
118 - rrd.setAttribute( 'onclick', 'removeResults();' );
 120+ rrd.addEventListener( 'click', removeResults );
119121 rrdD = document.createElement( 'div' );
120122 rrdD.setAttribute( 'id', 'left-arrow' );
121123 rrd.appendChild( rrdD );
122124 sq.insertBefore( rrd, sq.firstChild );
123125 } else {
124 - removeResults.style.display = 'block';
 126+ removeResultsEl.style.display = 'block';
125127 }
126128 focused = true;
127129 }
@@ -176,20 +178,6 @@
177179 }
178180 }
179181
180 -document.body.onmousedown = function( event ) {
181 - whichElement(event);
182 -}
183 -results.onmousedown = function( event ) {
184 - whichElement(event);
185 -}
186 -
187 -document.body.ontouchstart = function( event ) {
188 - whichElement(event);
189 -}
190 -results.ontouchstart = function( event ) {
191 - whichElement(event);
192 -}
193 -
194182 function whichElement( e ) {
195183 var targ;
196184 if ( !e ) {
@@ -309,6 +297,7 @@
310298 }
311299
312300 function sqValUpdate( sqValue ) {
 301+ var search = document.getElementById( 'search' );
313302 if ( search ) {
314303 search.value = sqValue + ' ';
315304 search.focus();
@@ -325,6 +314,7 @@
326315 }
327316
328317 function writeResults( sections ) {
 318+ var results = document.getElementById( 'results' );
329319 var term = htmlEntities( document.getElementById( 'search' ).value );
330320 results.style.display = 'block';
331321 if ( search ) {
@@ -368,4 +358,30 @@
369359 '<strong>$1</strong>' );
370360 }
371361 }
372 -}
\ No newline at end of file
 362+}
 363+
 364+function init() {
 365+ var results = document.getElementById( 'results' );
 366+ results.onmousedown = function( event ) {
 367+ whichElement( event );
 368+ };
 369+ document.body.onmousedown = function( event ) {
 370+ whichElement( event );
 371+ };
 372+ document.body.ontouchstart = function( event ) {
 373+ whichElement( event );
 374+ };
 375+ results.ontouchstart = function( event ) {
 376+ whichElement( event );
 377+ };
 378+}
 379+init();
 380+
 381+return {
 382+ init: init,
 383+ writeResults: writeResults,
 384+ createObjectArray: createObjectArray,
 385+ removeResults: removeResults
 386+};
 387+
 388+}();

Status & tagging log