verticalResizablePanes.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 resizerDisabledClass = `${layoutOptions.south.resizerClass}-disabled`
  41. function enableResizer() {
  42. if (layoutHandle.resizers && layoutHandle.resizers.south) {
  43. layoutHandle.resizers.south.removeClass(resizerDisabledClass)
  44. }
  45. }
  46. function disableResizer() {
  47. if (layoutHandle.resizers && layoutHandle.resizers.south) {
  48. layoutHandle.resizers.south.addClass(resizerDisabledClass)
  49. }
  50. }
  51. function handleDragEnd() {
  52. manualResizeIncoming = true
  53. }
  54. function handleResize(paneName, paneEl, paneState) {
  55. if (manualResizeIncoming) {
  56. storedSize = paneState.size
  57. }
  58. manualResizeIncoming = false
  59. }
  60. if (toggledExternally) {
  61. scope.$on(toggledExternally, (e, open) => {
  62. let newSize = 'auto'
  63. if (open) {
  64. if (storedSize) {
  65. newSize = storedSize
  66. }
  67. enableResizer()
  68. } else {
  69. disableResizer()
  70. }
  71. layoutHandle.sizePane('south', newSize)
  72. })
  73. }
  74. if (maxSize) {
  75. layoutOptions.south.maxSize = maxSize
  76. }
  77. if (minSize) {
  78. layoutOptions.south.minSize = minSize
  79. }
  80. // The `drag` event fires only when the user manually resizes the panes; the `resize` event fires even when
  81. // the layout library internally resizes itself. In order to get explicit user-initiated resizes, we need to
  82. // listen to `drag` events. However, when the `drag` event fires, the panes aren't yet finished sizing so we
  83. // get the pane size *before* the resize happens. We do get the correct size in the next `resize` event.
  84. // The solution to work around this is to set up a flag in `drag` events which tells the next `resize` event
  85. // that it was user-initiated (therefore, storing the value).
  86. layoutOptions.south.ondrag_end = handleDragEnd
  87. layoutOptions.south.onresize = handleResize
  88. const layoutHandle = element.layout(layoutOptions)
  89. }
  90. }))