]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/cat/authority/list.js
Add a speedbump (confirmation dialog) when deleting authority records
[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
77             // Deleting an authority record is unusual; let's be 100% sure
78             if (!confirm("Are you sure you want to delete record " + recId + "?")) {
79                 return;
80             }
81
82             pcrud = new openils.PermaCrud();
83             auth_rec = pcrud.retrieve("are", recId);
84             if (auth_rec) {
85                 pcrud.eliminate(auth_rec);
86                 alert("Deleted authority record # " + recId);
87             }
88         }, "label":"Delete"}).placeAt(auth_menu, "last");
89
90         auth_mb = new dijit.form.DropDownButton({dropDown: auth_menu, label:"Actions"});
91         auth_mb.placeAt("auth" + authId, "first");
92         auth_menu.startup();
93     });
94 }
95
96 function loadMarcEditor(pcrud, rec) {
97     /*
98        To run in Firefox directly, must set signed.applets.codebase_principal_support
99        to true in about:config
100      */
101     netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
102     win = window.open('/xul/server/cat/marcedit.xul'); // XXX version?
103
104     win.xulG = {
105         "record": {"marc": rec.marc()},
106         "save": {
107             "label": "Save",
108             "func": function(xmlString) {
109                 rec.marc(xmlString);
110                 rec.ischanged(true);
111                 pcrud.update(rec);
112                 alert("Record was saved");
113                 win.close();
114             }
115         }
116     };
117 }
118
119 function authListInit() {
120     term = cgi.param('authTerm') || '';
121     page = cgi.param('authPage') || 0;
122     axis = cgi.param('authAxis') || 'authority.author';
123     if (axis) {
124         dijit.byId('authAxis').attr('value', axis);
125     }
126     if (page) {
127         dijit.byId('authPage').attr('value', page);
128     }
129     if (term) {
130         dijit.byId('authTerm').attr('value', term);
131         displayRecords();
132     }
133     dojo.connect(dijit.byId('authTerm'), 'onBlur', 'displayRecords');
134 }
135 dojo.addOnLoad(authListInit);
136
137 function displayRecords(parms) {
138
139     if (parms && parms.page) {
140         if (parms.page == 'next') {
141             page = dijit.byId('authPage').attr('value');
142             dijit.byId('authPage').attr('value', page + 1);
143         } else if (parms.page == 'prev') {
144             page = dijit.byId('authPage').attr('value');
145             dijit.byId('authPage').attr('value', page - 1);
146         } else {
147             dijit.byId('authPage').attr('value', parms.page);
148         }
149     }
150
151     /* Protect against null input */
152     if (!dijit.byId('authTerm').attr('value')) {
153         return;
154     }
155
156     /* Clear out the current contents of the page */
157     widgets = dijit.findWidgets(dojo.byId('authlist-div'));
158     dojo.forEach(widgets, function(w) { w.destroyRecursive(true); });
159
160     dojo.query("#authlist-div div").orphan();
161
162     url = '/opac/extras/startwith/marcxml/'
163         + dijit.byId('authAxis').attr('value')
164         // + '/' + dijit.byId('authOU').attr('value')
165         + '/1' // replace with preceding line if OUs gain some meaning
166         + '/' + dijit.byId('authTerm').attr('value')
167         + '/' + dijit.byId('authPage').attr('value')
168     ;
169     dojo.xhrGet({"url":url, "handleAs":"xml", "content":{"format":"marcxml"}, "preventCache": true, "load":displayAuthorities });
170 }
171
172 function clearMergeRecords() {
173     records = dojo.query('.toMerge').orphan();
174     dojo.addClass('mergebox-div', 'hidden');
175 }
176
177 function mergeRecords() {
178     records = dojo.query('.toMerge').attr('id');
179     dojo.forEach(records, function(item, idx) {
180         records[idx] = parseInt(item.slice(item.lastIndexOf('_') + 1));
181     });
182     alert('TODO: actually merge the gathered records: ' + dojo.toJson(records));
183 }