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