-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcamomile-layer-state.html
161 lines (131 loc) · 4.92 KB
/
camomile-layer-state.html
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
<link rel="import" href="bower_components/polymer/polymer-element.html">
<script src="getElementAt.js"></script>
<dom-module id="camomile-layer-state">
<template>
</template>
<script>
// update/delete/add event/methods + optional sync with server
class CamomileLayerState extends Polymer.Element {
static get is() {
return "camomile-layer-state";
}
constructor() {
super();
}
ready() {
super.ready();
const layerInstance = getElementAt(this.srcLayer);
if(layerInstance.layer)
this._setupLayer(layerInstance);
layerInstance.addEventListener("select", () => this._setupLayer(layerInstance));
layerInstance.addEventListener("deselect", () => this._resetLayer());
const mediumInstance = getElementAt(this.srcMedium);
if(mediumInstance.medium)
this._setupMedium(mediumInstance);
mediumInstance.addEventListener("select", () => this._setupMedium(mediumInstance));
mediumInstance.addEventListener("deselect", () => this._resetMedium());
}
// do it both on local and remote edit + plan offline mode (not real time)
// currently going edit => server then server => event/edit (using wait)
// because add without server isn't really possible currently
addAnnotation(fragment,data) {
return this.client.createAnnotation(this.layer,this.medium,fragment,data);
}
updateAnnotation(id,data) {
return this.client.updateAnnotation(id,data);
}
deleteAnnotation(id) {
return this.client.deleteAnnotation(id);
}
_internalAdd(data,sender) {
const annotationIndex=this.annotations.findIndex(annotation => annotation._id===data._id);
if(annotationIndex!==-1)
return;
this.push("annotations",data);
this.dispatchEvent(new CustomEvent("add_annotation",{detail:{data,sender}}));
}
_internalUpdate(data,sender) {
const annotationIndex=this.annotations.findIndex(annotation => annotation._id===data._id);
this.splice("annotations", annotationIndex, 1,data);
this.dispatchEvent(new CustomEvent("update_annotation",{detail:{data,sender}}));
}
_internalDelete(id,sender) {
const annotationIndex=this.annotations.findIndex(annotation => annotation._id===id);
this.splice("annotations", annotationIndex, 1);
this.dispatchEvent(new CustomEvent("delete_annotation", {detail: {id, sender}}));
}
_setupLayer(layerInstance) {
if(this.layer) {
this.client.unwatchLayer(this.layer);
}
this.client = layerInstance.client;
this.layer=layerInstance.layer;
if(this.medium)
this._populateAnnotations(this.layer,this.medium);
console.log("layer state watch layer",layerInstance.layer);
this.client.watchLayer(layerInstance.layer, (data) => {
try {
const d = JSON.parse(data.data);
if (d.event['update_annotation']) {
this.client.getAnnotation(d.event['update_annotation'])
.then(data => {
return this._internalUpdate(data, 0);
})
.catch(err => console.log(err))
}
if (d.event['delete_annotation']) {
this._internalDelete(d.event['delete_annotation'], 0);
}
if (d.event['add_annotation']) {
this.client.getAnnotation(d.event['add_annotation'])
.then(data => {
return this._internalAdd(data, 0);
})
.catch(err => console.log(err))
}
}
catch(err) {
console.log(err);
}
});
}
_resetLayer() {
this.client= null;
this.layer=null;
this.annotations=null;
this.dispatchEvent(new CustomEvent("reset",{}));
}
_setupMedium(mediumInstance) {
this.client = mediumInstance.client;
this.medium=mediumInstance.medium;
if(this.layer)
this._populateAnnotations(this.layer,this.medium);
}
_resetMedium() {
this.client= null;
this.medium=null;
this.annotations=null;
this.dispatchEvent(new CustomEvent("reset",{}));
}
_populateAnnotations(layer,medium) {
this.client.getAnnotations({filter:{id_layer:layer,id_medium:medium}})
.then(data => {
console.log("annotations",data);
this.annotations=data;
this.dispatchEvent(new CustomEvent("populate_annotations",{detail:{data}}));
})
.catch(err => {
console.log(err);
});
}
static get properties() {
return {
srcLayer: String,
srcMedium: String,
id: String
};
}
}
customElements.define(CamomileLayerState.is, CamomileLayerState);
</script>
</dom-module>