]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/opac/copyloc.js
1eaae20c3f0410e42315b8254b36d5eca862f15c
[working/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     if (org_unit.can_have_vols != 't') {
14         dojo.addClass('adv_chunk_copy_location', 'hidden');
15         return;
16     }
17
18     var display_orgs = [];
19
20     // we want to display copy locations at the selected org,
21     // all parent orgs, and all child orgs.
22
23     function collect_child_orgs(org_id) {
24         display_orgs.push(org_id);
25         for (var id in aou_hash) { // for key in
26             if (aou_hash[id].parent_ou == org_id) 
27                 collect_child_orgs(id);
28         }
29     }
30
31     function collect_parent_orgs(org_id) {
32         if (!org_id) return;
33         display_orgs.push(org_id);
34         collect_parent_orgs(aou_hash[org_id].parent_ou);
35     }
36
37     collect_child_orgs(org_unit.id);
38     collect_parent_orgs(org_unit.parent_ou);
39     fetch_adv_copy_locations(display_orgs);
40 }
41
42 function fetch_adv_copy_locations(org_ids) {
43
44     var params = [{
45         cache : 1, 
46         fields : ['name', 'id', 'owning_lib'],
47         query : {owning_lib : org_ids, opac_visible : 't'}
48     }];
49
50     new OpenSRF.ClientSession('open-ils.fielder').request({
51         method: 'open-ils.fielder.acpl.atomic',
52         params: params,
53         async: true,
54         oncomplete: function(r) {
55             var resp = r.recv();
56             if (resp) {
57                 var list = resp.content();
58                 if (list && list.length) {
59                     render_adv_copy_locations(list);
60                 } else {
61                     dojo.addClass('adv_chunk_copy_location', 'hidden');
62                 }
63             } else {
64                 dojo.addClass('adv_chunk_copy_location', 'hidden');
65             }
66         }                                                              
67     }).send(); 
68 }
69
70 function render_adv_copy_locations(locations) {
71     var sel = dojo.byId('adv_copy_location_selector');
72     var cgi = new openils.CGI();
73
74     // collect any location values from the URL to re-populate the list
75     var url_selected = cgi.param('fi:locations');
76     if (url_selected) {
77         if (!dojo.isArray(url_selected)) 
78             url_selected = [url_selected];
79     }
80
81     dojo.removeClass('adv_chunk_copy_location', 'hidden');
82     
83     // sort by name
84     locations = locations.sort(
85         function(a, b) {return a.name < b.name ? -1 : 1}
86     );
87
88     // remove the previous list of locations
89     dojo.empty(sel);
90
91     // append the new list of locations
92     dojo.forEach(locations, function(loc) {
93         var attrs = {value : loc.id, innerHTML : loc.name};
94         if (url_selected && url_selected.indexOf(''+loc.id) > -1) {
95             attrs.selected = true;
96         }
97         sel.appendChild(dojo.create('option', attrs));
98     });
99 }
100
101 // load the locations on page load
102 dojo.addOnLoad(function() {
103     apply_adv_copy_locations();
104     dojo.connect(dojo.byId('adv_org_selector'), 
105         'onchange', apply_adv_copy_locations);
106 });
107