]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/cat/authority/list.js
LP#1403967: show 'subject heading thesaurus' value in Manage Authorities results
[working/Evergreen.git] / Open-ILS / web / js / ui / default / cat / authority / list.js
1 dojo.require('dijit.Dialog');
2 dojo.require('dijit.form.Button');
3 dojo.require('dijit.form.DropDownButton');
4 dojo.require('dijit.form.FilteringSelect');
5 dojo.require('dijit.form.Form');
6 dojo.require('dijit.form.NumberSpinner');
7 dojo.require('dijit.form.TextBox');
8 dojo.require("dijit.Menu");
9 dojo.require("dijit.MenuItem");
10 dojo.require('dojox.xml.parser');
11 dojo.require('DojoSRF');
12 dojo.require("fieldmapper.Fieldmapper");
13 dojo.require('openils.CGI');
14 dojo.require('openils.PermaCrud');
15 dojo.require('openils.XUL');
16 dojo.require('openils.widget.OrgUnitFilteringSelect');
17 dojo.require("openils.widget.PCrudAutocompleteBox");
18 dojo.require("MARC.FixedFields");
19 dojo.requireLocalization("openils.authority", "authority");
20 var auth_strings = dojo.i18n.getLocalization("openils.authority", "authority");
21
22 var cgi = new openils.CGI();
23 var pcrud = new openils.PermaCrud();
24
25 var _acs_cache_by_at = {};
26 function fetch_control_set(thesaurus) {
27     if (!_acs_cache_by_at[thesaurus]) {
28         var at = pcrud.retrieve(
29             "at", thesaurus,
30             {"flesh": 1, "flesh_fields": {"at": ["control_set"]}}
31         );
32         var cs;
33         if (at.control_set()) {
34             cs = at.control_set();
35         } else {
36             cs = new fieldmapper.acs();
37             cs.name("None");    // XXX i18n
38
39         }
40         _acs_cache_by_at[thesaurus] = cs;
41     }
42     return _acs_cache_by_at[thesaurus];
43 }
44
45 /*
46 // OrgUnits do not currently affect the retrieval of authority records,
47 // but this is how to display them if they become OrgUnit-aware
48 function authOUListInit() {
49     new openils.User().buildPermOrgSelector(
50         "STAFF_LOGIN", // anywhere you can log in
51         dijit.byId("authOU"),
52         null, // pre-selected org
53         null
54     );
55 }
56 dojo.addOnLoad(authOUListInit);
57 */
58 function displayAuthorities(data) { 
59
60     var idArr = [];
61     // Grab each record from the returned authority records
62     dojo.query("record", data).forEach(function(node) {
63         var auth = {};
64         auth.text = '';
65         auth.thesaurus = '|';
66         auth.id = 0;
67
68         // Grab each authority record field from the authority record
69         dojo.query("datafield[tag^='1']", node).forEach(function(dfNode) {
70             auth.text += dojox.xml.parser.textContent(dfNode); 
71             auth.name = dojo.attr(dfNode, 'tag');
72             auth.ind1 = dojo.attr(dfNode, 'ind1');
73             auth.ind2 = dojo.attr(dfNode, 'ind2');
74         });
75
76         
77         // Grab the ID of the authority record
78         dojo.query("datafield[tag='901']", node).query("subfield[code='c']").forEach(function(dfNode) {
79             auth.id = dojox.xml.parser.textContent(dfNode);
80         });
81
82         /* I wrap this in try/catch only because:
83          *  a) this interface hasn't hitherto relied on MARC.Record, and
84          *  b) the functionality we need it for is optional
85          */
86         try {
87             var marc = new MARC.Record({"rtype": "AUT", "xml": node});
88             auth.thesaurus = marc.extractFixedField("Subj", "|");
89         } catch (E) {
90             console.warn(
91                 "MARC.Record didn't work for authority record " +
92                 auth.id + ": " + E
93             );
94         }
95
96         idArr.push(parseInt(auth.id));
97
98         // Create the authority record listing entry. XXX i18n
99         dojo.place(
100             '<div class="authEntry" id="auth' + auth.id + '">' +
101             '<div class="text" id="authLabel' + auth.id + '">' +
102             '<span class="text">' + auth.text + '</span></div>' +
103             '<div class="authority-control-set-thesaurus"><span class="authority-control-set">Control Set: <span class="acs-name">' +
104             fetch_control_set(auth.thesaurus).name() +
105             '</span> <span class="acs-id">(#' +
106             fetch_control_set(auth.thesaurus).id() + ')</span>' +
107             '</span> <span class="authority-thesaurus">Thes: ' + auth.thesaurus + '</span></div></div>',
108             "authlist-div", "last"
109         );
110
111         // Add the menu of new/edit/delete/mark-for-merge options
112         var auth_menu = new dijit.Menu({});
113
114         // "Edit" menu item
115         new dijit.MenuItem({"id": "edit_" + auth.id, "onClick": function(){
116             var auth_rec = pcrud.retrieve("are", auth.id);
117             if (auth_rec) {
118                 loadMarcEditor(pcrud, auth_rec);
119             }
120         }, "label":auth_strings.MENU_EDIT}).placeAt(auth_menu, "first");
121
122         // "Merge" menu item
123         new dijit.MenuItem({"id": "merge_" + auth.id, "onClick":function(){
124             auth.text = '';
125             dojo.query('#auth' + auth.id).query('span.text').forEach(function(node) {
126                 auth.text += dojox.xml.parser.textContent(node); 
127             });
128
129             // If there is a toMerge item already, this is a target record
130             var mergeRole = '<td style="border: 1px solid black; padding-left: 0.5em; padding-right: 1em;">';
131             var isTarget = dojo.query('.toMerge').length;
132             if (isTarget) {
133                 mergeRole += auth_strings.TARGET_RECORD + '</td>';
134             } else {
135                 mergeRole += auth_strings.MASTER_RECORD + '</td>';
136             }
137
138             dojo.place('<tr class="toMerge" id="toMerge_' + auth.id + '"><td>' + mergeRole + '</td><td  style="border: 1px solid black;" id="mergeMeta_' + auth.id + '"></td><td style="border: 1px solid black; padding-left: 1em; padding-right: 1em;" >' + auth.text + '</td></tr>', 'mergebox-tbody', 'last');
139             dojo.place('<span class="authmeta" style="font-family: monospace;">' + auth.name + ' ' + auth.ind1 + auth.ind2 + '</span>', 'mergeMeta_' + auth.id, 'last');
140             dojo.removeClass('mergebox-div', 'hidden');
141         }, "label":auth_strings.MENU_MERGE}).placeAt(auth_menu, "last");
142
143         // "Delete" menu item
144         new dijit.MenuItem({
145             "id": "delete_" + auth.id,
146             "onClick":function(){
147                 auth.text = '';
148
149                 var auth_rec = pcrud.retrieve("are", auth.id);
150
151                 // Bit of a hack to get the linked bib count until an explicit ID
152                 var linkedBibs = dojox.xml.parser.textContent(
153                     dojo.query("#authLabel" + auth.id)[0].previousSibling
154                 );
155
156                 var delDlg = dijit.byId("delDialog_" + auth.id);
157
158                 dojo.query('#auth' + auth.id).query('span.text').forEach(function(node) {
159                     auth.text += dojo.trim(dojox.xml.parser.textContent(node)); 
160                 });
161
162                 if (!delDlg) {
163                     var content = '<div>' + dojo.string.substitute(auth_strings.CONFIRM_DELETE_TITLE, [auth.text]) + '</div>';
164                     if (parseInt(linkedBibs) > 0) {
165                         content = "<div id='delAuthSum_" + auth.id + "'>"
166                             + dojo.string.substitute(auth_strings.LINKED_BIBS, [linkedBibs])
167                             + "</div>";
168                     }
169                     content += "<div id='authMARC" + auth.id + "' style='width: 100%; display:none;'>";
170                     content += "<hr style='width: 100%;' />";
171                     content += marcToHTML(auth_rec.marc());
172                     content += "</div><hr style='width: 100%;' /><div>";
173                     content += "<input type='button' dojoType='dijit.form.Button' label='" + auth_strings.CANCEL + "' onClick='cancelDelete(" + auth.id + ")'/>";
174                     content += "<input type='button' dojoType='dijit.form.Button' label='" + auth_strings.DELETE + "' onClick='confirmDelete(" + auth.id + ")'/>";
175                     content += "<input id='viewMARC" + auth.id + "' type='button' "
176                         + "style='float:right;' dojoType='dijit.form.Button' "
177                         + "label='" + auth_strings.VIEW_MARC + "' onClick='viewMARC(" + auth.id + ")'/>";
178                     content += "<input id='hideMARC" + auth.id + "' type='button' "
179                         + "style='display: none; float:right;' dojoType='dijit.form.Button' "
180                         + "label='" + auth_strings.HIDE_MARC + "' onClick='hideMARC(" + auth.id + ")'/>";
181                     content += "</div>";
182                     delDlg = new dijit.Dialog({
183                         "id":"delDialog_" + auth.id,
184                         "title": dojo.string.substitute(auth_strings.CONFIRM_DELETE_PROMPT, [auth.id]),
185                         "content": content
186                     });
187                 }
188                 delDlg.show();
189
190         }, "label":auth_strings.DELETE}).placeAt(auth_menu, "last");
191
192         auth_mb = new dijit.form.DropDownButton({dropDown: auth_menu, label: auth_strings.ACTIONS, id:"menu" + auth.id});
193         auth_mb.placeAt(dojo.create("div", null, "auth" + auth.id, "first"), "first");
194         auth_menu.startup();
195     });
196
197     showBibCount(idArr);
198 }
199
200 function viewMARC(recId) {
201     dojo.style(dojo.byId("authMARC" + recId), 'display', 'block');
202     dojo.style(dijit.byId("viewMARC" + recId).domNode, 'display', 'none');
203     dojo.style(dijit.byId("hideMARC" + recId).domNode, 'display', 'block');
204 }
205
206 function hideMARC(recId) {
207     dojo.style(dojo.byId("authMARC" + recId), 'display', 'none');
208     dojo.style(dijit.byId("hideMARC" + recId).domNode, 'display', 'none');
209     dojo.style(dijit.byId("viewMARC" + recId).domNode, 'display', 'block');
210 }
211
212 function marcToHTML(marc) {
213     var html = '<table><tbody>';
214     marc = dojox.xml.parser.parse(marc);
215     dojo.query('leader', marc).forEach(function(node) {
216         html += '<tr><td>LDR</td><td>&nbsp;</td><td>&nbsp;</td><td>' + dojox.xml.parser.textContent(node) + '</td></tr>';
217     });
218     dojo.query('controlfield', marc).forEach(function(node) {
219         html += '<tr><td>' + dojo.attr(node, "tag") + '</td><td>&nbsp;</td><td>&nbsp;</td><td>' + dojox.xml.parser.textContent(node) + '</td></tr>';
220     });
221     dojo.query('datafield', marc).forEach(function(node) {
222         var cnt = 0;
223         html += '<tr><td>' + dojo.attr(node, "tag") + '</td><td>' + dojo.attr(node, "ind1") + '</td><td>' + dojo.attr(node, "ind2") + '</td>';
224         dojo.query('subfield', node).forEach(function(sf) {
225             if (cnt == 0) {
226                 html += '<td>$' + dojo.attr(sf, "code") + ' ' + dojox.xml.parser.textContent(sf) + '</td></tr>';
227                 cnt = 1;
228             } else {
229                 html += '<tr><td colspan="3"></td><td>$' + dojo.attr(sf, "code") + ' ' + dojox.xml.parser.textContent(sf) + '</td></tr>';
230             }
231         });
232     });
233     html += '</tbody></table>';
234     return html;
235 }
236
237 function cancelDelete(recId) {
238     dijit.byId("delDialog_" + recId).hide();
239 }
240
241 function confirmDelete(recId) {
242     var auth_rec = pcrud.retrieve("are", recId);
243     if (auth_rec) {
244         pcrud.eliminate(auth_rec);
245         dijit.byId("delDialog_" + recId).attr("content", dojo.string.substitute(auth_strings.CONFIRM_DELETE_RESULT, [recId]));
246         setTimeout(function() {
247             dijit.byId("delDialog_" + recId).hide();
248         }, 3000);
249     }
250 }
251
252 function showBibCount(authIds) {
253     /* Decorate the list with # of bibs linked to each authority record */
254     var ses = new OpenSRF.ClientSession('open-ils.cat');
255     var req = ses.request('open-ils.cat.authority.records.count_linked_bibs', authIds);
256     var linkedIds = [];
257     req.oncomplete = function(r) {
258         var msg = r.recv().content();
259         dojo.forEach(msg, function(auth) {
260                 linkedIds.push(auth.authority);
261                 dojo.place('<span class="bibcount">' + auth.bibs + '</span> ', 'authLabel' + auth.authority, 'first');
262             }
263         );
264
265         /* Assign counts of 0 for every non-linked authority */
266         dojo.forEach(authIds, function (id) {
267             var found = false;
268             dojo.forEach(linkedIds, function (lid) {
269                 if (id == lid) {
270                     found = true;
271                 }
272             });
273             if (!found) {
274                 dojo.place('<span class="bibcount">0</span> ', 'authLabel' + id, 'first');
275             }
276         });
277     }
278     req.send();
279 }
280
281 function loadMarcEditor(pcrud, rec) {
282
283     /* Prevent the spawned MARC editor from making its title bar inaccessible */
284     var initHeight = self.outerHeight - 40;
285     /* Setting an explicit height results in a super skinny window, so fix that up */
286     var initWidth = self.outerWidth / 2;
287
288     /*
289        To run in Firefox directly, must set signed.applets.codebase_principal_support
290        to true in about:config
291      */
292     win = window.open('/xul/server/cat/marcedit.xul','',    // XXX version?
293         'chrome,resizable=yes,height=' + initHeight + ',width=' + initWidth);
294
295     win.xulG = {
296         "record": {"marc": rec.marc(), "rtype": "are"},
297         "save": {
298             "label": auth_strings.SAVE,
299             "func": function(xmlString) {
300                 rec.marc(xmlString);
301                 rec.edit_date('now');
302                 rec.ischanged(true);
303                 pcrud.update(rec);
304                 alert(auth_strings.SAVE_RESULT_SUCCESS);
305                 win.close();
306             }
307         },
308         'lock_tab' : typeof xulG != 'undefined' ? (typeof xulG['lock_tab'] != 'undefined' ? xulG.lock_tab : undefined) : undefined,
309         'unlock_tab' : typeof xulG != 'undefined' ? (typeof xulG['unlock_tab'] != 'undefined' ? xulG.unlock_tab : undefined) : undefined
310     };
311 }
312
313 function authListInit() {
314     var term = cgi.param('authTerm') || '';
315     var page = cgi.param('authPage') || 0;
316     var axis = cgi.param('authAxis') || 'authority.author';
317     if (axis) {
318         dijit.byId('authAxis').attr('value', axis);
319     }
320     if (page) {
321         dijit.byId('authPage').attr('value', page);
322     }
323     if (term) {
324         dijit.byId('authTerm').attr('value', term);
325         displayRecords();
326     }
327
328     dojo.connect(dijit.byId('authAxis'), 'onKeyPress', function(evt) {
329         if (evt.keyCode == dojo.keys.ENTER) {
330             dijit.byId('authPage').attr('value', 0);
331             displayRecords();
332         }
333     }); 
334
335     dojo.connect(dijit.byId('authPage'), 'onKeyPress', function(evt) {
336         if (evt.keyCode == dojo.keys.ENTER) {
337             dijit.byId('authPage').attr('value', 0);
338             displayRecords();
339         }
340     });
341
342     dojo.connect(dijit.byId('authTerm'), 'onKeyPress', function(evt) {
343         if (evt.keyCode == dojo.keys.ENTER) {
344             dijit.byId('authPage').attr('value', 0);
345             displayRecords();
346         }
347     });
348
349     dijit.byId('authTerm').focus();
350
351 }
352 dojo.addOnLoad(authListInit);
353
354 function displayRecords(parms) {
355
356     if (parms && parms.page) {
357         if (parms.page == 'next') {
358             page = dijit.byId('authPage').attr('value');
359             dijit.byId('authPage').attr('value', page + 1);
360         } else if (parms.page == 'prev') {
361             page = dijit.byId('authPage').attr('value');
362             dijit.byId('authPage').attr('value', page - 1);
363         } else {
364             dijit.byId('authPage').attr('value', parms.page);
365         }
366     }
367
368     /* Protect against null input */
369     if (!dijit.byId('authTerm').attr('value')) {
370         return;
371     }
372
373     /* Clear out the current contents of the page */
374     var widgets = dijit.findWidgets(dojo.byId('authlist-div'));
375     dojo.forEach(widgets, function(w) { w.destroyRecursive(true); });
376
377     dojo.query("#authlist-div").query("div").orphan();
378
379     var url = '/opac/extras/browse/marcxml/authority.'
380         + dijit.byId('authAxis').attr('value')
381         // + '/' + dijit.byId('authOU').attr('value')
382         + '/1' // replace with preceding line if OUs gain some meaning
383         + '/' + encodeURIComponent(dijit.byId('authTerm').attr('value'))
384         + '/' + dijit.byId('authPage').attr('value')
385         + '/' + '20' // 20 results per page
386     ;
387     dojo.xhrGet({"url":url, "handleAs":"xml", "content":{"format":"marcxml"}, "preventCache": true, "load":displayAuthorities });
388 }
389
390 function clearMergeRecords() {
391     var records = dojo.query('.toMerge').orphan();
392     dojo.addClass('mergebox-div', 'hidden');
393 }
394
395 function mergeRecords() {
396     var records = dojo.query('.toMerge').attr('id');
397     dojo.forEach(records, function(item, idx) {
398         records[idx] = parseInt(item.slice(item.lastIndexOf('_') + 1));
399     });
400
401     /* Take the first record in the list and use that as the master */
402     fieldmapper.standardRequest(
403         ['open-ils.cat', 'open-ils.cat.authority.records.merge'],
404         {   async: false,
405             params: [openils.User.authtoken, records.shift(), records],
406             oncomplete : function(r) {
407                 alert(auth_strings.MERGE_RESULT_SUCCESS);
408                 clearMergeRecords();
409                 displayRecords();
410             }
411         }
412     );
413 }