v-splitter.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. function initVSplitter(_name, _left, _right, _alignTo = null) {
  2. var minSplitWidth = 300;
  3. if($('.v-split[data-name="' + _name + '"]').length) return;
  4. if(localStorage['v-split-rounded-' + _name]) {
  5. var tempFinalLeft = Math.round(parseFloat(localStorage['v-split-rounded-' + _name]));
  6. _left.css({flex: '0 0 ' + tempFinalLeft + '%'});
  7. _right.css({flex: '0 0 ' + (100 - tempFinalLeft) + '%'});
  8. }
  9. var origLeft = -1, finalLeft = -1, origX = -1, vMoving = false, totalWidth = _left.parent().width();
  10. _left.parent().css({position: 'relative'});
  11. _left.addClass('v-split-panel').css({maxWidth: 'unset', minWidth: 'unset'});
  12. _right.addClass('v-split-panel').css({maxWidth: 'unset', minWidth: 'unset'});
  13. const vSplit = $('<div/>').addClass('v-split').attr('data-name', _name).insertAfter(_left);
  14. function _setSplitterHeight() {
  15. if(!_alignTo) return;
  16. vSplit.css({marginTop: (_alignTo.position().top + 'px'), height: (_alignTo.outerHeight())});
  17. }
  18. _setSplitterHeight();
  19. vSplit.css({left: (_left.outerWidth() * 100 / totalWidth) + '%'})
  20. .off('mousedown.' + _name).on('mousedown.' + _name, function(_e) {
  21. totalWidth = _left.parent().width();
  22. origLeft = finalLeft = vSplit.position().left;
  23. origX = _e.screenX;
  24. vMoving = true;
  25. $('body').addClass('v-moving');
  26. $(this).addClass('v-split-active');
  27. return false;
  28. });
  29. $(document)
  30. .off('mousemove.' + _name).on('mousemove.' + _name, function(_e) {
  31. if(!vMoving) return;
  32. var tempFinalLeft = origLeft + (_e.screenX - origX);
  33. if(tempFinalLeft < minSplitWidth || tempFinalLeft > (totalWidth - minSplitWidth)) return;
  34. finalLeft = tempFinalLeft;
  35. vSplit.css({left: ((tempFinalLeft * 100) / totalWidth) + '%'});
  36. })
  37. .off('mouseup.' + _name).on('mouseup.' + _name, function(_e) {
  38. if(!vMoving) return;
  39. vMoving = false;
  40. $('body').removeClass('v-moving');
  41. var tempFinalLeft = ((_left.outerWidth() + (finalLeft - origLeft)) * 100 / totalWidth);
  42. tempFinalLeft = Math.round(tempFinalLeft);
  43. _left.css({flex: '0 0 ' + tempFinalLeft + '%'});
  44. vSplit.css({left: (_left.outerWidth() * 100 / totalWidth) + '%'})
  45. _right.css({flex: '0 0 ' + (100 - tempFinalLeft) + '%'});
  46. $('.v-split-active').removeClass('v-split-active');
  47. localStorage['v-split-rounded-' + _name] = tempFinalLeft;
  48. });
  49. $(window).off('resize.' + _name).on('resize.' + _name, function(_e) { _setSplitterHeight(); });
  50. $(window).off('scroll.' + _name).on('scroll.' + _name, function(_e) { _setSplitterHeight(); });
  51. }