Putting 4.2.0 on top of 4.0.17
[usit-rt.git] / share / static / js / cascaded.js
1 function filter_cascade_by_id (id, vals, is_hierarchical) {
2     var select = document.getElementById(id);
3     var complete_select = document.getElementById(id + "-Complete" );
4     return filter_cascade(select, complete_select, vals, is_hierarchical);
5 }
6
7 function filter_cascade (select, complete_select, vals, is_hierarchical) {
8     if ( !( vals instanceof Array ) ) {
9         vals = [vals];
10     }
11
12     if (!select) { return };
13     var i;
14     var children = select.childNodes;
15
16     if ( complete_select ) {
17         while (select.hasChildNodes()){
18             select.removeChild(select.firstChild);
19         }
20
21         var complete_children = complete_select.childNodes;
22
23         var cloned_labels = {};
24         var cloned_empty_label;
25         for ( var j = 0; j < vals.length; j++ ) {
26             var val = vals[j];
27             if ( val == '' && is_hierarchical ) {
28                 // no category, and the category is from a hierchical cf;
29                 // leave this set of options empty
30             } else if ( val == '' ) {
31                 // no category, let's clone all node
32                 for (i = 0; i < complete_children.length; i++) {
33                     if ( complete_children[i].cloneNode ) {
34                         var new_option = complete_children[i].cloneNode(true);
35                         select.appendChild(new_option);
36                     }
37                 }
38                 break;
39             }
40             else {
41                 var labels_to_clone = {};
42                 for (i = 0; i < complete_children.length; i++) {
43                     if (!complete_children[i].label ||
44                           (complete_children[i].hasAttribute &&
45                                 !complete_children[i].hasAttribute('label') ) ) {
46                         if ( cloned_empty_label ) {
47                             continue;
48                         }
49                     }
50                     else if ( complete_children[i].label == val ) {
51                         if ( cloned_labels[complete_children[i].label] ) {
52                             continue;
53                         }
54                         labels_to_clone[complete_children[i].label] = true;
55                     }
56                     else {
57                         continue;
58                     }
59
60                     if ( complete_children[i].cloneNode ) {
61                         var new_option = complete_children[i].cloneNode(true);
62                         select.appendChild(new_option);
63                     }
64                 }
65
66                 if ( !cloned_empty_label )
67                     cloned_empty_label = true;
68
69                 for ( label in labels_to_clone ) {
70                     if ( !cloned_labels[label] )
71                         cloned_labels[label] = true;
72                 }
73             }
74         }
75     }
76     else {
77 // for back compatibility
78         for (i = 0; i < children.length; i++) {
79             if (!children[i].label) { continue };
80             if ( val == '' && is_hierarchical ) {
81                 hide(children[i]);
82                 continue;
83             }
84             if ( val == '' || children[i].label.substr(0, val.length) == val) {
85                 show(children[i]);
86                 continue;
87             }
88             hide(children[i]);
89         }
90     }
91 }