How to use enketo-core - 10 common examples

To help you get started, we’ve selected a few enketo-core examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github kobotoolbox / enketo-express / public / js / src / module / controller-webform.js View on Github external
.then( record => {
            if ( !data.instanceStr && record && record.xml ) {
                records.setActive( records.getAutoSavedKey() );
                data.instanceStr = record.xml;
            }

            if ( data.instanceAttachments ) {
                fileManager.setInstanceAttachments( data.instanceAttachments );
            }

            form = new Form( formSelector, data, formOptions );
            loadErrors = form.init();

            // Remove loader. This will make the form visible.
            // In order to aggregate regular loadErrors and GoTo loaderrors,
            // this is placed in between form.init() and form.goTo().
            $( '.main-loader' ).remove();

            if ( settings.goTo && location.hash ) {
                console.log( 'going to ', location.hash.substring( 1 ) );
                loadErrors = loadErrors.concat( form.goTo( location.hash.substring( 1 ) ) );
            }

            if ( form.encryptionKey && !encryptor.isSupported() ) {
                loadErrors.unshift( t( 'error.encryptionnotsupported' ) );
            }
github kobotoolbox / enketo-express / public / js / src / module / controller-webform.js View on Github external
function _resetForm( confirmed ) {
    let message;

    if ( !confirmed && form.editStatus ) {
        message = t( 'confirm.save.msg' );
        gui.confirm( message )
            .then( confirmed => {
                if ( confirmed ) {
                    _resetForm( true );
                }
            } );
    } else {
        _setDraftStatus( false );
        form.resetView();
        form = new Form( formSelector, {
            modelStr: formData.modelStr,
            external: formData.external
        }, formOptions );
        const loadErrors = form.init();
        // formreset event will update the form media:
        form.view.$.trigger( 'formreset' );
        if ( records ) {
            records.setActive( null );
        }
        if ( loadErrors.length > 0 ) {
            gui.alertLoadErrors( loadErrors );
        }
    }
}
github kobotoolbox / enketo-express / public / js / src / enketo-webform.js View on Github external
function _prepareInstance( modelStr, defaults ) {
    let model;
    let init;
    let existingInstance = null;

    for ( const path in defaults ) {
        if ( Object.prototype.hasOwnProperty.call( defaults, path ) ) {
            model = model || new FormModel( modelStr, {
                full: false
            } );
            init = init || model.init();
            if ( Object.prototype.hasOwnProperty.call( defaults, path ) ) {
                // if this fails, the FormModel will output a console error and ignore the instruction
                model.node( path ).setVal( defaults[ path ] );
            }
            // TODO would be good to not include nodes that weren't in the defaults parameter
            // HOWEVER, that would also set number of repeats to 0, which may be undesired
            // TODO would be good to just pass model along instead of converting to string first
            existingInstance = model.getStr();
        }
    }
    return existingInstance;
}
github kobotoolbox / enketo-express / public / js / src / module / gui.js View on Github external
initTranslator()
        .then( setEventHandlers );

    // avoid Windows console errors
    if ( typeof window.console === 'undefined' ) {
        window.console = {
            log() {}
        };
    }
    if ( typeof window.console.debug === 'undefined' ) {
        console.debug = console.log;
    }

    // override feature detection (for development purposes)
    if ( settings.touch ) {
        support.touch = true;
        $( 'html' ).addClass( 'touch' );
    } else if ( settings.touch === false ) {
        support.touch = false;
        $( 'html' ).removeClass( 'touch' );
    }
}
github kobotoolbox / enketo-express / public / js / src / enketo-webform-view.js View on Github external
import gui from './module/gui';
import controller from './module/controller-webform';
import settings from './module/settings';
import connection from './module/connection';
import { init as initTranslator, t, localize } from './module/translator';

const $loader = $( '.main-loader' );
const $formheader = $( '.main > .paper > .form-header' );
const survey = {
    enketoId: settings.enketoId,
    instanceId: settings.instanceId
};

// Completely disable calculations in Enketo Core
import calcModule from 'enketo-core/src/js/calculate';
calcModule.update = () => {
    console.log( 'Calculations disabled.' );
};
// Completely disable instanceID and deprecatedID population in Enketo Core
import { FormModel } from 'enketo-core/src/js/form-model';
FormModel.prototype.setInstanceIdAndDeprecatedId = () => {
    console.log( 'InstanceID and deprecatedID population disabled.' );
};
// Completely disable preload items
import preloadModule from 'enketo-core/src/js/preload';
preloadModule.init = () => {
    console.log( 'Preloaders disabled.' );
};

initTranslator( survey )
    .then( survey => connection.getFormParts( survey ) )
    .then( formParts => {
github kobotoolbox / enketo-express / public / js / src / enketo-webform-view.js View on Github external
const $loader = $( '.main-loader' );
const $formheader = $( '.main > .paper > .form-header' );
const survey = {
    enketoId: settings.enketoId,
    instanceId: settings.instanceId
};

// Completely disable calculations in Enketo Core
import calcModule from 'enketo-core/src/js/calculate';
calcModule.update = () => {
    console.log( 'Calculations disabled.' );
};
// Completely disable instanceID and deprecatedID population in Enketo Core
import { FormModel } from 'enketo-core/src/js/form-model';
FormModel.prototype.setInstanceIdAndDeprecatedId = () => {
    console.log( 'InstanceID and deprecatedID population disabled.' );
};
// Completely disable preload items
import preloadModule from 'enketo-core/src/js/preload';
preloadModule.init = () => {
    console.log( 'Preloaders disabled.' );
};

initTranslator( survey )
    .then( survey => connection.getFormParts( survey ) )
    .then( formParts => {
        if ( survey.instanceId ) {
            return connection.getExistingInstance( survey )
                .then( response => {
                    formParts.instance = response.instance;
                    formParts.instanceAttachments = response.instanceAttachments;
github kobotoolbox / enketo-express / public / js / src / enketo-webform-view.js View on Github external
instanceId: settings.instanceId
};

// Completely disable calculations in Enketo Core
import calcModule from 'enketo-core/src/js/calculate';
calcModule.update = () => {
    console.log( 'Calculations disabled.' );
};
// Completely disable instanceID and deprecatedID population in Enketo Core
import { FormModel } from 'enketo-core/src/js/form-model';
FormModel.prototype.setInstanceIdAndDeprecatedId = () => {
    console.log( 'InstanceID and deprecatedID population disabled.' );
};
// Completely disable preload items
import preloadModule from 'enketo-core/src/js/preload';
preloadModule.init = () => {
    console.log( 'Preloaders disabled.' );
};

initTranslator( survey )
    .then( survey => connection.getFormParts( survey ) )
    .then( formParts => {
        if ( survey.instanceId ) {
            return connection.getExistingInstance( survey )
                .then( response => {
                    formParts.instance = response.instance;
                    formParts.instanceAttachments = response.instanceAttachments;
                    return formParts;
                } );
        }
        return formParts;
    } )
github kobotoolbox / enketo-express / public / js / src / module / file-manager.js View on Github external
if ( this.type === 'file' ) {
            file = this.files[ 0 ]; // Why doesn't this fail for empty file inputs?
        } else if ( this.value ) {
            canvas = $( this ).closest( '.question' )[ 0 ].querySelector( '.draw-widget canvas' );
            if ( canvas && !URL_RE.test( this.value ) ) {
                // TODO: In the future, we could do canvas.toBlob()
                file = utils.dataUriToBlobSync( canvas.toDataURL() );
                file.name = this.value;
            }
        }
        if ( file && file.name ) {
            // Correct file names by adding a unique-ish postfix
            // First create a clone, because the name property is immutable
            // TODO: in the future, when browser support increase we can invoke
            // the File constructor to do this.
            newFilename = getFilename( file, this.dataset.filenamePostfix );
            // If file is resized, get Blob representation of data URI
            if ( this.dataset.resized && this.dataset.resizedDataURI ) {
                file = utils.dataUriToBlobSync( this.dataset.resizedDataURI );
            }
            file = new Blob( [ file ], {
                type: file.type
            } );
            file.name = newFilename;
            files.push( file );
        }
    } );
github kobotoolbox / enketo-express / public / js / src / module / gui.js View on Github external
.then( () => {
            if ( formTheme === 'grid' || ( !formTheme && printHelper.isGrid() ) ) {
                const paper = { format: settings.format, landscape: settings.landscape, scale: settings.scale, margin: settings.margin };
                return printHelper.fixGrid( paper );
            }
        } )
        .then( () => // allow some time for repainting
github kobotoolbox / enketo-express / public / js / src / module / gui.js View on Github external
.then( () => {
            if ( formTheme === 'grid' || ( !formTheme && printHelper.isGrid() ) ) {
                const paper = { format: settings.format, landscape: settings.landscape, scale: settings.scale, margin: settings.margin };
                return printHelper.fixGrid( paper );
            }
        } )
        .then( () => // allow some time for repainting