Skip to content

Instantly share code, notes, and snippets.

@MartijnR
Created November 1, 2013 17:48
Show Gist options
  • Save MartijnR/7269098 to your computer and use it in GitHub Desktop.
Save MartijnR/7269098 to your computer and use it in GitHub Desktop.
enketo-xslt-test
/**
* Checks for general transformation or xml form errors by comparing stats. It is helpful,
* though an error is not always important
*/
FormView.prototype.checkForErrors = function( ) {
var i,
paths = [ ],
total = {},
$stats = $form.find( '#stats' );
if ( $stats.length > 0 ) {
total.jrItem = parseInt( $stats.find( '[id="jrItem"]' ).text( ), 10 );
total.jrInput = parseInt( $stats.find( '[id="jrInput"]' ).text( ), 10 );
total.jrItemset = parseInt( $stats.find( '[id="jrItemset"]' ).text( ), 10 );
total.jrUpload = parseInt( $stats.find( '[id="jrUpload"]' ).text( ), 10 );
total.jrTrigger = parseInt( $stats.find( '[id="jrTrigger"]' ).text( ), 10 );
total.jrConstraint = parseInt( $stats.find( '[id="jrConstraint"]' ).text( ), 10 );
total.jrRelevant = parseInt( $stats.find( '[id="jrRelevant"]' ).text( ), 10 );
total.jrCalculate = parseInt( $stats.find( '[id="jrCalculate"]' ).text( ), 10 );
total.jrPreload = parseInt( $stats.find( '[id="jrPreload"]' ).text( ), 10 );
/** @type {number} */
total.hRadio = $form.find( 'input[type="radio"]' ).length;
total.hCheck = $form.find( 'input[type="checkbox"]' ).length;
total.hSelect = $form.find( 'select:not(#form-languages)' ).length;
total.hItemset = $form.find( '.itemset-template' ).length;
total.hOption = $form.find( 'select:not(#form-languages) > option[value!=""]' ).length;
total.hInputNotRadioCheck = $form.find( 'textarea, input:not([type="radio"],[type="checkbox"])' ).length;
total.hTrigger = $form.find( '.trigger' ).length;
total.hRelevantNotRadioCheck = $form.find( '[data-relevant]:not([type="radio"],[type="checkbox"])' ).length;
total.hRelevantRadioCheck = $form.find( 'input[data-relevant][type="radio"],input[data-relevant][type="checkbox"]' ).parent( ).parent( 'fieldset' ).length;
total.hConstraintNotRadioCheck = $form.find( '[data-constraint]:not([type="radio"],[type="checkbox"])' ).length;
total.hConstraintRadioCheck = $form.find( 'input[data-constraint][type="radio"],input[data-constraint][type="checkbox"]' ).parent( ).parent( 'fieldset' ).length;
total.hCalculate = $form.find( '[data-calculate]' ).length;
total.hPreload = $form.find( '#jr-preload-items input' ).length;
if ( total.jrItemset === 0 && ( total.jrItem !== ( total.hOption + total.hRadio + total.hCheck ) ) ) {
console.error( ' total number of option fields differs between XML form and HTML form' );
}
if ( total.jrItemset !== total.hItemset ) {
console.error( ' total number of itemset fields differs between XML form (' + total.jrItemset + ') and HTML form (' + total.hItemset + ')' );
}
if ( ( total.jrInput + total.jrUpload ) !== ( total.hInputNotRadioCheck - total.hCalculate - total.hPreload ) ) {
console.error( ' total number of input/upload fields differs between XML form and HTML form' );
}
if ( total.jrTrigger != total.hTrigger ) {
console.error( ' total number of triggers differs between XML form and HTML form' );
}
if ( total.jrRelevant != ( total.hRelevantNotRadioCheck + total.hRelevantRadioCheck ) ) {
console.error( ' total number of branches differs between XML form and HTML form (not a problem if caused by obsolete bindings in xml form)' );
}
if ( total.jrConstraint != ( total.hConstraintNotRadioCheck + total.hConstraintRadioCheck ) ) {
console.error( ' total number of constraints differs between XML form (' + total.jrConstraint + ') and HTML form (' +
( total.hConstraintNotRadioCheck + total.hConstraintRadioCheck ) + ')(not a problem if caused by obsolete bindings in xml form).' +
' Note that constraints on <trigger> elements are ignored in the transformation and could cause this error too.' );
}
if ( total.jrCalculate != total.hCalculate ) {
console.error( ' total number of calculated items differs between XML form and HTML form' );
}
if ( total.jrPreload != total.hPreload ) {
console.error( ' total number of preload items differs between XML form and HTML form' );
}
//probably resource intensive: check if all nodes mentioned in name attributes exist in $data
$form.find( '[name]' ).each( function( ) {
if ( $.inArray( $( this ).attr( 'name' ), paths ) ) {
paths.push( $( this ).attr( 'name' ) );
}
} );
for ( i = 0; i < paths.length; i++ ) {
if ( model.node( paths[ i ] ).get( ).length < 1 ) {
console.error( 'Found name attribute: ' + paths[ i ] + ' that does not have a corresponding data node. Transformation error or XML form error (relative nodesets perhaps?' );
}
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment