r112223 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r112222‎ | r112223 | r112224 >
Date:17:15, 23 February 2012
Author:jdlrobson
Status:ok
Tags:
Comment:
shift search related code from application.js to opensearch.js

it makes more sense if all the search related code is in one place
Modified paths:
  • /trunk/extensions/MobileFrontend/javascripts/application.js (modified) (history)
  • /trunk/extensions/MobileFrontend/javascripts/beta_application.js (modified) (history)
  • /trunk/extensions/MobileFrontend/javascripts/beta_opensearch.js (modified) (history)
  • /trunk/extensions/MobileFrontend/javascripts/opensearch.js (modified) (history)
  • /trunk/extensions/MobileFrontend/tests/js/test_application.js (modified) (history)
  • /trunk/extensions/MobileFrontend/tests/js/test_opensearch.js (modified) (history)

Diff [purge]

Index: trunk/extensions/MobileFrontend/tests/js/test_opensearch.js
@@ -1,5 +1,50 @@
22 var MFEOS = MobileFrontend.opensearch;
33 var _ajax;
 4+
 5+module("MobileFrontend application.js: clear search", {
 6+ setup: function() {
 7+ $(['<div id="clearsearchtest"><div id="results"></div><input type="text" id="search">',
 8+ '<button id="clearsearch" title="Clear" style="display:none;">clear</button></div>'].join("")).appendTo(document.body);
 9+ MFEOS.initClearSearch();
 10+ },
 11+ teardown: function() {
 12+ $("#clearsearchtest").remove();
 13+ }
 14+});
 15+
 16+test("setup", function() {
 17+ strictEqual($("#clearsearch").attr("title"), "Clear", "check clearsearch tooltip");
 18+});
 19+
 20+test("reveal clearsearch on text", function() {
 21+ $("#search").val("hello");
 22+ var initialVisibility = $("#clearsearch").is(":visible");
 23+ MFET.triggerEvent($("#search")[0], "keyup")
 24+ strictEqual(initialVisibility, false, "at start clear button should be hidden.")
 25+ strictEqual($("#clearsearch").is(":visible"), true, "clear search is now visible");
 26+});
 27+
 28+test("hide clearsearch when no text", function() {
 29+ $("#clearsearch").show();
 30+ $("#search").val("");
 31+ var initialVisibility = $("#clearsearch").is(":visible");
 32+ MFET.triggerEvent($("#search")[0], "keyup");
 33+ strictEqual(initialVisibility, true, "at start we made it visible")
 34+ strictEqual($("#clearsearch").is("visible"), false, "now invisible due to lack of text in input");
 35+ strictEqual($("#results").is("visible"), false, "results also hidden");
 36+});
 37+
 38+test("click clearSearchBox", function() {
 39+ $("#search").val("hello world");
 40+ $("#results,#clearsearch").show();
 41+
 42+ MFET.triggerEvent($("#clearsearch")[0], "mousedown")
 43+
 44+ strictEqual($("#search").val(), "", "value reset");
 45+ strictEqual($("#results").is(":visible"), false, "results hidden");
 46+ strictEqual($("#clearsearch").is(":visible"), false, "clear search hidden");
 47+});
 48+
