]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/cat/authority/list.js
Show the number of bibs linked to each authority record
[working/Evergreen.git] / Open-ILS / web / js / ui / default / cat / authority / list.js
1 dojo.require('dijit.form.Button');
2 dojo.require('dijit.form.DropDownButton');
3 dojo.require('dijit.form.FilteringSelect');
4 dojo.require('dijit.form.Form');
5 dojo.require('dijit.form.NumberSpinner');
6 dojo.require('dijit.form.TextBox');
7 dojo.require("dijit.Menu");
8 dojo.require("dijit.MenuItem");
9 dojo.require('dojox.xml.parser');
10 dojo.require('DojoSRF');
11 dojo.require("fieldmapper.Fieldmapper");
12 dojo.require('openils.CGI');
13 dojo.require('openils.PermaCrud');
14 dojo.require('openils.XUL');
15 dojo.require('openils.widget.OrgUnitFilteringSelect');
16
17 var cgi = new openils.CGI();
18
19 /*
20 // OrgUnits do not currently affect the retrieval of authority records,
21 // but this is how to display them if they become OrgUnit-aware
22 function authOUListInit() {
23     new openils.User().buildPermOrgSelector(
24         "STAFF_LOGIN", // anywhere you can log in
25         dijit.byId("authOU"),
26         null, // pre-selected org
27         null
28     );
29 }
30 dojo.addOnLoad(authOUListInit);
31 */
32 function displayAuthorities(data) { 
33
34     var idArr = [];
35     // Grab each record from the returned authority records
36     dojo.query("record", data).forEach(function(node) {
37         authText = '';
38         authId = 0;
39
40         // Grab each authority record field from the authority record
41         dojo.query("datafield[tag^='1']", node).forEach(function(dfNode) {
42             authText += dojox.xml.parser.textContent(dfNode); 
43         });
44         // Grab the ID of the authority record
45         dojo.query("datafield[tag='901'] subfield[code='c']", node).forEach(function(dfNode) {
46             authId = dojox.xml.parser.textContent(dfNode); 
47         });
48
49         idArr.push(parseInt(authId));
50
51         // Create the authority record listing entry
52         dojo.place('<div class="authEntry" id="auth' + authId + '"><span class="text" id="authLabel' + authId + '">' + authText + '</span></div>', "authlist-div", "last");
53
54         // Add the menu of new/edit/delete/mark-for-merge options
55         var auth_menu = new dijit.Menu({});
56
57         // "Edit" menu item
58         new dijit.MenuItem({"id": "edit_" + authId, "onClick": function(){
59             recId = this.id.slice(this.id.lastIndexOf('_') + 1);
60             pcrud = new openils.PermaCrud();
61             auth_rec = pcrud.retrieve("are", recId);
62             if (auth_rec) {
63                 loadMarcEditor(pcrud, auth_rec);
64             }
65         }, "label":"Edit"}).placeAt(auth_menu, "first");
66
67         // "Merge" menu item
68         new dijit.MenuItem({"id": "merge_" + authId, "onClick":function(){
69             authText = '';
70             recId = this.id.slice(this.id.lastIndexOf('_') + 1);
71             dojo.query('#auth' + recId + ' span.text').forEach(function(node) {
72                 authText += dojox.xml.parser.textContent(node); 
73             });
74             dojo.place('<div class="toMerge" id="toMerge_' + recId + '">' +  authText + '</div>', 'mergebox-div', 'last');
75             dojo.removeClass('mergebox-div', 'hidden');
76         }, "label":"Mark for Merge"}).placeAt(auth_menu, "last");
77
78         // "Delete" menu item
79         new dijit.MenuItem({"id": "delete_" + authId, "onClick":function(){
80             recId = this.id.slice(this.id.lastIndexOf('_') + 1);
81
82             // Deleting an authority record is unusual; let's be 100% sure
83             if (!confirm("Are you sure you want to delete record " + recId + "?")) {
84                 return;
85             }
86
87             pcrud = new openils.PermaCrud();
88             auth_rec = pcrud.retrieve("are", recId);
89             if (auth_rec) {
90                 pcrud.eliminate(auth_rec);
91                 alert("Deleted authority record # " + recId);
92             }
93         }, "label":"Delete"}).placeAt(auth_menu, "last");
94
95         auth_mb = new dijit.form.DropDownButton({dropDown: auth_menu, label:"Actions", id:"menu" + authId});
96         auth_mb.placeAt("auth" + authId, "first");
97         auth_menu.startup();
98     });
99
100     showBibCount(idArr);
101
102 }
103
104 function showBibCount(authIds) {
105     /* Decorate the list with # of bibs linked to each authority record */
106     var ses = new OpenSRF.ClientSession('open-ils.cat');
107     var req = ses.request('open-ils.cat.authority.records.count_linked_bibs', authIds);
108     var linkedIds = [];
109     req.oncomplete = function(r) {
110         var msg = r.recv().content();
111         dojo.forEach(msg, function(auth) {
112                 linkedIds.push(auth.authority);
113                 dojo.place('<span class="bibcount">' + auth.bibs + '</span>', 'authLabel' + auth.authority, 'before');
114             }
115         );
116
117         /* Assign counts of 0 for every non-linked authority */
118         dojo.forEach(authIds, function (id) {
119             var found = false;
120             dojo.forEach(linkedIds, function (lid) {
121                 if (id == lid) {
122                     found = true;
123                 }
124             });
125             if (!found) {
126                 dojo.place('<span class="bibcount">0</span>', 'authLabel' + id, 'before');
127             }
128         });
129     }
130     req.send();
131 }
132
133 function loadMarcEditor(pcrud, rec) {
134     /*
135        To run in Firefox directly, must set signed.applets.codebase_principal_support
136        to true in about:config
137      */
138     netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
139     win = window.open('/xul/server/cat/marcedit.xul'); // XXX version?
140
141     win.xulG = {
142         "record": {"marc": rec.marc()},
143         "save": {
144             "label": "Save",
145             "func": function(xmlString) {
146                 rec.marc(xmlString);
147                 rec.ischanged(true);
148                 pcrud.update(rec);
149                 alert("Record was saved");
150                 win.close();
151             }
152         }
153     };
154 }
155
156 function authListInit() {
157     term = cgi.param('authTerm') || '';
158     page = cgi.param('authPage') || 0;
159     axis = cgi.param('authAxis') || 'authority.author';
160     if (axis) {
161         dijit.byId('authAxis').attr('value', axis);
162     }
163     if (page) {
164         dijit.byId('authPage').attr('value', page);
165     }
166     if (term) {
167         dijit.byId('authTerm').attr('value', term);
168         displayRecords();
169     }
170     dojo.connect(dijit.byId('authTerm'), 'onBlur', function() {
171         dijit.byId('authPage').attr('value', 0);
172         displayRecords();
173     });
174     dojo.connect(dijit.byId('authTerm'), 'onKeyPress', function(evt) {
175         if (evt.keyCode == dojo.keys.ENTER) {
176             dijit.byId('authPage').attr('value', 0);
177             displayRecords();
178         }
179     });
180
181 }
182 dojo.addOnLoad(authListInit);
183
184 function displayRecords(parms) {
185
186     if (parms && parms.page) {
187         if (parms.page == 'next') {
188             page = dijit.byId('authPage').attr('value');
189             dijit.byId('authPage').attr('value', page + 1);
190         } else if (parms.page == 'prev') {
191             page = dijit.byId('authPage').attr('value');
192             dijit.byId('authPage').attr('value', page - 1);
193         } else {
194             dijit.byId('authPage').attr('value', parms.page);
195         }
196     }
197
198     /* Protect against null input */
199     if (!dijit.byId('authTerm').attr('value')) {
200         return;
201     }
202
203     /* Clear out the current contents of the page */
204     widgets = dijit.findWidgets(dojo.byId('authlist-div'));
205     dojo.forEach(widgets, function(w) { w.destroyRecursive(true); });
206
207     dojo.query("#authlist-div div").orphan();
208
209     url = '/opac/extras/startwith/marcxml/'
210         + dijit.byId('authAxis').attr('value')
211         // + '/' + dijit.byId('authOU').attr('value')
212         + '/1' // replace with preceding line if OUs gain some meaning
213         + '/' + dijit.byId('authTerm').attr('value')
214         + '/' + dijit.byId('authPage').attr('value')
215     ;
216     dojo.xhrGet({"url":url, "handleAs":"xml", "content":{"format":"marcxml"}, "preventCache": true, "load":displayAuthorities });
217 }
218
219 function clearMergeRecords() {
220     records = dojo.query('.toMerge').orphan();
221     dojo.addClass('mergebox-div', 'hidden');
222 }
223
224 function mergeRecords() {
225     records = dojo.query('.toMerge').attr('id');
226     dojo.forEach(records, function(item, idx) {
227         records[idx] = parseInt(item.slice(item.lastIndexOf('_') + 1));
228     });
229
230     /* Take the first record in the list and use that as the master */
231     fieldmapper.standardRequest(
232         ['open-ils.cat', 'open-ils.cat.authority.records.merge'],
233         {   async: false,
234             params: [openils.User.authtoken, records.shift(), records],
235             oncomplete : function(r) {
236                 alert("Record merge is complete.");
237                 clearMergeRecords();
238                 displayRecords();
239             }
240         }
241     );
242 }