]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/server/cat/spine_labels.js
Cleaner implementation of LC subclass / Cutter spine label splitting
[working/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_weight = $('font_weight').value;  /* font weight */
104                 if (!label_cfg.font_weight) {
105                     label_cfg.font_weight = g.data.hash.aous['cat.label.font.weight'] || 'normal';
106                     $('font_weight').value = label_cfg.font_weight;
107                 }
108                 label_cfg.font_family = g.data.hash.aous['cat.label.font.family'] || 'monospace';
109                 label_cfg.pocket_width = Number($('plw').value) || 28; /* pocket label width */
110                 label_cfg.pocket_length = Number($('pll').value) || 9; /* pocket label length */
111
112                 if (override) {
113                     var gb = $('acn_' + g.volumes[override.acn].id());
114                     util.widgets.remove_children('acn_' + g.volumes[override.acn].id());
115                     generate_labels(g.volumes[override.acn], gb, label_cfg, override);
116                 } else {
117                     util.widgets.remove_children('panel');
118                     for (var i in g.volumes) {
119                         var vb = document.createElement('vbox'); pn.appendChild(vb);
120                         vb.setAttribute('name','template');
121                         vb.setAttribute('acn_id',g.volumes[i].id());
122                         var ds = document.createElement('description'); vb.appendChild(ds);
123                         ds.appendChild( document.createTextNode( g.volumes[i].label() ) );
124                         var ds2 = document.createElement('description'); vb.appendChild(ds2);
125                         ds2.appendChild( document.createTextNode( g.volumes[i].copies().length + ' ' + (
126                             g.volumes[i].copies().length == 1 ? $("catStrings").getString('staff.cat.spine_labels.copy') : $("catStrings").getString('staff.cat.spine_labels.copies')) ) );
127                         ds2.setAttribute('style','color: green');
128                         var hb = document.createElement('hbox'); vb.appendChild(hb);
129
130                         var gb = document.createElement('groupbox');
131                         hb.appendChild(gb); 
132                         gb.setAttribute('id','acn_' + g.volumes[i].id());
133                         gb.setAttribute('style','border: solid black 2px');
134
135                         generate_labels(g.volumes[i], gb, label_cfg, override);
136
137                         idx++;
138                     }
139                 }
140             } catch(E) {
141                 g.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.spine_labels.generate.std_unexpeceted_err'),E);
142             }
143         }
144
145         function generate_labels(volume, label_node, label_cfg, override) {
146             var names;
147             var callnum;
148
149             if (override && volume.id() == override.acn) {
150                 /* If we're calling ourself, we'll have an altered label */
151                 callnum = String(override.label);
152             } else {
153                 /* take the call number and split it on whitespace */
154                 callnum = String(volume.label());
155             }
156
157             /* for LC, split between classification subclass letters and numbers */
158             var lc_class_re = /^([A-Z]{1,3})([0-9]+.*?)$/i;
159             var lc_class_match = lc_class_re.exec(callnum);
160             if (lc_class_match && lc_class_match.length > 1) {
161                 callnum = lc_class_match[1] + ' ' + lc_class_match[2];
162             }
163
164             /* for LC, split between Cutter numbers */
165             var lc_cutter_re = /^(.*?)(\.[A-Z]{1}[0-9]+.*?)$/ig;
166             var lc_cutter_match = lc_cutter_re.exec(callnum);
167             if (lc_cutter_match && lc_cutter_match.length > 1) {
168                 callnum = '';
169                 for (var i = 1; i < lc_cutter_match.length; i++) {
170                     callnum += lc_cutter_match[i] + ' ';
171                 }
172             }
173
174             names = callnum.split(/\s+/);
175             var j = 0;
176             while (j < label_cfg.spine_length || j < label_cfg.pocket_length) {
177                 var hb2 = document.createElement('hbox'); label_node.appendChild(hb2);
178                 
179                 /* spine */
180                 if (j < label_cfg.spine_length) {
181                     var tb = document.createElement('textbox'); hb2.appendChild(tb); 
182                     tb.value = '';
183                     tb.setAttribute('class','plain');
184                     tb.setAttribute('style',
185                         'font-family: ' + label_cfg.font_family
186                         + '; font-size: ' + label_cfg.font_size
187                         + '; font-weight: ' + label_cfg.font_weight
188                     );
189                     tb.setAttribute('size',label_cfg.spine_width+1);
190                     tb.setAttribute('maxlength',label_cfg.spine_width);
191                     tb.setAttribute('name','spine');
192                     var spine_row_id = 'acn_' + volume.id() + '_spine_' + j;
193                     tb.setAttribute('id',spine_row_id);
194
195                     var name = names.shift();
196                     if (name) {
197                         name = String( name );
198
199                         /* if the name is greater than the label width... */
200                         if (name.length > label_cfg.spine_width) {
201                             /* then try to split it on periods */
202                             var sname = name.split(/\./);
203                             if (sname.length > 1) {
204                                 /* if we can, then put the periods back in on each splitted element */
205                                 if (name.match(/^\./)) sname[0] = '.' + sname[0];
206                                 for (var k = 1; k < sname.length; k++) sname[k] = '.' + sname[k];
207                                 /* and put all but the first one back into the names array */
208                                 names = sname.slice(1).concat( names );
209                                 /* if the name fragment is still greater than the label width... */
210                                 if (sname[0].length > label_cfg.spine_width) {
211                                     /* then just truncate and throw the rest back into the names array */
212                                     tb.value = sname[0].substr(0,label_cfg.spine_width);
213                                     names = [ sname[0].substr(label_cfg.spine_width) ].concat( names );
214                                 } else {
215                                     /* otherwise we're set */
216                                     tb.value = sname[0];
217                                 }
218                             } else {
219                                 /* if we can't split on periods, then just truncate and throw the rest back into the names array */
220                                 tb.value = name.substr(0,label_cfg.spine_width);
221                                 names = [ name.substr(label_cfg.spine_width) ].concat( names );
222                             }
223                         } else {
224                             /* otherwise we're set */
225                             tb.value = name;
226                         }
227                     }
228                     dojo.connect($(spine_row_id), 'onkeypress', 'spine_label_key_events');
229                 }
230
231                 /* pocket */
232                 if ($('pl').checked && j < label_cfg.pocket_length) {
233                     var tb2 = document.createElement('textbox'); hb2.appendChild(tb2); 
234                     tb2.value = '';
235                     tb2.setAttribute('class','plain');
236                     tb2.setAttribute('style',
237                         'font-family: ' + label_cfg.font_family
238                         + '; font-size: ' + label_cfg.font_size
239                         + '; font-weight: ' + label_cfg.font_weight
240                     );
241                     tb2.setAttribute('size',label_cfg.pocket_width+1); tb2.setAttribute('maxlength',label_cfg.pocket_width);
242                     tb2.setAttribute('name','pocket');
243                     if ($('title').checked && $('title_line').value == j + 1 && instanceOf(volume.record(),mvr)) {
244                         if (volume.record().title()) {
245                             tb2.value = util.text.wrap_on_space( volume.record().title(), label_cfg.pocket_width )[0];
246                         } else {
247                             tb2.value = '';
248                         }
249                     }
250                     if ($('title_r').checked && $('title_r_line').value == j + 1 && instanceOf(volume.record(),mvr)) {
251                         if (volume.record().title()) {
252                             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);
253                         } else {
254                             tb2.value = '';
255                         }
256                     }
257                     if ($('author').checked && $('author_line').value == j + 1 && instanceOf(volume.record(),mvr)) {
258                         if (volume.record().author()) {
259                             tb2.value = volume.record().author().substr(0,label_cfg.pocket_width);
260                         } else {
261                             tb2.value = '';
262                         }
263                     }
264                     if ($('call_number').checked && $('call_number_line').value == j + 1) {
265                         tb2.value = volume.label().substr(0,label_cfg.pocket_width);
266                     }
267                     if ($('owning_lib_shortname').checked && $('owning_lib_shortname_line').value == j + 1) {
268                         var lib = volume.owning_lib();
269                         if (!instanceOf(lib,aou)) lib = g.data.hash.aou[ lib ];
270                         tb2.value = lib.shortname().substr(0,label_cfg.pocket_width);
271                     }
272                     if ($('owning_lib').checked && $('owning_lib_line').value == j + 1) {
273                         var lib = volume.owning_lib();
274                         if (!instanceOf(lib,aou)) lib = g.data.hash.aou[ lib ];
275                         tb2.value = lib.name().substr(0,label_cfg.pocket_width);
276                     }
277                     if ($('shelving_location').checked && $('shelving_location_line').value == j + 1) {
278                         tb2.value = '%location%';
279                     }
280                     if ($('barcode').checked && $('barcode_line').value == j + 1) {
281                         tb2.value = '%barcode%';
282                     }
283                     if ($('custom1').checked && $('custom1_line').value == j + 1) {
284                         tb2.value = $('custom1_tb').value;
285                     }
286                     if ($('custom2').checked && $('custom2_line').value == j + 1) {
287                         tb2.value = $('custom2_tb').value;
288                     }
289                     if ($('custom3').checked && $('custom3_line').value == j + 1) {
290                         tb2.value = $('custom3_tb').value;
291                     }
292                     if ($('custom4').checked && $('custom4_line').value == j + 1) {
293                         tb2.value = $('custom4_tb').value;
294                     }
295                 }
296
297                 j++;
298             }
299         }
300
301         function spine_label_key_events (event) {
302
303             /* Current value of the inpux box */
304             var line_value = event.target.value;
305
306             /* Cursor positions */
307             var sel_start = event.target.selectionStart;
308             var sel_end = event.target.selectionEnd;
309
310             /* Identifiers for this row: "acn_ID_spine_ROW" */
311             var atts = event.target.id.split('_');
312             var row_id = {
313                 "acn": atts[1],
314                 "spine": atts[3],
315                 "prefix": 'acn_' + atts[1] + '_spine_'
316             };
317
318             switch (event.charOrCode) {
319                 case dojo.keys.ENTER : {
320                     /* Create a new row by inserting a space at the
321                      * current cursor point, then regenerating the
322                      * label
323                      */
324                     if (sel_start == sel_end) {
325                         if (sel_start == 0) {
326                             /* If the cursor is at the start of the line:
327                              * insert new line
328                              */
329                             line_value = ' ' + line_value;
330                         } else if (sel_start == line_value.length) {
331                             /* Special case if the cursor is at the end of the line:
332                              * move to next line
333                              */
334                             var next_row = $(row_id.prefix + (parseInt(row_id.spine) + 1));
335                             if (next_row) {
336                                 next_row.focus();
337                             }
338                             break;
339                         } else {
340                             line_value = line_value.substr(0, sel_start) + ' ' + line_value.substr(sel_end);
341                         }
342                     } else {
343                         line_value = line_value.substr(0, sel_start) + ' ' + line_value.substr(sel_end);
344                     }
345                     event.target.value = line_value;
346
347                     /* Recreate the label */
348                     var new_label = '';
349                     var chunk;
350                     var x = 0;
351                     while (chunk = $(row_id.prefix + x)) {
352                         if (x > 0) {
353                             new_label += ' ' + chunk.value;
354                         } else {
355                             new_label = chunk.value;
356                         }
357                         x++;
358                     }
359                     generate({"acn": row_id.acn, "label": new_label});
360                     $(row_id.prefix + row_id.spine).focus();
361                     break;
362                 }
363
364                 case dojo.keys.BACKSPACE : {
365                     /* Delete line if at the start of an input box */
366                     if (sel_start == 0) {
367                         var new_label = '';
368                         var chunk;
369                         var x = 0;
370                         while (x <= (row_id.spine - 1) && (chunk = $(row_id.prefix + x))) {
371                             if (x > 0) {
372                                 new_label += ' ' + chunk.value;
373                             } else {
374                                 new_label = chunk.value;
375                             }
376                             x++;
377                         }
378
379                         if (chunk = $(row_id.prefix + x)) {
380                             new_label += chunk.value;
381                             x++;
382                         }
383
384                         while (chunk = $(row_id.prefix + x)) {
385                             new_label += ' ' + chunk.value;
386                             x++;
387                         }
388                         generate({"acn": row_id.acn, "label": new_label});
389                         $(row_id.prefix + row_id.spine).focus();
390                     }
391                     break;
392                 }
393
394                 case dojo.keys.DELETE : {
395                     /* Delete line if at the end of an input box */
396                     if (sel_start == event.target.textLength) {
397                         var new_label = '';
398                         var chunk;
399                         var x = 0;
400                         while (x <= row_id.spine && (chunk = $(row_id.prefix + x))) {
401                             if (x > 0) {
402                                 new_label += ' ' + chunk.value;
403                             } else {
404                                 new_label = chunk.value;
405                             }
406                             x++;
407                         }
408
409                         if (chunk = $(row_id.prefix + x)) {
410                             new_label += chunk.value;
411                             x++;
412                         }
413
414                         while (chunk = $(row_id.prefix + x)) {
415                             new_label += ' ' + chunk.value;
416                             x++;
417                         }
418                         generate({"acn": row_id.acn, "label": new_label});
419                         $(row_id.prefix + row_id.spine).focus();
420                     }
421                     break;
422                 }
423
424                 case dojo.keys.UP_ARROW : {
425                     /* Move to the previous row */
426                     var prev_row = $(row_id.prefix + (parseInt(row_id.spine) - 1));
427                     if (prev_row) {
428                         prev_row.focus();
429                     }
430                     break;
431                 }
432
433                 case dojo.keys.DOWN_ARROW : {
434                     /* Move to the next row */
435                     var next_row = $(row_id.prefix + (parseInt(row_id.spine) + 1));
436                     if (next_row) {
437                         next_row.focus();
438                     }
439                     break;
440                 }
441
442                 default : {
443                     break;
444                 }
445             }
446         }
447
448         function expand_macros(text,copy,volume,record) {
449             var my = { 'acp' : copy, 'acn' : volume, 'mvr' : record };
450             var obj = { 'data' : g.data };
451             for (var i in g.col_map) {
452                 var re = g.col_map[i].regex;
453                 if (text.match(re)) {
454                     try {
455                         text = text.replace(re, (typeof g.col_map[i].render == 'function' ? g.col_map[i].render(my) : eval( g.col_map[i].render ) ) );
456                     } catch(E) {
457                         g.error.sdump('D_ERROR','spine_labels.js, expand_macros() = ' + E);
458                     }
459                 }
460             }
461             return text;
462         }
463
464         function preview(idx) {
465             try {
466                     netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
467                     var pt = Number( $('pt').value );  /* font size */
468                     if (!pt) {
469                         pt = g.data.hash.aous['cat.spine.font.size'] || 10;
470                         $('pt').value = pt;
471                     }
472                     var ff = g.data.hash.aous['cat.spine.font.family'] || 'monospace';
473                     var fw = $('font_weight').value;  /* font weight */
474                     if (!fw) {
475                         fw = g.data.hash.aous['cat.label.font.weight'] || 'normal';
476                     }
477                     var lm = Number($('lm').value); /* left margin */
478                     if (!lm) {
479                         lm = g.data.hash.aous['cat.spine.line.margin'] || 11;
480                     }
481                     var mm = Number($('mm').value); if (mm == NaN) mm = 2; /* middle margin */
482                     var lw = Number($('lw').value); /* spine label width */
483                     if (!lw) {
484                         lw = g.data.hash.aous['cat.spine.line.width'] || 8;
485                         $('lw').value = lw;
486                     }
487                     var ll = Number($('ll').value); /* spine label length */
488                     if (!ll) {
489                         ll = g.data.hash.aous['cat.spine.line.height'] || 9;
490                         $('ll').value = ll;
491                     }
492                     var plw = Number($('plw').value) || 28; var pll = Number($('pll').value) || 9; /* pocket label width and length */
493                     var html = "<html><head>";
494                     html += "<link type='text/css' rel='stylesheet' href='" + xulG.url_prefix('/xul/server/skin/print.css') + "'></link>"
495                     html += "<link type='text/css' rel='stylesheet' href='data:text/css,pre{font-family:" + ff + ";font-size:" + pt + "pt; font-weight: " + fw + ";}'></link>";
496                     html += "<title>Spine Labels</title></head><body>\n";
497                     var nl = document.getElementsByAttribute('name','template');
498                     for (var i = 0; i < nl.length; i++) {
499                         if (typeof idx == 'undefined' || idx == null) { } else {
500                             if (idx != i) continue;
501                         }
502                         var volume = g.volumes[ nl[i].getAttribute('acn_id') ];
503
504                         for (var j = 0; j < volume.copies().length; j++) {
505                             var copy = volume.copies()[j];
506                             if (i == 0 && j == 0) {
507                                 html += '<pre class="first_pre">\n';
508                             } else {
509                                 html += '<pre class="not_first_pre">\n';
510                             }
511                             var gb = nl[i].getElementsByTagName('groupbox')[0];
512                             var nl2 = gb.getElementsByAttribute('name','spine');
513                             for (var k = 0; k < nl2.length; k++) {
514                                 for (var m = 0; m < lm; m++) html += ' ';
515                                 html += util.text.preserve_string_in_html(expand_macros( nl2[k].value, copy, volume, volume.record() ).substr(0,lw));
516                                 if ($('pl').checked) {
517                                     var sib = nl2[k].nextSibling;
518                                     if (sib) {
519                                         for (var m = 0; m < lw - nl2[k].value.length; m++) html += ' ';
520                                         for (var m = 0; m < mm; m++) html += ' ';
521                                         html += util.text.preserve_string_in_html(expand_macros( sib.value, copy, volume, volume.record() ).substr(0,plw));
522                                     }
523                                 }
524                                 html += '\n';
525                             }
526                             html += '</pre hex="0C">\n';
527                         }
528                     }
529                     html += '</body></html>';
530                     var loc = ( urls.XUL_BROWSER );
531                     xulG.new_tab(
532                         loc,
533                         {
534                             'tab_name' : $("catStrings").getString('staff.cat.spine_labels.preview.title')
535                         },
536                         { 
537                             'url' : 'data:text/html;charset=utf-8,'+window.escape(html),
538                             'html_source' : html,
539                             'show_print_button' : 1,
540                             'no_xulG' : 1
541                         }
542                     );
543             } catch(E) {
544                 g.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.spine_labels.preview.std_unexpected_err'),E);
545             }
546         }
547
548