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 /* Jam the prefixes and suffixes into the volume object */
48 volume.prefix = label_prefix;
49 volume.suffix = label_suffix;
51 g.volumes[ volume.id() ] = volume;
53 if (g.volumes[ copy.call_number() ].copies()) {
54 var copies = g.volumes[ copy.call_number() ].copies();
56 g.volumes[ copy.call_number() ].copies( copies );
58 g.volumes[ copy.call_number() ].copies( [ copy ] );
64 if (typeof xulG != 'undefined') $('close').hidden = true;
68 g.error.standard_unexpected_error_alert('/xul/server/cat/spine_labels.xul',E);
70 alert('FIXME: ' + js2JSON(E));
75 function show_macros() {
76 JSAN.use('util.functional');
77 alert( util.functional.map_list( g.cols, function(o) { return '%' + o.id + '%'; } ).join(" ") );
80 function $(id) { return dojo.byId(id); }
82 function generate(override) {
85 JSAN.use('util.text');
86 JSAN.use('util.money');
87 JSAN.use('util.widgets');
89 $('preview').disabled = false;
91 /* Grab from OU settings, then fall back to hardcoded defaults */
93 label_cfg.spine_width = Number($('lw').value); /* spine label width */
94 if (!label_cfg.spine_width) {
95 label_cfg.spine_width = g.data.hash.aous['cat.spine.line.width'] || 8;
96 $('lw').value = label_cfg.spine_width;
98 label_cfg.spine_length = Number($('ll').value); /* spine label length */
99 if (!label_cfg.spine_length) {
100 label_cfg.spine_length = g.data.hash.aous['cat.spine.line.height'] || 9;
101 $('ll').value = label_cfg.spine_length;
103 label_cfg.spine_left_margin = Number($('lm').value); /* left margin */
104 if (!label_cfg.spine_left_margin) {
105 label_cfg.spine_left_margin = g.data.hash.aous['cat.spine.line.margin'] || 0;
106 $('lm').value = label_cfg.spine_left_margin;
108 label_cfg.font_size = Number( $('pt').value ); /* font size */
109 if (!label_cfg.font_size) {
110 label_cfg.font_size = g.data.hash.aous['cat.label.font.size'] || 10;
111 $('pt').value = label_cfg.font_size;
113 label_cfg.font_weight = $('font_weight').value; /* font weight */
114 if (!label_cfg.font_weight) {
115 label_cfg.font_weight = g.data.hash.aous['cat.label.font.weight'] || 'normal';
116 $('font_weight').value = label_cfg.font_weight;
118 label_cfg.font_family = g.data.hash.aous['cat.label.font.family'] || 'monospace';
119 label_cfg.pocket_width = Number($('plw').value) || 28; /* pocket label width */
120 label_cfg.pocket_length = Number($('pll').value) || 9; /* pocket label length */
123 var gb = $('acn_' + g.volumes[override.acn].id());
124 util.widgets.remove_children('acn_' + g.volumes[override.acn].id());
125 generate_labels(g.volumes[override.acn], gb, label_cfg, override);
127 util.widgets.remove_children('panel');
128 for (var i in g.volumes) {
129 var vb = document.createElement('vbox'); pn.appendChild(vb);
130 vb.setAttribute('name','template');
131 vb.setAttribute('acn_id',g.volumes[i].id());
132 var ds = document.createElement('description'); vb.appendChild(ds);
133 ds.appendChild( document.createTextNode( g.volumes[i].label() ) );
134 var ds2 = document.createElement('description'); vb.appendChild(ds2);
135 ds2.appendChild( document.createTextNode( g.volumes[i].copies().length + ' ' + (
136 g.volumes[i].copies().length == 1 ? $("catStrings").getString('staff.cat.spine_labels.copy') : $("catStrings").getString('staff.cat.spine_labels.copies')) ) );
137 ds2.setAttribute('style','color: green');
138 var hb = document.createElement('hbox'); vb.appendChild(hb);
140 var gb = document.createElement('groupbox');
142 gb.setAttribute('id','acn_' + g.volumes[i].id());
143 gb.setAttribute('style','border: solid black 2px');
145 generate_labels(g.volumes[i], gb, label_cfg, override);
151 g.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.spine_labels.generate.std_unexpeceted_err'),E);
155 function generate_labels(volume, label_node, label_cfg, override) {
159 if (override && volume.id() == override.acn) {
160 /* If we're calling ourself, we'll have an altered label */
161 callnum = String(override.label);
163 /* take the call number and split it on whitespace */
164 callnum = String(volume.label());
167 /* for LC, split between classification subclass letters and numbers */
168 var lc_class_re = /^([A-Z]{1,3})([0-9]+.*?)$/i;
169 var lc_class_match = lc_class_re.exec(callnum);
170 if (lc_class_match && lc_class_match.length > 1) {
171 callnum = lc_class_match[1] + ' ' + lc_class_match[2];
174 /* for LC, split between Cutter numbers */
175 var lc_cutter_re = /^(.*)(\.[A-Z]{1}[0-9]+.*?)$/ig;
176 var lc_cutter_match = lc_cutter_re.exec(callnum);
177 if (lc_cutter_match && lc_cutter_match.length > 1) {
179 for (var i = 1; i < lc_cutter_match.length; i++) {
180 callnum += lc_cutter_match[i] + ' ';
184 /* Only add the prefixes and suffixes once */
185 if (!override || volume.id() != override.acn) {
187 callnum = volume.prefix + ' ' + callnum;
190 callnum += ' ' + volume.suffix;
194 names = callnum.split(/\s+/);
196 while (j < label_cfg.spine_length || j < label_cfg.pocket_length) {
197 var hb2 = document.createElement('hbox'); label_node.appendChild(hb2);
200 if (j < label_cfg.spine_length) {
201 var tb = document.createElement('textbox'); hb2.appendChild(tb);
203 tb.setAttribute('class','plain');
204 tb.setAttribute('style',
205 'font-family: ' + label_cfg.font_family
206 + '; font-size: ' + label_cfg.font_size
207 + '; font-weight: ' + label_cfg.font_weight
209 tb.setAttribute('size',label_cfg.spine_width+1);
210 tb.setAttribute('maxlength',label_cfg.spine_width);
211 tb.setAttribute('name','spine');
212 var spine_row_id = 'acn_' + volume.id() + '_spine_' + j;
213 tb.setAttribute('id',spine_row_id);
215 var name = names.shift();
217 name = String( name );
219 /* if the name is greater than the label width... */
220 if (name.length > label_cfg.spine_width) {
221 /* then try to split it on periods */
222 var sname = name.split(/\./);
223 if (sname.length > 1) {
224 /* if we can, then put the periods back in on each splitted element */
225 if (name.match(/^\./)) sname[0] = '.' + sname[0];
226 for (var k = 1; k < sname.length; k++) sname[k] = '.' + sname[k];
227 /* and put all but the first one back into the names array */
228 names = sname.slice(1).concat( names );
229 /* if the name fragment is still greater than the label width... */
230 if (sname[0].length > label_cfg.spine_width) {
231 /* then just truncate and throw the rest back into the names array */
232 tb.value = sname[0].substr(0,label_cfg.spine_width);
233 names = [ sname[0].substr(label_cfg.spine_width) ].concat( names );
235 /* otherwise we're set */
239 /* if we can't split on periods, then just truncate and throw the rest back into the names array */
240 tb.value = name.substr(0,label_cfg.spine_width);
241 names = [ name.substr(label_cfg.spine_width) ].concat( names );
244 /* otherwise we're set */
248 dojo.connect($(spine_row_id), 'onkeypress', 'spine_label_key_events');
252 if ($('pl').checked && j < label_cfg.pocket_length) {
253 var tb2 = document.createElement('textbox'); hb2.appendChild(tb2);
255 tb2.setAttribute('class','plain');
256 tb2.setAttribute('style',
257 'font-family: ' + label_cfg.font_family
258 + '; font-size: ' + label_cfg.font_size
259 + '; font-weight: ' + label_cfg.font_weight
261 tb2.setAttribute('size',label_cfg.pocket_width+1); tb2.setAttribute('maxlength',label_cfg.pocket_width);
262 tb2.setAttribute('name','pocket');
263 if ($('title').checked && $('title_line').value == j + 1 && instanceOf(volume.record(),mvr)) {
264 if (volume.record().title()) {
265 tb2.value = util.text.wrap_on_space( volume.record().title(), label_cfg.pocket_width )[0];
270 if ($('title_r').checked && $('title_r_line').value == j + 1 && instanceOf(volume.record(),mvr)) {
271 if (volume.record().title()) {
272 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);
277 if ($('author').checked && $('author_line').value == j + 1 && instanceOf(volume.record(),mvr)) {
278 if (volume.record().author()) {
279 tb2.value = volume.record().author().substr(0,label_cfg.pocket_width);
284 if ($('call_number').checked && $('call_number_line').value == j + 1) {
285 tb2.value = volume.label().substr(0,label_cfg.pocket_width);
287 if ($('owning_lib_shortname').checked && $('owning_lib_shortname_line').value == j + 1) {
288 var lib = volume.owning_lib();
289 if (!instanceOf(lib,aou)) lib = g.data.hash.aou[ lib ];
290 tb2.value = lib.shortname().substr(0,label_cfg.pocket_width);
292 if ($('owning_lib').checked && $('owning_lib_line').value == j + 1) {
293 var lib = volume.owning_lib();
294 if (!instanceOf(lib,aou)) lib = g.data.hash.aou[ lib ];
295 tb2.value = lib.name().substr(0,label_cfg.pocket_width);
297 if ($('shelving_location').checked && $('shelving_location_line').value == j + 1) {
298 tb2.value = '%location%';
300 if ($('barcode').checked && $('barcode_line').value == j + 1) {
301 tb2.value = '%barcode%';
303 if ($('custom1').checked && $('custom1_line').value == j + 1) {
304 tb2.value = $('custom1_tb').value;
306 if ($('custom2').checked && $('custom2_line').value == j + 1) {
307 tb2.value = $('custom2_tb').value;
309 if ($('custom3').checked && $('custom3_line').value == j + 1) {
310 tb2.value = $('custom3_tb').value;
312 if ($('custom4').checked && $('custom4_line').value == j + 1) {
313 tb2.value = $('custom4_tb').value;
321 function spine_label_key_events (event) {
323 /* Current value of the inpux box */
324 var line_value = event.target.value;
326 /* Cursor positions */
327 var sel_start = event.target.selectionStart;
328 var sel_end = event.target.selectionEnd;
330 /* Identifiers for this row: "acn_ID_spine_ROW" */
331 var atts = event.target.id.split('_');
335 "prefix": 'acn_' + atts[1] + '_spine_'
338 switch (event.charOrCode) {
339 case dojo.keys.ENTER : {
340 /* Create a new row by inserting a space at the
341 * current cursor point, then regenerating the
344 if (sel_start == sel_end) {
345 if (sel_start == 0) {
346 /* If the cursor is at the start of the line:
349 line_value = ' ' + line_value;
350 } else if (sel_start == line_value.length) {
351 /* Special case if the cursor is at the end of the line:
354 var next_row = $(row_id.prefix + (parseInt(row_id.spine) + 1));
360 line_value = line_value.substr(0, sel_start) + ' ' + line_value.substr(sel_end);
363 line_value = line_value.substr(0, sel_start) + ' ' + line_value.substr(sel_end);
365 event.target.value = line_value;
367 /* Recreate the label */
371 while (chunk = $(row_id.prefix + x)) {
373 new_label += ' ' + chunk.value;
375 new_label = chunk.value;
379 generate({"acn": row_id.acn, "label": new_label});
380 $(row_id.prefix + row_id.spine).focus();
384 case dojo.keys.BACKSPACE : {
385 /* Delete line if at the start of an input box */
386 if (sel_start == 0 && sel_end == sel_start) {
390 while (x <= (row_id.spine - 1) && (chunk = $(row_id.prefix + x))) {
392 new_label += ' ' + chunk.value;
394 new_label = chunk.value;
399 if (chunk = $(row_id.prefix + x)) {
400 new_label += chunk.value;
404 while (chunk = $(row_id.prefix + x)) {
405 new_label += ' ' + chunk.value;
408 generate({"acn": row_id.acn, "label": new_label});
409 $(row_id.prefix + row_id.spine).focus();
411 if (sel_start == 0) {
412 /* Move to the previous row */
413 var prev_row = $(row_id.prefix + (parseInt(row_id.spine) - 1));
421 case dojo.keys.DELETE : {
422 /* Delete line if at the end of an input box */
423 if (sel_start == event.target.textLength) {
427 while (x <= row_id.spine && (chunk = $(row_id.prefix + x))) {
429 new_label += ' ' + chunk.value;
431 new_label = chunk.value;
436 if (chunk = $(row_id.prefix + x)) {
437 new_label += chunk.value;
441 while (chunk = $(row_id.prefix + x)) {
442 new_label += ' ' + chunk.value;
445 generate({"acn": row_id.acn, "label": new_label});
446 $(row_id.prefix + row_id.spine).focus();
451 case dojo.keys.UP_ARROW : {
452 /* Move to the previous row */
453 var prev_row = $(row_id.prefix + (parseInt(row_id.spine) - 1));
460 case dojo.keys.DOWN_ARROW : {
461 /* Move to the next row */
462 var next_row = $(row_id.prefix + (parseInt(row_id.spine) + 1));
475 function expand_macros(text,copy,volume,record) {
476 var my = { 'acp' : copy, 'acn' : volume, 'mvr' : record };
477 var obj = { 'data' : g.data };
478 for (var i in g.col_map) {
479 var re = g.col_map[i].regex;
480 if (text.match(re)) {
482 text = text.replace(re, (typeof g.col_map[i].render == 'function' ? g.col_map[i].render(my) : eval( g.col_map[i].render ) ) );
484 g.error.sdump('D_ERROR','spine_labels.js, expand_macros() = ' + E);
491 function preview(idx) {
493 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
494 var pt = Number( $('pt').value ); /* font size */
496 pt = g.data.hash.aous['cat.label.font.size'] || 10;
499 var ff = g.data.hash.aous['cat.label.font.family'] || 'monospace';
500 var fw = $('font_weight').value; /* font weight */
502 fw = g.data.hash.aous['cat.label.font.weight'] || 'normal';
504 var lm = Number($('lm').value); /* left margin */
506 lm = g.data.hash.aous['cat.spine.line.margin'] || 0;
508 var mm = Number($('mm').value); if (mm == NaN) mm = 2; /* middle margin */
509 var lw = Number($('lw').value); /* spine label width */
511 lw = g.data.hash.aous['cat.spine.line.width'] || 8;
514 var ll = Number($('ll').value); /* spine label length */
516 ll = g.data.hash.aous['cat.spine.line.height'] || 9;
519 var plw = Number($('plw').value) || 28; var pll = Number($('pll').value) || 9; /* pocket label width and length */
520 var html = "<html><head>";
521 html += "<link type='text/css' rel='stylesheet' href='" + xulG.url_prefix('/xul/server/skin/print.css') + "'></link>"
522 html += "<link type='text/css' rel='stylesheet' href='data:text/css,pre{font-family:" + ff + ";font-size:" + pt + "pt; font-weight: " + fw + ";}'></link>";
523 html += "<title>Spine Labels</title></head><body>\n";
524 var nl = document.getElementsByAttribute('name','template');
525 for (var i = 0; i < nl.length; i++) {
526 if (typeof idx == 'undefined' || idx == null) { } else {
527 if (idx != i) continue;
529 var volume = g.volumes[ nl[i].getAttribute('acn_id') ];
531 for (var j = 0; j < volume.copies().length; j++) {
532 var copy = volume.copies()[j];
533 if (i == 0 && j == 0) {
534 html += '<pre class="first_pre">\n';
536 html += '<pre class="not_first_pre">\n';
538 var gb = nl[i].getElementsByTagName('groupbox')[0];
539 var nl2 = gb.getElementsByAttribute('name','spine');
540 for (var k = 0; k < nl2.length; k++) {
541 for (var m = 0; m < lm; m++) html += ' ';
542 html += util.text.preserve_string_in_html(expand_macros( nl2[k].value, copy, volume, volume.record() ).substr(0,lw));
543 if ($('pl').checked) {
544 var sib = nl2[k].nextSibling;
546 for (var m = 0; m < lw - nl2[k].value.length; m++) html += ' ';
547 for (var m = 0; m < mm; m++) html += ' ';
548 html += util.text.preserve_string_in_html(expand_macros( sib.value, copy, volume, volume.record() ).substr(0,plw));
553 html += '</pre hex="0C">\n';
556 html += '</body></html>';
558 /* From https://developer.mozilla.org/en/Using_nsIXULAppInfo */
559 var appInfo = Components.classes["@mozilla.org/xre/app-info;1"]
560 .getService(Components.interfaces.nsIXULAppInfo);
561 var platformVer = appInfo.platformVersion;
563 /* We need to use different print strategies for different
564 * XUL versions, apparently
566 if (platformVer.substr(0, 5) == '1.9.0') {
567 preview_xul_190(html);
569 preview_xul_192(html);
574 g.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.spine_labels.preview.std_unexpected_err'),E);
578 function preview_xul_190(html) {
579 JSAN.use('util.window'); var win = new util.window();
580 var loc = ( urls.XUL_REMOTE_BROWSER );
581 //+ '?url=' + window.escape('about:blank') + '&show_print_button=1&alternate_print=1&no_xulG=1&title=' + window.escape('Spine Labels');
582 var w = win.open( loc, 'spine_preview', 'chrome,resizable,width=750,height=550');
584 'url' : 'about:blank',
585 'url_prefix' : function (u) { return xulG.url_prefix(u); },
586 'show_print_button' : 1,
587 'printer_context' : 'label',
588 'alternate_print' : 1,
590 'title' : $("catStrings").getString('staff.cat.spine_labels.preview.title'),
591 'on_url_load' : function(b) {
593 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
594 if (typeof w.xulG.written == 'undefined') {
595 w.xulG.written = true;
596 w.g.browser.get_content().document.write(html);
597 w.g.browser.get_content().document.close();
606 function preview_xul_192(html) {
607 var loc = ( urls.XUL_BROWSER );
611 'tab_name' : $("catStrings").getString('staff.cat.spine_labels.preview.title')
614 'url' : 'data:text/html;charset=utf-8,'+window.escape(html),
615 'html_source' : html,
616 'show_print_button' : 1,
617 'printer_context' : 'label',