Ticket #2140: fckkeystrokehandler.2.js

File fckkeystrokehandler.2.js, 5.3 KB (added by Kei Takashima, 16 years ago)
Line 
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
24var 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 */
34FCKKeystrokeHandler.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 */
50FCKKeystrokeHandler.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 the configuration for the keystrokes is missing some element or has any extra comma
58                // this item won't be valid, so skip it and keep on processing.
59                if ( !keyDef ) 
60                        continue ;
61               
62                if ( typeof( keyDef[0] ) == 'object' )          // It is an array with arrays defining the keystrokes.
63                        this.SetKeystrokes.apply( this, keyDef ) ;
64                else
65                {
66                        if ( keyDef.length == 1 )               // If it has only one element, remove the keystroke.
67                                delete this.Keystrokes[ keyDef[0] ] ;
68                        else                                                    // Otherwise add it.
69                                this.Keystrokes[ keyDef[0] ] = keyDef[1] === true ? true : keyDef ;
70                }
71        }
72}
73
74function _FCKKeystrokeHandler_OnKeyDown( ev, keystrokeHandler )
75{
76        // Get the key code.
77        var keystroke = ev.keyCode || ev.which ;
78
79        // Combine it with the CTRL, SHIFT and ALT states.
80        var keyModifiers = _FCKKeystrokeHandler_GetKeyModifiers(evt) ;
81
82        var keyCombination = keystroke + keyModifiers ;
83
84        var cancelIt = keystrokeHandler._CancelIt = false ;
85
86        // Look for its definition availability.
87        var keystrokeValue = keystrokeHandler.Keystrokes[ keyCombination ] ;
88
89  //    FCKDebug.Output( 'KeyDown: ' + keyCombination + ' - Value: ' + keystrokeValue ) ;
90
91  if( keystroke == 13 && ( FCKBrowserInfo.IsGecko && FCKBrowserInfo.IsMac )){
92      return true;
93  }
94
95
96        // If the keystroke is defined
97        if ( keystrokeValue )
98        {
99                // If the keystroke has been explicitly set to "true" OR calling the
100                // "OnKeystroke" event, it doesn't return "true", the default behavior
101                // must be preserved.
102                if ( keystrokeValue === true || !( keystrokeHandler.OnKeystroke && keystrokeHandler.OnKeystroke.apply( keystrokeHandler, keystrokeValue ) ) )
103                        return true ;
104
105                cancelIt = true ;
106        }
107
108        // By default, it will cancel all combinations with the CTRL key only (except positioning keys).
109        if ( cancelIt || ( keystrokeHandler.CancelCtrlDefaults && keyModifiers == CTRL && ( keystroke < 33 || keystroke > 40 ) ) )
110        {
111                keystrokeHandler._CancelIt = true ;
112
113                if ( ev.preventDefault )
114                        return ev.preventDefault() ;
115
116                ev.returnValue = false ;
117                ev.cancelBubble = true ;
118                return false ;
119        }
120
121        return true ;
122}
123
124function _FCKKeystrokeHandler_GetKeyModifiers( ev )
125{
126  var keyModifiers = 0;
127        if ( ev.ctrlKey || ev.metaKey )
128                keyModifiers += CTRL ;
129
130        if ( ev.shiftKey )
131                keyModifiers += SHIFT ;
132
133        if ( ev.altKey )
134                keyModifiers += ALT ;
135} 
136
137function _FCKKeystrokeHandler_OnKeyPress( ev, keystrokeHandler )
138{   
139  // Get the key code.
140        var keystroke = ev.keyCode || ev.which ;
141
142        // Combine it with the CTRL, SHIFT and ALT states.
143        var keyModifiers = _FCKKeystrokeHandler_GetKeyModifiers(ev);
144        var keyCombination = keystroke + keyModifiers ; 
145        var keystrokeValue = keystrokeHandler.Keystrokes[ keyCombination ] ;
146
147  if( keystroke == 13 && ( FCKBrowserInfo.IsGecko && FCKBrowserInfo.IsMac )){
148     if ( keystrokeValue ) {
149        if (keystrokeHandler.OnKeystroke) {
150            keystrokeHandler.OnKeystroke.apply( keystrokeHandler, keystrokeValue );
151        }
152        keystrokeHandler._CancelIt = true;
153     }                                   
154  }                                         
155 
156        if ( keystrokeHandler._CancelIt )
157        {
158    FCKDebug.Output( 'KeyPress Cancel', 'Red') ;
159
160                if ( ev.preventDefault )
161                        return ev.preventDefault() ;
162
163                return false ;
164        }
165
166        return true ;
167}
© 2003 – 2022, CKSource sp. z o.o. sp.k. All rights reserved. | Terms of use | Privacy policy