layout.js 12 KB

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