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'};
18 var fundLabelFormat = ['${0} (${1})', 'code', 'year'];
21 function gridDataLoader() {
22 fListGrid.resetStore();
23 fListGrid.showLoadProgressIndicator();
24 fieldmapper.standardRequest(
25 ["open-ils.acq", "open-ils.acq.distribution_formula.ranged.retrieve"], {
28 openils.User.authtoken,
29 fListGrid.displayOffset,
30 fListGrid.displayLimit
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();
44 function setFundSearchFilter(callback) {
45 new openils.User().getPermOrgList(
46 ['ADMIN_ACQ_DISTRIB_FORMULA'],
48 fundSearchFilter.org = orgs;
49 if (callback) callback();
51 true, true // descendants, id_list
57 pcrud = new openils.PermaCrud();
60 openils.Util.hide('formula-list-div');
61 setFundSearchFilter(drawFormulaSummary);
64 openils.Util.hide('formula-entry-div');
65 fListGrid.onPostCreate = function(fmObject) {
66 location.href = location.href + '/' + fmObject.id();
69 fListGrid.dataLoader = gridDataLoader;
74 function cloneSelectedFormula() {
75 var item = fListGrid.getSelectedItems()[0];
77 var formula = new fieldmapper.acqf().fromStoreItem(item);
78 fieldmapper.standardRequest(
79 ['open-ils.acq', 'open-ils.acq.distribution_formula.clone'],
83 openils.User.authtoken,
85 dojo.string.substitute(localeStrings.ACQ_DISTRIB_FORMULA_NAME_CLONE, [formula.name()])
87 oncomplete : function(r) {
88 if(r = openils.Util.readResponse(r)) {
89 location.href = oilsBasePath + '/conify/global/acq/distribution_formula/' + r;
96 openils.Util.addOnLoad(draw);
98 function getItemCount(rowIndex, item) {
100 var form = formCache[this.grid.store.getValue(item, "id")];
103 dojo.forEach(form.entries(), function(e) { count = count + e.item_count(); });
107 function byName(node, name) {
108 return dojo.query('[name='+name+']', node)[0];
111 function drawFormulaSummary() {
112 openils.Util.show('formula-entry-div');
114 var entries = pcrud.search('acqdfe', {formula: formulaId}, {order_by:{acqdfe : 'position'}});
115 formula = pcrud.retrieve('acqdf', formulaId);
116 formula.entries(entries);
118 dojo.byId('formula_head').innerHTML = formula.name();
119 dojo.byId('formula_head').onclick = function() {
120 var name = prompt(localeStrings.ACQ_DISTRIB_FORMULA_NAME_PROMPT, formula.name());
121 if(name && name != formula.name()) {
123 pcrud = new openils.PermaCrud();
124 pcrud.update(formula);
125 dojo.byId('formula_head').innerHTML = name;
129 dojo.forEach(entries, function(entry) { addEntry(entry); } );
132 function addEntry(entry) {
135 entryTbody = dojo.byId('formula-entry-tbody');
136 entryTemplate = entryTbody.removeChild(dojo.byId('formula-entry-tempate'));
137 dndSource = new dojo.dnd.Source(entryTbody);
138 dndSource.selectAll();
139 dndSource.deleteSelectedNodes();
140 dndSource.clearItems();
144 entry = new fieldmapper.acqdfe();
145 entry.formula(formulaId);
147 entry.owning_lib(openils.User.user.ws_ou());
148 entry.id(virtualId--);
150 formula.entries().push(entry);
153 var row = entryTbody.appendChild(entryTemplate.cloneNode(true));
154 row.setAttribute('entry', entry.id());
155 dndSource.insertNodes(false, [row]);
156 byName(row, 'delete').onclick = function() {
157 entry.isdeleted(true);
158 entryTbody.removeChild(row);
163 ['owning_lib', 'location', 'fund', 'circ_modifier', 'collection_code', 'item_count'],
165 new openils.widget.AutoFieldWidget({
170 labelFormat: (field == 'fund') ? fundLabelFormat : null,
171 searchFilter : (field == 'fund') ? fundSearchFilter : null,
172 parentNode : byName(row, field),
173 orgDefaultsToWs : true,
174 orgLimitPerms : ['ADMIN_ACQ_DISTRIB_FORMULA'],
175 widgetClass : (field == 'item_count') ? 'dijit.form.NumberSpinner' : null,
176 dijitArgs : (field == 'item_count') ? {min:1, places:0} : null
179 if (field == "collection_code") {
180 /* kludge for glitchy textbox */
181 _collection_code_textboxes.push(w);
183 dojo.connect(w, 'onChange',
185 entry[field]( newVal );
186 entry.ischanged(true);
194 /* For some reason (bug) the dndSource intercepts onMouseDown events
195 * that should hit dijit textboxes in our table thingy. Other dijits
196 * (buttons, filteringselects, etc) seem not to be affected. This
197 * workaround deals with the only textboxes we have for now: the ones
198 * for the collection_code field. */
199 if (!_collection_code_kludge_active) {
200 _collection_code_kludge_active = true;
201 var original = dojo.hitch(dndSource, dndSource.onMouseDown);
202 dndSource.onMouseDown = function(e) {
203 var hits = _collection_code_textboxes.filter(
205 var c = dojo.coords(w.domNode);
206 if (e.clientX >= c.x && e.clientX < c.x + c.w) {
207 if (e.clientY >= c.y && e.clientY < c.y + c.h) {
224 function saveFormula() {
226 var updatedEntries = [];
227 var deletedEntries = [];
229 // remove deleted entries from consideration for collision protection
230 for(var i = 0; i < formula.entries().length; i++) {
231 if(formula.entries()[i].isdeleted())
232 deletedEntries.push(formula.entries().splice(i--, 1)[0])
235 // update entry positions and create temporary collision avoidance entries
237 dndSource.getAllNodes(),
240 var entryId = node.getAttribute('entry');
241 var entry = formula.entries().filter(function(e) {return (e.id() == entryId)})[0];
243 if(entry.position() != pos) {
245 // update the position
246 var changedEntry = entry.clone();
247 changedEntry.position(pos);
248 changedEntry.ischanged(true);
249 updatedEntries.push(changedEntry);
251 // clear the virtual ID
252 if(changedEntry.isnew())
253 changedEntry.id(null);
255 var oldEntry = formula.entries().filter(function(e) {return (e.position() == pos)})[0];
258 // move the entry currently in that spot temporarily into negative territory
259 var moveMe = oldEntry.clone();
260 moveMe.ischanged(true);
261 moveMe.position(moveMe.position() * -1);
262 updatedEntries.unshift(moveMe);
269 // finally, for every entry that changed w/o changing position
270 // throw it on the list for update
274 if(entry.ischanged() && !entry.isdeleted() && !entry.isnew()) {
275 if(updatedEntries.filter(function(e) { return (e.id() == entry.id()) }).length == 0)
276 updatedEntries.push(entry);
281 updatedEntries = deletedEntries.concat(updatedEntries);
282 if(updatedEntries.length) {
283 pcrud = new openils.PermaCrud();
285 pcrud.apply(updatedEntries);
287 alert('error updating: ' + E);
290 location.href = location.href;