all files / addon/components/polaris-color-picker/ slidable.js

21.74% Statements 10/46
21.43% Branches 3/14
33.33% Functions 2/6
24.39% Lines 10/41
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                                                                                                                          10× 10× 10×                                                                                                                             10×   10× 10×                    
import $Ember from 'jquery';
import Component from '@ember/component';
import { computed } from '@ember/object';
import { isNone, typeOf } from '@ember/utils';
import { htmlSafe } from '@ember/string';
import layout from '../../templates/components/polaris-color-picker/slidable';
 
function startDrag(event) {
  this.set('isDragging', true);
  this.handleMove(event);
 
  // Set up global event listeners to handle dragging outside the slidable area.
  $Ember(window).on('mousemove', (...moveArgs) => { this.handleMove(...moveArgs); });
  $Ember(window).on('mouseup', () => { this.handleDragEnd(); });
 
  $Ember(window).on('touchmove', (...moveArgs) => { this.handleMove(...moveArgs); });
  $Ember(window).on('touchend', () => { this.handleDragEnd(); });
  $Ember(window).on('touchcancel', () => { this.handleDragEnd(); });
}
 
// Draggable marker, used to pick hue, saturation, brightness and alpha.
export default Component.extend({
  classNames: ['Polaris-ColorPicker__Slidable'],
 
  layout,
 
  /*
   * Public attributes.
   */
  /**
   * The current x position of the dragger
   *
   * @property draggerX
   * @type {Number}
   * @default 0
   */
  draggerX: 0,
 
  /**
   * The current y position of the dragger
   *
   * @property draggerY
   * @type {Number}
   * @default 0
   */
  draggerY: 0,
 
  /**
   * Callback for the outside world to receive the height of the dragger
   *
   * @property onDraggerHeightChanged
   * @type {function}
   * @default null
   */
  onDraggerHeightChanged: null,
 
  /*
   * Internal properties.
   */
  isDragging: false,
 
  draggerStyle: computed('draggerX', 'draggerY', function() {
    const { draggerX, draggerY } = this.getProperties('draggerX', 'draggerY');
    const transform = `translate3d(${ draggerX }px, ${ draggerY }px, 0)`;
    return htmlSafe(`transform: ${ transform };`);
  }).readOnly(),
 
  /*
   * Internal methods.
   */
  handleMove(event) {
    if (!this.get('isDragging')) {
      return;
    }
 
    event.stopImmediatePropagation();
    event.stopPropagation();
    event.preventDefault();
 
    if (event.touches && event.touches.length) {
      this.handleDraggerMove(event.touches[0].clientX, event.touches[0].clientY);
      return;
    }
 
    this.handleDraggerMove(event.clientX, event.clientY);
  },
 
  handleDragEnd() {
    this.set('isDragging', false);
 
    // Remove our global event listeners.
    $Ember(window).off('mousemove');
    $Ember(window).off('mouseup');
 
    $Ember(window).off('touchmove');
    $Ember(window).off('touchend');
    $Ember(window).off('touchcancel');
  },
 
  handleDraggerMove(clientX, clientY) {
    const moveHandler = this.get('onChange');
    if (typeof(moveHandler) !== 'function') {
      return;
    }
 
    const element = this.get('element');
    if (isNone(element)) {
      return;
    }
 
    const rect = element.getBoundingClientRect();
    moveHandler({
      x: clientX - rect.left,
      y: clientY - rect.top,
    });
  },
 
  /*
   * Action handlers.
   */
  mouseDown: startDrag,
  touchStart: startDrag,
 
  /*
   * Lifecycle hooks.
   */
  didRender() {
    this._super(...arguments);
 
    const onDraggerHeightChanged = this.get('onDraggerHeightChanged');
    if (typeOf(onDraggerHeightChanged) === 'function') {
      // Publish the height of our dragger.
      const draggerElement = this.$('div.Polaris-ColorPicker__Dragger')[0];
      Iif (isNone(draggerElement)) {
        return;
      }
 
      // Yes, for some strange reason this is width not height in the shopify code...
      onDraggerHeightChanged(draggerElement.clientWidth);
    }
  },
});