layout.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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 'libs/jquery-layout'
  18. import '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.initialSizeEast),
  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. }
  79. if (state.west != null) {
  80. if (
  81. attrs.minimumRestoreSizeWest == null ||
  82. (state.west.size >= attrs.minimumRestoreSizeWest &&
  83. !state.west.initClosed)
  84. ) {
  85. options.west = state.west
  86. }
  87. }
  88. }
  89. options.east.resizerCursor = 'ew-resize'
  90. options.west.resizerCursor = 'ew-resize'
  91. const repositionControls = function() {
  92. state = element.layout().readState()
  93. if (state.east != null) {
  94. const controls = element.find('> .ui-layout-resizer-controls')
  95. if (state.east.initClosed) {
  96. return controls.hide()
  97. } else {
  98. controls.show()
  99. return controls.css({
  100. right: state.east.size
  101. })
  102. }
  103. }
  104. }
  105. const repositionCustomToggler = function() {
  106. if (customTogglerEl == null) {
  107. return
  108. }
  109. state = element.layout().readState()
  110. const positionAnchor = customTogglerPane === 'east' ? 'right' : 'left'
  111. const paneState = state[customTogglerPane]
  112. if (paneState != null) {
  113. return customTogglerEl.css(
  114. positionAnchor,
  115. paneState.initClosed ? 0 : paneState.size
  116. )
  117. }
  118. }
  119. const resetOpenStates = function() {
  120. state = element.layout().readState()
  121. if (attrs.openEast != null && state.east != null) {
  122. const openEast = $parse(attrs.openEast)
  123. return openEast.assign(scope, !state.east.initClosed)
  124. }
  125. }
  126. // Someone moved the resizer
  127. var onInternalResize = function() {
  128. state = element.layout().readState()
  129. scope.$broadcast(`layout:${name}:resize`, state)
  130. repositionControls()
  131. if (hasCustomToggler) {
  132. repositionCustomToggler()
  133. }
  134. return resetOpenStates()
  135. }
  136. let oldWidth = element.width()
  137. // Something resized our parent element
  138. const onExternalResize = function() {
  139. if (
  140. attrs.resizeProportionally != null &&
  141. scope.$eval(attrs.resizeProportionally)
  142. ) {
  143. const eastState = element.layout().readState().east
  144. if (eastState != null) {
  145. const newInternalWidth =
  146. (eastState.size / oldWidth) * element.width()
  147. oldWidth = element.width()
  148. element.layout().sizePane('east', newInternalWidth)
  149. return
  150. }
  151. }
  152. return element.layout().resizeAll()
  153. }
  154. element.layout(options)
  155. element.layout().resizeAll()
  156. if (attrs.resizeOn != null) {
  157. for (let event of Array.from(attrs.resizeOn.split(','))) {
  158. scope.$on(event, () => onExternalResize())
  159. }
  160. }
  161. if (hasCustomToggler) {
  162. state = element.layout().readState()
  163. const customTogglerScope = scope.$new()
  164. customTogglerScope.isOpen = true
  165. customTogglerScope.isVisible = true
  166. if (
  167. (state[customTogglerPane] != null
  168. ? state[customTogglerPane].initClosed
  169. : undefined) === true
  170. ) {
  171. customTogglerScope.isOpen = false
  172. }
  173. customTogglerScope.tooltipMsgWhenOpen = customTogglerMsgWhenOpen
  174. customTogglerScope.tooltipMsgWhenClosed = customTogglerMsgWhenClosed
  175. customTogglerScope.tooltipPlacement =
  176. customTogglerPane === 'east' ? 'left' : 'right'
  177. customTogglerScope.handleClick = function() {
  178. element.layout().toggle(customTogglerPane)
  179. return repositionCustomToggler()
  180. }
  181. customTogglerEl = $compile(`\
  182. <a href \
  183. ng-show=\"isVisible\" \
  184. class=\"custom-toggler ${`custom-toggler-${customTogglerPane}`}\" \
  185. ng-class=\"isOpen ? 'custom-toggler-open' : 'custom-toggler-closed'\" \
  186. tooltip=\"{{ isOpen ? tooltipMsgWhenOpen : tooltipMsgWhenClosed }}\" \
  187. tooltip-placement=\"{{ tooltipPlacement }}\" \
  188. ng-click=\"handleClick()\">\
  189. `)(customTogglerScope)
  190. element.append(customTogglerEl)
  191. }
  192. var onPaneOpen = function(pane) {
  193. if (!hasCustomToggler && pane !== customTogglerPane) {
  194. return
  195. }
  196. return customTogglerEl
  197. .scope()
  198. .$applyAsync(() => (customTogglerEl.scope().isOpen = true))
  199. }
  200. var onPaneClose = function(pane) {
  201. if (!hasCustomToggler && pane !== customTogglerPane) {
  202. return
  203. }
  204. return customTogglerEl
  205. .scope()
  206. .$applyAsync(() => (customTogglerEl.scope().isOpen = false))
  207. }
  208. // Save state when exiting
  209. $(window).unload(() => {
  210. // Save only the state properties for the current layout, ignoring sublayouts inside it.
  211. // If we save sublayouts state (`children`), the layout library will use it when
  212. // initializing. This raises errors when the sublayout elements aren't available (due to
  213. // being loaded at init or just not existing for the current project/user).
  214. const stateToSave = _.mapValues(element.layout().readState(), pane =>
  215. _.omit(pane, 'children')
  216. )
  217. ide.localStorage(`layout.${name}`, stateToSave)
  218. })
  219. if (attrs.openEast != null) {
  220. scope.$watch(attrs.openEast, function(value, oldValue) {
  221. if (value != null && value !== oldValue) {
  222. if (value) {
  223. element.layout().open('east')
  224. } else {
  225. element.layout().close('east')
  226. }
  227. }
  228. return setTimeout(() => scope.$digest(), 0)
  229. })
  230. }
  231. if (attrs.allowOverflowOn != null) {
  232. const layoutObj = element.layout()
  233. const overflowPane = scope.$eval(attrs.allowOverflowOn)
  234. const overflowPaneEl = layoutObj.panes[overflowPane]
  235. // Set the panel as overflowing (gives it higher z-index and sets overflow rules)
  236. layoutObj.allowOverflow(overflowPane)
  237. // Read the given z-index value and increment it, so that it's higher than synctex controls.
  238. const overflowPaneZVal = overflowPaneEl.zIndex()
  239. overflowPaneEl.css('z-index', overflowPaneZVal + 1)
  240. }
  241. resetOpenStates()
  242. onInternalResize()
  243. if (attrs.layoutDisabled != null) {
  244. return scope.$watch(attrs.layoutDisabled, function(value) {
  245. if (value) {
  246. element.layout().hide('east')
  247. } else {
  248. element.layout().show('east')
  249. }
  250. if (hasCustomToggler) {
  251. return customTogglerEl.scope().$applyAsync(function() {
  252. customTogglerEl.scope().isOpen = !value
  253. return (customTogglerEl.scope().isVisible = !value)
  254. })
  255. }
  256. })
  257. }
  258. },
  259. post(scope, element, attrs) {
  260. const name = attrs.layout
  261. const state = element.layout().readState()
  262. return scope.$broadcast(`layout:${name}:linked`, state)
  263. }
  264. }
  265. }
  266. }))