/
mapiconmapobjectgroup.js
98 lines (82 loc) · 3.32 KB
/
mapiconmapobjectgroup.js
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
// Filled from the _fetchSuccess() function of this class
let MAP_ICON_TYPES = [];
/**
* Get the Map Icon Type for an ID in the MAP_ICON_TYPES array.
* @param mapIconTypeId
* @returns {null}
*/
function getMapIconType(mapIconTypeId) {
let mapIconType = null;
for (let i = 0; i < MAP_ICON_TYPES.length; i++) {
if (MAP_ICON_TYPES[i].id === mapIconTypeId) {
mapIconType = MAP_ICON_TYPES[i];
break;
}
}
return mapIconType;
}
class MapIconMapObjectGroup extends MapObjectGroup {
constructor(manager, name, editable) {
super(manager, name, editable);
let self = this;
this.title = 'Hide/show map icons';
this.fa_class = 'fa-icons';
if (this.manager.map.options.echo) {
window.Echo.join('route-edit.' + this.manager.map.getDungeonRoute().publicKey)
.listen('.mapicon-changed', (e) => {
self._restoreObject(e.mapicon, e.user);
})
.listen('.mapicon-deleted', (e) => {
let mapObject = self.findMapObjectById(e.id);
if (mapObject !== null) {
mapObject.localDelete();
self._showDeletedFromEcho(mapObject, e.user);
}
});
}
}
_createObject(layer) {
console.assert(this instanceof MapIconMapObjectGroup, 'this is not an MapIconMapObjectGroup', this);
if (isMapAdmin) {
return new AdminMapIcon(this.manager.map, layer);
} else {
return new MapIcon(this.manager.map, layer);
}
}
_restoreObject(remoteMapObject, username = null) {
console.assert(this instanceof MapIconMapObjectGroup, 'this is not a MapIconMapObjectGroup', this);
// Fetch the existing map comment if it exists
let mapIcon = this.findMapObjectById(remoteMapObject.id);
// Only create a new one if it's new for us
let mapIconType = getMapIconType(remoteMapObject.map_icon_type_id);
if (mapIcon === null) {
// Find the layer we should display on the map
let layer = new LeafletMapIconMarker();
layer.setLatLng(L.latLng(remoteMapObject.lat, remoteMapObject.lng));
mapIcon = this.createNew(layer);
}
mapIcon.id = remoteMapObject.id;
mapIcon.floor_id = remoteMapObject.floor_id;
mapIcon.map_icon_type_id = remoteMapObject.map_icon_type_id;
mapIcon.has_dungeon_route = remoteMapObject.has_dungeon_route;
mapIcon.comment = remoteMapObject.comment;
mapIcon.setMapIconType(mapIconType);
// We just downloaded the map icon, it's synced alright!
mapIcon.setSynced(true);
// Show echo notification or not
this._showReceivedFromEcho(mapIcon, username);
}
_fetchSuccess(response) {
super._fetchSuccess(response);
// no super call required
console.assert(this instanceof MapIconMapObjectGroup, 'this is not a MapIconMapObjectGroup', this);
MAP_ICON_TYPES = response.mapicontypes;
let mapIcons = response.mapicon;
// Now draw the patrols on the map
for (let index in mapIcons) {
if (mapIcons.hasOwnProperty(index)) {
this._restoreObject(mapIcons[index]);
}
}
}
}