UNPKG

@duoduo-oba/ng-devui

Version:

DevUI components based on Angular

1,403 lines (1,400 loc) 187 kB
import { ElementRef, Injectable, NgZone, EventEmitter, Component, ChangeDetectorRef, Directive, ComponentFactoryResolver, Input, Renderer2, Optional, Self, HostBinding, Output, HostListener, ContentChildren, NgModule } from '@angular/core'; import { Subject, Observable, Subscription, fromEvent, merge } from 'rxjs'; import { OverlayContainerRef } from '@duoduo-oba/ng-devui/overlay-container'; import { filter, distinctUntilChanged, tap, throttleTime } from 'rxjs/operators'; import { CommonModule } from '@angular/common'; /** * @fileoverview added by tsickle * Generated from: touch-support/dragdrop-touch.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** * 2020.03.23-Modified from https://github.com/Bernardo-Castilho/dragdroptouch, license: MIT,reason:Converting .js file to .ts file * Huawei Technologies Co.,Ltd.<devcloudmobile\@huawei.com> */ class DragDropTouch { constructor() { this.lastClick = 0; // ** event handlers this.touchstart = (/** * @param {?} e * @return {?} */ (e) => { if (this.shouldHandle(e)) { // raise double-click and prevent zooming if (Date.now() - this.lastClick < DragDropTouch.DBLCLICK) { if (this.dispatchEvent(e, 'dblclick', e.target)) { e.preventDefault(); this.reset(); return; } } // clear all variables this.reset(); // get nearest draggable element /** @type {?} */ const src = this.closestDraggable(e.target); if (src) { this.dragSource = src; this.ptDown = this.getPoint(e); this.lastTouch = e; if (DragDropTouch.IS_PRESS_HOLD_MODE) { this.pressHoldInterval = setTimeout((/** * @return {?} */ () => { this.bindTouchmoveTouchend(e); this.isDragEnabled = true; this.touchmove(e); }), DragDropTouch.PRESS_HOLD_AWAIT); } else { e.preventDefault(); this.bindTouchmoveTouchend(e); } } } }); this.touchmoveOnDocument = (/** * @param {?} e * @return {?} */ (e) => { if (this.shouldCancelPressHoldMove(e)) { this.reset(); return; } }); this.touchmove = (/** * @param {?} e * @return {?} */ (e) => { if (this.shouldCancelPressHoldMove(e)) { this.reset(); return; } if (this.shouldHandleMove(e) || this.shouldHandlePressHoldMove(e)) { /** @type {?} */ const target = this.getTarget(e); // start dragging if (this.dragSource && !this.img && this.shouldStartDragging(e)) { this.dispatchEvent(e, 'dragstart', this.dragSource); this.createImage(e); } // continue dragging if (this.img) { this.clearDragoverInterval(); this.lastTouch = e; e.preventDefault(); // prevent scrolling if (target !== this.lastTarget) { // according to drag drop implementation of the browser, dragenterB is supposed to fired before dragleaveA this.dispatchEvent(e, 'dragenter', target); this.dispatchEvent(this.lastTouch, 'dragleave', this.lastTarget); this.lastTarget = target; } this.moveImage(e); this.isDropZone = this.dispatchEvent(e, 'dragover', target); // should continue dispatch dragover event when touch position stay still this.setDragoverInterval(e); } } }); this.touchendOnDocument = (/** * @param {?} e * @return {?} */ (e) => { if (this.shouldHandle(e)) { if (!this.img) { this.dragSource = null; this.lastClick = Date.now(); } // finish dragging this.destroyImage(); if (this.dragSource) { this.reset(); } } }); this.touchend = (/** * @param {?} e * @return {?} */ (e) => { if (this.shouldHandle(e)) { // user clicked the element but didn't drag, so clear the source and simulate a click if (!this.img) { this.dragSource = null; // browser will dispatch click event after trigger touchend, since touchstart didn't preventDefault // this._dispatchEvent(this._lastTouch, 'click', e.target); this.lastClick = Date.now(); } // finish dragging this.destroyImage(); if (this.dragSource) { if (e.type.indexOf('cancel') < 0 && this.isDropZone) { this.dispatchEvent(this.lastTouch, 'drop', this.lastTarget); } this.dispatchEvent(this.lastTouch, 'dragend', this.dragSource); this.reset(); } } }); // enforce singleton pattern if (DragDropTouch.instance) { throw new Error('DragDropTouch instance already created.'); } // detect passive event support // https://github.com/Modernizr/Modernizr/issues/1894 /** @type {?} */ let supportsPassive = false; document.addEventListener('test', (/** * @return {?} */ () => { }), { /** * @return {?} */ get passive() { supportsPassive = true; return true; } }); // listen to touch events if (DragDropTouch.isTouchDevice()) { // 能响应触摸事件 /** @type {?} */ const d = document; /** @type {?} */ const ts = this.touchstart; /** @type {?} */ const tmod = this.touchmoveOnDocument; /** @type {?} */ const teod = this.touchendOnDocument; /** @type {?} */ const opt = supportsPassive ? { passive: false, capture: false } : false; /** @type {?} */ const optPassive = supportsPassive ? { passive: true } : false; d.addEventListener('touchstart', ts, opt); d.addEventListener('touchmove', tmod, optPassive); d.addEventListener('touchend', teod); d.addEventListener('touchcancel', teod); this.touchmoveListener = this.touchmove; this.touchendListener = this.touchend; this.listenerOpt = opt; } } /** * Gets a reference to the \@see:DragDropTouch singleton. * @return {?} */ static getInstance() { if (!DragDropTouch.instance) { DragDropTouch.instance = new DragDropTouch(); } return DragDropTouch.instance; } /** * @return {?} */ static isTouchDevice() { /** @type {?} */ const d = document; /** @type {?} */ const w = window; /** @type {?} */ let bool; if ('ontouchstart' in d // normal mobile device || 'ontouchstart' in w || navigator.maxTouchPoints > 0 || navigator.msMaxTouchPoints > 0 || window['DocumentTouch'] && document instanceof window['DocumentTouch']) { bool = true; } else { /** @type {?} */ const fakeBody = document.createElement('fakebody'); fakeBody.innerHTML += ` <style> @media (touch-enabled),(-webkit-touch-enabled),(-moz-touch-enabled),(-o-touch-enabled){ #touch_test { top: 42px; position: absolute; } } </style>`; document.documentElement.appendChild(fakeBody); /** @type {?} */ const touchTestNode = document.createElement('div'); touchTestNode.id = 'touch_test'; fakeBody.appendChild(touchTestNode); bool = touchTestNode.offsetTop === 42; fakeBody.parentElement.removeChild(fakeBody); } return bool; } // ** event listener binding /** * @param {?} e * @return {?} */ bindTouchmoveTouchend(e) { this.touchTarget = e.target; e.target.addEventListener('touchmove', this.touchmoveListener, this.listenerOpt); e.target.addEventListener('touchend', this.touchendListener); e.target.addEventListener('touchcancel', this.touchendListener); } /** * @return {?} */ removeTouchmoveTouchend() { if (this.touchTarget) { this.touchTarget.removeEventListener('touchmove', this.touchmoveListener); this.touchTarget.removeEventListener('touchend', this.touchendListener); this.touchTarget.removeEventListener('touchcancel', this.touchendListener); this.touchTarget = undefined; } } // ** utilities // ignore events that have been handled or that involve more than one touch /** * @param {?} e * @return {?} */ shouldHandle(e) { return e && !e.defaultPrevented && e.touches && e.touches.length < 2; } // use regular condition outside of press & hold mode /** * @param {?} e * @return {?} */ shouldHandleMove(e) { return !DragDropTouch.IS_PRESS_HOLD_MODE && this.shouldHandle(e); } // allow to handle moves that involve many touches for press & hold /** * @param {?} e * @return {?} */ shouldHandlePressHoldMove(e) { return DragDropTouch.IS_PRESS_HOLD_MODE && this.isDragEnabled && e && e.touches && e.touches.length; } // reset data if user drags without pressing & holding /** * @param {?} e * @return {?} */ shouldCancelPressHoldMove(e) { return DragDropTouch.IS_PRESS_HOLD_MODE && !this.isDragEnabled && this.getDelta(e) > DragDropTouch.PRESS_HOLD_MARGIN; } // start dragging when mouseover element matches drag handler selector and specified delta is detected /** * @param {?} e * @return {?} */ shouldStartDragging(e) { /** @type {?} */ const dragHandleSelector = this.getDragHandle(); // start dragging when mouseover element matches drag handler selector if (dragHandleSelector && !this.matchSelector(e.target, dragHandleSelector)) { return false; } // start dragging when specified delta is detected /** @type {?} */ const delta = this.getDelta(e); return delta > DragDropTouch.THRESHOLD || (DragDropTouch.IS_PRESS_HOLD_MODE && delta >= DragDropTouch.PRESS_HOLD_THRESHOLD); } // find drag handler selector for dragstart only with partial element /** * @return {?} */ getDragHandle() { if (this.dragSource) { return this.dragSource.getAttribute(DragDropTouch.DRAG_HANDLE_ATTR) || ''; } return ''; } // test if element matches selector /** * @param {?} element * @param {?} selector * @return {?} */ matchSelector(element, selector) { if (selector) { /** @type {?} */ const proto = Element.prototype; /** @type {?} */ const func = proto['matches'] || proto['matchesSelector'] || proto['mozMatchesSelector'] || proto['msMatchesSelector'] || proto['oMatchesSelector'] || proto['webkitMatchesSelector'] || (/** * @param {?} s * @return {?} */ function (s) { /** @type {?} */ const matches = (this.document || this.ownerDocument).querySelectorAll(s); /** @type {?} */ let i = matches.length; while (--i >= 0 && matches.item(i) !== this) { // do nothing } return i > -1; }); return func.call(element, selector); } return true; } // clear all members /** * @return {?} */ reset() { this.removeTouchmoveTouchend(); this.destroyImage(); this.dragSource = null; this.lastTouch = null; this.lastTarget = null; this.ptDown = null; this.isDragEnabled = false; this.isDropZone = false; this.dataTransfer = new DragDropTouch.DataTransfer(); clearInterval(this.pressHoldInterval); this.clearDragoverInterval(); } // get point for a touch event /** * @param {?} e * @param {?=} page * @return {?} */ getPoint(e, page) { if (e && e.touches) { e = e.touches[0]; } return { x: page ? e.pageX : e.clientX, y: page ? e.pageY : e.clientY }; } // get distance between the current touch event and the first one /** * @param {?} e * @return {?} */ getDelta(e) { if (DragDropTouch.IS_PRESS_HOLD_MODE && !this.ptDown) { return 0; } /** @type {?} */ const p = this.getPoint(e); return Math.abs(p.x - this.ptDown.x) + Math.abs(p.y - this.ptDown.y); } // get the element at a given touch event /** * @param {?} e * @return {?} */ getTarget(e) { /** @type {?} */ const pt = this.getPoint(e); /** @type {?} */ let el = document.elementFromPoint(pt.x, pt.y); while (el && getComputedStyle(el).pointerEvents === 'none') { el = el.parentElement; } return (/** @type {?} */ (el)); } // create drag image from source element /** * @param {?} e * @return {?} */ createImage(e) { // just in case... if (this.img) { this.destroyImage(); } // create drag image from custom element or drag source /** @type {?} */ const src = this.imgCustom || this.dragSource; this.img = src.cloneNode(true); this.copyStyle(src, this.img); this.img.style.top = this.img.style.left = '-9999px'; // if creating from drag source, apply offset and opacity if (!this.imgCustom) { /** @type {?} */ const rc = src.getBoundingClientRect(); /** @type {?} */ const pt = this.getPoint(e); this.imgOffset = { x: pt.x - rc.left, y: pt.y - rc.top }; this.img.style.opacity = DragDropTouch.OPACITY.toString(); } // add image to document this.moveImage(e); document.body.appendChild(this.img); } // dispose of drag image element /** * @return {?} */ destroyImage() { if (this.img && this.img.parentElement) { this.img.parentElement.removeChild(this.img); } this.img = null; this.imgCustom = null; } // move the drag image element /** * @param {?} e * @return {?} */ moveImage(e) { requestAnimationFrame((/** * @return {?} */ () => { if (this.img) { /** @type {?} */ const pt = this.getPoint(e, true); /** @type {?} */ const s = this.img.style; s.position = 'absolute'; s.pointerEvents = 'none'; s.zIndex = '999999'; s.left = Math.round(pt.x - this.imgOffset.x) + 'px'; s.top = Math.round(pt.y - this.imgOffset.y) + 'px'; } })); } // copy properties from an object to another /** * @param {?} dst * @param {?} src * @param {?} props * @return {?} */ copyProps(dst, src, props) { for (let i = 0; i < props.length; i++) { /** @type {?} */ const p = props[i]; dst[p] = src[p]; } } // copy styles/attributes from drag source to drag image element /** * @param {?} src * @param {?} dst * @return {?} */ copyStyle(src, dst) { // remove potentially troublesome attributes DragDropTouch.rmvAttrs.forEach((/** * @param {?} att * @return {?} */ function (att) { dst.removeAttribute(att); })); // copy canvas content if (src instanceof HTMLCanvasElement) { /** @type {?} */ const canSrc = src; /** @type {?} */ const canDst = dst; canDst.width = canSrc.width; canDst.height = canSrc.height; canDst.getContext('2d').drawImage(canSrc, 0, 0); } // copy canvas content for nested canvas element /** @type {?} */ const srcCanvases = src.querySelectorAll('canvas'); if (srcCanvases.length > 0) { /** @type {?} */ const dstCanvases = dst.querySelectorAll('canvas'); for (let i = 0; i < dstCanvases.length; i++) { /** @type {?} */ const cSrc = srcCanvases[i]; /** @type {?} */ const cDst = dstCanvases[i]; cDst.getContext('2d').drawImage(cSrc, 0, 0); } } // copy style (without transitions) /** @type {?} */ const cs = getComputedStyle(src); for (let i = 0; i < cs.length; i++) { /** @type {?} */ const key = cs[i]; if (key.indexOf('transition') < 0) { dst.style[key] = cs[key]; } } dst.style.pointerEvents = 'none'; // and repeat for all children for (let i = 0; i < src.children.length; i++) { this.copyStyle(src.children[i], dst.children[i]); } } // synthesize and dispatch an event // returns true if the event has been handled (e.preventDefault == true) /** * @param {?} e * @param {?} type * @param {?} target * @return {?} */ dispatchEvent(e, type, target) { if (e && target) { /** @type {?} */ const evt = document.createEvent('Event'); /** @type {?} */ const t = e.touches ? e.touches[0] : e; evt.initEvent(type, true, true); /** @type {?} */ const obj = { button: 0, which: 0, buttons: 1, dataTransfer: this.dataTransfer }; this.copyProps(evt, e, DragDropTouch.kbdProps); this.copyProps(evt, t, DragDropTouch.ptProps); this.copyProps(evt, { fromTouch: true }, ['fromTouch']); // mark as from touch event this.copyProps(evt, obj, Object.keys(obj)); target.dispatchEvent(evt); return evt.defaultPrevented; } return false; } // gets an element's closest draggable ancestor /** * @param {?} e * @return {?} */ closestDraggable(e) { for (; e; e = e.parentElement) { if (e.hasAttribute('draggable') && e.draggable) { return e; } } return null; } // repeat dispatch dragover event when touch point stay still /** * @param {?} e * @return {?} */ setDragoverInterval(e) { this.dragoverTimer = setInterval((/** * @return {?} */ () => { /** @type {?} */ const target = this.getTarget(e); if (target !== this.lastTarget) { this.dispatchEvent(e, 'dragenter', target); this.dispatchEvent(e, 'dragleave', this.lastTarget); this.lastTarget = target; } this.isDropZone = this.dispatchEvent(e, 'dragover', target); }), DragDropTouch.DRAG_OVER_TIME); } /** * @return {?} */ clearDragoverInterval() { if (this.dragoverTimer) { clearInterval(this.dragoverTimer); this.dragoverTimer = undefined; } } } DragDropTouch.THRESHOLD = 5; // pixels to move before drag starts // pixels to move before drag starts DragDropTouch.OPACITY = 0.5; // drag image opacity // drag image opacity DragDropTouch.DBLCLICK = 500; // max ms between clicks in a double click // max ms between clicks in a double click DragDropTouch.DRAG_OVER_TIME = 300; // interval ms when drag over // interval ms when drag over DragDropTouch.CTX_MENU = 900; // ms to hold before raising 'contextmenu' event // ms to hold before raising 'contextmenu' event DragDropTouch.IS_PRESS_HOLD_MODE = true; // decides of press & hold mode presence // decides of press & hold mode presence DragDropTouch.PRESS_HOLD_AWAIT = 400; // ms to wait before press & hold is detected // ms to wait before press & hold is detected DragDropTouch.PRESS_HOLD_MARGIN = 25; // pixels that finger might shiver while pressing // pixels that finger might shiver while pressing DragDropTouch.PRESS_HOLD_THRESHOLD = 0; // pixels to move before drag starts // pixels to move before drag starts DragDropTouch.DRAG_HANDLE_ATTR = 'data-drag-handle-selector'; DragDropTouch.rmvAttrs = 'id,class,style,draggable'.split(','); DragDropTouch.kbdProps = 'altKey,ctrlKey,metaKey,shiftKey'.split(','); DragDropTouch.ptProps = 'pageX,pageY,clientX,clientY,screenX,screenY'.split(','); DragDropTouch.instance = null; if (false) { /** @type {?} */ DragDropTouch.THRESHOLD; /** @type {?} */ DragDropTouch.OPACITY; /** @type {?} */ DragDropTouch.DBLCLICK; /** @type {?} */ DragDropTouch.DRAG_OVER_TIME; /** @type {?} */ DragDropTouch.CTX_MENU; /** @type {?} */ DragDropTouch.IS_PRESS_HOLD_MODE; /** @type {?} */ DragDropTouch.PRESS_HOLD_AWAIT; /** @type {?} */ DragDropTouch.PRESS_HOLD_MARGIN; /** @type {?} */ DragDropTouch.PRESS_HOLD_THRESHOLD; /** @type {?} */ DragDropTouch.DRAG_HANDLE_ATTR; /** @type {?} */ DragDropTouch.rmvAttrs; /** @type {?} */ DragDropTouch.kbdProps; /** @type {?} */ DragDropTouch.ptProps; /** * @type {?} * @private */ DragDropTouch.instance; /** @type {?} */ DragDropTouch.prototype.dataTransfer; /** @type {?} */ DragDropTouch.prototype.lastClick; /** @type {?} */ DragDropTouch.prototype.lastTouch; /** @type {?} */ DragDropTouch.prototype.lastTarget; /** @type {?} */ DragDropTouch.prototype.dragSource; /** @type {?} */ DragDropTouch.prototype.ptDown; /** @type {?} */ DragDropTouch.prototype.isDragEnabled; /** @type {?} */ DragDropTouch.prototype.isDropZone; /** @type {?} */ DragDropTouch.prototype.pressHoldInterval; /** @type {?} */ DragDropTouch.prototype.img; /** @type {?} */ DragDropTouch.prototype.imgCustom; /** @type {?} */ DragDropTouch.prototype.imgOffset; /** @type {?} */ DragDropTouch.prototype.dragoverTimer; /** @type {?} */ DragDropTouch.prototype.touchTarget; /** @type {?} */ DragDropTouch.prototype.touchmoveListener; /** @type {?} */ DragDropTouch.prototype.touchendListener; /** @type {?} */ DragDropTouch.prototype.listenerOpt; /** @type {?} */ DragDropTouch.prototype.touchstart; /** @type {?} */ DragDropTouch.prototype.touchmoveOnDocument; /** @type {?} */ DragDropTouch.prototype.touchmove; /** @type {?} */ DragDropTouch.prototype.touchendOnDocument; /** @type {?} */ DragDropTouch.prototype.touchend; } (function (DragDropTouch) { /** * Object used to hold the data that is being dragged during drag and drop operations. * * It may hold one or more data items of different types. For more information about * drag and drop operations and data transfer objects, see * <a href="https://developer.mozilla.org/en-US/docs/Web/API/DataTransfer">HTML Drag and Drop API</a>. * * This object is created automatically by the \@see:DragDropTouch singleton and is * accessible through the \@see:dataTransfer property of all drag events. */ class DataTransfer { constructor() { this._dropEffect = 'move'; this._effectAllowed = 'all'; this._data = {}; } /** * @return {?} */ get dropEffect() { return this._dropEffect; } /** * @param {?} value * @return {?} */ set dropEffect(value) { this._dropEffect = value; } /** * @return {?} */ get effectAllowed() { return this._effectAllowed; } /** * @param {?} value * @return {?} */ set effectAllowed(value) { this._effectAllowed = value; } /** * @return {?} */ get types() { return Object.keys(this._data); } /** * Removes the data associated with a given type. * * The type argument is optional. If the type is empty or not specified, the data * associated with all types is removed. If data for the specified type does not exist, * or the data transfer contains no data, this method will have no effect. * * @param {?} type Type of data to remove. * @return {?} */ clearData(type) { if (type !== null) { delete this._data[type]; } else { this._data = null; } } /** * Retrieves the data for a given type, or an empty string if data for that type does * not exist or the data transfer contains no data. * * @param {?} type Type of data to retrieve. * @return {?} */ getData(type) { return this._data[type] || ''; } /** * Set the data for a given type. * * For a list of recommended drag types, please see * https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Recommended_Drag_Types. * * @param {?} type Type of data to add. * @param {?} value Data to add. * @return {?} */ setData(type, value) { this._data[type] = value; } /** * Set the image to be used for dragging if a custom one is desired. * * @param {?} img An image element to use as the drag feedback image. * @param {?} offsetX The horizontal offset within the image. * @param {?} offsetY The vertical offset within the image. * @return {?} */ setDragImage(img, offsetX, offsetY) { /** @type {?} */ const ddt = DragDropTouch.getInstance(); ddt.imgCustom = img; ddt.imgOffset = { x: offsetX, y: offsetY }; } } DragDropTouch.DataTransfer = DataTransfer; if (false) { /** @type {?} */ DataTransfer.prototype.files; /** @type {?} */ DataTransfer.prototype.items; /** * @type {?} * @private */ DataTransfer.prototype._data; /** * Gets or sets the type of drag-and-drop operation currently selected. * The value must be 'none', 'copy', 'link', or 'move'. * @type {?} * @private */ DataTransfer.prototype._dropEffect; /** * Gets or sets the types of operations that are possible. * Must be one of 'none', 'copy', 'copyLink', 'copyMove', 'link', * 'linkMove', 'move', 'all' or 'uninitialized'. * @type {?} * @private */ DataTransfer.prototype._effectAllowed; /** * Gets an array of strings giving the formats that were set in the \@see:dragstart event. * @type {?} * @private */ DataTransfer.prototype._types; } })(DragDropTouch || (DragDropTouch = {})); /** * @fileoverview added by tsickle * Generated from: shared/utils.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ class Utils { /** * Polyfill for element.matches. * See: https://developer.mozilla.org/en/docs/Web/API/Element/matches#Polyfill * element * @param {?} element * @param {?} selectorName * @return {?} */ static matches(element, selectorName) { /** @type {?} */ const proto = Element.prototype; /** @type {?} */ const func = proto['matches'] || proto.matchesSelector || proto.mozMatchesSelector || proto.msMatchesSelector || proto.oMatchesSelector || proto.webkitMatchesSelector || (/** * @param {?} s * @return {?} */ function (s) { /** @type {?} */ const matches = (this.document || this.ownerDocument).querySelectorAll(s); /** @type {?} */ let i = matches.length; while (--i >= 0 && matches.item(i) !== this) { // do nothing } return i > -1; }); return func.call(element, selectorName); } /** * Applies the specified css class on nativeElement * elementRef * className * @param {?} elementRef * @param {?} className * @return {?} */ static addClass(elementRef, className) { if (className === undefined) { return; } /** @type {?} */ const e = this.getElementWithValidClassList(elementRef); if (e) { e.classList.add(className); } } /** * Removes the specified class from nativeElement * elementRef * className * @param {?} elementRef * @param {?} className * @return {?} */ static removeClass(elementRef, className) { if (className === undefined) { return; } /** @type {?} */ const e = this.getElementWithValidClassList(elementRef); if (e) { e.classList.remove(className); } } /** * Gets element with valid classList * * elementRef * @private * @param {?} elementRef * @return {?} ElementRef | null */ static getElementWithValidClassList(elementRef) { /** @type {?} */ const e = elementRef instanceof ElementRef ? elementRef.nativeElement : elementRef; if (e.classList !== undefined && e.classList !== null) { return e; } return null; } /** * @param {?} args * @param {?=} slice * @param {?=} sliceEnd * @return {?} */ static slice(args, slice, sliceEnd) { /** @type {?} */ const ret = []; /** @type {?} */ let len = args.length; if (0 === len) { return ret; } /** @type {?} */ const start = slice < 0 ? Math.max(0, slice + len) : slice || 0; if (sliceEnd !== undefined) { len = sliceEnd < 0 ? sliceEnd + len : sliceEnd; } while (len-- > start) { ret[len - start] = args[len]; } return ret; } // 动态添加styles /** * @param {?} el * @param {?} styles * @return {?} */ static addElStyles(el, styles) { if (styles instanceof Object) { for (const s in styles) { if (styles.hasOwnProperty(s)) { if (Array.isArray(styles[s])) { // 用于支持兼容渐退 styles[s].forEach((/** * @param {?} val * @return {?} */ val => { el.style[s] = val; })); } else { el.style[s] = styles[s]; } } } } } } /** * @fileoverview added by tsickle * Generated from: services/drag-drop.service.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ class DragDropService { /** * @param {?} ngZone */ constructor(ngZone) { this.ngZone = ngZone; this.dropTargets = []; this.dropEvent = new Subject(); this.dragEndEvent = new Subject(); this.dragStartEvent = new Subject(); this.subscription = Observable.create().subscribe(); this.dragEmptyImage = new Image(); this.dragItemParentName = ''; this.dragItemChildrenName = ''; this.intersectionObserver = null; /*协同拖拽需要 */ this.dragElShowHideEvent = new Subject(); this.followMouse4CloneNode = (/** * @param {?} event * @return {?} */ (event) => { const { offsetLeft, offsetTop } = this.dragOffset; const { clientX, clientY } = event; requestAnimationFrame((/** * @return {?} */ () => { if (!this.dragCloneNode) { return; } this.dragCloneNode.style.left = clientX - offsetLeft + 'px'; this.dragCloneNode.style.top = clientY - offsetTop + 'px'; })); }); this.touchInstance = DragDropTouch.getInstance(); // service not support OnInit, only support OnDestroy, so write in constructor // tslint:disable-next-line: max-line-length this.dragEmptyImage.src = 'data:image/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGBgAAAABQABh6FO1AAAAABJRU5ErkJggg=='; // safari的img必须要有src } /** * @return {?} */ newSubscription() { this.subscription.unsubscribe(); return this.subscription = Observable.create().subscribe(); } /** * @return {?} */ enableDraggedCloneNodeFollowMouse() { if (!this.dragCloneNode) { this.dragItemContainer = this.draggedEl.parentElement; if (this.dragPreviewDirective && this.dragPreviewDirective.dragPreviewTemplate) { this.dragPreviewDirective.createPreview(); this.dragCloneNode = this.dragPreviewDirective.getPreviewElement(); this.dragItemContainer = document.body; } else { this.dragCloneNode = this.draggedEl.cloneNode(true); } this.dragCloneNode.style.margin = '0'; if (this.dragFollowOptions && this.dragFollowOptions.appendToBody) { this.dragItemContainer = document.body; this.copyStyle(this.draggedEl, this.dragCloneNode); } if (this.dragItemChildrenName !== '') { /** @type {?} */ const parentElement = this.dragItemParentName === '' ? this.dragCloneNode : document.querySelector(this.dragItemParentName); /** @type {?} */ const dragItemChildren = parentElement.querySelectorAll(this.dragItemChildrenName); this.interceptChildNode(parentElement, dragItemChildren); } // 拷贝canvas的内容 /** @type {?} */ const originCanvasArr = this.draggedEl.querySelectorAll('canvas'); /** @type {?} */ const targetCanvasArr = this.dragCloneNode.querySelectorAll('canvas'); [].forEach.call(targetCanvasArr, (/** * @param {?} canvas * @param {?} index * @return {?} */ (canvas, index) => { canvas.getContext('2d').drawImage(originCanvasArr[index], 0, 0); })); this.ngZone.runOutsideAngular((/** * @return {?} */ () => { document.addEventListener('dragover', this.followMouse4CloneNode, { capture: true, passive: true }); })); this.dragCloneNode.style.width = this.dragOffset.width + 'px'; this.dragCloneNode.style.height = this.dragOffset.height + 'px'; if (!(this.dragPreviewDirective && this.dragPreviewDirective.dragPreviewTemplate && this.dragPreviewDirective.dragPreviewOptions && this.dragPreviewDirective.dragPreviewOptions.skipBatchPreview)) { // 批量拖拽样式 if (this.batchDragging && this.batchDragData && this.batchDragData.length > 1) { // 创建一个节点容器 /** @type {?} */ const node = document.createElement('div'); node.appendChild(this.dragCloneNode); node.classList.add('batch-dragged-node'); /* 计数样式定位 */ if (this.batchDragStyle && this.batchDragStyle.length && this.batchDragStyle.indexOf('badge') > -1) { /** @type {?} */ const badge = document.createElement('div'); badge.innerText = this.batchDragData.length + ''; badge.classList.add('batch-dragged-node-count'); node.style.position = 'relative'; /** @type {?} */ const style = { position: 'absolute', right: '5px', top: '-12px', height: '24px', width: '24px', borderRadius: '12px', fontSize: '14px', lineHeight: '24px', textAlign: 'center', color: '#fff', background: ['#5170ff', 'var(--brand-1, #5170ff)'] }; Utils.addElStyles(badge, style); node.appendChild(badge); } /* 层叠感样式定位 */ if (this.batchDragStyle && this.batchDragStyle.length && this.batchDragStyle.indexOf('stack') > -1) { /** @type {?} */ let stack = 2; if (this.batchDragData.length === 2) { stack = 1; } for (let i = 0; i < stack; i++) { /** @type {?} */ const stackNode = this.dragCloneNode.cloneNode(false); /** @type {?} */ const stackStyle = { position: 'absolute', left: -5 * (i + 1) + 'px', top: -5 * (i + 1) + 'px', zIndex: -(i + 1) + '', width: this.dragOffset.width + 'px', height: this.dragOffset.height + 'px', background: '#fff', border: ['1px solid #5170ff', '1px solid var(--brand-1, #5170ff)'] }; Utils.addElStyles(stackNode, stackStyle); node.appendChild(stackNode); } } this.dragCloneNode = node; } } this.dragCloneNode.classList.add('drag-clone-node'); if (!(this.dragPreviewDirective && this.dragPreviewDirective.dragPreviewTemplate)) { this.dragCloneNode.style.width = this.dragOffset.width + 'px'; this.dragCloneNode.style.height = this.dragOffset.height + 'px'; } this.dragCloneNode.style.position = 'fixed'; this.dragCloneNode.style.zIndex = '1090'; this.dragCloneNode.style.pointerEvents = 'none'; this.dragCloneNode.style.top = this.dragOffset.top + 'px'; this.dragCloneNode.style.left = this.dragOffset.left + 'px'; this.dragCloneNode.style.willChange = 'left, top'; this.dragItemContainer.appendChild(this.dragCloneNode); this.ngZone.runOutsideAngular((/** * @return {?} */ () => { setTimeout((/** * @return {?} */ () => { if (this.draggedEl) { this.draggedEl.style.display = 'none'; this.dragElShowHideEvent.next(false); if (this.dragOriginPlaceholder) { this.dragOriginPlaceholder.style.display = 'block'; } } })); })); } } /** * @return {?} */ disableDraggedCloneNodeFollowMouse() { if (this.dragCloneNode) { document.removeEventListener('dragover', this.followMouse4CloneNode, { capture: true }); this.dragItemContainer.removeChild(this.dragCloneNode); this.draggedEl.style.display = ''; this.dragElShowHideEvent.next(true); } if (this.dragPreviewDirective && this.dragPreviewDirective.dragPreviewTemplate) { this.dragPreviewDirective.destroyPreview(); } this.dragCloneNode = undefined; this.dragItemContainer = undefined; if (this.intersectionObserver) { this.intersectionObserver.disconnect(); } } /** * @param {?} parentNode * @param {?} childNodeList * @return {?} */ interceptChildNode(parentNode, childNodeList) { /** @type {?} */ const interceptOptions = { root: parentNode }; this.intersectionObserver = new IntersectionObserver(this.setChildNodeHide, interceptOptions); [].forEach.call(childNodeList, (/** * @param {?} childNode * @return {?} */ childNode => { this.intersectionObserver.observe(childNode); })); } /** * @param {?} entries * @return {?} */ setChildNodeHide(entries) { entries.forEach((/** * @param {?} element * @return {?} */ element => { const { isIntersecting, target: childNode } = element; if (isIntersecting) { childNode.style.display = 'block'; } else { childNode.style.display = 'none'; } })); } /** * @param {?=} identity * @param {?=} order * @return {?} */ getBatchDragData(identity, order = 'draggedElFirst') { /** @type {?} */ const result = this.batchDragData.map((/** * @param {?} dragData * @return {?} */ dragData => dragData.dragData)); if (typeof order === 'function') { result.sort((/** @type {?} */ (order))); } else if (order === 'draggedElFirst') { /** @type {?} */ let dragData = this.dragData; if (identity) { /** @type {?} */ const realDragData = this.batchDragData.filter((/** * @param {?} dd * @return {?} */ dd => dd.identity === identity)).pop().dragData; dragData = realDragData; } result.splice(result.indexOf(dragData), 1); result.splice(0, 0, dragData); } return result; } /** * usage: * constructor(..., private dragDropService: DragDropService) {} * cleanBatchDragData() { this.dragDropService.cleanBatchDragData(); } * @return {?} */ cleanBatchDragData() { /** @type {?} */ const batchDragData = this.batchDragData; if (this.batchDragData) { this.batchDragData .filter((/** * @param {?} dragData * @return {?} */ dragData => dragData.draggable)) .map((/** * @param {?} dragData * @return {?} */ dragData => dragData.draggable)) .forEach((/** * @param {?} draggable * @return {?} */ draggable => draggable.batchDraggable.dragData = undefined)); this.batchDragData = undefined; this.batchDragGroup = undefined; } return batchDragData; } /** * @param {?} source * @param {?} target * @return {?} */ copyStyle(source, target) { ['id', 'class', 'style', 'draggable'].forEach((/** * @param {?} att * @return {?} */ function (att) { target.removeAttribute(att); })); // copy style (without transitions) /** @type {?} */ const computedStyle = getComputedStyle(source); for (let i = 0; i < computedStyle.length; i++) { /** @type {?} */ const key = computedStyle[i]; if (key.indexOf('transition') < 0) { target.style[key] = computedStyle[key]; } } target.style.pointerEvents = 'none'; // and repeat for all children for (let i = 0; i < source.children.length; i++) { this.copyStyle(source.children[i], target.children[i]); } } } DragDropService.decorators = [ { type: Injectable } ]; /** @nocollapse */ DragDropService.ctorParameters = () => [ { type: NgZone } ]; if (false) { /** @type {?} */ DragDropService.prototype.dragData; /** @type {?} */ DragDropService.prototype.draggedEl; /** @type {?} */ DragDropService.prototype.draggedElIdentity; /** @type {?} */ DragDropService.prototype.batchDragData; /** @type {?} */ DragDropService.prototype.batchDragGroup; /** @type {?} */ DragDropService.prototype.batchDragStyle; /** @type {?} */ DragDropService.prototype.batchDragging; /** @type {?} */ DragDropService.prototype.scope; /** @type {?} */ DragDropService.prototype.dropTargets; /** @type {?} */ DragDropService.prototype.dropEvent; /** @type {?} */ DragDropService.prototype.dragEndEvent; /** @type {?} */ DragDropService.prototype.dragStartEvent; /** @type {?} */ DragDropService.prototype.dropOnItem; /** @type {?} */ DragDropService.prototype.dragFollow; /** @type {?} */ DragDropService.prototype.dragFollowOptions; /** @type {?} */ DragDropService.prototype.dropOnOrigin; /** @type {?} */ DragDropService.prototype.draggedElFollowingMouse; /** @type {?} */ DragDropService.prototype.dragOffset; /** @type {?} */ DragDropService.prototype.subscription; /** @type {?} */ DragDropService.prototy