]> git.proxmox.com Git - sencha-touch.git/blob - src/src/event/Dom.js
import Sencha Touch 2.4.2 source
[sencha-touch.git] / src / src / event / Dom.js
1 /**
2 * @private
3 * @extends Object
4 * DOM event. This class really extends {@link Ext.event.Event}, but for documentation
5 * purposes it's members are listed inside {@link Ext.event.Event}.
6 */
7 Ext.define('Ext.event.Dom', {
8 extend: 'Ext.event.Event',
9
10 constructor: function(event) {
11 var target = event.target,
12 touches;
13
14 if (target && target.nodeType !== 1) {
15 target = target.parentNode;
16 }
17 touches = event.changedTouches;
18 if (touches) {
19 touches = touches[0];
20 this.pageX = touches.pageX;
21 this.pageY = touches.pageY;
22 }
23 else {
24 this.pageX = event.pageX;
25 this.pageY = event.pageY;
26 }
27
28 this.browserEvent = this.event = event;
29 this.target = this.delegatedTarget = target;
30 this.type = event.type;
31
32 this.timeStamp = this.time = +event.timeStamp;
33
34 return this;
35 },
36
37 /**
38 * @property {Number} distance
39 * The distance of the event.
40 *
41 * **This is only available when the event type is `swipe` and `pinch`.**
42 */
43
44 /**
45 * @property {HTMLElement} target
46 * The target HTMLElement for this event. For example; if you are listening to a tap event and you tap on a `<div>` element,
47 * this will return that `<div>` element.
48 */
49
50 /**
51 * @property {Number} pageX The browsers x coordinate of the event.
52 */
53
54 /**
55 * @property {Number} pageY The browsers y coordinate of the event.
56 */
57
58 stopEvent: function() {
59 this.preventDefault();
60
61 return this.callParent();
62 },
63
64 /**
65 * Prevents the browsers default handling of the event.
66 */
67 preventDefault: function() {
68 this.browserEvent.preventDefault();
69 },
70
71 /**
72 * Gets the x coordinate of the event.
73 * @deprecated 2.0 Please use {@link #pageX} property directly.
74 * @return {Number}
75 */
76 getPageX: function() {
77 return this.pageX || this.browserEvent.pageX;
78 },
79
80 /**
81 * Gets the y coordinate of the event.
82 * @deprecated 2.0 Please use {@link #pageX} property directly.
83 * @return {Number}
84 */
85 getPageY: function() {
86 return this.pageY || this.browserEvent.pageY;
87 },
88
89 /**
90 * Gets the X and Y coordinates of the event.
91 * @deprecated 2.0 Please use the {@link #pageX} and {@link #pageY} properties directly.
92 * @return {Array}
93 */
94 getXY: function() {
95 if (!this.xy) {
96 this.xy = [this.getPageX(), this.getPageY()];
97 }
98
99 return this.xy;
100 },
101
102 /**
103 * Gets the target for the event. Unlike {@link #target}, this returns the main element for your event. So if you are
104 * listening to a tap event on Ext.Viewport.element, and you tap on an inner element of Ext.Viewport.element, this will
105 * return Ext.Viewport.element.
106 *
107 * If you want the element you tapped on, then use {@link #target}.
108 *
109 * @param {String} selector (optional) A simple selector to filter the target or look for an ancestor of the target
110 * @param {Number/Mixed} [maxDepth=10||document.body] (optional) The max depth to
111 * search as a number or element (defaults to 10 || document.body)
112 * @param {Boolean} returnEl (optional) `true` to return a Ext.Element object instead of DOM node.
113 * @return {HTMLElement}
114 */
115 getTarget: function(selector, maxDepth, returnEl) {
116 if (arguments.length === 0) {
117 return this.delegatedTarget;
118 }
119
120 return selector ? Ext.fly(this.target).findParent(selector, maxDepth, returnEl) : (returnEl ? Ext.get(this.target) : this.target);
121 },
122
123 /**
124 * Returns the time of the event.
125 * @return {Date}
126 */
127 getTime: function() {
128 return this.time;
129 },
130
131 setDelegatedTarget: function(target) {
132 this.delegatedTarget = target;
133 },
134
135 makeUnpreventable: function() {
136 this.browserEvent.preventDefault = Ext.emptyFn;
137 }
138 });