1 /*
  2 Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.
  3 For licensing, see LICENSE.html or http://ckeditor.com/license
  4 */
  5
  6 /**
  7  * @fileOverview Defines the {@link CKEDITOR.dom.event} class, which
  8  *		represents the a native DOM event object.
  9  */
 10
 11 /**
 12  * Represents a native DOM event object.
 13  * @constructor
 14  * @param {Object} domEvent A native DOM event object.
 15  * @example
 16  */
 17 CKEDITOR.dom.event = function( domEvent )
 18 {
 19 	/**
 20 	 * The native DOM event object represented by this class instance.
 21 	 * @type Object
 22 	 * @example
 23 	 */
 24 	this.$ = domEvent;
 25 };
 26
 27 CKEDITOR.dom.event.prototype =
 28 {
 29 	/**
 30 	 * Gets the key code associated to the event.
 31 	 * @returns {Number} The key code.
 32 	 * @example
 33 	 * alert( event.getKey() );  "65" is "a" has been pressed
 34 	 */
 35 	getKey : function()
 36 	{
 37 		return this.$.keyCode || this.$.which;
 38 	},
 39
 40 	/**
 41 	 * Gets a number represeting the combination of the keys pressed during the
 42 	 * event. It is the sum with the current key code and the {@link CKEDITOR.CTRL},
 43 	 * {@link CKEDITOR.SHIFT} and {@link CKEDITOR.ALT} constants.
 44 	 * @returns {Number} The number representing the keys combination.
 45 	 * @example
 46 	 * alert( event.getKeystroke() == 65 );                                   // "a" key
 47 	 * alert( event.getKeystroke() == CKEDITOR.CTRL + 65 );                   // CTRL + "a" key
 48 	 * alert( event.getKeystroke() == CKEDITOR.CTRL + CKEDITOR.SHIFT + 65 );  // CTRL + SHIFT + "a" key
 49 	 */
 50 	getKeystroke : function()
 51 	{
 52 		var keystroke = this.getKey();
 53
 54 		if ( this.$.ctrlKey || this.$.metaKey )
 55 			keystroke += CKEDITOR.CTRL;
 56
 57 		if ( this.$.shiftKey )
 58 			keystroke += CKEDITOR.SHIFT;
 59
 60 		if ( this.$.altKey )
 61 			keystroke += CKEDITOR.ALT;
 62
 63 		return keystroke;
 64 	},
 65
 66 	/**
 67 	 * Prevents the original behavior of the event to happen. It can optionally
 68 	 * stop propagating the event in the event chain.
 69 	 * @param {Boolean} [stopPropagation] Stop propagating this event in the
 70 	 *		event chain.
 71 	 * @example
 72 	 * var element = CKEDITOR.document.getById( 'myElement' );
 73 	 * element.on( 'click', function( ev )
 74 	 *     {
 75 	 *         // The DOM event object is passed by the "data" property.
 76 	 *         var domEvent = ev.data;
 77 	 *         // Prevent the click to chave any effect in the element.
 78 	 *         domEvent.preventDefault();
 79 	 *     });
 80 	 */
 81 	preventDefault : function( stopPropagation )
 82 	{
 83 		var $ = this.$;
 84 		if ( $.preventDefault )
 85 			$.preventDefault();
 86 		else
 87 			$.returnValue = false;
 88
 89 		if ( stopPropagation )
 90 		{
 91 			if ( $.stopPropagation )
 92 				$.stopPropagation();
 93 			else
 94 				$.cancelBubble = true;
 95 		}
 96 	},
 97 	/**
 98 	 * Returns the DOM node where the event was targeted to.
 99 	 * @returns {CKEDITOR.dom.node} The target DOM node.
100 	 * @example
101 	 * var element = CKEDITOR.document.getById( 'myElement' );
102 	 * element.on( 'click', function( ev )
103 	 *     {
104 	 *         // The DOM event object is passed by the "data" property.
105 	 *         var domEvent = ev.data;
106 	 *         // Add a CSS class to the event target.
107 	 *         domEvent.getTarget().addClass( 'clicked' );
108 	 *     });
109 	 */
110
111 	getTarget : function()
112 	{
113 		var rawNode = this.$.target || this.$.srcElement;
114 		return rawNode ? new CKEDITOR.dom.node( rawNode ) : null;
115 	}
116 };
117
118 /**
119  * CTRL key (1000).
120  * @constant
121  * @example
122  */
123 CKEDITOR.CTRL = 1000;
124
125 /**
126  * SHIFT key (2000).
127  * @constant
128  * @example
129  */
130 CKEDITOR.SHIFT = 2000;
131
132 /**
133  * ALT key (4000).
134  * @constant
135  * @example
136  */
137 CKEDITOR.ALT = 4000;
138