]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/server/serial/sdist_editor.js
Better coordination of MFHD/SRE adding/deleting
[working/Evergreen.git] / Open-ILS / xul / staff_client / server / serial / sdist_editor.js
1 dump('entering serial/sdist_editor.js\n');
2 // vim:noet:sw=4:ts=4:
3
4 JSAN.use('serial.editor_base');
5
6 if (typeof serial == 'undefined') serial = {};
7 serial.sdist_editor = function (params) {
8     try {
9         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
10         JSAN.use('util.error'); this.error = new util.error();
11         JSAN.use('OpenILS.data'); this.data = new OpenILS.data(); this.data.init({'via':'stash'});
12         JSAN.use('util.network'); this.network = new util.network();
13     } catch(E) {
14         dump('serial/sdist_editor: ' + E + '\n');
15     }
16
17     /* This keeps track of what fields have been edited for styling purposes */
18     this.changed = {};
19
20     /* This holds the original values for prepopulating the field editors */
21     this.editor_values = {};
22
23     // setup sre arrays
24     this.sre_id_map = {};
25     this.sres_ou_map = {};
26     this.build_sre_maps();
27
28     // update sre maps on demand
29     var obj = this;
30     window.parent.addEventListener("MFHDChange", function() {obj.build_sre_maps()}, false);
31 };
32
33 serial.sdist_editor.prototype = {
34     // we could do this with non-standard '__proto__' property instead
35     'editor_base_init' : serial.editor_base.editor_base_init,
36     'editor_base_apply' : serial.editor_base.editor_base_apply,
37     'editor_base_save' : serial.editor_base.editor_base_save,
38
39     'fm_type' : 'sdist',
40     'fm_type_plural' : 'sdists',
41     'can_have_notes' : true,
42
43     'init' : function (params) {
44         var obj = this;
45
46         params.retrieve_function = 'FM_SDIST_FLESHED_BATCH_RETRIEVE.authoritative';
47
48         obj.editor_base_init(params);
49
50         obj.multi_org_edit = false;
51         var org_unit = obj.sdists[0].holding_lib();
52         for (var i = 1; i < obj.sdists.length; i++) {
53             if (obj.sdists[i].holding_lib() != org_unit) {
54                 obj.multi_org_edit = true;
55                 break;
56             }
57         }        
58
59         /* Do it */
60         obj.summarize( obj.sdists );
61         obj.render();
62     },
63
64     /******************************************************************************************************/
65     /* Restore backup copies */
66
67     'reset' :  serial.editor_base.editor_base_reset,
68
69     /******************************************************************************************************/
70     /* Apply a value to a specific field on all the copies being edited */
71
72     'apply' : function(field,value) {
73         var obj = this;
74
75         // null out call number if the holding lib is changed
76         obj.holding_lib_changed = (field == 'holding_lib');
77         var loop_func = function(sdist) {
78             if (obj.holding_lib_changed) {
79                 var field_name_list = ['bind_call_number','receive_call_number','bind_unit_template','receive_unit_template','record_entry'];
80                 for (var i = 0; i < field_name_list.length; i++) {
81                     sdist[field_name_list[i]](null);
82                     obj.changed[fieldmapper.IDL.fmclasses.sdist.field_map[field_name_list[i]].label] = true;
83                 }
84             }
85         }
86         obj.editor_base_apply(field, value, loop_func);
87         obj.holding_lib_changed = false;
88     },
89
90     /******************************************************************************************************/
91
92     'render_call_number' : function(cn) {
93         var obj = this;
94         if (cn == null) { // true for both 'null' AND undefined
95             return '';
96         } else if (typeof cn != 'object') {
97             return obj.acn_label_map[cn];
98         } else {
99             return cn.label()
100         }
101     },
102
103     'render_unit_template' : function(ut) {
104         var obj = this;
105         if (ut == null) { // true for both 'null' AND undefined
106             return '';
107         } else if (typeof ut != 'object') {
108             return obj.act_name_map[ut];
109         } else {
110             return ut.name()
111         }
112     },
113
114     'render_record_entry' : function(sre) {
115         var obj = this;
116         var sre_id;
117         if (sre == null) { // true for both 'null' AND undefined
118             return '';
119         } else if (typeof sre != 'object') {
120             sre_id = sre;
121         } else {
122             sre_id = sre.id();
123         }
124         return obj.sre_id_map[sre_id].label;
125     },
126
127     'init_panes' : function () {
128         var obj = this;
129         obj.panes_and_field_names = {
130
131         /* These get shown in the left panel */
132         '_editor_left_pane' :
133         [
134             [
135                 'id',
136                 { 
137                     //input: 'c = function(v){ obj.apply("distribution",v); if (typeof post_c == "function") post_c(v); }; x = document.createElement("textbox"); x.addEventListener("apply",function(f){ return function(ev) { f(ev.target.value); } }(c), false);',
138
139                 }
140             ],
141             [
142                 'label',
143                 { 
144                     input: 'c = function(v){ obj.apply("label",v); if (typeof post_c == "function") post_c(v); }; x = document.createElement("textbox"); x.setAttribute("value",obj.editor_values.label); x.addEventListener("apply",function(f){ return function(ev) { f(ev.target.value); } }(c), false);',
145                     value_key: 'label',
146                     required: true
147                 }
148             ],
149             [
150                 'summary_method',
151                 {
152                     render: 'obj.summary_methods[fm.summary_method()]',
153                     input: 'c = function(v){ obj.apply("summary_method",v); if (typeof post_c == "function") post_c(v); }; x = util.widgets.make_menulist( util.functional.map_object_to_list( obj.summary_methods, function(obj,i) { return [ obj[i], i ]; })); x.setAttribute("value",obj.editor_values.summary_method); x.addEventListener("apply",function(f){ return function(ev) { f(ev.target.value); } }(c), false);',
154                     value_key: 'summary_method',
155                     dropdown_key: 'fm.summary_method() == null ? null : fm.summary_method()'
156                 }
157             ],
158             [
159                 'unit_label_prefix',
160                 {
161                     input: 'c = function(v){ obj.apply("unit_label_prefix",v); if (typeof post_c == "function") post_c(v); }; x = document.createElement("textbox"); x.setAttribute("value",obj.editor_values.unit_label_prefix); x.addEventListener("apply",function(f){ return function(ev) { f(ev.target.value); } }(c), false);',
162                     value_key: 'unit_label_prefix'
163                 }
164             ],
165             [
166                 'unit_label_suffix',
167                 { 
168                     input: 'c = function(v){ obj.apply("unit_label_suffix",v); if (typeof post_c == "function") post_c(v); }; x = document.createElement("textbox"); x.setAttribute("value",obj.editor_values.unit_label_suffix); x.addEventListener("apply",function(f){ return function(ev) { f(ev.target.value); } }(c), false);',
169                     value_key: 'unit_label_suffix'
170                 }
171             ],
172         ],
173         /* These get shown in the right panel */
174             '_editor_right_pane' :
175         [
176             [
177                 'holding_lib',
178                 {
179                     render: 'typeof fm.holding_lib() == "object" ? fm.holding_lib().shortname() : obj.data.hash.aou[ fm.holding_lib() ].shortname()',
180                     input: 'c = function(v){ if (obj.editor_values.holding_lib != v) obj.apply("holding_lib",v); if (typeof post_c == "function") post_c(v); }; x = util.widgets.make_menulist( util.functional.map_list( obj.data.list.aou, function(myobj) { var sname = myobj.shortname(); for (i = sname.length; i < 20; i++) sname += " "; return [ myobj.name() ? sname + " " + myobj.name() : myobj.shortname(), myobj.id(), ( ! get_bool( obj.data.hash.aout[ myobj.ou_type() ].can_have_vols() ) ), ( obj.data.hash.aout[ myobj.ou_type() ].depth() * 2), ]; }), obj.data.list.au[0].ws_ou()); x.setAttribute("value",obj.editor_values.holding_lib); x.addEventListener("apply",function(f){ return function(ev) { f(ev.target.value); } }(c), false);',
181                     value_key: 'holding_lib',
182                     dropdown_key: 'typeof fm.holding_lib() == "object" ? fm.holding_lib().id() : fm.holding_lib()',
183                 }
184             ],
185         ],
186         /* These get shown in the right 'library-specific-options' panel */
187         '_editor_lso_pane' :
188         [
189             [
190                 'record_entry',
191                 {
192                     render: 'obj.render_record_entry(fm.record_entry())',
193                     input: 'if(!obj.multi_org_edit) { c = function(v){ obj.apply("record_entry",v); if (typeof post_c == "function") post_c(v); }; x = util.widgets.make_menulist( util.functional.map_list( obj.get_sre_details_list(), function(obj) { return [ obj.label, obj.id ]; }).sort()); x.setAttribute("value",obj.editor_values.record_entry); x.addEventListener("apply",function(f){ return function(ev) { f(ev.target.value); } }(c), false); }',
194                     value_key: 'record_entry',
195                     dropdown_key: 'fm.record_entry() == null ? null : typeof fm.record_entry() == "object" ? fm.record_entry().id() : fm.record_entry()'
196                 }
197             ],
198             [
199                 'receive_call_number',
200                 {
201                     render: 'obj.render_call_number(fm.receive_call_number())',
202                     input: 'if(!obj.multi_org_edit) { c = function(v){ obj.apply("receive_call_number",v); if (typeof post_c == "function") post_c(v); }; x = util.widgets.make_menulist( util.functional.map_list( obj.get_acn_list(), function(obj) { return [ obj.label(), obj.id() ]; }).sort()); x.setAttribute("value",obj.editor_values.receive_call_number); x.addEventListener("apply",function(f){ return function(ev) { f(ev.target.value); } }(c), false); }',
203                     value_key: 'receive_call_number',
204                     dropdown_key: 'fm.receive_call_number() == null ? null : typeof fm.receive_call_number() == "object" ? fm.receive_call_number().id() : fm.receive_call_number()'
205                 }
206             ],
207             [
208                 'bind_call_number',
209                 {
210                     render: 'obj.render_call_number(fm.bind_call_number())',
211                     input: 'if(!obj.multi_org_edit) { c = function(v){ obj.apply("bind_call_number",v); if (typeof post_c == "function") post_c(v); }; x = util.widgets.make_menulist( util.functional.map_list( obj.get_acn_list(), function(obj) { return [ obj.label(), obj.id() ]; }).sort()); x.setAttribute("value",obj.editor_values.bind_call_number); x.addEventListener("apply",function(f){ return function(ev) { f(ev.target.value); } }(c), false); }',
212                     value_key: 'bind_call_number',
213                     dropdown_key: 'fm.bind_call_number() == null ? null : typeof fm.bind_call_number() == "object" ? fm.bind_call_number().id() : fm.bind_call_number()'
214                 }
215             ],
216             [
217                 'receive_unit_template',
218                 {
219                     render: 'obj.render_unit_template(fm.receive_unit_template())',
220                     input: 'if(!obj.multi_org_edit) { c = function(v){ obj.apply("receive_unit_template",v); if (typeof post_c == "function") post_c(v); }; x = util.widgets.make_menulist( util.functional.map_list( obj.get_act_list(), function(obj) { return [ obj.name(), obj.id() ]; }).sort()); x.setAttribute("value",obj.editor_values.receive_unit_template); x.addEventListener("apply",function(f){ return function(ev) { f(ev.target.value); } }(c), false); }',
221                     value_key: 'receive_unit_template',
222                     dropdown_key: 'fm.receive_unit_template() == null ? null : typeof fm.receive_unit_template() == "object" ? fm.receive_unit_template().id() : fm.receive_unit_template()'
223                 }
224             ],
225             [
226                 'bind_unit_template',
227                 {
228                     render: 'obj.render_unit_template(fm.bind_unit_template())',
229                     input: 'if(!obj.multi_org_edit) { c = function(v){ obj.apply("bind_unit_template",v); if (typeof post_c == "function") post_c(v); }; x = util.widgets.make_menulist( util.functional.map_list( obj.get_act_list(), function(obj) { return [ obj.name(), obj.id() ]; }).sort()); x.setAttribute("value",obj.editor_values.bind_unit_template); x.addEventListener("apply",function(f){ return function(ev) { f(ev.target.value); } }(c), false); }',
230                     value_key: 'bind_unit_template',
231                     dropdown_key: 'fm.bind_unit_template() == null ? null : typeof fm.bind_unit_template() == "object" ? fm.bind_unit_template().id() : fm.bind_unit_template()'
232                 }
233             ],
234         ],
235
236         };
237         for (i in obj.panes_and_field_names) {
238             obj.panes_and_field_names[obj.xul_id_prefix + i] = obj.panes_and_field_names[i];
239             delete obj.panes_and_field_names[i];
240         }
241     },
242
243     /******************************************************************************************************/
244     /* This loops through all our fieldnames and all the copies, tallying up counts for the different values */
245
246     'summarize' :  serial.editor_base.editor_base_summarize,
247
248     /******************************************************************************************************/
249     /* Display the summarized data and inputs for editing */
250
251     'render' :  serial.editor_base.editor_base_render,
252
253     /******************************************************************************************************/
254     /* This actually draws the change button and input widget for a given field */
255     'render_input' : serial.editor_base.editor_base_render_input,
256
257     /******************************************************************************************************/
258     /* save the distributions */
259
260     'save' : function() {
261         var obj = this;
262         obj.editor_base_save('open-ils.serial.distribution.fleshed.batch.update');
263     },
264
265     /******************************************************************************************************/
266     /* spawn notes interface */
267
268     'notes' : function() {
269         var obj = this;
270         JSAN.use('util.window'); var win = new util.window();
271         win.open(
272             urls.XUL_SERIAL_NOTES, 
273             //+ '?copy_id=' + window.escape(obj.sdists[0].id()),
274             $('serialStrings').getString('staff.serial.sdist_editor.notes'),'chrome,resizable,modal',
275             { 'object_id' : obj.sdists[0].id(), 'function_type' : 'SDISTN', 'object_type' : 'distribution', 'constructor' : sdistn }
276         );
277     },
278
279     /******************************************************************************************************/
280     'save_attributes' : serial.editor_base.editor_base_save_attributes,
281
282     /******************************************************************************************************/
283     /* Build maps of sre details for both display and selection purposes */
284
285     'build_sre_maps' : function() {
286         var obj = this;
287         try {
288             obj.sre_id_map = {};
289             obj.sres_ou_map = {};
290             var parent_g = window.parent.g;
291             if (parent_g.mfhd) {
292                 var mfhd_details = parent_g.mfhd.details;
293                 for (var i = 0; i < mfhd_details.length; i++) {
294                     var mfhd_detail = {};
295                     for (j in mfhd_details[i]) {
296                         mfhd_detail[j] = mfhd_details[i][j];
297                     }
298                     mfhd_detail.label = mfhd_detail.label + ' (' + (mfhd_detail.entryNum + 1) + ')';
299                     var sre_id = mfhd_detail.id;
300                     var org_unit_id = mfhd_detail.owning_lib;
301                     obj.sre_id_map[sre_id] = mfhd_detail;
302                     if (!obj.sres_ou_map[org_unit_id]) {
303                         obj.sres_ou_map[org_unit_id] = [];
304                     }
305                     obj.sres_ou_map[org_unit_id].push(mfhd_detail);
306                 }
307             }
308         } catch(E) {
309             obj.error.standard_unexpected_error_alert('build_sre_maps',E);
310         }
311     },
312
313     /******************************************************************************************************/
314     /* This returns a list of sre details appropriate for the distributions being edited */
315
316     'get_sre_details_list' : function() {
317         var obj = this;
318         try {
319             /* we only show this list if dealing with one org_unit, default to first sdist*/
320             var lib_id = typeof obj.sdists[0].holding_lib() == 'object' ? obj.sdists[0].holding_lib().id() : obj.sdists[0].holding_lib();
321             var sre_details_list = obj.sres_ou_map[lib_id];
322             if (sre_details_list == null) {
323                 return [];
324             } else {
325                 return sre_details_list;
326             }
327         } catch(E) {
328             obj.error.standard_unexpected_error_alert('get_sre_details_list',E);
329             return [];
330         }
331     },
332
333     /******************************************************************************************************/
334     /* This returns a list of acn's appropriate for the distributions being edited */
335
336     'get_acn_list' : function() {
337         var obj = this;
338         try {
339             var lib_id = typeof obj.sdists[0].holding_lib() == 'object' ? obj.sdists[0].holding_lib().id() : obj.sdists[0].holding_lib();
340
341             if (!obj.acn_lists) {
342                 obj.acn_lists = {};
343             }
344
345             // return cached version if we have it
346             // TODO: clear cache on holding_lib change? (cannot remember how to reproduce this bug)
347             if (obj.acn_lists[lib_id]) {
348                 return obj.acn_lists[lib_id];
349             }
350
351             var acn_list = obj.network.request(
352                 'open-ils.pcrud',
353                 'open-ils.pcrud.search.acn',
354                 [ ses(), {"record" : obj.docid, "owning_lib" : lib_id, "deleted" : 'f' }, {"order_by" : {"acn" : "label"} } ]
355             );
356
357             if (!acn_list) {
358                 return [];
359             } else if (!acn_list.length) {
360                 acn_list = [acn_list];
361             }
362
363             // build label map
364             obj.acn_label_map = {};
365             for (i = 0; i < acn_list.length; i++) {
366                 obj.acn_label_map[acn_list[i].id()] = acn_list[i].label();
367             }
368
369             // cache the list
370             obj.acn_lists[lib_id] = acn_list;
371             return acn_list;
372
373         } catch(E) {
374             obj.error.standard_unexpected_error_alert('get_acn_list',E);
375             return [];
376         }
377     },
378
379     /******************************************************************************************************/
380     /* This returns a list of asset copy templates appropriate for the distributions being edited */
381
382     'get_act_list' : function() {
383         var obj = this;
384         try {
385             /* we only show this list if dealing with one org_unit, default to first sdist*/
386             var lib_id = typeof obj.sdists[0].holding_lib() == 'object' ? obj.sdists[0].holding_lib().id() : obj.sdists[0].holding_lib();
387
388             if (!obj.act_lists) {
389                 obj.act_lists = {};
390             }
391
392             // return cached version if we have it
393             if (obj.act_lists[lib_id]) {
394                 return obj.act_lists[lib_id];
395             }
396             
397             var act_list = obj.network.request(
398                 'open-ils.pcrud',
399                 'open-ils.pcrud.search.act',
400                 [ ses(), {"owning_lib" : lib_id }, {"order_by" : {"act" : "name"} } ]
401             );
402
403             if (act_list == null) {
404                 return [];
405             } else if (!act_list.length) {
406                 act_list = [act_list];
407             }
408
409             // build name map
410             obj.act_name_map = {};
411             for (i = 0; i < act_list.length; i++) {
412                 obj.act_name_map[act_list[i].id()] = act_list[i].name();
413             }
414
415             // cache the list
416             obj.act_lists[lib_id] = act_list;
417             return act_list;
418         } catch(E) {
419             obj.error.standard_unexpected_error_alert('get_act_list',E);
420             return [];
421         }
422     },
423     /******************************************************************************************************/
424     'summary_methods' : {
425         "add_to_sre" : $('serialStrings').getString('staff.serial.sdist_editor.add_to_sre.label'),
426         "merge_with_sre" : $('serialStrings').getString('staff.serial.sdist_editor.merge_with_sre.label'),
427         "use_sre_only" : $('serialStrings').getString('staff.serial.sdist_editor.use_sre_only.label'),
428         "use_sdist_only" : $('serialStrings').getString('staff.serial.sdist_editor.use_sdist_only.label'),
429     }
430 };
431
432 dump('exiting serial/sdist_editor.js\n');