[ Index ] |
|
Code source de FCKeditor 2.4 |
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 * File Name: fckkeystrokehandler.js 22 * Control keyboard keystroke combinations. 23 * 24 * File Authors: 25 * Frederico Caldeira Knabben (www.fckeditor.net) 26 */ 27 28 var FCKKeystrokeHandler = function( cancelCtrlDefaults ) 29 { 30 this.Keystrokes = new Object() ; 31 this.CancelCtrlDefaults = ( cancelCtrlDefaults !== false ) ; 32 } 33 34 /* 35 * Listen to keystroke events in an element or DOM document object. 36 * @target: The element or document to listen to keystroke events. 37 */ 38 FCKKeystrokeHandler.prototype.AttachToElement = function( target ) 39 { 40 // For newer browsers, it is enough to listen to the keydown event only. 41 // Some browsers instead, don't cancel key events in the keydown, but in the 42 // keypress. So we must do a longer trip in those cases. 43 FCKTools.AddEventListenerEx( target, 'keydown', _FCKKeystrokeHandler_OnKeyDown, this ) ; 44 if ( FCKBrowserInfo.IsGecko10 || FCKBrowserInfo.IsOpera || ( FCKBrowserInfo.IsGecko && FCKBrowserInfo.IsMac ) ) 45 FCKTools.AddEventListenerEx( target, 'keypress', _FCKKeystrokeHandler_OnKeyPress, this ) ; 46 } 47 48 /* 49 * Sets a list of keystrokes. It can receive either a single array or "n" 50 * arguments, each one being an array of 1 or 2 elemenst. The first element 51 * is the keystroke combination, and the second is the value to assign to it. 52 * If the second element is missing, the keystroke definition is removed. 53 */ 54 FCKKeystrokeHandler.prototype.SetKeystrokes = function() 55 { 56 // Look through the arguments. 57 for ( var i = 0 ; i < arguments.length ; i++ ) 58 { 59 var keyDef = arguments[i] ; 60 61 if ( typeof( keyDef[0] ) == 'object' ) // It is an array with arrays defining the keystrokes. 62 this.SetKeystrokes.apply( this, keyDef ) ; 63 else 64 { 65 if ( keyDef.length == 1 ) // If it has only one element, removed the keystroke. 66 delete this.Keystrokes[ keyDef[0] ] ; 67 else // Otherwise add it. 68 this.Keystrokes[ keyDef[0] ] = keyDef[1] === true ? true : keyDef ; 69 } 70 } 71 } 72 73 function _FCKKeystrokeHandler_OnKeyDown( ev, keystrokeHandler ) 74 { 75 // Get the key code. 76 var keystroke = ev.keyCode || ev.which ; 77 78 // Combine it with the CTRL, SHIFT and ALT states. 79 var keyModifiers = 0 ; 80 81 if ( ev.ctrlKey || ev.metaKey ) 82 keyModifiers += CTRL ; 83 84 if ( ev.shiftKey ) 85 keyModifiers += SHIFT ; 86 87 if ( ev.altKey ) 88 keyModifiers += ALT ; 89 90 var keyCombination = keystroke + keyModifiers ; 91 92 var cancelIt = keystrokeHandler._CancelIt = false ; 93 94 // Look for its definition availability. 95 var keystrokeValue = keystrokeHandler.Keystrokes[ keyCombination ] ; 96 97 // FCKDebug.Output( 'KeyDown: ' + keyCombination + ' - Value: ' + keystrokeValue ) ; 98 99 // If the keystroke is defined 100 if ( keystrokeValue ) 101 { 102 // If the keystroke has been explicetly set to "true" OR calling the 103 // "OnKeystroke" event, it doesn't return "true", the default behavior 104 // must be preserved. 105 if ( keystrokeValue === true || !( keystrokeHandler.OnKeystroke && keystrokeHandler.OnKeystroke.apply( keystrokeHandler, keystrokeValue ) ) ) 106 return true ; 107 108 cancelIt = true ; 109 } 110 111 // By default, it will cancel all combinations with the CTRL key only (except positioning keys). 112 if ( cancelIt || ( keystrokeHandler.CancelCtrlDefaults && keyModifiers == CTRL && ( keystroke < 33 || keystroke > 40 ) ) ) 113 { 114 keystrokeHandler._CancelIt = true ; 115 116 if ( ev.preventDefault ) 117 return ev.preventDefault() ; 118 119 ev.returnValue = false ; 120 ev.cancelBubble = true ; 121 return false ; 122 } 123 124 return true ; 125 } 126 127 function _FCKKeystrokeHandler_OnKeyPress( ev, keystrokeHandler ) 128 { 129 if ( keystrokeHandler._CancelIt ) 130 { 131 // FCKDebug.Output( 'KeyPress Cancel', 'Red') ; 132 133 if ( ev.preventDefault ) 134 return ev.preventDefault() ; 135 136 return false ; 137 } 138 139 return true ; 140 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 15:28:05 2007 | par Balluche grâce à PHPXref 0.7 |