2cfac2f24794b81f5dcdbdeafe139e72d9c0e728
[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
15 var pcrud;
16 var cgi;
17 var sub;
18 var sub_id;
19
20 function node_by_name(name, ctx) {
21     return dojo.query("[name='" + name + "']", ctx)[0];
22 }
23
24 /* typing save: add {get,set}Value() to all HTML <select> elements */
25 HTMLSelectElement.prototype.getValue = function() {
26     return this.options[this.selectedIndex].value;
27 }
28 HTMLSelectElement.prototype.setValue = function(s) {
29     for (var i = 0; i < this.options.length; i++) {
30         if (s == this.options[i].value) {
31             this.selectedIndex = i;
32             break;
33         }
34     }
35 }
36
37 function load_sub_grid(id, oncomplete) {
38     if (!pcrud) return; /* first run, onLoad hasn't fired yet */
39     if (!sub_grid._fresh) {
40         var dist_ids = pcrud.search(
41             "sdist", {"subscription": id}, {"id_list": true}
42         );
43         pcrud.retrieve(
44             "ssub", id, {
45                 "onresponse": function(r) {
46                     if (r = openils.Util.readResponse(r)) {
47                         sub = r;
48                         var data = ssub.toStoreData([r]);
49                         data.items[0].num_dist = dist_ids ? dist_ids.length : 0;
50                         sub_grid.setStore(
51                             new dojo.data.ItemFileReadStore({"data": data})
52                         );
53                         sub_grid._fresh = true;
54                     }
55                 },
56                 "oncomplete": function() {
57                     if (oncomplete) oncomplete();
58                 }
59             }
60         );
61     }
62 }
63
64 /* TODO: make these formatters caching */
65 function format_bib(bib_id) {
66     if (!bib_id) {
67         return "";
68     } else {
69         var result;
70         fieldmapper.standardRequest(
71             ["open-ils.search",
72                 "open-ils.search.biblio.record.mods_slim.retrieve"], {
73                 "async": false,
74                 "params": [bib_id],
75                 "oncomplete": function(r) {
76                     if (r = openils.Util.readResponse(r)) {
77                         var parts = [];
78                         if (r.title())
79                             parts.push(r.title());
80                         if (r.author())
81                             parts.push(r.author());
82                         if (r.author())
83                             parts.push(r.publisher());
84
85                         if (!parts.length)
86                             parts.push(r.tcn());
87
88                         result = parts.join(" / ");
89                     }
90                 }
91             }
92         );
93         return "<a href='" + oilsBasePath +
94             "/serial/list_subscription?record_entry=" + bib_id + "'>" +
95             result + "</a>";
96     }
97 }
98
99 function format_date(s) {
100     return s ? openils.Util.timeStamp(s, {"selector": "date"}) : "";
101 }
102
103 function format_org_unit(aou_id) {
104     return aou_id ? aou.findOrgUnit(aou_id).shortname() : "";
105 }
106
107 function get_id_and_label(rowIndex, item) {
108     if (!item) return {"id": "", "label": ""};
109     return {
110         "id": this.grid.store.getValue(item, "id"),
111         "label": this.grid.store.getValue(item, "label")
112     };
113 }
114
115 function format_siss_label(blob) {
116     if (!blob.id) return "";
117     return "<a href='" +
118         oilsBasePath + "/serial/list_item?issuance=" + blob.id +
119         "'>" + (blob.label ? blob.label : "[None]") + "</a>"; /* XXX i18n */
120 }
121
122 function format_sdist_label(blob) {
123     if (!blob.id) return "";
124     var link = "<a href='" +
125         oilsBasePath + "/serial/list_stream?distribution=" + blob.id +
126         "'>" + (blob.label ? blob.label : "[None]") + "</a>"; /* XXX i18n */
127
128     var sstr_list = pcrud.search(
129         "sstr",{"distribution":blob.id},{"id_list":true}
130     );
131     count = sstr_list ? sstr_list.length : 0;
132     link += "&nbsp;&nbsp; " + count + " stream(s)";
133     return link;
134 }
135
136 function append_stream_count(dist_id) {
137     var span = dojo.byId("dist_link_" + dist_id);
138     if (span.childNodes.length) /* textNodes count as childnodes */
139         return;
140     pcrud.search(
141         "sstr", {"distribution": dist_id}, {
142             "id_list": true,
143             "oncomplete": function(r) {
144                 var resp = openils.Util.readResponse(r);
145                 var count = resp ? resp.length : 0;
146
147                 /* XXX i18n */
148                 span.innerHTML = "&nbsp;&nbsp; " + count + " stream(s)";
149             }
150         }
151     );
152 }
153
154 function open_batch_receive() {
155     if (!sub) {
156         alert("Let the interface load all the way first.");
157         return;
158     }
159
160     var url = "XUL_SERIAL_BATCH_RECEIVE?docid=" +
161         sub.record_entry() + "&subid=" + sub.id();
162
163     try {
164         openils.XUL.newTabEasy(url, "Batch Receive"); /* XXX i18n */
165     } catch (E) {
166         location.href = url;
167     }
168 }
169
170 function toggle_clone_ident_field(dij) {
171     setTimeout(
172         function() {
173             var disabled = !dij.attr("checked");
174             clone_ident.attr("disabled", disabled);
175             if (!disabled) clone_ident.focus();
176         }, 175
177     );
178 }
179
180 function clone_subscription(form) {
181     if (form.use_ident == "yes") {
182         fieldmapper.standardRequest(
183             ["open-ils.serial",
184                 "open-ils.serial.biblio.record_entry.by_identifier.atomic"], {
185                 "params": [form.ident, {"id_list": true}],
186                 "async": true,
187                 "oncomplete": function(r) {
188                     r = openils.Util.readResponse(r);
189                     if (!r || !r.length) {
190                         alert("No matches for that indentifier."); /*XXX i18n*/
191                     } else if (r.length != 1) {
192                         alert("Too many matches for that identifier. Use a " +
193                             "unique identifier."); /* XXX i18n */
194                     } else {
195                         _clone_subscription(r[0]);
196                     }
197                 }
198             }
199         );
200     } else {
201         _clone_subscription();
202     }
203 }
204
205 function _clone_subscription(bre_id) {
206     progress_dialog.show(true);
207
208     fieldmapper.standardRequest(
209         ["open-ils.serial", "open-ils.serial.subscription.clone"], {
210             "params": [openils.User.authtoken, sub_id, bre_id],
211             "async": false,
212             "oncomplete": function(r) {
213                 progress_dialog.hide();
214                 if (!(r = openils.Util.readResponse(r))) {
215                     alert("Error cloning subscription."); /* XXX i18n */
216                 } else {
217                     location.href =
218                         oilsBasePath + "/serial/subscription?id=" + r;
219                 }
220
221                 /* cloning doesn't clone holdings, so nothing changes at
222                  * OPAC view just because of this, so no need to try
223                  * reload_opac().  */
224             }
225         }
226     );
227 }
228
229 openils.Util.addOnLoad(
230     function() {
231         var tab_dispatch = {
232             "distributions": distributions_tab,
233             "issuances": issuances_tab
234         };
235
236         cgi = new openils.CGI();
237         pcrud = new openils.PermaCrud();
238
239         sub_id = cgi.param("id");
240         load_sub_grid(
241             sub_id,
242             (cgi.param("tab") in tab_dispatch) ?
243                 function() {
244                     tab_container.selectChild(
245                         tab_dispatch[cgi.param("tab")]
246                     );
247                 } : null
248         );
249     }
250 );