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;
15 var _collection_code_textboxes = [];
16 var _collection_code_kludge_active = false;
18 function gridDataLoader() {
19 fListGrid.resetStore();
20 fListGrid.showLoadProgressIndicator();
21 fieldmapper.standardRequest(
22 ["open-ils.acq", "open-ils.acq.distribution_formula.ranged.retrieve"], {
25 openils.User.authtoken,
26 fListGrid.displayOffset,
27 fListGrid.displayLimit
29 "onresponse": function(r) {
30 var form = openils.Util.readResponse(r);
31 formCache[form.id()] = form;
32 fListGrid.store.newItem(form.toStoreItem());
34 "oncomplete": function() {
35 fListGrid.hideLoadProgressIndicator();
43 pcrud = new openils.PermaCrud();
46 openils.Util.hide('formula-list-div');
50 openils.Util.hide('formula-entry-div');
51 fListGrid.onPostCreate = function(fmObject) {
52 location.href = location.href + '/' + fmObject.id();
55 fListGrid.dataLoader = gridDataLoader;
60 function cloneSelectedFormula() {
61 var item = fListGrid.getSelectedItems()[0];
63 var formula = new fieldmapper.acqf().fromStoreItem(item);
64 fieldmapper.standardRequest(
65 ['open-ils.acq', 'open-ils.acq.distribution_formula.clone'],
69 openils.User.authtoken,
71 dojo.string.substitute(localeStrings.ACQ_DISTRIB_FORMULA_NAME_CLONE, [formula.name()])
73 oncomplete : function(r) {
74 if(r = openils.Util.readResponse(r)) {
75 location.href = oilsBasePath + '/conify/global/acq/distribution_formula/' + r;
82 openils.Util.addOnLoad(draw);
84 function getItemCount(rowIndex, item) {
86 var form = formCache[this.grid.store.getValue(item, "id")];
89 dojo.forEach(form.entries(), function(e) { count = count + e.item_count(); });
93 function byName(node, name) {
94 return dojo.query('[name='+name+']', node)[0];
97 function drawFormulaSummary() {
98 openils.Util.show('formula-entry-div');
100 var entries = pcrud.search('acqdfe', {formula: formulaId}, {order_by:{acqdfe : 'position'}});
101 formula = pcrud.retrieve('acqdf', formulaId);
102 formula.entries(entries);
104 dojo.byId('formula_head').innerHTML = formula.name();
105 dojo.byId('formula_head').onclick = function() {
106 var name = prompt(localeStrings.ACQ_DISTRIB_FORMULA_NAME_PROMPT, formula.name());
107 if(name && name != formula.name()) {
109 pcrud = new openils.PermaCrud();
110 pcrud.update(formula);
111 dojo.byId('formula_head').innerHTML = name;
115 dojo.forEach(entries, function(entry) { addEntry(entry); } );
118 function addEntry(entry) {
121 entryTbody = dojo.byId('formula-entry-tbody');
122 entryTemplate = entryTbody.removeChild(dojo.byId('formula-entry-tempate'));
123 dndSource = new dojo.dnd.Source(entryTbody);
124 dndSource.selectAll();
125 dndSource.deleteSelectedNodes();
126 dndSource.clearItems();
130 entry = new fieldmapper.acqdfe();
131 entry.formula(formulaId);
133 entry.owning_lib(openils.User.user.ws_ou());
134 entry.id(virtualId--);
136 formula.entries().push(entry);
139 var row = entryTbody.appendChild(entryTemplate.cloneNode(true));
140 row.setAttribute('entry', entry.id());
141 dndSource.insertNodes(false, [row]);
142 byName(row, 'delete').onclick = function() {
143 entry.isdeleted(true);
144 entryTbody.removeChild(row);
149 ['owning_lib', 'location', 'fund', 'circ_modifier', 'collection_code', 'item_count'],
151 new openils.widget.AutoFieldWidget({
156 parentNode : byName(row, field),
157 orgDefaultsToWs : true,
158 orgLimitPerms : ['ADMIN_ACQ_DISTRIB_FORMULA'],
159 widgetClass : (field == 'item_count') ? 'dijit.form.NumberSpinner' : null,
160 dijitArgs : (field == 'item_count') ? {min:1, places:0} : null
163 if (field == "collection_code") {
164 /* kludge for glitchy textbox */
165 _collection_code_textboxes.push(w);
167 dojo.connect(w, 'onChange',
169 entry[field]( newVal );
170 entry.ischanged(true);
178 /* For some reason (bug) the dndSource intercepts onMouseDown events
179 * that should hit dijit textboxes in our table thingy. Other dijits
180 * (buttons, filteringselects, etc) seem not to be affected. This
181 * workaround deals with the only textboxes we have for now: the ones
182 * for the collection_code field. */
183 if (!_collection_code_kludge_active) {
184 _collection_code_kludge_active = true;
185 var original = dojo.hitch(dndSource, dndSource.onMouseDown);
186 dndSource.onMouseDown = function(e) {
187 var hits = _collection_code_textboxes.filter(
189 var c = dojo.coords(w.domNode);
190 if (e.clientX >= c.x && e.clientX < c.x + c.w) {
191 if (e.clientY >= c.y && e.clientY < c.y + c.h) {
208 function saveFormula() {
210 var updatedEntries = [];
211 var deletedEntries = [];
213 // remove deleted entries from consideration for collision protection
214 for(var i = 0; i < formula.entries().length; i++) {
215 if(formula.entries()[i].isdeleted())
216 deletedEntries.push(formula.entries().splice(i--, 1)[0])
219 // update entry positions and create temporary collision avoidance entries
221 dndSource.getAllNodes(),
224 var entryId = node.getAttribute('entry');
225 var entry = formula.entries().filter(function(e) {return (e.id() == entryId)})[0];
227 if(entry.position() != pos) {
229 // update the position
230 var changedEntry = entry.clone();
231 changedEntry.position(pos);
232 changedEntry.ischanged(true);
233 updatedEntries.push(changedEntry);
235 // clear the virtual ID
236 if(changedEntry.isnew())
237 changedEntry.id(null);
239 var oldEntry = formula.entries().filter(function(e) {return (e.position() == pos)})[0];
242 // move the entry currently in that spot temporarily into negative territory
243 var moveMe = oldEntry.clone();
244 moveMe.ischanged(true);
245 moveMe.position(moveMe.position() * -1);
246 updatedEntries.unshift(moveMe);
253 // finally, for every entry that changed w/o changing position
254 // throw it on the list for update
258 if(entry.ischanged() && !entry.isdeleted() && !entry.isnew()) {
259 if(updatedEntries.filter(function(e) { return (e.id() == entry.id()) }).length == 0)
260 updatedEntries.push(entry);
265 updatedEntries = deletedEntries.concat(updatedEntries);
266 if(updatedEntries.length) {
267 pcrud = new openils.PermaCrud();
269 pcrud.apply(updatedEntries);
271 alert('error updating: ' + E);
274 location.href = location.href;