Ticket #3099: 3099.diff

File 3099.diff, 5.2 KB (added by Gary Wilson <gary.wilson@…>, 17 years ago)

a patch instead of the whole file. also fixed indention and spacing.

  • django/contrib/admin/media/js/SelectBox.js

    === modified file 'django/contrib/admin/media/js/SelectBox.js'
     
    33    init: function(id) {
    44        var box = document.getElementById(id);
    55        var node;
    6         SelectBox.cache[id] = new Array();
    7         var cache = SelectBox.cache[id];
    8         for (var i = 0; (node = box.options[i]); i++) {
    9             cache.push({ value: node.value, text: node.text, displayed: 1 });
     6        SelectBox.cache[id] = new Object();
     7        var boxCache = SelectBox.cache[id];
     8        for (var i=0; (node=box.options[i]); i++) {
     9            boxCache[node.value] = {value: node.value, text: node.text, displayed: 1};
    1010        }
    1111    },
    1212    redisplay: function(id) {
    1313        // Repopulate HTML select box from cache
    1414        var box = document.getElementById(id);
    1515        box.options.length = 0; // clear all options
    16         for (var i = 0, j = SelectBox.cache[id].length; i < j; i++) {
    17             var node = SelectBox.cache[id][i];
     16        var elementsArray = SelectBox.sort(id);
     17        for (var i=0; i<elementsArray.length; i++ ) {
     18            var node = elementsArray[i]; //SelectBox.cache[id][nodeValue];
    1819            if (node.displayed) {
    1920                box.options[box.options.length] = new Option(node.text, node.value, false, false);
    2021            }
     
    2526        // the words in text. (It's an AND search.)
    2627        var tokens = text.toLowerCase().split(/\s+/);
    2728        var node, token;
    28         for (var i = 0; (node = SelectBox.cache[id][i]); i++) {
     29        for (var nodeValue in SelectBox.cache[id]) {
     30            node = SelectBox.cache[id][nodeValue];
    2931            node.displayed = 1;
    30             for (var j = 0; (token = tokens[j]); j++) {
     32            for (var j=0; (token=tokens[j]); j++) {
    3133                if (node.text.toLowerCase().indexOf(token) == -1) {
    3234                    node.displayed = 0;
    3335                }
     
    3638        SelectBox.redisplay(id);
    3739    },
    3840    delete_from_cache: function(id, value) {
    39         var node, delete_index = null;
    40         for (var i = 0; (node = SelectBox.cache[id][i]); i++) {
    41             if (node.value == value) {
    42                 delete_index = i;
    43                 break;
    44             }
    45         }
    46         var j = SelectBox.cache[id].length - 1;
    47         for (var i = delete_index; i < j; i++) {
    48             SelectBox.cache[id][i] = SelectBox.cache[id][i+1];
    49         }
    50         SelectBox.cache[id].length--;
     41        delete SelectBox.cache[id][value];
    5142    },
    5243    add_to_cache: function(id, option) {
    53         SelectBox.cache[id].push({ value: option.value, text: option.text, displayed: 1 });
     44        SelectBox.cache[id][option.value] = {value: option.value, text: option.text, displayed: 1};
    5445    },
    5546    cache_contains: function(id, value) {
    56         // Check if an item is contained in the cache
    57         var node;
    58         for (var i = 0; (node = SelectBox.cache[id][i]); i++) {
    59             if (node.value == value) {
    60                 return true;
    61             }
    62         }
    63         return false;
     47        return SelectBox.cache[id].hasOwnProperty(value);
    6448    },
    6549    move: function(from, to) {
    6650        var from_box = document.getElementById(from);
    6751        var to_box = document.getElementById(to);
    6852        var option;
    69         for (var i = 0; (option = from_box.options[i]); i++) {
     53        for (var i=0; (option=from_box.options[i]); i++) {
    7054            if (option.selected && SelectBox.cache_contains(from, option.value)) {
    71                 SelectBox.add_to_cache(to, { value: option.value, text: option.text, displayed: 1 });
     55                SelectBox.add_to_cache(to, {value: option.value, text: option.text, displayed: 1});
    7256                SelectBox.delete_from_cache(from, option.value);
    7357            }
    7458        }
     
    7963        var from_box = document.getElementById(from);
    8064        var to_box = document.getElementById(to);
    8165        var option;
    82         for (var i = 0; (option = from_box.options[i]); i++) {
    83             SelectBox.add_to_cache(to, { value: option.value, text: option.text, displayed: 1 });
     66        for (var i=0; (option=from_box.options[i]); i++) {
     67            SelectBox.add_to_cache(to, {value: option.value, text: option.text, displayed: 1});
    8468            SelectBox.delete_from_cache(from, option.value);
    8569        }
    8670        SelectBox.redisplay(from);
    8771        SelectBox.redisplay(to);
    8872    },
    8973    sort: function(id) {
    90         SelectBox.cache[id].sort( function(a, b) {
     74        var cache = SelectBox.cache[id];
     75        var array = new Array();
     76        for (optionValue in cache) {
     77            var option = cache[optionValue];
     78            array.push(option);
     79        }
     80        array.sort(function(a, b) {
    9181            a = a.text.toLowerCase();
    9282            b = b.text.toLowerCase();
    9383            try {
     
    9888                // silently fail on IE 'unknown' exception
    9989            }
    10090            return 0;
    101         } );
     91        });
     92        return array;
    10293    },
    10394    select_all: function(id) {
    10495        var box = document.getElementById(id);
    105         for (var i = 0; i < box.options.length; i++) {
     96        for (var i=0; i<box.options.length; i++) {
    10697            box.options[i].selected = 'selected';
    10798        }
    10899    }
Back to Top