1 /*
  2 Copyright (c) 2003-2012, 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 			this.stopPropagation();
 91 	},
 92 
 93 	stopPropagation : function()
 94 	{
 95 		var $ = this.$;
 96 		if ( $.stopPropagation )
 97 			$.stopPropagation();
 98 		else
 99 			$.cancelBubble = true;
100 	},
101 
102 	/**
103 	 * Returns the DOM node where the event was targeted to.
104 	 * @returns {CKEDITOR.dom.node} The target DOM node.
105 	 * @example
106 	 * var element = CKEDITOR.document.getById( 'myElement' );
107 	 * element.on( 'click', function( ev )
108 	 *     {
109 	 *         // The DOM event object is passed by the "data" property.
110 	 *         var domEvent = ev.data;
111 	 *         // Add a CSS class to the event target.
112 	 *         domEvent.getTarget().addClass( 'clicked' );
113 	 *     });
114 	 */
115 
116 	getTarget : function()
117 	{
118 		var rawNode = this.$.target || this.$.srcElement;
119 		return rawNode ? new CKEDITOR.dom.node( rawNode ) : null;
120 	},
121 
122 	/**
123 	 * Retrieves the coordinates of the mouse pointer relative to the top-left
124 	 * corner of the document, in mouse related event.
125 	 * @returns {Object} The object contains the position.
126 	 * @example
127 	 * element.on( 'mousemouse', function( ev )
128 	 *     {
129 	 *         var pageOffset = ev.data.getPageOffset();
130 	 *         alert( pageOffset.x ); // page offset X
131 	 *         alert( pageOffset.y ); // page offset Y
132 	 *     });
133 	 */
134 	getPageOffset : function()
135 	{
136 		var doc = this.getTarget().getDocument().$;
137 		var pageX = this.$.pageX || this.$.clientX + ( doc.documentElement.scrollLeft || doc.body.scrollLeft );
138 		var pageY = this.$.pageY || this.$.clientY + ( doc.documentElement.scrollTop || doc.body.scrollTop );
139 		return { x : pageX, y : pageY };
140 	}
141 };
142 
143 // For the followind constants, we need to go over the Unicode boundaries
144 // (0x10FFFF) to avoid collision.
145 
146 /**
147  * CTRL key (0x110000).
148  * @constant
149  * @example
150  */
151 CKEDITOR.CTRL = 0x110000;
152 
153 /**
154  * SHIFT key (0x220000).
155  * @constant
156  * @example
157  */
158 CKEDITOR.SHIFT = 0x220000;
159 
160 /**
161  * ALT key (0x440000).
162  * @constant
163  * @example
164  */
165 CKEDITOR.ALT = 0x440000;
166