3 if (typeof JSAN == 'undefined') { throw( $("commonStrings").getString('common.jsan.missing') ); }
4 JSAN.errorLevel = "die"; // none, warn, or die
5 JSAN.addRepository('/xul/server/');
6 JSAN.use('util.error'); g.error = new util.error();
7 g.error.sdump('D_TRACE','my_init() for spine_labels.xul');
9 JSAN.use('util.network'); g.network = new util.network();
14 if (g.cgi.param('barcodes')) {
15 g.barcodes = g.barcodes.concat( JSON2js(g.cgi.param('barcodes')) );
17 JSAN.use('OpenILS.data'); g.data = new OpenILS.data(); g.data.stash_retrieve();
18 if (g.data.temp_barcodes_for_labels) {
19 g.barcodes = g.barcodes.concat( g.data.temp_barcodes_for_labels );
20 g.data.temp_barcodes_for_labels = null; g.data.stash('temp_barcodes_for_labels');
23 g.barcodes = g.barcodes.concat( xulG.barcodes );
26 JSAN.use('circ.util');
27 g.cols = circ.util.columns( {} );
29 for (var i = 0; i < g.cols.length; i++) {
30 g.col_map[ g.cols[i].id ] = { 'regex' : new RegExp('%' + g.cols[i].id + '%',"g"), 'render' : g.cols[i].render };
35 for (var i = 0; i < g.barcodes.length; i++) {
36 var copy = g.network.simple_request( 'FM_ACP_RETRIEVE_VIA_BARCODE.authoritative', [ g.barcodes[i] ] );
37 if (typeof copy.ilsevent != 'undefined') throw(copy);
38 var label_prefix = copy.location().label_prefix() || '';
39 var label_suffix = copy.location().label_suffix() || '';
40 if (!g.volumes[ copy.call_number() ]) {
41 var volume = g.network.simple_request( 'FM_ACN_RETRIEVE.authoritative', [ copy.call_number() ] );
42 if (typeof volume.ilsevent != 'undefined') throw(volume);
43 var record = g.network.simple_request('MODS_SLIM_RECORD_RETRIEVE.authoritative', [ volume.record() ]);
44 volume.record( record );
46 /* The volume object has native prefix and suffixes now, so affix the ones coming from copy locations */
47 var temp_prefix = label_prefix + ' ' + (typeof volume.prefix() == 'object' ? volume.prefix().label() : volume.prefix());
48 var temp_suffix = (typeof volume.suffix() == 'object' ? volume.suffix().label() : volume.suffix()) + ' ' + label_suffix;
50 /* And assume that leading and trailing spaces can be trimmed */
51 temp_prefix = temp_prefix.replace(/\s+$/,'').replace(/^\s+/,'');
52 temp_suffix = temp_suffix.replace(/\s+$/,'').replace(/^\s+/,'');
54 volume.prefix( temp_prefix );
55 volume.suffix( temp_suffix );
57 g.volumes[ volume.id() ] = volume;
59 if (g.volumes[ copy.call_number() ].copies()) {
60 var copies = g.volumes[ copy.call_number() ].copies();
62 g.volumes[ copy.call_number() ].copies( copies );
64 g.volumes[ copy.call_number() ].copies( [ copy ] );
70 if (typeof xulG != 'undefined') $('close').hidden = true;
74 g.error.standard_unexpected_error_alert('/xul/server/cat/spine_labels.xul',E);
76 alert('FIXME: ' + js2JSON(E));
81 function show_macros() {
82 JSAN.use('util.functional');
83 alert( util.functional.map_list( g.cols, function(o) { return '%' + o.id + '%'; } ).join(" ") );
86 function $(id) { return dojo.byId(id); }
88 function generate(override) {
91 JSAN.use('util.text');
92 JSAN.use('util.money');
93 JSAN.use('util.widgets');
95 $('preview').disabled = false;
97 /* Grab from OU settings, then fall back to hardcoded defaults */
99 label_cfg.spine_width = Number($('lw').value); /* spine label width */
100 if (!label_cfg.spine_width) {
101 label_cfg.spine_width = g.data.hash.aous['cat.spine.line.width'] || 8;
102 $('lw').value = label_cfg.spine_width;
104 label_cfg.spine_length = Number($('ll').value); /* spine label length */
105 if (!label_cfg.spine_length) {
106 label_cfg.spine_length = g.data.hash.aous['cat.spine.line.height'] || 9;
107 $('ll').value = label_cfg.spine_length;
109 label_cfg.spine_left_margin = Number($('lm').value); /* left margin */
110 if (!label_cfg.spine_left_margin) {
111 label_cfg.spine_left_margin = g.data.hash.aous['cat.spine.line.margin'] || 0;
112 $('lm').value = label_cfg.spine_left_margin;
114 label_cfg.font_size = Number( $('pt').value ); /* font size */
115 if (!label_cfg.font_size) {
116 label_cfg.font_size = g.data.hash.aous['cat.label.font.size'] || 10;
117 $('pt').value = label_cfg.font_size;
119 label_cfg.font_weight = $('font_weight').value; /* font weight */
120 if (!label_cfg.font_weight) {
121 label_cfg.font_weight = g.data.hash.aous['cat.label.font.weight'] || 'normal';
122 $('font_weight').value = label_cfg.font_weight;
124 label_cfg.font_family = g.data.hash.aous['cat.label.font.family'] || 'monospace';
125 label_cfg.pocket_width = Number($('plw').value) || 28; /* pocket label width */
126 label_cfg.pocket_length = Number($('pll').value) || 9; /* pocket label length */
129 var gb = $('acn_' + g.volumes[override.acn].id());
130 util.widgets.remove_children('acn_' + g.volumes[override.acn].id());
131 generate_labels(g.volumes[override.acn], gb, label_cfg, override);
133 util.widgets.remove_children('panel');
134 for (var i in g.volumes) {
135 var vb = document.createElement('vbox'); pn.appendChild(vb);
136 vb.setAttribute('name','template');
137 vb.setAttribute('acn_id',g.volumes[i].id());
138 var ds = document.createElement('description'); vb.appendChild(ds);
139 ds.appendChild( document.createTextNode( g.volumes[i].label() ) );
140 var ds2 = document.createElement('description'); vb.appendChild(ds2);
141 ds2.appendChild( document.createTextNode( g.volumes[i].copies().length + ' ' + (
142 g.volumes[i].copies().length == 1 ? $("catStrings").getString('staff.cat.spine_labels.copy') : $("catStrings").getString('staff.cat.spine_labels.copies')) ) );
143 ds2.setAttribute('style','color: green');
144 var hb = document.createElement('hbox'); vb.appendChild(hb);
146 var gb = document.createElement('groupbox');
148 gb.setAttribute('id','acn_' + g.volumes[i].id());
149 gb.setAttribute('style','border: solid black 2px');
151 generate_labels(g.volumes[i], gb, label_cfg, override);
157 g.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.spine_labels.generate.std_unexpeceted_err'),E);
161 function generate_labels(volume, label_node, label_cfg, override) {
165 if (override && volume.id() == override.acn) {
166 /* If we're calling ourself, we'll have an altered label */
167 callnum = String(override.label);
169 /* take the call number and split it on whitespace */
170 callnum = String(volume.label());
172 /* handle spine labels differently if using LC */
173 var lab_class = volume.label_class();
174 if (lab_class.id() == 3) {
175 /* Establish a pattern where every return value should be isolated on its own line
176 on the spine label: subclass letters, subclass numbers, cutter numbers, trailing stuff (date) */
177 var patt1 = /^([A-Z]{1,3})\s*(\d+(?:\.\d+)?)\s*(\.[A-Z]\d*)\s*([A-Z]\d*)?\s*(\d\d\d\d(?:-\d\d\d\d)?)?\s*(.*)$/i;
178 var result = callnum.match(patt1);
180 callnum = result.slice(1).join('\t');
182 callnum = callnum.split(/\s+/).join('\t');
185 /* If result is null, leave callnum alone. Can't parse this malformed call num */
187 callnum = callnum.split(/\s+/).join('\t');
190 /* Only add the prefixes and suffixes once */
191 if (!override || volume.id() != override.acn) {
192 if (volume.prefix()) {
193 callnum = volume.prefix() + '\t' + callnum;
195 if (volume.suffix()) {
196 callnum += '\t' + volume.suffix();
200 /* At this point, the call number pieces are separated by tab characters. This allows
201 * some space-containing constructs like "v. 1" to appear on one line
203 callnum = callnum.replace(/\t\t/g,'\t'); /* Squeeze out empties */
204 names = callnum.split('\t');
206 while (j < label_cfg.spine_length || j < label_cfg.pocket_length) {
207 var hb2 = document.createElement('hbox'); label_node.appendChild(hb2);
210 if (j < label_cfg.spine_length) {
211 var tb = document.createElement('textbox'); hb2.appendChild(tb);
213 tb.setAttribute('class','plain');
214 tb.setAttribute('style',
215 'font-family: ' + label_cfg.font_family
216 + '; font-size: ' + label_cfg.font_size
217 + '; font-weight: ' + label_cfg.font_weight
219 tb.setAttribute('size',label_cfg.spine_width+1);
220 tb.setAttribute('maxlength',label_cfg.spine_width);
221 tb.setAttribute('name','spine');
222 var spine_row_id = 'acn_' + volume.id() + '_spine_' + j;
223 tb.setAttribute('id',spine_row_id);
225 var name = names.shift();
227 name = String( name );
229 /* if the name is greater than the label width... */
230 if (name.length > label_cfg.spine_width) {
231 /* then try to split it on periods */
232 var sname = name.split(/\./);
233 if (sname.length > 1) {
234 /* if we can, then put the periods back in on each splitted element */
235 if (name.match(/^\./)) sname[0] = '.' + sname[0];
236 for (var k = 1; k < sname.length; k++) sname[k] = '.' + sname[k];
237 /* and put all but the first one back into the names array */
238 names = sname.slice(1).concat( names );
239 /* if the name fragment is still greater than the label width... */
240 if (sname[0].length > label_cfg.spine_width) {
241 /* then just truncate and throw the rest back into the names array */
242 tb.value = sname[0].substr(0,label_cfg.spine_width);
243 names = [ sname[0].substr(label_cfg.spine_width) ].concat( names );
245 /* otherwise we're set */
249 /* if we can't split on periods, then just truncate and throw the rest back into the names array */
250 tb.value = name.substr(0,label_cfg.spine_width);
251 names = [ name.substr(label_cfg.spine_width) ].concat( names );
254 /* otherwise we're set */
258 dojo.connect($(spine_row_id), 'onkeypress', 'spine_label_key_events');
262 if ($('pl').checked && j < label_cfg.pocket_length) {
263 var tb2 = document.createElement('textbox'); hb2.appendChild(tb2);
265 tb2.setAttribute('class','plain');
266 tb2.setAttribute('style',
267 'font-family: ' + label_cfg.font_family
268 + '; font-size: ' + label_cfg.font_size
269 + '; font-weight: ' + label_cfg.font_weight
271 tb2.setAttribute('size',label_cfg.pocket_width+1); tb2.setAttribute('maxlength',label_cfg.pocket_width);
272 tb2.setAttribute('name','pocket');
273 if ($('title').checked && $('title_line').value == j + 1 && instanceOf(volume.record(),mvr)) {
274 if (volume.record().title()) {
275 tb2.value = util.text.wrap_on_space( volume.record().title(), label_cfg.pocket_width )[0];
280 if ($('title_r').checked && $('title_r_line').value == j + 1 && instanceOf(volume.record(),mvr)) {
281 if (volume.record().title()) {
282 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);
287 if ($('author').checked && $('author_line').value == j + 1 && instanceOf(volume.record(),mvr)) {
288 if (volume.record().author()) {
289 tb2.value = volume.record().author().substr(0,label_cfg.pocket_width);
294 if ($('call_number').checked && $('call_number_line').value == j + 1) {
296 (volume.prefix() + ' ' + volume.label() + ' ' + volume.suffix())
299 .substr(0,label_cfg.pocket_width)
302 if ($('owning_lib_shortname').checked && $('owning_lib_shortname_line').value == j + 1) {
303 var lib = volume.owning_lib();
304 if (!instanceOf(lib,aou)) lib = g.data.hash.aou[ lib ];
305 tb2.value = lib.shortname().substr(0,label_cfg.pocket_width);
307 if ($('owning_lib').checked && $('owning_lib_line').value == j + 1) {
308 var lib = volume.owning_lib();
309 if (!instanceOf(lib,aou)) lib = g.data.hash.aou[ lib ];
310 tb2.value = lib.name().substr(0,label_cfg.pocket_width);
312 if ($('shelving_location').checked && $('shelving_location_line').value == j + 1) {
313 tb2.value = '%location%';
315 if ($('barcode').checked && $('barcode_line').value == j + 1) {
316 tb2.value = '%barcode%';
318 if ($('custom1').checked && $('custom1_line').value == j + 1) {
319 tb2.value = $('custom1_tb').value;
321 if ($('custom2').checked && $('custom2_line').value == j + 1) {
322 tb2.value = $('custom2_tb').value;
324 if ($('custom3').checked && $('custom3_line').value == j + 1) {
325 tb2.value = $('custom3_tb').value;
327 if ($('custom4').checked && $('custom4_line').value == j + 1) {
328 tb2.value = $('custom4_tb').value;
336 function spine_label_key_events (event) {
338 /* Current value of the inpux box */
339 var line_value = event.target.value;
341 /* Cursor positions */
342 var sel_start = event.target.selectionStart;
343 var sel_end = event.target.selectionEnd;
345 /* Identifiers for this row: "acn_ID_spine_ROW" */
346 var atts = event.target.id.split('_');
350 "prefix": 'acn_' + atts[1] + '_spine_'
353 switch (event.charOrCode) {
354 case dojo.keys.ENTER : {
355 /* Create a new row by inserting a space at the
356 * current cursor point, then regenerating the
359 if (sel_start == sel_end) {
360 if (sel_start == 0) {
361 /* If the cursor is at the start of the line:
364 line_value = ' ' + line_value;
365 } else if (sel_start == line_value.length) {
366 /* Special case if the cursor is at the end of the line:
369 var next_row = $(row_id.prefix + (parseInt(row_id.spine) + 1));
375 line_value = line_value.substr(0, sel_start) + ' ' + line_value.substr(sel_end);
378 line_value = line_value.substr(0, sel_start) + ' ' + line_value.substr(sel_end);
380 event.target.value = line_value;
382 /* Recreate the label */
386 while (chunk = $(row_id.prefix + x)) {
388 new_label += ' ' + chunk.value;
390 new_label = chunk.value;
394 generate({"acn": row_id.acn, "label": new_label});
395 $(row_id.prefix + row_id.spine).focus();
399 case dojo.keys.BACKSPACE : {
400 /* Delete line if at the start of an input box */
401 if (sel_start == 0 && sel_end == sel_start) {
405 while (x <= (row_id.spine - 1) && (chunk = $(row_id.prefix + x))) {
407 new_label += ' ' + chunk.value;
409 new_label = chunk.value;
414 if (chunk = $(row_id.prefix + x)) {
415 new_label += chunk.value;
419 while (chunk = $(row_id.prefix + x)) {
420 new_label += ' ' + chunk.value;
423 generate({"acn": row_id.acn, "label": new_label});
424 $(row_id.prefix + row_id.spine).focus();
426 if (sel_start == 0) {
427 /* Move to the previous row */
428 var prev_row = $(row_id.prefix + (parseInt(row_id.spine) - 1));
436 case dojo.keys.DELETE : {
437 /* Delete line if at the end of an input box */
438 if (sel_start == event.target.textLength) {
442 while (x <= row_id.spine && (chunk = $(row_id.prefix + x))) {
444 new_label += ' ' + chunk.value;
446 new_label = chunk.value;
451 if (chunk = $(row_id.prefix + x)) {
452 new_label += chunk.value;
456 while (chunk = $(row_id.prefix + x)) {
457 new_label += ' ' + chunk.value;
460 generate({"acn": row_id.acn, "label": new_label});
461 $(row_id.prefix + row_id.spine).focus();
466 case dojo.keys.UP_ARROW : {
467 /* Move to the previous row */
468 var prev_row = $(row_id.prefix + (parseInt(row_id.spine) - 1));
475 case dojo.keys.DOWN_ARROW : {
476 /* Move to the next row */
477 var next_row = $(row_id.prefix + (parseInt(row_id.spine) + 1));
490 function expand_macros(text,copy,volume,record) {
491 var my = { 'acp' : copy, 'acn' : volume, 'mvr' : record };
492 var obj = { 'data' : g.data };
493 for (var i in g.col_map) {
494 var re = g.col_map[i].regex;
495 if (text.match(re)) {
497 text = text.replace(re, (typeof g.col_map[i].render == 'function' ? g.col_map[i].render(my) : eval( g.col_map[i].render ) ) );
499 g.error.sdump('D_ERROR','spine_labels.js, expand_macros() = ' + E);
506 function preview(idx) {
508 var pt = Number( $('pt').value ); /* font size */
510 pt = g.data.hash.aous['cat.label.font.size'] || 10;
513 var ff = g.data.hash.aous['cat.label.font.family'] || 'monospace';
514 var fw = $('font_weight').value; /* font weight */
516 fw = g.data.hash.aous['cat.label.font.weight'] || 'normal';
518 var lm = Number($('lm').value); /* left margin */
520 lm = g.data.hash.aous['cat.spine.line.margin'] || 0;
522 var mm = Number($('mm').value); if (isNaN(mm)) mm = 2; /* middle margin */
523 var lw = Number($('lw').value); /* spine label width */
525 lw = g.data.hash.aous['cat.spine.line.width'] || 8;
528 var ll = Number($('ll').value); /* spine label length */
530 ll = g.data.hash.aous['cat.spine.line.height'] || 9;
533 var plw = Number($('plw').value) || 28; var pll = Number($('pll').value) || 9; /* pocket label width and length */
534 var html = "<html><head>";
535 html += "<link type='text/css' rel='stylesheet' href='" + xulG.url_prefix('/xul/server/skin/print.css') + "'></link>"
536 html += "<link type='text/css' rel='stylesheet' href='data:text/css,pre{font-family:" + ff + ";font-size:" + pt + "pt; font-weight: " + fw + ";}'></link>";
537 html += "<title>Spine Labels</title></head><body>\n";
538 var nl = document.getElementsByAttribute('name','template');
539 for (var i = 0; i < nl.length; i++) {
540 if (typeof idx == 'undefined' || idx == null) { } else {
541 if (idx != i) continue;
543 var volume = g.volumes[ nl[i].getAttribute('acn_id') ];
545 for (var j = 0; j < volume.copies().length; j++) {
546 var copy = volume.copies()[j];
547 if (i == 0 && j == 0) {
548 html += '<pre class="first_pre">\n';
550 html += '<pre class="not_first_pre">\n';
552 var gb = nl[i].getElementsByTagName('groupbox')[0];
553 var nl2 = gb.getElementsByAttribute('name','spine');
554 for (var k = 0; k < nl2.length; k++) {
555 for (var m = 0; m < lm; m++) html += ' ';
556 html += util.text.preserve_string_in_html(expand_macros( nl2[k].value, copy, volume, volume.record() ).substr(0,lw));
557 if ($('pl').checked) {
558 var sib = nl2[k].nextSibling;
560 for (var m = 0; m < lw - nl2[k].value.length; m++) html += ' ';
561 for (var m = 0; m < mm; m++) html += ' ';
562 html += util.text.preserve_string_in_html(expand_macros( sib.value, copy, volume, volume.record() ).substr(0,plw));
567 html += '</pre hex="0C">\n';
570 html += '</body></html>';
572 /* From https://developer.mozilla.org/en/Using_nsIXULAppInfo */
573 var appInfo = Components.classes["@mozilla.org/xre/app-info;1"]
574 .getService(Components.interfaces.nsIXULAppInfo);
575 var platformVer = appInfo.platformVersion;
577 /* We need to use different print strategies for different
578 * XUL versions, apparently
580 if (platformVer.substr(0, 5) == '1.9.0') {
581 preview_xul_190(html);
583 preview_xul_192(html);
588 g.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.spine_labels.preview.std_unexpected_err'),E);
592 function preview_xul_190(html) {
593 JSAN.use('util.window'); var win = new util.window();
594 var loc = ( urls.XUL_REMOTE_BROWSER );
595 var w = win.open( loc, 'spine_preview', 'chrome,resizable,width=750,height=550');
597 'url' : 'about:blank',
598 'url_prefix' : function (u,s) { return xulG.url_prefix(u,s); },
599 'show_print_button' : 1,
600 'printer_context' : 'label',
601 'alternate_print' : 1,
603 'title' : $("catStrings").getString('staff.cat.spine_labels.preview.title'),
604 'on_url_load' : function(b) {
606 if (typeof w.xulG.written == 'undefined') {
607 w.xulG.written = true;
608 w.g.browser.get_content().document.write(html);
609 w.g.browser.get_content().document.close();
618 function preview_xul_192(html) {
619 var loc = ( urls.XUL_BROWSER );
623 'tab_name' : $("catStrings").getString('staff.cat.spine_labels.preview.title')
626 'url' : 'data:text/html;charset=utf-8,' + encodeURIComponent(html),
627 'html_source' : html,
628 'show_print_button' : 1,
629 'printer_context' : 'label',