summaryrefslogtreecommitdiffstats
path: root/third_party/google_input_tools/src/chrome/os/inputview/handler/pointerhandler.js
blob: c89434e2c9014b7fe44c297b1f38ae6f59acde43 (plain)
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
// Copyright 2014 The ChromeOS IME Authors. All Rights Reserved.
// limitations under the License.
// See the License for the specific language governing permissions and
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// distributed under the License is distributed on an "AS-IS" BASIS,
// Unless required by applicable law or agreed to in writing, software
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// You may obtain a copy of the License at
// you may not use this file except in compliance with the License.
// Licensed under the Apache License, Version 2.0 (the "License");
//
goog.provide('i18n.input.chrome.inputview.handler.PointerHandler');

goog.require('goog.Timer');
goog.require('goog.events.EventHandler');
goog.require('goog.events.EventTarget');
goog.require('goog.events.EventType');
goog.require('goog.math.Coordinate');
goog.require('i18n.input.chrome.inputview.handler.PointerActionBundle');

goog.scope(function() {



/**
 * The pointer controller.
 *
 * @param {!Element=} opt_target .
 * @constructor
 * @extends {goog.events.EventTarget}
 */
i18n.input.chrome.inputview.handler.PointerHandler = function(opt_target) {
  goog.base(this);

  /**
   * The pointer handlers.
   *
   * @type {!Object.<number, !i18n.input.chrome.inputview.handler.PointerActionBundle>}
   * @private
   */
  this.pointerActionBundles_ = {};

  /**
   * The event handler.
   *
   * @type {!goog.events.EventHandler}
   * @private
   */
  this.eventHandler_ = new goog.events.EventHandler(this);

  var target = opt_target || document;
  this.eventHandler_.
      listen(target, [goog.events.EventType.MOUSEDOWN,
        goog.events.EventType.TOUCHSTART], this.onPointerDown_, true).
      listen(document, goog.events.EventType.TOUCHEND, this.onPointerUp_, true).
      listen(document, goog.events.EventType.MOUSEUP, this.onPointerUp_, true).
      listen(target, goog.events.EventType.TOUCHMOVE, this.onPointerMove_,
          true).
      listen(target, goog.events.EventType.MOUSEMOVE, this.onPointerMove_,
          true);
};
goog.inherits(i18n.input.chrome.inputview.handler.PointerHandler,
    goog.events.EventTarget);
var PointerHandler = i18n.input.chrome.inputview.handler.PointerHandler;
var PointerActionBundle =
    i18n.input.chrome.inputview.handler.PointerActionBundle;
var Util = i18n.input.chrome.inputview.handler.Util;


/**
 * The canvas class name.
 * @const {string}
 * @private
 */
PointerHandler.CANVAS_CLASS_NAME_ = 'ita-hwt-canvas';


/**
 * Mouse down tick, which is for delayed pointer up for tap action on touchpad.
 *
 * @private {Date}
 */
PointerHandler.prototype.mouseDownTick_ = null;


/**
 * Event handler for previous mousedown or touchstart target.
 *
 * @private {i18n.input.chrome.inputview.handler.PointerActionBundle}
 */
PointerHandler.prototype.previousPointerActionBundle_ = null;


/**
 * Pointer action bundle for mouse down.
 * This is used in mouse up handler because mouse up event may have different
 * target than the mouse down event.
 *
 * @private {i18n.input.chrome.inputview.handler.PointerActionBundle}
 */
PointerHandler.prototype.pointerActionBundleForMouseDown_ = null;


/**
 * Gets the pointer handler for |view|. If not exists, creates a new one.
 *
 * @param {!i18n.input.chrome.inputview.elements.Element} view .
 * @return {!i18n.input.chrome.inputview.handler.PointerActionBundle} .
 * @private
 */
PointerHandler.prototype.getPointerActionBundle_ = function(view) {
  var uid = goog.getUid(view);
  if (!this.pointerActionBundles_[uid]) {
    this.pointerActionBundles_[uid] = new i18n.input.chrome.inputview.handler.
        PointerActionBundle(view, this);
  }
  return this.pointerActionBundles_[uid];
};


/**
 * Callback for mouse/touch down on the target.
 *
 * @param {!goog.events.BrowserEvent} e The event.
 * @private
 */
PointerHandler.prototype.onPointerDown_ = function(e) {
  var view = Util.getView(/** @type {!Node} */ (e.target));
  if (!view) {
    this.dispatchEvent(new i18n.input.chrome.inputview.events.PointerEvent(
        null, i18n.input.chrome.inputview.events.EventType.POINTER_DOWN,
        e.target, 0, 0, new Date().getTime()));
    return;
  }
  var pointerActionBundle = this.getPointerActionBundle_(view);
  if (this.previousPointerActionBundle_ &&
      this.previousPointerActionBundle_ != pointerActionBundle) {
    this.previousPointerActionBundle_.cancelDoubleClick();
  }
  this.previousPointerActionBundle_ = pointerActionBundle;
  pointerActionBundle.handlePointerDown(e);
  if (view.pointerConfig.preventDefault) {
    e.preventDefault();
  }
  if (view.pointerConfig.stopEventPropagation) {
    e.stopPropagation();
  }
  if (e.type == goog.events.EventType.MOUSEDOWN) {
    this.mouseDownTick_ = new Date();
    this.pointerActionBundleForMouseDown_ = pointerActionBundle;
  }
};


/**
 * Callback for pointer out.
 *
 * @param {!goog.events.BrowserEvent} e The event.
 * @private
 */
PointerHandler.prototype.onPointerUp_ = function(e) {
  var pointerActionBundle;
  if (e.type == goog.events.EventType.MOUSEUP) {
    // If mouseup happens too fast after mousedown, it may be a tap action on
    // touchpad, so delay the pointer up action so user can see the visual
    // flash.
    if (this.mouseDownTick_ && new Date() - this.mouseDownTick_ < 10) {
      goog.Timer.callOnce(this.onPointerUp_.bind(this, e), 50);
      return;
    }
    if (this.pointerActionBundleForMouseDown_) {
      this.pointerActionBundleForMouseDown_.handlePointerUp(e);
      pointerActionBundle = this.pointerActionBundleForMouseDown_;
      this.pointerActionBundleForMouseDown_ = null;
    }
  } else {
    var view = Util.getView(/** @type {!Node} */ (e.target));
    if (!view) {
      return;
    }
    pointerActionBundle = this.pointerActionBundles_[goog.getUid(view)];
    if (pointerActionBundle) {
      pointerActionBundle.handlePointerUp(e);
    }
  }
  e.preventDefault();
  if (pointerActionBundle && pointerActionBundle.view &&
      pointerActionBundle.view.pointerConfig.stopEventPropagation) {
    e.stopPropagation();
  }
};


/**
 * Callback for touchmove or mousemove.
 *
 * @param {!goog.events.BrowserEvent} e The event.
 * @private
 */
PointerHandler.prototype.onPointerMove_ = function(e) {
  if (e.type == goog.events.EventType.MOUSEMOVE) {
    if (this.pointerActionBundleForMouseDown_) {
      this.pointerActionBundleForMouseDown_.handlePointerMove(
          /** @type {!Event} */ (e.getBrowserEvent()));
    }
    return;
  }
  var touches = e.getBrowserEvent()['touches'];
  if (!touches || touches.length == 0) {
    return;
  }
  if (touches.length > 1) {
    e.preventDefault();
  }
  var shouldPreventDefault = false;
  var shouldStopEventPropagation = false;
  for (var i = 0; i < touches.length; i++) {
    var view = Util.getView(/** @type {!Node} */ (touches[i].target));
    if (view) {
      var pointerActionBundle = this.pointerActionBundles_[goog.getUid(view)];
      if (pointerActionBundle) {
        pointerActionBundle.handlePointerMove(touches[i]);
      }
      if (view.pointerConfig.preventDefauat) {
        shouldPreventDefault = true;
      }
      if (view.pointerConfig.stopEventPropagation) {
        shouldStopEventPropagation = true;
      }
    }
  }
  if (shouldPreventDefault) {
    e.preventDefault();
  }
  if (shouldStopEventPropagation) {
    e.stopPropagation();
  }
};


/** @override */
PointerHandler.prototype.disposeInternal = function() {
  for (var bundle in this.pointerActionBundles_) {
    goog.dispose(bundle);
  }
  goog.dispose(this.eventHandler_);

  goog.base(this, 'disposeInternal');
};

});  // goog.scope