]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/serial/subscription.js
Serials: provide a way for users to create and modify serial items
[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/server/serial/batch_receive.xul?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 openils.Util.addOnLoad(
171     function() {
172         var tab_dispatch = {
173             "distributions": distributions_tab,
174             "issuances": issuances_tab
175         };
176
177         cgi = new openils.CGI();
178         pcrud = new openils.PermaCrud();
179
180         sub_id = cgi.param("id");
181         load_sub_grid(
182             sub_id,
183             (cgi.param("tab") in tab_dispatch) ?
184                 function() {
185                     tab_container.selectChild(
186                         tab_dispatch[cgi.param("tab")]
187                     );
188                 } : null
189         );
190     }
191 );