All files / coral-dragaction/src/scripts DragAction.js

83.71% Statements 257/307
69.05% Branches 145/210
97.44% Functions 38/39
83.71% Lines 257/307

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985                                  2x 2x 2x 2x 2x   2x 2x 2x   2x 2x                           2x                         92x 256x   256x       256x 30x     226x 226x   226x 226x   226x   62x     164x                       382x 8x 374x 354x 20x 20x                       420x   420x   420x       420x                                 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x                       166x   166x 58x 86x 56x 56x     30x       166x       10x       10x                                       2x           542x   542x 2x     540x   540x 540x                 540x 540x 540x   540x       540x   540x           540x   540x   540x   540x   540x   540x 540x 540x 540x 540x 540x 540x 540x   540x                     2x         92x         92x 92x   92x         92x     92x 92x   92x 72x 72x     92x   92x   92x 92x 92x   92x   92x 16x 16x     76x     92x     92x 28x     92x                         20872x 146x 146x 146x   146x   146x 146x 146x       146x 146x         146x   146x 146x 146x         146x                 146x   146x   146x 146x         146x   10x                                     10x 10x             10x 10x                 146x   146x 80x   80x 10x     10x 6x   4x 4x     70x       146x 102x   102x 10x     10x 6x   4x 4x     92x         146x 146x   146x   146x 36x   36x 20x   20x                   36x               110x                                     11354x 90x   90x 90x   90x 72x 72x     90x   90x   90x 14x 14x     76x     90x 20x   20x 20x   20x                     90x                           24x                             24x                                                                                           25x   25x 25x 8x     17x               17x           17x                         90x   90x   90x 12x 12x   12x     78x   78x     90x   90x 6x 6x       90x 50x             90x 2x 2x 2x       90x                   6x                       6x     902x     902x   902x     902x   902x   902x 4x 4x   4x     898x     902x 344x   344x 344x 344x   344x   344x   344x   344x   344x   344x                                 558x   558x   558x   558x   558x   558x   558x                         4x       584x 584x   584x 38x   546x                           296x     994x 994x                         220x     988x         92x     304x                         184x     552x                 562x                                                                                                                                                                                                                      
/**
 * Copyright 2019 Adobe. All rights reserved.
 * This file is licensed to you 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 REPRESENTATIONS
 * OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
 
import Vent from '@adobe/vent';
import {events, validate, transform, commons} from '../../../coral-utils';
 
// Attributes
const DROP_ZONE_ATTRIBUTE = 'coral-dragaction-dropzone';
const HANDLE_ATTRIBUTE = 'coral-dragaction-handle';
const AXIS_ATTRIBUTE = 'coral-dragaction-axis';
const SCROLL_ATTRIBUTE = 'coral-dragaction-scroll';
const CONTAINMENT_ATTRIBUTE = 'coral-dragaction-containment';
 
// Classes
const OPEN_HAND_CLASS = 'u-coral-openHand';
const CLOSE_HAND_CLASS = 'u-coral-closedHand';
const IS_DRAGGING_CLASS = 'is-dragging';
 
// Scroll offset default values
const DEFAULT_SCROLL_OFFSET = 20;
const DEFAULT_SCROLL_BY = 10;
 
/**
 Enumeration for {@link DragAction} axis restrictions.
 
 @typedef {Object} DragActionAxisEnum
 
 @property {String} FREE
 Allows vertically and horizontally dragging.
 @property {String} VERTICAL
 Allows vertically dragging only.
 @property {String} HORIZONTAL
 Allows horizontally dragging only.
 */
const axis = {
  FREE: 'free',
  VERTICAL: 'vertical',
  HORIZONTAL: 'horizontal'
};
 
/**
 @ignore
 @param {HTMLElement} element
 @returns {HTMLElement}
 First parent element with overflow [hidden|scroll|auto]
 */
function getViewContainer(element) {
  while (element) {
    const p = element.parentNode;
I
    if (!p) {
      return p;
    }
    if (p.matches('body')) {
      return p;
    }
 
    const computedStyle = window.getComputedStyle(p);
    const overflow = computedStyle.overflow;
 
    // IE11 can return a value for overflow even if it was not set compared to other browsers so we check for X and Y.
    const overflowX = computedStyle.overflowX;
    const overflowY = computedStyle.overflowY;
 
    if ((overflow === 'hidden' || overflow === 'auto' || overflow === 'scroll') &&
      // @polyfill IE11
      overflow === overflowX && overflow === overflowY) {
      return p;
    }
 
    element = p;
  }
}
 
