This repository has been archived by the owner on Aug 2, 2019. It is now read-only.
forked from leplatrem/daybed-map
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.js
152 lines (135 loc) · 4.93 KB
/
models.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
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
var MapRecord = Daybed.Record.extend({
/**
* Returns instance layer, if its model has a geometry field.
* It basically builds a Leaflet layer, from the coordinates values
* stored in daybed record.
* @returns {L.Layer}
*/
getLayer: function () {
if (!this.layer) {
var geomfield = this.definition.geomField();
if (!geomfield) return;
var factories = {
'point': function (coords) {return L.circleMarker([coords[1], coords[0]]);},
'line': function (coords) {return L.polyline(L.GeoJSON.coordsToLatLngs(coords));},
'polygon': function (coords) {return L.polygon(L.GeoJSON.coordsToLatLngs(coords[0]));}
};
var coords = this.get(geomfield.name);
if (typeof coords === 'string') {
coords = JSON.parse(coords);
}
this.layer = coords ? factories[geomfield.type](coords) : null;
var style = L.Util.extend({}, Daybed.SETTINGS.STYLES['default']);
// Has color ?
var colorField = this.definition.colorField();
if (colorField) {
style.color = this.get(colorField.name);
style.fillColor = style.color;
}
// Has icon ?
var iconField = this.definition.iconField();
if (iconField && 'point' == this.definition.geomField().type) {
var marker = L.AwesomeMarkers.icon({
prefix: 'fa',
markerColor: style.color,
icon: this.get(iconField.name)
});
this.layer = L.marker(this.layer.getLatLng(), {icon: marker});
}
else {
this.layer.setStyle(style);
}
}
return this.layer;
},
/**
* Sets record geometry field from a Leaflet layer.
* @param {L.Layer} layer
*/
setLayer: function (layer) {
var geomfield = this.definition.geomField();
if (!geomfield) return;
var coords = layer.toGeoJSON().geometry.coordinates,
attrs = {};
attrs[geomfield.name] = JSON.stringify(coords);
this.set(attrs);
}
});
var MapRecordList = Daybed.RecordList.extend({
model: MapRecord
});
var MapModel = Daybed.Definition.extend({
metaTypes: _.extend(Daybed.Definition.prototype.metaTypes, {
'color': 'string',
'icon': 'string'
}),
recordSchema: function () {
var geom = function (f) {
return {type: 'TextArea',
editorAttrs: {style: 'display: none'},
help: (f.label || '') + ' <span>(on map)</span>'};
};
var fieldMapping = {
'color': function () {
return { type: 'Select', options: [
'red', 'blue', 'orange', 'green', 'purple',
'darkred', 'darkgreen', 'darkblue', 'darkpurple', 'cadetblue'
] };
},
'icon': function () {
return { type: 'Select', options: [
{group: 'Location',
options: ['home', 'music', 'medkit', 'camera-retro',
'info-sign', 'plane', 'shopping-cart']},
{group: 'Food & Drink',
options: ['food', 'glass', 'coffee']},
{group: 'Symbols',
options: ['flag', 'star', 'suitcase', 'comments']}
] };
},
'point': geom,
'line': geom,
'polygon': geom
};
var schema = Daybed.Definition.prototype.recordSchema.call(this);
$(this.attributes.fields).each(function (i, field) {
var build = fieldMapping[field.meta || field.type];
if (build) {
schema[field.name] = build(field);
}
});
return schema;
},
fields: function () {
var fields = Daybed.Definition.prototype.fields.call(this);
var geomField = this.geomField();
if (!geomField)
return fields;
return fields.filter(function (f) {
return f.name != geomField.name;
});
},
/**
* Returns the first field whose type is Geometry.
* @returns {string} ``null`` if no geometry field in *Definition*
*/
geomField: function () {
for (var i in this.attributes.fields) {
var f = this.attributes.fields[i];
if (f.type == 'point' || f.type == 'line' || f.type == 'polygon') {
return f;
}
}
return null;
},
_getFields: function (metatype) {
return _.filter(this.attributes.fields,
function(f) { return f.meta == metatype; });
},
colorField: function () {
return this._getFields('color')[0];
},
iconField: function () {
return this._getFields('icon')[0];
}
});