static/js/forms.js
changeset 9 0327b83959e9
parent 6 72c73df76db2
child 15 e098ee83b363
equal deleted inserted replaced
8:27e37082625e 9:0327b83959e9
     1 /**
     1 /**
     2  * jQuery support code for our forms
     2  * jQuery support code for our forms
     3  */
     3  */
     4 
     4 
     5 (function($) {
     5 (function($) {
       
     6     /*
       
     7      * Form field is empty - i.e. null value
       
     8      *
       
     9      * Pure whitespace also counts
       
    10      */
       
    11     $.fn.empty = function () {
       
    12         return !this.val() || $.trim(this.val()) == "";
       
    13     }
       
    14 
       
    15     /**
       
    16      * Query or set form field disabled state
       
    17      */
       
    18     $.fn.disabled = function (flag) {
       
    19         if (flag == undefined)
       
    20             // XXX: jQuery returns `true` here?
       
    21             return !!this.attr("disabled");
       
    22 
       
    23         if (flag)
       
    24             this.attr("disabled", "disabled");
       
    25         else
       
    26             this.removeAttr("disabled");
       
    27     }
       
    28 
     6     /*
    29     /*
     7      * When non-zero <select>/<option> is selected, apply that option as pre-filled values for other form items
    30      * When non-zero <select>/<option> is selected, apply that option as pre-filled values for other form items
     8      */
    31      */
     9     $.fn.formSelectPreset = function (opts) {
    32     $.fn.formSelectPreset = function (opts) {
    10         opts = $.extend({
    33         opts = $.extend({
    16 
    39 
    17             // which element to apply selected option text to
    40             // which element to apply selected option text to
    18             textTarget: null,
    41             textTarget: null,
    19         }, opts);
    42         }, opts);
    20 
    43 
    21         this.change(function (event) {
    44         function update () {
    22             // selected option value (i.e. id)
    45             // selected option value (i.e. id)
    23             value = $(this).val();
    46             value = $(this).val();
    24 
    47 
    25             // selected option text (i.e. title/name)
    48             // selected option text (i.e. title/name)
    26             text = $.trim($(this).find("option:selected").text());
    49             text = $.trim($(this).find("option:selected").text());
    31                 [ opts.textTarget, text ]
    54                 [ opts.textTarget, text ]
    32             ];
    55             ];
    33 
    56 
    34             if (value == opts.valueDefault) {
    57             if (value == opts.valueDefault) {
    35                 // clear and re-enable fields
    58                 // clear and re-enable fields
    36                 if (opts.valueTarget) {
    59                 if (opts.valueTarget && (opts.valueTarget.disabled() || opts.valueTarget.empty())) {
    37                     opts.valueTarget.removeAttr('disabled');
    60                     opts.valueTarget.disabled(false);
    38                     opts.valueTarget.val("");
    61                     opts.valueTarget.val("");
    39                 }
    62                 }
    40 
    63 
    41                 if (opts.textTarget) {
    64                 if (opts.textTarget && (opts.textTarget.disabled() || opts.textTarget.empty())) {
    42                     opts.textTarget.removeAttr('disabled');
    65                     opts.textTarget.disabled(false);
    43                     opts.textTarget.val("");
    66                     opts.textTarget.val("");
    44                 }
    67                 }
    45 
    68 
    46                 return;
    69                 return;
    47             }
    70             }
    48 
    71 
    49             // set field values
    72             // set field values
    50             if (opts.valueTarget) {
    73             if (opts.valueTarget) {
    51                 opts.valueTarget.attr('disabled', "disabled");
    74                 opts.valueTarget.disabled(true);
    52                 opts.valueTarget.val(value);
    75                 opts.valueTarget.val(value);
    53             }
    76             }
    54 
    77 
    55             if (opts.textTarget) {
    78             if (opts.textTarget) {
    56                 opts.textTarget.attr('disabled', "disabled");
    79                 opts.textTarget.disabled(true);
    57                 opts.textTarget.val(text);
    80                 opts.textTarget.val(text);
    58             }
    81             }
    59         });
    82         }
       
    83         
       
    84         // update linked field state on update, and startup..
       
    85         this.change(update);
       
    86         this.change()
    60     }
    87     }
    61 })(jQuery);
    88 })(jQuery);