618a11035c156d89da83689851e876fd4f111ad3
[ckeditor.git] / _source / core / dom / event.js
1 /*
2 Copyright (c) 2003-2011, 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 // For the followind constants, we need to go over the Unicode boundaries
124 // (0x10FFFF) to avoid collision.
125
126 /**
127 * CTRL key (0x110000).
128 * @constant
129 * @example
130 */
131 CKEDITOR.CTRL = 0x110000;
132
133 /**
134 * SHIFT key (0x220000).
135 * @constant
136 * @example
137 */
138 CKEDITOR.SHIFT = 0x220000;
139
140 /**
141 * ALT key (0x440000).
142 * @constant
143 * @example
144 */
145 CKEDITOR.ALT = 0x440000;