449 module("MobileFrontend opensearch.js - writeResults", {
550 setup: function() {
651 _ajax = MobileFrontend.utils.ajax;
Index: trunk/extensions/MobileFrontend/tests/js/test_application.js
@@ -75,50 +75,6 @@
7676 strictEqual(cookieEnd, "off", "banner now set for dismissal");
7777 });
7878
79 -module("MobileFrontend application.js: clear search", {
80 - setup: function() {
81 - MFET.createFixtures();
82 - MFE.init();
83 - $("#clearsearch").hide();
84 - },
85 - teardown: function() {
86 - MFET.cleanFixtures();
87 - }
88 -});
89 -
90 -test("setup", function() {
91 - strictEqual($("#clearsearch").attr("title"), "Clear", "check clearsearch tooltip");
92 -});
93 -
94 -test("reveal clearsearch on text", function() {
95 - $("#search").val("hello");
96 - var initialVisibility = $("#clearsearch").is(":visible");
97 - MFET.triggerEvent($("#search")[0], "keyup")
98 - strictEqual(initialVisibility, false, "at start clear button should be hidden.")
99 - strictEqual($("#clearsearch").is(":visible"), true, "clear search is now visible");
100 -});
101 -
102 -test("hide clearsearch when no text", function() {
103 - $("#clearsearch").show();
104 - $("#search").val("");
105 - var initialVisibility = $("#clearsearch").is(":visible");
106 - MFET.triggerEvent($("#search")[0], "keyup");
107 - strictEqual(initialVisibility, true, "at start we made it visible")
108 - strictEqual($("#clearsearch").is("visible"), false, "now invisible due to lack of text in input");
109 - strictEqual($("#results").is("visible"), false, "results also hidden");
110 -});
111 -
112 -test("click clearSearchBox", function() {
113 - $("#search").val("hello world");
114 - $("#results,#clearsearch").show();
115 -
116 - MFET.triggerEvent($("#clearsearch")[0], "mousedown")
117 -
118 - strictEqual($("#search").val(), "", "value reset");
119 - strictEqual($("#results").is(":visible"), false, "results hidden");
120 - strictEqual($("#clearsearch").is(":visible"), false, "clear search hidden");
121 -});
122 -
12379 module("MobileFrontend application.js: logo click", {
12480 setup: function() {
12581 MFET.createFixtures();
Index: trunk/extensions/MobileFrontend/javascripts/opensearch.js
@@ -136,6 +136,42 @@
137137 }
138138 }
139139
 140+ function initClearSearch() {
 141+ var clearSearch = document.getElementById( 'clearsearch' ),
 142+ search = document.getElementById( 'search' ),
 143+ results = document.getElementById( 'results' );
 144+ function handleClearSearchLink() {
 145+ if ( clearSearch ) {
 146+ if ( search.value.length > 0 ) {
 147+ clearSearch.style.display = 'block';
 148+ } else {
 149+ clearSearch.style.display = 'none';
 150+ if ( results ) {
 151+ results.style.display = 'none';
 152+ }
 153+ }
 154+ }
 155+ }
 156+
 157+ function clearSearchBox( event ) {
 158+ search.value = '';
 159+ clearSearch.style.display = 'none';
 160+ if ( results ) {
 161+ results.style.display = 'none';
 162+ }
 163+ if ( event ) {
 164+ event.preventDefault();
 165+ }
 166+ }
 167+
 168+ function onFocusHandler() {
 169+ search.select();
 170+ }
 171+ u( clearSearch ).bind( 'mousedown', clearSearchBox, true );
 172+ u( search ).bind( 'keyup', handleClearSearchLink, false );
 173+ u( search ).bind( 'click', onFocusHandler, true );
 174+ }
 175+
140176 function init() {
141177 var results = document.getElementById( 'results' );
142178 results.onmousedown = whichElement;
@@ -144,9 +180,11 @@
145181 results.ontouchstart = whichElement;
146182 }
147183 init();
 184+ initClearSearch();
148185
149186 return {
150187 init: init,
 188+ initClearSearch: initClearSearch,
151189 writeResults: writeResults,
152190 createObjectArray: createObjectArray
153191 };
Index: trunk/extensions/MobileFrontend/javascripts/beta_application.js
@@ -32,24 +32,12 @@
3333 heading.insertBefore( createButton( false ), heading.firstChild );
3434 utilities( heading ).bind( 'click', openSectionHandler, false );
3535 }
36 - search = document.getElementById( 'search' );
37 - clearSearch = document.getElementById( 'clearsearch' );
3836 results = document.getElementById( 'results' );
3937 languageSelection = document.getElementById( 'languageselection' );
4038
4139 zeroRatedBanner = document.getElementById( 'zero-rated-banner' ) ||
4240 document.getElementById( 'zero-rated-banner-red' );
4341
44 - function initClearSearchLink() {
45 - function onFocusHandler() {
46 - search.select();
47 - }
48 - utilities( clearSearch ).bind( 'mousedown', clearSearchBox, true );
49 - utilities( search ).bind( 'keyup', handleClearSearchLink, false );
50 - utilities( search ).bind( 'keydown', handleDefaultText, false );
51 - utilities( search ).bind( 'click', onFocusHandler, true );
52 - }
53 -
5442 function navigateToLanguageSelection() {
5543 var url;
5644 if ( languageSelection ) {
@@ -61,39 +49,10 @@
6250 }
6351 utilities( languageSelection ).bind( 'change', navigateToLanguageSelection );
6452
65 - function handleDefaultText() {
66 - var pE = document.getElementById( 'placeholder' );
67 - if ( pE ) {
68 - pE.style.display = 'none';
69 - }
70 - }
71 -
72 - function handleClearSearchLink() {
73 - if ( clearSearch ) {
74 - if ( search.value.length > 0 ) {
75 - clearSearch.style.display = 'block';
76 - } else {
77 - clearSearch.style.display = 'none';
78 - }
79 - }
80 - }
81 -
82 - function clearSearchBox( event ) {
83 - search.value = '';
84 - clearSearch.style.display = 'none';
85 - if ( event ) {
86 - event.preventDefault();
87 - }
88 - }
89 -
9053 function logoClick() {
9154 var n = document.getElementById( 'nav' ).style;
9255 n.display = n.display === 'block' ? 'none' : 'block';
9356 }
94 - initClearSearchLink();
95 - search.onpaste = function() {
96 - handleDefaultText();
97 - };
9857 utilities( document.getElementById( 'logo' ) ).bind( 'click', logoClick );
9958 dismissNotification = document.getElementById( 'dismiss-notification' );
10059
Index: trunk/extensions/MobileFrontend/javascripts/beta_opensearch.js
@@ -244,17 +244,58 @@
245245 }
246246 }
247247
 248+ function handleDefaultText() {
 249+ var pE = document.getElementById( 'placeholder' );
 250+ if ( pE ) {
 251+ pE.style.display = 'none';
 252+ }
 253+ }
 254+
 255+ function initClearSearch() {
 256+ var clearSearch = document.getElementById( 'clearsearch' ),
 257+ search = document.getElementById( 'search' ),
 258+ results = document.getElementById( 'results' );
 259+ function handleClearSearchLink() {
 260+ if ( clearSearch ) {
 261+ if ( search.value.length > 0 ) {
 262+ clearSearch.style.display = 'block';
 263+ } else {
 264+ clearSearch.style.display = 'none';
 265+ }
 266+ }
 267+ }
 268+
 269+ function clearSearchBox( event ) {
 270+ search.value = '';
 271+ clearSearch.style.display = 'none';
 272+ if ( event ) {
 273+ event.preventDefault();
 274+ }
 275+ }
 276+
 277+ function onFocusHandler() {
 278+ search.select();
 279+ }
 280+ u( clearSearch ).bind( 'mousedown', clearSearchBox, true );
 281+ u( search ).bind( 'keyup', handleClearSearchLink, false );
 282+ u( search ).bind( 'keydown', handleDefaultText, false );
 283+ u( search ).bind( 'click', onFocusHandler, true );
 284+ }
 285+
