r112101 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r112100‎ | r112101 | r112102 >
Date:12:13, 22 February 2012
Author:jdlrobson
Status:ok
Tags:
Comment:
jslint application and beta_application code
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,29 +1,31 @@
2 -MobileFrontend = function() {
 2+/*global document, window */
 3+/*jslint sloppy: true, white:true, maxerr: 50, indent: 4, plusplus: true*/
 4+MobileFrontend = (function() {
35
46 function init() {
 7+ var i, search, clearSearch, results, languageSelection, a,
 8+ sectionHeadings = document.getElementsByClassName( 'section_heading' ),
 9+ dismissNotification, cookieNameZeroVisibility, zeroRatedBanner, zeroRatedBannerVisibility;
510 document.body.className = "jsEnabled";
6 - var sectionHeadings = document.getElementsByClassName( 'section_heading' );
7 - for( var i = 0; i < sectionHeadings.length; i++ ) {
8 - sectionHeadings[i].addEventListener( 'click', function() {
9 - var sectionNumber = this.id ? this.id.split( '_' )[1] : -1;
10 - if(sectionNumber > -1) {
11 - wm_toggle_section( sectionNumber );
12 - }
13 - }, false );
 11+ function openSectionHandler() {
 12+ var sectionNumber = this.id ? this.id.split( '_' )[1] : -1;
 13+ if( sectionNumber > -1 ) {
 14+ wm_toggle_section( sectionNumber );
 15+ }
1416 }
15 - var search = document.getElementById( 'search' );
16 - var clearSearch = document.getElementById( 'clearsearch' );
17 - var results = document.getElementById( 'results' );
18 - var languageSelection = document.getElementById( 'languageselection' );
 17+ for( i = 0; i < sectionHeadings.length; i++ ) {
 18+ sectionHeadings[i].addEventListener( 'click', openSectionHandler, false );
 19+ }
 20+ search = document.getElementById( 'search' );
 21+ clearSearch = document.getElementById( 'clearsearch' );
 22+ results = document.getElementById( 'results' );
 23+ languageSelection = document.getElementById( 'languageselection' );
1924
20 - var zeroRatedBanner = document.getElementById( 'zero-rated-banner' );
 25+ zeroRatedBanner = document.getElementById( 'zero-rated-banner' ) ||
 26+ document.getElementById( 'zero-rated-banner-red' );
2127
22 - if ( !zeroRatedBanner ) {
23 - var zeroRatedBanner = document.getElementById( 'zero-rated-banner-red' );
24 - }
25 -
2628 function initClearSearchLink() {
27 - clearSearch.setAttribute( 'title','Clear' );
 29+ clearSearch.setAttribute( 'title', 'Clear' );
2830 clearSearch.addEventListener( 'mousedown', clearSearchBox, true );
2931 search.addEventListener( 'keyup', handleClearSearchLink, false );
3032 search.addEventListener( 'keydown', handleDefaultText, false );
@@ -38,7 +40,7 @@
3941 }
4042 }
4143 }
42 - document.getElementById( 'languageselection' ).addEventListener( 'change', navigateToLanguageSelection );
 44+ languageSelection.addEventListener( 'change', navigateToLanguageSelection );
4345
4446 function handleDefaultText() {
4547 var pE = document.getElementById( 'placeholder' );
@@ -66,28 +68,28 @@
6769 }
6870
6971 function logoClick() {
70 - var n = document.getElementById( 'nav' ).style;
71 - n.display = n.display == 'block' ? 'none' : 'block';
72 - if (n.display == 'block') {
 72+ var n = document.getElementById( 'nav' ).style, newWidth;
 73+ n.display = n.display === 'block' ? 'none' : 'block';
 74+ if (n.display === 'block') {
7375 if ( languageSelection ) {
7476 if ( languageSelection.offsetWidth > 175 ) {
75 - var newWidth = languageSelection.offsetWidth + 30;
 77+ newWidth = languageSelection.offsetWidth + 30;
7678 n.width = newWidth + 'px';
7779 }
7880 }
7981 }
80 - };
 82+ }
8183 initClearSearchLink();
8284 search.onpaste = function() {
8385 handleDefaultText();
8486 };
8587 document.getElementById( 'logo' ).addEventListener( 'click', logoClick );
86 - var dismissNotification = document.getElementById( 'dismiss-notification' );
 88+ dismissNotification = document.getElementById( 'dismiss-notification' );
8789
8890 if ( dismissNotification ) {
89 - var cookieNameZeroVisibility = 'zeroRatedBannerVisibility';
90 - var zeroRatedBanner = document.getElementById( 'zero-rated-banner' );
91 - var zeroRatedBannerVisibility = readCookie( cookieNameZeroVisibility );
 91+ cookieNameZeroVisibility = 'zeroRatedBannerVisibility';
 92+ zeroRatedBanner = document.getElementById( 'zero-rated-banner' );
 93+ zeroRatedBannerVisibility = readCookie( cookieNameZeroVisibility );
9294
9395 if ( zeroRatedBannerVisibility === 'off' ) {
9496 zeroRatedBanner.style.display = 'none';
@@ -100,17 +102,17 @@
101103 }
102104 };
103105 }
104 - if ( document.location.hash.indexOf( '#' ) == 0 ) {
105 - wm_reveal_for_hash( document.location.hash );
106 - }
107106
108 - for ( var a = document.getElementsByTagName( 'a' ), i = 0; i < a.length; i++ ) {
109 - a[i].addEventListener( 'click', function() {
110 - if ( this.hash.indexOf( '#' ) == 0 ) {
111 - wm_reveal_for_hash( this.hash );
112 - }
113 - });
 107+ function checkHash() {
 108+ var hash = this.hash || document.location.hash;
 109+ if ( hash.indexOf( '#' ) === 0 ) {
 110+ wm_reveal_for_hash( hash );
 111+ }
114112 }
 113+ checkHash();
 114+ for ( a = document.getElementsByTagName( 'a' ), i = 0; i < a.length; i++ ) {
 115+ a[i].addEventListener( 'click', checkHash );
 116+ }
115117
116118 // Try to scroll and hide URL bar
117119 window.scrollTo( 0, 1 );
@@ -118,13 +120,16 @@
119121 init();
120122
121123 function wm_reveal_for_hash( hash ) {
122 - var targetel = document.getElementById( hash.substr(1) );
 124+ var targetel = document.getElementById( hash.substr(1) ),
 125+ p, section_idx;
123126 if ( targetel ) {
124 - for (var p = targetel.parentNode; p && p.className != 'content_block' && p.className != 'section_heading'; ) {
 127+ p = targetel;
 128+ while ( p && p.className !== 'content_block' &&
 129+ p.className !== 'section_heading' ) {
125130 p = p.parentNode;
126131 }
127 - if ( p && p.style.display != 'block' ) {
128 - var section_idx = parseInt( p.id.split( '_' )[1] );
 132+ if ( p && p.style.display !== 'block' ) {
 133+ section_idx = parseInt( p.id.split( '_' )[1], 10 );
129134 wm_toggle_section( section_idx );
130135 }
131136 }
@@ -132,40 +137,41 @@
133138
134139 function wm_toggle_section( section_id ) {
135140 var b = document.getElementById( 'section_' + section_id ),
136 - bb = b.getElementsByTagName( 'button' );
137 - for ( var i = 0; i <= 1; i++ ) {
138 - var s = bb[i].style;
139 - s.display = s.display == 'none' || ( i && !s.display ) ? 'inline-block' : 'none';
 141+ bb = b.getElementsByTagName( 'button' ), i, s, e;
 142+ for ( i = 0; i <= 1; i++ ) {
 143+ s = bb[i].style;
 144+ s.display = s.display === 'none' || ( i && !s.display ) ? 'inline-block' : 'none';
140145 }
141 - for ( var i = 0, d = ['content_','anchor_']; i<=1; i++ ) {
142 - var e = document.getElementById( d[i] + section_id );
143 -
 146+ for ( i = 0, d = ['content_','anchor_']; i<=1; i++ ) {
 147+ e = document.getElementById( d[i] + section_id );
144148 if ( e ) {
145 - e.style.display = e.style.display == 'block' ? 'none' : 'block';
 149+ e.style.display = e.style.display === 'block' ? 'none' : 'block';
146150 }
147151 }
148152 }
149153
150154 function writeCookie( name, value, days ) {
 155+ var date, expires;
151156 if ( days ) {
152 - var date = new Date();
 157+ date = new Date();
153158 date.setTime( date.getTime() + ( days * 24 * 60 * 60 *1000 ) );
154 - var expires = '; expires=' + date.toGMTString();
 159+ expires = '; expires=' + date.toGMTString();
155160 } else {
156 - var expires = '';
 161+ expires = '';
157162 }
158163 document.cookie = name + '=' + value + expires + '; path=/';
159164 }
160165
161166 function readCookie( name ) {
162 - var nameVA = name + '=';
163 - var ca = document.cookie.split( ';' );
164 - for( var i=0; i < ca.length; i++ ) {
165 - var c = ca[i];
 167+ var nameVA = name + '=',
 168+ ca = document.cookie.split( ';' ),
 169+ c, i;
 170+ for( i=0; i < ca.length; i++ ) {
 171+ c = ca[i];
166172 while ( c.charAt(0) === ' ' ) {
167173 c = c.substring( 1, c.length );
168174 }
169 - if ( c.indexOf( nameVA ) == 0 ) {
 175+ if ( c.indexOf( nameVA ) === 0 ) {
170176 return c.substring( nameVA.length, c.length );
171177 }
172178 }
@@ -179,8 +185,8 @@
180186
181187 function utilities( el ) {
182188 function addClass( name ) {
183 - var className = el.className;
184 - var classNames = className.split( ' ' );
 189+ var className = el.className,
 190+ classNames = className.split( ' ' );
185191 classNames.push(name); // TODO: only push if unique
186192 el.className = classNames.join( ' ' );
187193 }
@@ -213,5 +219,4 @@
214220 utils: utilities
215221 };
216222
217 -}();
218 -
 223+}());
Index: trunk/extensions/MobileFrontend/javascripts/application.js
@@ -1,23 +1,28 @@
2 -MobileFrontend = function() {
 2+/*global document, window */
 3+/*jslint sloppy: true, white:true, maxerr: 50, indent: 4, plusplus: true*/
 4+MobileFrontend = (function() {
35
46 function init() {
 7+ var i, search, clearSearch, results, languageSelection, a,
 8+ sectionHeadings = document.getElementsByClassName( 'section_heading' ),
 9+ dismissNotification, cookieNameZeroVisibility, zeroRatedBanner, zeroRatedBannerVisibility;
510 document.body.className = "jsEnabled";
6 - var sectionHeadings = document.getElementsByClassName( 'section_heading' );
7 - for( var i = 0; i < sectionHeadings.length; i++ ) {
8 - sectionHeadings[i].addEventListener( 'click', function() {
9 - var sectionNumber = this.id ? this.id.split( '_' )[1] : -1;
10 - if(sectionNumber > -1) {
11 - wm_toggle_section( sectionNumber );
12 - }
13 - }, false );
 11+ function openSectionHandler() {
 12+ var sectionNumber = this.id ? this.id.split( '_' )[1] : -1;
 13+ if( sectionNumber > -1 ) {
 14+ wm_toggle_section( sectionNumber );
 15+ }
1416 }
15 - var search = document.getElementById( 'search' );
16 - var clearSearch = document.getElementById( 'clearsearch' );
17 - var results = document.getElementById( 'results' );
18 - var languageSelection = document.getElementById( 'languageselection' );
 17+ for( i = 0; i < sectionHeadings.length; i++ ) {
 18+ sectionHeadings[i].addEventListener( 'click', openSectionHandler, false );
 19+ }
 20+ search = document.getElementById( 'search' );
 21+ clearSearch = document.getElementById( 'clearsearch' );
 22+ results = document.getElementById( 'results' );
 23+ languageSelection = document.getElementById( 'languageselection' );
1924
2025 function initClearSearchLink() {
21 - clearSearch.setAttribute( 'title','Clear' );
 26+ clearSearch.setAttribute( 'title', 'Clear' );
2227 clearSearch.addEventListener( 'mousedown', clearSearchBox, true );
2328 search.addEventListener( 'keyup', handleClearSearchLink, false );
2429 }
@@ -31,7 +36,7 @@
3237 }
3338 }
3439 }
35 - document.getElementById( 'languageselection' ).addEventListener( 'change', navigateToLanguageSelection );
 40+ languageSelection.addEventListener( 'change', navigateToLanguageSelection );
3641
3742 function handleClearSearchLink() {
3843 if ( clearSearch ) {
@@ -58,25 +63,25 @@
5964 }
6065
6166 function logoClick() {
62 - var n = document.getElementById( 'nav' ).style;
63 - n.display = n.display == 'block' ? 'none' : 'block';
64 - if (n.display == 'block') {
 67+ var n = document.getElementById( 'nav' ).style, newWidth;
 68+ n.display = n.display === 'block' ? 'none' : 'block';
 69+ if (n.display === 'block') {
6570 if ( languageSelection ) {
6671 if ( languageSelection.offsetWidth > 175 ) {
67 - var newWidth = languageSelection.offsetWidth + 30;
 72+ newWidth = languageSelection.offsetWidth + 30;
6873 n.width = newWidth + 'px';
6974 }
7075 }
7176 }
72 - };
 77+ }
7378 initClearSearchLink();
7479 document.getElementById( 'logo' ).addEventListener( 'click', logoClick );
75 - var dismissNotification = document.getElementById( 'dismiss-notification' );
 80+ dismissNotification = document.getElementById( 'dismiss-notification' );
7681
7782 if ( dismissNotification ) {
78 - var cookieNameZeroVisibility = 'zeroRatedBannerVisibility';
79 - var zeroRatedBanner = document.getElementById( 'zero-rated-banner' );
80 - var zeroRatedBannerVisibility = readCookie( cookieNameZeroVisibility );
 83+ cookieNameZeroVisibility = 'zeroRatedBannerVisibility';
 84+ zeroRatedBanner = document.getElementById( 'zero-rated-banner' );
 85+ zeroRatedBannerVisibility = readCookie( cookieNameZeroVisibility );
8186
8287 if ( zeroRatedBannerVisibility === 'off' ) {
8388 zeroRatedBanner.style.display = 'none';
@@ -89,17 +94,17 @@
9095 }
9196 };
9297 }
93 - if ( document.location.hash.indexOf( '#' ) == 0 ) {
94 - wm_reveal_for_hash( document.location.hash );
95 - }
9698
97 - for ( var a = document.getElementsByTagName( 'a' ), i = 0; i < a.length; i++ ) {
98 - a[i].addEventListener( 'click', function() {
99 - if ( this.hash.indexOf( '#' ) == 0 ) {
100 - wm_reveal_for_hash( this.hash );
101 - }
102 - });
 99+ function checkHash() {
 100+ var hash = this.hash || document.location.hash;
 101+ if ( hash.indexOf( '#' ) === 0 ) {
 102+ wm_reveal_for_hash( hash );
 103+ }
103104 }
 105+ checkHash();
 106+ for ( a = document.getElementsByTagName( 'a' ), i = 0; i < a.length; i++ ) {
 107+ a[i].addEventListener( 'click', checkHash );
 108+ }
104109
105110 // Try to scroll and hide URL bar
106111 window.scrollTo( 0, 1 );
@@ -107,13 +112,16 @@
108113 init();
109114
110115 function wm_reveal_for_hash( hash ) {
111 - var targetel = document.getElementById( hash.substr(1) );
 116+ var targetel = document.getElementById( hash.substr(1) ),
 117+ p, section_idx;
112118 if ( targetel ) {
113 - for (var p = targetel.parentNode; p && p.className != 'content_block' && p.className != 'section_heading'; ) {
 119+ p = targetel;
 120+ while ( p && p.className !== 'content_block' &&
 121+ p.className !== 'section_heading' ) {
114122 p = p.parentNode;
115123 }
116 - if ( p && p.style.display != 'block' ) {
117 - var section_idx = parseInt( p.id.split( '_' )[1] );
 124+ if ( p && p.style.display !== 'block' ) {
 125+ section_idx = parseInt( p.id.split( '_' )[1], 10 );
118126 wm_toggle_section( section_idx );
119127 }
120128 }
@@ -121,40 +129,41 @@
122130
123131 function wm_toggle_section( section_id ) {
124132 var b = document.getElementById( 'section_' + section_id ),
125 - bb = b.getElementsByTagName( 'button' );
126 - for ( var i = 0; i <= 1; i++ ) {
127 - var s = bb[i].style;
128 - s.display = s.display == 'none' || ( i && !s.display ) ? 'inline-block' : 'none';
 133+ bb = b.getElementsByTagName( 'button' ), i, s, e;
 134+ for ( i = 0; i <= 1; i++ ) {
 135+ s = bb[i].style;
 136+ s.display = s.display === 'none' || ( i && !s.display ) ? 'inline-block' : 'none';
129137 }
130 - for ( var i = 0, d = ['content_','anchor_']; i<=1; i++ ) {
131 - var e = document.getElementById( d[i] + section_id );
132 -
 138+ for ( i = 0, d = ['content_','anchor_']; i<=1; i++ ) {
 139+ e = document.getElementById( d[i] + section_id );
133140 if ( e ) {
134 - e.style.display = e.style.display == 'block' ? 'none' : 'block';
 141+ e.style.display = e.style.display === 'block' ? 'none' : 'block';
135142 }
136143 }
137144 }
138145
139146 function writeCookie( name, value, days ) {
 147+ var date, expires;
140148 if ( days ) {
141 - var date = new Date();
 149+ date = new Date();
142150 date.setTime( date.getTime() + ( days * 24 * 60 * 60 *1000 ) );
143 - var expires = '; expires=' + date.toGMTString();
 151+ expires = '; expires=' + date.toGMTString();
144152 } else {
145 - var expires = '';
 153+ expires = '';
146154 }
147155 document.cookie = name + '=' + value + expires + '; path=/';
148156 }
149157
150158 function readCookie( name ) {
151 - var nameVA = name + '=';
152 - var ca = document.cookie.split( ';' );
153 - for( var i=0; i < ca.length; i++ ) {
154 - var c = ca[i];
 159+ var nameVA = name + '=',
 160+ ca = document.cookie.split( ';' ),
 161+ c, i;
 162+ for( i=0; i < ca.length; i++ ) {
 163+ c = ca[i];
155164 while ( c.charAt(0) === ' ' ) {
156165 c = c.substring( 1, c.length );
157166 }
158 - if ( c.indexOf( nameVA ) == 0 ) {
 167+ if ( c.indexOf( nameVA ) === 0 ) {
159168 return c.substring( nameVA.length, c.length );
160169 }
161170 }
@@ -168,8 +177,8 @@
169178
170179 function utilities( el ) {
171180 function addClass( name ) {
172 - var className = el.className;
173 - var classNames = className.split( ' ' );
 181+ var className = el.className,
 182+ classNames = className.split( ' ' );
174183 classNames.push(name); // TODO: only push if unique
175184 el.className = classNames.join( ' ' );
176185 }
@@ -202,5 +211,4 @@
203212 utils: utilities
204213 };
205214
206 -}();
207 -
 215+}());

Status & tagging log