This repository has been archived by the owner on Jun 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.js
127 lines (114 loc) · 5.15 KB
/
main.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
requirejs.config( {
baseUrl: '../../src/js/',
paths: {
'enketo-js': '../../lib/enketo-core/src/js',
'enketo-widget': '../../lib/enketo-core/src/widget',
'enketo-config': '../../config.json',
'enketo-json': '../../lib/enketo-json/src',
'jquery.xpath': '../../lib/enketo-core/lib/jquery-xpath/jquery.xpath',
'jquery.touchswipe': '../../lib/enketo-core/lib/jquery-touchswipe/jquery.touchSwipe',
text: '../../lib/enketo-core/lib/text/text',
xpath: '../../lib/enketo-core/lib/xpath/build/xpathjs_javarosa',
gmaps: 'http://maps.google.com/maps/api/js?v=3.exp&sensor=false&libraries=places&callback=gmapsLoaded', // add API key?
jquery: '../../lib/enketo-core/lib/jquery',
bootstrap: '../../lib/enketo-core/lib/bootstrap',
Modernizr: '../../lib/enketo-core/lib/Modernizr',
leaflet: '../../lib/enketo-core/lib/leaflet/leaflet',
'bootstrap-slider': '../../lib/enketo-core/lib/bootstrap-slider/js/bootstrap-slider',
androidContext: '../../build/mock/androidcontext.mock', //replace with real one in dristhi app
mockForms: '../../build/mock/transforms.mock', //not required in dristhi app
mockInstances: '../../build/mock/instances.mock', //not required in dristhi app
ziggy: '../../lib/ziggy/ziggy/src'
},
shim: {
'xpath': {
exports: 'XPathJS'
},
'bootstrap': {
deps: [ 'jquery' ],
exports: 'jQuery.fn.popover'
},
'widget/date/bootstrap3-datepicker/js/bootstrap-datepicker': {
deps: [ 'jquery' ],
exports: 'jQuery.fn.datepicker'
},
'widget/time/bootstrap3-timepicker/js/bootstrap-timepicker': {
deps: [ 'jquery' ],
exports: 'jQuery.fn.timepicker'
},
'Modernizr': {
exports: 'Modernizr'
},
// not required in dristhi app:
'mockForms': {
exports: 'mockForms'
},
// not required in dristhi app:
'mockInstances': {
exports: 'mockInstances'
}
}
} );
// in Dristhi app, do not load mockForms
if ( window.androidContext ) {
define( 'mockForms', null );
}
requirejs( [ 'enketo-js/Form', 'FormDataController', 'enketo-json/FormModelJSON', 'gui', 'util', 'androidContext', 'jquery', 'plugins' ],
function( Form, FormDataController, FormModelJSON, gui, util, androidContext, $ ) {
'use strict';
var modelXMLStr, existingInstanceJSON, instanceToEditXMLStr, loadErrors, modelJSON, form, formHTML, instanceId,
queryParams = util.getAllQueryParams(),
formDataController = new FormDataController( queryParams ),
$submitButton = $( '#submit-form' );
window.onerror = function( m, u, l ) {
console.error( "Javascript Error: , msg: {0}, url: {1}, line: {2}".format( m, u, l ) );
return true;
};
// getting the HTML form
formHTML = androidContext.getForm();
if ( !formHTML ) {
return gui.alert( 'The form could not be retrieved.', 'Form Load Error' );
}
$( 'form.or' ).replaceWith( formHTML );
// getting the existing JSON instance
existingInstanceJSON = formDataController.get();
if ( !existingInstanceJSON ) {
$( 'form.or' ).remove();
instanceId = queryParams.instanceId;
return gui.alert( 'JSON Instance with id "' + instanceId + '" could not be found.' );
}
// getting the default XML model
modelXMLStr = androidContext.getModel();
if ( !modelXMLStr ) {
$( 'form.or' ).remove();
return gui.alert( 'The model could not be retrieved.', 'Form Load Error' );
}
modelJSON = new FormModelJSON( existingInstanceJSON );
instanceToEditXMLStr = modelJSON.toXML();
form = new Form( 'form.or:eq(0)', modelXMLStr, instanceToEditXMLStr );
loadErrors = form.init();
console.log( 'load errors: ' + loadErrors );
androidContext.onLoadFinished();
$submitButton.prop( 'disabled', false );
//controller for submission of data to drishti
$submitButton.click( function( event ) {
var jData, saveResult;
$submitButton.btnBusyState( true );
// without this weird timeout trick the button won't change until form.validateForm() is complete
// something odd that seems to happen when adding things to DOM.
setTimeout( function() {
if ( typeof form !== 'undefined' ) {
if ( !form.validate() ) {
gui.alert( 'Form contains errors <br/>(please see fields marked in red)' );
$submitButton.btnBusyState( false );
return;
} else {
jData = modelJSON.get( form );
delete jData.errors;
saveResult = formDataController.save( form.getInstanceID(), jData );
$submitButton.btnBusyState( false );
}
}
}, 100 );
} );
} );