-
Notifications
You must be signed in to change notification settings - Fork 92
/
NcAppContent.vue
439 lines (384 loc) · 10.5 KB
/
NcAppContent.vue
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
<!--
- SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
- SPDX-License-Identifier: AGPL-3.0-or-later
-->
<docs>
### General description
This components provides a wrapper around the main app's content.
Single-column layouts can just use the default slot. A resizable column
can be added by providing content to the named slot `list`.
### CSS variables
In the css section some css variables are declared and will be available for
all the children of the NcAppContent component
### Examples
#### Usage: Single-column content
```vue
<template>
<NcAppContent>
<h2>Single-column main content</h2>
</NcAppContent>
</template>
```
#### Usage: Two resizable columns
```vue
<template>
<NcAppContent>
<template #list>
<div>Resizable list content</div>
</template>
<div>Main content</div>
</NcAppContent>
</template>
```
#### Overriding Defaults
The default, min and max sizes (in percent) of the resizable list column can be overridden.
The list size must be between the min and the max width value.
```
<NcAppContent
:list-size="35"
:list-min-width="20"
:list-max-width="45"
>...</NcAppContent>
```
</docs>
<template>
<main id="app-content-vue" class="app-content no-snapper" :class="{ 'app-content--has-list': hasList }">
<h1 v-if="pageHeading" class="hidden-visually">
{{ pageHeading }}
</h1>
<template v-if="hasList">
<!-- Mobile view does not allow resizeable panes -->
<div v-if="isMobile || layout === 'no-split'"
class="app-content-wrapper app-content-wrapper--no-split"
:class="{
'app-content-wrapper--show-details': showDetails,
'app-content-wrapper--show-list': !showDetails,
'app-content-wrapper--mobile': isMobile,}">
<NcAppDetailsToggle v-if="showDetails" @click.native.stop.prevent="hideDetails" />
<slot v-if="!showDetails" name="list" />
<slot v-else />
</div>
<div v-else-if="layout === 'vertical-split' || layout === 'horizontal-split'" class="app-content-wrapper">
<Splitpanes :horizontal="layout === 'horizontal-split'"
class="default-theme"
:class="{ 'splitpanes--horizontal': layout === 'horizontal-split',
'splitpanes--vertical': layout === 'vertical-split'
}"
@resized="handlePaneResize">
<Pane class="splitpanes__pane-list"
:size="listPaneSize || paneDefaults.list.size"
:min-size="paneDefaults.list.min"
:max-size="paneDefaults.list.max">
<!-- @slot Provide a list to the app content -->
<slot name="list" />
</Pane>
<Pane class="splitpanes__pane-details"
:size="detailsPaneSize"
:min-size="paneDefaults.details.min"
:max-size="paneDefaults.details.max">
<!-- @slot Provide the main content to the app content -->
<slot />
</Pane>
</Splitpanes>
</div>
</template>
<!-- @slot Provide the main content to the app content -->
<slot v-if="!hasList" />
</main>
</template>
<script>
import NcAppDetailsToggle from './NcAppDetailsToggle.vue'
import { useIsMobile } from '../../composables/useIsMobile/index.js'
import { getBuilder } from '@nextcloud/browser-storage'
import { useSwipe } from '@vueuse/core'
import { Splitpanes, Pane } from 'splitpanes'
import 'splitpanes/dist/splitpanes.css'
import { emit } from '@nextcloud/event-bus'
const browserStorage = getBuilder('nextcloud').persist().build()
/**
* App content container to be used for the main content of your app
*
*/
export default {
name: 'NcAppContent',
components: {
NcAppDetailsToggle,
Pane,
Splitpanes,
},
props: {
/**
* Allows to disable the control by swipe of the app navigation open state
*/
allowSwipeNavigation: {
type: Boolean,
default: true,
},
/**
* Allows you to set the default width of the resizable list in % on vertical-split
* Allows you to set the default height of the resizable list in % on horizontal-split
* Must be between listMinWidth and listMaxWidth
*/
listSize: {
type: Number,
default: 20,
},
/**
* Allows you to set the minimum width of the list column in % on vertical-split
* Allows you to set the minimum height of the list column in % on horizontal-split
*/
listMinWidth: {
type: Number,
default: 15,
},
/**
* Allows you to set the maximum width of the list column in % on vertical-split
* Allows you to set the maximum height of the list column in % on horizontal-split
*/
listMaxWidth: {
type: Number,
default: 40,
},
/**
* Specify the config key for the pane config sizes
* Default is the global var appName if you use the webpack-vue-config
*/
paneConfigKey: {
type: String,
default: '',
},
/**
* When in mobile view, only the list or the details are shown
* If you provide a list, you need to provide a variable
* that will be set to true by the user when an element of
* the list gets selected. The details will then show a back
* arrow to return to the list that will update this prop to false.
*/
showDetails: {
type: Boolean,
default: true,
},
/**
* Specify the `<h1>` page heading
*/
pageHeading: {
type: String,
default: null,
},
/**
* Content layout used when there is a list together with content:
* - `vertical-split` - a 2-column layout with list and default content separated vertically
* - `no-split` - a single column layout; List is shown when `showDetails` is `false`, otherwise the default slot content is shown with a back button to return to the list.
* - 'horizontal-split' - a 2-column layout with list and default content separated horizontally
* On mobile screen `no-split` layout is forced.
*/
layout: {
type: String,
default: 'vertical-split',
validator(value) {
return ['no-split', 'vertical-split', 'horizontal-split'].includes(value)
},
},
},
emits: [
'update:showDetails',
'resize:list',
],
setup() {
return {
isMobile: useIsMobile(),
}
},
data() {
return {
contentHeight: 0,
hasList: false,
hasContent: false,
swiping: {},
listPaneSize: this.restorePaneConfig(),
}
},
computed: {
paneConfigID() {
// If provided, let's use it
if (this.paneConfigKey !== '') {
return `pane-list-size-${this.paneConfigKey}`
}
try {
// Using the webpack-vue-config, appName is a global variable
// This will throw a ReferenceError when the global variable is missing
// In that case either you provide paneConfigKey or else it fallback
// to a global storage key
return `pane-list-size-${appName}`
} catch (e) {
console.info('[INFO] AppContent:', 'falling back to global nextcloud pane config')
return 'pane-list-size-nextcloud'
}
},
detailsPaneSize() {
if (this.listPaneSize) {
return 100 - this.listPaneSize
}
return this.paneDefaults.details.size
},
paneDefaults() {
return {
list: {
size: this.listSize,
min: this.listMinWidth,
max: this.listMaxWidth,
},
// set the inverse values of the details column
// based on the provided (or default) values of the list column
details: {
size: 100 - this.listSize,
min: 100 - this.listMaxWidth,
max: 100 - this.listMinWidth,
},
}
},
},
updated() {
this.checkSlots()
},
mounted() {
if (this.allowSwipeNavigation) {
this.swiping = useSwipe(this.$el, {
onSwipeEnd: this.handleSwipe,
})
}
this.checkSlots()
this.restorePaneConfig()
},
methods: {
/**
* handle the swipe event
*
* @param {TouchEvent} e The touch event
* @param {import('@vueuse/core').SwipeDirection} direction The swipe direction of the event
*/
handleSwipe(e, direction) {
const minSwipeX = 70
const touchZone = 300
if (Math.abs(this.swiping.lengthX) > minSwipeX) {
if (this.swiping.coordsStart.x < (touchZone / 2) && direction === 'right') {
emit('toggle-navigation', {
open: true,
})
} else if (this.swiping.coordsStart.x < touchZone * 1.5 && direction === 'left') {
emit('toggle-navigation', {
open: false,
})
}
}
},
handlePaneResize(event) {
const listPaneSize = parseInt(event[0].size, 10)
browserStorage.setItem(this.paneConfigID, JSON.stringify(listPaneSize))
this.listPaneSize = listPaneSize
/**
* Emitted when the list pane is resized by the user
*/
this.$emit('resize:list', { size: listPaneSize })
console.debug('AppContent pane config', listPaneSize)
},
// $slots is not reactive, we need to update this manually
checkSlots() {
this.hasList = !!this.$scopedSlots.list
this.hasContent = !!this.$scopedSlots.default
},
// browserStorage is not reactive, we need to update this manually
restorePaneConfig() {
const listPaneSize = parseInt(browserStorage.getItem(this.paneConfigID), 10)
if (!isNaN(listPaneSize) && listPaneSize !== this.listPaneSize) {
console.debug('AppContent pane config', listPaneSize)
this.listPaneSize = listPaneSize
return listPaneSize
}
},
/**
* The user clicked the back arrow from the details view
*/
hideDetails() {
this.$emit('update:showDetails', false)
},
},
}
</script>
<style lang="scss" scoped>
.app-content {
position: initial;
z-index: 1000;
flex-basis: 100vw;
height: 100%;
// Overriding server styles TODO: cleanup!
margin: 0 !important;
background-color: var(--color-main-background);
min-width: 0;
&:not(.app-content--has-list) {
overflow: auto;
}
}
.app-content-wrapper {
position: relative;
width: 100%;
height: 100%;
}
// Mobile list/details handling
.app-content-wrapper--no-split {
&.app-content-wrapper--show-list :deep() {
.app-content-list {
display: flex;
}
.app-content-details {
display: none;
}
}
&.app-content-wrapper--show-details :deep() {
.app-content-list {
display: none;
}
.app-content-details {
display: block;
}
}
}
:deep(.splitpanes.default-theme) {
.app-content-list {
max-width: none;
/* Thin scrollbar is hard to catch on resizable columns */
scrollbar-width: auto;
}
.splitpanes__pane {
background-color: transparent;
transition: none;
&-list {
min-width: 300px;
position: sticky;
@media only screen and (width < $breakpoint-mobile) {
display: none;
}
}
&-details {
overflow-y: auto;
@media only screen and (width < $breakpoint-mobile) {
min-width: 100%;
}
}
}
&.splitpanes--vertical {
.splitpanes__splitter {
background-color: var(--color-main-background);
border-left: 1px solid var(--color-border);
&::before, &::after {
background-color: var(--color-border);
}
}
}
}
.app-content-wrapper--show-list {
:deep(.app-content-list) {
max-width: none;
}
}
</style>