1 function PoItemTable() {
4 this.init = function(po, pcrud) {
6 this.pcrud = pcrud || new openils.PermaCrud();
8 this.tHead = dojo.byId("acq-po-item-table-headings");
9 this.tBody = dojo.byId("acq-po-item-table-items");
10 this.template = this.tBody.removeChild(dojo.query("tr", this.tBody)[0]);
11 dojo.byId("acq-po-item-table-new-charge").onclick = function() {
14 dojo.byId("acq-po-item-table-save-new").onclick = function() {
19 "searchFilter": {"active": "t"},
20 "searchFormat": ["${0} (${1})", "code", "year"],
22 "<span class='fund_${0}'>${1} (${2})</span>",
25 "dijitArgs": {"labelType": "html"},
29 // limit funds fetched to those the user can use
30 new openils.User().getPermOrgList(
31 ['CREATE_PURCHASE_ORDER', 'MANAGE_FUND'],
32 function(orgs) { self.fundAWArgs.searchFilter.org = orgs },
33 true, true // descendants, id_list
39 this.empty = function(which) {
40 if (this._empty == which) return; /* nothing to do */
42 openils.Util[which ? "show" : "hide"]("acq-po-item-table-i-am-empty");
43 openils.Util[which ? "hide" : "show"](this.tHead, "table-header-group");
47 this.reset = function() {
51 dojo.empty(this.tBody);
57 this.hide = function() { openils.Util.hide("acq-po-item-table"); };
59 this.show = function() { openils.Util.show("acq-po-item-table"); };
61 this.disableSave = function() {
62 dojo.byId("acq-po-item-table-save-new").disabled = true;
65 this.rowIndices = function() {
66 return openils.Util.objectProperties(this.rows);
69 this.newRowIndices = function() {
70 return this.rowIndices().filter(function(o) { return o < 0; });
73 this.saveNew = function() {
74 var virtIds = this.newRowIndices();
75 var po_items = virtIds.map(
77 var widgets = self.rows[k];
78 var po_item = new acqpoi();
79 for (var field in widgets)
80 po_item[field](widgets[field].attr("value"));
81 po_item.purchase_order(self.po.id());
86 progressDialog.show(true);
90 "oncomplete": function(r, objs) {
91 progressDialog.hide();
92 r = openils.Util.readResponse(r); /* may not use */
94 virtIds.forEach(function(k) { self.deleteRow(k); });
95 objs.forEach(function(o) { self.addItem(o); });
101 this._deleteRow = function(id) {
102 dojo.destroy(dojo.query("[rowId='" + id + "']")[0]);
103 delete this.rows[id];
104 delete this.realItems[id];
106 if (!this.rowIndices().length) this.reset();
107 else if (!this.newRowIndices().length) this.disableSave();
110 this.deleteRow = function(id) {
112 progressDialog.show(true);
113 fieldmapper.standardRequest(
114 ['open-ils.acq', 'open-ils.acq.po_item.delete'],
116 params: [openils.User.authtoken, id],
117 oncomplete : function(r) {
118 progressDialog.hide();
119 r = openils.Util.readResponse(r); /* may not use */
131 this._addItemRow = function(item) {
132 var ourId = item ? item.id() : this.rowId--;
135 this.realItems[ourId] = item;
137 this.rows[ourId] = {};
138 var row = dojo.clone(this.template);
139 dojo.attr(row, "rowId", ourId);
141 nodeByName("delete", row).onclick = function() {
142 self.deleteRow(ourId);
145 return {"id": ourId, "node": row};
148 /* add a row with widgets for the user to enter new data */
149 this.addItem = function(item) {
150 var row = this._addItemRow(item);
152 dojo.query("td[name]", row.node).forEach(
154 var field = dojo.attr(element, "name");
155 var em = dojo.attr(element, "em");
156 var awArgs = dojo.mixin(
159 "parentNode": dojo.create(
160 "div", {"style": "width: " +
161 String(Number(em) + 1) + "em"},
164 "orgLimitPerms": ["CREATE_PURCHASE_ORDER"],
165 "dijitArgs": {"style": "width: " + em + "em"},
166 "readOnly": Boolean(item)
168 (field == "fund" ? self.fundAWArgs : {}),
169 (item ? {"fmObject": item} : {"fmClass": "acqpoi"})
171 new openils.widget.AutoFieldWidget(awArgs).build(
172 function(w) { self.rows[row.id][field] = w; }
179 dojo.place(row.node, this.tBody, "last");
181 dojo.byId("acq-po-item-table-save-new").disabled = false;
184 this.init.apply(this, arguments);