Index: trunk/extensions/Narayam/ext.narayam.core.js |
— | — | @@ -29,7 +29,7 @@ |
30 | 30 | var availableSchemes = mw.config.get( 'wgNarayamAvailableSchemes' ) || {}; |
31 | 31 | // Currently selected scheme |
32 | 32 | var currentScheme = null; |
33 | | - // Shortcut key |
| 33 | + // Shortcut key for turning Narayam on and off |
34 | 34 | var shortcutKey = mw.config.get( 'wgNarayamShortcutKey' ) || { |
35 | 35 | altKey: false, |
36 | 36 | ctrlKey: false, |
— | — | @@ -100,6 +100,11 @@ |
101 | 101 | return -1; |
102 | 102 | } |
103 | 103 | |
| 104 | + /** |
| 105 | + * Check whether a keypress event corresponds to the shortcut key |
| 106 | + * @param e Event object |
| 107 | + * @return bool |
| 108 | + */ |
104 | 109 | function isShortcutKey( e ) { |
105 | 110 | return e.altKey == shortcutKey.altKey && |
106 | 111 | e.ctrlKey == shortcutKey.ctrlKey && |
— | — | @@ -107,6 +112,10 @@ |
108 | 113 | String.fromCharCode( e.which ).toLowerCase() == shortcutKey.key.toLowerCase(); |
109 | 114 | } |
110 | 115 | |
| 116 | + /** |
| 117 | + * Get a description of the shortcut key, e.g. "Ctrl-M" |
| 118 | + * @return string |
| 119 | + */ |
111 | 120 | function shortcutText() { |
112 | 121 | var text = ''; |
113 | 122 | // TODO: Localize these things (in core, too) |
— | — | @@ -123,6 +132,10 @@ |
124 | 133 | return text; |
125 | 134 | } |
126 | 135 | |
| 136 | + /** |
| 137 | + * Keydown event handler. Handles shortcut key presses |
| 138 | + * @param e Event object |
| 139 | + */ |
127 | 140 | function onkeydown( e ) { |
128 | 141 | // If the current scheme uses the alt key, ignore keydown for Alt+? combinations |
129 | 142 | if ( enabled && currentScheme.extended_keyboard && e.altKey && !e.ctrlKey ) { |
— | — | @@ -136,6 +149,10 @@ |
137 | 150 | return true; |
138 | 151 | } |
139 | 152 | |
| 153 | + /** |
| 154 | + * Keypress event handler. This is where the real work happens |
| 155 | + * @param e Event object |
| 156 | + */ |
140 | 157 | function onkeypress( e ) { |
141 | 158 | if ( !enabled ) { |
142 | 159 | return true; |
— | — | @@ -201,6 +218,10 @@ |
202 | 219 | return false; |
203 | 220 | } |
204 | 221 | |
| 222 | + /** |
| 223 | + * Change handler for the scheme dropdown. Updates the current scheme |
| 224 | + * based on the new selection in the dropdown. |
| 225 | + */ |
205 | 226 | function updateSchemeFromSelect() { |
206 | 227 | var scheme = $( this ).val(); |
207 | 228 | that.setScheme( scheme ); |
— | — | @@ -226,6 +247,9 @@ |
227 | 248 | } |
228 | 249 | }; |
229 | 250 | |
| 251 | + /** |
| 252 | + * Enable Narayam |
| 253 | + */ |
230 | 254 | this.enable = function() { |
231 | 255 | if ( !enabled && currentScheme !== null ) { |
232 | 256 | $inputs.addClass( 'narayam-input' ); |
— | — | @@ -235,6 +259,9 @@ |
236 | 260 | } |
237 | 261 | }; |
238 | 262 | |
| 263 | + /** |
| 264 | + * Disable Narayam |
| 265 | + */ |
239 | 266 | this.disable = function() { |
240 | 267 | if ( enabled ) { |
241 | 268 | $inputs.removeClass( 'narayam-input' ); |
— | — | @@ -244,6 +271,9 @@ |
245 | 272 | } |
246 | 273 | }; |
247 | 274 | |
| 275 | + /** |
| 276 | + * Toggle the enabled/disabled state |
| 277 | + */ |
248 | 278 | this.toggle = function() { |
249 | 279 | if ( enabled ) { |
250 | 280 | that.disable(); |
— | — | @@ -268,6 +298,10 @@ |
269 | 299 | } |
270 | 300 | }; |
271 | 301 | |
| 302 | + /** |
| 303 | + * Change the current transliteration scheme |
| 304 | + * @param name String |
| 305 | + */ |
272 | 306 | this.setScheme = function( name ) { |
273 | 307 | if ( name in schemes ) { |
274 | 308 | currentScheme = schemes[name]; |
— | — | @@ -275,6 +309,11 @@ |
276 | 310 | } |
277 | 311 | }; |
278 | 312 | |
| 313 | + /** |
| 314 | + * Set up Narayam. This adds the scheme dropdown, binds the handlers |
| 315 | + * and initializes the enabled/disabled state and selected scheme |
| 316 | + * from a cookie or wgNarayamEnableByDefault |
| 317 | + */ |
279 | 318 | this.setup = function() { |
280 | 319 | // Build scheme dropdown |
281 | 320 | $select = $( '<select />' ); |