-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathApp.vue
345 lines (326 loc) · 10.9 KB
/
App.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
<script setup>
import './assets/tailwind.css';
import { objectTypelookup, lookupPageSize } from './utils/lookupTables';
import { GridLayer } from './draw/GridLayer';
import ScaleStage from './draw/ScaleStage';
import Exporter from './exporter/Exporter';
import Importer from './exporter/Importer';
import haspTheme from './Hasp/HaspTheme';
</script>
<template>
<div class="flex bg-gray-900">
<aside class="w-60 bg-gray-900 space-y-2 pl-2 pr-2">
<div class="sticky top-0 bg-gray-900 pb-2 z-40 space-y-2 ">
<form-label name="components" />
<!-- <form-button icon="envelope-open-text" @click="addPage">Page</form-button> -->
<div class="grid grid-cols-3 gap-2">
<form-button :icon="lookupIcon['page'].icon" :docicon="lookupIcon['page'].docicon"
:title="lookupIcon['page'].title" @click="addPage">Page</form-button>
<div v-for="(name, obj) in objectTypelookup" :key=obj>
<!-- <form-button color="gray" :icon=lookupIcon[obj] @click="addObject(obj)">{{ obj }}</form-button> -->
<form-button color="gray" :icon=lookupIcon[obj].icon :docicon=lookupIcon[obj].docicon
:title=lookupIcon[obj].title @click="addObject(obj)">{{ obj }}</form-button>
</div>
</div>
<form-button color="danger" title="Select the object to be deleted first." icon="media/icons/trash.png"
@click="deleteObject">delete</form-button>
<div class="grid grid-cols-2 gap-2">
<form-button color="success" icon="media/icons/import.png"
title="Import a valid OpenHASP file. Pages and objects are automaticaly created"
@click="$refs.file.click()">import</form-button>
<form-button color="success" icon="media/icons/export.png" title="Export the design to an openHASP file."
@click="exportData">export</form-button>
</div>
<input class="hidden" @change="importFile" ref="file" type="file">
</div>
<div class="w-45 space-y-2">
<Accordion class="pr-2" title="Settings">
<form-input name="Project name" type="text" v-model="projectName" />
<form-select name="page-size" v-model="pageSize">
<option v-for="spec, ref in lookupPageSize" :value="ref">{{ spec.title }}</option>
</form-select>
<form-input name="page width" type="number" min="240" max="800" v-model="layer.pageWidth" />
<form-input name="page height" type="number" min="240" max="800" v-model="layer.pageHeight" />
<form-input name="grid" type="number" min="5" max="20" step="5" v-model="gridSize"
v-on:input="changeGridSize($event.target.value)" />
<div class="flex ml-2 mt-2">
<!-- <form-checkbox v-model="showid" v-on:change="showId($event)" /> -->
<form-checkbox v-model="layer.showId" v-on:change="layer.showObjectId($event)" />
<form-label class="ml-2">show id</form-label>
</div>
<!-- <form-select name="theme" v-model="haspTheme">
<option value="haspdark">Hasp Dark</option>
<option value="hasplight">Hasp Light</option>
<option value="mono">Mono</option>
<option value="materialdark">Material Dark</option>
<option value="materiallight">Material Light</option>
</form-select> -->
<div class="grid grid-cols-2 justify-items-end m-2 content-end">
<form-label>primary-color</form-label>
<div class="justify-items-end">
<ColorPicker v-model:pureColor="theme.primary_color" useType="pure" format="hex8" lang="En" shape="circle"
v-on:pureColorChange="theme.colorChange('primary_color', $event)" />
</div>
</div>
<div class="grid grid-cols-2 justify-items-end m-2 content-end">
<form-label>secondary-color</form-label>
<div class="justify-items-end">
<ColorPicker v-model:pureColor="secondaryColor" useType="pure" format="hex8" lang="En" shape="circle" />
</div>
</div>
</Accordion>
<PropertiesPanel v-model:node="selectedNode" v-if="selectedNode" />
</div>
</aside>
<div class="h-screen sticky top-0 md:container rounded-md ml-2 bg-yellow-50 cursor-pointer " id="konva_container">
</div>
</div>
<Modal v-model:show="showModal" width="large">
<h3 class="text-xl leading-6 font-medium text-gray-900">
Design export
</h3>
<form class="mt-7 w-full">
<form-group>
<form-checkbox label="homeassistant config" v-model=hassExport />
<form-label>homeassistant config</form-label>
</form-group>
<form-group>
<form-textarea class="mb-4 " type="text" readonly rows="20" wrap="off" v-model="exportDataText" />
</form-group>
<form-button @click="exportDataTofile(true)">
Save to file →
</form-button>
</form>
</Modal>
</template>
<script>
import Konva from 'konva';
import { lookupIcon, lookupPageSize } from './utils/lookupTables';
import Modal from './components/Modal.vue';
import PropertiesPanel from './components/PropertiesPanel.vue'
import Accordion from './components/Accordion.vue';
import { ColorPicker } from "vue3-colorpicker";
import "vue3-colorpicker/style.css";
const width = window.innerWidth;
const height = window.innerHeight;
var colors = '#194d33';
export default {
components: {
Modal,
Accordion,
ColorPicker,
},
data() {
return {
projectName: 'openhasp',
pageSize: "large_portrait",
// pageWidth: 320,
// pageHeight: 480,
haspTheme: 'materialdark',
pageColor: '#4b4b4bff',
primaryColor: '#42a5f6ff',
secondaryColor: '#fb8c00ff',
stage: undefined,
stageScale: 1,
position: { x: 0, y: 0 },
layer: undefined,
text: '',
gridSize: 10,
haspId: 0,
showModal: false,
selectedNode: null,
theme: new haspTheme(),
exportDataText: '',
hassExport: false,
showid: false,
};
},
mounted: function () {
let container = document.querySelector('#konva_container');
this.stage = new ScaleStage({
container: 'konva_container',
width: container.offsetWidth,
height: container.offsetHeight,
draggable: true,
grid_size: this.gridSize,
callback: this.stageCallback,
});
this.layer = new GridLayer({
x: 0,
y: 0,
draggable: false,
grid_size: this.gridSize,
callback: this.layerCallback,
theme: this.theme,
});
this.setPageSize();
this.stage.add(this.layer);
// window.addEventListener('resize', this.fitStageIntoParentContainer);
},
watch: {
readiusProperty: function () {
if (this.readiusProperty < 0)
this.readiusProperty = 0;
this.layer.setNodeProperties({ radius: Number(this.readiusProperty) });
},
borderWidthProperty: function () {
if (this.borderWidthProperty < 0)
this.borderWidthProperty = 0;
this.layer.setNodeProperties({ border_width: Number(this.borderWidthProperty) })
},
haspId: function () {
this.layer.setNodeProperties({ haspid: Number(this.haspId) })
},
text: function () {
this.layer.setNodeProperties({ text: this.text })
},
color: function () {
this.layer.setObjectColor(this.color);
},
selectedNode: function () {
// console.log(this.selectedNode)
},
pageSize: {
handler: function () {
this.setPageSize();
}
},
},
methods: {
setPageSize: function () {
const size = lookupPageSize[this.pageSize];
if (this.layer) {
this.layer.pageWidth = size.w;
this.layer.pageHeight = size.h;
}
},
changeGridSize: function () {
if (this.stage != undefined)
this.stage.setGridSize(this.gridSize);
},
closeModal() {
this.showModal = false;
},
exportData(e) {
const exp = this.exportDataTofile(false);
this.exportDataText = exp.jsonl;
this.showModal = true;
},
exportDataTofile(doSave) {
this.showModal = false;
const settings = {
project_name: this.projectName,
page_size: this.pageSize,
}
const exporter = new Exporter();
// console.log(this.hassExport)
let exp = exporter.doExport(settings, this.layer, doSave);
exporter.hassExport(settings,this.layer);
return exp
},
callback(result) {
// console.log(result)
if (result.project_name)
this.projectName = result.project_name;
if (result.page_size)
this.pageSize = result.page_size
},
importFile() {
this.file = this.$refs.file.files[0];
if (!this.file) {
return this.parseError('Selecteer een bestand om deze te importeren.');
}
const importer = new Importer()
importer.doImport(this.file, this.layer, this.callback);
},
addPage(e) {
this.layer.addPage({ type: "page", bg_color: this.pageColor });
},
addObject(type) {
// console.log('ADD')
// console.log(type)
this.layer.addObject({ type: type })
},
deleteObject(e) {
if (this.selectedNode.canDelete) {
this.layer.deleteSelectedNodes();
this.selectedNode = undefined;
}
},
showId(e) {
this.layer.showObjectId(e);
},
stageCallback(data) {
if (data.scale)
this.stageScale = data.scale;
},
layerCallback(node) {
this.selectedNode = undefined;
this.selectedNode = node;
if (node.isType("page")) {
this.haspId = node.haspid;
this.showPanel = true;
} else {
// console.log('set properties')
this.showPanel = false;
this.readiusProperty = node.getRadius();
this.borderWidthProperty = node.getBorderWidth();
this.position = { pos: node.position(), size: node.getSize() };
if (node.isType('btn')) {
this.text = node.text;
}
}
},
fitStageIntoContainer() {
const container = document.querySelector('#konva_container');
// console.log(container)
this.stage.width(container.offsetWidth);
this.stage.height(container.offsetHeight);
},
addServerNode(n) {
// console.log("tata");
if (n.children && n.children.length > 0) return;
const id = `${Date.now()}`;
const newNode = {
text: `loaded from server`,
children: [],
state: {},
};
// add the node to nodes
this.nodes[id] = newNode;
// set children
n.children = [id];
// end loading
n.state.isLoading = false;
},
},
};
</script>
<style>
.container {
position: relative;
width: 50%;
}
.image {
display: block;
width: 100%;
height: auto;
}
.overlay {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
height: 100%;
width: 100%;
opacity: 0;
transition: .3s ease;
background-color: #275bec;
color: white;
border-radius: 10px;
}
.container:hover .overlay {
opacity: 0.8;
}
</style>