Upgrade to 4.2.2
[usit-rt.git] / share / static / js / cascaded.js
1 function filter_cascade_by_id (id, vals, is_hierarchical) {
2     var element = document.getElementById(id);
3     if (!element) { return };
4
5     if ( element.tagName == 'SELECT' ) {
6         var complete_select = document.getElementById(id + "-Complete" );
7         return filter_cascade_select(element, complete_select, vals, is_hierarchical);
8     }
9     else {
10         if ( !( vals instanceof Array ) ) {
11             vals = [vals];
12         }
13
14         if ( is_hierarchical && (vals.length == 0 || (vals.length == 1 && vals[0] == '')) ) {
15             // no category, and the category is from a hierchical cf;
16             // leave it empty
17             jQuery(element).find('div').hide();
18         }
19         else {
20             jQuery(element).find('div').hide().find('input').prop('disabled', true);
21             jQuery(element).find('div[name=]').show().find('input').prop('disabled', false);
22             jQuery(element).find('div.none').show().find('input').prop('disabled',false);
23             for ( var j = 0; j < vals.length; j++ ) {
24                 jQuery(element).find('div[name^=' + vals[j] + ']').show().find('input').prop('disabled', false);
25             }
26         }
27     }
28 }
29
30 function filter_cascade_select (select, complete_select, vals, is_hierarchical) {
31     if ( !( vals instanceof Array ) ) {
32         vals = [vals];
33     }
34
35     if (!select) { return };
36     var i;
37     var children = select.childNodes;
38
39     if ( complete_select ) {
40         jQuery(select).children().remove();
41
42         var complete_children = complete_select.childNodes;
43
44         var cloned_labels = {};
45         var cloned_empty_label;
46         for ( var j = 0; j < vals.length; j++ ) {
47             var val = vals[j];
48             if ( val == '' && is_hierarchical ) {
49                 // no category, and the category is from a hierchical cf;
50                 // leave this set of options empty
51             } else if ( val == '' ) {
52                 // no category, let's clone all node
53                 jQuery(select).append(jQuery(complete_children).clone());
54                 break;
55             }
56             else {
57                 var labels_to_clone = {};
58                 for (i = 0; i < complete_children.length; i++) {
59                     if (!complete_children[i].label ||
60                           (complete_children[i].hasAttribute &&
61                                 !complete_children[i].hasAttribute('label') ) ) {
62                         if ( cloned_empty_label ) {
63                             continue;
64                         }
65                     }
66                     else if ( complete_children[i].label == val ) {
67                         if ( cloned_labels[complete_children[i].label] ) {
68                             continue;
69                         }
70                         labels_to_clone[complete_children[i].label] = true;
71                     }
72                     else {
73                         continue;
74                     }
75
76                     jQuery(select).append(jQuery(complete_children[i]).clone());
77                 }
78
79                 if ( !cloned_empty_label )
80                     cloned_empty_label = true;
81
82                 for ( label in labels_to_clone ) {
83                     if ( !cloned_labels[label] )
84                         cloned_labels[label] = true;
85                 }
86             }
87         }
88     }
89     else {
90 // for back compatibility
91         for (i = 0; i < children.length; i++) {
92             if (!children[i].label) { continue };
93             if ( val == '' && is_hierarchical ) {
94                 hide(children[i]);
95                 continue;
96             }
97             if ( val == '' || children[i].label.substr(0, val.length) == val) {
98                 show(children[i]);
99                 continue;
100             }
101             hide(children[i]);
102         }
103     }
104 }