]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/acq/lineitem/search.js
Acq: fix minor bug, sometimes view/place orders interface failed if you clicked
[working/Evergreen.git] / Open-ILS / web / js / ui / default / acq / lineitem / search.js
1 dojo.require("dijit.form.Form");
2 dojo.require("dijit.form.Button");
3 dojo.require("dijit.form.RadioButton");
4 dojo.require("dijit.form.TextBox");
5 dojo.require("dijit.form.FilteringSelect");
6 dojo.require("dojo.data.ItemFileReadStore");
7 dojo.require("openils.User");
8 dojo.require("openils.Util");
9 dojo.require("openils.PermaCrud");
10 dojo.require("openils.XUL");
11 dojo.require("openils.widget.AutoFieldWidget");
12
13 var combinedAttrValueArray = [];
14 var scalarAttrSearchManager;
15 var liTable;
16
17 function prepareStateStore(pcrud) {
18     stateSelector.store = new dojo.data.ItemFileReadStore({
19         "data": {
20             "label": "description",
21             "identifier": "code",
22             "items": [
23                 /* XXX i18n; Also, this list shouldn't be hardcoded here. */
24                 {"code": "new", "description": "New"},
25                 {"code": "on-order", "description": "On Order"},
26                 {"code": "pending-order", "description": "Pending Order"}
27             ]
28         }
29     });
30 }
31
32 function prepareScalarSearchStore(pcrud) {
33 }
34
35 function prepareArraySearchStore(pcrud) {
36     attrArrayDefSelector.store = new dojo.data.ItemFileReadStore({
37         "data": acqliad.toStoreData(
38             pcrud.search("acqliad", {"code": li_exportable_attrs})
39         )
40     });
41 }
42
43 function prepareAgencySelector() {
44     new openils.widget.AutoFieldWidget({
45         "fmClass": "acqpo",
46         "fmField": "ordering_agency",
47         "parentNode": dojo.byId("agency_selector"),
48         "orgLimitPerms": ["VIEW_PURCHASE_ORDER"],
49         "dijitArgs": {"name": "agency", "required": false}
50     }).build();
51 }
52
53 function toggleAttrSearchType(which, checked) {
54     /* This would be cooler with a slick dispatch table instead of branchy
55      * logic, but whatever... */
56     if (checked) {
57         if (which == "scalar") {
58             if (scalarAttrSearchManager.index < 1)
59                 scalarAttrSearchManager.add();
60             openils.Util.show("oils-acq-li-search-attr-scalar", "inline-block");
61             openils.Util.hide("oils-acq-li-search-attr-array");
62         } else if (which == "array") {
63             openils.Util.hide("oils-acq-li-search-attr-scalar");
64             openils.Util.show("oils-acq-li-search-attr-array", "inline");
65         } else {
66             openils.Util.hide("oils-acq-li-search-attr-scalar");
67             openils.Util.hide("oils-acq-li-search-attr-array");
68         }
69     }
70 }
71
72 var buildAttrSearchClause = {
73     "array": function(v) {
74         if (!v.array_def) {
75             throw new Error(localeStrings.SELECT_AN_LI_ATTRIBUTE);
76         }
77         return {
78             "attr_value_pairs":
79                 [[Number(v.array_def), combinedAttrValueArray]] /* [[sic]] */
80         };
81     },
82     "scalar": function(v) {
83         var r = scalarAttrSearchManager.buildSearchClause();
84         if (r.attr_value_pairs.length < 1) {
85             throw new Error(localeStrings.SELECT_AN_LI_ATTRIBUTE);
86         } else {
87             return r;
88         }
89     },
90     "none": function(v) {
91         return {};
92     }
93 };
94
95 function naivelyParse(data) {
96     return data.split(/[\n, ]/).filter(function(o) {return o.length > 0; });
97 }
98
99 function clearTerms() {
100     combinedAttrValueArray = [];
101     dojo.byId("records-up").innerHTML = 0;
102 }
103
104 function loadTermsFromFile() {
105     var rawdata;
106
107     try {
108         /* FIXME 128k is completely arbitrary; needs researched for
109          * a sane limit and should also be made configurable. */
110         rawdata = openils.XUL.contentFromFileOpenDialog(
111             localeStrings.LI_ATTR_SEARCH_CHOOSE_FILE, 1024 * 128
112         );
113     } catch (E) {
114         alert(E);
115     }
116
117     if (rawdata) {
118         try {
119             combinedAttrValueArray =
120                 combinedAttrValueArray.concat(naivelyParse(rawdata));
121             dojo.byId("records-up").innerHTML = combinedAttrValueArray.length;
122         } catch (E) {
123             alert(E);
124         }
125     }
126 }
127
128 function buildSearchClause(values) {
129     var o = {};
130     if (values.state) o.li_states = [values.state];
131     if (values.agency) o.po_agencies = [Number(values.agency)];
132     return o;
133 }
134
135 function doSearch(values) {
136     var results_this_time = 0;
137     liTable.reset();
138     try {
139         fieldmapper.standardRequest(
140             ["open-ils.acq", "open-ils.acq.lineitem.search.by_attributes"], {
141                 "params": [
142                     openils.User.authtoken,
143                     dojo.mixin(
144                         buildAttrSearchClause[values.attr_search_type](values),
145                         buildSearchClause(values)
146                     ),
147                     {
148                         "clear_marc": true, "flesh_attrs": true,
149                         "flesh_notes": true
150                     }
151                 ],
152                 "async": true,
153                 "onresponse": function(r) {
154                     var li = openils.Util.readResponse(r);
155                     if (li) {
156                         results_this_time++;
157                         liTable.addLineitem(li);
158                         liTable.show("list");
159                     }
160                 },
161                 "oncomplete": function() {
162                     if (results_this_time < 1) {
163                         alert(localeStrings.NO_RESULTS);
164                     }
165                 }
166             }
167         );
168     } catch (E) {
169         alert(E); // XXX
170     }
171 }
172
173 function myScalarAttrSearchManager(template_id, pcrud) {
174     this.template = dojo.byId(template_id);
175     this.store = new dojo.data.ItemFileReadStore({
176         "data": acqliad.toStoreData(
177             pcrud.search("acqliad", {"id": {"!=": null}})
178         )
179     });
180     this.rows = {};
181     this.index = 0;
182 };
183 myScalarAttrSearchManager.prototype.remove = function(n) {
184     dojo.destroy("scalar_attr_holder_" + n);
185     delete this.rows[n];
186 };
187 myScalarAttrSearchManager.prototype.add = function() {
188     var self = this;
189     var n = this.index;
190     var clone = dojo.clone(this.template);
191     var def = dojo.query('input[name="def"]', clone)[0];
192     var value = dojo.query('input[name="value"]', clone)[0];
193     var a = dojo.query('a', clone)[0];
194
195     clone.id = "scalar_attr_holder_" + n;
196     a.onclick = function() { self.remove(n); };
197
198     this.rows[n] = [
199         new dijit.form.FilteringSelect({
200             "id": "scalar_def_" + n,
201             "name": "scalar_def_" + n,
202             "store": this.store,
203             "labelAttr": "description",
204             "searchAttr": "description"
205         }, def),
206         new dijit.form.TextBox({
207             "id": "scalar_value_" + n,
208             "name": "scalar_value_" + n
209         }, value)
210     ];
211
212     this.index++;
213
214     dojo.place(clone, "oils-acq-li-search-scalar-adder", "before");
215     openils.Util.show(clone);
216 };
217 myScalarAttrSearchManager.prototype.buildSearchClause = function() {
218     var list = [];
219     for (var k in this.rows) {
220         var def = this.rows[k][0].attr("value");
221         var val = this.rows[k][1].attr("value");
222         if (def != "" && val != "")
223             list.push([Number(def), val]);
224     }
225     return {"attr_value_pairs": list};
226 };
227 myScalarAttrSearchManager.prototype.simplifiedPairs = function() {
228     var result = {};
229     for (var k in this.rows) {
230         result[this.rows[k][0].attr("value")] = this.rows[k][1].attr("value");
231     }
232     return result;
233 };
234 myScalarAttrSearchManager.prototype.newBrief = function() {
235     location.href = oilsBasePath + "/acq/picklist/brief_record?prepop=" +
236         encodeURIComponent(js2JSON(this.simplifiedPairs()));
237 };
238
239
240 function load() {
241     var pcrud = new openils.PermaCrud();
242
243     prepareStateStore(pcrud);
244     prepareArraySearchStore(pcrud);
245
246     prepareAgencySelector();
247
248     liTable = new AcqLiTable();
249     scalarAttrSearchManager = new myScalarAttrSearchManager(
250         "oils-acq-li-search-scalar-template", pcrud
251     );
252
253     openils.Util.show("oils-acq-li-search-form-holder");
254 }
255
256 openils.Util.addOnLoad(load);