]> git.evergreen-ils.org Git - 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
[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     dojo.forEach(
160         main_entries, function(af) { renderAuthorityMainEntry(m, af, tbody); }
161     );
162
163     return table;
164 }
165
166 /* displayAuthorityRecords: given a DOM document object that contains marcxml
167  * records, display each one in a table using the apporiate control set to
168  * determine which fields to show.
169  */
170 function displayAuthorityRecords(axis, doc) {
171     if (!acs_helper)
172         acs_helper = new openils.AuthorityControlSet();
173
174     /* XXX I wanted to use bibtemplate here, but now I'm not sure it makes
175      * sense: the template itself would have to be dynamic, as it would vary
176      * from record to record when different control sets were in use.
177      */
178     var auth_ids = [];
179
180     dojo.empty("authority-record-holder");
181
182     var records = dojo.query("record", doc);
183     last_fetched_length = records.length;
184
185     dojo.forEach(
186         records,
187         function(record) {
188             var m = new MARC.Record({"xml": record});
189
190             /* is 001 reliable for this? I'm guessing not */
191             var auth_id = m.field("001").data;
192             auth_ids.push(auth_id);
193
194             var cs = acs_helper.controlSetByThesaurusCode(
195                 m.extractFixedField("Subj")
196             );
197
198             dojo.place(
199                 renderAuthorityRecord(m, cs.raw, auth_id),
200                 "authority-record-holder"
201             );
202         }
203     );
204     displayRecordCounts(axis, auth_ids);
205     swapCanvas(dojo.byId("canvas_main"));
206 }
207
208 function displayRecordCounts(axis, auth_ids) {
209     fieldmapper.standardRequest(
210         ["open-ils.cat", "open-ils.cat.authority.records.count_linked_bibs"], {
211             "params": [auth_ids],
212             "async": true,
213             "oncomplete": function(r) {
214                 if ((r = openils.Util.readResponse(r))) {
215                     dojo.forEach(r, function(blob) {
216                         if (blob.bibs > 0) {
217                             displayRecordCount(axis, blob.authority, blob.bibs);
218                         }
219                     });
220                 }
221             }
222         }
223     );
224 }
225
226 function displayRecordCount(axis, id, count) {
227     /* 1) put record count where we can see it */
228     dojo.query("#authority_" + id + " .authority-count-holder")[0].innerHTML =
229         "(" + count + ")"; /* XXX i18n ? */
230
231     /* 2) also, provide a link to show those records */
232     var span = dojo.query("#authority_" + id + " .authority-content")[0];
233
234     var args = {};
235     args.page = RRESULT;
236     args[PARAM_DEPTH] = depthSelGetDepth();
237     args[PARAM_FORM] = "all";
238     args[PARAM_LOCATION] = depthSelGetNewLoc();
239     args[PARAM_STYPE] = "keyword";
240     args[PARAM_RTYPE] = "keyword";
241     args[PARAM_TERM] = "identifier|authority_id[" + id + "]";
242
243     var axis_obj = acs_helper.browseAxisByCode(axis);
244     if (axis_obj.sorter())
245         args[PARAM_TERM] += " sort(" + axis_obj.sorter() + ")";
246
247     dojo.create(
248         "a", {
249             "innerHTML": span.innerHTML,
250             "href": buildOPACLink(args),
251             "className": "authority-content",
252             "title": "Show related bibliographic holdings" /* XXX i18n! */
253         },
254         span, "replace"
255     );
256 }
257