forked from enketo/enketo-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
89 lines (83 loc) · 3.01 KB
/
app.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
/**
* This file is just meant to facilitate enketo-core development as a standalone library.
*
* When using enketo-core as a library inside your app, it is recommended to just **ignore** this file.
* Place a replacement for this controller elsewhere in your app.
*/
'use strict';
import $ from 'jquery';
import support from './src/js/support';
import { Form } from './src/js/form';
import fileManager from './src/js/file-manager';
var loadErrors;
var form;
var formStr;
var modelStr;
var xform = getURLParameter( 'xform' );
// if querystring touch=true is added, override detected touchscreen presence
if ( getURLParameter( 'touch' ) === 'true' ) {
support.touch = true;
$( 'html' ).addClass( 'touch' );
}
// Check if HTML form is hardcoded or needs to be retrieved
// note: when running this file in enketo-core-performance-monitor xform = 'null'
if ( xform && xform !== 'null' ) {
$( '.guidance' ).remove();
xform = /^https?:\/\//.test( xform ) ? xform : location.origin + '/' + xform;
var transformerUrl = 'http://' + location.hostname + ':8085/transform?xform=' + xform;
$.getJSON( transformerUrl )
.done( function( survey ) {
formStr = survey.form;
modelStr = survey.model;
$( '.form-header' ).after( formStr );
initializeForm();
} )
.fail( function() {
window.alert( 'Error fetching form from enketo-transformer at:\n\n' + transformerUrl + '.\n\nPlease check that enketo-transformer has been started.' );
} );
} else if ( $( 'form.or' ).length > 0 ) {
$( '.guidance' ).remove();
modelStr = window.globalModelStr;
initializeForm();
}
// validate handler for validate button
$( '#validate-form' ).on( 'click', function() {
// validate form
form.validate()
.then( function( valid ) {
if ( !valid ) {
window.alert( 'Form contains errors. Please see fields marked in red.' );
} else {
window.alert( 'Form is valid! (see XML record and media files in the console)' );
$( 'form.or' ).trigger( 'beforesave' );
console.log( 'record:', form.getDataStr() );
console.log( 'media files:', fileManager.getCurrentFiles() );
}
} );
} );
// initialize the form
function initializeForm() {
form = new Form( 'form.or:eq(0)', {
modelStr: modelStr
}, {
arcGis: {
basemaps: [ 'streets', 'topo', 'satellite', 'osm' ],
webMapId: 'f2e9b762544945f390ca4ac3671cfa72',
hasZ: true
},
'clearIrrelevantImmediately': true
} );
// for debugging
window.form = form;
//initialize form and check for load errors
loadErrors = form.init();
if ( loadErrors.length > 0 ) {
window.alert( 'loadErrors: ' + loadErrors.join( ', ' ) );
}
}
// get query string parameter
function getURLParameter( name ) {
return decodeURI(
( new RegExp( name + '=' + '(.+?)(&|$)' ).exec( location.search ) || [ null, null ] )[ 1 ]
);
}