v-splitter.js 2.9 KB

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