]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/serial/subscription.js
Alternate Serial Control UI misc bugfixes, minor tweaks and improvements.
[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 /* typing save: add {get,set}Value() to all HTML <select> elements */
21 HTMLSelectElement.prototype.getValue = function() {
22     return this.options[this.selectedIndex].value;
23 }
24 HTMLSelectElement.prototype.setValue = function(s) {
25     for (var i = 0; i < this.options.length; i++) {
26         if (s == this.options[i].value) {
27             this.selectedIndex = i;
28             break;
29         }
30     }
31 }
32
33 function load_sub_grid(id, oncomplete) {
34     if (!pcrud) return; /* first run, onLoad hasn't fired yet */
35     if (!sub_grid._fresh) {
36         var dist_ids = pcrud.search(
37             "sdist", {"subscription": id}, {"id_list": true}
38         );
39         pcrud.retrieve(
40             "ssub", id, {
41                 "onresponse": function(r) {
42                     if (r = openils.Util.readResponse(r)) {
43                         sub = r;
44                         var data = ssub.toStoreData([r]);
45                         data.items[0].num_dist = dist_ids ? dist_ids.length : 0;
46                         sub_grid.setStore(
47                             new dojo.data.ItemFileReadStore({"data": data})
48                         );
49                         sub_grid._fresh = true;
50                     }
51                 },
52                 "oncomplete": function() {
53                     if (oncomplete) oncomplete();
54                 }
55             }
56         );
57     }
58 }
59
60 /* TODO: make these formatters caching */
61 function format_bib(bib_id) {
62     if (!bib_id) {
63         return "";
64     } else {
65         var result;
66         fieldmapper.standardRequest(
67             ["open-ils.search",
68                 "open-ils.search.biblio.record.mods_slim.retrieve"], {
69                 "async": false,
70                 "params": [bib_id],
71                 "oncomplete": function(r) {
72                     if (r = openils.Util.readResponse(r)) {
73                         var parts = [];
74                         if (r.title())
75                             parts.push(r.title());
76                         if (r.author())
77                             parts.push(r.author());
78                         if (r.author())
79                             parts.push(r.publisher());
80
81                         if (!parts.length)
82                             parts.push(r.tcn());
83
84                         result = parts.join(" / ");
85                     }
86                 }
87             }
88         );
89         return "<a href='" + oilsBasePath +
90             "/serial/list_subscription?record_entry=" + bib_id + "'>" +
91             result + "</a>";
92     }
93 }
94
95 function format_date(s) {
96     return s ? openils.Util.timeStamp(s, {"selector": "date"}) : "";
97 }
98
99 function format_org_unit(aou_id) {
100     return aou_id ? aou.findOrgUnit(aou_id).shortname() : "";
101 }
102
103 function get_sdist(rowIndex, item) {
104     if (!item) return {"id": "", "label": ""};
105     return {
106         "id": this.grid.store.getValue(item, "id"),
107         "label": this.grid.store.getValue(item, "label")
108     };
109 }
110
111 function format_sdist_label(blob) {
112     if (!blob.id) return "";
113     var link = "<a href='" +
114         oilsBasePath + "/serial/list_stream?distribution=" + blob.id +
115         "'>" + (blob.label ? blob.label : "[None]") + "</a>"; /* XXX i18n */
116
117     var sstr_list = pcrud.search(
118         "sstr",{"distribution":blob.id},{"id_list":true}
119     );
120     count = sstr_list ? sstr_list.length : 0;
121     link += "&nbsp;&nbsp; " + count + " stream(s)";
122     return link;
123 }
124
125 function append_stream_count(dist_id) {
126     var span = dojo.byId("dist_link_" + dist_id);
127     if (span.childNodes.length) /* textNodes count as childnodes */
128         return;
129     pcrud.search(
130         "sstr", {"distribution": dist_id}, {
131             "id_list": true,
132             "oncomplete": function(r) {
133                 var resp = openils.Util.readResponse(r);
134                 var count = resp ? resp.length : 0;
135
136                 /* XXX i18n */
137                 span.innerHTML = "&nbsp;&nbsp; " + count + " stream(s)";
138             }
139         }
140     );
141 }
142
143 function open_batch_receive() {
144     if (!sub) {
145         alert("Let the interface load all the way first.");
146         return;
147     }
148
149     var url = "/xul/server/serial/batch_receive.xul?docid=" +
150         sub.record_entry() + "&subid=" + sub.id();
151
152     try {
153         openils.XUL.newTabEasy(url, "Batch Receive"); /* XXX i18n */
154     } catch (E) {
155         location.href = url;
156     }
157 }
158
159 openils.Util.addOnLoad(
160     function() {
161         cgi = new openils.CGI();
162         pcrud = new openils.PermaCrud();
163
164         sub_id = cgi.param("id");
165         load_sub_grid(
166             sub_id,
167             (cgi.param("tab") == "distributions") ?
168                 function() { tab_container.selectChild(distributions_tab); } :
169                 null
170         );
171     }
172 );