layout.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /* eslint-disable
  2. max-len,
  3. no-return-assign,
  4. no-useless-escape,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS101: Remove unnecessary use of Array.from
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * DS207: Consider shorter variations of null checks
  13. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  14. */
  15. import App from '../../base'
  16. import _ from 'lodash'
  17. import '../../vendor/libs/jquery-layout'
  18. import '../../vendor/libs/jquery.ui.touch-punch'
  19. export default App.directive('layout', ($parse, $compile, ide) => ({
  20. compile() {
  21. return {
  22. pre(scope, element, attrs) {
  23. let customTogglerEl, spacingClosed, spacingOpen, state
  24. const name = attrs.layout
  25. const { customTogglerPane } = attrs
  26. const { customTogglerMsgWhenOpen } = attrs
  27. const { customTogglerMsgWhenClosed } = attrs
  28. const hasCustomToggler =
  29. customTogglerPane != null &&
  30. customTogglerMsgWhenOpen != null &&
  31. customTogglerMsgWhenClosed != null
  32. if (attrs.spacingOpen != null) {
  33. spacingOpen = parseInt(attrs.spacingOpen, 10)
  34. } else {
  35. spacingOpen = 7
  36. }
  37. if (attrs.spacingClosed != null) {
  38. spacingClosed = parseInt(attrs.spacingClosed, 10)
  39. } else {
  40. spacingClosed = 7
  41. }
  42. const options = {
  43. spacing_open: spacingOpen,
  44. spacing_closed: spacingClosed,
  45. slidable: false,
  46. enableCursorHotkey: false,
  47. onopen: pane => {
  48. return onPaneOpen(pane)
  49. },
  50. onclose: pane => {
  51. return onPaneClose(pane)
  52. },
  53. onresize: () => {
  54. return onInternalResize()
  55. },
  56. maskIframesOnResize: scope.$eval(
  57. attrs.maskIframesOnResize || 'false'
  58. ),
  59. east: {
  60. size: scope.$eval(attrs.initialSizeEast),
  61. initClosed: scope.$eval(attrs.initClosedEast),
  62. },
  63. west: {
  64. size: scope.$eval(attrs.initialSizeWest),
  65. initClosed: scope.$eval(attrs.initClosedWest),
  66. },
  67. }
  68. // Restore previously recorded state
  69. if ((state = ide.localStorage(`layout.${name}`)) != null) {
  70. if (state.east != null) {
  71. if (
  72. attrs.minimumRestoreSizeEast == null ||
  73. (state.east.size >= attrs.minimumRestoreSizeEast &&
  74. !state.east.initClosed)
  75. ) {
  76. options.east = state.east
  77. }
  78. options.east.initClosed = state.east.initClosed
  79. }
  80. if (state.west != null) {
  81. if (
  82. attrs.minimumRestoreSizeWest == null ||
  83. (state.west.size >= attrs.minimumRestoreSizeWest &&
  84. !state.west.initClosed)
  85. ) {
  86. options.west = state.west
  87. }
  88. // NOTE: disabled so that the file tree re-opens on page load
  89. // options.west.initClosed = state.west.initClosed
  90. }
  91. }
  92. options.east.resizerCursor = 'ew-resize'
  93. options.west.resizerCursor = 'ew-resize'
  94. function repositionControls() {
  95. state = layout.readState()
  96. if (state.east != null) {
  97. const controls = element.find('> .ui-layout-resizer-controls')
  98. if (state.east.initClosed) {
  99. return controls.hide()
  100. } else {
  101. controls.show()
  102. return controls.css({
  103. right: state.east.size,
  104. })
  105. }
  106. }
  107. }
  108. function repositionCustomToggler() {
  109. if (customTogglerEl == null) {
  110. return
  111. }
  112. state = layout.readState()
  113. const positionAnchor = customTogglerPane === 'east' ? 'right' : 'left'
  114. const paneState = state[customTogglerPane]
  115. if (paneState != null) {
  116. return customTogglerEl.css(
  117. positionAnchor,
  118. paneState.initClosed ? 0 : paneState.size
  119. )
  120. }
  121. }
  122. function resetOpenStates() {
  123. state = layout.readState()
  124. if (attrs.openEast != null && state.east != null) {
  125. const openEast = $parse(attrs.openEast)
  126. return openEast.assign(scope, !state.east.initClosed)
  127. }
  128. }
  129. // Someone moved the resizer
  130. function onInternalResize() {
  131. state = layout.readState()
  132. scope.$broadcast(`layout:${name}:resize`, state)
  133. repositionControls()
  134. if (hasCustomToggler) {
  135. repositionCustomToggler()
  136. }
  137. return resetOpenStates()
  138. }
  139. let oldWidth = element.width()
  140. // Something resized our parent element
  141. const onExternalResize = function () {
  142. if (
  143. attrs.resizeProportionally != null &&
  144. scope.$eval(attrs.resizeProportionally)
  145. ) {
  146. const eastState = layout.readState().east
  147. if (eastState != null) {
  148. const currentWidth = element.width()
  149. if (currentWidth > 0) {
  150. const newInternalWidth =
  151. (eastState.size / oldWidth) * currentWidth
  152. oldWidth = currentWidth
  153. layout.sizePane('east', newInternalWidth)
  154. }
  155. return
  156. }
  157. }
  158. ide.$timeout(() => {
  159. layout.resizeAll()
  160. })
  161. }
  162. const layout = element.layout(options)
  163. layout.resizeAll()
  164. if (attrs.resizeOn != null) {
  165. for (const event of Array.from(attrs.resizeOn.split(','))) {
  166. scope.$on(event, () => onExternalResize())
  167. }
  168. }
  169. if (hasCustomToggler) {
  170. state = layout.readState()
  171. const customTogglerScope = scope.$new()
  172. customTogglerScope.isOpen = true
  173. customTogglerScope.isVisible = true
  174. if (
  175. (state[customTogglerPane] != null
  176. ? state[customTogglerPane].initClosed
  177. : undefined) === true
  178. ) {
  179. customTogglerScope.isOpen = false
  180. }
  181. customTogglerScope.tooltipMsgWhenOpen = customTogglerMsgWhenOpen
  182. customTogglerScope.tooltipMsgWhenClosed = customTogglerMsgWhenClosed
  183. customTogglerScope.tooltipPlacement =
  184. customTogglerPane === 'east' ? 'left' : 'right'
  185. customTogglerScope.handleClick = function () {
  186. layout.toggle(customTogglerPane)
  187. return repositionCustomToggler()
  188. }
  189. customTogglerEl = $compile(`\
  190. <a href \
  191. ng-show=\"isVisible\" \
  192. class=\"custom-toggler ${`custom-toggler-${customTogglerPane}`}\" \
  193. ng-class=\"isOpen ? 'custom-toggler-open' : 'custom-toggler-closed'\" \
  194. tooltip=\"{{ isOpen ? tooltipMsgWhenOpen : tooltipMsgWhenClosed }}\" \
  195. tooltip-placement=\"{{ tooltipPlacement }}\" \
  196. ng-click=\"handleClick()\">\
  197. `)(customTogglerScope)
  198. element.append(customTogglerEl)
  199. }
  200. function onPaneOpen(pane) {
  201. if (!hasCustomToggler && pane !== customTogglerPane) {
  202. return
  203. }
  204. return customTogglerEl
  205. .scope()
  206. .$applyAsync(() => (customTogglerEl.scope().isOpen = true))
  207. }
  208. function onPaneClose(pane) {
  209. if (!hasCustomToggler && pane !== customTogglerPane) {
  210. return
  211. }
  212. return customTogglerEl
  213. .scope()
  214. .$applyAsync(() => (customTogglerEl.scope().isOpen = false))
  215. }
  216. // Save state when exiting
  217. $(window).unload(() => {
  218. // Save only the state properties for the current layout, ignoring sublayouts inside it.
  219. // If we save sublayouts state (`children`), the layout library will use it when
  220. // initializing. This raises errors when the sublayout elements aren't available (due to
  221. // being loaded at init or just not existing for the current project/user).
  222. const stateToSave = _.mapValues(layout.readState(), pane =>
  223. _.omit(pane, 'children')
  224. )
  225. ide.localStorage(`layout.${name}`, stateToSave)
  226. })
  227. if (attrs.openEast != null) {
  228. scope.$watch(attrs.openEast, function (value, oldValue) {
  229. if (value != null && value !== oldValue) {
  230. if (value) {
  231. layout.open('east')
  232. } else {
  233. layout.close('east')
  234. }
  235. if (hasCustomToggler) {
  236. repositionCustomToggler()
  237. customTogglerEl.scope().$applyAsync(function () {
  238. customTogglerEl.scope().isOpen = value
  239. })
  240. }
  241. }
  242. return setTimeout(() => scope.$digest(), 0)
  243. })
  244. }
  245. if (attrs.allowOverflowOn != null) {
  246. const overflowPane = scope.$eval(attrs.allowOverflowOn)
  247. const overflowPaneEl = layout.panes[overflowPane]
  248. // Set the panel as overflowing (gives it higher z-index and sets overflow rules)
  249. layout.allowOverflow(overflowPane)
  250. // Read the given z-index value and increment it, so that it's higher than synctex controls.
  251. const overflowPaneZVal = overflowPaneEl.zIndex()
  252. overflowPaneEl.css('z-index', overflowPaneZVal + 1)
  253. }
  254. resetOpenStates()
  255. onInternalResize()
  256. if (attrs.layoutDisabled != null) {
  257. return scope.$watch(attrs.layoutDisabled, function (value) {
  258. if (value) {
  259. layout.hide('east')
  260. } else {
  261. layout.show('east')
  262. }
  263. if (hasCustomToggler) {
  264. return customTogglerEl.scope().$applyAsync(function () {
  265. customTogglerEl.scope().isOpen = !value
  266. return (customTogglerEl.scope().isVisible = !value)
  267. })
  268. }
  269. })
  270. }
  271. },
  272. post(scope, element, attrs) {
  273. const name = attrs.layout
  274. const state = element.layout().readState()
  275. return scope.$broadcast(`layout:${name}:linked`, state)
  276. },
  277. }
  278. },
  279. }))