-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathSchemaObjectNode.vue
More file actions
296 lines (264 loc) · 7.88 KB
/
SchemaObjectNode.vue
File metadata and controls
296 lines (264 loc) · 7.88 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
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
<script setup lang="ts">
import {
SchemaElementData,
SchemaObjectAttributeData,
SchemaObjectNodeData,
} from '@/schema/graph-representation/schemaGraphTypes';
import SchemaObjectAttribute from '@/components/panels/schema-diagram/SchemaObjectAttribute.vue';
import type {Path} from '@/utility/path';
import InputText from 'primevue/inputtext';
import {Position, Handle} from '@vue-flow/core';
import {useSettings} from '@/settings/useSettings';
import {ref} from 'vue';
import type {AttributeTypeChoice} from '@/schema/graph-representation/typeUtils';
import Button from 'primevue/button';
import {FontAwesomeIcon} from '@fortawesome/vue-fontawesome';
import {isSubSchemaDefinedInDefinitions} from '@/schema/schemaReadingUtils';
import {getObjectDisplayName} from '@/schema/graph-representation/schemaGraphConstructor';
const props = defineProps<{
data: SchemaObjectNodeData;
targetPosition?: Position;
sourcePosition?: Position;
selectedData?: SchemaElementData;
typeChoices: AttributeTypeChoice[];
}>();
const emit = defineEmits<{
(e: 'select_element', path: Path): void;
(e: 'zoom_into_element', path: Path): void;
(e: 'delete_element', objectData: SchemaElementData): void;
(e: 'extract_inlined_element', objectData: SchemaObjectNodeData): void;
(
e: 'update_object_name',
objectData: SchemaObjectNodeData,
oldName: string,
newName: string
): void;
(
e: 'update_attribute_name',
attributeData: SchemaObjectAttributeData,
oldName: string,
newName: string
): void;
(
e: 'update_attribute_type',
attributeData: SchemaObjectAttributeData,
newType: AttributeTypeChoice
): void;
(e: 'add_attribute', objectData: SchemaObjectNodeData): void;
(
e: 'update_attribute_required',
attributeData: SchemaObjectAttributeData,
required: boolean
): void;
(
e: 'update_attribute_nullable',
attributeData: SchemaObjectAttributeData,
nullable: boolean
): void;
}>();
const objectName = ref(props.data.name || '');
const settings = useSettings();
function updateAttributeRequired(attributeData: SchemaObjectAttributeData, required: boolean) {
emit('update_attribute_required', attributeData, required);
}
function updateAttributeNullable(attributeData: SchemaObjectAttributeData, nullable: boolean) {
emit('update_attribute_nullable', attributeData, nullable);
}
function isObjectEditable() {
return (isHighlighted() || isAttributeHighlighted()) && settings.value.schemaDiagram.editMode;
}
function isNameEditable() {
return isObjectEditable() && props.data.hasUserDefinedName;
}
function isDefinedInDefinitions() {
return isSubSchemaDefinedInDefinitions(props.data.absolutePath);
}
function isRootObject() {
return props.data.absolutePath.length == 0;
}
function isExtractable() {
return !isDefinedInDefinitions() && isObjectEditable() && !isRootObject();
}
function clickedNode() {
emit('select_element', props.data.absolutePath);
}
function doubleClickedNode() {
emit('zoom_into_element', props.data.absolutePath);
}
function clickedAttribute(path: Path) {
emit('select_element', path);
}
function updateObjectName() {
const newName = objectName.value.trim();
if (newName.length == 0) {
return;
}
if (!props.data.name) {
throw new Error(
'Object name is not defined. This should not happen. Only objects with a name should allow edits of the name. Please report this issue.'
);
}
emit('update_object_name', props.data, props.data.name, newName);
}
function updateAttributeName(
attributeData: SchemaObjectAttributeData,
oldName: string,
newName: string
) {
emit('update_attribute_name', attributeData, oldName, newName);
}
function updateAttributeType(
attributeData: SchemaObjectAttributeData,
newType: AttributeTypeChoice
) {
emit('update_attribute_type', attributeData, newType);
}
function deleteObject() {
deleteElement(props.data);
}
function extractInlinedObject() {
emit('extract_inlined_element', props.data);
}
function deleteElement(data: SchemaElementData) {
emit('delete_element', data);
}
function addAttribute() {
emit('add_attribute', props.data);
}
function isHighlighted() {
return props.selectedData && props.selectedData == props.data;
}
function isAttributeHighlighted() {
if (props.selectedData) {
for (const attribute of props.data.attributes) {
if (props.selectedData == attribute) {
return true;
}
}
}
return false;
}
</script>
<template>
<div
:class="{'bg-yellow-100': isHighlighted(), 'vue-flow__node-schemaobject': !isHighlighted}"
@click="clickedNode()"
@click.stop
@dblclick="doubleClickedNode()">
<Handle type="target" :position="props.targetPosition!" class="vue-flow__handle"></Handle>
<div v-if="!isNameEditable() || !isDefinedInDefinitions()">
<b>
{{
getObjectDisplayName(
props.data.name,
props.data.title,
props.data.fallbackDisplayName,
isDefinedInDefinitions()
)
}}
</b>
<Button
v-if="isExtractable()"
class="vue-flow-object-button"
size="small"
v-tooltip.bottom="
'Extract inlined object schema to definitions (will enable renaming and more)'
"
@mousedown.stop
@click.stop
@dblclick.stop
@click="_ => extractInlinedObject()">
<FontAwesomeIcon :icon="'fa-wrench fa-solid'" />
</Button>
</div>
<div v-else>
<InputText
type="text"
class="vue-flow-object-name-inputtext"
v-model="objectName"
@blur="updateObjectName"
@mousedown.stop
@click.stop
@dblclick.stop
@keydown.stop
@keyup.enter="updateObjectName" />
<Button
class="vue-flow-object-button"
size="small"
v-tooltip.bottom="'Delete Object'"
@mousedown.stop
@click.stop
@dblclick.stop
@click="_ => deleteObject()">
<FontAwesomeIcon :icon="'fa-trash fa-solid'" />
</Button>
</div>
<hr />
<SchemaObjectAttribute
v-if="settings.schemaDiagram.showAttributes"
v-for="attribute in props.data!.attributes"
:data="attribute!"
:key="attribute!.name + attribute.index + attribute.typeDescription"
:selected-data="props.selectedData"
:type-choices="props.typeChoices"
@select_element="clickedAttribute"
@update_attribute_name="updateAttributeName"
@update_attribute_type="updateAttributeType"
@update_attribute_required="updateAttributeRequired"
@update_attribute_nullable="updateAttributeNullable"
@delete_element="deleteElement" />
<div v-if="isObjectEditable()">
<Button
size="small"
v-tooltip.bottom="'Add Property'"
@click="_ => addAttribute()"
@click.stop
@dblclick.stop
@mousedown.stop
class="vue-flow-object-button">
<FontAwesomeIcon :icon="'fa-plus fa-solid'" />
</Button>
</div>
<Handle
id="main"
type="source"
:position="props.sourcePosition!"
class="vue-flow__handle"
:style="[props.sourcePosition == Position.Right ? {top: '14px'} : {}]"></Handle>
</div>
</template>
<style>
.vue-flow__node-schemaobject {
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
padding: 4px;
border: 1px solid #ddd;
color: #2e3d49;
background: linear-gradient(135deg, #fafcff 0%, #ffffff 100%);
}
.vue-flow__node-schemaobject:hover {
box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2);
transform: translateY(-2px);
}
.vue-flow__handle {
font-size: 12px;
border: none;
height: unset;
width: unset;
background: transparent;
}
.vue-flow-object-name-inputtext {
height: 26px;
font-size: 14px;
font-weight: bold;
}
.vue-flow-object-button {
font-size: 11px;
position: relative;
border: none; /* Remove border */
background: none; /* Remove background */
padding: 0; /* Remove padding */
margin-left: 5px;
color: black;
}
</style>