init.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /**
  2. * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
  3. * SPDX-License-Identifier: AGPL-3.0-or-later
  4. */
  5. /* globals Snap */
  6. import _ from 'underscore'
  7. import $ from 'jquery'
  8. import moment from 'moment'
  9. import { initSessionHeartBeat } from './session-heartbeat.js'
  10. import OC from './OC/index.js'
  11. import { setUp as setUpContactsMenu } from './components/ContactsMenu.js'
  12. import { setUp as setUpMainMenu } from './components/MainMenu.js'
  13. import { setUp as setUpUserMenu } from './components/UserMenu.js'
  14. import { interceptRequests } from './utils/xhr-request.js'
  15. // keep in sync with core/css/variables.scss
  16. const breakpointMobileWidth = 1024
  17. const initLiveTimestamps = () => {
  18. // Update live timestamps every 30 seconds
  19. setInterval(() => {
  20. $('.live-relative-timestamp').each(function() {
  21. const timestamp = parseInt($(this).attr('data-timestamp'), 10)
  22. $(this).text(moment(timestamp).fromNow())
  23. })
  24. }, 30 * 1000)
  25. }
  26. /**
  27. * Moment doesn't have aliases for every locale and doesn't parse some locale IDs correctly so we need to alias them
  28. */
  29. const localeAliases = {
  30. zh: 'zh-cn',
  31. zh_Hans: 'zh-cn',
  32. zh_Hans_CN: 'zh-cn',
  33. zh_Hans_HK: 'zh-cn',
  34. zh_Hans_MO: 'zh-cn',
  35. zh_Hans_SG: 'zh-cn',
  36. zh_Hant: 'zh-hk',
  37. zh_Hant_HK: 'zh-hk',
  38. zh_Hant_MO: 'zh-mo',
  39. zh_Hant_TW: 'zh-tw',
  40. }
  41. let locale = OC.getLocale()
  42. if (Object.prototype.hasOwnProperty.call(localeAliases, locale)) {
  43. locale = localeAliases[locale]
  44. }
  45. /**
  46. * Set users locale to moment.js as soon as possible
  47. */
  48. moment.locale(locale)
  49. /**
  50. * Initializes core
  51. */
  52. export const initCore = () => {
  53. interceptRequests()
  54. $(window).on('unload.main', () => { OC._unloadCalled = true })
  55. $(window).on('beforeunload.main', () => {
  56. // super-trick thanks to http://stackoverflow.com/a/4651049
  57. // in case another handler displays a confirmation dialog (ex: navigating away
  58. // during an upload), there are two possible outcomes: user clicked "ok" or
  59. // "cancel"
  60. // first timeout handler is called after unload dialog is closed
  61. setTimeout(() => {
  62. OC._userIsNavigatingAway = true
  63. // second timeout event is only called if user cancelled (Chrome),
  64. // but in other browsers it might still be triggered, so need to
  65. // set a higher delay...
  66. setTimeout(() => {
  67. if (!OC._unloadCalled) {
  68. OC._userIsNavigatingAway = false
  69. }
  70. }, 10000)
  71. }, 1)
  72. })
  73. $(document).on('ajaxError.main', function(event, request, settings) {
  74. if (settings && settings.allowAuthErrors) {
  75. return
  76. }
  77. OC._processAjaxError(request)
  78. })
  79. initSessionHeartBeat()
  80. OC.registerMenu($('#expand'), $('#expanddiv'), false, true)
  81. // toggle for menus
  82. $(document).on('mouseup.closemenus', event => {
  83. const $el = $(event.target)
  84. if ($el.closest('.menu').length || $el.closest('.menutoggle').length) {
  85. // don't close when clicking on the menu directly or a menu toggle
  86. return false
  87. }
  88. OC.hideMenus()
  89. })
  90. setUpMainMenu()
  91. setUpUserMenu()
  92. setUpContactsMenu()
  93. // just add snapper for logged in users
  94. // and if the app doesn't handle the nav slider itself
  95. if ($('#app-navigation').length && !$('html').hasClass('lte9')
  96. && !$('#app-content').hasClass('no-snapper')) {
  97. // App sidebar on mobile
  98. const snapper = new Snap({
  99. element: document.getElementById('app-content'),
  100. disable: 'right',
  101. maxPosition: 300, // $navigation-width
  102. minDragDistance: 100,
  103. })
  104. $('#app-content').prepend('<div id="app-navigation-toggle" class="icon-menu" style="display:none" tabindex="0"></div>')
  105. // keep track whether snapper is currently animating, and
  106. // prevent to call open or close while that is the case
  107. // to avoid duplicating events (snap.js doesn't check this)
  108. let animating = false
  109. snapper.on('animating', () => {
  110. // we need this because the trigger button
  111. // is also implicitly wired to close by snapper
  112. animating = true
  113. })
  114. snapper.on('animated', () => {
  115. animating = false
  116. })
  117. snapper.on('start', () => {
  118. // we need this because dragging triggers that
  119. animating = true
  120. })
  121. snapper.on('end', () => {
  122. // we need this because dragging stop triggers that
  123. animating = false
  124. })
  125. snapper.on('open', () => {
  126. $appNavigation.attr('aria-hidden', 'false')
  127. })
  128. snapper.on('close', () => {
  129. $appNavigation.attr('aria-hidden', 'true')
  130. })
  131. // These are necessary because calling open or close
  132. // on snapper during an animation makes it trigger an
  133. // unfinishable animation, which itself will continue
  134. // triggering animating events and cause high CPU load,
  135. //
  136. // Ref https://github.com/jakiestfu/Snap.js/issues/216
  137. const oldSnapperOpen = snapper.open
  138. const oldSnapperClose = snapper.close
  139. const _snapperOpen = () => {
  140. if (animating || snapper.state().state !== 'closed') {
  141. return
  142. }
  143. oldSnapperOpen('left')
  144. }
  145. const _snapperClose = () => {
  146. if (animating || snapper.state().state === 'closed') {
  147. return
  148. }
  149. oldSnapperClose()
  150. }
  151. // Needs to be deferred to properly catch in-between
  152. // events that snap.js is triggering after dragging.
  153. //
  154. // Skipped when running unit tests as we are not testing
  155. // the snap.js workarounds...
  156. if (!window.TESTING) {
  157. snapper.open = () => {
  158. _.defer(_snapperOpen)
  159. }
  160. snapper.close = () => {
  161. _.defer(_snapperClose)
  162. }
  163. }
  164. $('#app-navigation-toggle').click((e) => {
  165. // close is implicit in the button by snap.js
  166. if (snapper.state().state !== 'left') {
  167. snapper.open()
  168. }
  169. })
  170. $('#app-navigation-toggle').keypress(e => {
  171. if (snapper.state().state === 'left') {
  172. snapper.close()
  173. } else {
  174. snapper.open()
  175. }
  176. })
  177. // close sidebar when switching navigation entry
  178. const $appNavigation = $('#app-navigation')
  179. $appNavigation.attr('aria-hidden', 'true')
  180. $appNavigation.delegate('a, :button', 'click', event => {
  181. const $target = $(event.target)
  182. // don't hide navigation when changing settings or adding things
  183. if ($target.is('.app-navigation-noclose')
  184. || $target.closest('.app-navigation-noclose').length) {
  185. return
  186. }
  187. if ($target.is('.app-navigation-entry-utils-menu-button')
  188. || $target.closest('.app-navigation-entry-utils-menu-button').length) {
  189. return
  190. }
  191. if ($target.is('.add-new')
  192. || $target.closest('.add-new').length) {
  193. return
  194. }
  195. if ($target.is('#app-settings')
  196. || $target.closest('#app-settings').length) {
  197. return
  198. }
  199. snapper.close()
  200. })
  201. let navigationBarSlideGestureEnabled = false
  202. let navigationBarSlideGestureAllowed = true
  203. let navigationBarSlideGestureEnablePending = false
  204. OC.allowNavigationBarSlideGesture = () => {
  205. navigationBarSlideGestureAllowed = true
  206. if (navigationBarSlideGestureEnablePending) {
  207. snapper.enable()
  208. navigationBarSlideGestureEnabled = true
  209. navigationBarSlideGestureEnablePending = false
  210. }
  211. }
  212. OC.disallowNavigationBarSlideGesture = () => {
  213. navigationBarSlideGestureAllowed = false
  214. if (navigationBarSlideGestureEnabled) {
  215. const endCurrentDrag = true
  216. snapper.disable(endCurrentDrag)
  217. navigationBarSlideGestureEnabled = false
  218. navigationBarSlideGestureEnablePending = true
  219. }
  220. }
  221. const toggleSnapperOnSize = () => {
  222. if ($(window).width() > breakpointMobileWidth) {
  223. $appNavigation.attr('aria-hidden', 'false')
  224. snapper.close()
  225. snapper.disable()
  226. navigationBarSlideGestureEnabled = false
  227. navigationBarSlideGestureEnablePending = false
  228. } else if (navigationBarSlideGestureAllowed) {
  229. snapper.enable()
  230. navigationBarSlideGestureEnabled = true
  231. navigationBarSlideGestureEnablePending = false
  232. } else {
  233. navigationBarSlideGestureEnablePending = true
  234. }
  235. }
  236. $(window).resize(_.debounce(toggleSnapperOnSize, 250))
  237. // initial call
  238. toggleSnapperOnSize()
  239. }
  240. initLiveTimestamps()
  241. }