r112118 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r112117‎ | r112118 | r112119 >
Date:16:53, 22 February 2012
Author:jdlrobson
Status:ok
Tags:
Comment:
abstract addEventListener

IE < 9 doesn't support addEventListener so it is
useful to have a utility function to attach events going
forward.

Note discussion here:
http://www.mediawiki.org/wiki/Special:Code/MediaWiki/112019
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)

Diff [purge]

Index: trunk/extensions/MobileFrontend/javascripts/opensearch.js
@@ -5,7 +5,8 @@
66 numResults = 5, pixels = 'px',
77 results = document.getElementById( 'results' ),
88 search = document.getElementById( 'search' ),
9 - sb = document.getElementById( 'searchbox' );
 9+ sb = document.getElementById( 'searchbox' ),
 10+ u = MobileFrontend.utils;
1011
1112 if ( scriptPath ) {
1213 apiUrl = scriptPath + apiUrl;
@@ -43,7 +44,7 @@
4445 }
4546
4647 window.onload = function () {
47 - search.addEventListener( 'keyup',
 48+ u( search ).bind( 'keyup',
4849 function() {
4950 clearTimeout( timer );
5051 var term = this.value;
@@ -131,7 +132,7 @@
132133 label = document.createTextNode( '+' );
133134 link.appendChild(label);
134135 link.className = 'sq-val-update';
135 - link.addEventListener( 'click', suggestionListener );
 136+ u( link ).bind( 'click', suggestionListener );
136137 suggestionsResult.appendChild( link );
137138
138139 link = document.createElement( 'a' );
Index: trunk/extensions/MobileFrontend/javascripts/beta_application.js
@@ -1,5 +1,9 @@
22 /*global document, window */
33 /*jslint sloppy: true, white:true, maxerr: 50, indent: 4, plusplus: true*/
 4+/*
 5+TODO: getElementsByClassName not supported by IE < 9
 6+TODO: addEventListener not supported by IE < 9
 7+*/
48 MobileFrontend = (function() {
59
610 function init() {
@@ -14,7 +18,7 @@
1519 }
1620 }
1721 for( i = 0; i < sectionHeadings.length; i++ ) {
18 - sectionHeadings[i].addEventListener( 'click', openSectionHandler, false );
 22+ utilities( sectionHeadings[i] ).bind( 'click', openSectionHandler, false );
1923 }
2024 search = document.getElementById( 'search' );
2125 clearSearch = document.getElementById( 'clearsearch' );
@@ -29,10 +33,10 @@
3034 search.select();
3135 }
3236 clearSearch.setAttribute( 'title', 'Clear' );
33 - clearSearch.addEventListener( 'mousedown', clearSearchBox, true );
34 - search.addEventListener( 'keyup', handleClearSearchLink, false );
35 - search.addEventListener( 'keydown', handleDefaultText, false );
36 - search.addEventListener( 'click', onFocusHandler, true );
 37+ utilities( clearSearch ).bind( 'mousedown', clearSearchBox, true );
 38+ utilities( search ).bind( 'keyup', handleClearSearchLink, false );
 39+ utilities( search ).bind( 'keydown', handleDefaultText, false );
 40+ utilities( search ).bind( 'click', onFocusHandler, true );
3741 }
3842
3943 function navigateToLanguageSelection() {
@@ -44,7 +48,7 @@
4549 }
4650 }
4751 }
48 - languageSelection.addEventListener( 'change', navigateToLanguageSelection );
 52+ utilities( languageSelection ).bind( 'change', navigateToLanguageSelection );
4953
5054 function handleDefaultText() {
5155 var pE = document.getElementById( 'placeholder' );
@@ -87,7 +91,7 @@
8892 search.onpaste = function() {
8993 handleDefaultText();
9094 };
91 - document.getElementById( 'logo' ).addEventListener( 'click', logoClick );
 95+ utilities( document.getElementById( 'logo' ) ).bind( 'click', logoClick );
9296 dismissNotification = document.getElementById( 'dismiss-notification' );
9397
9498 if ( dismissNotification ) {
@@ -115,7 +119,7 @@
116120 }
117121 checkHash();
118122 for ( a = document.getElementsByTagName( 'a' ), i = 0; i < a.length; i++ ) {
119 - a[i].addEventListener( 'click', checkHash );
 123+ utilities( a[i] ).bind( 'click', checkHash );
120124 }
121125
122126 // Try to scroll and hide URL bar
@@ -207,8 +211,16 @@
208212 el.className = newClasses.join( ' ' );
209213 }
210214
 215+ function bind( type, handler ) {
 216+ if ( el.addEventListener ) { // standardised browser
 217+ el.addEventListener( type, handler, false );
 218+ } else if( el.attachEvent ) {
 219+ el.attachEvent( 'on' + type, handler );
 220+ }
 221+ }
211222 return {
212223 addClass: addClass,
 224+ bind: bind,
213225 removeClass: removeClass
214226 };
215227 }
Index: trunk/extensions/MobileFrontend/javascripts/beta_opensearch.js
@@ -14,7 +14,8 @@
1515 zeroRatedBanner = document.getElementById( 'zero-rated-banner' ) ||
1616 document.getElementById( 'zero-rated-banner-red' ),
1717 clearSearch = document.getElementById( 'clearsearch' ),
18 - focused = false, ol = {};
 18+ focused = false, ol = {},
 19+ u = MobileFrontend.utils;
