]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/server/cat/bucketz39_dialog.js
Replace deprecated javascript escape() with encodeURIComponent()
[Evergreen.git] / Open-ILS / xul / staff_client / server / cat / bucketz39_dialog.js
1 var dialog;
2
3 function Bucketz39Dialog() {
4
5     /**
6      * builds the  Z39 sources and Z39 search indexes grid
7     */
8     this._build_options_grid = function(list, key, id_attr, label_attr) {
9
10         // determine the number of columns per row dynamically
11         var grid = dojo.byId(key).parentNode;
12         var colcount = grid.getElementsByTagName('column').length;
13
14         var row;
15         dojo.forEach(list, function(obj, idx) {
16
17             if (idx % colcount == 0) {
18                 row = dojo.create('row');
19                 dojo.byId(key).appendChild(row);
20             }
21
22             var attrs = {
23                 value : obj[id_attr](),
24                 label : obj[label_attr]()
25             };
26             attrs[key] = 1;
27
28             row.appendChild(dojo.create('checkbox', attrs));
29         });
30     }
31
32     /**
33      * Fetches needed data
34      */
35     this.init = function() {
36         var self = this;
37         var pcrud = new OpenSRF.ClientSession('open-ils.pcrud');
38
39         // vandelay queues
40         pcrud.request({
41             method : 'open-ils.pcrud.search.vbq.atomic',
42             params : [
43                 this.authtoken, 
44                 {owner : this.user_id, queue_type : 'bib'}, 
45                 {order_by : {vbq : 'name'}}
46             ],
47             oncomplete : function(r) {
48                 if (resp = r.recv()) {
49                     var qlist = resp.content();
50                     dojo.forEach(qlist, function(q) {
51                         var attrs = {label : q.name()};
52                         var item = dojo.create('menuitem', attrs);
53                         dojo.byId('queue_selector').appendChild(item);
54                     });
55                 }
56             }
57         }).send();
58
59         // z39 index field maps
60         pcrud.request({
61             method : 'open-ils.pcrud.search.czifm.atomic',
62             params : [
63                 this.authtoken, 
64                 {id : {'!=' : null}}, 
65                 {order_by : {czifm : 'label'}}
66             ],
67             oncomplete : function(r) {
68                 self._build_options_grid(
69                     r.recv().content(), 
70                     'index_selector', 'id', 'label');
71             }
72         }).send();
73
74         // z39 sources
75         pcrud.request({
76             method : 'open-ils.pcrud.search.czs.atomic',
77             params : [
78                 this.authtoken, 
79                 {name : {'!=' : null}},
80                 {order_by : {czs : 'name'}}
81             ],
82             oncomplete : function(r) {
83                 self._build_options_grid(
84                     r.recv().content(), 
85                     'source_selector', 'name', 'label');
86             }
87         }).send();
88
89         pcrud.request({
90             method : 'open-ils.pcrud.search.vms.atomic',
91             params : [this.authtoken, {
92                 owner : this._ws_ancestors(),
93                 mtype : 'biblio'
94             }],
95             oncomplete : function(r) {
96                 var sets = r.recv().content();
97                 dojo.forEach(sets, function(set) {
98                     var attrs = {label : set.name(), value : set.id() };
99                     var item = dojo.create('menuitem', attrs);
100                     dojo.byId('match_set_selector').appendChild(item);
101                 });
102             }
103         }).send();
104
105     }
106
107     /* my workstation org unit plus ancestors as a flat list */
108     this._ws_ancestors = function() {
109         JSAN.use('OpenILS.data');
110         var data = new OpenILS.data(); 
111         data.stash_retrieve();
112         var org = data.hash.aou[ this.ws_ou ]
113         var org_list = [];
114
115         while (org) {
116             org_list.push(org.id());
117             org = data.hash.aou[org.parent_ou()];
118         }
119         return org_list;
120     }
121
122     /**
123      * extracts params from UI form elements
124      */
125     this._collect_params = function() {
126
127         // request params
128         var params = [this.authtoken, this.bucket_id];
129
130         // Z39 sources
131         params.push(dojo.query('[source_selector]').filter(
132             function(cbox) { return cbox.checked }).map(
133                 function(cbox) { return cbox.getAttribute('value') }));
134
135         // indexes
136         params.push(dojo.query('[index_selector]').filter(
137             function(cbox) { return cbox.checked }).map(
138                 function(cbox) { return cbox.getAttribute('value') }));
139
140         params.push({
141             // queue name (editable menulist)
142             queue_name : dojo.byId('queue_selector').parentNode.value,
143             // match set ID
144             match_set : dojo.byId('match_set_selector').parentNode.value
145         });
146
147         return params;
148     }
149
150     this.submit = function() {
151         var self = this;
152         
153         // progress labels
154         this.search_bib_count = dojo.byId('search-bib-count');
155         this.search_queue_count = dojo.byId('search-queue-count');
156         this.search_progress = dojo.byId('search-progress');
157
158         // hide submit row
159         dojo.addClass(dojo.byId('search-submit-row'), 'hideme');
160
161         // show progress rows
162         dojo.forEach(
163             dojo.query('.search_result_row'),
164             function(row) { dojo.removeClass(row, 'hideme') }
165         );
166
167         var params = this._collect_params();
168         dump('Submitting z39 search with: ' + js2JSON(params) + '\n');
169
170         var ses = new OpenSRF.ClientSession('open-ils.search');
171         ses.request({
172             method : 'open-ils.search.z3950.bucket_search_queue',
173             params : params,
174             onresponse : function(r) {
175                 var resp = r.recv();
176                 if (!resp) return;
177                 var stat = resp.content();
178
179                 dojo.attr(self.search_bib_count, 'value', ''+stat.bre_count);
180                 dojo.attr(self.search_queue_count, 'value', ''+stat.queue_count);
181
182                 var scount = Number(stat.search_count);
183                 if (scount) {
184                     dojo.attr(self.search_progress, 'value', ''+Math.floor(
185                         (Number(stat.search_complete) / scount) * 100
186                     ));
187                 }
188
189                 // queue object is returned in the final response
190                 self.queue = stat.queue;
191             },
192             oncomplete : function() {
193                 dojo.removeClass(dojo.byId('final-actions-row'), 'hideme');
194             }
195         }).send();
196     }
197
198     // Open a new XUL tab focused on the Vandelay queue containing the results.
199     this.open_queue = function() {
200         /*
201         labelKey = labelKey || 'menu.cmd_open_conify.tab';
202         var label = offlineStrings.getString(labelKey);
203         */
204         var label = 'MARC Import/Export'; // TODO
205        
206         // URL
207         /*
208         var url_prefix = this.xulG.url_prefix || window.url_prefix;
209         */
210         var urls = this.xulG.urls || window.urls;
211         var loc = urls.XUL_BROWSER + '?url=' + 
212             window.encodeURIComponent(
213                 this.xulG.url_prefix('EG_WEB_BASE/') +
214                 'vandelay/vandelay?qtype=bib&qid=' + this.queue.id()
215             );
216         
217         var content_params = {
218             'no_xulG': false,
219             'show_print_button': true,
220             'show_nav_buttons': true 
221         };  
222        
223         this.xulG.new_tab(loc, {tab_name: label}, content_params);
224         window.close();
225     }
226 }
227
228 function my_init() {
229     dialog = new Bucketz39Dialog();
230     dialog.user_id   = window.arguments[0];
231     dialog.authtoken = window.arguments[1];
232     dialog.ws_ou     = window.arguments[2];
233     dialog.bucket_id = window.arguments[3];
234     dialog.xulG      = window.arguments[4];
235     dialog.init();
236 }