-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsubmit.js
111 lines (82 loc) · 3.77 KB
/
submit.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
/* jslint browser: true, white: true, sloppy: true, maxerr: 1000 */
/* global L, $, tools, alerts, api, ui, maps, features, forms */
/**
* Saving forms data to the backend
*/
// Save features on the map
var saving = ( function () {
'use strict';
function _saveFeature (formobj, formtype) {
var form_type = tools.capitalizeFirstLetter(formtype);
var token = localStorage.getItem('token') || tools.token;
// Prepare a submission object by checking if the form has any arrayed keys and join them in a string
var submission_obj = tools.joinObjectProperties(formobj);
var params = {
url: api['create' + form_type].url(),
method: api['create' + form_type].method,
auth: 'Bearer ' + token,
data: submission_obj
};
tools.makeApiCall(params, window.fetch)
.catch(function(error) {
console.log(error);
// alerts.showAlert(1, 'danger', 1500, error.message);
ui.sidebar.hide();
tools.resetIconStyle();
})
.then(function(response) {
console.log('post request:', response);
// Remove any unsaved marker
for ( var i in maps.unsavedMarkersLayerGroup._layers ) {
if ( maps.unsavedMarkersLayerGroup._layers.hasOwnProperty(i) ) {
tools.resetIconStyle(i);
maps.map.removeLayer(maps.unsavedMarkersLayerGroup.getLayer(i));
}
}
// Reload map features after an item is saved
// TODO need to change this logic for loading newly created features
// call the route to retrieve data for a single feature after saved
// and only render this particular feature
// features.loadOne();
features.loadFeature(formtype);
alerts.showAlert(25, 'success', 1500);
ui.sidebar.hide();
});
}
function _bindEvents (obj) {
console.log('current formobj: ', obj);
var current_form = obj;
current_form.on('keyup change keydown', function () {
current_form.validator('validate');
});
// Form submission
current_form.validator().on('submit', function (e) {
if ( e.isDefaultPrevented() ) {
// isDefaultPrevented is the way the validator plugin tells sthg is wrong with the form
alerts.showAlert(30, 'danger', 2000);
} else {
e.preventDefault();
// Get the data from the form
var form_name = current_form[0].className;
var form_obj = current_form.serializeObject();
// console.log('Form obj:', form_obj);
var clean_form_obj = tools.deleteEmptyKeys(form_obj);
// console.log('Clean form obj:', clean_form_obj);
// extract the form type from the classname and trim the string
// with Navigo we can omit this because the info will be in the route obj
var form_type = form_name.substr(form_name.lastIndexOf('-') + 1).trim();
// Save the data with ajax
// _saveFeature.bin({formObj: clean_form_obj, formType: form_type})
_saveFeature(clean_form_obj, form_type);
}
});
}
function init () {
// we init this code only when a form is created in forms._bindEvents() in src/js/forms/forms.js
this.form = null;
// Cache the current form, there's always only one .form-feature in the DOM
this.form = $('.form-feature');
_bindEvents(this.form);
}
return { init: init }
}());