8752e3499975487a9cd9c6862c9ef67f3a9b8135
[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             fieldmapper.standardRequest(
114                 ['open-ils.acq', 'open-ils.acq.po_item.delete'],
115                 {   async : true,
116                     params: [openils.User.authtoken, id],
117                     oncomplete : function(r) {
118                         progressDialog.hide();
119                         r = openils.Util.readResponse(r); /* may not use */
120                         if (r == '1') {
121                             self._deleteRow(id);
122                         } 
123                     }
124                 }
125             );
126         } else {
127             this._deleteRow(id);
128         }
129     };
130
131     this._addItemRow = function(item) {
132         var ourId = item ? item.id() : this.rowId--;
133
134         if (item)
135             this.realItems[ourId] = item;
136
137         this.rows[ourId] = {};
138         var row = dojo.clone(this.template);
139         dojo.attr(row, "rowId", ourId);
140
141         nodeByName("delete", row).onclick = function() {
142             self.deleteRow(ourId);
143         };
144
145         return {"id": ourId, "node": row};
146     };
147
148     /* add a row with widgets for the user to enter new data */
149     this.addItem = function(item) {
150         var row = this._addItemRow(item);
151
152         dojo.query("td[name]", row.node).forEach(
153             function(element) {
154                 var field = dojo.attr(element, "name");
155                 var em = dojo.attr(element, "em");
156                 var awArgs = dojo.mixin(
157                     {
158                         "fmField": field,
159                         "parentNode": dojo.create(
160                             "div", {"style": "width: " +
161                                 String(Number(em) + 1) + "em"},
162                             element, "only"
163                         ),
164                         "orgLimitPerms": ["CREATE_PURCHASE_ORDER"],
165                         "dijitArgs": {"style": "width: " + em + "em"},
166                         "readOnly": Boolean(item)
167                     },
168                     (field == "fund" ? self.fundAWArgs : {}),
169                     (item ? {"fmObject": item} : {"fmClass": "acqpoi"})
170                 );
171                 new openils.widget.AutoFieldWidget(awArgs).build(
172                     function(w) { self.rows[row.id][field] = w; }
173                 );
174             }
175         );
176
177         this.empty(false);
178
179         dojo.place(row.node, this.tBody, "last");
180         if (!item)
181             dojo.byId("acq-po-item-table-save-new").disabled = false;
182     };
183
184     this.init.apply(this, arguments);
185 }