]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/acq/po/item_table.js
3c1f9a55c58d125b28136325754fc226b4a8caa6
[working/Evergreen.git] / Open-ILS / web / js / ui / default / acq / po / item_table.js
1 function PoItemTable() {
2     var self = this;
3
4     this.init = function(po, pcrud) {
5         this.po = po;
6         this.pcrud = pcrud || new openils.PermaCrud();
7
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() {
12             self.addItem();
13         };
14         dojo.byId("acq-po-item-table-save-new").onclick = function() {
15             self.saveNew();
16         };
17
18         this.fundAWArgs = {
19             "searchFilter": {"active": "t"},
20             "searchFormat": ["${0} (${1})", "code", "year"],
21             "labelFormat": [
22                 "<span class='fund_${0}'>${1} (${2})</span>",
23                 "id", "code", "year"
24             ],
25             "dijitArgs": {"labelType": "html"},
26             "noCache": true
27         };
28
29         this.reset();
30     };
31
32     this.empty = function(which) {
33         if (this._empty == which) return; /* nothing to do */
34
35         openils.Util[which ? "show" : "hide"]("acq-po-item-table-i-am-empty");
36         openils.Util[which ? "hide" : "show"](this.tHead, "table-header-group");
37         this._empty = which;
38     };
39
40     this.reset = function() {
41         this.rowId = -1;
42         this.rows = {};
43         this.realItems = {};
44         dojo.empty(this.tBody);
45         this.empty(true);
46
47         this.disableSave();
48     };
49
50     this.hide = function() { openils.Util.hide("acq-po-item-table"); };
51
52     this.show = function() { openils.Util.show("acq-po-item-table"); };
53
54     this.disableSave = function() {
55         dojo.byId("acq-po-item-table-save-new").disabled = true;
56     };
57
58     this.rowIndices = function() {
59         return openils.Util.objectProperties(this.rows);
60     };
61
62     this.newRowIndices = function() {
63         return this.rowIndices().filter(function(o) { return o < 0; });
64     };
65
66     this.saveNew = function() {
67         var virtIds = this.newRowIndices();
68         var po_items = virtIds.map(
69             function(k) {
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());
75                 return po_item;
76             }
77         );
78
79         progressDialog.show(true);
80
81         pcrud.create(
82             po_items, {
83                 "oncomplete": function(r, objs) {
84                     progressDialog.hide();
85                     r = openils.Util.readResponse(r); /* may not use */
86
87                     virtIds.forEach(function(k) { self.deleteRow(k); });
88                     objs.forEach(function(o) { self.addItem(o); });
89                 }
90             }
91         );
92     };
93
94     this._deleteRow = function(id) {
95         dojo.destroy(dojo.query("[rowId='" + id + "']")[0]);
96         delete this.rows[id];
97         delete this.realItems[id];
98
99         if (!this.rowIndices().length) this.reset();
100         else if (!this.newRowIndices().length) this.disableSave();
101     };
102
103     this.deleteRow = function(id) {
104         if (id > 0) {
105             progressDialog.show(true);
106             pcrud.eliminate(
107                 this.realItems[id], {
108                     "oncomplete": function(r) {
109                         progressDialog.hide();
110                         r = openils.Util.readResponse(r); /* may not use */
111
112                         self._deleteRow(id);
113                     }
114                 }
115             );
116         } else {
117             this._deleteRow(id);
118         }
119     };
120
121     this._addItemRow = function(item) {
122         var ourId = item ? item.id() : this.rowId--;
123
124         if (item)
125             this.realItems[ourId] = item;
126
127         this.rows[ourId] = {};
128         var row = dojo.clone(this.template);
129         dojo.attr(row, "rowId", ourId);
130
131         nodeByName("delete", row).onclick = function() {
132             self.deleteRow(ourId);
133         };
134
135         return {"id": ourId, "node": row};
136     };
137
138     /* add a row with widgets for the user to enter new data */
139     this.addItem = function(item) {
140         var row = this._addItemRow(item);
141
142         dojo.query("td[name]", row.node).forEach(
143             function(element) {
144                 var field = dojo.attr(element, "name");
145                 var em = dojo.attr(element, "em");
146                 var awArgs = dojo.mixin(
147                     {
148                         "fmField": field,
149                         "parentNode": dojo.create(
150                             "div", {"style": "width: " +
151                                 String(Number(em) + 1) + "em"},
152                             element, "only"
153                         ),
154                         "orgLimitPerms": ["CREATE_PURCHASE_ORDER"],
155                         "dijitArgs": {"style": "width: " + em + "em"},
156                         "readOnly": Boolean(item)
157                     },
158                     (field == "fund" ? self.fundAWArgs : {}),
159                     (item ? {"fmObject": item} : {"fmClass": "acqpoi"})
160                 );
161                 new openils.widget.AutoFieldWidget(awArgs).build(
162                     function(w) { self.rows[row.id][field] = w; }
163                 );
164             }
165         );
166
167         this.empty(false);
168
169         dojo.place(row.node, this.tBody, "last");
170         if (!item)
171             dojo.byId("acq-po-item-table-save-new").disabled = false;
172     };
173
174     this.init.apply(this, arguments);
175 }