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