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');
10 JSAN.use('util.network'); g.network = new util.network();
15 if (g.cgi.param('barcodes')) {
16 g.barcodes = g.barcodes.concat( JSON2js(g.cgi.param('barcodes')) );
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');
24 g.barcodes = g.barcodes.concat( xulG.barcodes );
27 JSAN.use('circ.util');
28 g.cols = circ.util.columns( {} );
30 for (var i = 0; i < g.cols.length; i++) {
31 g.col_map[ g.cols[i].id ] = { 'regex' : new RegExp('%' + g.cols[i].id + '%',"g"), 'render' : g.cols[i].render };
36 for (var i = 0; i < g.barcodes.length; i++) {
37 var copy = g.network.simple_request( 'FM_ACP_RETRIEVE_VIA_BARCODE.authoritative', [ g.barcodes[i] ] );
38 if (typeof copy.ilsevent != 'undefined') throw(copy);
39 var label_prefix = copy.location().label_prefix() || '';
40 var label_suffix = copy.location().label_suffix() || '';
41 if (!g.volumes[ copy.call_number() ]) {
42 var volume = g.network.simple_request( 'FM_ACN_RETRIEVE.authoritative', [ copy.call_number() ] );
43 if (typeof volume.ilsevent != 'undefined') throw(volume);
44 var record = g.network.simple_request('MODS_SLIM_RECORD_RETRIEVE.authoritative', [ volume.record() ]);
45 volume.record( record );
47 /* The volume object has native prefix and suffixes now, so affix the ones coming from copy locations */
48 var temp_prefix = label_prefix + ' ' + (typeof volume.prefix() == 'object' ? volume.prefix().label() : volume.prefix());
49 var temp_suffix = (typeof volume.suffix() == 'object' ? volume.suffix().label() : volume.suffix()) + ' ' + label_suffix;
51 /* And assume that leading and trailing spaces can be trimmed */
52 temp_prefix = temp_prefix.replace(/\s+$/,'').replace(/^\s+/,'');
53 temp_suffix = temp_suffix.replace(/\s+$/,'').replace(/^\s+/,'');
55 volume.prefix( temp_prefix );
56 volume.suffix( temp_suffix );
58 g.volumes[ volume.id() ] = volume;
60 if (g.volumes[ copy.call_number() ].copies()) {
61 var copies = g.volumes[ copy.call_number() ].copies();
63 g.volumes[ copy.call_number() ].copies( copies );
65 g.volumes[ copy.call_number() ].copies( [ copy ] );
71 if (typeof xulG != 'undefined') $('close').hidden = true;
75 g.error.standard_unexpected_error_alert('/xul/server/cat/spine_labels.xul',E);
77 alert('FIXME: ' + js2JSON(E));
82 function show_macros() {
83 JSAN.use('util.functional');
84 alert( util.functional.map_list( g.cols, function(o) { return '%' + o.id + '%'; } ).join(" ") );
87 function $(id) { return dojo.byId(id); }
89 function generate(override) {
92 JSAN.use('util.text');
93 JSAN.use('util.money');
94 JSAN.use('util.widgets');
96 $('preview').disabled = false;
98 /* Grab from OU settings, then fall back to hardcoded defaults */
100 label_cfg.spine_width = Number($('lw').value); /* spine label width */
101 if (!label_cfg.spine_width) {
102 label_cfg.spine_width = g.data.hash.aous['cat.spine.line.width'] || 8;
103 $('lw').value = label_cfg.spine_width;
105 label_cfg.spine_length = Number($('ll').value); /* spine label length */
106 if (!label_cfg.spine_length) {
107 label_cfg.spine_length = g.data.hash.aous['cat.spine.line.height'] || 9;
108 $('ll').value = label_cfg.spine_length;
110 label_cfg.spine_left_margin = Number($('lm').value); /* left margin */
111 if (!label_cfg.spine_left_margin) {
112 label_cfg.spine_left_margin = g.data.hash.aous['cat.spine.line.margin'] || 0;
113 $('lm').value = label_cfg.spine_left_margin;
115 label_cfg.font_size = Number( $('pt').value ); /* font size */
116 if (!label_cfg.font_size) {
117 label_cfg.font_size = g.data.hash.aous['cat.label.font.size'] || 10;
118 $('pt').value = label_cfg.font_size;
120 label_cfg.font_weight = $('font_weight').value; /* font weight */
121 if (!label_cfg.font_weight) {
122 label_cfg.font_weight = g.data.hash.aous['cat.label.font.weight'] || 'normal';
123 $('font_weight').value = label_cfg.font_weight;
125 label_cfg.font_family = g.data.hash.aous['cat.label.font.family'] || 'monospace';
126 label_cfg.pocket_width = Number($('plw').value) || 28; /* pocket label width */
127 label_cfg.pocket_length = Number($('pll').value) || 9; /* pocket label length */
130 var gb = $('acn_' + g.volumes[override.acn].id());
131 util.widgets.remove_children('acn_' + g.volumes[override.acn].id());
132 generate_labels(g.volumes[override.acn], gb, label_cfg, override);
134 util.widgets.remove_children('panel');
135 for (var i in g.volumes) {
136 var vb = document.createElement('vbox'); pn.appendChild(vb);
137 vb.setAttribute('name','template');
138 vb.setAttribute('acn_id',g.volumes[i].id());
139 var ds = document.createElement('description'); vb.appendChild(ds);
140 ds.appendChild( document.createTextNode( g.volumes[i].label() ) );
141 var ds2 = document.createElement('description'); vb.appendChild(ds2);
142 ds2.appendChild( document.createTextNode( g.volumes[i].copies().length + ' ' + (
143 g.volumes[i].copies().length == 1 ? $("catStrings").getString('staff.cat.spine_labels.copy') : $("catStrings").getString('staff.cat.spine_labels.copies')) ) );
144 ds2.setAttribute('style','color: green');
145 var hb = document.createElement('hbox'); vb.appendChild(hb);
147 var gb = document.createElement('groupbox');
149 gb.setAttribute('id','acn_' + g.volumes[i].id());
150 gb.setAttribute('style','border: solid black 2px');
152 generate_labels(g.volumes[i], gb, label_cfg, override);
158 g.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.spine_labels.generate.std_unexpeceted_err'),E);
162 function generate_labels(volume, label_node, label_cfg, override) {
166 if (override && volume.id() == override.acn) {
167 /* If we're calling ourself, we'll have an altered label */
168 callnum = String(override.label);
170 /* take the call number and split it on whitespace */
171 callnum = String(volume.label());
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] + ' ';
191 /* Only add the prefixes and suffixes once */
192 if (!override || volume.id() != override.acn) {
193 if (volume.prefix()) {
194 callnum = volume.prefix() + ' ' + callnum;
196 if (volume.suffix()) {
197 callnum += ' ' + volume.suffix();
201 names = callnum.split(/\s+/);
203 while (j < label_cfg.spine_length || j < label_cfg.pocket_length) {
204 var hb2 = document.createElement('hbox'); label_node.appendChild(hb2);
207 if (j < label_cfg.spine_length) {
208 var tb = document.createElement('textbox'); hb2.appendChild(tb);
210 tb.setAttribute('class','plain');
211 tb.setAttribute('style',
212 'font-family: ' + label_cfg.font_family
213 + '; font-size: ' + label_cfg.font_size
214 + '; font-weight: ' + label_cfg.font_weight
216 tb.setAttribute('size',label_cfg.spine_width+1);
217 tb.setAttribute('maxlength',label_cfg.spine_width);
218 tb.setAttribute('name','spine');
219 var spine_row_id = 'acn_' + volume.id() + '_spine_' + j;
220 tb.setAttribute('id',spine_row_id);
222 var name = names.shift();
224 name = String( name );
226 /* if the name is greater than the label width... */
227 if (name.length > label_cfg.spine_width) {
228 /* then try to split it on periods */
229 var sname = name.split(/\./);
230 if (sname.length > 1) {
231 /* if we can, then put the periods back in on each splitted element */
232 if (name.match(/^\./)) sname[0] = '.' + sname[0];
233 for (var k = 1; k < sname.length; k++) sname[k] = '.' + sname[k];
234 /* and put all but the first one back into the names array */
235 names = sname.slice(1).concat( names );
236 /* if the name fragment is still greater than the label width... */
237 if (sname[0].length > label_cfg.spine_width) {
238 /* then just truncate and throw the rest back into the names array */
239 tb.value = sname[0].substr(0,label_cfg.spine_width);
240 names = [ sname[0].substr(label_cfg.spine_width) ].concat( names );
242 /* otherwise we're set */
246 /* if we can't split on periods, then just truncate and throw the rest back into the names array */
247 tb.value = name.substr(0,label_cfg.spine_width);
248 names = [ name.substr(label_cfg.spine_width) ].concat( names );
251 /* otherwise we're set */
255 dojo.connect($(spine_row_id), 'onkeypress', 'spine_label_key_events');
259 if ($('pl').checked && j < label_cfg.pocket_length) {
260 var tb2 = document.createElement('textbox'); hb2.appendChild(tb2);
262 tb2.setAttribute('class','plain');
263 tb2.setAttribute('style',
264 'font-family: ' + label_cfg.font_family
265 + '; font-size: ' + label_cfg.font_size
266 + '; font-weight: ' + label_cfg.font_weight
268 tb2.setAttribute('size',label_cfg.pocket_width+1); tb2.setAttribute('maxlength',label_cfg.pocket_width);
269 tb2.setAttribute('name','pocket');
270 if ($('title').checked && $('title_line').value == j + 1 && instanceOf(volume.record(),mvr)) {
271 if (volume.record().title()) {
272 tb2.value = util.text.wrap_on_space( volume.record().title(), label_cfg.pocket_width )[0];
277 if ($('title_r').checked && $('title_r_line').value == j + 1 && instanceOf(volume.record(),mvr)) {
278 if (volume.record().title()) {
279 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);
284 if ($('author').checked && $('author_line').value == j + 1 && instanceOf(volume.record(),mvr)) {
285 if (volume.record().author()) {
286 tb2.value = volume.record().author().substr(0,label_cfg.pocket_width);
291 if ($('call_number').checked && $('call_number_line').value == j + 1) {
292 tb2.value = volume.label().substr(0,label_cfg.pocket_width);
294 if ($('owning_lib_shortname').checked && $('owning_lib_shortname_line').value == j + 1) {
295 var lib = volume.owning_lib();
296 if (!instanceOf(lib,aou)) lib = g.data.hash.aou[ lib ];
297 tb2.value = lib.shortname().substr(0,label_cfg.pocket_width);
299 if ($('owning_lib').checked && $('owning_lib_line').value == j + 1) {
300 var lib = volume.owning_lib();
301 if (!instanceOf(lib,aou)) lib = g.data.hash.aou[ lib ];
302 tb2.value = lib.name().substr(0,label_cfg.pocket_width);
304 if ($('shelving_location').checked && $('shelving_location_line').value == j + 1) {
305 tb2.value = '%location%';
307 if ($('barcode').checked && $('barcode_line').value == j + 1) {
308 tb2.value = '%barcode%';
310 if ($('custom1').checked && $('custom1_line').value == j + 1) {
311 tb2.value = $('custom1_tb').value;
313 if ($('custom2').checked && $('custom2_line').value == j + 1) {
314 tb2.value = $('custom2_tb').value;
316 if ($('custom3').checked && $('custom3_line').value == j + 1) {
317 tb2.value = $('custom3_tb').value;
319 if ($('custom4').checked && $('custom4_line').value == j + 1) {
320 tb2.value = $('custom4_tb').value;
328 function spine_label_key_events (event) {
330 /* Current value of the inpux box */
331 var line_value = event.target.value;
333 /* Cursor positions */
334 var sel_start = event.target.selectionStart;
335 var sel_end = event.target.selectionEnd;
337 /* Identifiers for this row: "acn_ID_spine_ROW" */
338 var atts = event.target.id.split('_');
342 "prefix": 'acn_' + atts[1] + '_spine_'
345 switch (event.charOrCode) {
346 case dojo.keys.ENTER : {
347 /* Create a new row by inserting a space at the
348 * current cursor point, then regenerating the
351 if (sel_start == sel_end) {
352 if (sel_start == 0) {
353 /* If the cursor is at the start of the line:
356 line_value = ' ' + line_value;
357 } else if (sel_start == line_value.length) {
358 /* Special case if the cursor is at the end of the line:
361 var next_row = $(row_id.prefix + (parseInt(row_id.spine) + 1));
367 line_value = line_value.substr(0, sel_start) + ' ' + line_value.substr(sel_end);
370 line_value = line_value.substr(0, sel_start) + ' ' + line_value.substr(sel_end);
372 event.target.value = line_value;
374 /* Recreate the label */
378 while (chunk = $(row_id.prefix + x)) {
380 new_label += ' ' + chunk.value;
382 new_label = chunk.value;
386 generate({"acn": row_id.acn, "label": new_label});
387 $(row_id.prefix + row_id.spine).focus();
391 case dojo.keys.BACKSPACE : {
392 /* Delete line if at the start of an input box */
393 if (sel_start == 0 && sel_end == sel_start) {
397 while (x <= (row_id.spine - 1) && (chunk = $(row_id.prefix + x))) {
399 new_label += ' ' + chunk.value;
401 new_label = chunk.value;
406 if (chunk = $(row_id.prefix + x)) {
407 new_label += chunk.value;
411 while (chunk = $(row_id.prefix + x)) {
412 new_label += ' ' + chunk.value;
415 generate({"acn": row_id.acn, "label": new_label});
416 $(row_id.prefix + row_id.spine).focus();
418 if (sel_start == 0) {
419 /* Move to the previous row */
420 var prev_row = $(row_id.prefix + (parseInt(row_id.spine) - 1));
428 case dojo.keys.DELETE : {
429 /* Delete line if at the end of an input box */
430 if (sel_start == event.target.textLength) {
434 while (x <= row_id.spine && (chunk = $(row_id.prefix + x))) {
436 new_label += ' ' + chunk.value;
438 new_label = chunk.value;
443 if (chunk = $(row_id.prefix + x)) {
444 new_label += chunk.value;
448 while (chunk = $(row_id.prefix + x)) {
449 new_label += ' ' + chunk.value;
452 generate({"acn": row_id.acn, "label": new_label});
453 $(row_id.prefix + row_id.spine).focus();
458 case dojo.keys.UP_ARROW : {
459 /* Move to the previous row */
460 var prev_row = $(row_id.prefix + (parseInt(row_id.spine) - 1));
467 case dojo.keys.DOWN_ARROW : {
468 /* Move to the next row */
469 var next_row = $(row_id.prefix + (parseInt(row_id.spine) + 1));
482 function expand_macros(text,copy,volume,record) {
483 var my = { 'acp' : copy, 'acn' : volume, 'mvr' : record };
484 var obj = { 'data' : g.data };
485 for (var i in g.col_map) {
486 var re = g.col_map[i].regex;
487 if (text.match(re)) {
489 text = text.replace(re, (typeof g.col_map[i].render == 'function' ? g.col_map[i].render(my) : eval( g.col_map[i].render ) ) );
491 g.error.sdump('D_ERROR','spine_labels.js, expand_macros() = ' + E);
498 function preview(idx) {
500 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
501 var pt = Number( $('pt').value ); /* font size */
503 pt = g.data.hash.aous['cat.label.font.size'] || 10;
506 var ff = g.data.hash.aous['cat.label.font.family'] || 'monospace';
507 var fw = $('font_weight').value; /* font weight */
509 fw = g.data.hash.aous['cat.label.font.weight'] || 'normal';
511 var lm = Number($('lm').value); /* left margin */
513 lm = g.data.hash.aous['cat.spine.line.margin'] || 0;
515 var mm = Number($('mm').value); if (mm == NaN) mm = 2; /* middle margin */
516 var lw = Number($('lw').value); /* spine label width */
518 lw = g.data.hash.aous['cat.spine.line.width'] || 8;
521 var ll = Number($('ll').value); /* spine label length */
523 ll = g.data.hash.aous['cat.spine.line.height'] || 9;
526 var plw = Number($('plw').value) || 28; var pll = Number($('pll').value) || 9; /* pocket label width and length */
527 var html = "<html><head>";
528 html += "<link type='text/css' rel='stylesheet' href='" + xulG.url_prefix('/xul/server/skin/print.css') + "'></link>"
529 html += "<link type='text/css' rel='stylesheet' href='data:text/css,pre{font-family:" + ff + ";font-size:" + pt + "pt; font-weight: " + fw + ";}'></link>";
530 html += "<title>Spine Labels</title></head><body>\n";
531 var nl = document.getElementsByAttribute('name','template');
532 for (var i = 0; i < nl.length; i++) {
533 if (typeof idx == 'undefined' || idx == null) { } else {
534 if (idx != i) continue;
536 var volume = g.volumes[ nl[i].getAttribute('acn_id') ];
538 for (var j = 0; j < volume.copies().length; j++) {
539 var copy = volume.copies()[j];
540 if (i == 0 && j == 0) {
541 html += '<pre class="first_pre">\n';
543 html += '<pre class="not_first_pre">\n';
545 var gb = nl[i].getElementsByTagName('groupbox')[0];
546 var nl2 = gb.getElementsByAttribute('name','spine');
547 for (var k = 0; k < nl2.length; k++) {
548 for (var m = 0; m < lm; m++) html += ' ';
549 html += util.text.preserve_string_in_html(expand_macros( nl2[k].value, copy, volume, volume.record() ).substr(0,lw));
550 if ($('pl').checked) {
551 var sib = nl2[k].nextSibling;
553 for (var m = 0; m < lw - nl2[k].value.length; m++) html += ' ';
554 for (var m = 0; m < mm; m++) html += ' ';
555 html += util.text.preserve_string_in_html(expand_macros( sib.value, copy, volume, volume.record() ).substr(0,plw));
560 html += '</pre hex="0C">\n';
563 html += '</body></html>';
565 /* From https://developer.mozilla.org/en/Using_nsIXULAppInfo */
566 var appInfo = Components.classes["@mozilla.org/xre/app-info;1"]
567 .getService(Components.interfaces.nsIXULAppInfo);
568 var platformVer = appInfo.platformVersion;
570 /* We need to use different print strategies for different
571 * XUL versions, apparently
573 if (platformVer.substr(0, 5) == '1.9.0') {
574 preview_xul_190(html);
576 preview_xul_192(html);
581 g.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.spine_labels.preview.std_unexpected_err'),E);
585 function preview_xul_190(html) {
586 JSAN.use('util.window'); var win = new util.window();
587 var loc = ( urls.XUL_REMOTE_BROWSER );
588 //+ '?url=' + window.escape('about:blank') + '&show_print_button=1&alternate_print=1&no_xulG=1&title=' + window.escape('Spine Labels');
589 var w = win.open( loc, 'spine_preview', 'chrome,resizable,width=750,height=550');
591 'url' : 'about:blank',
592 'url_prefix' : function (u) { return xulG.url_prefix(u); },
593 'show_print_button' : 1,
594 'printer_context' : 'label',
595 'alternate_print' : 1,
597 'title' : $("catStrings").getString('staff.cat.spine_labels.preview.title'),
598 'on_url_load' : function(b) {
600 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
601 if (typeof w.xulG.written == 'undefined') {
602 w.xulG.written = true;
603 w.g.browser.get_content().document.write(html);
604 w.g.browser.get_content().document.close();
613 function preview_xul_192(html) {
614 var loc = ( urls.XUL_BROWSER );
618 'tab_name' : $("catStrings").getString('staff.cat.spine_labels.preview.title')
621 'url' : 'data:text/html;charset=utf-8,'+window.escape(html),
622 'html_source' : html,
623 'show_print_button' : 1,
624 'printer_context' : 'label',