]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/cat/authority/list.js
Give the Authority List interface the ability to merge records... almost
[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('openils.CGI');
11 dojo.require('dojo.dnd.Source');
12 dojo.require('openils.PermaCrud');
13 dojo.require('openils.XUL');
14 dojo.require('openils.widget.OrgUnitFilteringSelect');
15
16 var cgi = new openils.CGI();
17
18 /*
19 // OrgUnits do not currently affect the retrieval of authority records,
20 // but this is how to display them if they become OrgUnit-aware
21 function authOUListInit() {
22     new openils.User().buildPermOrgSelector(
23         "STAFF_LOGIN", // anywhere you can log in
24         dijit.byId("authOU"),
25         null, // pre-selected org
26         null
27     );
28 }
29 dojo.addOnLoad(authOUListInit);
30 */
31 function displayAuthorities(data) { 
32     // Grab each record from the returned authority records
33     dojo.query("record", data).forEach(function(node) {
34         authText = '';
35         authId = 0;
36
37         // Grab each authority record field from the authority record
38         dojo.query("datafield[tag^='1']", node).forEach(function(dfNode) {
39             authText += dojox.xml.parser.textContent(dfNode); 
40         });
41         // Grab the ID of the authority record
42         dojo.query("datafield[tag='901'] subfield[code='c']", node).forEach(function(dfNode) {
43             authId = dojox.xml.parser.textContent(dfNode); 
44         });
45
46         // Create the authority record listing entry
47         dojo.place('<div class="authEntry" id="auth' + authId + '"><span class="text">' + authText + '</span></div>', "authlist-div", "last");
48
49         // Add the menu of new/edit/delete/mark-for-merge options
50         var auth_menu = new dijit.Menu({});
51
52         // "Edit" menu item
53         new dijit.MenuItem({"id": "edit_" + authId, "onClick": function(){
54             recId = this.id.slice(this.id.lastIndexOf('_') + 1);
55             pcrud = new openils.PermaCrud();
56             auth_rec = pcrud.retrieve("are", recId);
57             if (auth_rec) {
58                 loadMarcEditor(pcrud, auth_rec);
59             }
60         }, "label":"Edit"}).placeAt(auth_menu, "first");
61
62         // "Merge" menu item
63         new dijit.MenuItem({"id": "merge_" + authId, "onClick":function(){
64             authText = '';
65             recId = this.id.slice(this.id.lastIndexOf('_') + 1);
66             dojo.query('#auth' + recId + ' span.text').forEach(function(node) {
67                 authText += dojox.xml.parser.textContent(node); 
68             });
69             dojo.place('<div class="toMerge" id="toMerge_' + recId + '">' +  authText + '</div>', 'mergebox-div', 'last');
70             dojo.removeClass('mergebox-div', 'hidden');
71         }, "label":"Mark for Merge"}).placeAt(auth_menu, "last");
72
73         // "Delete" menu item
74         new dijit.MenuItem({"id": "delete_" + authId, "onClick":function(){
75             recId = this.id.slice(this.id.lastIndexOf('_') + 1);
76             pcrud = new openils.PermaCrud();
77             auth_rec = pcrud.retrieve("are", recId);
78             if (auth_rec) {
79                 pcrud.eliminate(auth_rec);
80                 alert("Deleted authority record # " + recId);
81             }
82         }, "label":"Delete"}).placeAt(auth_menu, "last");
83
84         auth_mb = new dijit.form.DropDownButton({dropDown: auth_menu, label:"Actions"});
85         auth_mb.placeAt("auth" + authId, "first");
86         auth_menu.startup();
87     });
88 }
89
90 function loadMarcEditor(pcrud, rec) {
91     /*
92        To run in Firefox directly, must set signed.applets.codebase_principal_support
93        to true in about:config
94      */
95     netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
96     win = window.open('/xul/server/cat/marcedit.xul'); // XXX version?
97
98     win.xulG = {
99         "record": {"marc": rec.marc()},
100         "save": {
101             "label": "Save",
102             "func": function(xmlString) {
103                 rec.marc(xmlString);
104                 rec.ischanged(true);
105                 pcrud.update(rec);
106                 alert("Record was saved");
107                 win.close();
108             }
109         }
110     };
111 }
112
113 function authListInit() {
114     term = cgi.param('authTerm') || '';
115     page = cgi.param('authPage') || 0;
116     axis = cgi.param('authAxis') || 'authority.author';
117     if (axis) {
118         dijit.byId('authAxis').attr('value', axis);
119     }
120     if (page) {
121         dijit.byId('authPage').attr('value', page);
122     }
123     if (term) {
124         dijit.byId('authTerm').attr('value', term);
125         displayRecords();
126     }
127     dojo.connect(dijit.byId('authTerm'), 'onBlur', 'displayRecords');
128 }
129 dojo.addOnLoad(authListInit);
130
131 function displayRecords(parms) {
132
133     if (parms && parms.page) {
134         if (parms.page == 'next') {
135             page = dijit.byId('authPage').attr('value');
136             dijit.byId('authPage').attr('value', page + 1);
137         } else if (parms.page == 'prev') {
138             page = dijit.byId('authPage').attr('value');
139             dijit.byId('authPage').attr('value', page - 1);
140         } else {
141             dijit.byId('authPage').attr('value', parms.page);
142         }
143     }
144
145     /* Protect against null input */
146     if (!dijit.byId('authTerm').attr('value')) {
147         return;
148     }
149
150     /* Clear out the current contents of the page */
151     widgets = dijit.findWidgets(dojo.byId('authlist-div'));
152     dojo.forEach(widgets, function(w) { w.destroyRecursive(true); });
153
154     dojo.query("#authlist-div div").orphan();
155
156     url = '/opac/extras/startwith/marcxml/'
157         + dijit.byId('authAxis').attr('value')
158         // + '/' + dijit.byId('authOU').attr('value')
159         + '/1' // replace with preceding line if OUs gain some meaning
160         + '/' + dijit.byId('authTerm').attr('value')
161         + '/' + dijit.byId('authPage').attr('value')
162     ;
163     dojo.xhrGet({"url":url, "handleAs":"xml", "content":{"format":"marcxml"}, "preventCache": true, "load":displayAuthorities });
164 }
165
166 function clearMergeRecords() {
167     records = dojo.query('.toMerge').orphan();
168     dojo.addClass('mergebox-div', 'hidden');
169 }
170
171 function mergeRecords() {
172     records = dojo.query('.toMerge').attr('id');
173     dojo.forEach(records, function(item, idx) {
174         records[idx] = parseInt(item.slice(item.lastIndexOf('_') + 1));
175     });
176     alert('TODO: actually merge the gathered records: ' + dojo.toJson(records));
177 }