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

arturo's avatar
arturo committed
3 4 5
exports._add = add;
exports._remove = remove;
/**
6
 * @function add
arturo's avatar
arturo committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * @param {Window} window
 * @param {Document} document
 * @param {String} sourceQuery
 * @param {String} targetQuery
 * @param {String} type
 */
function add(window, document, sourceQuery, targetQuery, type) {
  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) {
25 26 27
    var height = e.clientY - target.offsetTop
    var width = e.clientX - target.offsetLeft

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

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

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

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