248286 function init() {
249287 var results = document.getElementById( 'results' );
250288 results.onmousedown = whichElement;
251289 document.body.onmousedown = whichElement;
252290 document.body.ontouchstart = whichElement;
253291 results.ontouchstart = whichElement;
 292+ search.onpaste = handleDefaultText;
254293 }
255294 init();
 295+ initClearSearch();
256296
257297 return {
258298 init: init,
 299+ initClearSearch: initClearSearch,
259300 writeResults: writeResults,
260301 createObjectArray: createObjectArray,
261302 removeResults: removeResults
Index: trunk/extensions/MobileFrontend/javascripts/application.js
@@ -3,7 +3,7 @@
44 MobileFrontend = (function() {
55
66 function init() {
7 - var i, search, clearSearch, results, languageSelection, a, heading,
 7+ var i, results, languageSelection, a, heading,
88 sectionHeadings = document.getElementsByClassName( 'section_heading' ),
99 dismissNotification, cookieNameZeroVisibility, zeroRatedBanner, zeroRatedBannerVisibility;
1010 utilities( document.body ).addClass( 'jsEnabled' );
@@ -28,20 +28,9 @@
2929 heading.insertBefore( createButton( false ), heading.firstChild );
3030 utilities( heading ).bind( 'click', openSectionHandler, false );
3131 }
32 - search = document.getElementById( 'search' );
33 - clearSearch = document.getElementById( 'clearsearch' );
3432 results = document.getElementById( 'results' );
3533 languageSelection = document.getElementById( 'languageselection' );
3634
37 - function initClearSearchLink() {
38 - function onFocusHandler() {
39 - search.select();
40 - }
41 - utilities( clearSearch ).bind( 'mousedown', clearSearchBox, true );
42 - utilities( search ).bind( 'keyup', handleClearSearchLink, false );
43 - utilities( search ).bind( 'click', onFocusHandler, true );
44 - }
45 -
4635 function navigateToLanguageSelection() {
4736 var url;
4837 if ( languageSelection ) {
@@ -53,35 +42,10 @@
5443 }
5544 utilities( languageSelection ).bind( 'change', navigateToLanguageSelection );
5645
57 - function handleClearSearchLink() {
58 - if ( clearSearch ) {
59 - if ( search.value.length > 0 ) {
60 - clearSearch.style.display = 'block';
61 - } else {
62 - clearSearch.style.display = 'none';
63 - if ( results ) {
64 - results.style.display = 'none';
65 - }
66 - }
67 - }
68 - }
69 -
70 - function clearSearchBox( event ) {
71 - search.value = '';
72 - clearSearch.style.display = 'none';
73 - if ( results ) {
74 - results.style.display = 'none';
75 - }
76 - if ( event ) {
77 - event.preventDefault();
78 - }
79 - }
80 -
8146 function logoClick() {
8247 var n = document.getElementById( 'nav' ).style;
8348 n.display = n.display === 'block' ? 'none' : 'block';
8449 }
85 - initClearSearchLink();
8650 utilities( document.getElementById( 'logo' ) ).bind( 'click', logoClick );
8751 dismissNotification = document.getElementById( 'dismiss-notification' );
8852

Status & tagging log