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