/* General Utils ---------------------------------------------------------------------------------------------------------------------- */ $.simulateByPoint = (type, options) => { let docEl = $(document) let point = options.point let clientX let clientY let node if (point) { clientX = point.left - docEl.scrollLeft() clientY = point.top - docEl.scrollTop() node = document.elementFromPoint(clientX, clientY) $(node).simulate(type, options) } } /* Touch ---------------------------------------------------------------------------------------------------------------------- */ let origSimulateEvent = $.simulate.prototype.simulateEvent let touchUID = Date.now() $.simulate.prototype.simulateEvent = function (elem, type, options) { // eslint-disable-line func-names if (elem === window && type === 'resize') { return this.simulateWindowResize() } if (/^touch/.test(type)) { return this.simulateTouchEvent(elem, type, options) } return origSimulateEvent.apply(this, arguments) // eslint-disable-line prefer-rest-params } $.simulate.prototype.simulateWindowResize = function () { // eslint-disable-line func-names // from https://stackoverflow.com/a/1818513/96342 let event if (typeof Event !== 'undefined') { try { event = new Event('resize') } catch (ex) { // why would fail? } } if (!event) { event = document.createEvent('UIEvents') event.initUIEvent('resize', true, false, window, 0) } this.dispatchEvent(window, 'resize', event) } $.simulate.prototype.simulateTouchEvent = function (elem, type, options) { // eslint-disable-line func-names // http://stackoverflow.com/a/29019278/96342 let event = document.createEvent('Event') event.initEvent(type, true, true); // cancelable, bubbleable (event as any).touches = [{ target: elem, identifier: touchUID, pageX: options.clientX, pageY: options.clientY, screenX: options.clientX, screenY: options.clientY, clientX: options.clientX, clientY: options.clientY, }] touchUID += 1 this.dispatchEvent(elem, type, event, options) } $.simulateMouseClick = function (elem) { // eslint-disable-line func-names let $elem = $(elem) let clientCoords = { clientX: $elem.offset().left + $elem.outerWidth() / 2, clientY: $elem.offset().top + $elem.outerHeight() / 2, } $elem.simulate('mousemove', clientCoords) $elem.simulate('mousedown', clientCoords) $elem.simulate('mouseup', clientCoords) $elem.simulate('click', clientCoords) } $.simulateTouchClick = function (elem) { // eslint-disable-line func-names let $elem = $(elem) let clientCoords = { clientX: $elem.offset().left + $elem.outerWidth() / 2, clientY: $elem.offset().top + $elem.outerHeight() / 2, } $elem.simulate('touchstart', clientCoords) $elem.simulate('touchend', clientCoords) $elem.simulate('mousemove', clientCoords) $elem.simulate('mousedown', clientCoords) $elem.simulate('mouseup', clientCoords) $elem.simulate('click', clientCoords) } /* Drag-n-drop ---------------------------------------------------------------------------------------------------------------------- */ let DEBUG_DELAY = 500 let DEBUG_MIN_DURATION = 2000 let DEBUG_MIN_MOVES = 100 let DRAG_DEFAULTS = { point: null, // the start point localPoint: { left: '50%', top: '50%' }, end: null, // can be a point or an el localEndPoint: { left: '50%', top: '50%' }, dx: 0, dy: 0, moves: 5, duration: 100, // ms } let dragStackCnt = 0 $.simulate.prototype.simulateDrag = function () { // eslint-disable-line func-names let options = $.extend({}, DRAG_DEFAULTS, this.options) let targetNode = this.target // raw DOM node let targetEl = $(targetNode) // jq object let dx = options.dx let dy = options.dy let duration = options.duration let moves = options.moves let startPoint let endEl let endPoint let localPoint let offset // compute start point if (options.point) { startPoint = options.point } else { localPoint = normalizeElPoint(options.localPoint, targetEl) offset = targetEl.offset() startPoint = { left: offset.left + localPoint.left, top: offset.top + localPoint.top, } } // compute end point if (options.end) { if (isPoint(options.end)) { endPoint = options.end } else { // assume options.end is an element endEl = $(options.end) localPoint = normalizeElPoint(options.localEndPoint, endEl) offset = endEl.offset() endPoint = { left: offset.left + localPoint.left, top: offset.top + localPoint.top, } } } if (endPoint) { dx = endPoint.left - startPoint.left dy = endPoint.top - startPoint.top } moves = Math.max(moves, options.debug ? DEBUG_MIN_MOVES : 1) duration = Math.max(duration, options.debug ? DEBUG_MIN_DURATION : 10) simulateDrag( this, targetNode, startPoint, dx, dy, moves, duration, options, ) } function simulateDrag(self, targetNode, startPoint, dx, dy, moveCnt, duration, options) { let debug = options.debug let isTouch = options.isTouch let docNode = targetNode.ownerDocument let docEl = $(docNode) let waitTime = duration / moveCnt let moveIndex = 0 let clientCoords let intervalId let dotEl let dragId if (debug) { dotEl = $('