]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/conify/global/asset/copy_location_order.js
e11f34fbfbbb0746b9797f160ce2c558d7ad0d1e
[working/Evergreen.git] / Open-ILS / web / js / ui / default / conify / global / asset / copy_location_order.js
1 dojo.require('dijit.layout.ContentPane');
2 dojo.require("dojo.dnd.Container");
3 dojo.require("dojo.dnd.Source");
4 dojo.require('openils.widget.OrgUnitFilteringSelect');
5 dojo.require('fieldmapper.OrgUtils');
6 dojo.require('openils.User');
7 dojo.require('openils.Util');
8 dojo.require('openils.widget.AutoGrid');
9 dojo.require('openils.PermaCrud');
10 dojo.require('openils.widget.ProgressDialog');
11
12 var user;
13 var orders;
14 var locations;
15 var source;
16
17 function init() {
18
19      user = new openils.User();
20      source = new dojo.dnd.Source('acl-ol');
21
22      user.buildPermOrgSelector(
23         'ADMIN_COPY_LOCATION_ORDER', 
24         contextOrgSelector, 
25         null, 
26         function() {
27               dojo.connect(contextOrgSelector, 'onChange', filterGrid);
28         }
29     );
30
31     filterGrid(user.user.ws_ou());
32 }
33
34 function filterGrid(org) {
35
36     // fetch the locations and order entries
37     if(!orders) {
38         var pcrud = new openils.PermaCrud({authtoken : user.authtoken});
39         orders = pcrud.search('acplo', {org : org}, {order_by : {acplo : 'position'}});
40         locations = pcrud.search('acpl', 
41             {owning_lib : fieldmapper.aou.orgNodeTrail(fieldmapper.aou.findOrgUnit(org), true)}, 
42             {order_by : {acpl : 'name'}}
43         ); 
44     }
45
46     // init the DnD environment
47     source.selectAll();
48     source.deleteSelectedNodes();
49     source.clearItems();
50
51     var locs = [];
52
53     // sort and append by existing order settings
54     dojo.forEach(
55         orders,
56         function(order) {
57             locs = locs.concat(
58                 locations.filter(
59                     function(l) { return l.id() == order.location(); }
60                 )
61             );
62         }
63     );
64
65     // append any non-sorted locations
66     dojo.forEach(locations, 
67         function(l) {
68             if(!locs.filter(function(ll) { return ll.id() == l.id() })[0])
69                 locs.push(l);
70         }
71     );
72
73     // shove them into the DnD environment
74     dojo.forEach(locs,
75         function(loc) {
76             if(!loc) return;
77             var node = source.insertNodes(false, [ 
78                 { 
79                     data : loc.name() + ' (' + fieldmapper.aou.findOrgUnit(loc.owning_lib()).shortname()+')',
80                     type : [loc.id()+''] // use the type field to store the ID
81                 }
82             ]);
83         }
84     );
85 }
86
87 function applyChanges() {
88     progressDialog.show();
89
90     var newOrders = [];
91     var contextOrg = contextOrgSelector.attr('value');
92
93     // pull the locations out of the DnD environment and create order entries for them
94     dojo.forEach(
95         source.getAllNodes(),
96         function(node) {
97             var item = source.getItem(node.id);
98             var o = new fieldmapper.acplo();
99             o.position(newOrders.length + 1);
100             o.location(item.type[0]); // location.id() is stored in DnD item type
101             o.org(contextOrg);
102             newOrders.push(o);
103         }
104     );
105
106     fieldmapper.standardRequest(
107         ['open-ils.circ', 'open-ils.circ.copy_location_order.update'],
108         {
109             async : true,
110             params : [openils.User.authtoken, newOrders],
111             onresponse : function(r) {
112                 if(r = openils.Util.readResponse(r)) {
113                     if(r.orders) {
114                         orders = r.order;
115                         progressDialog.hide();
116                         filterGrid(contextOrg);
117                         return;
118                     } 
119                     progressDialog.update(r);
120                 }
121             },
122         }
123     );
124 }
125
126 openils.Util.addOnLoad(init);
127