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