Index: trunk/phase3/skins/common/mwsuggest.js |
— | — | @@ -46,6 +46,9 @@ |
47 | 47 | window.os_container_max_width = 2; |
48 | 48 | // currently active animation timer |
49 | 49 | window.os_animation_timer = null; |
| 50 | +// whether MWSuggest is enabled. Set to false when os_MWSuggestDisable() is called |
| 51 | +window.os_enabled = true; |
| 52 | + |
50 | 53 | /** |
51 | 54 | * <datalist> is a new HTML5 element that allows you to manually supply |
52 | 55 | * suggestion lists and have them rendered according to the right platform |
— | — | @@ -107,7 +110,11 @@ |
108 | 111 | |
109 | 112 | /** Initialization, call upon page onload */ |
110 | 113 | 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++ ) { |
112 | 119 | var id = os_autoload_inputs[i]; |
113 | 120 | var form = os_autoload_forms[i]; |
114 | 121 | element = document.getElementById( id ); |
— | — | @@ -117,6 +124,25 @@ |
118 | 125 | } |
119 | 126 | }; |
120 | 127 | |
| 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 | + |
121 | 147 | /** Init Result objects and event handlers */ |
122 | 148 | window.os_initHandlers = function( name, formname, element ) { |
123 | 149 | var r = new os_Results( name, formname ); |
— | — | @@ -126,20 +152,20 @@ |
127 | 153 | return; |
128 | 154 | } |
129 | 155 | // 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 ); |
133 | 159 | if ( !os_use_datalist ) { |
134 | 160 | // 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 ); |
137 | 163 | // We don't want browser auto-suggestions interfering with our div, but |
138 | 164 | // autocomplete must be on for datalist to work (at least in Opera |
139 | 165 | // 10.10). |
140 | 166 | element.setAttribute( 'autocomplete', 'off' ); |
141 | 167 | } |
142 | 168 | // stopping handler |
143 | | - os_hookEvent( formElement, 'submit', function( event ) { return os_eventOnsubmit( event ); } ); |
| 169 | + os_hookEvent( formElement, 'submit', os_eventOnsubmit ); |
144 | 170 | os_map[name] = r; |
145 | 171 | // toggle link |
146 | 172 | if( document.getElementById( r.toggle ) == null ) { |
— | — | @@ -167,6 +193,30 @@ |
168 | 194 | |
169 | 195 | }; |
170 | 196 | |
| 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 | + |
171 | 221 | window.os_hookEvent = function( element, hookName, hookFunct ) { |
172 | 222 | if ( element.addEventListener ) { |
173 | 223 | element.addEventListener( hookName, hookFunct, false ); |
— | — | @@ -175,6 +225,14 @@ |
176 | 226 | } |
177 | 227 | }; |
178 | 228 | |
| 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 | + |
179 | 237 | /******************** |
180 | 238 | * Keyboard events |
181 | 239 | ********************/ |