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