/**
 @ignore
 @param {String|HTMLElement|NodeList} el
 @returns {Array.<HTMLElement>}
 X and y position whether event was generated by a click or a touch
 */
function transformToArray(el) {
  if (typeof el === 'string') {
    return Array.prototype.slice.call(document.querySelectorAll(el));
  } else if (el instanceof HTMLElement) {
    returEn [el];
  } else if (Object.prototype.toString.call(el) === '[object NodeList]') {
    return Array.prototype.slice.call(el);
  }
}
 
/**
 @ignore
 @param {Object} event
 @returns {Object}
 X and y position whether event was generated by a click or a touch
 */
function getPagePosition(event) {
  let touch = {};
 
  Iif (event.changedTouches && event.changedTouches.length > 0) {
    touch = event.changedTouches[0];
  } else Iif (event.touches && event.touches.length > 0) {
    touch = event.touches[0];
  }
 
  return {
    x: touch.pageX || event.pageX,
    y: touch.pageY || event.pageY
  };
}
 
/**
 @ignore
 @param {HTMLElement} scrollingElement
 element that scrolls the document
 @param {HTMLElement} a
 @param {HTMLElement} b
 @returns {Boolean}
 Whether a is within b bounds
 */
function within(scrollingElement, a, b) {
  const aBoundingClientRect = a.getBoundingClientRect();
  const bBoundingClientRect = b.getBoundingClientRect();
  const documentScrollTop = scrollingElement.scrollTop;
  const documentScrollLeft = scrollingElement.scrollLeft;
 
  const al = aBoundingClientRect.left + documentScrollLeft;
  const ar = al + aBoundingClientRect.width;
  const bl = bBoundingClientRect.left + documentScrollLeft;
  const br = bl + bBoundingClientRect.width;
 
  const at = aBoundingClientRect.top + documentScrollTop;
  const ab = at + aBoundingClientRect.height;
  const bt = bBoundingClientRect.top + documentScrollTop;
  const bb = bt + bBoundingClientRect.height;
 
  return !(bl > ar || br < al || (bt > ab || bb < at));
}
 
/**
 @ignore
 @param {DragAction} dragAction
 Coral.DragAction instance
 @returns {HTMLElement}
 The dropzone that is being hovered by the dragged element or null if none
 */
function isOverDropZone(dragAction) {
  let el = null;
  if (dragAction._dropZones && dragAction._dropZones.length) {
    dragAction._dropZones.some((dropZone) => {
      if (within(dragAction._scrollingElement, dragAction._dragElement, dropZone)) {
        el = dropZone;
        return true;
      }
 
      return false;
    });
  }
 
  return el;
}
 
function getScrollParent(node) {
  Iif (!node) {
    return null;
  }
 
  return node.scrollHeight > node.clientHeight ? node : getScrollParent(node.parentNode);
}
 
/**
 @class Coral.DragAction
 @classdesc This a decorator which adds draggable functionality to elements.
 To define draggable actions on specific elements, handles can be used.
 A handle is given a special attribute :
 - <code>coral-dragaction</code> attribute adds draggable functionality to the corresponding element.
 - <code>coral-dragaction-handle</code> attribute allows dragging only by dragging the specified handle.
 - <code>coral-dragaction-dropzone</code> attribute is used to indicate possible dropzones making it possible
 to build drag-and-drop enabled interfaces in conjunction with <code>DragAction</code> events.
 - <code>coral-dragaction-axis</code> and setting it to either <code>horizontal</code> or <code>vertical</code>,
 it is possible to restrict the drag'n'drop to a single axis.
 - <code>coral-dragaction-scroll</code> attribute will scroll the container when the draggable is moved beyond the viewport.
 - <code>coral-dragaction-containment</code>, the draggable element will be constrained to its container.
 @param {String|HTMLElement} dragElement
 The draggable element.
 */
