Skip to content
This repository has been archived by the owner on Jun 9, 2018. It is now read-only.

Commit

Permalink
moved script injection back inline, #36
Browse files Browse the repository at this point in the history
  • Loading branch information
MartijnR committed Mar 11, 2014
1 parent dd58044 commit 56bccd3
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 131 deletions.
1 change: 0 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
83 changes: 42 additions & 41 deletions build/mock/androidcontext.mock.js
Original file line number Diff line number Diff line change
@@ -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';
}
8 changes: 6 additions & 2 deletions build/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

<link href="css/dristhi.css" media="all" rel="stylesheet" type="text/css">

<!-- leave this, also inside Dristhi app -->
<script type="text/javascript" src="mock/androidContext.mock.js"></script>

<!-- for development: separate and unminified: -->
<!--<script type="text/javascript" data-main="../../main.js" src="../../lib/enketo-core/lib/require.js"></script>-->

Expand Down Expand Up @@ -41,8 +44,9 @@ <h3></h3>
<article class="paper">
<div class="form-header clearfix"><span class="form-language-selector"><span>Choose Language</span></span></div>

<form class="or">
</form>
<script>
document.write(androidContext.getForm());
</script>

<button id="submit-form" class="btn btn-info btn-large">Save</button>
<div class="enketo-power">Powered by <img src="images/enketo_bare_100x37.png" alt="enketo logo" /></div>
Expand Down
89 changes: 38 additions & 51 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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(),
Expand All @@ -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 <br/>(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 <br/>(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 );
} );
} );

} );
76 changes: 40 additions & 36 deletions src/js/FormDataController.js
Original file line number Diff line number Diff line change
@@ -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;
} );

0 comments on commit 56bccd3

Please sign in to comment.