diff --git a/Gruntfile.js b/Gruntfile.js index ee8eea3..ef1e2a5 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -68,7 +68,6 @@ module.exports = function( grunt ) { baseUrl: 'src/js', mainConfigFile: "main.js", findNestedDependencies: true, - exclude: [ 'mockForms' ], include: ( function() { //add widgets js and widget config.json files var widgets = grunt.file.readJSON( 'config.json' ).widgets; diff --git a/build/mock/androidcontext.mock.js b/build/mock/androidcontext.mock.js index defd219..23a82db 100755 --- a/build/mock/androidcontext.mock.js +++ b/build/mock/androidcontext.mock.js @@ -1,57 +1,58 @@ -define( [ 'mockForms' ], function( mockForms ) { - return { +if ( !window.androidContext ) { + var getURLParameter, loadScript; + + console.log( 'loading the things' ); + + loadScript = function( filename ) { + //var fileref = document.createElement( 'script' ); + //fileref.setAttribute( "type", "text/javascript" ); + //fileref.setAttribute( "src", filename ); + //document.getElementsByTagName( "head" )[ 0 ].appendChild( fileref ) + // get some kind of XMLHttpRequest + var xhrObj = new XMLHttpRequest(); + // open and send a synchronous request + xhrObj.open( 'GET', filename, false ); + xhrObj.send( '' ); + // add the returned content to a newly created script tag + var se = document.createElement( 'script' ); + se.type = "text/javascript"; + se.text = xhrObj.responseText; + document.getElementsByTagName( 'head' )[ 0 ].appendChild( se ); + }; + + getURLParameter = function( name ) { + return decodeURI( + ( RegExp( name + '=' + '(.+?)(&|$)' ).exec( location.search ) || [ , null ] )[ 1 ] + ); + }; + + loadScript( '../build/mock/transforms.mock.js' ); + loadScript( '../build/mock/instances.mock.js' ); + + window.ENVIRONMENT = 'web'; + + window.androidContext = { //formName: 'ANC_Registration_24_5_12', //formName: 'EC_Registration_24_5_12', formName: getURLParameter( 'formName' ), getForm: function() { - if(window.androidContext){ - return window.androidContext.getForm(); - } return mockForms[ this.formName ].html_form; }, getModel: function() { - if(window.androidContext){ - return window.androidContext.getModel(); - } return mockForms[ this.formName ].xml_model; }, goBack: function() { - if(window.androidContext){ - window.androidContext.goBack(); - } + console.log( 'Wut?' ); } }; -} ); - -// what is this? -/* -var logContext = { - logError: function( e ) { - console.log( e ); - } -}; -*/ - -String.prototype.format = function( a, b, c ) { - return a + b + c; -}; - -function getURLParameter( name ) { - return decodeURI( - ( RegExp( name + '=' + '(.+?)(&|$)' ).exec( location.search ) || [ , null ] )[ 1 ] - ); -} -//this happens automatically in main app for mobiles -/* -function setToMobileMode() { - require( [ 'Modernizr' ], function( Modernizr ) { - Modernizr.touch = true; - var html = document.getElementsByTagName( 'html' )[ 0 ]; - html.className = html.className.replace( /no-touch/, 'touch' ); - } ); -}; -*/ + String.prototype.format = function( a, b, c ) { + return a + b + c; + }; + +} else { + window.ENVIROMENT = 'dristhi'; +} diff --git a/build/template.html b/build/template.html index bdfdfee..a1360af 100755 --- a/build/template.html +++ b/build/template.html @@ -10,6 +10,9 @@ + + + @@ -41,8 +44,9 @@

Choose Language
-
-
+
Powered by enketo logo
diff --git a/main.js b/main.js index f9b6f67..f91edbc 100644 --- a/main.js +++ b/main.js @@ -13,9 +13,6 @@ requirejs.config( { jquery: '../../lib/enketo-core/lib/jquery', bootstrap: '../../lib/enketo-core/lib/bootstrap', Modernizr: '../../lib/enketo-core/lib/Modernizr', - 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: { @@ -36,25 +33,14 @@ requirejs.config( { }, '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, $ ) { + +requirejs( [ 'enketo-js/Form', 'FormDataController', 'enketo-json/FormModelJSON', 'gui', 'util', 'jquery', 'plugins' ], + function( Form, FormDataController, FormModelJSON, gui, util, $ ) { 'use strict'; var modelXMLStr, existingInstanceJSON, instanceToEditXMLStr, loadErrors, modelJSON, form, instanceId, queryParams = util.getAllQueryParams(), @@ -65,51 +51,52 @@ requirejs( [ 'enketo-js/Form', 'FormDataController', 'enketo-json/FormModelJSON' return true; }; - $( 'form.or' ).replaceWith( androidContext.getForm() ); + //$( 'form.or' ).replaceWith( androidContext.getForm() ); //switches to touch=true, useful for desktop development, won't affect performance of production app. //if ( typeof setToMobileMode === 'function' ) { // setToMobileMode(); //} + $( document ).ready( function() { + existingInstanceJSON = formDataController.get(); - existingInstanceJSON = formDataController.get(); - - if ( !existingInstanceJSON ) { - $( 'form.or' ).remove(); - instanceId = queryParams.instanceId || undefined; - return gui.alert( 'JSON Instance with id "' + instanceId + '" could not be found.' ); - } + if ( !existingInstanceJSON ) { + $( 'form.or' ).remove(); + instanceId = queryParams.instanceId || undefined; + return gui.alert( 'JSON Instance with id "' + instanceId + '" could not be found.' ); + } - modelXMLStr = androidContext.getModel(); - modelJSON = new FormModelJSON( existingInstanceJSON ); - instanceToEditXMLStr = modelJSON.toXML(); - form = new Form( 'form.or:eq(0)', modelXMLStr, instanceToEditXMLStr ); + modelXMLStr = androidContext.getModel(); + modelJSON = new FormModelJSON( existingInstanceJSON ); + instanceToEditXMLStr = modelJSON.toXML(); + form = new Form( 'form.or:eq(0)', modelXMLStr, instanceToEditXMLStr ); - loadErrors = form.init(); - console.log( 'load errors', loadErrors ); + loadErrors = form.init(); + console.log( 'load errors', loadErrors ); - //controller for submission of data to drishti - $( document ).on( 'click', 'button#submit-form:not(:disabled)', function( event ) { - var jData, saveResult, - $button = $( this ); - $( this ).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' ) { + //controller for submission of data to drishti + $( document ).on( 'click', 'button#submit-form:not(:disabled)', function( event ) { + var jData, saveResult, + $button = $( this ); + $( this ).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
(please see fields marked in red)' ); - $button.btnBusyState( false ); - return; - } else { - jData = modelJSON.get( form ); - delete jData.errors; - saveResult = formDataController.save( form.getInstanceID(), jData ); - $button.btnBusyState( false ); + if ( !form.validate() ) { + gui.alert( 'Form contains errors
(please see fields marked in red)' ); + $button.btnBusyState( false ); + return; + } else { + jData = modelJSON.get( form ); + delete jData.errors; + saveResult = formDataController.save( form.getInstanceID(), jData ); + $button.btnBusyState( false ); + } } - } - }, 100 ); + }, 100 ); + } ); } ); } ); diff --git a/src/js/FormDataController.js b/src/js/FormDataController.js index 61f3a68..70d69f2 100644 --- a/src/js/FormDataController.js +++ b/src/js/FormDataController.js @@ -1,44 +1,48 @@ /*global mockForms2, mockInstances*/ -define( [ 'ziggy/FormDataController', 'mockInstances' ], - function( formDataController, mockInstances ) { +// in Dristhi app, do not load mockForms +if ( window.ENVIRONMENT === 'web' ) { + define( 'ziggy/FormDataController', null ); +} + +define( [ 'ziggy/FormDataController' ], function( ziggyController ) { + + /** + * [FormDataController description] + * @param {{instanceId: string, entityId: string}} params [description] + * @constructor + */ + function FormDataController( params ) { + params = params || {}; /** - * [FormDataController description] - * @param {{instanceId: string, entityId: string}} params [description] - * @constructor + * Gets instance as JSON from Dristhi DB - Should this be asynchronous? + * @return {?*} Form Data JSON object */ - function FormDataController( params ) { - params = params || {}; - var androidContext = window.androidContext; - /** - * Gets instance as JSON from Dristhi DB - Should this be asynchronous? - * @return {?*} Form Data JSON object - */ - this.get = function() { - if ( !androidContext ) { - return mockInstances[ params.instanceId ] || null; - } - return formDataController.get( params ) || null; - }; + this.get = function() { + if ( window.ENVIRONMENT === 'web' ) { + return mockInstances[ params.instanceId ] || null; + } + return ziggyController.get( params ) || null; + }; - /** - * Passes instance as JSON to store in Dristhi DB - Should this be asynchronous? - * @param {string} instanceId the new instanceID of the record - * @param {*} data Form Data JSON object - * @return {boolean} - */ - this.save = function( instanceId, data ) { - if ( !androidContext ) { - console.log( 'saving...', data ); - } else { - formDataController.save( params, data ); - androidContext.goBack(); - } - }; + /** + * Passes instance as JSON to store in Dristhi DB - Should this be asynchronous? + * @param {string} instanceId the new instanceID of the record + * @param {*} data Form Data JSON object + * @return {boolean} + */ + this.save = function( instanceId, data ) { + if ( window.ENVIRONMENT === 'web' ) { + console.log( 'saving...', data ); + } else { + ziggyController.save( params, data ); + androidContext.goBack(); + } + }; - this.remove = function( instanceId ) {}; - } + this.remove = function( instanceId ) {}; + } - return FormDataController; - } ); + return FormDataController; +} );