class DragAction {
  /**
   Takes the {HTMLElement} to be dragged as argument.
 
   @param {HTMLElement} dragElement
   */
  constructor(dragElement) {
    if (!dragElement) {
      throw new Error('Coral.DragAction: dragElement is missing');
    }
 
    let el = null;
    if (dragElement instanceof HTMLElement) {
      el = dragElement;
    } else if (typeof dragElement === 'string') {
    E  el = document.querySelector(dragElement);
      if (!el) {
        throw new Error('Coral.DragAction: dragElement is null');
      }
    }

    this._id = commons.getUID();
    this._dragElementValue = dragElement;
    this._dragElement = el;
 
    // Destroy instance if existing
    if (this._dragElement.dragAction) {
      this._dragElement.dragAction.destroy();
    }
I
    const computedStyle = window.getComputedStyle(this._dragElement);
 
    // Store initial position
    this._initialPosition = {
      position: computedStyle.position,
      left: computedStyle.left,
      top: computedStyle.top
    };
 
    // Prepare Vent
    this._dragEvents = new Vent(this._dragElement);
 
    // Handle options. Binds events to dragElement if no handles defined or found
    this.handle = this._dragElement.getAttribute(HANDLE_ATTRIBUTE);
 
    // DropZone options
    this.dropZone = this._dragElement.getAttribute(DROP_ZONE_ATTRIBUTE);
 
    // Axis horizontal|vertical
    this.axis = this._dragElement.getAttribute(AXIS_ATTRIBUTE);
 
    // Scroll options
    this.scroll = this._dragElement.matches(`[${SCROLL_ATTRIBUTE}]`);
 
    // Restriction to container
    this.containment = this._dragElement.matches(`[${CONTAINMENT_ATTRIBUTE}]`);
 
    this._drag = this._drag.bind(this);
    this._dragEnd = this._dragEnd.bind(this);
    this._dragOnKeyEnd = this._dragOnKeyEnd.bind(this);
 
    events.on(`touchmove.DragAction${this._id}`, this._drag);
    events.on(`mousemove.DragAction${this._id}`, this._drag);
    events.on(`touchend.DragAction${this._id}`, this._dragEnd);
    events.on(`mouseup.DragAction${this._id}`, this._dragEnd);
 
    // Store reference on dragElement
    this._dragElement.dragAction = this;
  }
 
  /**
   The draggable element.
 
   @name dragElement
   @readonly
   @type {String|HTMLElement}
   @htImlattribute coral-dragaction
   */
  get dragElement() {
    return this._dragElementValue;
  }

  /**
   The handle allowing to drag the element.
I
   @name handle
   @type {String|HTMLElement}
   @htmlattribute coral-dragaction-handle
   */
  get handle() {
    return this._handle;
  }
 
  set handle(value) {
    // Set new value
    this._handle = value;
 
    // Unbind events
    this._dragEvents.off('.DragAction');
 
    // Remove classes
    document.body.classList.remove(CLOSE_HAND_CLASS);
    this._dragElement.classList.remove(IS_DRAGGING_CLASS);
    if (this._handles && this._handles.length) {
      this._handles.forEach((handle) => {
        handle._dragEvents.off('.DragAction');
        handle.classList.remove(OPEN_HAND_CLASS);
      });
    } else {
      this._dragElement.classList.remove(OPEN_HAND_CLASS);
    }
 
    if (typeof value === 'string' ||
      value instanceof HTMLElement ||
      Object.prototype.toString.call(value) === '[object NodeList]') {
      this._handles = transformToArray(value);
 
      // Bind events
      if (this._handles && this._handles.length) {
        this._handles.forEach((handle) => {
          handle._dragEvents = handle._dragEvents || new Vent(handle);
          handle._dragEvents.on('mousedown.DragAction', this._dragStart.bind(this));
          handle._dragEvents.on('touchstart.DragAction', this._dragStart.bind(this));
          handle._dragEvents.on('keydown.DragAction', this._dragOnKeyDown.bind(this));
          handle._dragEvents.on('keyup.DragAction', this._dragOnKeyUp.bind(this));
          handle._dragEvents.on('focusout.DragAction', this._dragOnFocusOut.bind(this));
          handle.classList.add(OPEN_HAND_CLASS);
        });
      } else {
        this._dragEvents.on('touchstart.DragAction', this._dragStart.bind(this));
        this._dragEvents.on('mousedown.DragAction', this._dragStart.bind(this));
        this._dragEvents.on('keydown.DragAction', this._dragOnKeyDown.bind(this));
        this._dragEvents.on('keyup.DragAction', this._dragOnKeyUp.bind(this));
        this._dragEvents.on('focusout.DragAction', this._dragOnFocusOut.bind(this));
        this._dragElement.classList.add(OPEN_HAND_CLASS);
      }
    } else {
      // Defaults to the dragElement
      this._handles = [];
      this._dragEvents.on('touchstart.DragAction', this._dragStart.bind(this));
      this._dragEvents.on('mousedown.DragAction', this._dragStart.bind(this));
      this._dragEvents.on('keydown.DragAction', this._dragOnKeyDown.bind(this));
      this._dragEvents.on('keyup.DragAction', this._dragOnKeyUp.bind(this));
      this._dragEvents.on('focusout.DragAction', this._dragOnFocusOut.bind(this));
      this._dragElement.classList.add(OPEN_HAND_CLASS);
    }
  }
 
