123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- 'use strict';
- goog.provide('Blockly.Touch');
- goog.require('goog.events');
- goog.require('goog.events.BrowserFeature');
- goog.require('goog.string');
- Blockly.Touch.touchIdentifier_ = null;
- Blockly.Touch.onTouchUpWrapper_ = null;
- Blockly.Touch.TOUCH_MAP = {};
- if (goog.events.BrowserFeature.TOUCH_ENABLED) {
- Blockly.Touch.TOUCH_MAP = {
- 'mousedown': ['touchstart'],
- 'mousemove': ['touchmove'],
- 'mouseup': ['touchend', 'touchcancel']
- };
- }
- Blockly.longPid_ = 0;
- Blockly.longStart_ = function(e, uiObject) {
- Blockly.longStop_();
- Blockly.longPid_ = setTimeout(function() {
- e.button = 2;
- uiObject.onMouseDown_(e);
- }, Blockly.LONGPRESS);
- };
- Blockly.longStop_ = function() {
- if (Blockly.longPid_) {
- clearTimeout(Blockly.longPid_);
- Blockly.longPid_ = 0;
- }
- };
- Blockly.onMouseUp_ = function(e) {
- var workspace = Blockly.getMainWorkspace();
- if (workspace.dragMode_ == Blockly.DRAG_NONE) {
- return;
- }
- Blockly.Touch.clearTouchIdentifier();
- Blockly.Css.setCursor(Blockly.Css.Cursor.OPEN);
- workspace.dragMode_ = Blockly.DRAG_NONE;
-
- if (Blockly.Touch.onTouchUpWrapper_) {
- Blockly.unbindEvent_(Blockly.Touch.onTouchUpWrapper_);
- Blockly.Touch.onTouchUpWrapper_ = null;
- }
- if (Blockly.onMouseMoveWrapper_) {
- Blockly.unbindEvent_(Blockly.onMouseMoveWrapper_);
- Blockly.onMouseMoveWrapper_ = null;
- }
- };
- Blockly.onMouseMove_ = function(e) {
- var workspace = Blockly.getMainWorkspace();
- if (workspace.dragMode_ != Blockly.DRAG_NONE) {
- var dx = e.clientX - workspace.startDragMouseX;
- var dy = e.clientY - workspace.startDragMouseY;
- var metrics = workspace.startDragMetrics;
- var x = workspace.startScrollX + dx;
- var y = workspace.startScrollY + dy;
- x = Math.min(x, -metrics.contentLeft);
- y = Math.min(y, -metrics.contentTop);
- x = Math.max(x, metrics.viewWidth - metrics.contentLeft -
- metrics.contentWidth);
- y = Math.max(y, metrics.viewHeight - metrics.contentTop -
- metrics.contentHeight);
-
- workspace.scrollbar.set(-x - metrics.contentLeft,
- -y - metrics.contentTop);
-
- if (Math.sqrt(dx * dx + dy * dy) > Blockly.DRAG_RADIUS) {
- Blockly.longStop_();
- workspace.dragMode_ = Blockly.DRAG_FREE;
- }
- e.stopPropagation();
- e.preventDefault();
- }
- };
- Blockly.Touch.clearTouchIdentifier = function() {
- Blockly.Touch.touchIdentifier_ = null;
- };
- Blockly.Touch.shouldHandleEvent = function(e) {
- return !Blockly.Touch.isMouseOrTouchEvent(e) ||
- Blockly.Touch.checkTouchIdentifier(e);
- };
- Blockly.Touch.checkTouchIdentifier = function(e) {
- var identifier = (e.changedTouches && e.changedTouches[0] &&
- e.changedTouches[0].identifier != undefined &&
- e.changedTouches[0].identifier != null) ?
- e.changedTouches[0].identifier : 'mouse';
-
-
- if (Blockly.Touch.touchIdentifier_ != undefined &&
- Blockly.Touch.touchIdentifier_ != null) {
-
-
- return Blockly.Touch.touchIdentifier_ == identifier;
- }
- if (e.type == 'mousedown' || e.type == 'touchstart') {
-
-
- Blockly.Touch.touchIdentifier_ = identifier;
- return true;
- }
-
-
-
- return false;
- };
- Blockly.Touch.setClientFromTouch = function(e) {
- if (goog.string.startsWith(e.type, 'touch')) {
-
- var touchPoint = e.changedTouches[0];
- e.clientX = touchPoint.clientX;
- e.clientY = touchPoint.clientY;
- }
- };
- Blockly.Touch.isMouseOrTouchEvent = function(e) {
- return goog.string.startsWith(e.type, 'touch') ||
- goog.string.startsWith(e.type, 'mouse');
- };
- Blockly.Touch.splitEventByTouches = function(e) {
- var events = [];
- if (e.changedTouches) {
- for (var i = 0; i < e.changedTouches.length; i++) {
- var newEvent = {
- type: e.type,
- changedTouches: [e.changedTouches[i]],
- target: e.target,
- stopPropagation: function(){ e.stopPropagation(); },
- preventDefault: function(){ e.preventDefault(); }
- };
- events[i] = newEvent;
- }
- } else {
- events.push(e);
- }
- return events;
- };
|