1 dojo.require("dojo.dnd.Container");
2 dojo.require("dojo.dnd.Source");
3 dojo.require('openils.widget.AutoGrid');
4 dojo.require('dijit.form.FilteringSelect');
5 dojo.require('openils.PermaCrud');
6 dojo.require('openils.widget.AutoFieldWidget');
9 var formula, entryTbody, entryTemplate, dndSource;
16 pcrud = new openils.PermaCrud();
19 openils.Util.hide('formula-list-div');
23 openils.Util.hide('formula-entry-div');
24 fListGrid.onPostCreate = function(fmObject) {
25 location.href = location.href + '/' + fmObject.id();
28 fieldmapper.standardRequest(
29 ['open-ils.acq', 'open-ils.acq.distribution_formula.ranged.retrieve'],
31 params: [openils.User.authtoken],
32 onresponse: function (r) {
33 var form = openils.Util.readResponse(r);
34 formCache[form.id()] = form;
35 fListGrid.store.newItem(form.toStoreItem());
37 oncomplete: function() {
38 fListGrid.hideLoadProgressIndicator();
45 openils.Util.addOnLoad(draw);
47 function getItemCount(rowIndex, item) {
49 var form = formCache[this.grid.store.getValue(item, "id")];
52 dojo.forEach(form.entries(), function(e) { count = count + e.item_count(); });
56 function byName(node, name) {
57 return dojo.query('[name='+name+']', node)[0];
60 function drawFormulaSummary() {
61 openils.Util.show('formula-entry-div');
63 var entries = pcrud.search('acqdfe', {formula: formulaId}, {order_by:{acqdfe : 'position'}});
64 formula = pcrud.retrieve('acqdf', formulaId);
65 formula.entries(entries);
67 dojo.byId('formula_head').innerHTML = formula.name();
68 dojo.forEach(entries, function(entry) { addEntry(entry); } );
71 function addEntry(entry) {
74 entryTbody = dojo.byId('formula-entry-tbody');
75 entryTemplate = entryTbody.removeChild(dojo.byId('formula-entry-tempate'));
76 dndSource = new dojo.dnd.Source(entryTbody);
77 dndSource.selectAll();
78 dndSource.deleteSelectedNodes();
79 dndSource.clearItems();
83 entry = new fieldmapper.acqdfe();
84 entry.formula(formulaId);
86 entry.owning_lib(openils.User.user.ws_ou());
87 entry.id(virtualId--);
89 formula.entries().push(entry);
92 var row = entryTbody.appendChild(entryTemplate.cloneNode(true));
93 row.setAttribute('entry', entry.id());
94 dndSource.insertNodes(false, [row]);
95 byName(row, 'delete').onclick = function() {
96 entry.isdeleted(true);
97 entryTbody.removeChild(row);
102 ['owning_lib', 'location', 'item_count'],
104 new openils.widget.AutoFieldWidget({
109 parentNode : byName(row, field),
110 orgDefaultsToWs : true,
111 orgLimitPerms : ['ADMIN_ACQ_DISTRIB_FORMULA'],
112 widgetClass : (field == 'item_count') ? 'dijit.form.NumberSpinner' : null,
113 dijitArgs : (field == 'item_count') ? {min:1, places:0} : null
116 dojo.connect(w, 'onChange',
118 entry[field]( newVal );
119 entry.ischanged(true);
128 function saveFormula() {
130 var updatedEntries = [];
131 var deletedEntries = [];
133 // remove deleted entries from consideration for collision protection
134 for(var i = 0; i < formula.entries().length; i++) {
135 if(formula.entries()[i].isdeleted())
136 deletedEntries.push(formula.entries().splice(i--, 1)[0])
139 // update entry positions and create temporary collision avoidance entries
141 dndSource.getAllNodes(),
144 var entryId = node.getAttribute('entry');
145 var entry = formula.entries().filter(function(e) {return (e.id() == entryId)})[0];
147 if(entry.position() != pos) {
149 // update the position
150 var changedEntry = entry.clone();
151 changedEntry.position(pos);
152 changedEntry.ischanged(true);
153 updatedEntries.push(changedEntry);
155 // clear the virtual ID
156 if(changedEntry.isnew())
157 changedEntry.id(null);
159 var oldEntry = formula.entries().filter(function(e) {return (e.position() == pos)})[0];
162 // move the entry currently in that spot temporarily into negative territory
163 var moveMe = oldEntry.clone();
164 moveMe.ischanged(true);
165 moveMe.position(moveMe.position() * -1);
166 updatedEntries.unshift(moveMe);
173 // finally, for every entry that changed w/o changing position
174 // throw it on the list for update
178 if(entry.ischanged() && !entry.isdeleted() && !entry.isnew()) {
179 if(updatedEntries.filter(function(e) { return (e.id() == entry.id()) }).length == 0)
180 updatedEntries.push(entry);
185 updatedEntries = deletedEntries.concat(updatedEntries);
186 if(updatedEntries.length) {
187 pcrud = new openils.PermaCrud();
189 pcrud.apply(updatedEntries);
191 alert('error updating: ' + E);
194 location.href = location.href;