  /**
   The dropZone to drop the dragged element.
 
   @name dropZone
   @type {String|HTMLElement}
   @htmlattribute coral-dragaction-dropzone
   */
  get dropZone() {
    return this._dropZone;
  }
 
  set dropZone(value) {
    // Set new value
    this._dropZone = value;
    this._dropZoneEntered = false;
 
    if (typeof value === 'string' ||
      value instanceof HTMLElement ||
      Object.prototype.toString.call(value) === '[object NodeList]') {
      this._dropZones = transformToArray(value);
    } else {
      this._dropZones = [];
    }
  }
 
  /**
   The axis to constrain drag movement. See {@link DragActionAxisEnum}.
I
   @name axis
   @type {StrinEg}
   @default DragActionAxisEnum.FREE
   @htmlatEtribute coral-dragaction-axis
   */
  get axis() {
    return this._axis;
  }
 
  set axis(value) {
    value = transform.string(value);
    this._Iaxis = validate.enumeration(axis)(value) && value || axis.FREE;
  }

  /**
   Whether to scroll the container when the draggable element is moved beyond the viewport.

   @name scroll
   @default false
   @type {Boolean}
   @htmlattribute coral-dragaction-scroll
   */
  get scroll() {
    return this._scroll;
  }

  set scroll(value) {
    this._scroll = transform.boolean(value);
  }
 
  get useScrolElParent() {
    return this._useScrollParent || false;
  }

  set useScrollParent(value) {
    this._useScrollParent = transform.boolean(value);
  }
 
  /**E
   Whether to constrain the draggable element to its container viewport.
 
   @name containment
   @default false
   @type {Boolean}
   @htmlattribute coral-dragaction-containment
   */
  get containment() {
    return this._containment;
  }
 
  set containment(value) {
    this._containment = transform.boolean(value);
  }
 
  /** @privaIte */
  _dragStart(event) {
    if (events.isVirtualEvent(event)) {
      return;
    }
 
    // ContainerE
    const viewContainer = getViewContainer(this._dragElement) || document.body;
    this._container = this.useScrollParent ? getScrollParent(this._dragElement) || viewContainer : viewContainer;
 
    // Prevent dragging ghost image
    if (event.target.tagName === 'IMG') {
      event.preventDefault();
    }
 
    // Prevent touchscreen windows to scroll while dragging
    events.on('touchmove.DragAction', (e) => {
      e.preventDefault();
    });I

    document.body._overflow = window.getComputedStyle(document.body).overflow;
    document.body.style.overflow = 'hidden';
 
    if (!this._container.matches('body')) {
      this._containEer._overflow = window.getComputedStyle(this._container).overflow;
      this._container.style.overflow = this.scroll ? 'scroll' : 'hidden';
    }
 
    const pagePosition = getPagePosition(event);
    const dragElementBoundingClientRect = this._dragElement.getBoundingClientRect();
    this._dragPosition = getPagePosition(event);
    this._dragPosition.y -= dragElementBoundingClientRect.top + this._scrollingElement.scrollTop;
    this._dragPosition.x -= dragElementBoundingClientRect.left + this._scrollingElement.scrollLeft;
 
    // Handle classes
    document.body.classList.add(CLOSE_HAND_CLASS);
    if (this._handles && this._handles.length) {
      this._handles.forEach((handle) => {
        handle.classList.remove(OPEN_HAND_CLASS);
      });
    } else {
      this._dragElement.classList.remove(OPEN_HAND_CLASS);
    }
    this._dragElement.classList.add(IS_DRAGGING_CLASS);
 
    // Apply relative position by default
    if (window.getComputedStyle(this._dragElement).position === 'static') {
      this._dragElement.style.position = 'relative';
    }
 
    this._dragEvents.dispatch('coral-dragaction:dragstart', {
      detail: {
        dragElement: this._dragElement,
        pageX: pagePosition.x,
        pageY: pagePosition.y
      }
    });
  }
 
