-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalpaca-extra.js
239 lines (226 loc) · 7.49 KB
/
alpaca-extra.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// some extra goodies for alpaca
/* making a Date Range Picker field */
$.alpaca.Fields.DateRangeField = $.alpaca.Fields.DatetimeField.extend({
getFieldType: function() {
return 'daterange';
},
afterRenderControl: function(model, callback) {
var self = this;
if (self.view.type !== "display")
{
if ($.fn.daterangepicker)
{
/* make sure it updates correctly if you want to show placeholder text */
if (self.options.placeholder) {
self.options.picker['autoUpdateInput'] = false;
};
self.getControlEl().daterangepicker(self.options.picker);
// alright the autoupdate is ok again:
self.getControlEl().data('daterangepicker').autoUpdateInput = true;
self.picker = self.getControlEl().data('daterangepicker');
/* if (self.picker && self.options.dateFormat)
{
self.picker.format(self.options.dateFormat);
}
if (self.picker)
{
self.options.dateFormat = self.picker.format();
}*/
// with daterangepicker, trigger change using plugin
self.control.on("apply.daterangepicker", function(e) {
// we use a timeout here because we want this to run AFTER control click handlers
setTimeout(function() {
self.onChange.call(self, e);
self.triggerWithPropagation("change", e);
}, 250);
});
// set value if provided
/* if (self.data) {
self.picker.date(self.data);
}*/
}
}
callback();
},
});
$.alpaca.registerFieldClass('daterange', $.alpaca.Fields.DateRangeField);
// have it use the date-time template be default
$.alpaca.registerDefaultFormatFieldMapping("date-time", "daterange");
/* a selectlist with integrated leaflet map */
$.alpaca.Fields.LeafletSelect = $.alpaca.Fields.SelectField.extend({
getFieldType: function() {
return 'leaflet-select';
},
clickMarker: function(e) {
var marker = e.target;
var marker_val = marker['value'];
var event = e.originalEvent;
var shift = event.shiftKey || event.ctrlKey || event.metaKey;
var selected_sites = this.getValue();
if (selected_sites) {
var marker_is_selected = selected_sites.indexOf(marker_val) != -1;
} else {
var marker_is_selected = false;
};
if (shift && marker_is_selected) {
this.deselectMarker(marker);
} else {
this.selectMarker(marker, shift);
};
if (this.options.multiple && $.fn.multiselect && !this.isDisplayOnly()) {
$(this.getControlEl()).multiselect('refresh');
}
this.control.change();
},
selectMarker: function(marker, shift) {
/* when the marker is selected the style should
change and the corresponding list item should be
selected, then a change event is called */
var selected_vals = this.getValue();
var marker_val = marker['value'];
marker.setStyle(this.options.selectedMarker);
if (shift && selected_vals.indexOf(marker_val) == -1) {
selected_vals.push(marker_val);
} else {
selected_vals = [marker_val];
};
this.setValue(selected_vals);
},
deselectMarker: function(marker) {
marker.setStyle(this.options.marker);
var marker_val = marker['value'];
var new_vals = [];
$.each(this.getValue(), function(i, val) {
if (val != marker_val) {
new_vals.push(val);
};
});
this.setValue(new_vals);
},
getControlEl: function()
{
return this.control.slice(0, 1);
},
makeMap: function() {
/* organize the markers */
this.markers = L.featureGroup();
var locations = this.options.locations;
var selected_sites = this.getValue();
var markerFun = this.options.markerFunction || L.circleMarker;
if (locations) {
// (this loop will break if locations haven't been provided)
$.each(this.getEnum(), function(i, site) {
if (locations[site]) {
var marker = markerFun(locations[site], this.options.marker);
marker['value'] = site;
marker.on('click', this.clickMarker.bind(this));
if (selected_sites && selected_sites.indexOf(marker['value']) != -1) {
marker.setStyle(this.options.selectedMarker);
};
marker.bindTooltip(this.options.optionLabels[i]);
this.markers.addLayer(marker);
};
}.bind(this));
};
var map_div = this.containerItemEl[0].getElementsByClassName('alpaca-form-leaflet-select-map')[0];
var map_options = this.options.map_options;
/* remove existing map if needed */
if (this.map) {
this.map.remove();
this.map = null;
}
console.log('beforemap');
this.map = L.map(map_div, map_options).fitBounds(this.markers.getBounds().pad(.05));
console.log('aftermap');
if (this.options.tile) {
};
L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=pk.eyJ1Ijoic2tlcHRpa29zIiwiYSI6ImNqNWU2NjNhYzAwcDEycWpqdTJtNWJmNGYifQ.kxK-j2hWsX46EhH5PnsTfA', {
maxZoom: 18,
id: 'mapbox.streets'
}).addTo(this.map);
this.markers.addTo(this.map);
},
selectAfterRenderControl: $.alpaca.Fields.SelectField.prototype.afterRenderControl,
afterRenderControl: function(model, callback)
{
console.log('afterrender');
var self = this;
if (this.options.hideSelect) {
if (this.options.multiple && $.fn.multiselect && !this.isDisplayOnly()) {
this.selectAfterRenderControl(model, callback);
$(this.field).find('button').hide();
} else {
$(this.control[0]).hide();
this.selectAfterRenderControl(model, callback);
};
} else {
this.selectAfterRenderControl(model, callback);
};
/* initialize the map when added to the display */
if (!this._constructing) {
this.makeMap();
} else {
this.on('ready', function() {
console.log('ready');
this.makeMap();
}.bind(this));
};
},
onChange: function(e) {
// ignore events coming from the map
var target_tag = e.target.tagName;
if (target_tag == 'SELECT' || target_tag == 'BUTTON') {
var selected_sites = this.getValue() || [];
$.each(this.markers.getLayers(), function(i, marker) {
/* set the appropriate styles */
if (selected_sites.indexOf(marker['value']) == -1) {
marker.setStyle(this.options.marker);
} else {
marker.setStyle(this.options.selectedMarker);
};
}.bind(this));
};
},
selectSetValue: $.alpaca.Fields.SelectField.prototype.setValue,
setValue: function(val) {
this.selectSetValue(val);
// update the map
var selected_sites = this.getValue();
$.each(this.markers.getLayers(), function(i, marker) {
/* set the appropriate styles */
if (selected_sites && selected_sites.indexOf(marker['value']) != -1) {
marker.setStyle(this.options.selectedMarker);
} else {
marker.setStyle(this.options.marker);
};
}.bind(this));
}
});
$.alpaca.registerFieldClass('leaflet-select', $.alpaca.Fields.LeafletSelect);
/* add the leaflet template to bootstrap-edit */
$.alpaca.registerView({
"id": 'bootstrap-edit-extra',
"parent": 'bootstrap-edit',
"templates": {
'control-leaflet-select': '<select id="{{id}}" {{#if options.readonly}}readonly="readonly"{{/if}} {{#if options.multiple}}multiple="multiple"{{/if}} {{#if options.size}}size="{{options.size}}"{{/if}} {{#if name}}name="{{name}}"{{/if}}> \
{{#if options.multiple}} \
{{#if options.hideNone}} \
{{else}} \
<option value="">{{{options.noneLabel}}}</option> \
{{/if}} \
{{#each selectOptions}} \
<option value="{{{value}}}" {{#if selected}}selected="selected"{{/if}}>{{text}}</option> \
{{/each}} \
{{else}} \
{{#if options.hideNone}} \
{{else}} \
<option value="">{{{options.noneLabel}}}</option> \
{{/if}} \
{{#each selectOptions}} \
<option value="{{{value}}}" {{#if selected}}selected="selected"{{/if}}>{{text}}</option> \
{{/each}} \
{{/if}} \
</select> \
<div id="map-{{id}}" class="alpaca-form-leaflet-select-map"></div>'
}
});