-
-
Notifications
You must be signed in to change notification settings - Fork 265
/
farmOS.map.behaviors.move.js
104 lines (84 loc) · 3.13 KB
/
farmOS.map.behaviors.move.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
99
100
101
102
103
104
(function ($) {
farmOS.map.behaviors.move = {
attach: function (instance) {
// Save the instance to this for reference in other methods.
/**
* @todo
* This assumes that there is only one map on the page. It will apply the
* preview layer to the last map.
*/
this.instance = instance;
// Create an editable movement layer.
// Init as an empty vector layer, the layer can be recreated
// as an editable WKT layer later.
var opts = {
title: 'Movement',
color: 'orange',
};
this.movementLayer = this.instance.addLayer('vector', opts);
this.addEditBehaviors();
},
// Update the assets current location map layer.
previewCurrentLocation: function (wkt) {
// Remove current location layer.
if (this.currentLocationLayer) {
this.instance.map.removeLayer(this.currentLocationLayer);
this.currentLocationLayer = null;
}
// Only add a layer if WKT is not an empty string.
if (wkt.length > 0) {
// Create current location layer with the WKT.
// Do not put the layer inside a group, because map.removeLayer() (used
// above) does not recurse into layer groups.
var opts = {
title: 'Current Location',
color: 'blue',
wkt: wkt,
};
this.currentLocationLayer = this.instance.addLayer('wkt', opts);
this.instance.zoomToVectors();
// Add measure behavior.
this.instance.addBehavior('measure', { layer: this.currentLocationLayer });
}
},
// Recreate the Movement map layer.
updateMovementLayer: function (wkt) {
// Remove current location layer.
if (this.movementLayer) {
this.instance.map.removeLayer(this.movementLayer);
this.movementLayer = null;
}
// Create current location layer with the WKT.
// Do not put the layer inside a group, because map.removeLayer() (used
// above) does not recurse into layer groups.
var opts = {
title: 'Movement',
color: 'orange',
wkt: wkt,
};
this.movementLayer = this.instance.addLayer('wkt', opts);
this.addEditBehaviors();
// Zoom to all vector layers.
this.instance.zoomToVectors();
// Save the map instance ID.
const target = this.instance.target;
// Update the data field with the selected areas WKT.
$('#' + target).parent().find('textarea').val(wkt);
// When features are changed in the map, drop the WKT into the data field.
this.instance.edit.wktOn('featurechange', function(wkt) {
$('#' + target).parent().find('textarea').val(wkt);
});
},
// Adds behaviors for editing movements
addEditBehaviors() {
// Make the layer editable.
this.instance.addBehavior('edit', { layer: this.movementLayer });
// Add the snappingGrid behavior.
this.instance.addBehavior('snappingGrid');
// Add measure behavior.
this.instance.addBehavior('measure', { layer: this.movementLayer});
},
// Make sure this runs after farmOS.map.behaviors.wkt.
weight: 101,
};
}(jQuery));