  /** @private */
  _drag(event) {
    if (this._dragElement.classList.contains(IS_DRAGGING_CLASS)) {
      const pagIePosition = getPagePosition(event);

      const documentScrollTop = this._scrollingElement.scrollTop;
      const documentScrollLeft = this._scrollingElement.scrollLeft;
 
      const dragElementBoundingClientRect = this._dragElement.getBoundingClientRect();
      const dragElementHeight = dragElementBoundingClientRect.height;
      const dragElementWidth = dragElementBoundingClientRect.width;
      const dragElementPosition = {
        top: dragElementBoundingClientRect.top + documentScrollTop,
        left: dragElementBoundingClientRect.left + documentScrollLeft
      };
      const dragElementComputedStyle = window.getComputedStyle(this._dragElement);
      const dragElementCSSPosition = {
        top: parseFloat(dragElementComputedStyle.top) || 0,
        left: parseFloat(dragElementComputedStyle.left) || 0
      };
 
      const containerBoundingClientRect = this._container.getBoundingClientRect();
      const containerWidth = containerBoundingClientRect.width;
      const containerHeight = containerBoundingClientRect.height;
      const containerPosition = {
        top: containerBoundingClientRect.top + documentScrollTop,
        left: containerBoundingClientRect.left + documentScrollLeft
      };
 
      this._dragEvents.dispatch('coral-dragaction:drag', {
        detail: {
          dragElement: this._dragElement,
          pageX: pagePosition.x,
          pageY: pagePosition.y
        }
      });
 
      // Remove selection
      if (document.selection) {
        document.selection.empty();
      } else if (window.getSelection) {
        // @polyfill ie
        if (window.getSelection().removeAllRanges) {
          window.getSelection().removeAllRanges();
        }
      }
 
      // Need to scroll ?
      if (Ethis.scroll) {
        // Scroll element is the document
        if (this._container === document.body) {
          // Scroll to the top
          if (dragElementBoundingClientRect.top < DEFAULT_SCROLL_OFFSET) {
            this._scrollingElement.scrollTop = documentScrollTop - DEFAULT_SCROLL_BY;
          }
          // Scroll to the bottom but don't go further than the maximum scroll position of the document
          else if (dragElementBoundingClientRect.top + dragElementBoundingClientRect.height > window.innerHeight - DEFAULT_SCROLL_OFFSET &&
            dragElementPosition.top + dragElementBoundingClientRect.height + DEFAULT_SCROLL_OFFSET < this._scrollingElement.scrollHeight) {
            this._scrollingElement.scrollTop = documentScrollTop + DEFAULT_SCROLL_BY;
          }
 
          // Scroll to the left
          if (dragElementBoundingClientRect.left < DEFAULT_SCROLL_OFFSET) {
            this._scrollingElement.scrollLeft = documentScrollLeft - DEFAULT_SCROLL_BY;
          }
          // Scroll to the right but don't go further than the maximum scroll position of the document
          else if (dragElementBoundingClientRect.left + dragElementBoundingClientRect.width > window.innerWidth - DEFAULT_SCROLL_OFFSET &&
            dragElementPosition.left + dragElementBoundingClientRect.width + DEFAULT_SCROLL_OFFSET < this._scrollingElement.scrollWidth) {
            this._scrollingElement.scrollLeft = documentScrollLeft + DEFAULT_SCROLL_BY;
          }
        }
        // Scroll element is an element other than the document
        else {
          // Scroll to the top
          if (dragElementBoundingClientRect.top - containerBoundingClientRect.top < DEFAULT_SCROLL_OFFSET) {
            this._container.scrollTop = this._container.scrollTop - DEFAULT_SCROLL_BY;
          }
          // Scroll to the bottom but don't go further than the maximum scroll position of the container
          else if (dragElementBoundingClientRect.top - containerBoundingClientRect.top + dragElementBoundingClientRect.height > containerBoundingClientRect.height - DEFAULT_SCROLL_OFFSET &&
            dragElementBoundingClientRect.top - containerBoundingClientRect.top + dragElementBoundingClientRect.height < containerBoundingClientRect.height) {
            this._container.scrollTop = this._container.scrollTop + DEFAULT_SCROLL_BY;
          }

          // Scroll to the left
          if (dragElementBoundingClientRect.left - containerBoundingClientRect.left < DEFAULT_SCROLL_OFFSET) {
            this._container.scrollLeft = this._container.scrollLeft - DEFAULT_SCROLL_BY;
          }
          // Scroll to the bottom but don't go further than the maximum scroll position of the container
          else if (dragElementBoundingClientRect.left - containerBoundingClientRect.left + dragElementBoundingClientRect.width > containerBoundingClientRect.width - DEFAULT_SCROLL_OFFSET &&
            dragElementBoundingClientRect.left - containerBoundingClientRect.left + dragElementBoundingClientRect.width < containerBoundingClientRect.width) {
            this._container.scrollLeft = this._container.scrollLeft + DEFAULT_SCROLL_BY;
          }
        }
      }

      // Set drag element's new position
      const newPosition = {};
 
      if (this.axis !== 'horizontal') {
        const top = pagePosition.y - this._dragPosition.y;

        // Applying container containment for y movements
        if (this.containment) {
          if (top >= containerPosition.top && top + dragElementHeight <= containerPosition.top + containerHeight) {
            newPosition.top = top;
          }
          // put the drag element to the container's top
          else if (pagePosition.y <= containerPosition.top) {
            newPosition.top = containerPosition.top;
          }
          // put the drag element to the container's bottom
          else if (pagePosition.y >= containerPosition.top + containerHeight) {
            newPosition.top = containerPosition.top + containerHeight - dragElementHeight;
          }
        } else {
          newPosition.top = top;
        }
      }
      if (this.axis !== 'vertical') {
        const left = pagePosition.x - this._dragPosition.x;
 
        // Applying container containment for x movements
        if (this.containment) {
          if (left >= containerPosition.left && left + dragElementWidth <= containerPosition.left + containerWidth) {
            newPosition.left = left;
          }
          // put the drag element to the container's left
          else if (pagePosition.x <= containerPosition.left) {
            newPosition.left = containerPosition.left;
          }
          // put the drag element to the container's right
          else if (pagePosition.x >= containerPosition.left + containerWidth) {
            newPosition.left = containerPosition.left + containerWidth - dragElementWidth;
          }
        } else {
          newPosition.left = left;
        }
      }
 
      // Set the new position
      this._dragElement.style.top = `${newPosition.top - dragElementPosition.top + dragElementCSSPosition.top}px`;
      this._dragElement.style.left = `${newPosition.left - dragElementPosition.left + dragElementCSSPosition.left}px`;
 
      // Trigger dropzone related events
      const dropZone = isOverDropZone(this);
      if (dropZone) {
        this._dropElement = dropZone;
        if (!this._dropZoneEntered) {
          this._dropZoneEntered = true;
          this._dragEvents.dispatch('coral-dragaction:dragenter', {
            detail: {
              dragElement: this._dragElement,
              pageX: pagePosition.x,
              pageY: pagePosition.y,
              dropElement: this._dropElement
            }
          });
        }
 
        this._dragEvents.dispatch('coral-dragaction:dragover', {
          detail: {
            dragElement: this._dragElement,
            pageX: pagePosition.x,
            pageY: pagePosition.y,
            dropElement: this._dropElement
          }
        });
      } else if (this._dropZoneEntered) {
        this._dragEvents.dispatch('coral-dragaction:dragleave', {
          detail: {
            dragElement: this._dragElement,
            pageX: pagePosition.x,
            pageY: pagePosition.y,
            dropElement: this._dropElement
          }
        });
        this._dropZoneEntered = false;
      }
    }
  }
 
