plugged in load time ordering and apply changes operation to delete old order entries...
authorerickson <erickson@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Wed, 28 Oct 2009 15:59:14 +0000 (15:59 +0000)
committererickson <erickson@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Wed, 28 Oct 2009 15:59:14 +0000 (15:59 +0000)
git-svn-id: svn://svn.open-ils.org/ILS/trunk@14650 dcc99617-32d9-48b4-a31d-7c20da2025e4

Open-ILS/web/js/ui/default/conify/global/asset/copy_location_order.js
Open-ILS/web/templates/default/conify/global/asset/copy_location_order.tt2

index be177e3..3bdd802 100644 (file)
@@ -7,9 +7,9 @@ dojo.require('openils.User');
 dojo.require('openils.Util');
 dojo.require('openils.widget.AutoGrid');
 dojo.require('openils.PermaCrud');
+dojo.require('openils.widget.ProgressDialog');
 
 var user;
-var pcrud;
 var orders;
 var locations;
 var source;
@@ -17,7 +17,6 @@ var source;
 function init() {
 
      user = new openils.User();
-     pcrud = new openils.PermaCrud({authtoken : user.authtoken});
      source = new dojo.dnd.Source('acl-ol');
 
      user.buildPermOrgSelector(
@@ -33,20 +32,101 @@ function init() {
 }
 
 function filterGrid(org) {
+
+    // fetch the locations and order entries
+    var pcrud = new openils.PermaCrud({authtoken : user.authtoken});
     orders = pcrud.search('acplo', {org : org}, {order_by : {acplo : 'position'}});
-    locations = pcrud.search('acpl', {owning_lib : org}); //TODO
+    locations = pcrud.search('acpl', {owning_lib : org}, {order_by : {acpl : 'name'}}); // TODO
+
+    // init the DnD environment
     source.selectAll();
     source.deleteSelectedNodes();
     source.clearItems();
 
+    var locs = [];
+
+    // sort and append by existing order settings
+    dojo.forEach(orders, 
+        function(order) {
+            locs.push( 
+                locations.filter(function(l) {return l.id() == order.location()})[0] 
+            );
+        }
+    );
+
+    // append any non-sorted locations
     dojo.forEach(locations, 
+        function(l) {
+            if(!locs.filter(function(ll) { return ll.id() == l.id() })[0])
+                locs.push(l);
+        }
+    );
+
+    // shove them into the DnD environment
+    dojo.forEach(locs,
         function(loc) {
-            source.insertNodes(false, [
-                loc.name() + ' (' + fieldmapper.aou.findOrgUnit(loc.owning_lib()).shortname()+')'
+            var node = source.insertNodes(false, [ 
+                { 
+                    data : loc.name() + ' (' + fieldmapper.aou.findOrgUnit(loc.owning_lib()).shortname()+')',
+                    type : [loc.id()+''] // use the type field to store the ID
+                }
             ]);
         }
     );
 }
 
+function applyChanges() {
+    progressDialog.show(true);
+    if(orders.length) 
+        deleteOrders(createOrders);
+    else
+        createOrders();
+}
+
+function deleteOrders(onload) {
+    // delete the existing order entries in preparation for new ones
+    var pcrud = new openils.PermaCrud({authtoken : user.authtoken});
+    pcrud.delete(
+        orders,
+        {
+            async : true,
+            oncomplete : function() {
+                if(onload) onload();
+            }
+        }
+    );
+}
+
+function createOrders() {
+
+    var newOrders = [];
+
+    // pull the locations out of the DnD environment and create order entries for them
+    dojo.forEach(
+        source.getAllNodes(),
+        function(node) {
+            var item = source.getItem(node.id);
+            var o = new fieldmapper.acplo();
+            o.position(newOrders.length + 1);
+            o.location(item.type[0]); // location.id() is stored in DnD item type
+            o.org(contextOrgSelector.attr('value'));
+            newOrders.push(o);
+        }
+    );
+
+    // send the order entries off to the server
+    var pcrud = new openils.PermaCrud({authtoken : user.authtoken});
+    pcrud.create(
+        newOrders,
+        {
+            async : true,
+            oncomplete : function(r) {
+                progressDialog.hide();
+                filterGrid(contextOrgSelector.attr('value'));
+            }
+        }
+    );
+}
+
 openils.Util.addOnLoad(init);
 
index 6c2813a..fef9088 100644 (file)
@@ -5,8 +5,7 @@
 
 <div dojoType="dijit.layout.ContentPane" layoutAlign="client" class='oils-header-panel'>
     <div>Copy Location Order</div>
-    <div>
-    </div>
+    <div></div>
 </div>
 
 <div dojoType="dijit.layout.ContentPane" layoutAlign="client">
             labelAttr='shortname'>
     </select>
     <button dojoType='dijit.form.Button' onClick='applyChanges()'>Apply Changes</button>
+    <span>To move an item, drag it up or down with the mouse.</span>
 </div>
 
 <div dojoType="dijit.layout.ContentPane" layoutAlign="client">
     <ol id='acl-ol'></ol>
 </div>
 
+<div class='hidden'>
+    <div dojoType='openils.widget.ProgressDialog' jsId='progressDialog'/>
+</div>
+
 [% END %]