v-splitter.js 2.9 KB

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