  /** @private */
  _dragEnd(event) {
    if (this._dragElement.classList.contains(IS_DRAGGING_CLASS)) {
      const pagePosition = getPagePosition(event);
 
      // Restore overflow
      document.body.style.overflow = document.body._overflow;
      document.body._overflow = undefined;
 
      if (!this._container.matches('body')) {
        this._container.style.overflow = this._container._overflow;
        this._container._overflow = undefined;
      }
 
      document.body.classList.remove(CLOSE_HAND_CLASS);
      thIis._dragElement.classList.remove(IS_DRAGGING_CLASS);

      if (this._handles && this._handles.length) {
        this._handles.forEach((handle) => {
          handle.classList.add(OPEN_HAND_CLASS);
        });
      } else {
        this._dragElement.classList.add(OPEN_HAND_CLASS);
      }
 
      if (this._dropZoneEntered) {
        const dropZone = isOverDropZone(this);
        if (dropZone) {
          this._dropElement = dropZone;
          this._dragEvents.dispatch('coral-dragaction:drop', {
            detail: {
              dragElement: this._dragElement,
              pageX: pagePosition.x,
              pageY: pagePosition.y,
              dropElement: this._dropElement
            }
          });
        }
      }
 
      this._dragEvents.dispatch('coral-dragaction:dragend', {
        detail: {
          dragElement: this._dragElement,
          pageX: pagePosition.x,
          pageY: pagePosition.y
        }
      });
    }
  }
 
