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');
7 dojo.requireLocalization('openils.conify', 'conify');
8 var localeStrings = dojo.i18n.getLocalization('openils.conify', 'conify');
12 var formula, entryTbody, entryTemplate, dndSource;
19 pcrud = new openils.PermaCrud();
22 openils.Util.hide('formula-list-div');
26 openils.Util.hide('formula-entry-div');
27 fListGrid.onPostCreate = function(fmObject) {
28 location.href = location.href + '/' + fmObject.id();
31 fieldmapper.standardRequest(
32 ['open-ils.acq', 'open-ils.acq.distribution_formula.ranged.retrieve'],
34 params: [openils.User.authtoken],
35 onresponse: function (r) {
36 var form = openils.Util.readResponse(r);
37 formCache[form.id()] = form;
38 fListGrid.store.newItem(form.toStoreItem());
40 oncomplete: function() {
41 fListGrid.hideLoadProgressIndicator();
49 function cloneSelectedFormula() {
50 var item = fListGrid.getSelectedItems()[0];
52 var formula = new fieldmapper.acqf().fromStoreItem(item);
53 fieldmapper.standardRequest(
54 ['open-ils.acq', 'open-ils.acq.distribution_formula.clone'],
58 openils.User.authtoken,
60 dojo.string.substitute(localeStrings.ACQ_DISTRIB_FORMULA_NAME_CLONE, [formula.name()])
62 oncomplete : function(r) {
63 if(r = openils.Util.readResponse(r)) {
64 location.href = oilsBasePath + '/conify/global/acq/distribution_formula/' + r;
71 openils.Util.addOnLoad(draw);
73 function getItemCount(rowIndex, item) {
75 var form = formCache[this.grid.store.getValue(item, "id")];
78 dojo.forEach(form.entries(), function(e) { count = count + e.item_count(); });
82 function byName(node, name) {
83 return dojo.query('[name='+name+']', node)[0];
86 function drawFormulaSummary() {
87 openils.Util.show('formula-entry-div');
89 var entries = pcrud.search('acqdfe', {formula: formulaId}, {order_by:{acqdfe : 'position'}});
90 formula = pcrud.retrieve('acqdf', formulaId);
91 formula.entries(entries);
93 dojo.byId('formula_head').innerHTML = formula.name();
94 dojo.byId('formula_head').onclick = function() {
95 var name = prompt(localeStrings.ACQ_DISTRIB_FORMULA_NAME_PROMPT, formula.name());
96 if(name && name != formula.name()) {
98 pcrud = new openils.PermaCrud();
99 pcrud.update(formula);
100 dojo.byId('formula_head').innerHTML = name;
104 dojo.forEach(entries, function(entry) { addEntry(entry); } );
107 function addEntry(entry) {
110 entryTbody = dojo.byId('formula-entry-tbody');
111 entryTemplate = entryTbody.removeChild(dojo.byId('formula-entry-tempate'));
112 dndSource = new dojo.dnd.Source(entryTbody);
113 dndSource.selectAll();
114 dndSource.deleteSelectedNodes();
115 dndSource.clearItems();
119 entry = new fieldmapper.acqdfe();
120 entry.formula(formulaId);
122 entry.owning_lib(openils.User.user.ws_ou());
123 entry.id(virtualId--);
125 formula.entries().push(entry);
128 var row = entryTbody.appendChild(entryTemplate.cloneNode(true));
129 row.setAttribute('entry', entry.id());
130 dndSource.insertNodes(false, [row]);
131 byName(row, 'delete').onclick = function() {
132 entry.isdeleted(true);
133 entryTbody.removeChild(row);
138 ['owning_lib', 'location', 'item_count'],
140 new openils.widget.AutoFieldWidget({
145 parentNode : byName(row, field),
146 orgDefaultsToWs : true,
147 orgLimitPerms : ['ADMIN_ACQ_DISTRIB_FORMULA'],
148 widgetClass : (field == 'item_count') ? 'dijit.form.NumberSpinner' : null,
149 dijitArgs : (field == 'item_count') ? {min:1, places:0} : null
152 dojo.connect(w, 'onChange',
154 entry[field]( newVal );
155 entry.ischanged(true);
164 function saveFormula() {
166 var updatedEntries = [];
167 var deletedEntries = [];
169 // remove deleted entries from consideration for collision protection
170 for(var i = 0; i < formula.entries().length; i++) {
171 if(formula.entries()[i].isdeleted())
172 deletedEntries.push(formula.entries().splice(i--, 1)[0])
175 // update entry positions and create temporary collision avoidance entries
177 dndSource.getAllNodes(),
180 var entryId = node.getAttribute('entry');
181 var entry = formula.entries().filter(function(e) {return (e.id() == entryId)})[0];
183 if(entry.position() != pos) {
185 // update the position
186 var changedEntry = entry.clone();
187 changedEntry.position(pos);
188 changedEntry.ischanged(true);
189 updatedEntries.push(changedEntry);
191 // clear the virtual ID
192 if(changedEntry.isnew())
193 changedEntry.id(null);
195 var oldEntry = formula.entries().filter(function(e) {return (e.position() == pos)})[0];
198 // move the entry currently in that spot temporarily into negative territory
199 var moveMe = oldEntry.clone();
200 moveMe.ischanged(true);
201 moveMe.position(moveMe.position() * -1);
202 updatedEntries.unshift(moveMe);
209 // finally, for every entry that changed w/o changing position
210 // throw it on the list for update
214 if(entry.ischanged() && !entry.isdeleted() && !entry.isnew()) {
215 if(updatedEntries.filter(function(e) { return (e.id() == entry.id()) }).length == 0)
216 updatedEntries.push(entry);
221 updatedEntries = deletedEntries.concat(updatedEntries);
222 if(updatedEntries.length) {
223 pcrud = new openils.PermaCrud();
225 pcrud.apply(updatedEntries);
227 alert('error updating: ' + E);
230 location.href = location.href;