Upgrade to 4.2.8
[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[data-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                 var match = jQuery(element).find('div[data-name]').filter(function(){
25                     return jQuery(this).data('name').indexOf(vals[j]) == 0
26                 });
27                 match.show().find('input').prop('disabled', false);
28             }
29         }
30     }
31 }
32
33 function filter_cascade_select (select, complete_select, vals, is_hierarchical) {
34     if ( !( vals instanceof Array ) ) {
35         vals = [vals];
36     }
37
38     if (!select) { return };
39     var i;
40     var children = select.childNodes;
41
42     if ( complete_select ) {
43         jQuery(select).children().remove();
44
45         var complete_children = complete_select.childNodes;
46
47         var cloned_labels = {};
48         var cloned_empty_label;
49         for ( var j = 0; j < vals.length; j++ ) {
50             var val = vals[j];
51             if ( val == '' && is_hierarchical ) {
52                 // no category, and the category is from a hierchical cf;
53                 // leave this set of options empty
54             } else if ( val == '' ) {
55                 // no category, let's clone all node
56                 jQuery(select).append(jQuery(complete_children).clone());
57                 break;
58             }
59             else {
60                 var labels_to_clone = {};
61                 for (i = 0; i < complete_children.length; i++) {
62                     if (!complete_children[i].label ||
63                           (complete_children[i].hasAttribute &&
64                                 !complete_children[i].hasAttribute('label') ) ) {
65                         if ( cloned_empty_label ) {
66                             continue;
67                         }
68                     }
69                     else if ( complete_children[i].label == val ) {
70                         if ( cloned_labels[complete_children[i].label] ) {
71                             continue;
72                         }
73                         labels_to_clone[complete_children[i].label] = true;
74                     }
75                     else {
76                         continue;
77                     }
78
79                     jQuery(select).append(jQuery(complete_children[i]).clone());
80                 }
81
82                 if ( !cloned_empty_label )
83                     cloned_empty_label = true;
84
85                 for ( label in labels_to_clone ) {
86                     if ( !cloned_labels[label] )
87                         cloned_labels[label] = true;
88                 }
89             }
90         }
91     }
92     else {
93 // for back compatibility
94         for (i = 0; i < children.length; i++) {
95             if (!children[i].label) { continue };
96             if ( val == '' && is_hierarchical ) {
97                 hide(children[i]);
98                 continue;
99             }
100             if ( val == '' || children[i].label.substr(0, val.length) == val) {
101                 show(children[i]);
102                 continue;
103             }
104             hide(children[i]);
105         }
106     }
107 }