  /** @private */
  _dragOnKeyDown(event) {
    switch (event.code) {
      case 'Space':
      case 'ArrowDown':
      case 'ArrowUp':
      case 'Enter':
      case 'Escape':
        event.preventDefault();
        break;
    }
  }
 
  /** @private */
  _dragOnKeyUp(event) {
    switch (event.code) {
      case 'Space':
        if (!this.isKeyboardDragging) {
          this._dragEvents.dispatch('coral-dragaction:dragonkeyspace', {
            detail: {
              dragElement: this._dragElement
            }
          });
          this.isKeyboardDragging = true;
        } else {
          this._dragOnKeyEnd();
        }
        break;
      case 'ArrowDown':
        Ethis._dragEvents.dispatch('coral-dragaction:dragoveronkeyarrowdown', {
          detail: {
            dragElement: this._dragElement
          }
        });
        break;
      case 'ArrowUp':
        this._dragEvents.dispatch('coral-dragaction:dragoveronkeyarrowup', {
          detail: {
            dragElement: this._dragElement
          }
        });
        break;
      case 'Enter':
      case 'Escape':
        this._dragOnKeyEnd();
        break;
    }
  }

  /** @private */
  _dragOnFocusOut(event) {
    window.setTimeout(() => {
      if (document.activeElement === event.target) {
        return;
      }
      this._dragOnKeyEnd();
    }, 0);
  }
 
  /** @private */
  _dragOnKeyEnd() {
    this._dragEvents.dispatch('coral-dragaction:dragendonkey', {
      detail: {
        dragElement: this._dragElement
      }
    });
    this.isKeyboardDragging = false;
  }
 
  /**
   Remove draggable actions
 
   @function destroy
   @param {Boolean} restorePosition
   Whether to restore the draggable element to its initial position
   */
  destroy(restorePosition) {
    // Unbind events and remove classes
    document.body.classList.remove(CLOSE_HAND_CLASS);
    this._dragElement.classList.remove(IS_DRAGGING_CLASS);
    if (this._handles && this._handles.length) {
      this._handles.forEach((handle) => {
        handle._dragEvents.off('.DragAction');
        handle.classList.remove(OPEN_HAND_CLASS);
      });
    } else {
      this._dragEvents.off('.DragAction');
      this._dragElement.classList.remove(OPEN_HAND_CLASS);
    }
 
    events.off(`.DragAction${this._id}`);
 
    // Restore overflow
    if (document.body._overflow) {
      document.body.style.overflow = document.body._overflow;
      document.body._overflow = undefined;
    }
 
    // Container might not have been initialized
    if (this._container) {
      if (!this._container.matches('body') && this._container._overflow) {
        this._container.style.overflow = this._container._overflow;
        this._container._overflow = undefined;
      }
    }
 
    // Set to initial position
    if (restorePosition) {
      this._dragElement.style.position = this._initialPosition.position;
      this._dragElement.style.top = this._initialPosition.top;
      this._dragElement.style.left = this._initialPosition.left;
    }
 
    // Remove reference
    this._dragElement.dragAction = undefined;
  }
 
