-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathactions.js
377 lines (267 loc) · 11.4 KB
/
actions.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/* jslint browser: true, white: true, sloppy: true, maxerr: 1000 */
/* global L, $, tools, alerts, api, ui, maps, features, forms */
/**
* User actions on the map and on features that are already present
* FIXME event not triggered
*/
var actions = ( function () {
'use strict';
function mapClick (map) {
// this function needs to be public because we should be able to pause the listener (map.off('click')) from elsewhere
console.log('caught a click to map from actions.mapClick()');
// Stop leaflet hash else it overrides the url for the router
maps.hash.stopListening();
// Check that there's not already something else going on in the UI and if yes then we don't do anything with the click
if ( !tools.checkOpenUiElement(map) ) {
return;
}
// Else place a marker on the map
else {
// Make the actual marker and add it to the map
var marker = L.marker(map.latlng, { icon: maps.icons.genericMarker, draggable: false, feature_type: 'generic' });
// Add the marker to the unsaved layer
maps.unsavedMarkersLayerGroup.addLayer(marker);
// Fetch the internal leaflet id
var marker_id = maps.unsavedMarkersLayerGroup.getLayerId(marker);
// Send the marker id to the forms so we can retrieve the object without sending it around
// TODO use setter function for states?
tools.states.currentFeatureId = marker_id;
// Set listeners in the case the marker isn't saved
actions.bindUnsavedMarkerEvents(marker_id);
// Build the route
var route = router.generate('form', {
id: marker_id,
type: 'menu'
});
console.log('route:', route);
// Show the menu with a delay on mobile
if ( window.isMobile ) {
maps.map.panTo(marker.getLatLng());
setTimeout( function () {
router.navigate(route);
}, 1000);
} else {
maps.map.panToOffset(marker.getLatLng(), tools.getHorizontalOffset());
router.navigate(route);
}
}
}
function _unsavedMarkerClick (obj) {
// Clear all the marker icon styles when clicking on a marker when user is doing sthg
// inside a form for another marker else it's possible to modify a new marker with
// another marker not cleared nor saved. This can only happen on desktop.
console.log('unsavedMarkersLayerGroup data: ', maps.unsavedMarkersLayerGroup);
// Stop leaflet hash else it overrides the url for the router
maps.hash.stopListening();
for ( var i in maps.unsavedMarkersLayerGroup._layers ) {
if ( maps.unsavedMarkersLayerGroup._layers.hasOwnProperty(i) ) {
tools.resetIconStyle(i);
}
}
// TODO do we need this sate at all?
tools.states.currentFeatureId = maps.unsavedMarkersLayerGroup.getLayerId(obj.layer);
maps.map.panToOffset(obj.layer.getLatLng(), tools.getHorizontalOffset());
// Build the route
var route = router.generate('form', {
id: tools.states.currentFeatureId,
type: 'menu'
});
console.log('route:', route);
router.navigate(route)
}
function _featureClick (obj) {
console.log("map feature clicked obj: ", obj);
// Stop leaflet hash else it overrides the url for the router
maps.hash.stopListening();
// We must stop the bubbling else the map catches clicks
L.DomEvent.stopPropagation(obj);
var id = obj.layer.options.id;
var type = obj.layer.options.feature_type;
// Build the route from the obj data with the db feature id and feature type
var route = router.generate('feature.show', {
id : id,
type : type
});
console.log('route:', route);
router.navigate(route);
// Center the feature on the map
tools.centerFeatureOnMap(maps.map, obj.layer);
}
function act (type, object/*, action*/) {
// TODO pass by router?
// TODO set navigo links in template
// TODO get type from object
// TODO merge attendCleaning, confirmGarbage and cleanGarbage as they all have the similar mechanism
console.log('action type: ', type);
console.log(object);
type = type.trim();
switch (type) {
case 'cleaned' : _cleanGarbage(object.options);
break;
// case 'cleaned' :
// case 'confirm' :
// case 'attend' : _toggleFeatureState(object.options, type);
// break;
case 'confirm' : _confirmGarbage(object.options);
break;
case 'attend' : _attendCleaning(object.options);
break;
case 'join' : _joinGame(object.options);
break;
// we pass the full leaflet object to the delete method so we can remove it from the map without going through loops
case 'delete' : _deleteFeature(object);
break;
case 'edit' : _editFeature(object.options);
break;
default: return;
}
}
function _editFeature (o) {
// TODO fill the form templates with the current marker data
// TODO more secure way to restrict edition, check session.checkLogin()
// TODO Push the data to the form on .btn-edit click (requires to build all forms with templates)
var user_id = localStorage.getItem('userid'),
user_id_match = e.created_by;
if ( user_id == user_id_match ) {
// TEMPORARY warning about edit system
alerts.showAlert(11, "warning", 3000);
}
else {
alerts.showAlert(9, "danger", 3000);
return;
}
}
function _confirmGarbage (o) {
console.log('event value object options from _confirm garbage: ', o);
var token = localStorage.getItem('token') || tools.token;
var feature_type = tools.capitalizeFirstLetter(o.feature_type);
var params = {
url: api['confirm' + feature_type].url(o.id),
method: 'PUT',
auth: "Bearer" + token
};
tools.makeApiCall(params, window.fetch)
.catch(function(error) {
console.log(error);
}).then(function(response) {
ui.setContent(null, response.data.data);
response.data.message.indexOf('litter') === 0 ? features.loadFeature('litter') : features.loadFeature('garbage');
});
}
function _deleteFeature (o) {
console.log("object passed to function: ", o);
var token = localStorage.getItem('token') || tools.token;
// Check user rights
// TODO warn user before deletion
if ( (o.options.marked_by || o.options.created_by) === parseInt(localStorage.getItem('id'),10) ) {
var type = tools.capitalizeFirstLetter(o.options.feature_type);
var params = {
url: api['delete' + type].url(o.options.id),
method: api['delete' + type].method,
auth: "Bearer " + token
};
tools.makeApiCall(params, window.fetch)
.then(function(response) {
maps.map.removeLayer(o);
alerts.showAlert(7, "success", 1500);
ui.bottombar.hide();
})
.catch(function(err) {
console.log(err);
alerts.showAlert(6, "warning", 2000);
});
} else {
ui.bottombar.hide();
alerts.showAlert(0, "danger", 2500);
return;
}
}
function _attendCleaning (o) {
// TODO merge with confirmGarbage() and change backend route for /attend/
var token = localStorage.getItem('token') || tools.token;
var params = {
url: api.attendCleaning.url(o.id),
method: 'PUT',
auth: "Bearer" + token
};
tools.makeApiCall(params, window.fetch)
.catch(function(error) {
console.log(error);
}).then(function(response) {
ui.setContent(null, response.data.data);
features.loadFeature('cleaning');
});
}
function _cleanGarbage (o) {
var token = localStorage.getItem('token') || tools.token;
var feature_type = tools.capitalizeFirstLetter(o.feature_type);
var params = {
url: api['clean' + feature_type].url(o.id),
method: 'PUT',
auth: "Bearer" + token
};
tools.makeApiCall(params, window.fetch)
.catch(function(error) {
console.log(error);
}).then(function(response) {
ui.setContent(null, response.data.data);
response.data.message.indexOf('litter') === 0 ? features.loadFeature('litter') : features.loadFeature('garbage');
});
}
function _joinGame (o) {
console.log('game not implemented');
return;
}
function _bindEvents () {
// NOTE the event listeners for most map feature actions are set in the ui/ui.js file in ui.setContent()
// because we need to bind the feature data for each action
console.log('binding basic map actions');
maps.map.on('click', mapClick);
// binding the 'click' on the L.clustermarkers only listen for click on actual markers, not the cluster markers
maps.garbageLayerGroup.on('click', _featureClick);
maps.litterLayerGroup.on('click', _featureClick);
// Bind click on standard L.featureGroup
maps.cleaningLayerGroup.on('click', _featureClick);
maps.areaLayerGroup.on('click', _featureClick);
// bind click to unsavedmarkers on larger screens
if ( !window.isMobile ) {
maps.unsavedMarkersLayerGroup.on('click', _unsavedMarkerClick);
}
}
function bindUnsavedMarkerEvents (id) {
var marker = maps.unsavedMarkersLayerGroup.getLayer(id);
var cancel_button = $('.btn-cancel');
// var cancel_button = document.querySelectorAll('.btn-cancel');
ui.sidebar.on ('hide', function () {
tools.resetIconStyle(id);
});
// Close sidebar if cancel button clicked and delete unsaved markers
cancel_button.on('click', function (e) {
// cancel_button.addEventListener('click', function (e) {
e.preventDefault();
ui.sidebar.hide();
if ( id ) {
tools.resetIconStyle(id);
maps.map.removeLayer(marker);
} else {
maps.unsavedMarkersLayerGroup.clearLayers();
}
});
}
function init () {
// Check the map obj is available
if ( maps.map !== undefined ) {
_bindEvents();
} else {
setTimeout( function () {
maps.map.once('ready', _bindEvents);
}, 1000);
}
}
return { mapClick : mapClick
, act : act
, bindUnsavedMarkerEvents : bindUnsavedMarkerEvents
, init : init
, featureClick : _featureClick
}
}());