static/js/forms.js
author Tero Marttila <terom@fixme.fi>
Thu, 23 Dec 2010 19:56:44 +0200
changeset 15 e098ee83b363
parent 9 0327b83959e9
child 18 5d06a3658699
permissions -rw-r--r--
Implement OrderContractDocument
/**
 * jQuery support code for our forms
 */

(function($) {
    /*
     * Form field is empty - i.e. null value
     *
     * Pure whitespace also counts
     */
    $.fn.empty = function () {
        return !this.val() || $.trim(this.val()) == "";
    }

    /**
     * Query or set form field disabled state
     */
    $.fn.disabled = function (flag) {
        if (flag == undefined)
            // XXX: jQuery returns `true` here?
            return !!this.attr("disabled");

        if (flag)
            this.attr("disabled", "disabled");
        else
            this.removeAttr("disabled");
    }

    $.fn.checked = function (flag) {
        if (flag == undefined)
            // XXX: jQuery returns true here?
            return !!this.attr("checked");


        if (flag)
            this.attr("checked", "checked");
        else
            this.removeAttr("checked");
    }

    /*
     * The given checkbox acts as an enable/disable toggle for this form control
     */
    $.fn.formEnabledBy = function (checkbox) {
        var target = this;

        checkbox.change(function () {
            target.disabled(!checkbox.checked());
        });
        checkbox.change();
    }

    /*
     * When non-zero <select>/<option> is selected, apply that option as pre-filled values for other form items
     */
    $.fn.formSelectPreset = function (opts) {
        opts = $.extend({
            // which option value is the "default", i.e. 'create new'
            valueDefault: 0,

            // which element to apply selected option value (id) to
            valueTarget: null,

            // which element to apply selected option text to
            textTarget: null,
        }, opts);

        function update () {
            // selected option value (i.e. id)
            value = $(this).val();

            // selected option text (i.e. title/name)
            text = $.trim($(this).find("option:selected").text());

            // fields to set
            field_values = [
                [ opts.valueTarget, value ],
                [ opts.textTarget, text ]
            ];

            if (value == opts.valueDefault) {
                // clear and re-enable fields
                if (opts.valueTarget && (opts.valueTarget.disabled() || opts.valueTarget.empty())) {
                    opts.valueTarget.disabled(false);
                    opts.valueTarget.val("");
                }

                if (opts.textTarget && (opts.textTarget.disabled() || opts.textTarget.empty())) {
                    opts.textTarget.disabled(false);
                    opts.textTarget.val("");
                }

                return;
            }

            // set field values
            if (opts.valueTarget) {
                opts.valueTarget.disabled(true);
                opts.valueTarget.val(value);
            }

            if (opts.textTarget) {
                opts.textTarget.disabled(true);
                opts.textTarget.val(text);
            }
        }
        
        // update linked field state on update, and startup..
        this.change(update);
        this.change()
    }
})(jQuery);