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