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

arturo's avatar
arturo committed
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
exports._add = add;
exports._remove = remove;
/**
 * @name add
 * @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) {
    if (type === 'both' || type === 'horizontal')
      target.style.height = (e.clientY - target.offsetTop) + 'px';
    if (type === 'both' || type === 'vertical')
      target.style.width = (e.clientX - target.offsetLeft) + 'px';

    // prevent "user-select" highlights
    document.body.classList.add('no-user-select');
  }

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

    document.body.classList.remove('no-user-select');
  }
}
/**
 * @name remove
 * @param {Document} document
 * @param {String} sourceQuery
 */
arturo's avatar
arturo committed
46
function remove(document, sourceQuery) {
arturo's avatar
arturo committed
47
  var source = document.querySelector(sourceQuery);
arturo's avatar
arturo committed
48
  console.log(sourceQuery)
arturo's avatar
arturo committed
49 50
  source.removeEventListener('mousedown');
}