1 | /* |
---|
2 | * FCKeditor - The text editor for Internet - http://www.fckeditor.net |
---|
3 | * Copyright (C) 2003-2007 Frederico Caldeira Knabben |
---|
4 | * |
---|
5 | * == BEGIN LICENSE == |
---|
6 | * |
---|
7 | * Licensed under the terms of any of the following licenses at your |
---|
8 | * choice: |
---|
9 | * |
---|
10 | * - GNU General Public License Version 2 or later (the "GPL") |
---|
11 | * http://www.gnu.org/licenses/gpl.html |
---|
12 | * |
---|
13 | * - GNU Lesser General Public License Version 2.1 or later (the "LGPL") |
---|
14 | * http://www.gnu.org/licenses/lgpl.html |
---|
15 | * |
---|
16 | * - Mozilla Public License Version 1.1 or later (the "MPL") |
---|
17 | * http://www.mozilla.org/MPL/MPL-1.1.html |
---|
18 | * |
---|
19 | * == END LICENSE == |
---|
20 | * |
---|
21 | * Control keyboard keystroke combinations. |
---|
22 | */ |
---|
23 | |
---|
24 | var FCKKeystrokeHandler = function( cancelCtrlDefaults ) |
---|
25 | { |
---|
26 | this.Keystrokes = new Object() ; |
---|
27 | this.CancelCtrlDefaults = ( cancelCtrlDefaults !== false ) ; |
---|
28 | } |
---|
29 | |
---|
30 | /* |
---|
31 | * Listen to keystroke events in an element or DOM document object. |
---|
32 | * @target: The element or document to listen to keystroke events. |
---|
33 | */ |
---|
34 | FCKKeystrokeHandler.prototype.AttachToElement = function( target ) |
---|
35 | { |
---|
36 | // For newer browsers, it is enough to listen to the keydown event only. |
---|
37 | // Some browsers instead, don't cancel key events in the keydown, but in the |
---|
38 | // keypress. So we must do a longer trip in those cases. |
---|
39 | FCKTools.AddEventListenerEx( target, 'keydown', _FCKKeystrokeHandler_OnKeyDown, this ) ; |
---|
40 | if ( FCKBrowserInfo.IsGecko10 || FCKBrowserInfo.IsOpera || ( FCKBrowserInfo.IsGecko && FCKBrowserInfo.IsMac ) ) |
---|
41 | FCKTools.AddEventListenerEx( target, 'keypress', _FCKKeystrokeHandler_OnKeyPress, this ) ; |
---|
42 | } |
---|
43 | |
---|
44 | /* |
---|
45 | * Sets a list of keystrokes. It can receive either a single array or "n" |
---|
46 | * arguments, each one being an array of 1 or 2 elemenst. The first element |
---|
47 | * is the keystroke combination, and the second is the value to assign to it. |
---|
48 | * If the second element is missing, the keystroke definition is removed. |
---|
49 | */ |
---|
50 | FCKKeystrokeHandler.prototype.SetKeystrokes = function() |
---|
51 | { |
---|
52 | // Look through the arguments. |
---|
53 | for ( var i = 0 ; i < arguments.length ; i++ ) |
---|
54 | { |
---|
55 | var keyDef = arguments[i] ; |
---|
56 | |
---|
57 | if ( typeof( keyDef[0] ) == 'object' ) // It is an array with arrays defining the keystrokes. |
---|
58 | this.SetKeystrokes.apply( this, keyDef ) ; |
---|
59 | else |
---|
60 | { |
---|
61 | if ( keyDef.length == 1 ) // If it has only one element, removed the keystroke. |
---|
62 | delete this.Keystrokes[ keyDef[0] ] ; |
---|
63 | else // Otherwise add it. |
---|
64 | this.Keystrokes[ keyDef[0] ] = keyDef[1] === true ? true : keyDef ; |
---|
65 | } |
---|
66 | } |
---|
67 | } |
---|
68 | |
---|
69 | function _FCKKeystrokeHandler_OnKeyDown( ev, keystrokeHandler ) |
---|
70 | { |
---|
71 | // Get the key code. |
---|
72 | var keystroke = ev.keyCode || ev.which ; |
---|
73 | |
---|
74 | // Combine it with the CTRL, SHIFT and ALT states. |
---|
75 | var keyModifiers = 0 ; |
---|
76 | |
---|
77 | if ( ev.ctrlKey || ev.metaKey ) |
---|
78 | keyModifiers += CTRL ; |
---|
79 | |
---|
80 | if ( ev.shiftKey ) |
---|
81 | keyModifiers += SHIFT ; |
---|
82 | |
---|
83 | if ( ev.altKey ) |
---|
84 | keyModifiers += ALT ; |
---|
85 | |
---|
86 | var keyCombination = keystroke + keyModifiers ; |
---|
87 | |
---|
88 | var cancelIt = keystrokeHandler._CancelIt = false ; |
---|
89 | |
---|
90 | // Look for its definition availability. |
---|
91 | var keystrokeValue = keystrokeHandler.Keystrokes[ keyCombination ] ; |
---|
92 | |
---|
93 | // FCKDebug.Output( 'KeyDown: ' + keyCombination + ' - Value: ' + keystrokeValue ) ; |
---|
94 | |
---|
95 | // If the keystroke is defined |
---|
96 | if ( keystrokeValue ) |
---|
97 | { |
---|
98 | // If the keystroke has been explicetly set to "true" OR calling the |
---|
99 | // "OnKeystroke" event, it doesn't return "true", the default behavior |
---|
100 | // must be preserved. |
---|
101 | if ( keystrokeValue === true || !( keystrokeHandler.OnKeystroke && keystrokeHandler.OnKeystroke.apply( keystrokeHandler, keystrokeValue ) ) ) |
---|
102 | return true ; |
---|
103 | |
---|
104 | cancelIt = true ; |
---|
105 | } |
---|
106 | |
---|
107 | // By default, it will cancel all combinations with the CTRL key only (except positioning keys). |
---|
108 | if ( cancelIt || ( keystrokeHandler.CancelCtrlDefaults && keyModifiers == CTRL && ( keystroke < 33 || keystroke > 40 ) ) ) |
---|
109 | { |
---|
110 | keystrokeHandler._CancelIt = true ; |
---|
111 | |
---|
112 | if ( ev.preventDefault ) |
---|
113 | return ev.preventDefault() ; |
---|
114 | |
---|
115 | ev.returnValue = false ; |
---|
116 | ev.cancelBubble = true ; |
---|
117 | return false ; |
---|
118 | } |
---|
119 | |
---|
120 | return true ; |
---|
121 | } |
---|
122 | |
---|
123 | function _FCKKeystrokeHandler_OnKeyPress( ev, keystrokeHandler ) |
---|
124 | { |
---|
125 | if ( keystrokeHandler._CancelIt ) |
---|
126 | { |
---|
127 | // FCKDebug.Output( 'KeyPress Cancel', 'Red') ; |
---|
128 | |
---|
129 | if ( ev.preventDefault ) |
---|
130 | return ev.preventDefault() ; |
---|
131 | |
---|
132 | return false ; |
---|
133 | } |
---|
134 | |
---|
135 | return true ; |
---|
136 | } |
---|