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