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 if (volume.label_class() == 3) {
174 /* for LC, split between classification subclass letters and numbers */
175 var lc_class_re = /^([A-Z]{1,3})([0-9]+.*?)$/i;
176 var lc_class_match = lc_class_re.exec(callnum);
177 if (lc_class_match && lc_class_match.length > 1) {
178 callnum = lc_class_match[1] + ' ' + lc_class_match[2];
181 /* for LC, split between Cutter numbers */
182 var lc_cutter_re = /^(.*)(\.[A-Z]{1}[0-9]+.*?)$/ig;
183 var lc_cutter_match = lc_cutter_re.exec(callnum);
184 if (lc_cutter_match && lc_cutter_match.length > 1) {
186 for (var i = 1; i < lc_cutter_match.length; i++) {
187 callnum += lc_cutter_match[i] + ' ';
192 /* Only add the prefixes and suffixes once */
193 if (!override || volume.id() != override.acn) {
194 if (volume.prefix()) {
195 callnum = volume.prefix() + ' ' + callnum;
197 if (volume.suffix()) {
198 callnum += ' ' + volume.suffix();
202 names = callnum.split(/\s+/);
204 while (j < label_cfg.spine_length || j < label_cfg.pocket_length) {
205 var hb2 = document.createElement('hbox'); label_node.appendChild(hb2);
208 if (j < label_cfg.spine_length) {
209 var tb = document.createElement('textbox'); hb2.appendChild(tb);
211 tb.setAttribute('class','plain');
212 tb.setAttribute('style',
213 'font-family: ' + label_cfg.font_family
214 + '; font-size: ' + label_cfg.font_size
215 + '; font-weight: ' + label_cfg.font_weight
217 tb.setAttribute('size',label_cfg.spine_width+1);
218 tb.setAttribute('maxlength',label_cfg.spine_width);
219 tb.setAttribute('name','spine');
220 var spine_row_id = 'acn_' + volume.id() + '_spine_' + j;
221 tb.setAttribute('id',spine_row_id);
223 var name = names.shift();
225 name = String( name );
227 /* if the name is greater than the label width... */
228 if (name.length > label_cfg.spine_width) {
229 /* then try to split it on periods */
230 var sname = name.split(/\./);
231 if (sname.length > 1) {
232 /* if we can, then put the periods back in on each splitted element */
233 if (name.match(/^\./)) sname[0] = '.' + sname[0];
234 for (var k = 1; k < sname.length; k++) sname[k] = '.' + sname[k];
235 /* and put all but the first one back into the names array */
236 names = sname.slice(1).concat( names );
237 /* if the name fragment is still greater than the label width... */
238 if (sname[0].length > label_cfg.spine_width) {
239 /* then just truncate and throw the rest back into the names array */
240 tb.value = sname[0].substr(0,label_cfg.spine_width);
241 names = [ sname[0].substr(label_cfg.spine_width) ].concat( names );
243 /* otherwise we're set */
247 /* if we can't split on periods, then just truncate and throw the rest back into the names array */
248 tb.value = name.substr(0,label_cfg.spine_width);
249 names = [ name.substr(label_cfg.spine_width) ].concat( names );
252 /* otherwise we're set */
256 dojo.connect($(spine_row_id), 'onkeypress', 'spine_label_key_events');
260 if ($('pl').checked && j < label_cfg.pocket_length) {
261 var tb2 = document.createElement('textbox'); hb2.appendChild(tb2);
263 tb2.setAttribute('class','plain');
264 tb2.setAttribute('style',
265 'font-family: ' + label_cfg.font_family
266 + '; font-size: ' + label_cfg.font_size
267 + '; font-weight: ' + label_cfg.font_weight
269 tb2.setAttribute('size',label_cfg.pocket_width+1); tb2.setAttribute('maxlength',label_cfg.pocket_width);
270 tb2.setAttribute('name','pocket');
271 if ($('title').checked && $('title_line').value == j + 1 && instanceOf(volume.record(),mvr)) {
272 if (volume.record().title()) {
273 tb2.value = util.text.wrap_on_space( volume.record().title(), label_cfg.pocket_width )[0];
278 if ($('title_r').checked && $('title_r_line').value == j + 1 && instanceOf(volume.record(),mvr)) {
279 if (volume.record().title()) {
280 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);
285 if ($('author').checked && $('author_line').value == j + 1 && instanceOf(volume.record(),mvr)) {
286 if (volume.record().author()) {
287 tb2.value = volume.record().author().substr(0,label_cfg.pocket_width);
292 if ($('call_number').checked && $('call_number_line').value == j + 1) {
294 (volume.prefix() + ' ' + volume.label() + ' ' + volume.suffix())
297 .substr(0,label_cfg.pocket_width)
300 if ($('owning_lib_shortname').checked && $('owning_lib_shortname_line').value == j + 1) {
301 var lib = volume.owning_lib();
302 if (!instanceOf(lib,aou)) lib = g.data.hash.aou[ lib ];
303 tb2.value = lib.shortname().substr(0,label_cfg.pocket_width);
305 if ($('owning_lib').checked && $('owning_lib_line').value == j + 1) {
306 var lib = volume.owning_lib();
307 if (!instanceOf(lib,aou)) lib = g.data.hash.aou[ lib ];
308 tb2.value = lib.name().substr(0,label_cfg.pocket_width);
310 if ($('shelving_location').checked && $('shelving_location_line').value == j + 1) {
311 tb2.value = '%location%';
313 if ($('barcode').checked && $('barcode_line').value == j + 1) {
314 tb2.value = '%barcode%';
316 if ($('custom1').checked && $('custom1_line').value == j + 1) {
317 tb2.value = $('custom1_tb').value;
319 if ($('custom2').checked && $('custom2_line').value == j + 1) {
320 tb2.value = $('custom2_tb').value;
322 if ($('custom3').checked && $('custom3_line').value == j + 1) {
323 tb2.value = $('custom3_tb').value;
325 if ($('custom4').checked && $('custom4_line').value == j + 1) {
326 tb2.value = $('custom4_tb').value;
334 function spine_label_key_events (event) {
336 /* Current value of the inpux box */
337 var line_value = event.target.value;
339 /* Cursor positions */
340 var sel_start = event.target.selectionStart;
341 var sel_end = event.target.selectionEnd;
343 /* Identifiers for this row: "acn_ID_spine_ROW" */
344 var atts = event.target.id.split('_');
348 "prefix": 'acn_' + atts[1] + '_spine_'
351 switch (event.charOrCode) {
352 case dojo.keys.ENTER : {
353 /* Create a new row by inserting a space at the
354 * current cursor point, then regenerating the
357 if (sel_start == sel_end) {
358 if (sel_start == 0) {
359 /* If the cursor is at the start of the line:
362 line_value = ' ' + line_value;
363 } else if (sel_start == line_value.length) {
364 /* Special case if the cursor is at the end of the line:
367 var next_row = $(row_id.prefix + (parseInt(row_id.spine) + 1));
373 line_value = line_value.substr(0, sel_start) + ' ' + line_value.substr(sel_end);
376 line_value = line_value.substr(0, sel_start) + ' ' + line_value.substr(sel_end);
378 event.target.value = line_value;
380 /* Recreate the label */
384 while (chunk = $(row_id.prefix + x)) {
386 new_label += ' ' + chunk.value;
388 new_label = chunk.value;
392 generate({"acn": row_id.acn, "label": new_label});
393 $(row_id.prefix + row_id.spine).focus();
397 case dojo.keys.BACKSPACE : {
398 /* Delete line if at the start of an input box */
399 if (sel_start == 0 && sel_end == sel_start) {
403 while (x <= (row_id.spine - 1) && (chunk = $(row_id.prefix + x))) {
405 new_label += ' ' + chunk.value;
407 new_label = chunk.value;
412 if (chunk = $(row_id.prefix + x)) {
413 new_label += chunk.value;
417 while (chunk = $(row_id.prefix + x)) {
418 new_label += ' ' + chunk.value;
421 generate({"acn": row_id.acn, "label": new_label});
422 $(row_id.prefix + row_id.spine).focus();
424 if (sel_start == 0) {
425 /* Move to the previous row */
426 var prev_row = $(row_id.prefix + (parseInt(row_id.spine) - 1));
434 case dojo.keys.DELETE : {
435 /* Delete line if at the end of an input box */
436 if (sel_start == event.target.textLength) {
440 while (x <= row_id.spine && (chunk = $(row_id.prefix + x))) {
442 new_label += ' ' + chunk.value;
444 new_label = chunk.value;
449 if (chunk = $(row_id.prefix + x)) {
450 new_label += chunk.value;
454 while (chunk = $(row_id.prefix + x)) {
455 new_label += ' ' + chunk.value;
458 generate({"acn": row_id.acn, "label": new_label});
459 $(row_id.prefix + row_id.spine).focus();
464 case dojo.keys.UP_ARROW : {
465 /* Move to the previous row */
466 var prev_row = $(row_id.prefix + (parseInt(row_id.spine) - 1));
473 case dojo.keys.DOWN_ARROW : {
474 /* Move to the next row */
475 var next_row = $(row_id.prefix + (parseInt(row_id.spine) + 1));
488 function expand_macros(text,copy,volume,record) {
489 var my = { 'acp' : copy, 'acn' : volume, 'mvr' : record };
490 var obj = { 'data' : g.data };
491 for (var i in g.col_map) {
492 var re = g.col_map[i].regex;
493 if (text.match(re)) {
495 text = text.replace(re, (typeof g.col_map[i].render == 'function' ? g.col_map[i].render(my) : eval( g.col_map[i].render ) ) );
497 g.error.sdump('D_ERROR','spine_labels.js, expand_macros() = ' + E);
504 function preview(idx) {
506 var pt = Number( $('pt').value ); /* font size */
508 pt = g.data.hash.aous['cat.label.font.size'] || 10;
511 var ff = g.data.hash.aous['cat.label.font.family'] || 'monospace';
512 var fw = $('font_weight').value; /* font weight */
514 fw = g.data.hash.aous['cat.label.font.weight'] || 'normal';
516 var lm = Number($('lm').value); /* left margin */
518 lm = g.data.hash.aous['cat.spine.line.margin'] || 0;
520 var mm = Number($('mm').value); if (isNaN(mm)) mm = 2; /* middle margin */
521 var lw = Number($('lw').value); /* spine label width */
523 lw = g.data.hash.aous['cat.spine.line.width'] || 8;
526 var ll = Number($('ll').value); /* spine label length */
528 ll = g.data.hash.aous['cat.spine.line.height'] || 9;
531 var plw = Number($('plw').value) || 28; var pll = Number($('pll').value) || 9; /* pocket label width and length */
532 var html = "<html><head>";
533 html += "<link type='text/css' rel='stylesheet' href='" + xulG.url_prefix('/xul/server/skin/print.css') + "'></link>"
534 html += "<link type='text/css' rel='stylesheet' href='data:text/css,pre{font-family:" + ff + ";font-size:" + pt + "pt; font-weight: " + fw + ";}'></link>";
535 html += "<title>Spine Labels</title></head><body>\n";
536 var nl = document.getElementsByAttribute('name','template');
537 for (var i = 0; i < nl.length; i++) {
538 if (typeof idx == 'undefined' || idx == null) { } else {
539 if (idx != i) continue;
541 var volume = g.volumes[ nl[i].getAttribute('acn_id') ];
543 for (var j = 0; j < volume.copies().length; j++) {
544 var copy = volume.copies()[j];
545 if (i == 0 && j == 0) {
546 html += '<pre class="first_pre">\n';
548 html += '<pre class="not_first_pre">\n';
550 var gb = nl[i].getElementsByTagName('groupbox')[0];
551 var nl2 = gb.getElementsByAttribute('name','spine');
552 for (var k = 0; k < nl2.length; k++) {
553 for (var m = 0; m < lm; m++) html += ' ';
554 html += util.text.preserve_string_in_html(expand_macros( nl2[k].value, copy, volume, volume.record() ).substr(0,lw));
555 if ($('pl').checked) {
556 var sib = nl2[k].nextSibling;
558 for (var m = 0; m < lw - nl2[k].value.length; m++) html += ' ';
559 for (var m = 0; m < mm; m++) html += ' ';
560 html += util.text.preserve_string_in_html(expand_macros( sib.value, copy, volume, volume.record() ).substr(0,plw));
565 html += '</pre hex="0C">\n';
568 html += '</body></html>';
570 /* From https://developer.mozilla.org/en/Using_nsIXULAppInfo */
571 var appInfo = Components.classes["@mozilla.org/xre/app-info;1"]
572 .getService(Components.interfaces.nsIXULAppInfo);
573 var platformVer = appInfo.platformVersion;
575 /* We need to use different print strategies for different
576 * XUL versions, apparently
578 if (platformVer.substr(0, 5) == '1.9.0') {
579 preview_xul_190(html);
581 preview_xul_192(html);
586 g.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.spine_labels.preview.std_unexpected_err'),E);
590 function preview_xul_190(html) {
591 JSAN.use('util.window'); var win = new util.window();
592 var loc = ( urls.XUL_REMOTE_BROWSER );
593 //+ '?url=' + window.escape('about:blank') + '&show_print_button=1&alternate_print=1&no_xulG=1&title=' + window.escape('Spine Labels');
594 var w = win.open( loc, 'spine_preview', 'chrome,resizable,width=750,height=550');
596 'url' : 'about:blank',
597 'url_prefix' : function (u,s) { return xulG.url_prefix(u,s); },
598 'show_print_button' : 1,
599 'printer_context' : 'label',
600 'alternate_print' : 1,
602 'title' : $("catStrings").getString('staff.cat.spine_labels.preview.title'),
603 'on_url_load' : function(b) {
605 if (typeof w.xulG.written == 'undefined') {
606 w.xulG.written = true;
607 w.g.browser.get_content().document.write(html);
608 w.g.browser.get_content().document.close();
617 function preview_xul_192(html) {
618 var loc = ( urls.XUL_BROWSER );
622 'tab_name' : $("catStrings").getString('staff.cat.spine_labels.preview.title')
625 'url' : 'data:text/html;charset=utf-8,' + encodeURIComponent(html),
626 'html_source' : html,
627 'show_print_button' : 1,
628 'printer_context' : 'label',