-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathGroup.vue
More file actions
222 lines (218 loc) · 9.39 KB
/
Copy pathGroup.vue
File metadata and controls
222 lines (218 loc) · 9.39 KB
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
<template>
<div class="nrdb-layout-group--grid" :style="`grid-template-columns: repeat(min(${ columns }, var(--layout-columns)), 1fr); `">
<div
v-if="resizable" ref="group-resize-view" class="nrdb-resizable" :class="{'resizing': groupResizing.active}"
:style="{ 'width': groupResizing.current.width ? `${groupResizing.current.width}px` : null, 'z-index': 99 }"
>
<div
draggable="true"
class="nrdb-resizable--handle nrdb-resizable--right"
@dragstart="onHandleDragStart($event, index, 'group', group, 'top', 'right')"
@drag="onHandleDrag($event, index, 'group', group, 'top', 'right')"
@dragover="onHandleOver($event, index, 'group', group, 'top', 'right')"
@dragend="onHandleDragEnd($event, index, 'group', group, 'top', 'right')"
@dragenter.prevent
/>
<!-- Add Spacer button -->
<div class="nrdb-resizable--toolbar">
<v-btn v-tooltip:bottom="'Add Spacer'" icon="mdi-card-plus-outline" size="small" variant="text" class="nrdb-resizable--toolbar-button" @click="addSpacer" />
</div>
</div>
<div
v-for="(w, $index) in widgets"
:id="'nrdb-ui-widget-' + w.id"
:key="w.id"
:draggable="resizable"
class="nrdb-ui-widget"
:class="getWidgetClass(w)"
:style="[{display: 'grid', 'grid-template-columns': 'minmax(0, 1fr)', 'gap': 'var(--widget-gap)'}, widgetStyles(w)]"
@dragstart="!resizable ? null : onWidgetDragStart($event, $index, w)"
@dragover="!resizable ? null : onWidgetDragOver($event, $index, w)"
@dragend="!resizable ? null : onWidgetDragEnd($event, $index, w)"
@dragleave="!resizable ? null : onWidgetDragLeave($event, $index, w)"
@drop.prevent
@dragenter.prevent
>
<!-- <div style="font-size: small; background-color: aquamarine;">w.props.height: {{ w.props.height }}</div> -->
<component :is="w.component" :id="w.id" ref="widget-content" :props="w.props" :state="w.state" :style="`grid-row-end: span ${effectiveHeight(w)}`" />
<div
v-if="resizable && !groupDragging" ref="widget-resize-view"
class="nrdb-resizable nrdb-resizable-widget"
:class="widgetResizing.widgetId === w.id || widgetDragging.widgetId === w.id ? getWidgetEditingClass(w) : null"
style="z-index: 100; min-width: 28px; min-height: 28px;"
:style="widgetResizing.widgetId === w.id ? getWidgetEditingStyle(w) : null"
>
<!-- Delete Spacer button -->
<div v-if="w.type === 'ui-spacer'" class="nrdb-resizable--toolbar">
<v-btn v-tooltip:bottom="'Delete Spacer'" icon="mdi-card-minus-outline" size="small" variant="text" class="nrdb-resizable--toolbar-button" @click="removeWidget(w)" />
</div>
<div
draggable="true"
class="nrdb-resizable--handle nrdb-resizable--right"
@dragstart="onHandleDragStart($event, index, 'widget', w, 'ew')"
@drag="onHandleDrag($event, index, 'widget', w, 'ew')"
@dragover="onHandleOver($event, index, 'widget', w, 'ew')"
@dragend="onHandleDragEnd($event, index, 'widget', w, 'ew')"
@dragenter.prevent
@dblclick="w.props.width = ''; w.layout.width = '3'"
/>
<div
draggable="true"
class="nrdb-resizable--handle nrdb-resizable--bottom"
@dragstart="onHandleDragStart($event, index, 'widget', w, 'ns')"
@drag="onHandleDrag($event, index, 'widget', w, 'ns')"
@dragover="onHandleOver($event, index, 'widget', w, 'ns')"
@dragend="onHandleDragEnd($event, index, 'widget', w, 'ns')"
@dragenter.prevent
@dblclick="w.props.height = ''; w.layout.height = '1'"
/>
</div>
</div>
<img ref="blank-img" style="position: absolute;" src="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==" alt="">
</div>
</template>
<script>
import WYSIWYG from './wysiwyg/index.js'
function hasNumberProperty (obj, prop) {
return Object.prototype.hasOwnProperty.call(obj, prop) && !isNaN(+obj[prop])
}
export default {
name: 'WidgetGroup',
mixins: [WYSIWYG],
props: {
group: {
type: Object,
required: true
},
index: {
type: Number,
required: true
},
widgets: {
type: Array,
required: true
},
resizable: {
type: Boolean,
default: false
},
groupDragging: {
type: Boolean,
default: false
}
},
emits: ['resize', 'widget-added', 'widget-removed', 'refresh-state-from-store'],
data () {
return {
localWidgets: null
}
},
computed: {
columns () {
return this.groupResizing.current.columns > 0 ? this.groupResizing.current.columns : +this.group.width
},
widgetStyles () {
return (widget) => {
const styles = {}
const height = this.effectiveHeight(widget)
const width = widget.props.width
styles['grid-row-end'] = `span ${height}`
styles['grid-template-rows'] = `repeat(${height}, var(--widget-row-height))`
styles['grid-column-end'] = `span min(${this.getWidgetWidth(+width)}, var(--layout-columns))`
return styles
}
}
},
methods: {
effectiveHeight (widget) {
const height = widget.props.height
if (!height || Number(height) === 0) {
if (widget.type === 'ui-form') {
return (widget.props.options.length / (widget.props.splitLayout ? 2 : 1)) + 2
}
if (widget.type === 'ui-chart') {
return 8
}
if (widget.type === 'ui-gauge' && widget.props.gtype === 'gauge-34') {
return 3
}
}
return height
},
getWidgetClass (widget) {
const classes = []
// ensure each widget has a class for its type
classes.push(`nrdb-${widget.type}`)
if (widget.props.className) {
classes.push(widget.props.className)
}
if (widget.state.class) {
classes.push(widget.state.class)
}
// hide the widget if required
if (this.resizable === false && widget.state.visible === false) {
classes.push('d-none')
}
return classes.join(' ')
},
getWidgetEditingClass (widget) {
const classes = []
// dragging interaction classes
const dragClass = this.resizable ? this.getWidgetDragDropClass(widget) : null // Mixin method
if (dragClass) {
classes.push(dragClass)
}
const resizeClass = this.getWidgetResizingClass(widget) // Mixin method
if (resizeClass) {
classes.push(resizeClass)
}
return classes.join(' ')
},
getWidgetEditingStyle (widget) {
if (!this.widgetResizing.active || this.resizeType !== 'widget') {
return null
}
const style = { }
if (this.resizeMode === 'ew' && hasNumberProperty(this.widgetResizing.current, 'width')) {
style.width = `${this.widgetResizing.current.width}px`
}
if (this.resizeMode === 'ns' && hasNumberProperty(this.widgetResizing.current, 'height')) {
style.height = `${this.widgetResizing.current.height}px`
}
return style
},
getWidgetWidth (width) {
const w = +width // convert to number if it's a string
if (!isNaN(w) && w > 0) {
return Math.min(width, this.columns)
}
return this.columns
},
addSpacer () {
this.$store.dispatch('wysiwyg/addSpacer', {
group: this.group.id,
name: 'spacer',
order: this.widgets.length + 1 || 0,
height: 1, // consider taking height and width of last widget or last spacer in group?
width: 1
}).then((newWidget) => {
console.log('New Widget:', newWidget)
this.$emit('widget-added', { widget: newWidget, group: this.group })
}).catch((error) => {
console.error('Error adding spacer:', error)
})
},
removeWidget (widget) {
this.$store.dispatch('wysiwyg/removeWidget', { id: widget.id }).then(() => {
console.log('Widget removed')
this.$emit('widget-removed', { widget })
}).catch((error) => {
console.error('Error deleting widget:', error)
})
}
}
}
</script>
<style scoped lang="scss">
@use './wysiwyg/resizable.scss' as *;
</style>