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