r112309 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r112308‎ | r112309 | r112310 >
Date:10:19, 24 February 2012
Author:jdlrobson
Status:ok
Tags:
Comment:
separate banner code from search code and toggle code

this separates all the banner code into a separate banner
added test file test_banner and moved tests from test_application
here

this allows us more flexibility to do things such as not serve banner
code for certain devices which don't need it.
have avoided creating a beta_banner for current time since changes between
application and beta_application so minimal (essentially beta banner checks
for #zero-rated-banner-red in absence of #zero-rated-banner
Modified paths:
  • /trunk/extensions/MobileFrontend/MobileFrontend.body.php (modified) (history)
  • /trunk/extensions/MobileFrontend/javascripts/application.js (modified) (history)
  • /trunk/extensions/MobileFrontend/javascripts/banner.js (added) (history)
  • /trunk/extensions/MobileFrontend/javascripts/beta_application.js (modified) (history)
  • /trunk/extensions/MobileFrontend/templates/ApplicationTemplate.php (modified) (history)
  • /trunk/extensions/MobileFrontend/tests/js/test_application.js (modified) (history)
  • /trunk/extensions/MobileFrontend/tests/js/test_banner.js (added) (history)

Diff [purge]

Index: trunk/extensions/MobileFrontend/templates/ApplicationTemplate.php
@@ -73,6 +73,7 @@
7474 {$this->data['footerHtml']}
7575 <!--[if gt IE 9]><!-->
7676 {$startScriptTag}{$javaScriptPath}{$betaPrefix}application.js?version=01132011120915{$endScriptTag}
 77+ {$startScriptTag}{$javaScriptPath}banner.js{$endScriptTag}
7778 {$openSearchScript}
7879 {$filePageScript}
7980 <!--[endif]-->
Index: trunk/extensions/MobileFrontend/tests/js/test_application.js
@@ -21,60 +21,6 @@
2222 strictEqual($(el).hasClass("bar"), false);
2323 });
2424
25 -module("MobileFrontend application.js: cookies");
26 -
27 -test("read and write cookies", function() {
28 - var cookie_name = "test_cookies_module";
29 - MFE.writeCookie(cookie_name, "yes", 400);
30 - var cookieVal = MFE.readCookie(cookie_name);
31 - strictEqual(cookieVal, "yes",
32 - "Are you running off localhost?");
33 -});
34 -
35 -test("read and write cookies with spaces", function() {
36 - var cookie_name = "test_cookies_module";
37 - MFE.writeCookie(cookie_name, " yes this has spaces ", 400);
38 - MFE.writeCookie(cookie_name + "2", " yes this has spaces ", 400);
39 - var cookieVal = MFE.readCookie(cookie_name);
40 - strictEqual(cookieVal, "yes this has spaces",
41 - "spaces are kept and trailing whitespace is removed");
42 -});
43 -
44 -test("remove cookie via write", function() {
45 - var cookie_name = "test_cookies_module";
46 - MFE.writeCookie(cookie_name, "", -1);
47 - var cookieVal = MFE.readCookie(cookie_name);
48 - strictEqual(cookieVal, null, "Cookie deleted");
49 -});
50 -
51 -var BANNER_COOKIE_NAME = "zeroRatedBannerVisibility";
52 -module("MobileFrontend application.js: notifications", {
53 - setup: function() {
54 - MFET.cleanFixtures();
55 - MFE.removeCookie(BANNER_COOKIE_NAME);
56 - MFET.createFixtures();
57 - MFE.init();
58 - },
59 - teardown: function() {
60 - MFET.cleanFixtures();
61 - MFE.removeCookie(BANNER_COOKIE_NAME);
62 - }
63 -});
64 -
65 -test("dismiss notification", function() {
66 - var cookieStart = MFE.readCookie(BANNER_COOKIE_NAME);
67 - strictEqual(cookieStart, null, "no cookie set at start");
68 - strictEqual($("#zero-rated-banner").is(":visible"), true, "banner should be on show");
69 -
70 - // trigger dismiss event
71 - $("#dismiss-notification").trigger("click");
72 -
73 - var cookieEnd = MFE.readCookie(BANNER_COOKIE_NAME);
74 - strictEqual(cookieStart, null, "no cookie set at start");
75 - strictEqual($("#zero-rated-banner").is(":visible"), false, "banner should now be hidden");
76 - strictEqual(cookieEnd, "off", "banner now set for dismissal");
77 -});
78 -
7925 module("MobileFrontend application.js: logo click", {
8026 setup: function() {
8127 MFET.createFixtures();
Index: trunk/extensions/MobileFrontend/tests/js/test_banner.js
@@ -0,0 +1,56 @@
 2+var MFEB = MobileFrontend.banner;
 3+var MFEBT = window.MobileFrontendTests;
 4+
 5+module("MobileFrontend banner.js: cookies");
 6+
 7+test("read and write cookies", function() {
 8+ var cookie_name = "test_cookies_module";
 9+ MFEB.writeCookie(cookie_name, "yes", 400);
 10+ var cookieVal = MFEB.readCookie(cookie_name);
 11+ strictEqual(cookieVal, "yes",
 12+ "Are you running off localhost?");
 13+});
 14+
 15+test("read and write cookies with spaces", function() {
 16+ var cookie_name = "test_cookies_module";
 17+ MFEB.writeCookie(cookie_name, " yes this has spaces ", 400);
 18+ MFEB.writeCookie(cookie_name + "2", " yes this has spaces ", 400);
 19+ var cookieVal = MFEB.readCookie(cookie_name);
 20+ strictEqual(cookieVal, "yes this has spaces",
 21+ "spaces are kept and trailing whitespace is removed");
 22+});
 23+
 24+test("remove cookie via write", function() {
 25+ var cookie_name = "test_cookies_module";
 26+ MFEB.writeCookie(cookie_name, "", -1);
 27+ var cookieVal = MFEB.readCookie(cookie_name);
 28+ strictEqual(cookieVal, null, "Cookie deleted");
 29+});
 30+
 31+var BANNER_COOKIE_NAME = "zeroRatedBannerVisibility";
 32+module("MobileFrontend application.js: notifications", {
 33+ setup: function() {
 34+ MFEBT.cleanFixtures();
 35+ MFEB.removeCookie(BANNER_COOKIE_NAME);
 36+ MFEBT.createFixtures();
 37+ MFEB.init();
 38+ },
 39+ teardown: function() {
 40+ MFEBT.cleanFixtures();
 41+ MFEB.removeCookie(BANNER_COOKIE_NAME);
 42+ }
 43+});
 44+
 45+test("MobileFrontend banner.js: dismiss notification", function() {
 46+ var cookieStart = MFEB.readCookie(BANNER_COOKIE_NAME);
 47+ strictEqual(cookieStart, null, "no cookie set at start");
 48+ strictEqual($("#zero-rated-banner").is(":visible"), true, "banner should be on show");
 49+
 50+ // trigger dismiss event
 51+ $("#dismiss-notification").trigger("click");
 52+
 53+ var cookieEnd = MFEB.readCookie(BANNER_COOKIE_NAME);
 54+ strictEqual(cookieStart, null, "no cookie set at start");
 55+ strictEqual($("#zero-rated-banner").is(":visible"), false, "banner should now be hidden");
 56+ strictEqual(cookieEnd, "off", "banner now set for dismissal");
 57+});
Index: trunk/extensions/MobileFrontend/MobileFrontend.body.php
@@ -1304,8 +1304,8 @@
13051305 public function addTestModules( array &$testModules, ResourceLoader &$resourceLoader ) {
13061306 $testModules['qunit']['ext.mobilefrontend.tests'] = array(
13071307 'scripts' => array( 'tests/js/fixtures.js', 'javascripts/application.js',
1308 - 'javascripts/opensearch.js',
1309 - 'tests/js/test_application.js', 'tests/js/test_opensearch.js' ),
 1308+ 'javascripts/opensearch.js', 'javascripts/banner.js',
 1309+ 'tests/js/test_application.js', 'tests/js/test_opensearch.js', 'tests/js/test_banner.js' ),
13101310 'dependencies' => array( ),
13111311 'localBasePath' => dirname( __FILE__ ),
13121312 'remoteExtPath' => 'MobileFrontend',
Index: trunk/extensions/MobileFrontend/javascripts/beta_application.js
@@ -8,8 +8,7 @@
99
1010 function init() {
1111 var i, search, clearSearch, results, languageSelection, a, heading,
12 - sectionHeadings = document.getElementsByClassName( 'section_heading' ),
13 - dismissNotification, cookieNameZeroVisibility, zeroRatedBanner, zeroRatedBannerVisibility;
 12+ sectionHeadings = document.getElementsByClassName( 'section_heading' );
1413 utilities( document.body ).addClass( 'jsEnabled' );
1514 function openSectionHandler() {
1615 var sectionNumber = this.id ? this.id.split( '_' )[1] : -1;
@@ -35,9 +34,6 @@
3635 results = document.getElementById( 'results' );
3736 languageSelection = document.getElementById( 'languageselection' );
3837
39 - zeroRatedBanner = document.getElementById( 'zero-rated-banner' ) ||
40 - document.getElementById( 'zero-rated-banner-red' );
41 -
4238 function navigateToLanguageSelection() {
4339 var url;
4440 if ( languageSelection ) {
@@ -54,25 +50,7 @@
5551 n.display = n.display === 'block' ? 'none' : 'block';
5652 }
5753 utilities( document.getElementById( 'logo' ) ).bind( 'click', logoClick );
58 - dismissNotification = document.getElementById( 'dismiss-notification' );
5954
60 - if ( dismissNotification ) {
61 - cookieNameZeroVisibility = 'zeroRatedBannerVisibility';
62 - zeroRatedBanner = document.getElementById( 'zero-rated-banner' );
63 - zeroRatedBannerVisibility = readCookie( cookieNameZeroVisibility );
64 -
65 - if ( zeroRatedBannerVisibility === 'off' ) {
66 - zeroRatedBanner.style.display = 'none';
67 - }
68 -
69 - dismissNotification.onclick = function() {
70 - if ( zeroRatedBanner ) {
71 - zeroRatedBanner.style.display = 'none';
72 - writeCookie( cookieNameZeroVisibility, 'off', 1 );
73 - }
74 - };
75 - }
76 -
7755 function checkHash() {
7856 var hash = this.hash || document.location.hash;
7957 if ( hash.indexOf( '#' ) === 0 ) {
@@ -120,39 +98,6 @@
12199 }
122100 }
123101
124 - function writeCookie( name, value, days ) {
125 - var date, expires;
126 - if ( days ) {
127 - date = new Date();
128 - date.setTime( date.getTime() + ( days * 24 * 60 * 60 *1000 ) );
129 - expires = '; expires=' + date.toGMTString();
130 - } else {
131 - expires = '';
132 - }
133 - document.cookie = name + '=' + value + expires + '; path=/';
134 - }
135 -
136 - function readCookie( name ) {
137 - var nameVA = name + '=',
138 - ca = document.cookie.split( ';' ),
139 - c, i;
140 - for( i=0; i < ca.length; i++ ) {
141 - c = ca[i];
142 - while ( c.charAt(0) === ' ' ) {
143 - c = c.substring( 1, c.length );
144 - }
145 - if ( c.indexOf( nameVA ) === 0 ) {
146 - return c.substring( nameVA.length, c.length );
147 - }
148 - }
149 - return null;
150 - }
151 -
152 - function removeCookie( name ) {
153 - writeCookie( name, '', -1 );
154 - return null;
155 - }
156 -
157102 function utilities( el ) {
158103 function addClass( name ) {
159104 var className = el.className,
@@ -200,9 +145,6 @@
201146 };
202147
203148 return {
204 - readCookie: readCookie,
205 - writeCookie: writeCookie,
206 - removeCookie: removeCookie,
207149 wm_reveal_for_hash: wm_reveal_for_hash,
208150 wm_toggle_section: wm_toggle_section,
209151 init: init,
Index: trunk/extensions/MobileFrontend/javascripts/application.js
@@ -4,8 +4,7 @@
55
66 function init() {
77 var i, results, languageSelection, a, heading,
8 - sectionHeadings = document.getElementsByClassName( 'section_heading' ),
9 - dismissNotification, cookieNameZeroVisibility, zeroRatedBanner, zeroRatedBannerVisibility;
 8+ sectionHeadings = document.getElementsByClassName( 'section_heading' );
109 utilities( document.body ).addClass( 'jsEnabled' );
1110 function openSectionHandler() {
1211 var sectionNumber = this.id ? this.id.split( '_' )[1] : -1;
@@ -47,25 +46,7 @@
4847 n.display = n.display === 'block' ? 'none' : 'block';
4948 }
5049 utilities( document.getElementById( 'logo' ) ).bind( 'click', logoClick );
51 - dismissNotification = document.getElementById( 'dismiss-notification' );
5250
53 - if ( dismissNotification ) {
54 - cookieNameZeroVisibility = 'zeroRatedBannerVisibility';
55 - zeroRatedBanner = document.getElementById( 'zero-rated-banner' );
56 - zeroRatedBannerVisibility = readCookie( cookieNameZeroVisibility );
57 -
58 - if ( zeroRatedBannerVisibility === 'off' ) {
59 - zeroRatedBanner.style.display = 'none';
60 - }
61 -
62 - dismissNotification.onclick = function() {
63 - if ( zeroRatedBanner ) {
64 - zeroRatedBanner.style.display = 'none';
65 - writeCookie( cookieNameZeroVisibility, 'off', 1 );
66 - }
67 - };
68 - }
69 -
7051 function checkHash() {
7152 var hash = this.hash || document.location.hash;
7253 if ( hash.indexOf( '#' ) === 0 ) {
@@ -113,39 +94,6 @@
11495 }
11596 }
11697
117 - function writeCookie( name, value, days ) {
118 - var date, expires;
119 - if ( days ) {
120 - date = new Date();
121 - date.setTime( date.getTime() + ( days * 24 * 60 * 60 *1000 ) );
122 - expires = '; expires=' + date.toGMTString();
123 - } else {
124 - expires = '';
125 - }
126 - document.cookie = name + '=' + value + expires + '; path=/';
127 - }
128 -
129 - function readCookie( name ) {
130 - var nameVA = name + '=',
131 - ca = document.cookie.split( ';' ),
132 - c, i;
133 - for( i=0; i < ca.length; i++ ) {
134 - c = ca[i];
135 - while ( c.charAt(0) === ' ' ) {
136 - c = c.substring( 1, c.length );
137 - }
138 - if ( c.indexOf( nameVA ) === 0 ) {
139 - return c.substring( nameVA.length, c.length );
140 - }
141 - }
142 - return null;
143 - }
144 -
145 - function removeCookie( name ) {
146 - writeCookie( name, '', -1 );
147 - return null;
148 - }
149 -
15098 function utilities( el ) {
15199 function addClass( name ) {
152100 var className = el.className,
@@ -193,9 +141,6 @@
194142 };
195143
196144 return {
197 - readCookie: readCookie,
198 - writeCookie: writeCookie,
199 - removeCookie: removeCookie,
200145 wm_reveal_for_hash: wm_reveal_for_hash,
201146 wm_toggle_section: wm_toggle_section,
202147 init: init,
Index: trunk/extensions/MobileFrontend/javascripts/banner.js
@@ -0,0 +1,64 @@
 2+MobileFrontend.banner = (function() {
 3+ function init() {
 4+ var dismissNotification, cookieNameZeroVisibility, zeroRatedBanner, zeroRatedBannerVisibility;
 5+ dismissNotification = document.getElementById( 'dismiss-notification' );
 6+ if ( dismissNotification ) {
 7+ cookieNameZeroVisibility = 'zeroRatedBannerVisibility';
 8+ zeroRatedBanner = document.getElementById( 'zero-rated-banner' ) ||
 9+ document.getElementById( 'zero-rated-banner-red' );
 10+ zeroRatedBannerVisibility = readCookie( cookieNameZeroVisibility );
 11+
 12+ if ( zeroRatedBannerVisibility === 'off' ) {
 13+ zeroRatedBanner.style.display = 'none';
 14+ }
 15+
 16+ dismissNotification.onclick = function() {
 17+ if ( zeroRatedBanner ) {
 18+ zeroRatedBanner.style.display = 'none';
 19+ writeCookie( cookieNameZeroVisibility, 'off', 1 );
 20+ }
 21+ };
 22+ }
 23+ }
 24+
 25+ function writeCookie( name, value, days ) {
 26+ var date, expires;
 27+ if ( days ) {
 28+ date = new Date();
 29+ date.setTime( date.getTime() + ( days * 24 * 60 * 60 *1000 ) );
 30+ expires = '; expires=' + date.toGMTString();
 31+ } else {
 32+ expires = '';
 33+ }
 34+ document.cookie = name + '=' + value + expires + '; path=/';
 35+ }
 36+
 37+ function readCookie( name ) {
 38+ var nameVA = name + '=',
 39+ ca = document.cookie.split( ';' ),
 40+ c, i;
 41+ for( i=0; i < ca.length; i++ ) {
 42+ c = ca[i];
 43+ while ( c.charAt(0) === ' ' ) {
 44+ c = c.substring( 1, c.length );
 45+ }
 46+ if ( c.indexOf( nameVA ) === 0 ) {
 47+ return c.substring( nameVA.length, c.length );
 48+ }
 49+ }
 50+ return null;
 51+ }
 52+
 53+ function removeCookie( name ) {
 54+ writeCookie( name, '', -1 );
 55+ return null;
 56+ }
 57+
 58+ init();
 59+ return {
 60+ init: init,
 61+ readCookie: readCookie,
 62+ writeCookie: writeCookie,
 63+ removeCookie: removeCookie
 64+ }
 65+})();

Status & tagging log