]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/opac/skin/default/js/authbrowse.js
Merge branch 'master' of git.evergreen-ils.org:Evergreen into template-toolkit-opac...
[working/Evergreen.git] / Open-ILS / web / opac / skin / default / js / authbrowse.js
1 dojo.require("openils.CGI");
2 dojo.require("openils.Util");
3 dojo.require("MARC.FixedFields");
4 dojo.require("openils.AuthorityControlSet");
5 var cgi, acs_helper, last_fetched_length = 0;
6
7 attachEvt("common", "init", doAuthorityBrowse);
8
9 /* repeatable, supports all args or no args */
10 function doAuthorityBrowse(axis, term, page, per_page) {
11     swapCanvas(dojo.byId("loading_alt"));
12
13     if (!axis) {
14         if (!cgi) cgi = new openils.CGI();
15         axis = cgi.param(PARAM_AUTHORITY_BROWSE_AXIS);
16         term = cgi.param(PARAM_AUTHORITY_BROWSE_TERM);
17         page = cgi.param(PARAM_AUTHORITY_BROWSE_PAGE) || 0;
18         per_page = cgi.param(PARAM_AUTHORITY_BROWSE_PER_PAGE) || 21;
19     }
20
21     var url = '/opac/extras/browse/marcxml/authority.'
22         + axis
23         + '/1' /* this will be OU if OU ever means anything for authorities */
24         + '/' + term /* FIXME urlescape or however it's spelt */
25         + '/' + page
26         + '/' + per_page
27     ;
28     dojo.xhrGet({
29         "url": url,
30         "handleAs": "xml",
31         "content": {"format": "marcxml"},
32         "preventCache": true,
33         "load": function(doc) {
34             displayAuthorityRecords(axis, doc);
35             setPagingLinks(axis, term, page, per_page);
36         }
37     });
38 }
39
40 function setPagingLinks(axis, term, page, per_page) {
41     var up_page = Number(page) - 1;
42     var down_page = Number(page) + 1;
43
44     unHideMe(dojo.byId("authority-page-up"));
45     dojo.attr(
46         "authority-page-up", "onclick", function() {
47             doAuthorityBrowse(axis, term, up_page, per_page);
48         }
49     );
50
51     /* XXX In theory this would generally stop the "next page" link from
52      * showing up when it's unwanted, but in practice the supercat/unapi
53      * call we make doesn't return the number of records it's supposed to.
54      */
55 //    if (last_fetched_length == per_page) {
56         unHideMe(dojo.byId("authority-page-down"));
57         dojo.attr(
58             "authority-page-down", "onclick", function() {
59                 doAuthorityBrowse(axis, term, down_page, per_page);
60             }
61         );
62 //    } else {
63 //        hideMe(dojo.byId("authority-page-down"));
64 //    }
65 }
66
67 function renderAuthorityTagContent(m, af) {
68     /* XXX This doesn't take into account possible tag repeatability -- a
69      * bona fide library scientist could probably improve this. :-)
70      */
71     if (af.tag() && af.sf_list()) {
72         return dojo.filter(
73             dojo.map(
74                 af.sf_list().split(""),
75                 function(code) {
76                     var result = m.subfield(af.tag(), code);
77                     return (typeof(result[1]) == "undefined") ? "" : result[1];
78                 }
79             ), function(datum) { return datum.length > 0; }
80         ).join(" ");
81     } else {
82         return "";
83     }
84 }
85
86 function renderAuthoritySubEntry(m, field, tbody) {
87     var content =
88         openils.Util.trimString(renderAuthorityTagContent(m, field));
89     if (!content.length) return;    /* don't display empty tags */
90
91     var tr = dojo.create("tr", null, tbody);
92     dojo.create("td", {"style": {"width": "2em"}, "innerHTML": ""}, tr);
93     dojo.create(
94         "td", {
95             "className": "authority-tag-label",
96             "innerHTML": field.name() + ":",
97             "title": field.description() || ""
98         }, tr
99     );
100     dojo.create(
101         "td", {
102             "className": "authority-tag-content authority-record-right",
103             "innerHTML": content
104         }, tr
105     );
106
107     if (field.sub_entries() && field.sub_entries().length) {
108         /* I *think* this shouldn't happen with good data? */
109         console.log("I, too, have " + field.sub_entries().length +
110             "sub_entries");
111     }
112 }
113
114 function renderAuthorityMainEntry(m, field, tbody) {
115     var content =
116         openils.Util.trimString(renderAuthorityTagContent(m, field));
117     if (!content.length) return;    /* don't display empty tags */
118
119     var tr = dojo.create("tr", null, tbody);
120     var content_holder = dojo.create(
121         "td", {
122             "className": "authority-tag-content authority-record-main",
123             "colspan": 2
124         }, tr
125     );
126     dojo.create(
127         "span",
128         {"className":"authority-content", "innerHTML": content},
129         content_holder
130     );
131     dojo.create("span", {"className":"authority-count-holder"}, content_holder);
132     dojo.create(
133         "td", {
134             "className": "authority-tag-label authority-record-right",
135             "innerHTML": field.name(),
136             "title": field.description() || ""
137         }, tr
138     );
139
140     if (field.sub_entries()) {
141         dojo.forEach(
142             field.sub_entries(),
143             function(f) { renderAuthoritySubEntry(m, f, tbody); }
144         );
145     }
146 }
147
148 function renderAuthorityRecord(m, control_set, auth_id) {
149     var main_entries = openils.Util.objectSort(
150         dojo.filter(
151             control_set.authority_fields(),
152             function(o) { return o.main_entry() == null; }
153         ), "tag"
154     );
155
156     var table = dojo.create("table", {"className": "authority-record"});
157     var tbody = dojo.create("tbody", {"id": "authority_" + auth_id}, table);
158
159
160     dojo.forEach(
161         main_entries, function(af) { renderAuthorityMainEntry(m, af, tbody); }
162     );
163
164     return table;
165 }
166
167 /* displayAuthorityRecords: given a DOM document object that contains marcxml
168  * records, display each one in a table using the apporiate control set to
169  * determine which fields to show.
170  */
171 function displayAuthorityRecords(axis, doc) {
172     if (!acs_helper)
173         acs_helper = new openils.AuthorityControlSet();
174
175     /* XXX I wanted to use bibtemplate here, but now I'm not sure it makes
176      * sense: the template itself would have to be dynamic, as it would vary
177      * from record to record when different control sets were in use.
178      */
179     var auth_ids = [];
180
181     dojo.empty("authority-record-holder");
182
183     var records = dojo.query("record", doc);
184     last_fetched_length = records.length;
185
186     dojo.forEach(
187         records,
188         function(record) {
189             var m = new MARC.Record({"xml": record});
190
191             var auth_id = m.subfield("901","c")[1];
192             auth_ids.push(auth_id);
193
194             /* best guess */
195             var t = m.field(/^1/);
196             if (dojo.isArray(t)) t = t[0];
197
198             var cs = acs_helper.findControlSetsForAuthorityTag( t.tag );
199             if (dojo.isArray(cs)) cs = cs[0];
200
201             acs_helper.controlSetId( cs );
202
203             dojo.place(
204                 renderAuthorityRecord(m, acs_helper.controlSet().raw, auth_id),
205                 "authority-record-holder"
206             );
207         }
208     );
209     displayRecordCounts(axis, auth_ids);
210     swapCanvas(dojo.byId("canvas_main"));
211 }
212
213 function displayRecordCounts(axis, auth_ids) {
214     fieldmapper.standardRequest(
215         ["open-ils.cat", "open-ils.cat.authority.records.count_linked_bibs"], {
216             "params": [auth_ids],
217             "async": true,
218             "oncomplete": function(r) {
219                 if ((r = openils.Util.readResponse(r))) {
220                     dojo.forEach(r, function(blob) {
221                         if (blob.bibs > 0) {
222                             displayRecordCount(axis, blob.authority, blob.bibs);
223                         }
224                     });
225                 }
226             }
227         }
228     );
229 }
230
231 function displayRecordCount(axis, id, count) {
232     /* 1) put record count where we can see it */
233     dojo.query("#authority_" + id + " .authority-count-holder")[0].innerHTML =
234         "(" + count + ")"; /* XXX i18n ? */
235
236     /* 2) also, provide a link to show those records */
237     var span = dojo.query("#authority_" + id + " .authority-content")[0];
238
239     var args = {};
240     args.page = RRESULT;
241     args[PARAM_DEPTH] = depthSelGetDepth();
242     args[PARAM_FORM] = "all";
243     args[PARAM_LOCATION] = depthSelGetNewLoc();
244     args[PARAM_STYPE] = "keyword";
245     args[PARAM_RTYPE] = "keyword";
246     args[PARAM_TERM] = "identifier|authority_id[" + id + "]";
247
248     var axis_obj = acs_helper.browseAxisByCode(axis);
249     if (axis_obj.sorter())
250         args[PARAM_TERM] += " sort(" + axis_obj.sorter() + ")";
251
252     dojo.create(
253         "a", {
254             "innerHTML": span.innerHTML,
255             "href": buildOPACLink(args),
256             "className": "authority-content",
257             "title": "Show related bibliographic holdings" /* XXX i18n! */
258         },
259         span, "replace"
260     );
261 }
262