layout.js 10.0 KB

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