All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ovea.js.dynamic-form.dynamic-form.js Maven / Gradle / Ivy

There is a newer version: 1.12.ga
Show newest version
/*
 * Copyright (C) 2011 Ovea 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
if (window.ovea == undefined) {
    window.ovea = {};
}
if (ovea.DynamicForm == undefined) {
    (function($) {

        var FormElement = function(dynaform, keys) {
            this.dynaform = dynaform;
            this.keys = keys || [];
        };

        FormElement.prototype = {

            toString: function() {
                return this.keys.toString();
            },

            value: function(key) {
                var json = this.values();
                if (json) {
                    return json[key || this.keys[0]];
                }
            },

            values: function() {
                if (this.keys.length) {
                    return this.dynaform.toObject(this.keys[0], {
                        selector: ':input:enabled[name]'
                    });
                }
            },

            onChange: function(func) {
                this.dynaform.bind(this.keys, 'question.changed', func);
                return this;
            },

            onInvalid: function(func) {
                this.dynaform.bind(this.keys, 'question.invalid', func);
                return this;
            },

            onAllValid: function(func) {
                var valid = {};
                for (var i in this.keys) {
                    valid[this.keys[i]] = false;
                }
                var df = this.dynaform;
                df.bind(this.keys, 'question.valid', function(evt) {
                    valid[evt.key] = true;
                    for (var id in valid) {
                        if (valid[id] === false && !df.merges[id].has('[df-excluded]').length) {
                            return;
                        }
                    }
                    func.apply(this, arguments);
                });
                df.bind(this.keys, 'question.invalid', function(evt) {
                    valid[evt.key] = false;
                });
                return this;
            },

            onValid: function(func) {
                this.dynaform.bind(this.keys, 'question.valid', func);
                return this;
            },

            validate: function() {
                return this.dynaform.validate(this.keys);
            },

            isValid: function() {
                return this.dynaform.isValid(this.keys);
            }

        };

        ovea.DynamicForm = function(options) {

            this.elements = {};
            this.merges = {};
            this.templates = {};
            this.renderings = {};
            this.conditions = {};
            this.eventHandlers = {};
            this.specValidators = {};
            this.validators = {};

            this._renderingCount = 0;

            this.options = $.extend({
                specification: {},
                choiceLimit: 4,
                htmlGroup: '
' }, options || {}); if (typeof this.options.specification == 'string') { var self = this; $.ajax({ url: self.options.specification, dataType: 'json', async: false, cache: false, success: function(spec) { self.options.specification = spec; }, error: function(xhr, error) { if (console && console.error) { console.error(error + " - url = " + self.options.specification, arguments); } } }); } /** * TEMPLATES **/ /* simple elements */ this.addTemplate('text', '
'); this.addTemplate('range', '
'); this.addTemplate('choice-radio-open', '
'); this.addTemplate('choice-checkbox-open', '
'); this.addTemplate('choice-list-open', '
'); /* containers*/ this.addTemplate('choice-list', '
'); this.addTemplate('choice-flat', '
'); this.addTemplate('choice-radio', '
'); this.addTemplate('choice-checkbox', '
'); /* contained elements */ this.addTemplate('option', ''); this.addTemplate('radio', ''); this.addTemplate('checkbox', ''); this.addTemplate('select', '