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"},
32 this.empty = function(which) {
33 if (this._empty == which) return; /* nothing to do */
35 openils.Util[which ? "show" : "hide"]("acq-po-item-table-i-am-empty");
36 openils.Util[which ? "hide" : "show"](this.tHead, "table-header-group");
40 this.reset = function() {
44 dojo.empty(this.tBody);
50 this.hide = function() { openils.Util.hide("acq-po-item-table"); };
52 this.show = function() { openils.Util.show("acq-po-item-table"); };
54 this.disableSave = function() {
55 dojo.byId("acq-po-item-table-save-new").disabled = true;
58 this.rowIndices = function() {
59 return openils.Util.objectProperties(this.rows);
62 this.newRowIndices = function() {
63 return this.rowIndices().filter(function(o) { return o < 0; });
66 this.saveNew = function() {
67 var virtIds = this.newRowIndices();
68 var po_items = virtIds.map(
70 var widgets = self.rows[k];
71 var po_item = new acqpoi();
72 for (var field in widgets)
73 po_item[field](widgets[field].attr("value"));
74 po_item.purchase_order(self.po.id());
79 progressDialog.show(true);
83 "oncomplete": function(r, objs) {
84 progressDialog.hide();
85 r = openils.Util.readResponse(r); /* may not use */
87 virtIds.forEach(function(k) { self.deleteRow(k); });
88 objs.forEach(function(o) { self.addItem(o); });
94 this._deleteRow = function(id) {
95 dojo.destroy(dojo.query("[rowId='" + id + "']")[0]);
97 delete this.realItems[id];
99 if (!this.rowIndices().length) this.reset();
100 else if (!this.newRowIndices().length) this.disableSave();
103 this.deleteRow = function(id) {
105 progressDialog.show(true);
107 this.realItems[id], {
108 "oncomplete": function(r) {
109 progressDialog.hide();
110 r = openils.Util.readResponse(r); /* may not use */
121 this._addItemRow = function(item) {
122 var ourId = item ? item.id() : this.rowId--;
125 this.realItems[ourId] = item;
127 this.rows[ourId] = {};
128 var row = dojo.clone(this.template);
129 dojo.attr(row, "rowId", ourId);
131 nodeByName("delete", row).onclick = function() {
132 self.deleteRow(ourId);
135 return {"id": ourId, "node": row};
138 /* add a row with widgets for the user to enter new data */
139 this.addItem = function(item) {
140 var row = this._addItemRow(item);
142 dojo.query("td[name]", row.node).forEach(
144 var field = dojo.attr(element, "name");
145 var em = dojo.attr(element, "em");
146 var awArgs = dojo.mixin(
149 "parentNode": dojo.create(
150 "div", {"style": "width: " +
151 String(Number(em) + 1) + "em"},
154 "orgLimitPerms": ["CREATE_PURCHASE_ORDER"],
155 "dijitArgs": {"style": "width: " + em + "em"},
156 "readOnly": Boolean(item)
158 (field == "fund" ? self.fundAWArgs : {}),
159 (item ? {"fmObject": item} : {"fmClass": "acqpoi"})
161 new openils.widget.AutoFieldWidget(awArgs).build(
162 function(w) { self.rows[row.id][field] = w; }
169 dojo.place(row.node, this.tBody, "last");
171 dojo.byId("acq-po-item-table-save-new").disabled = false;
174 this.init.apply(this, arguments);