1920
2021 if ( scriptPath ) {
2122 apiUrl = scriptPath + apiUrl;
@@ -28,7 +29,7 @@
2930 var viewportmeta = document.querySelector( 'meta[name="viewport"]' );
3031 if ( viewportmeta ) {
3132 viewportmeta.content = 'width=device-width, minimum-scale=1.0, maximum-scale=1.0, initial-scale=1.0';
32 - document.body.addEventListener( 'gesturestart', function () {
 33+ u( document.body ).bind( 'gesturestart', function () {
3334 viewportmeta.content = 'width=device-width, initial-scale=1.0';
3435 }, false );
3536 }
@@ -72,7 +73,7 @@
7374 rrd = document.createElement( 'a' );
7475 rrd.setAttribute( 'href', '#' );
7576 rrd.setAttribute( 'id', 'remove-results' );
76 - rrd.addEventListener( 'click', removeResults );
 77+ u( rrd ).bind( 'click', removeResults );
7778 rrdD = document.createElement( 'div' );
7879 rrdD.setAttribute( 'id', 'left-arrow' );
7980 rrd.appendChild( rrdD );
@@ -135,7 +136,7 @@
136137 }
137138
138139 window.onload = function () {
139 - search.addEventListener( 'keyup',
 140+ u( search ).bind( 'keyup',
140141 function() {
141142 clearTimeout( timer );
142143 term = this.value;
@@ -233,7 +234,7 @@
234235 label = document.createTextNode( '+' );
235236 link.appendChild(label);
236237 link.className = 'sq-val-update';
237 - link.addEventListener( 'click', suggestionListener );
 238+ u( link ).bind( 'click', suggestionListener );
238239 suggestionsResult.appendChild( link );
239240
240241 link = document.createElement( 'a' );
Index: trunk/extensions/MobileFrontend/javascripts/application.js
@@ -14,7 +14,7 @@
1515 }
1616 }
1717 for( i = 0; i < sectionHeadings.length; i++ ) {
18 - sectionHeadings[i].addEventListener( 'click', openSectionHandler, false );
 18+ utilities( sectionHeadings[i] ).bind( 'click', openSectionHandler, false );
1919 }
2020 search = document.getElementById( 'search' );
2121 clearSearch = document.getElementById( 'clearsearch' );
@@ -26,9 +26,9 @@
2727 search.select();
2828 }
2929 clearSearch.setAttribute( 'title', 'Clear' );
30 - clearSearch.addEventListener( 'mousedown', clearSearchBox, true );
31 - search.addEventListener( 'keyup', handleClearSearchLink, false );
32 - search.addEventListener( 'click', onFocusHandler, true );
 30+ utilities( clearSearch ).bind( 'mousedown', clearSearchBox, true );
 31+ utilities( search ).bind( 'keyup', handleClearSearchLink, false );
 32+ utilities( search ).bind( 'click', onFocusHandler, true );
3333 }
3434
3535 function navigateToLanguageSelection() {
@@ -40,7 +40,7 @@
4141 }
4242 }
4343 }
44 - languageSelection.addEventListener( 'change', navigateToLanguageSelection );
 44+ utilities( languageSelection ).bind( 'change', navigateToLanguageSelection );
4545
4646 function handleClearSearchLink() {
4747 if ( clearSearch ) {
@@ -79,7 +79,7 @@
8080 }
8181 }
8282 initClearSearchLink();
83 - document.getElementById( 'logo' ).addEventListener( 'click', logoClick );
 83+ utilities( document.getElementById( 'logo' ) ).bind( 'click', logoClick );
8484 dismissNotification = document.getElementById( 'dismiss-notification' );
8585
8686 if ( dismissNotification ) {
@@ -107,7 +107,7 @@
108108 }
109109 checkHash();
110110 for ( a = document.getElementsByTagName( 'a' ), i = 0; i < a.length; i++ ) {
111 - a[i].addEventListener( 'click', checkHash );
 111+ utilities( a[i] ).bind( 'click', checkHash );
112112 }
113113
114114 // Try to scroll and hide URL bar
@@ -199,8 +199,16 @@
200200 el.className = newClasses.join( ' ' );
201201 }
202202
 203+ function bind( type, handler ) {
 204+ if ( el.addEventListener ) { // standardised browser
 205+ el.addEventListener( type, handler, false );
 206+ } else if( el.attachEvent ) {
 207+ el.attachEvent( 'on' + type, handler );
 208+ }
 209+ }
203210 return {
204211 addClass: addClass,
 212+ bind: bind,
205213 removeClass: removeClass
206214 };
207215 }

Status & tagging log