1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
|
/**
* @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
*
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
* @author Jan-Christoph Borchardt <hey@jancborchardt.net>
* @author John Molakvoæ <skjnldsv@protonmail.com>
* @author nacho <nacho@ownyourbits.com>
* @author Vincent Petry <vincent@nextcloud.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/* globals Snap */
import _ from 'underscore'
import $ from 'jquery'
import moment from 'moment'
import cssVars from 'css-vars-ponyfill'
import { initSessionHeartBeat } from './session-heartbeat'
import OC from './OC/index'
import { setUp as setUpContactsMenu } from './components/ContactsMenu'
import { setUp as setUpMainMenu } from './components/MainMenu'
import { setUp as setUpUserMenu } from './components/UserMenu'
import PasswordConfirmation from './OC/password-confirmation'
// keep in sync with core/css/variables.scss
const breakpointMobileWidth = 1024
const resizeMenu = () => {
const appList = $('#appmenu li')
const rightHeaderWidth = $('.header-right').outerWidth()
const headerWidth = $('header').outerWidth()
const usePercentualAppMenuLimit = 0.67
const minAppsDesktop = 12
let availableWidth = headerWidth - $('#nextcloud').outerWidth() - (rightHeaderWidth > 210 ? rightHeaderWidth : 210)
const isMobile = $(window).width() < breakpointMobileWidth
if (!isMobile) {
availableWidth = availableWidth * usePercentualAppMenuLimit
}
let appCount = Math.floor((availableWidth / $(appList).width()))
if (isMobile && appCount > minAppsDesktop) {
appCount = minAppsDesktop
}
if (!isMobile && appCount < minAppsDesktop) {
appCount = minAppsDesktop
}
// show at least 2 apps in the popover
if (appList.length - 1 - appCount >= 1) {
appCount--
}
$('#more-apps a').removeClass('active')
let lastShownApp
for (let k = 0; k < appList.length - 1; k++) {
const name = $(appList[k]).data('id')
if (k < appCount) {
$(appList[k]).removeClass('hidden')
$('#apps li[data-id=' + name + ']').addClass('in-header')
lastShownApp = appList[k]
} else {
$(appList[k]).addClass('hidden')
$('#apps li[data-id=' + name + ']').removeClass('in-header')
// move active app to last position if it is active
if (appCount > 0 && $(appList[k]).children('a').hasClass('active')) {
$(lastShownApp).addClass('hidden')
$('#apps li[data-id=' + $(lastShownApp).data('id') + ']').removeClass('in-header')
$(appList[k]).removeClass('hidden')
$('#apps li[data-id=' + name + ']').addClass('in-header')
}
}
}
// show/hide more apps icon
if ($('#apps li:not(.in-header)').length === 0) {
$('#more-apps').hide()
$('#navigation').hide()
} else {
$('#more-apps').show()
}
}
const initLiveTimestamps = () => {
// Update live timestamps every 30 seconds
setInterval(() => {
$('.live-relative-timestamp').each(function() {
$(this).text(OC.Util.relativeModifiedDate(parseInt($(this).attr('data-timestamp'), 10)))
})
}, 30 * 1000)
}
/**
* Set users locale to moment.js as soon as possible
*/
moment.locale(OC.getLocale())
/**
* Initializes core
*/
export const initCore = () => {
const userAgent = window.navigator.userAgent
const msie = userAgent.indexOf('MSIE ')
const trident = userAgent.indexOf('Trident/')
const edge = userAgent.indexOf('Edge/')
if (msie > 0 || trident > 0) {
// (IE 10 or older) || IE 11
$('html').addClass('ie')
} else if (edge > 0) {
// for edge
$('html').addClass('edge')
}
// css variables fallback for IE
if (msie > 0 || trident > 0 || edge > 0) {
console.info('Legacy browser detected, applying css vars polyfill')
cssVars({
watch: true,
// set edge < 16 as incompatible
onlyLegacy: !(/Edge\/([0-9]{2})\./i.test(navigator.userAgent)
&& parseInt(/Edge\/([0-9]{2})\./i.exec(navigator.userAgent)[1]) < 16),
})
}
$(window).on('unload.main', () => { OC._unloadCalled = true })
$(window).on('beforeunload.main', () => {
// super-trick thanks to http://stackoverflow.com/a/4651049
// in case another handler displays a confirmation dialog (ex: navigating away
// during an upload), there are two possible outcomes: user clicked "ok" or
// "cancel"
// first timeout handler is called after unload dialog is closed
setTimeout(() => {
OC._userIsNavigatingAway = true
// second timeout event is only called if user cancelled (Chrome),
// but in other browsers it might still be triggered, so need to
// set a higher delay...
setTimeout(() => {
if (!OC._unloadCalled) {
OC._userIsNavigatingAway = false
}
}, 10000)
}, 1)
})
$(document).on('ajaxError.main', function(event, request, settings) {
if (settings && settings.allowAuthErrors) {
return
}
OC._processAjaxError(request)
})
initSessionHeartBeat()
OC.registerMenu($('#expand'), $('#expanddiv'), false, true)
// toggle for menus
$(document).on('mouseup.closemenus', event => {
const $el = $(event.target)
if ($el.closest('.menu').length || $el.closest('.menutoggle').length) {
// don't close when clicking on the menu directly or a menu toggle
return false
}
OC.hideMenus()
})
setUpMainMenu()
setUpUserMenu()
setUpContactsMenu()
// move triangle of apps dropdown to align with app name triangle
// 2 is the additional offset between the triangles
if ($('#navigation').length) {
$('#header #nextcloud + .menutoggle').on('click', () => {
$('#menu-css-helper').remove()
const caretPosition = $('.header-appname + .icon-caret').offset().left - 2
if (caretPosition > 255) {
// if the app name is longer than the menu, just put the triangle in the middle
} else {
$('head').append('<style id="menu-css-helper">#navigation:after { left: ' + caretPosition + 'px }</style>')
}
})
$('#header #appmenu .menutoggle').on('click', () => {
$('#appmenu').toggleClass('menu-open')
if ($('#appmenu').is(':visible')) {
$('#menu-css-helper').remove()
}
})
}
$(window).resize(resizeMenu)
setTimeout(resizeMenu, 0)
// just add snapper for logged in users
// and if the app doesn't handle the nav slider itself
if ($('#app-navigation').length && !$('html').hasClass('lte9')
&& !$('#app-content').hasClass('no-snapper')) {
// App sidebar on mobile
const snapper = new Snap({
element: document.getElementById('app-content'),
disable: 'right',
maxPosition: 300, // $navigation-width
minDragDistance: 100,
})
$('#app-content').prepend('<div id="app-navigation-toggle" class="icon-menu" style="display:none" tabindex="0"></div>')
// keep track whether snapper is currently animating, and
// prevent to call open or close while that is the case
// to avoid duplicating events (snap.js doesn't check this)
let animating = false
snapper.on('animating', () => {
// we need this because the trigger button
// is also implicitly wired to close by snapper
animating = true
})
snapper.on('animated', () => {
animating = false
})
snapper.on('start', () => {
// we need this because dragging triggers that
animating = true
})
snapper.on('end', () => {
// we need this because dragging stop triggers that
animating = false
})
// These are necessary because calling open or close
// on snapper during an animation makes it trigger an
// unfinishable animation, which itself will continue
// triggering animating events and cause high CPU load,
//
// Ref https://github.com/jakiestfu/Snap.js/issues/216
const oldSnapperOpen = snapper.open
const oldSnapperClose = snapper.close
const _snapperOpen = () => {
if (animating || snapper.state().state !== 'closed') {
return
}
oldSnapperOpen('left')
}
const _snapperClose = () => {
if (animating || snapper.state().state === 'closed') {
return
}
oldSnapperClose()
}
// Needs to be deferred to properly catch in-between
// events that snap.js is triggering after dragging.
//
// Skipped when running unit tests as we are not testing
// the snap.js workarounds...
if (!window.TESTING) {
snapper.open = () => {
_.defer(_snapperOpen)
}
snapper.close = () => {
_.defer(_snapperClose)
}
}
$('#app-navigation-toggle').click((e) => {
// close is implicit in the button by snap.js
if (snapper.state().state !== 'left') {
snapper.open()
}
})
$('#app-navigation-toggle').keypress(e => {
if (snapper.state().state === 'left') {
snapper.close()
} else {
snapper.open()
}
})
// close sidebar when switching navigation entry
const $appNavigation = $('#app-navigation')
$appNavigation.delegate('a, :button', 'click', event => {
const $target = $(event.target)
// don't hide navigation when changing settings or adding things
if ($target.is('.app-navigation-noclose')
|| $target.closest('.app-navigation-noclose').length) {
return
}
if ($target.is('.app-navigation-entry-utils-menu-button')
|| $target.closest('.app-navigation-entry-utils-menu-button').length) {
return
}
if ($target.is('.add-new')
|| $target.closest('.add-new').length) {
return
}
if ($target.is('#app-settings')
|| $target.closest('#app-settings').length) {
return
}
snapper.close()
})
let navigationBarSlideGestureEnabled = false
let navigationBarSlideGestureAllowed = true
let navigationBarSlideGestureEnablePending = false
OC.allowNavigationBarSlideGesture = () => {
navigationBarSlideGestureAllowed = true
if (navigationBarSlideGestureEnablePending) {
snapper.enable()
navigationBarSlideGestureEnabled = true
navigationBarSlideGestureEnablePending = false
}
}
OC.disallowNavigationBarSlideGesture = () => {
navigationBarSlideGestureAllowed = false
if (navigationBarSlideGestureEnabled) {
const endCurrentDrag = true
snapper.disable(endCurrentDrag)
navigationBarSlideGestureEnabled = false
navigationBarSlideGestureEnablePending = true
}
}
const toggleSnapperOnSize = () => {
if ($(window).width() > breakpointMobileWidth) {
snapper.close()
snapper.disable()
navigationBarSlideGestureEnabled = false
navigationBarSlideGestureEnablePending = false
} else if (navigationBarSlideGestureAllowed) {
snapper.enable()
navigationBarSlideGestureEnabled = true
navigationBarSlideGestureEnablePending = false
} else {
navigationBarSlideGestureEnablePending = true
}
}
$(window).resize(_.debounce(toggleSnapperOnSize, 250))
// initial call
toggleSnapperOnSize()
}
initLiveTimestamps()
PasswordConfirmation.init()
}
|