Initial dev repository
[kcls-web.git] / 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(orders, 
55         function(order) {
56             locs.push( 
57                 locations.filter(function(l) {return l.id() == order.location()})[0] 
58             );
59         }
60     );
61
62     // append any non-sorted locations
63     dojo.forEach(locations, 
64         function(l) {
65             if(!locs.filter(function(ll) { return ll.id() == l.id() })[0])
66                 locs.push(l);
67         }
68     );
69
70     // shove them into the DnD environment
71     dojo.forEach(locs,
72         function(loc) {
73             if(!loc) return;
74             var node = source.insertNodes(false, [ 
75                 { 
76                     data : loc.name() + ' (' + fieldmapper.aou.findOrgUnit(loc.owning_lib()).shortname()+')',
77                     type : [loc.id()+''] // use the type field to store the ID
78                 }
79             ]);
80         }
81     );
82 }
83
84 function applyChanges() {
85     progressDialog.show();
86
87     var newOrders = [];
88     var contextOrg = contextOrgSelector.attr('value');
89
90     // pull the locations out of the DnD environment and create order entries for them
91     dojo.forEach(
92         source.getAllNodes(),
93         function(node) {
94             var item = source.getItem(node.id);
95             var o = new fieldmapper.acplo();
96             o.position(newOrders.length + 1);
97             o.location(item.type[0]); // location.id() is stored in DnD item type
98             o.org(contextOrg);
99             newOrders.push(o);
100         }
101     );
102
103     fieldmapper.standardRequest(
104         ['open-ils.circ', 'open-ils.circ.copy_location_order.update'],
105         {
106             async : true,
107             params : [openils.User.authtoken, newOrders],
108             onresponse : function(r) {
109                 if(r = openils.Util.readResponse(r)) {
110                     if(r.orders) {
111                         orders = r.order;
112                         progressDialog.hide();
113                         filterGrid(contextOrg);
114                         return;
115                     } 
116                     progressDialog.update(r);
117                 }
118             },
119         }
120     );
121 }
122
123 openils.Util.addOnLoad(init);
124