r111677 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r111676‎ | r111677 | r111678 >
Date:20:43, 16 February 2012
Author:jdlrobson
Status:ok
Tags:
Comment:
wrap startup code in an init function

this is a first step towards writing unit tests as
it will allow us to initialise dom elements created
at runtime
Modified paths:
  • /trunk/extensions/MobileFrontend/javascripts/application.js (modified) (history)
  • /trunk/extensions/MobileFrontend/javascripts/beta_application.js (modified) (history)

Diff [purge]

Index: trunk/extensions/MobileFrontend/javascripts/beta_application.js
@@ -1,4 +1,3 @@
2 -document.body.className = "jsEnabled";
32 var search = document.getElementById( 'search' );
43 var clearSearch = document.getElementById( 'clearsearch' );
54 var results = document.getElementById( 'results' );
@@ -10,8 +9,6 @@
1110 var zeroRatedBanner = document.getElementById( 'zero-rated-banner-red' );
1211 }
1312
14 -initClearSearchLink();
15 -
1613 function initClearSearchLink() {
1714 clearSearch.setAttribute( 'title','Clear' );
1815 clearSearch.addEventListener( 'mousedown', clearSearchBox, true );
@@ -19,10 +16,6 @@
2017 search.addEventListener( 'keydown', handleDefaultText, false );
2118 }
2219
23 -search.onpaste = function() {
24 - handleDefaultText();
25 -};
26 -
2720 function navigateToLanguageSelection() {
2821 var url;
2922 if ( languageSelection ) {
@@ -71,26 +64,48 @@
7265 }
7366 };
7467
75 -for ( var a = document.getElementsByTagName( 'a' ), i = 0; i < a.length; i++ ) {
76 - a[i].onclick = function() {
77 - if ( this.hash.indexOf( '#' ) == 0 ) {
78 - wm_reveal_for_hash( this.hash );
 68+function init() {
 69+ document.body.className = "jsEnabled";
 70+ initClearSearchLink();
 71+ search.onpaste = function() {
 72+ handleDefaultText();
 73+ };
 74+ document.getElementById( 'logo' ).addEventListener( 'click', logoClick );
 75+ var dismissNotification = document.getElementById( 'dismiss-notification' );
 76+
 77+ if ( dismissNotification ) {
 78+ var cookieNameZeroVisibility = 'zeroRatedBannerVisibility';
 79+ var zeroRatedBanner = document.getElementById( 'zero-rated-banner' );
 80+ var zeroRatedBannerVisibility = readCookie( cookieNameZeroVisibility );
 81+
 82+ if ( zeroRatedBannerVisibility === 'off' ) {
 83+ zeroRatedBanner.style.display = 'none';
7984 }
 85+
 86+ dismissNotification.onclick = function() {
 87+ if ( zeroRatedBanner ) {
 88+ zeroRatedBanner.style.display = 'none';
 89+ writeCookie( cookieNameZeroVisibility, 'off', 1 );
 90+ }
 91+ };
8092 }
81 -}
 93+ if ( document.location.hash.indexOf( '#' ) == 0 ) {
 94+ wm_reveal_for_hash( document.location.hash );
 95+ }
8296
83 -function init() {
84 - document.getElementById( 'logo' ).addEventListener( 'click', logoClick );
 97+ for ( var a = document.getElementsByTagName( 'a' ), i = 0; i < a.length; i++ ) {
 98+ a[i].onclick = function() {
 99+ if ( this.hash.indexOf( '#' ) == 0 ) {
 100+ wm_reveal_for_hash( this.hash );
 101+ }
 102+ }
 103+ }
 104+
 105+ // Try to scroll and hide URL bar
 106+ window.scrollTo( 0, 1 );
85107 }
86108 init();
87109
88 -if ( document.location.hash.indexOf( '#' ) == 0 ) {
89 - wm_reveal_for_hash( document.location.hash );
90 -}
91 -
92 -// Try to scroll and hide URL bar
93 -window.scrollTo( 0, 1 );
94 -
95110 function wm_reveal_for_hash( hash ) {
96111 var targetel = document.getElementById( hash.substr(1) );
97112 if ( targetel ) {
@@ -150,22 +165,3 @@
151166 writeCookie( name, '', -1 );
152167 return null;
153168 }
154 -
155 -var dismissNotification = document.getElementById( 'dismiss-notification' );
156 -
157 -if ( dismissNotification ) {
158 - var cookieNameZeroVisibility = 'zeroRatedBannerVisibility';
159 - var zeroRatedBanner = document.getElementById( 'zero-rated-banner' );
160 - var zeroRatedBannerVisibility = readCookie( cookieNameZeroVisibility );
161 -
162 - if ( zeroRatedBannerVisibility === 'off' ) {
163 - zeroRatedBanner.style.display = 'none';
164 - }
165 -
166 - dismissNotification.onclick = function() {
167 - if ( zeroRatedBanner ) {
168 - zeroRatedBanner.style.display = 'none';
169 - writeCookie( cookieNameZeroVisibility, 'off', 1 );
170 - }
171 - };
172 -}
\ No newline at end of file
Index: trunk/extensions/MobileFrontend/javascripts/application.js
@@ -1,11 +1,8 @@
2 -document.body.className = "jsEnabled";
32 var search = document.getElementById( 'search' );
43 var clearSearch = document.getElementById( 'clearsearch' );
54 var results = document.getElementById( 'results' );
65 var languageSelection = document.getElementById( 'languageselection' );
76
8 -initClearSearchLink();
9 -
107 function initClearSearchLink() {
118 clearSearch.setAttribute( 'title','Clear' );
129 clearSearch.addEventListener( 'mousedown', clearSearchBox, true );
@@ -59,27 +56,45 @@
6057 }
6158 };
6259
63 -// And this...
64 -for ( var a = document.getElementsByTagName( 'a' ), i = 0; i < a.length; i++ ) {
65 - a[i].onclick = function() {
66 - if ( this.hash.indexOf( '#' ) == 0 ) {
67 - wm_reveal_for_hash( this.hash );
 60+function init() {
 61+ document.body.className = "jsEnabled";
 62+ initClearSearchLink();
 63+ document.getElementById( 'logo' ).addEventListener( 'click', logoClick );
 64+ var dismissNotification = document.getElementById( 'dismiss-notification' );
 65+
 66+ if ( dismissNotification ) {
 67+ var cookieNameZeroVisibility = 'zeroRatedBannerVisibility';
 68+ var zeroRatedBanner = document.getElementById( 'zero-rated-banner' );
 69+ var zeroRatedBannerVisibility = readCookie( cookieNameZeroVisibility );
 70+
 71+ if ( zeroRatedBannerVisibility === 'off' ) {
 72+ zeroRatedBanner.style.display = 'none';
6873 }
 74+
 75+ dismissNotification.onclick = function() {
 76+ if ( zeroRatedBanner ) {
 77+ zeroRatedBanner.style.display = 'none';
 78+ writeCookie( cookieNameZeroVisibility, 'off', 1 );
 79+ }
 80+ };
6981 }
70 -}
 82+ if ( document.location.hash.indexOf( '#' ) == 0 ) {
 83+ wm_reveal_for_hash( document.location.hash );
 84+ }
7185
72 -function init() {
73 - document.getElementById( 'logo' ).addEventListener( 'click', logoClick );
 86+ for ( var a = document.getElementsByTagName( 'a' ), i = 0; i < a.length; i++ ) {
 87+ a[i].onclick = function() {
 88+ if ( this.hash.indexOf( '#' ) == 0 ) {
 89+ wm_reveal_for_hash( this.hash );
 90+ }
 91+ }
 92+ }
 93+
 94+ // Try to scroll and hide URL bar
 95+ window.scrollTo( 0, 1 );
7496 }
7597 init();
7698
77 -if ( document.location.hash.indexOf( '#' ) == 0 ) {
78 - wm_reveal_for_hash( document.location.hash );
79 -}
80 -
81 -// Try to scroll and hide URL bar
82 -window.scrollTo( 0, 1 );
83 -
8499 function wm_reveal_for_hash( hash ) {
85100 var targetel = document.getElementById( hash.substr(1) );
86101 if ( targetel ) {
@@ -139,22 +154,3 @@
140155 writeCookie( name, '', -1 );
141156 return null;
142157 }
143 -
144 -var dismissNotification = document.getElementById( 'dismiss-notification' );
145 -
146 -if ( dismissNotification ) {
147 - var cookieNameZeroVisibility = 'zeroRatedBannerVisibility';
148 - var zeroRatedBanner = document.getElementById( 'zero-rated-banner' );
149 - var zeroRatedBannerVisibility = readCookie( cookieNameZeroVisibility );
150 -
151 - if ( zeroRatedBannerVisibility === 'off' ) {
152 - zeroRatedBanner.style.display = 'none';
153 - }
154 -
155 - dismissNotification.onclick = function() {
156 - if ( zeroRatedBanner ) {
157 - zeroRatedBanner.style.display = 'none';
158 - writeCookie( cookieNameZeroVisibility, 'off', 1 );
159 - }
160 - };
161 -}
\ No newline at end of file

Status & tagging log