select.js 4.3 KB
Newer Older
1 2 3 4 5 6
// send async request to the given URL (which will send back serialized ListBoxModel object),
// then use the result to fill the list box.
function updateListBox(listBox,url,config) {
    config = config || {};
    config = object(config);
    var originalOnSuccess = config.onSuccess;
7 8 9 10 11 12 13
    var l = $(listBox);
    var status = findFollowingTR(listBox, "validation-error-area").firstChild.nextSibling;
    if (status.firstChild && status.firstChild.getAttribute('data-select-ajax-error')) {
        status.innerHTML = "";
    }
    config.onSuccess = function (rsp) {
        l.removeClassName("select-ajax-pending");
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
        var currentSelection = l.value;

        // clear the contents
        while(l.length>0)   l.options[0] = null;

        var selectionSet = false; // is the selection forced by the server?
        var possibleIndex = null; // if there's a new option that matches the current value, remember its index
        var opts = eval('('+rsp.responseText+')').values;
        for( var i=0; i<opts.length; i++ ) {
            l.options[i] = new Option(opts[i].name,opts[i].value);
            if(opts[i].selected) {
                l.selectedIndex = i;
                selectionSet = true;
            }
            if (opts[i].value==currentSelection)
                possibleIndex = i;
        }

        // if no value is explicitly selected by the server, try to select the same value
        if (!selectionSet && possibleIndex!=null)
            l.selectedIndex = possibleIndex;

        if (originalOnSuccess!=undefined)
            originalOnSuccess(rsp);
38 39 40 41 42 43 44 45 46 47
    };
    config.onFailure = function (rsp) {
        l.removeClassName("select-ajax-pending");
        status.innerHTML = rsp.responseText;
        if (status.firstChild) {
            status.firstChild.setAttribute('data-select-ajax-error', 'true')
        }
        Behaviour.applySubtree(status);
        // deleting values can result in the data loss, so let's not do that unless instructed
        var header = rsp.getResponseHeader('X-Jenkins-Select-Error');
48
        if (header && "clear" === header.toLowerCase()) {
49 50 51 52 53
            // clear the contents
            while (l.length > 0)   l.options[0] = null;
        }

    };
54

55
    l.addClassName("select-ajax-pending");
56 57 58
    new Ajax.Request(url, config);
}

59
Behaviour.specify("SELECT.select", 'select', 1000, function(e) {
60 61

        function hasChanged(selectEl, originalValue) {
G
gusreiber 已提交
62
            // seems like a race condition allows this to fire before the 'selectEl' is defined. If that happens, exit..
T
tfennelly 已提交
63
            if(!selectEl || !selectEl.options || !selectEl.options.length > 0)
G
gusreiber 已提交
64 65
              return false;
            var firstValue = selectEl.options[0].value;
66 67 68 69 70 71 72 73 74 75
            var selectedValue = selectEl.value;
            if (originalValue == "" && selectedValue == firstValue) {
                // There was no value pre-selected but after the call to updateListBox the first value is selected by
                // default. This must not be considered a change.
                return false;
            } else {
                return originalValue != selectedValue;
            }
        };

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
        // controls that this SELECT box depends on
        refillOnChange(e,function(params) {
            var value = e.value;
            updateListBox(e,e.getAttribute("fillUrl"),{
                parameters: params,
                onSuccess: function() {
                    if (value=="") {
                        // reflect the initial value. if the control depends on several other SELECT.select,
                        // it may take several updates before we get the right items, which is why all these precautions.
                        var v = e.getAttribute("value");
                        if (v) {
                            e.value = v;
                            if (e.value==v) e.removeAttribute("value"); // we were able to apply our initial value
                        }
                    }

K
Kohsuke Kawaguchi 已提交
92 93
                    fireEvent(e,"filled"); // let other interested parties know that the items have changed

94
                    // if the update changed the current selection, others listening to this control needs to be notified.
95 96 97
                    if (hasChanged(e, value)) {
                        fireEvent(e,"change");
                    }
98 99 100
                }
            });
        });
101
});