layout.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. aria-label=\"{{ isOpen ? tooltipMsgWhenOpen : tooltipMsgWhenClosed }}\">\
  198. `)(customTogglerScope)
  199. element.append(customTogglerEl)
  200. }
  201. function onPaneOpen(pane) {
  202. if (!hasCustomToggler && pane !== customTogglerPane) {
  203. return
  204. }
  205. return customTogglerEl
  206. .scope()
  207. .$applyAsync(() => (customTogglerEl.scope().isOpen = true))
  208. }
  209. function onPaneClose(pane) {
  210. if (!hasCustomToggler && pane !== customTogglerPane) {
  211. return
  212. }
  213. return customTogglerEl
  214. .scope()
  215. .$applyAsync(() => (customTogglerEl.scope().isOpen = false))
  216. }
  217. // Ensure editor resizes after loading. This is to handle the case where
  218. // the window has been resized while the editor is loading
  219. scope.$on('editor:loaded', () => {
  220. ide.$timeout(() => layout.resizeAll())
  221. })
  222. // Save state when exiting
  223. $(window).unload(() => {
  224. // Save only the state properties for the current layout, ignoring sublayouts inside it.
  225. // If we save sublayouts state (`children`), the layout library will use it when
  226. // initializing. This raises errors when the sublayout elements aren't available (due to
  227. // being loaded at init or just not existing for the current project/user).
  228. const stateToSave = _.mapValues(layout.readState(), pane =>
  229. _.omit(pane, 'children')
  230. )
  231. ide.localStorage(`layout.${name}`, stateToSave)
  232. })
  233. if (attrs.openEast != null) {
  234. scope.$watch(attrs.openEast, function (value, oldValue) {
  235. if (value != null && value !== oldValue) {
  236. if (value) {
  237. layout.open('east')
  238. } else {
  239. layout.close('east')
  240. }
  241. if (hasCustomToggler) {
  242. repositionCustomToggler()
  243. customTogglerEl.scope().$applyAsync(function () {
  244. customTogglerEl.scope().isOpen = value
  245. })
  246. }
  247. }
  248. return setTimeout(() => scope.$digest(), 0)
  249. })
  250. }
  251. if (attrs.allowOverflowOn != null) {
  252. const overflowPane = scope.$eval(attrs.allowOverflowOn)
  253. const overflowPaneEl = layout.panes[overflowPane]
  254. // Set the panel as overflowing (gives it higher z-index and sets overflow rules)
  255. layout.allowOverflow(overflowPane)
  256. // Read the given z-index value and increment it, so that it's higher than synctex controls.
  257. const overflowPaneZVal = overflowPaneEl.zIndex()
  258. overflowPaneEl.css('z-index', overflowPaneZVal + 1)
  259. }
  260. resetOpenStates()
  261. onInternalResize()
  262. if (attrs.layoutDisabled != null) {
  263. return scope.$watch(attrs.layoutDisabled, function (value) {
  264. if (value) {
  265. layout.hide('east')
  266. } else {
  267. layout.show('east')
  268. }
  269. if (hasCustomToggler) {
  270. return customTogglerEl.scope().$applyAsync(function () {
  271. customTogglerEl.scope().isOpen = !value
  272. return (customTogglerEl.scope().isVisible = !value)
  273. })
  274. }
  275. })
  276. }
  277. },
  278. post(scope, element, attrs) {
  279. const name = attrs.layout
  280. const state = element.layout().readState()
  281. return scope.$broadcast(`layout:${name}:linked`, state)
  282. },
  283. }
  284. },
  285. }))