verticalResizablePanes.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import App from '../../base'
  2. const layoutOptions = {
  3. center: {
  4. paneSelector: '[vertical-resizable-top]',
  5. paneClass: 'vertical-resizable-top',
  6. size: 'auto'
  7. },
  8. south: {
  9. paneSelector: '[vertical-resizable-bottom]',
  10. paneClass: 'vertical-resizable-bottom',
  11. resizerClass: 'vertical-resizable-resizer',
  12. resizerCursor: 'ns-resize',
  13. size: 'auto',
  14. resizable: true,
  15. closable: false,
  16. slidable: false,
  17. spacing_open: 6,
  18. spacing_closed: 6,
  19. maxSize: '75%'
  20. }
  21. }
  22. export default App.directive('verticalResizablePanes', localStorage => ({
  23. restrict: 'A',
  24. link(scope, element, attrs) {
  25. const name = attrs.verticalResizablePanes
  26. const minSize = attrs.verticalResizablePanesMinSize
  27. const maxSize = attrs.verticalResizablePanesMaxSize
  28. let storedSize = null
  29. let manualResizeIncoming = false
  30. if (name) {
  31. const storageKey = `vertical-resizable:${name}:south-size`
  32. storedSize = localStorage(storageKey)
  33. $(window).unload(() => {
  34. if (storedSize) {
  35. localStorage(storageKey, storedSize)
  36. }
  37. })
  38. }
  39. const toggledExternally = attrs.verticalResizablePanesToggledExternallyOn
  40. const resizeOn = attrs.verticalResizablePanesResizeOn
  41. const resizerDisabledClass = `${layoutOptions.south.resizerClass}-disabled`
  42. function enableResizer() {
  43. if (layoutHandle.resizers && layoutHandle.resizers.south) {
  44. layoutHandle.resizers.south.removeClass(resizerDisabledClass)
  45. }
  46. }
  47. function disableResizer() {
  48. if (layoutHandle.resizers && layoutHandle.resizers.south) {
  49. layoutHandle.resizers.south.addClass(resizerDisabledClass)
  50. }
  51. }
  52. function handleDragEnd() {
  53. manualResizeIncoming = true
  54. }
  55. function handleResize(paneName, paneEl, paneState) {
  56. if (manualResizeIncoming) {
  57. storedSize = paneState.size
  58. }
  59. manualResizeIncoming = false
  60. }
  61. if (toggledExternally) {
  62. scope.$on(toggledExternally, (e, open) => {
  63. let newSize = 'auto'
  64. if (open) {
  65. if (storedSize) {
  66. newSize = storedSize
  67. }
  68. enableResizer()
  69. } else {
  70. disableResizer()
  71. }
  72. layoutHandle.sizePane('south', newSize)
  73. })
  74. }
  75. if (resizeOn) {
  76. scope.$on(resizeOn, () => {
  77. layoutHandle.resizeAll()
  78. })
  79. }
  80. if (maxSize) {
  81. layoutOptions.south.maxSize = maxSize
  82. }
  83. if (minSize) {
  84. layoutOptions.south.minSize = minSize
  85. }
  86. // The `drag` event fires only when the user manually resizes the panes; the `resize` event fires even when
  87. // the layout library internally resizes itself. In order to get explicit user-initiated resizes, we need to
  88. // listen to `drag` events. However, when the `drag` event fires, the panes aren't yet finished sizing so we
  89. // get the pane size *before* the resize happens. We do get the correct size in the next `resize` event.
  90. // The solution to work around this is to set up a flag in `drag` events which tells the next `resize` event
  91. // that it was user-initiated (therefore, storing the value).
  92. layoutOptions.south.ondrag_end = handleDragEnd
  93. layoutOptions.south.onresize = handleResize
  94. const layoutHandle = element.layout(layoutOptions)
  95. }
  96. }))