r82129 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r82128‎ | r82129 | r82130 >
Date:19:55, 14 February 2011
Author:catrope
Status:ok
Tags:
Comment:
(bug 27402) Add support for disabling MWSuggest
Modified paths:
  • /trunk/phase3/skins/common/mwsuggest.js (modified) (history)

Diff [purge]

Index: trunk/phase3/skins/common/mwsuggest.js
@@ -46,6 +46,9 @@
4747 window.os_container_max_width = 2;
4848 // currently active animation timer
4949 window.os_animation_timer = null;
 50+// whether MWSuggest is enabled. Set to false when os_MWSuggestDisable() is called
 51+window.os_enabled = true;
 52+
5053 /**
5154 * <datalist> is a new HTML5 element that allows you to manually supply
5255 * suggestion lists and have them rendered according to the right platform
@@ -107,7 +110,11 @@
108111
109112 /** Initialization, call upon page onload */
110113 window.os_MWSuggestInit = function() {
111 - for( i = 0; i < os_autoload_inputs.length; i++ ) {
 114+ if ( !window.os_enabled ) {
 115+ return;
 116+ }
 117+
 118+ for( var i = 0; i < os_autoload_inputs.length; i++ ) {
112119 var id = os_autoload_inputs[i];
113120 var form = os_autoload_forms[i];
114121 element = document.getElementById( id );
@@ -117,6 +124,25 @@
118125 }
119126 };
120127
 128+/* Teardown, called when things like SimpleSearch need to disable MWSuggest */
 129+window.os_MWSuggestTeardown = function() {
 130+ for( var i = 0; i < os_autoload_inputs.length; i++ ) {
 131+ var id = os_autoload_inputs[i];
 132+ var form = os_autoload_forms[i];
 133+ element = document.getElementById( id );
 134+ if( element != null ) {
 135+ os_teardownHandlers( id, form, element );
 136+ }
 137+ }
 138+};
 139+
 140+/* Call this to disable MWSuggest. Works regardless of whether MWSuggest has been initialized already. */
 141+window.os_MWSuggestDisable = function() {
 142+ window.os_MWSuggestTeardown();
 143+ window.os_enabled = false;
 144+}
 145+
 146+
121147 /** Init Result objects and event handlers */
122148 window.os_initHandlers = function( name, formname, element ) {
123149 var r = new os_Results( name, formname );
@@ -126,20 +152,20 @@
127153 return;
128154 }
129155 // event handler
130 - os_hookEvent( element, 'keyup', function( event ) { os_eventKeyup( event ); } );
131 - os_hookEvent( element, 'keydown', function( event ) { os_eventKeydown( event ); } );
132 - os_hookEvent( element, 'keypress', function( event ) { os_eventKeypress( event ); } );
 156+ os_hookEvent( element, 'keyup', os_eventKeyup );
 157+ os_hookEvent( element, 'keydown', os_eventKeydown );
 158+ os_hookEvent( element, 'keypress', os_eventKeypress );
133159 if ( !os_use_datalist ) {
134160 // These are needed for the div hack to hide it if the user blurs.
135 - os_hookEvent( element, 'blur', function( event ) { os_eventBlur( event ); } );
136 - os_hookEvent( element, 'focus', function( event ) { os_eventFocus( event ); } );
 161+ os_hookEvent( element, 'blur', os_eventBlur );
 162+ os_hookEvent( element, 'focus', os_eventFocus );
137163 // We don't want browser auto-suggestions interfering with our div, but
138164 // autocomplete must be on for datalist to work (at least in Opera
139165 // 10.10).
140166 element.setAttribute( 'autocomplete', 'off' );
141167 }
142168 // stopping handler
143 - os_hookEvent( formElement, 'submit', function( event ) { return os_eventOnsubmit( event ); } );
 169+ os_hookEvent( formElement, 'submit', os_eventOnsubmit );
144170 os_map[name] = r;
145171 // toggle link
146172 if( document.getElementById( r.toggle ) == null ) {
@@ -167,6 +193,30 @@
168194
169195 };
170196
 197+window.os_teardownHandlers = function( name, formname, element ) {
 198+ var formElement = document.getElementById( formname );
 199+ if( !formElement ) {
 200+ // Older browsers (Opera 8) cannot get form elements
 201+ return;
 202+ }
 203+
 204+ os_unhookEvent( element, 'keyup', os_eventKeyup );
 205+ os_unhookEvent( element, 'keydown', os_eventKeydown );
 206+ os_unhookEvent( element, 'keypress', os_eventKeypress );
 207+ if ( !os_use_datalist ) {
 208+ // These are needed for the div hack to hide it if the user blurs.
 209+ os_unhookEvent( element, 'blur', os_eventBlur );
 210+ os_unhookEvent( element, 'focus', os_eventFocus );
 211+ // We don't want browser auto-suggestions interfering with our div, but
 212+ // autocomplete must be on for datalist to work (at least in Opera
 213+ // 10.10).
 214+ element.removeAttribute( 'autocomplete' );
 215+ }
 216+ // stopping handler
 217+ os_unhookEvent( formElement, 'submit', os_eventOnsubmit );
 218+};
 219+
 220+
171221 window.os_hookEvent = function( element, hookName, hookFunct ) {
172222 if ( element.addEventListener ) {
173223 element.addEventListener( hookName, hookFunct, false );
@@ -175,6 +225,14 @@
176226 }
177227 };
178228
 229+window.os_unhookEvent = function( element, hookName, hookFunct ) {
 230+ if ( element.removeEventListener ) {
 231+ element.removeEventListener( hookName, hookFunct, false );
 232+ } else if ( element.detachEvent ) {
 233+ element.detachEvent( 'on' + hookName, hookFunct );
 234+ }
 235+}
 236+
179237 /********************
180238 * Keyboard events
181239 ********************/

Follow-up revisions

RevisionCommit summaryAuthorDate
r82130(Bug 27402) Disable MWSuggest when SimpleSearch is enabled. Requires MWSugges...catrope19:55, 14 February 2011
r821511.17wmf1: MFT r78964, r79086, r79087, r79091, r82004, r82025, r82048, r82070,...catrope22:55, 14 February 2011
r85151MFT: r82000, r82004, r82020, r82025, r82038, r82039, r82048, r82070, r82081, ...demon20:39, 1 April 2011

Status & tagging log