]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/ui/default/cat/authority/list.js
b719ecc0808ece6926b8ddfb213b27041a907231
[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
75             // If there is a toMerge item already, this is a target record
76             var mergeRole = '<td style="border: 1px solid black; padding-left: 0.5em; padding-right: 1em;">';
77             var isTarget = dojo.query('.toMerge').length;
78             if (isTarget) {
79                 mergeRole += 'Target</td>';
80             } else {
81                 mergeRole += 'Master</td>';
82             }
83             dojo.place('<tr class="toMerge" id="toMerge_' + recId + '">' + mergeRole + '<td style="border: 1px solid black; padding-left: 1em;">' + authText + '</td></tr>', 'mergebox-tbody', 'last');
84             dojo.removeClass('mergebox-div', 'hidden');
85         }, "label":"Mark for Merge"}).placeAt(auth_menu, "last");
86
87         // "Delete" menu item
88         new dijit.MenuItem({"id": "delete_" + authId, "onClick":function(){
89             recId = this.id.slice(this.id.lastIndexOf('_') + 1);
90
91             // Deleting an authority record is unusual; let's be 100% sure
92             if (!confirm("Are you sure you want to delete record " + recId + "?")) {
93                 return;
94             }
95
96             pcrud = new openils.PermaCrud();
97             auth_rec = pcrud.retrieve("are", recId);
98             if (auth_rec) {
99                 pcrud.eliminate(auth_rec);
100                 alert("Deleted authority record # " + recId);
101             }
102         }, "label":"Delete"}).placeAt(auth_menu, "last");
103
104         auth_mb = new dijit.form.DropDownButton({dropDown: auth_menu, label:"Actions", id:"menu" + authId});
105         auth_mb.placeAt("auth" + authId, "first");
106         auth_menu.startup();
107     });
108
109     showBibCount(idArr);
110
111 }
112
113 function showBibCount(authIds) {
114     /* Decorate the list with # of bibs linked to each authority record */
115     var ses = new OpenSRF.ClientSession('open-ils.cat');
116     var req = ses.request('open-ils.cat.authority.records.count_linked_bibs', authIds);
117     var linkedIds = [];
118     req.oncomplete = function(r) {
119         var msg = r.recv().content();
120         dojo.forEach(msg, function(auth) {
121                 linkedIds.push(auth.authority);
122                 dojo.place('<span class="bibcount">' + auth.bibs + '</span>', 'authLabel' + auth.authority, 'before');
123             }
124         );
125
126         /* Assign counts of 0 for every non-linked authority */
127         dojo.forEach(authIds, function (id) {
128             var found = false;
129             dojo.forEach(linkedIds, function (lid) {
130                 if (id == lid) {
131                     found = true;
132                 }
133             });
134             if (!found) {
135                 dojo.place('<span class="bibcount">0</span>', 'authLabel' + id, 'before');
136             }
137         });
138     }
139     req.send();
140 }
141
142 function loadMarcEditor(pcrud, rec) {
143     /*
144        To run in Firefox directly, must set signed.applets.codebase_principal_support
145        to true in about:config
146      */
147     netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
148     win = window.open('/xul/server/cat/marcedit.xul'); // XXX version?
149
150     win.xulG = {
151         "record": {"marc": rec.marc(), "rtype": "are"},
152         "save": {
153             "label": "Save",
154             "func": function(xmlString) {
155                 rec.marc(xmlString);
156                 rec.ischanged(true);
157                 pcrud.update(rec);
158                 alert("Record was saved");
159                 win.close();
160             }
161         }
162     };
163 }
164
165 function authListInit() {
166     term = cgi.param('authTerm') || '';
167     page = cgi.param('authPage') || 0;
168     axis = cgi.param('authAxis') || 'authority.author';
169     if (axis) {
170         dijit.byId('authAxis').attr('value', axis);
171     }
172     if (page) {
173         dijit.byId('authPage').attr('value', page);
174     }
175     if (term) {
176         dijit.byId('authTerm').attr('value', term);
177         displayRecords();
178     }
179     dojo.connect(dijit.byId('authTerm'), 'onBlur', function() {
180         dijit.byId('authPage').attr('value', 0);
181         displayRecords();
182     });
183     dojo.connect(dijit.byId('authTerm'), 'onKeyPress', function(evt) {
184         if (evt.keyCode == dojo.keys.ENTER) {
185             dijit.byId('authPage').attr('value', 0);
186             displayRecords();
187         }
188     });
189
190 }
191 dojo.addOnLoad(authListInit);
192
193 function displayRecords(parms) {
194
195     if (parms && parms.page) {
196         if (parms.page == 'next') {
197             page = dijit.byId('authPage').attr('value');
198             dijit.byId('authPage').attr('value', page + 1);
199         } else if (parms.page == 'prev') {
200             page = dijit.byId('authPage').attr('value');
201             dijit.byId('authPage').attr('value', page - 1);
202         } else {
203             dijit.byId('authPage').attr('value', parms.page);
204         }
205     }
206
207     /* Protect against null input */
208     if (!dijit.byId('authTerm').attr('value')) {
209         return;
210     }
211
212     /* Clear out the current contents of the page */
213     widgets = dijit.findWidgets(dojo.byId('authlist-div'));
214     dojo.forEach(widgets, function(w) { w.destroyRecursive(true); });
215
216     dojo.query("#authlist-div div").orphan();
217
218     url = '/opac/extras/startwith/marcxml/'
219         + dijit.byId('authAxis').attr('value')
220         // + '/' + dijit.byId('authOU').attr('value')
221         + '/1' // replace with preceding line if OUs gain some meaning
222         + '/' + dijit.byId('authTerm').attr('value')
223         + '/' + dijit.byId('authPage').attr('value')
224     ;
225     dojo.xhrGet({"url":url, "handleAs":"xml", "content":{"format":"marcxml"}, "preventCache": true, "load":displayAuthorities });
226 }
227
228 function clearMergeRecords() {
229     records = dojo.query('.toMerge').orphan();
230     dojo.addClass('mergebox-div', 'hidden');
231 }
232
233 function mergeRecords() {
234     records = dojo.query('.toMerge').attr('id');
235     dojo.forEach(records, function(item, idx) {
236         records[idx] = parseInt(item.slice(item.lastIndexOf('_') + 1));
237     });
238
239     /* Take the first record in the list and use that as the master */
240     fieldmapper.standardRequest(
241         ['open-ils.cat', 'open-ils.cat.authority.records.merge'],
242         {   async: false,
243             params: [openils.User.authtoken, records.shift(), records],
244             oncomplete : function(r) {
245                 alert("Record merge is complete.");
246                 clearMergeRecords();
247                 displayRecords();
248             }
249         }
250     );
251 }