  /**
   Returns {@link DragAction} axis restrictions.
 
   @return {DragActionAxisEnum}
   */
  static get axis() {
    return axis;
  }
 
  /** @private */
  get _scrollingElement() {
    // @polyfill ie11
    // Element that scrolls the document.
    return document.scrollingElement || document.documentElement;
  }
 
  /**
   Triggered when the {@link DragAction#dragElement} starts to be dragged.
 
   @typedef {CustomEvent} coral-dragaction:dragstart
 
   @property {HTMLElement} dragElement
   The dragged element
   @property {Number} pageX
   The mouse position relative to the left edge of the document.
   @property {Number} pageY
   The mouse position relative to the top edge of the document.
   */
 
  /**
   Triggered when the {@link DragAction#dragElement} is being dragged.
 
   @typedef {CustomEvent} coral-dragaction:drag
 
   @property {HTMLElement} dragElement
   The dragged element
   @property {Number} pageX
   The mouse position relative to the left edge of the document.
   @property {Number} pageY
   The mouse position relative to the top edge of the document.
   */
 
  /**
   Triggered when the {@link DragAction#dragElement} stops to be dragged.
 
   @typedef {CustomEvent} coral-dragaction:dragend
 
   @property {HTMLElement} dragElement
   The dragged element
   @property {Number} pageX
   The mouse position relative to the left edge of the document.
   @property {Number} pageY
   The mouse position relative to the top edge of the document.
   */
 
  /**
   Triggered when the {@link DragAction#dragElement} enters a drop element.
 
   @typedef {CustomEvent} coral-dragaction:dragenter
 
   @property {HTMLElement} dragElement
   The dragged element
   @property {HTMLElement} dropElement
   The drop element
   @property {Number} pageX
   The mouse position relative to the left edge of the document.
   @property {Number} pageY
   The mouse position relative to the top edge of the document.
   */
 
  /**
   Triggered when the {@link DragAction#dragElement} is over a drop element.
 
   @typedef {CustomEvent} coral-dragaction:dragover
 
   @property {HTMLElement} dragElement
   The dragged element
   @property {HTMLElement} dropElement
   The drop element
   @property {Number} pageX
   The mouse position relative to the left edge of the document.
   @property {Number} pageY
   The mouse position relative to the top edge of the document.
   */
 
  /**
   Triggered when the {@link DragAction#dragElement} leaves a drop element.
 
   @typedef {CustomEvent} coral-dragaction:dragleave
 
   @property {HTMLElement} dragElement
   The dragged element
   @property {HTMLElement} dropElement
   The drop element
   @property {Number} pageX
   The mouse position relative to the left edge of the document.
   @property {Number} pageY
   The mouse position relative to the top edge of the document.
   */
 
  /**
   Triggered when the {@link DragAction#dragElement} is dropped on a drop element.
 
   @typedef {CustomEvent} coral-dragaction:drop
 
   @property {HTMLElement} dragElement
   The dragged element
   @property {HTMLElement} dropElement
   The drop element
   @property {Number} pageX
   The mouse position relative to the left edge of the document.
   @property {Number} pageY
   The mouse position relative to the top edge of the document.
   */
 
  /**
   Triggered when the {@link DragAction#dragElement} is selected to be dragged.
   @typedef {CustomEvent} coral-dragaction:dragonkeyspace
   @property {HTMLElement} dragElement
   The dragged element
   */
 
  /**
   Triggered when the {@link DragAction#dragElement} is moved on arrow down pressed.
   @typedef {CustomEvent} coral-dragaction:dragoveronkeyarrowdown
   @property {HTMLElement} dragElement
   The dragged element
   */
 
  /**
   Triggered when the {@link DragAction#dragElement} is moved on arrow up pressed.
   @typedef {CustomEvent} coral-dragaction:dragoveronkeyarrowup
   @property {HTMLElement} dragElement
   The dragged element
   */
 
  /**
   Triggered when the {@link DragAction#dragElement} is losing focus.
   @typedef {CustomEvent} coral-dragaction:dragonkeyfocusout
   @property {HTMLElement} dragElement
   The dragged element
   */
 
  /**
   Triggered when the {@link DragAction#dragElement} is dropped on key.
   @typedef {CustomEvent} coral-dragaction:dragendonkey
   @property {HTMLElement} dragElement
   The dragged element
   */
}
 
export default DragAction;