| 1 |
/* |
| 2 |
|
| 3 |
* FCKeditor - The text editor for Internet - http://www.fckeditor.net |
| 4 |
|
| 5 |
* Copyright (C) 2003-2009 Frederico Caldeira Knabben |
| 6 |
|
| 7 |
* |
| 8 |
|
| 9 |
* == BEGIN LICENSE == |
| 10 |
|
| 11 |
* |
| 12 |
|
| 13 |
* Licensed under the terms of any of the following licenses at your |
| 14 |
|
| 15 |
* choice: |
| 16 |
|
| 17 |
* |
| 18 |
|
| 19 |
* - GNU General Public License Version 2 or later (the "GPL") |
| 20 |
|
| 21 |
* http://www.gnu.org/licenses/gpl.html |
| 22 |
|
| 23 |
* |
| 24 |
|
| 25 |
* - GNU Lesser General Public License Version 2.1 or later (the "LGPL") |
| 26 |
|
| 27 |
* http://www.gnu.org/licenses/lgpl.html |
| 28 |
|
| 29 |
* |
| 30 |
|
| 31 |
* - Mozilla Public License Version 1.1 or later (the "MPL") |
| 32 |
|
| 33 |
* http://www.mozilla.org/MPL/MPL-1.1.html |
| 34 |
|
| 35 |
* |
| 36 |
|
| 37 |
* == END LICENSE == |
| 38 |
|
| 39 |
* |
| 40 |
|
| 41 |
* Control keyboard keystroke combinations. |
| 42 |
|
| 43 |
*/ |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
var FCKKeystrokeHandler = function( cancelCtrlDefaults ) |
| 48 |
|
| 49 |
{ |
| 50 |
|
| 51 |
this.Keystrokes = new Object() ; |
| 52 |
|
| 53 |
this.CancelCtrlDefaults = ( cancelCtrlDefaults !== false ) ; |
| 54 |
|
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
/* |
| 60 |
|
| 61 |
* Listen to keystroke events in an element or DOM document object. |
| 62 |
|
| 63 |
* @target: The element or document to listen to keystroke events. |
| 64 |
|
| 65 |
*/ |
| 66 |
|
| 67 |
FCKKeystrokeHandler.prototype.AttachToElement = function( target ) |
| 68 |
|
| 69 |
{ |
| 70 |
|
| 71 |
// For newer browsers, it is enough to listen to the keydown event only. |
| 72 |
|
| 73 |
// Some browsers instead, don't cancel key events in the keydown, but in the |
| 74 |
|
| 75 |
// keypress. So we must do a longer trip in those cases. |
| 76 |
|
| 77 |
FCKTools.AddEventListenerEx( target, 'keydown', _FCKKeystrokeHandler_OnKeyDown, this ) ; |
| 78 |
|
| 79 |
if ( FCKBrowserInfo.IsGecko10 || FCKBrowserInfo.IsOpera || ( FCKBrowserInfo.IsGecko && FCKBrowserInfo.IsMac ) ) |
| 80 |
|
| 81 |
FCKTools.AddEventListenerEx( target, 'keypress', _FCKKeystrokeHandler_OnKeyPress, this ) ; |
| 82 |
|
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
/* |
| 88 |
|
| 89 |
* Sets a list of keystrokes. It can receive either a single array or "n" |
| 90 |
|
| 91 |
* arguments, each one being an array of 1 or 2 elemenst. The first element |
| 92 |
|
| 93 |
* is the keystroke combination, and the second is the value to assign to it. |
| 94 |
|
| 95 |
* If the second element is missing, the keystroke definition is removed. |
| 96 |
|
| 97 |
*/ |
| 98 |
|
| 99 |
FCKKeystrokeHandler.prototype.SetKeystrokes = function() |
| 100 |
|
| 101 |
{ |
| 102 |
|
| 103 |
// Look through the arguments. |
| 104 |
|
| 105 |
for ( var i = 0 ; i < arguments.length ; i++ ) |
| 106 |
|
| 107 |
{ |
| 108 |
|
| 109 |
var keyDef = arguments[i] ; |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
// If the configuration for the keystrokes is missing some element or has any extra comma |
| 114 |
|
| 115 |
// this item won't be valid, so skip it and keep on processing. |
| 116 |
|
| 117 |
if ( !keyDef ) |
| 118 |
|
| 119 |
continue ; |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
if ( typeof( keyDef[0] ) == 'object' ) // It is an array with arrays defining the keystrokes. |
| 124 |
|
| 125 |
this.SetKeystrokes.apply( this, keyDef ) ; |
| 126 |
|
| 127 |
else |
| 128 |
|
| 129 |
{ |
| 130 |
|
| 131 |
if ( keyDef.length == 1 ) // If it has only one element, remove the keystroke. |
| 132 |
|
| 133 |
delete this.Keystrokes[ keyDef[0] ] ; |
| 134 |
|
| 135 |
else // Otherwise add it. |
| 136 |
|
| 137 |
this.Keystrokes[ keyDef[0] ] = keyDef[1] === true ? true : keyDef ; |
| 138 |
|
| 139 |
} |
| 140 |
|
| 141 |
} |
| 142 |
|
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
function _FCKKeystrokeHandler_OnKeyDown( ev, keystrokeHandler ) |
| 148 |
|
| 149 |
{ |
| 150 |
|
| 151 |
// Get the key code. |
| 152 |
|
| 153 |
var keystroke = ev.keyCode || ev.which ; |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
// Combine it with the CTRL, SHIFT and ALT states. |
| 158 |
|
| 159 |
var keyModifiers = 0 ; |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
if ( ev.ctrlKey || ev.metaKey ) |
| 164 |
|
| 165 |
keyModifiers += CTRL ; |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
if ( ev.shiftKey ) |
| 170 |
|
| 171 |
keyModifiers += SHIFT ; |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
if ( ev.altKey ) |
| 176 |
|
| 177 |
keyModifiers += ALT ; |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
var keyCombination = keystroke + keyModifiers ; |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
var cancelIt = keystrokeHandler._CancelIt = false ; |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
// Look for its definition availability. |
| 190 |
|
| 191 |
var keystrokeValue = keystrokeHandler.Keystrokes[ keyCombination ] ; |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
// FCKDebug.Output( 'KeyDown: ' + keyCombination + ' - Value: ' + keystrokeValue ) ; |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
// If the keystroke is defined |
| 200 |
|
| 201 |
if ( keystrokeValue ) |
| 202 |
|
| 203 |
{ |
| 204 |
|
| 205 |
// If the keystroke has been explicitly set to "true" OR calling the |
| 206 |
|
| 207 |
// "OnKeystroke" event, it doesn't return "true", the default behavior |
| 208 |
|
| 209 |
// must be preserved. |
| 210 |
|
| 211 |
if ( keystrokeValue === true || !( keystrokeHandler.OnKeystroke && keystrokeHandler.OnKeystroke.apply( keystrokeHandler, keystrokeValue ) ) ) |
| 212 |
|
| 213 |
return true ; |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
cancelIt = true ; |
| 218 |
|
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
// By default, it will cancel all combinations with the CTRL key only (except positioning keys). |
| 224 |
|
| 225 |
if ( cancelIt || ( keystrokeHandler.CancelCtrlDefaults && keyModifiers == CTRL && ( keystroke < 33 || keystroke > 40 ) ) ) |
| 226 |
|
| 227 |
{ |
| 228 |
|
| 229 |
keystrokeHandler._CancelIt = true ; |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
if ( ev.preventDefault ) |
| 234 |
|
| 235 |
return ev.preventDefault() ; |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
ev.returnValue = false ; |
| 240 |
|
| 241 |
ev.cancelBubble = true ; |
| 242 |
|
| 243 |
return false ; |
| 244 |
|
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
return true ; |
| 250 |
|
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
function _FCKKeystrokeHandler_OnKeyPress( ev, keystrokeHandler ) |
| 256 |
|
| 257 |
{ |
| 258 |
|
| 259 |
if ( keystrokeHandler._CancelIt ) |
| 260 |
|
| 261 |
{ |
| 262 |
|
| 263 |
// FCKDebug.Output( 'KeyPress Cancel', 'Red') ; |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
if ( ev.preventDefault ) |
| 268 |
|
| 269 |
return ev.preventDefault() ; |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
return false ; |
| 274 |
|
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
return true ; |
| 280 |
|
| 281 |
} |
| 282 |
|
| 283 |
|