-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodels.js
131 lines (117 loc) · 4.14 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
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 = factories[geomfield.type](coords);
}
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.description + ' <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;
},
/**
* Returns field names that are not of type geometry.
* @returns {Array[string]}
*/
mainFields: function () {
var geomField = this.geomField();
if (!geomField)
return this.attributes.fields;
return this.attributes.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];
}
});