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