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;
17 var fundSearchFilter = {active : 't'};
19 function gridDataLoader() {
20 fListGrid.resetStore();
21 fListGrid.showLoadProgressIndicator();
22 fieldmapper.standardRequest(
23 ["open-ils.acq", "open-ils.acq.distribution_formula.ranged.retrieve"], {
26 openils.User.authtoken,
27 fListGrid.displayOffset,
28 fListGrid.displayLimit
30 "onresponse": function(r) {
31 var form = openils.Util.readResponse(r);
32 formCache[form.id()] = form;
33 fListGrid.store.newItem(form.toStoreItem());
35 "oncomplete": function() {
36 fListGrid.hideLoadProgressIndicator();
42 function setFundSearchFilter(callback) {
43 new openils.User().getPermOrgList(
44 ['ADMIN_ACQ_DISTRIB_FORMULA'],
46 fundSearchFilter.org = orgs;
47 if (callback) callback();
49 true, true // descendants, id_list
55 pcrud = new openils.PermaCrud();
58 openils.Util.hide('formula-list-div');
59 setFundSearchFilter(drawFormulaSummary);
62 openils.Util.hide('formula-entry-div');
63 fListGrid.onPostCreate = function(fmObject) {
64 location.href = location.href + '/' + fmObject.id();
67 fListGrid.dataLoader = gridDataLoader;
72 function cloneSelectedFormula() {
73 var item = fListGrid.getSelectedItems()[0];
75 var formula = new fieldmapper.acqf().fromStoreItem(item);
76 fieldmapper.standardRequest(
77 ['open-ils.acq', 'open-ils.acq.distribution_formula.clone'],
81 openils.User.authtoken,
83 dojo.string.substitute(localeStrings.ACQ_DISTRIB_FORMULA_NAME_CLONE, [formula.name()])
85 oncomplete : function(r) {
86 if(r = openils.Util.readResponse(r)) {
87 location.href = oilsBasePath + '/conify/global/acq/distribution_formula/' + r;
94 openils.Util.addOnLoad(draw);
96 function getItemCount(rowIndex, item) {
98 var form = formCache[this.grid.store.getValue(item, "id")];
101 dojo.forEach(form.entries(), function(e) { count = count + e.item_count(); });
105 function byName(node, name) {
106 return dojo.query('[name='+name+']', node)[0];
109 function drawFormulaSummary() {
110 openils.Util.show('formula-entry-div');
112 var entries = pcrud.search('acqdfe', {formula: formulaId}, {order_by:{acqdfe : 'position'}});
113 formula = pcrud.retrieve('acqdf', formulaId);
114 formula.entries(entries);
116 dojo.byId('formula_head').innerHTML = formula.name();
117 dojo.byId('formula_head').onclick = function() {
118 var name = prompt(localeStrings.ACQ_DISTRIB_FORMULA_NAME_PROMPT, formula.name());
119 if(name && name != formula.name()) {
121 pcrud = new openils.PermaCrud();
122 pcrud.update(formula);
123 dojo.byId('formula_head').innerHTML = name;
127 dojo.forEach(entries, function(entry) { addEntry(entry); } );
130 function addEntry(entry) {
133 entryTbody = dojo.byId('formula-entry-tbody');
134 entryTemplate = entryTbody.removeChild(dojo.byId('formula-entry-tempate'));
135 dndSource = new dojo.dnd.Source(entryTbody);
136 dndSource.selectAll();
137 dndSource.deleteSelectedNodes();
138 dndSource.clearItems();
142 entry = new fieldmapper.acqdfe();
143 entry.formula(formulaId);
145 entry.owning_lib(openils.User.user.ws_ou());
146 entry.id(virtualId--);
148 formula.entries().push(entry);
151 var row = entryTbody.appendChild(entryTemplate.cloneNode(true));
152 row.setAttribute('entry', entry.id());
153 dndSource.insertNodes(false, [row]);
154 byName(row, 'delete').onclick = function() {
155 entry.isdeleted(true);
156 entryTbody.removeChild(row);
161 ['owning_lib', 'location', 'fund', 'circ_modifier', 'collection_code', 'item_count'],
163 new openils.widget.AutoFieldWidget({
168 searchFilter : (field == 'fund') ? fundSearchFilter : null,
169 parentNode : byName(row, field),
170 orgDefaultsToWs : true,
171 orgLimitPerms : ['ADMIN_ACQ_DISTRIB_FORMULA'],
172 widgetClass : (field == 'item_count') ? 'dijit.form.NumberSpinner' : null,
173 dijitArgs : (field == 'item_count') ? {min:1, places:0} : null
176 if (field == "collection_code") {
177 /* kludge for glitchy textbox */
178 _collection_code_textboxes.push(w);
180 dojo.connect(w, 'onChange',
182 entry[field]( newVal );
183 entry.ischanged(true);
191 /* For some reason (bug) the dndSource intercepts onMouseDown events
192 * that should hit dijit textboxes in our table thingy. Other dijits
193 * (buttons, filteringselects, etc) seem not to be affected. This
194 * workaround deals with the only textboxes we have for now: the ones
195 * for the collection_code field. */
196 if (!_collection_code_kludge_active) {
197 _collection_code_kludge_active = true;
198 var original = dojo.hitch(dndSource, dndSource.onMouseDown);
199 dndSource.onMouseDown = function(e) {
200 var hits = _collection_code_textboxes.filter(
202 var c = dojo.coords(w.domNode);
203 if (e.clientX >= c.x && e.clientX < c.x + c.w) {
204 if (e.clientY >= c.y && e.clientY < c.y + c.h) {
221 function saveFormula() {
223 var updatedEntries = [];
224 var deletedEntries = [];
226 // remove deleted entries from consideration for collision protection
227 for(var i = 0; i < formula.entries().length; i++) {
228 if(formula.entries()[i].isdeleted())
229 deletedEntries.push(formula.entries().splice(i--, 1)[0])
232 // update entry positions and create temporary collision avoidance entries
234 dndSource.getAllNodes(),
237 var entryId = node.getAttribute('entry');
238 var entry = formula.entries().filter(function(e) {return (e.id() == entryId)})[0];
240 if(entry.position() != pos) {
242 // update the position
243 var changedEntry = entry.clone();
244 changedEntry.position(pos);
245 changedEntry.ischanged(true);
246 updatedEntries.push(changedEntry);
248 // clear the virtual ID
249 if(changedEntry.isnew())
250 changedEntry.id(null);
252 var oldEntry = formula.entries().filter(function(e) {return (e.position() == pos)})[0];
255 // move the entry currently in that spot temporarily into negative territory
256 var moveMe = oldEntry.clone();
257 moveMe.ischanged(true);
258 moveMe.position(moveMe.position() * -1);
259 updatedEntries.unshift(moveMe);
266 // finally, for every entry that changed w/o changing position
267 // throw it on the list for update
271 if(entry.ischanged() && !entry.isdeleted() && !entry.isnew()) {
272 if(updatedEntries.filter(function(e) { return (e.id() == entry.id()) }).length == 0)
273 updatedEntries.push(entry);
278 updatedEntries = deletedEntries.concat(updatedEntries);
279 if(updatedEntries.length) {
280 pcrud = new openils.PermaCrud();
282 pcrud.apply(updatedEntries);
284 alert('error updating: ' + E);
287 location.href = location.href;