]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/ui/default/opac/copyloc.js
LP2061136 - Stamping 1405 DB upgrade script
[Evergreen.git] / Open-ILS / web / js / ui / default / opac / copyloc.js
1 dojo.require("DojoSRF");
2 dojo.require("openils.CGI");
3
4 // called on initial page load and when the advance search org unit
5 // selector is changed.
6 function apply_adv_copy_locations() {
7
8     // patron selected org
9     var sel = dojo.byId('adv_org_selector');
10     var selected_id = sel.options[sel.selectedIndex].getAttribute('value');
11     var org_unit = aou_hash[selected_id];
12
13     var display_orgs = [];
14
15     // we want to display copy locations at the selected org,
16     // all parent orgs, and all child orgs.
17
18     function collect_child_orgs(org_id) {
19         display_orgs.push(org_id);
20         for (var id in aou_hash) { // for key in
21             if (aou_hash[id].parent_ou == org_id) 
22                 collect_child_orgs(id);
23         }
24     }
25
26     function collect_parent_orgs(org_id) {
27         if (!org_id) return;
28         display_orgs.push(org_id);
29         collect_parent_orgs(aou_hash[org_id].parent_ou);
30     }
31
32     display_orgs.push(org_unit.id);
33     collect_parent_orgs(org_unit.parent_ou);
34     fetch_adv_copy_locations(display_orgs);
35 }
36
37 function fetch_adv_copy_locations(org_ids) {
38
39     var params = [{
40         cache : 1, 
41         fields : ['name', 'id', 'owning_lib'],
42         query : {owning_lib : org_ids, opac_visible : 't', deleted : 'f'}
43     }];
44
45     new OpenSRF.ClientSession('open-ils.fielder').request({
46         method: 'open-ils.fielder.acpl.atomic',
47         params: params,
48         async: true,
49         oncomplete: function(r) {
50             var resp = r.recv();
51             if (resp) {
52                 var list = resp.content();
53                 if (list && list.length) {
54                     render_adv_copy_locations(list);
55                     render_adv_copy_locations_new(list);
56                 } else {
57                     dojo.addClass('adv_chunk_copy_location', 'hidden');
58                 }
59             } else {
60                 dojo.addClass('adv_chunk_copy_location', 'hidden');
61             }
62         }                                                              
63     }).send(); 
64 }
65
66 function render_adv_copy_locations_new(locations) {
67     var sel = dojo.byId('adv_copy_location_selector_new');
68     if(sel)
69     {
70     dojo.empty(sel);
71     var cgi = new openils.CGI();
72
73     // collect any location values from the URL to re-populate the list
74     var url_selected = cgi.param('fi:locations');
75     if (url_selected) {
76         if (!dojo.isArray(url_selected)) 
77             url_selected = [url_selected];
78     }
79
80     dojo.removeClass('adv_chunk_copy_location', 'hidden');
81     
82     // sort by name
83     locations = locations.sort(
84         function(a, b) {return a.name < b.name ? -1 : 1}
85     );
86
87     
88     var ulist = dojo.create('ul', {class: "adv_filters"});
89     // append the new list of locations
90     dojo.forEach(locations, function(loc) {
91         var attrs = {value : loc.id, name : "fi:locations", type: "checkbox", class: "form-check-input"};
92         if (url_selected && url_selected.indexOf(''+loc.id) > -1) {
93             attrs.selected = true;
94         }
95         
96         
97         ulist.appendChild(dojo.create('li')).appendChild(dojo.create('div', {class: "form-check"})).appendChild(dojo.create('label', {innerHTML : loc.name, class: "form-check-label"})).prepend(dojo.create('input', attrs));
98     });
99     sel.appendChild(dojo.create("div", {class: "card-body"})).appendChild(ulist);}
100 }
101
102    
103
104 function render_adv_copy_locations(locations) {
105     var sel = dojo.byId('adv_copy_location_selector');
106     if(sel){
107
108     
109     var cgi = new openils.CGI();
110
111     // collect any location values from the URL to re-populate the list
112     var url_selected = cgi.param('fi:locations');
113     if (url_selected) {
114         if (!dojo.isArray(url_selected)) 
115             url_selected = [url_selected];
116     }
117
118     dojo.removeClass('adv_chunk_copy_location', 'hidden');
119     
120     // sort by name
121     locations = locations.sort(
122         function(a, b) {return a.name < b.name ? -1 : 1}
123     );
124
125     // remove the previous list of locations
126     dojo.empty(sel);
127
128     // append the new list of locations
129     dojo.forEach(locations, function(loc) {
130         var attrs = {value : loc.id, innerHTML : loc.name};
131         if (url_selected && url_selected.indexOf(''+loc.id) > -1) {
132             attrs.selected = true;
133         }
134         sel.appendChild(dojo.create('option', attrs));
135     });
136 }
137 }
138
139 // load the locations on page load
140 dojo.addOnLoad(function() {
141     apply_adv_copy_locations();
142     dojo.connect(dojo.byId('adv_org_selector'), 
143         'onchange', apply_adv_copy_locations);
144 });
145