Resize.js 1.62 KB
Newer Older
arturo's avatar
arturo committed
1 2
'use strict';

arturo's avatar
arturo committed
3
/**
4
 * @function add
arturo's avatar
arturo committed
5 6 7 8 9 10
 * @param {Window} window
 * @param {Document} document
 * @param {String} sourceQuery
 * @param {String} targetQuery
 * @param {String} type
 */
11
export function _add(window, document, sourceQuery, targetQuery, type) {
arturo's avatar
arturo committed
12 13 14 15 16 17 18 19 20 21 22
  var source = document.querySelector(sourceQuery);
  var target = document.querySelector(targetQuery);

  source.addEventListener('mousedown', initialiseResize, false);

  function initialiseResize(e) {
    window.addEventListener('mousemove', startResizing, false);
    window.addEventListener('mouseup', stopResizing, false);
  }

  function startResizing(e) {
23 24 25
    var height = e.clientY - target.offsetTop
    var width = e.clientX - target.offsetLeft

arturo's avatar
arturo committed
26
    if (type === 'both' || type === 'horizontal')
27
      target.style.height = height + 'px';
arturo's avatar
arturo committed
28
    if (type === 'both' || type === 'vertical')
29
      target.style.width = width + 'px';
arturo's avatar
arturo committed
30 31 32

    // prevent "user-select" highlights
    document.body.classList.add('no-user-select');
33 34
    // prevent event focus losing (eg. while hovering iframe, see #422)
    document.body.classList.add('no-pointer-events');
arturo's avatar
arturo committed
35 36 37 38 39 40 41
  }

  function stopResizing(e) {
    window.removeEventListener('mousemove', startResizing, false);
    window.removeEventListener('mouseup', stopResizing, false);

    document.body.classList.remove('no-user-select');
42
    document.body.classList.remove('no-pointer-events');
arturo's avatar
arturo committed
43 44 45
  }
}
/**
46
 * @function remove
arturo's avatar
arturo committed
47 48 49
 * @param {Document} document
 * @param {String} sourceQuery
 */
50
export function _remove(document, sourceQuery) {
arturo's avatar
arturo committed
51
  var source = document.querySelector(sourceQuery);
arturo's avatar
arturo committed
52
  console.log(sourceQuery)
arturo's avatar
arturo committed
53 54
  source.removeEventListener('mousedown');
}