/
Grid.vue
191 lines (169 loc) · 4.29 KB
/
Grid.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
<template>
<div
v-if="layout.length"
class="w-100"
:class="{
'editable': editable,
'flex-grow-1 d-flex': isStretchable,
}"
>
<grid-layout
:layout.sync="layout"
:col-num="48"
:row-height="10"
vertical-compact
:is-resizable="editable"
:is-draggable="editable"
:cols="columnNumber"
:margin="[0, 0]"
:responsive="!editable"
:use-css-transforms="false"
class="flex-grow-1 d-flex w-100 h-100"
>
<template
v-for="(item, index) in layout"
>
<grid-item
v-if="!blocks[item.i].meta.hidden"
:key="item.i"
ref="items"
:i="item.i"
:h="item.h"
:w="item.w"
:x="item.x"
:y="item.y"
:min-w="6"
:min-h="5"
:class="{ 'h-100': isStretchable }"
class="grid-item"
style="touch-action: none;"
@moved="onBlockUpdated(index)"
@resized="onBlockUpdated(index)"
>
<slot
:block="blocks[item.i]"
:index="index"
:block-index="item.i"
:bounding-rect="boundingRects[index]"
v-on="$listeners"
/>
</grid-item>
</template>
</grid-layout>
</div>
<div
v-else
class="no-builder-grid h-100 pt-5 container text-center"
>
<h4>
{{ $t('noBlock') }}
</h4>
</div>
</template>
<script>
import { GridLayout, GridItem } from 'vue-grid-layout'
import { throttle } from 'lodash'
import { compose } from '@cortezaproject/corteza-js'
export default {
i18nOptions: {
namespaces: 'page',
},
components: {
GridLayout,
GridItem,
},
props: {
blocks: {
type: Array,
default: () => ([]),
},
editable: {
type: Boolean,
},
},
data () {
return {
// all blocks in vue-grid friendly structure
layout: [],
// Grid items bounding rect info
boundingRects: [],
}
},
computed: {
oneBlockLayout () {
return this.blocks.filter(({ meta }) => !meta.hidden).length === 1
},
isStretchable () {
return !this.editable && this.oneBlockLayout
},
columnNumber () {
if (this.oneBlockLayout) {
return { lg: 1, md: 1, sm: 1, xs: 1, xxs: 1 }
}
return { lg: 48, md: 48, sm: 1, xs: 1, xxs: 1 }
},
},
watch: {
blocks: {
immediate: true,
deep: true,
handler (blocks) {
blocks = blocks.map(({ meta, xywh: [x, y, w, h] }, i) => {
// To avoid collision with hidden elements
return meta.hidden ? { i, x: 0, y: 0, w: 0, h: 0 } : { i, x, y, w, h }
})
// Only use next tick if previous layout exists and not editing
if (this.layout.length && !this.editable) {
this.layout = []
// Next tick is important, otherwise it can lead to overlapping blocks
this.$nextTick(() => {
this.layout = blocks
})
} else {
this.layout = blocks
}
},
},
},
mounted () {
window.addEventListener('resize', this.windowResizeThrottledHandler)
this.recalculateBoundingRect()
},
beforeDestroy () {
this.setDefaultValues()
this.destroyEvents()
},
methods: {
windowResizeThrottledHandler: throttle(function () { this.recalculateBoundingRect() }, 500),
// Fetch bounding boxes of all grid items
recalculateBoundingRect () {
this.boundingRects = (this.$refs.items || [])
.map(({ $el }) => {
const { x, y, width: w, height: h } = $el.getBoundingClientRect()
return { x, y, w, h }
})
},
onBlockUpdated (index) {
this.$emit('item-updated', index)
this.$emit('update:blocks', this.layout.map(
({ x, y, w, h, i }) => new compose.PageBlockMaker({ ...this.blocks[i], xywh: [x, y, w, h] }),
))
},
setDefaultValues () {
this.layout = []
this.boundingRects = []
},
destroyEvents () {
window.removeEventListener('resize', this.windowResizeThrottledHandler)
},
},
}
</script>
<style lang="scss">
.editable {
.grid-item {
background-image: linear-gradient(45deg, $gray-200 25%, $white 25%, $white 50%, $gray-200 50%, $gray-200 75%, $white 75%, $white 100%);
background-size: 28.28px 28.28px;
}
}
</style>