Populate variable needed for batch item receive
[working/Evergreen.git] / Open-ILS / web / js / ui / default / serial / subscription.js
1 dojo.require("dijit.form.Button");
2 dojo.require("dijit.form.RadioButton");
3 dojo.require("dijit.form.FilteringSelect");
4 dojo.require("dijit.form.DropDownButton");
5 dojo.require("dijit.TooltipDialog");
6 dojo.require("dijit.layout.TabContainer");
7 dojo.require("dijit.layout.ContentPane");
8 dojo.require("dojox.grid.DataGrid");
9 dojo.require("openils.widget.AutoGrid");
10 dojo.require("openils.widget.ProgressDialog");
11 dojo.require("openils.widget.HoldingCode");
12 dojo.require("openils.PermaCrud");
13 dojo.require("openils.CGI");
14 dojo.requireLocalization('openils.serial', 'serial');
15 var localeStrings = dojo.i18n.getLocalization('openils.serial', 'serial');
16
17 var pcrud;
18 var cgi;
19 var sub;
20 var sub_id;
21 var context_url_param;
22
23 function node_by_name(name, ctx) {
24     return dojo.query("[name='" + name + "']", ctx)[0];
25 }
26
27 /* typing save: add {get,set}Value() to all HTML <select> elements */
28 HTMLSelectElement.prototype.getValue = function() {
29     return this.options[this.selectedIndex].value;
30 }
31 HTMLSelectElement.prototype.setValue = function(s) {
32     for (var i = 0; i < this.options.length; i++) {
33         if (s == this.options[i].value) {
34             this.selectedIndex = i;
35             break;
36         }
37     }
38 }
39
40 function load_sub_grid(id, oncomplete) {
41     if (!pcrud) return; /* first run, onLoad hasn't fired yet */
42     if (!sub_grid._fresh) {
43         var dist_ids = pcrud.search(
44             "sdist", {"subscription": id}, {"id_list": true}
45         );
46         pcrud.retrieve(
47             "ssub", id, {
48                 "onresponse": function(r) {
49                     if (r = openils.Util.readResponse(r)) {
50                         sub = r;
51                         var data = ssub.toStoreData([r]);
52                         data.items[0].num_dist = dist_ids ? dist_ids.length : 0;
53                         sub_grid.setStore(
54                             new dojo.data.ItemFileReadStore({"data": data})
55                         );
56                         sub_grid._fresh = true;
57                     }
58                 },
59                 "oncomplete": function() {
60                     if (oncomplete) oncomplete();
61                 }
62             }
63         );
64     }
65 }
66
67 /* TODO: make these formatters caching */
68 function format_bib(bib_id) {
69     if (!bib_id) {
70         return "";
71     } else {
72         var result;
73         fieldmapper.standardRequest(
74             ["open-ils.search",
75                 "open-ils.search.biblio.record.mods_slim.retrieve"], {
76                 "async": false,
77                 "params": [bib_id],
78                 "oncomplete": function(r) {
79                     if (r = openils.Util.readResponse(r)) {
80                         var parts = [];
81                         if (r.title())
82                             parts.push(r.title());
83                         if (r.author())
84                             parts.push(r.author());
85                         if (r.author())
86                             parts.push(r.publisher());
87
88                         if (!parts.length)
89                             parts.push(r.tcn());
90
91                         result = parts.join(" / ");
92                     }
93                 }
94             }
95         );
96         return "<a href='" + oilsBasePath +
97             "/serial/list_subscription?record_entry=" + bib_id + "'>" +
98             result + "</a>";
99     }
100 }
101
102 function format_date(s) {
103     return s ? openils.Util.timeStamp(s, {"selector": "date"}) : "";
104 }
105
106 function format_org_unit(aou_id) {
107     return aou_id ? aou.findOrgUnit(aou_id).shortname() : "";
108 }
109
110 function get_id_and_label(rowIndex, item) {
111     if (!item) return {"id": "", "label": ""};
112     return {
113         "id": this.grid.store.getValue(item, "id"),
114         "label": this.grid.store.getValue(item, "label")
115     };
116 }
117
118 function format_siss_label(blob) {
119     if (!blob.id) return "";
120     return "<a href='" +
121         oilsBasePath + "/serial/list_item?issuance=" + blob.id +
122         context_url_param + "'>" + (blob.label ? blob.label : "[None]") +
123         "</a>"; /* XXX i18n */
124 }
125
126 function format_sdist_label(blob) {
127     if (!blob.id) return "";
128     var link = "<a href='" +
129         oilsBasePath + "/serial/list_stream?distribution=" + blob.id +
130         context_url_param + "'>" + (blob.label ? blob.label : "[None]") +
131         "</a>"; /* XXX i18n */
132
133     var sstr_list = pcrud.search(
134         "sstr",{"distribution":blob.id},{"id_list":true}
135     );
136     count = sstr_list ? sstr_list.length : 0;
137     link += "&nbsp;&nbsp; " + count + " stream(s)"; //XXX i18n
138     return link;
139 }
140
141 function append_stream_count(dist_id) {
142     var span = dojo.byId("dist_link_" + dist_id);
143     if (span.childNodes.length) /* textNodes count as childnodes */
144         return;
145     pcrud.search(
146         "sstr", {"distribution": dist_id}, {
147             "id_list": true,
148             "oncomplete": function(r) {
149                 var resp = openils.Util.readResponse(r);
150                 var count = resp ? resp.length : 0;
151
152                 /* XXX i18n */
153                 span.innerHTML = "&nbsp;&nbsp; " + count + " stream(s)";
154             }
155         }
156     );
157 }
158
159 function open_batch_receive() {
160     if (!sub) {
161         alert("Let the interface load all the way first.");
162         return;
163     }
164
165     var url = "XUL_SERIAL_BATCH_RECEIVE?docid=" +
166         sub.record_entry() + "&subid=" + sub.id();
167
168     try {
169         openils.XUL.newTabEasy(url, "Batch Receive"); /* XXX i18n */
170     } catch (E) {
171         location.href = url;
172     }
173 }
174
175 function toggle_clone_ident_field(dij) {
176     setTimeout(
177         function() {
178             var disabled = !dij.attr("checked");
179             clone_ident.attr("disabled", disabled);
180             if (!disabled) clone_ident.focus();
181         }, 175
182     );
183 }
184
185 function clone_subscription(form) {
186     if (form.use_ident == "yes") {
187         fieldmapper.standardRequest(
188             ["open-ils.serial",
189                 "open-ils.serial.biblio.record_entry.by_identifier.atomic"], {
190                 "params": [form.ident, {"id_list": true}],
191                 "async": true,
192                 "oncomplete": function(r) {
193                     r = openils.Util.readResponse(r);
194                     if (!r || !r.length) {
195                         alert("No matches for that indentifier."); /*XXX i18n*/
196                     } else if (r.length != 1) {
197                         alert("Too many matches for that identifier. Use a " +
198                             "unique identifier."); /* XXX i18n */
199                     } else {
200                         _clone_subscription(r[0]);
201                     }
202                 }
203             }
204         );
205     } else {
206         _clone_subscription();
207     }
208 }
209
210 function _clone_subscription(bre_id) {
211     progress_dialog.show(true);
212
213     fieldmapper.standardRequest(
214         ["open-ils.serial", "open-ils.serial.subscription.clone"], {
215             "params": [openils.User.authtoken, sub_id, bre_id],
216             "async": false,
217             "oncomplete": function(r) {
218                 progress_dialog.hide();
219                 if (!(r = openils.Util.readResponse(r))) {
220                     alert("Error cloning subscription."); /* XXX i18n */
221                 } else {
222                     location.href =
223                         oilsBasePath + "/serial/subscription?id=" + r;
224                 }
225
226                 /* cloning doesn't clone holdings, so nothing changes at
227                  * OPAC view just because of this, so no need to try
228                  * reload_opac().  */
229             }
230         }
231     );
232 }
233
234 function open_notes(obj_type, grid) {
235     if (grid.getSelectedRows().length != 1) {
236         alert( localeStrings.REQUIRE_ONE_ROW );
237         return;
238     }
239
240     var id = grid.getSelectedItems()[0].id[0];
241     var args_by_obj_type = {
242         'sub' : { 'function_type' : 'SSUBN', 'object_type' : 'subscription', 'constructor' : ssubn, 'title' : dojo.string.substitute(localeStrings.NOTES_SSUB, [id]) },
243         'dist' : { 'function_type' : 'SDISTN', 'object_type' : 'distribution', 'constructor' : sdistn, 'title' : dojo.string.substitute(localeStrings.NOTES_SDIST, [id]) }
244     };
245     args_by_obj_type[obj_type].object_id = id;
246
247     try {
248         window.openDialog(
249             xulG.url_prefix('XUL_SERIAL_NOTES'),
250             obj_type+'_notes',
251             'chrome,resizable,modal',
252             args_by_obj_type[obj_type]
253         );
254     } catch (E) {
255         alert(E); /* XXX */
256     }
257 }
258
259 openils.Util.addOnLoad(
260     function() {
261         var tab_dispatch = {
262             "distributions": distributions_tab,
263             "issuances": issuances_tab
264         };
265
266         cgi = new openils.CGI();
267         pcrud = new openils.PermaCrud();
268
269         context = cgi.param("context");
270         sub_id = cgi.param("id");
271         owning_lib = cgi.param("owning_lib");
272         record_entry = cgi.param("record_entry");
273
274         if (context) {
275             context_url_param = '&context=' + context;
276         } else {
277             context_url_param = '';
278         }
279
280         if (context != 'scv') {
281             load_sub_grid(
282                 sub_id,
283                 (cgi.param("tab") in tab_dispatch) ?
284                     function() {
285                         tab_container.selectChild(
286                             tab_dispatch[cgi.param("tab")]
287                         );
288                     } : null
289             );
290         } else {
291             build_sre_maps(dist_grid);
292             dist_grid.empty_store = new dojo.data.ItemFileReadStore({
293                 "data": {
294                     "identifier": "record_entry",
295                     "label": "label",
296                     "items": []
297                 }
298             })
299             dist_grid.overrideEditWidgets.record_entry =
300                 new dijit.form.FilteringSelect({
301                     "store" : dist_grid.empty_store,
302                     "searchAttr" : "label",
303                     "name" : "record_entry"
304                 });
305             dist_grid.overrideEditWidgets.record_entry.shove = {};
306             if (sub_id == 'new') {
307                 ssub_grid.overrideEditWidgets.record_entry =
308                         new dijit.form.TextBox({
309                             "disabled": true, "value": record_entry
310                         });
311                 ssub_grid.overrideWidgetArgs.owning_lib = {widgetValue : owning_lib, dijitArgs : {disabled : true}};
312
313                 ssub_grid.onPostCreate = function(fmObject) {
314                     sub_id = fmObject.id();
315                     parent.document.getElementById(window.name).refresh_command(fmObject);
316                 }
317
318                 ssub_grid.showCreateDialog();
319             }
320             ssub_grid.onPostUpdate = function(fmObject) {
321                 parent.document.getElementById(window.name).refresh_command();
322             }
323             ssub_grid.onItemReceived = function(item) {
324                 sub = item;
325             }
326             if (cgi.param("tab") in tab_dispatch) {
327                 ssub_grid._fresh = false; // force View/Edit tab to reload (otherwise, it is blank) XXX why?
328                 tab_container.selectChild(tab_dispatch[cgi.param("tab")]);
329             }
330             parent.document.getElementById(window.name).style.visibility = 'visible'; // unhide the editor pane (iframe)
331         }
332     }
333 );