]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/server/cat/spine_labels.js
Make spine label settings configurable as org unit settings
[Evergreen.git] / Open-ILS / xul / staff_client / server / cat / spine_labels.js
1         function my_init() {
2             try {
3                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
4                 if (typeof JSAN == 'undefined') { throw( $("commonStrings").getString('common.jsan.missing') ); }
5                 JSAN.errorLevel = "die"; // none, warn, or die
6                 JSAN.addRepository('/xul/server/');
7                 JSAN.use('util.error'); g.error = new util.error();
8                 g.error.sdump('D_TRACE','my_init() for spine_labels.xul');
9
10                 JSAN.use('util.network'); g.network = new util.network();
11
12                 g.cgi = new CGI();
13
14                 g.barcodes = [];
15                 if (g.cgi.param('barcodes')) {
16                     g.barcodes = g.barcodes.concat( JSON2js(g.cgi.param('barcodes')) );
17                 }
18                 JSAN.use('OpenILS.data'); g.data = new OpenILS.data(); g.data.stash_retrieve();
19                 if (g.data.temp_barcodes_for_labels) {
20                     g.barcodes = g.barcodes.concat( g.data.temp_barcodes_for_labels );
21                     g.data.temp_barcodes_for_labels = null; g.data.stash('temp_barcodes_for_labels');
22                 }
23
24                 JSAN.use('circ.util');
25                 g.cols = circ.util.columns( {} );
26                 g.col_map = {};
27                 for (var i = 0; i < g.cols.length; i++) {
28                     g.col_map[ g.cols[i].id ] = { 'regex' : new RegExp('%' + g.cols[i].id + '%',"g"), 'render' : g.cols[i].render };
29                 }
30
31                 g.volumes = {};
32
33                 for (var i = 0; i < g.barcodes.length; i++) {
34                     var copy = g.network.simple_request( 'FM_ACP_RETRIEVE_VIA_BARCODE.authoritative', [ g.barcodes[i] ] );
35                     if (typeof copy.ilsevent != 'undefined') throw(copy);
36                     if (!g.volumes[ copy.call_number() ]) {
37                         var volume = g.network.simple_request( 'FM_ACN_RETRIEVE.authoritative', [ copy.call_number() ] );
38                         if (typeof volume.ilsevent != 'undefined') throw(volume);
39                         var record = g.network.simple_request('MODS_SLIM_RECORD_RETRIEVE.authoritative', [ volume.record() ]);
40                         volume.record( record );
41                         g.volumes[ volume.id() ] = volume;
42                     }
43                     if (g.volumes[ copy.call_number() ].copies()) {
44                         var copies = g.volumes[ copy.call_number() ].copies();
45                         copies.push( copy );
46                         g.volumes[ copy.call_number() ].copies( copies );
47                     } else {
48                         g.volumes[ copy.call_number() ].copies( [ copy ] );
49                     }
50                 }
51
52                 generate();
53
54                 if (typeof xulG != 'undefined') $('close').hidden = true;
55
56             } catch(E) {
57                 try {
58                     g.error.standard_unexpected_error_alert('/xul/server/cat/spine_labels.xul',E);
59                 } catch(F) {
60                     alert('FIXME: ' + js2JSON(E));
61                 }
62             }
63         }
64
65         function show_macros() {
66             JSAN.use('util.functional');
67             alert( util.functional.map_list( g.cols, function(o) { return '%' + o.id + '%'; } ).join(" ") );
68         }
69
70         function $(id) { return dojo.byId(id); }
71
72         function generate(override) {
73             try {
74                 var idx = 0;
75                 JSAN.use('util.text');
76                 JSAN.use('util.money');
77                 JSAN.use('util.widgets');
78                 var pn = $('panel');
79                 $('preview').disabled = false;
80
81                 /* Grab from OU settings, then fall back to hardcoded defaults */
82                 var label_cfg = {};
83                 label_cfg.spine_width = Number($('lw').value); /* spine label width */
84                 if (!label_cfg.spine_width) {
85                     label_cfg.spine_width = g.data.hash.aous['cat.spine.line.width'] || 8;
86                     $('lw').value = label_cfg.spine_width;
87                 }
88                 label_cfg.spine_length = Number($('ll').value); /* spine label length */
89                 if (!label_cfg.spine_length) {
90                     label_cfg.spine_length = g.data.hash.aous['cat.spine.line.height'] || 9;
91                     $('ll').value = label_cfg.spine_length;
92                 }
93                 label_cfg.spine_left_margin = Number($('lm').value); /* left margin */
94                 if (!label_cfg.spine_left_margin) {
95                     label_cfg.spine_left_margin = g.data.hash.aous['cat.spine.line.margin'] || 11;
96                     $('lm').value = label_cfg.spine_left_margin;
97                 }
98                 label_cfg.font_size = Number( $('pt').value );  /* font size */
99                 if (!label_cfg.font_size) {
100                     label_cfg.font_size = g.data.hash.aous['cat.label.font.size'] || 10;
101                     $('pt').value = label_cfg.font_size;
102                 }
103                 label_cfg.font_family = g.data.hash.aous['cat.label.font.family'] || 'monospace';
104                 label_cfg.pocket_width = Number($('plw').value) || 28; /* pocket label width */
105                 label_cfg.pocket_length = Number($('pll').value) || 9; /* pocket label length */
106
107                 if (override) {
108                     var gb = $('acn_' + g.volumes[override.acn].id());
109                     util.widgets.remove_children('acn_' + g.volumes[override.acn].id());
110                     generate_labels(g.volumes[override.acn], gb, label_cfg, override);
111                 } else {
112                     util.widgets.remove_children('panel');
113                     for (var i in g.volumes) {
114                         var vb = document.createElement('vbox'); pn.appendChild(vb);
115                         vb.setAttribute('name','template');
116                         vb.setAttribute('acn_id',g.volumes[i].id());
117                         var ds = document.createElement('description'); vb.appendChild(ds);
118                         ds.appendChild( document.createTextNode( g.volumes[i].label() ) );
119                         var ds2 = document.createElement('description'); vb.appendChild(ds2);
120                         ds2.appendChild( document.createTextNode( g.volumes[i].copies().length + ' ' + (
121                             g.volumes[i].copies().length == 1 ? $("catStrings").getString('staff.cat.spine_labels.copy') : $("catStrings").getString('staff.cat.spine_labels.copies')) ) );
122                         ds2.setAttribute('style','color: green');
123                         var hb = document.createElement('hbox'); vb.appendChild(hb);
124
125                         var gb = document.createElement('groupbox');
126                         hb.appendChild(gb); 
127                         gb.setAttribute('id','acn_' + g.volumes[i].id());
128
129                         generate_labels(g.volumes[i], gb, label_cfg, override);
130
131                         idx++;
132                     }
133                 }
134             } catch(E) {
135                 g.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.spine_labels.generate.std_unexpeceted_err'),E);
136             }
137         }
138
139         function generate_labels(volume, label_node, label_cfg, override) {
140             var names;
141
142             if (override && volume.id() == override.acn) {
143                 /* If we're calling ourself, we'll have an altered label */
144                 names = String(override.label).split(/\s+/);
145             } else {
146                 /* take the call number and split it on whitespace */
147                 names = String(volume.label()).split(/\s+/);
148             }
149             var j = 0;
150             while (j < label_cfg.spine_length || j < label_cfg.pocket_length) {
151                 var hb2 = document.createElement('hbox'); label_node.appendChild(hb2);
152                 
153                 /* spine */
154                 if (j < label_cfg.spine_length) {
155                     var tb = document.createElement('textbox'); hb2.appendChild(tb); 
156                     tb.value = '';
157                     tb.setAttribute('class','plain');
158                     tb.setAttribute('style','font-family: ' + label_cfg.font_family + '; font-size: ' + label_cfg.font_size);
159                     tb.setAttribute('size',label_cfg.spine_width+1);
160                     tb.setAttribute('maxlength',label_cfg.spine_width);
161                     tb.setAttribute('name','spine');
162                     var spine_row_id = 'acn_' + volume.id() + '_spine_' + j;
163                     tb.setAttribute('id',spine_row_id);
164                     var name = names.shift();
165                     if (name) {
166                         name = String( name );
167                         /* if the name is greater than the label width... */
168                         if (name.length > label_cfg.spine_width) {
169                             /* then try to split it on periods */
170                             var sname = name.split(/\./);
171                             if (sname.length > 1) {
172                                 /* if we can, then put the periods back in on each splitted element */
173                                 if (name.match(/^\./)) sname[0] = '.' + sname[0];
174                                 for (var k = 1; k < sname.length; k++) sname[k] = '.' + sname[k];
175                                 /* and put all but the first one back into the names array */
176                                 names = sname.slice(1).concat( names );
177                                 /* if the name fragment is still greater than the label width... */
178                                 if (sname[0].length > label_cfg.spine_width) {
179                                     /* then just truncate and throw the rest back into the names array */
180                                     tb.value = sname[0].substr(0,label_cfg.spine_width);
181                                     names = [ sname[0].substr(label_cfg.spine_width) ].concat( names );
182                                 } else {
183                                     /* otherwise we're set */
184                                     tb.value = sname[0];
185                                 }
186                             } else {
187                                 /* if we can't split on periods, then just truncate and throw the rest back into the names array */
188                                 tb.value = name.substr(0,label_cfg.spine_width);
189                                 names = [ name.substr(label_cfg.spine_width) ].concat( names );
190                             }
191                         } else {
192                             /* otherwise we're set */
193                             tb.value = name;
194                         }
195                     }
196                     dojo.connect($(spine_row_id), 'onkeypress', 'spine_label_key_events');
197                 }
198
199                 /* pocket */
200                 if ($('pl').checked && j < label_cfg.pocket_length) {
201                     var tb2 = document.createElement('textbox'); hb2.appendChild(tb2); 
202                     tb2.value = '';
203                     tb2.setAttribute('class','plain'); tb2.setAttribute('style','font-family: ' + label_cfg.font_family + '; font-size: ' + label_cfg.font_size);
204                     tb2.setAttribute('size',label_cfg.pocket_width+1); tb2.setAttribute('maxlength',label_cfg.pocket_width);
205                     tb2.setAttribute('name','pocket');
206                     if ($('title').checked && $('title_line').value == j + 1 && instanceOf(volume.record(),mvr)) {
207                         if (volume.record().title()) {
208                             tb2.value = util.text.wrap_on_space( volume.record().title(), label_cfg.pocket_width )[0];
209                         } else {
210                             tb2.value = '';
211                         }
212                     }
213                     if ($('title_r').checked && $('title_r_line').value == j + 1 && instanceOf(volume.record(),mvr)) {
214                         if (volume.record().title()) {
215                             tb2.value = ( ($('title_r_indent').checked ? ' ' : '') + util.text.wrap_on_space( volume.record().title(), label_cfg.pocket_width )[1]).substr(0,label_cfg.pocket_width);
216                         } else {
217                             tb2.value = '';
218                         }
219                     }
220                     if ($('author').checked && $('author_line').value == j + 1 && instanceOf(volume.record(),mvr)) {
221                         if (volume.record().author()) {
222                             tb2.value = volume.record().author().substr(0,label_cfg.pocket_width);
223                         } else {
224                             tb2.value = '';
225                         }
226                     }
227                     if ($('call_number').checked && $('call_number_line').value == j + 1) {
228                         tb2.value = volume.label().substr(0,label_cfg.pocket_width);
229                     }
230                     if ($('owning_lib_shortname').checked && $('owning_lib_shortname_line').value == j + 1) {
231                         var lib = volume.owning_lib();
232                         if (!instanceOf(lib,aou)) lib = g.data.hash.aou[ lib ];
233                         tb2.value = lib.shortname().substr(0,label_cfg.pocket_width);
234                     }
235                     if ($('owning_lib').checked && $('owning_lib_line').value == j + 1) {
236                         var lib = volume.owning_lib();
237                         if (!instanceOf(lib,aou)) lib = g.data.hash.aou[ lib ];
238                         tb2.value = lib.name().substr(0,label_cfg.pocket_width);
239                     }
240                     if ($('shelving_location').checked && $('shelving_location_line').value == j + 1) {
241                         tb2.value = '%location%';
242                     }
243                     if ($('barcode').checked && $('barcode_line').value == j + 1) {
244                         tb2.value = '%barcode%';
245                     }
246                     if ($('custom1').checked && $('custom1_line').value == j + 1) {
247                         tb2.value = $('custom1_tb').value;
248                     }
249                     if ($('custom2').checked && $('custom2_line').value == j + 1) {
250                         tb2.value = $('custom2_tb').value;
251                     }
252                     if ($('custom3').checked && $('custom3_line').value == j + 1) {
253                         tb2.value = $('custom3_tb').value;
254                     }
255                     if ($('custom4').checked && $('custom4_line').value == j + 1) {
256                         tb2.value = $('custom4_tb').value;
257                     }
258                 }
259
260                 j++;
261             }
262         }
263
264         function spine_label_key_events (event) {
265
266             /* Current value of the inpux box */
267             var line_value = event.target.value;
268
269             /* Cursor positions */
270             var sel_start = event.target.selectionStart;
271             var sel_end = event.target.selectionEnd;
272
273             /* Identifiers for this row: "acn_ID_spine_ROW" */
274             var atts = event.target.id.split('_');
275             var row_id = {
276                 "acn": atts[1],
277                 "spine": atts[3],
278                 "prefix": 'acn_' + atts[1] + '_spine_'
279             };
280
281             switch (event.charOrCode) {
282                 case dojo.keys.ENTER : {
283                     /* Create a new row by inserting a space at the
284                      * current cursor point, then regenerating the
285                      * label
286                      */
287                     if (sel_start == sel_end) {
288                         /* Special case if the cursor is at the start of the line */
289                         if (sel_start == 0) {
290                             line_value = ' ' + line_value;
291                         } else {
292                             line_value = line_value.substr(0, sel_start) + ' ' + line_value.substr(sel_end);
293                         }
294                     } else {
295                         line_value = line_value.substr(0, sel_start) + ' ' + line_value.substr(sel_end);
296                     }
297                     event.target.value = line_value;
298
299                     /* Recreate the label */
300                     var new_label = '';
301                     var chunk;
302                     var x = 0;
303                     while (chunk = $(row_id.prefix + x)) {
304                         if (x > 0) {
305                             new_label += ' ' + chunk.value;
306                         } else {
307                             new_label = chunk.value;
308                         }
309                         x++;
310                     }
311                     generate({"acn": row_id.acn, "label": new_label});
312                     $(row_id.prefix + row_id.spine).focus();
313                     break;
314                 }
315
316                 case dojo.keys.BACKSPACE : {
317                     /* Delete line if at the start of an input box */
318                     if (sel_start == 0) {
319                         var new_label = '';
320                         var chunk;
321                         var x = 0;
322                         while (x <= (row_id.spine - 1) && (chunk = $(row_id.prefix + x))) {
323                             if (x > 0) {
324                                 new_label += ' ' + chunk.value;
325                             } else {
326                                 new_label = chunk.value;
327                             }
328                             x++;
329                         }
330
331                         if (chunk = $(row_id.prefix + x)) {
332                             new_label += chunk.value;
333                             x++;
334                         }
335
336                         while (chunk = $(row_id.prefix + x)) {
337                             new_label += ' ' + chunk.value;
338                             x++;
339                         }
340                         generate({"acn": row_id.acn, "label": new_label});
341                         $(row_id.prefix + row_id.spine).focus();
342                     }
343                     break;
344                 }
345
346                 case dojo.keys.DELETE : {
347                     /* Delete line if at the end of an input box */
348                     if (sel_start == event.target.textLength) {
349                         var new_label = '';
350                         var chunk;
351                         var x = 0;
352                         while (x <= row_id.spine && (chunk = $(row_id.prefix + x))) {
353                             if (x > 0) {
354                                 new_label += ' ' + chunk.value;
355                             } else {
356                                 new_label = chunk.value;
357                             }
358                             x++;
359                         }
360
361                         if (chunk = $(row_id.prefix + x)) {
362                             new_label += chunk.value;
363                             x++;
364                         }
365
366                         while (chunk = $(row_id.prefix + x)) {
367                             new_label += ' ' + chunk.value;
368                             x++;
369                         }
370                         generate({"acn": row_id.acn, "label": new_label});
371                         $(row_id.prefix + row_id.spine).focus();
372                     }
373                     break;
374                 }
375
376                 case dojo.keys.UP_ARROW : {
377                     /* Move to the previous row */
378                     var prev_row = $(row_id.prefix + (parseInt(row_id.spine) - 1));
379                     if (prev_row) {
380                         prev_row.focus();
381                     }
382                     break;
383                 }
384
385                 case dojo.keys.DOWN_ARROW : {
386                     /* Move to the next row */
387                     var next_row = $(row_id.prefix + (parseInt(row_id.spine) + 1));
388                     if (next_row) {
389                         next_row.focus();
390                     }
391                     break;
392                 }
393             }
394         }
395
396         function expand_macros(text,copy,volume,record) {
397             var my = { 'acp' : copy, 'acn' : volume, 'mvr' : record };
398             var obj = { 'data' : g.data };
399             for (var i in g.col_map) {
400                 var re = g.col_map[i].regex;
401                 if (text.match(re)) {
402                     try {
403                         text = text.replace(re, (typeof g.col_map[i].render == 'function' ? g.col_map[i].render(my) : eval( g.col_map[i].render ) ) );
404                     } catch(E) {
405                         g.error.sdump('D_ERROR','spine_labels.js, expand_macros() = ' + E);
406                     }
407                 }
408             }
409             return text;
410         }
411
412         function preview(idx) {
413             try {
414                     netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
415                     var pt = Number( $('pt').value );  /* font size */
416                     if (!pt) {
417                         pt = g.data.hash.aous['cat.spine.font.size'] || 10;
418                         $('pt').value = pt;
419                     }
420                     var ff = g.data.hash.aous['cat.spine.font.family'] || 'monospace';
421                     var lm = Number($('lm').value); /* left margin */
422                     if (!lm) {
423                         lm = g.data.hash.aous['cat.spine.line.margin'] || 11;
424                     }
425                     var mm = Number($('mm').value); if (mm == NaN) mm = 2; /* middle margin */
426                     var lw = Number($('lw').value); /* spine label width */
427                     if (!lw) {
428                         lw = g.data.hash.aous['cat.spine.line.width'] || 8;
429                         $('lw').value = lw;
430                     }
431                     var ll = Number($('ll').value); /* spine label length */
432                     if (!ll) {
433                         ll = g.data.hash.aous['cat.spine.line.height'] || 9;
434                         $('ll').value = ll;
435                     }
436                     var plw = Number($('plw').value) || 28; var pll = Number($('pll').value) || 9; /* pocket label width and length */
437                     var html = "<html><head>";
438                     html += "<link type='text/css' rel='stylesheet' href='" + xulG.url_prefix('/xul/server/skin/print.css') + "'></link>"
439                     html += "<link type='text/css' rel='stylesheet' href='data:text/css,pre{font-family:" + ff + ";font-size:" + pt + "pt;}'></link>";
440                     html += "<title>Spine Labels</title></head><body>\n";
441                     var nl = document.getElementsByAttribute('name','template');
442                     for (var i = 0; i < nl.length; i++) {
443                         if (typeof idx == 'undefined' || idx == null) { } else {
444                             if (idx != i) continue;
445                         }
446                         var volume = g.volumes[ nl[i].getAttribute('acn_id') ];
447
448                         for (var j = 0; j < volume.copies().length; j++) {
449                             var copy = volume.copies()[j];
450                             if (i == 0 && j == 0) {
451                                 html += '<pre class="first_pre">\n';
452                             } else {
453                                 html += '<pre class="not_first_pre">\n';
454                             }
455                             var gb = nl[i].getElementsByTagName('groupbox')[0];
456                             var nl2 = gb.getElementsByAttribute('name','spine');
457                             for (var k = 0; k < nl2.length; k++) {
458                                 for (var m = 0; m < lm; m++) html += ' ';
459                                 html += util.text.preserve_string_in_html(expand_macros( nl2[k].value, copy, volume, volume.record() ).substr(0,lw));
460                                 if ($('pl').checked) {
461                                     var sib = nl2[k].nextSibling;
462                                     if (sib) {
463                                         for (var m = 0; m < lw - nl2[k].value.length; m++) html += ' ';
464                                         for (var m = 0; m < mm; m++) html += ' ';
465                                         html += util.text.preserve_string_in_html(expand_macros( sib.value, copy, volume, volume.record() ).substr(0,plw));
466                                     }
467                                 }
468                                 html += '\n';
469                             }
470                             html += '</pre hex="0C">\n';
471                         }
472                     }
473                     html += '</body></html>';
474                     var loc = ( urls.XUL_BROWSER );
475                     xulG.new_tab(
476                         loc,
477                         {
478                             'tab_name' : $("catStrings").getString('staff.cat.spine_labels.preview.title')
479                         },
480                         { 
481                             'url' : 'data:text/html;charset=utf-8,'+window.escape(html),
482                             'html_source' : html,
483                             'show_print_button' : 1,
484                             'no_xulG' : 1
485                         }
486                     );
487             } catch(E) {
488                 g.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.spine_labels.preview.std_unexpected_err'),E);
489             }
490         }
491
492