File size: 12,200 Bytes
e1d589b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
 * Copyright 2014 Amadeus s.a.s.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

var klass = require("../klass");
var touchEvent = require("./touchEvent");

var Gesture = klass({
    /**
     * Defines the number of touch for the gesture.
     */
    NB_TOUCHES : 1,

    /**
     * Constructor.
     */
    $constructor : function (nodeInstance, callback) {
        /**
         * Reference to the target
         * @type HTMLElement
         */
        this.target = nodeInstance.node;

        /**
         * Reference to the event handler
         * @type Function
         */
        this.callback = callback;

        /**
         * Event map uses ./touchEvent for touch event detection.
         * @type Object
         */
        this.touchEventMap = touchEvent.touchEventMap;
        /**
         * A flag indicating whether the starting events are already attached.
         * @type Boolean
         */
        this.eventsAlreadyAttached = false;
        /**
         * The id of the timer associated to the gesture.
         * @type Integer
         */
        this.timerId = null;
        /**
         * The gesture initial data.
         * @type Object
         */
        this.startData = null;
        /**
         * The gesture current data.
         * @type Object
         */
        this.currentData = null;

        this.callbackMap = {};

        this._connectTouchEvents();
    },

    $dispose: function() {
        if (this.callbackMap) {
            this._disconnectTouchEvents();
        }
        this.callbackMap = null;
    },

    /**
     * Connects touch events.
     * @private
     */
    _connectTouchEvents : function () {
        if (!this.eventsAlreadyAttached) {
            var map = this._getInitialListenersList();
            for (var i = 0; i < map.length; i++) {
                this._addListener(map[i].evt, map[i].cb);
            }
            this.eventsAlreadyAttached = true;
        }
    },

    /**
     * Disconnects touch events added in _connectTouchEvents.
     * @private
     */
    _disconnectTouchEvents : function () {
        var map = this._getInitialListenersList();
        for (var i = 0; i < map.length; i++) {
            this._removeListener(map[i].evt, map[i].cb);
        }
        this.eventsAlreadyAttached = false;
    },

    /**
     * Returns the list of listeners to be attached when loading the gesture. These listeners are also detached during the gesture, and reattached
     * when it ends or when it is cancelled.
     * @protected
     * @return {Object} the list of listeners.
     */
    _getInitialListenersList : function () {
        return [];
    },

    /**
     * Returns the list of listeners to be attached when the gesture is started, and detached when the gesture is
     * finished.
     * @protected
     * @return {Object} the list of listeners.
     */
    _getAdditionalListenersList : function () {
        return [];
    },

    /**
     * Returns the map of fake events to be raised during the gesture lifecycle. Format: {start: "start_event_name",
     * move: "move_event_name", end : "end_event_name", cancel: "cancel_event_name"}
     * @protected
     * @return {Object} the map of listeners.
     */
    _getFakeEventsMap : function () {
        return {};
    },

    /**
     * Generic start point for a gesture: unregisters initial listeners, registers additional listeners, set initial
     * data, optional fake event raised
     * @param {Object} event the original event
     * @protected
     * @return {Boolean} false if preventDefault is true
     */
    _gestureStart : function (event, extraData) {
        if (!this.__validateNbTouches(event)) {
            return null;
        }
        this._disconnectTouchEvents();
        this.startData = {
            positions : touchEvent.getPositions(event),
            time : (new Date()).getTime()
        };
        this.currentData = null;
        this._connectAdditionalTouchEvents();
        if (this._getFakeEventsMap().start) {
            return this._raiseFakeEvent(event, this._getFakeEventsMap().start, extraData);
        } else {
            return (event.returnValue != null) ? event.returnValue : !event.defaultPrevented;
        }
    },

    /**
     * Generic optional move: manages intermediate states during the gesture
     * @param {Object} event the original event
     * @protected
     * @return {Boolean} false if preventDefault is true
     */
    _gestureMove : function (event, extraData) {
        if (!this.__validateNbTouches(event)) {
            return null;
        }
        this.currentData = {
            positions : touchEvent.getPositions(event),
            time : (new Date()).getTime()
        };
        if (this._getFakeEventsMap().move) {
            return this._raiseFakeEvent(event, this._getFakeEventsMap().move, extraData);
        } else {
            return (event.returnValue != null) ? event.returnValue : !event.defaultPrevented;
        }
    },

    /**
     * Generic success end point for the gesture: final fake event raised, additional listeners unregistered,
     * initial listeners attached again.
     * @param {Object} event the original event
     * @protected
     * @return {Boolean} false if preventDefault is true
     */
    _gestureEnd : function (event, extraData) {
        if (!this.__validateNbTouches(event)) {
            return null;
        }
        this._disconnectAdditionalTouchEvents();
        this._connectTouchEvents();
        this.currentData = {
            positions : touchEvent.getPositions(event),
            time : (new Date()).getTime()
        };
        if (this._getFakeEventsMap().end) {
            return this._raiseFakeEvent(event, this._getFakeEventsMap().end, extraData);
        } else {
            return (event.returnValue != null) ? event.returnValue : !event.defaultPrevented;
        }
    },

    /**
     * Generic failure end point for the gesture: optional fake event raised, additional listeners unregistered,
     * initial listeners attached again.
     * @param {Object} event the original event
     * @protected
     * @return {Boolean} false if preventDefault is true
     */
    _gestureCancel : function (event, extraData) {
        this._disconnectAdditionalTouchEvents();
        this._connectTouchEvents();
        this.currentData = {
            positions : touchEvent.getPositions(event),
            time : (new Date()).getTime()
        };
        if (this._getFakeEventsMap().cancel) {
            return this._raiseFakeEvent(event, this._getFakeEventsMap().cancel, extraData);
        } else {
            return (event.returnValue != null) ? event.returnValue : !event.defaultPrevented;
        }
    },

    /**
     * Registers the listeners added during the gesture lifecycle, once the gesture is started.
     * @private
     */
    _connectAdditionalTouchEvents : function () {
        var map = this._getAdditionalListenersList();
        for (var i = 0; i < map.length; i++) {
            this._addListener(map[i].evt, map[i].cb);
        }
    },

    /**
     * Unregisters the listeners added during the gesture lifecycle.
     * @private
     */
    _disconnectAdditionalTouchEvents : function () {
        var map = this._getAdditionalListenersList();
        for (var i = 0; i < map.length; i++) {
            this._removeListener(map[i].evt, map[i].cb);
        }
    },

    /**
     * Adds an event listener
     * @param {Object} eventName the event name
     * @param {Object} cb the callback for the event
     * @protected
     */
    _addListener : function (eventName, cb) {
        this.callbackMap[eventName] = cb;
        var addEL = (this.target.addEventListener !== undefined);
        if (!addEL) {
            var self = this;
            this._attachEventFn = function (evt) {
                self.handleEvent(evt);
            };
        }
        if (addEL) {
            this.target.addEventListener(eventName, this, false);
        } else {
            this.target.attachEvent("on" + eventName, this._attachEventFn);
        }
    },

    /**
     * Event Listener callback
     */
    handleEvent : function (evt) {
        if (this.callbackMap[evt.type]) {
            var cb = this.callbackMap[evt.type];
            if (cb) {
                return cb(evt);
            }
        }
        
    },

    /**
     * Removes an event listener
     * @param {String} eventName the event name
     * @param {Object} cb the callback for the event
     * @protected
     */
    _removeListener : function (eventName, cb) {
        this.callbackMap[eventName] = null;
        var rmEL = (this.target.removeEventListener !== undefined);
        if (rmEL) {
            this.target.removeEventListener(eventName, this, false);
        } else {
            this.target.detachEvent("on" + eventName, this._attachEventFn);
        }
    },

    /**
     * Raises a fake event, using Delegate.delegate to accurately delegate the event to the appropriate DOM element.
     * @param {Object} event the original event
     * @param {String} name the fake event name
     * @protected
     * @return {Boolean} false if preventDefault is true
     */
    _raiseFakeEvent : function (event, name, extraData) {
        var target = (event.target) ? event.target : event.srcElement;
        var fakeEvent = touchEvent.getFakeEvent(name, target);
        if (!event.returnValue) {
            fakeEvent.preventDefault();
        }
        if (event.cancelBubble) {
            fakeEvent.stopPropagation();
        }
        fakeEvent.pageX = event.pageX;
        fakeEvent.pageY = event.pageY;
        fakeEvent.clientX = event.clientX;
        fakeEvent.clientY = event.clientY;
        fakeEvent.touches = event.touches;
        fakeEvent.changedTouches = event.changedTouches;
        fakeEvent.isPrimary = event.isPrimary;
        if (this.startData) {
            if (this.startData.time) {
                fakeEvent.duration = (new Date()).getTime() - this.startData.time;
            }
        }
        if (!extraData) {
            extraData = {};
        }
        extraData.startX = this.startData.positions[0].x;
        extraData.startY = this.startData.positions[0].y;
        var position = touchEvent.getPositions(event);
        extraData.currentX = position[0].x;
        extraData.currentY = position[0].y;
        fakeEvent.detail = extraData;
        this.callback(fakeEvent);
        event.cancelBubble = fakeEvent.hasStopPropagation;
        event.returnValue = !fakeEvent.hasPreventDefault;
        return event.returnValue;
    },

    /**
     * Returns the distance between two points.
     * @param {Integer} x1 x of the first point
     * @param {Integer} y1 y of the first point
     * @param {Integer} x2 x of the second point
     * @param {Integer} y2 y of the second point
     * @protected
     * @return {Number} the distance
     */
    _calculateDistance : function (x1, y1, x2, y2) {
        return Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
    },

    /**
     * Validates the event against the number of touches required for the gesture. Rules: - single touch gestures
     * are canceled as soon as multi touch is started. - multi touch gestures need all touch events (IE 10
     * constraint).
     * @param {Object} event the event
     * @private
     * @return {Boolean} true if the event's touches are validated for the current gesture.
     */
    __validateNbTouches : function (event) {
        var fingerIndex = touchEvent.getFingerIndex(event);
        return this.NB_TOUCHES == 1 && fingerIndex === 0 || this.NB_TOUCHES == 2 && fingerIndex >= 0;
    }
});

module.exports.Gesture = Gesture;