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