8455c82d869b2f9a420472c1ee59fe121475f72a
[working/Evergreen.git] / Open-ILS / web / js / ui / default / serial / list_item.js
1 dojo.require("dijit.form.Button");
2 dojo.require("dijit.form.DateTextBox");
3 dojo.require("dijit.form.TextBox");
4 dojo.require("dijit.form.NumberSpinner");
5 dojo.require("dijit.form.FilteringSelect");
6 dojo.require("openils.widget.PCrudAutocompleteBox");
7 dojo.require("openils.widget.AutoGrid");
8 dojo.require("openils.widget.ProgressDialog");
9 dojo.require("openils.PermaCrud");
10 dojo.require("openils.CGI");
11
12 var pcrud, cgi, issuance_id;
13 var sitem_cache = {};
14
15 function load_sitem_grid() {
16     sitem_grid.overrideEditWidgets.status = status_selector;
17     sitem_grid.overrideEditWidgets.status.shove = {};   /* sic */
18
19     sitem_grid.dataLoader = sitem_data_loader;
20     sitem_grid.dataLoader();
21 }
22
23 function load_siss_display() {
24     pcrud.retrieve(
25         "siss", issuance_id, {
26             "onresponse": function(r) {
27                 if (r = openils.Util.readResponse(r)) {
28                     var link = dojo.byId("siss_label_here");
29                     link.onclick = function() {
30                         location.href = oilsBasePath +
31                             "/eg/serial/subscription?id=" +
32                             r.subscription() + "&tab=issuances";
33                     }
34                     link.innerHTML = r.label();
35                     prepare_create_dialog(r.subscription());
36                 }
37             }
38         }
39     );
40 }
41
42 function sitem_data_loader() {
43     sitem_grid.resetStore();
44     sitem_grid.showLoadProgressIndicator();
45
46     fieldmapper.standardRequest(
47         ["open-ils.serial", "open-ils.serial.items.by_issuance"], {
48             "params": [
49                 openils.User.authtoken, issuance_id, {
50                     "limit": sitem_grid.displayLimit,
51                     "offset": sitem_grid.displayOffset
52                 }
53             ],
54             "async": true,
55             "onresponse": function(r) {
56                 var item = openils.Util.readResponse(r);
57                 sitem_cache[item.id()] = item;
58                 sitem_grid.store.newItem(item.toStoreItem());
59             },
60             "oncomplete": function(r) {
61                 sitem_grid.hideLoadProgressIndicator();
62             }
63         }
64     );
65 }
66
67 function _get_field(store, item, field) {
68     if (!item) return "";
69     var id = store.getValue(item, "id");
70     return sitem_cache[id][field]();
71 }
72
73 /* create the get_foo() functions used by our AutoGrid */
74 ["creator", "editor", "stream", "unit"].forEach(
75     function(field) {
76         window["get_" + field] = function(row_index, item) {
77             return _get_field(this.grid.store, item, field);
78         };
79     }
80 );
81
82 function format_user(user) {
83     return user ? user.usrname() : "";
84 }
85
86 function format_stream(stream) {
87     return stream ? (stream.routing_label() || "[None]") : ""; /* XXX i18n */
88 }
89
90 function format_unit(unit) {
91     return unit ? (unit.barcode() || "[None]") : ""; /* XXX i18n */
92 }
93
94 function update_sitem_safely(obj, opts, edit_pane) {
95     fieldmapper.standardRequest(
96         ["open-ils.serial", "open-ils.serial.item.update"], {
97             "params": [openils.User.authtoken, obj],
98             "async": true,
99             "oncomplete": function(r) {
100                 if (r = openils.Util.readResponse(r)) {
101                     if (edit_pane.onPostSubmit)
102                         edit_pane.onPostSubmit(null, [r]);
103                 }
104             }
105         }
106     );
107 }
108
109 function prepare_create_dialog(sub_id) {
110     pcrud.search(
111         "sdist", {"subscription": sub_id}, {
112             "id_list": true,
113             "async": true,
114             "oncomplete": function(r) {
115                 if (r = openils.Util.readResponse(r)) {
116                     new openils.widget.PCrudAutocompleteBox({
117                         "fmclass": "sstr",
118                         "searchAttr": "routing_label",
119                         "hasDownArrow": true,
120                         "name": "stream",
121                         "store_options": {
122                             "base_filter": {"distribution": r},
123                             "honor_retrieve_all": true
124                         }
125                     }, "stream_selector");
126                 }
127             }
128         }
129     );
130 }
131
132 function create_new_items(form) {
133     var item = new sitem();
134  
135     item.issuance(issuance_id);    /* from global */
136     item.stream(form.stream);
137     item.status(form.status);
138     item.date_expected(
139         form.date_expected ?
140             dojo.date.stamp.toISOString(form.date_expected) : null
141     );
142     item.date_received(
143         form.date_received ?
144             dojo.date.stamp.toISOString(form.date_received) : null
145     );
146
147     progress_dialog.show(true);
148     fieldmapper.standardRequest(
149         ["open-ils.serial", "open-ils.serial.item.create"], {
150             "params": [openils.User.authtoken, item, form.count],
151             "async": true,
152             "oncomplete": function(r) {
153                 progress_dialog.hide();
154                 if (r = openils.Util.readResponse(r)) {
155                     sitem_grid.refresh();
156                 }
157             }
158         }
159     );
160 }
161
162 openils.Util.addOnLoad(
163     function() {
164         cgi = new openils.CGI();
165         pcrud = new openils.PermaCrud();
166
167         issuance_id = cgi.param("issuance");
168         load_siss_display();
169         load_sitem_grid();
170     }
171 );