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 JSAN.use('circ.util');
25 g.cols = circ.util.columns( {} );
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 };
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 if (!g.volumes[ copy.call_number() ]) {
37 var volume = g.network.simple_request( 'FM_ACN_RETRIEVE.authoritative', [ copy.call_number() ] );
38 if (typeof volume.ilsevent != 'undefined') throw(volume);
39 var record = g.network.simple_request('MODS_SLIM_RECORD_RETRIEVE.authoritative', [ volume.record() ]);
40 volume.record( record );
41 g.volumes[ volume.id() ] = volume;
43 if (g.volumes[ copy.call_number() ].copies()) {
44 var copies = g.volumes[ copy.call_number() ].copies();
46 g.volumes[ copy.call_number() ].copies( copies );
48 g.volumes[ copy.call_number() ].copies( [ copy ] );
54 if (typeof xulG != 'undefined') $('close').hidden = true;
58 g.error.standard_unexpected_error_alert('/xul/server/cat/spine_labels.xul',E);
60 alert('FIXME: ' + js2JSON(E));
65 function show_macros() {
66 JSAN.use('util.functional');
67 alert( util.functional.map_list( g.cols, function(o) { return '%' + o.id + '%'; } ).join(" ") );
70 function $(id) { return dojo.byId(id); }
72 function generate(override) {
75 JSAN.use('util.text');
76 JSAN.use('util.money');
77 JSAN.use('util.widgets');
79 $('preview').disabled = false;
80 var lw = Number($('lw').value) || 8; /* spine label width */
81 var ll = Number($('ll').value) || 9; /* spine label length */
82 var plw = Number($('plw').value) || 28; /* pocket label width */
83 var pll = Number($('pll').value) || 9; /* pocket label length */
86 var gb = $('acn_' + g.volumes[override.acn].id());
87 util.widgets.remove_children('acn_' + g.volumes[override.acn].id());
88 generate_labels(g.volumes[override.acn], gb, lw, ll, plw, pll, override);
90 util.widgets.remove_children('panel');
91 for (var i in g.volumes) {
92 var vb = document.createElement('vbox'); pn.appendChild(vb);
93 vb.setAttribute('name','template');
94 vb.setAttribute('acn_id',g.volumes[i].id());
95 var ds = document.createElement('description'); vb.appendChild(ds);
96 ds.appendChild( document.createTextNode( g.volumes[i].label() ) );
97 var ds2 = document.createElement('description'); vb.appendChild(ds2);
98 ds2.appendChild( document.createTextNode( g.volumes[i].copies().length + ' ' + (
99 g.volumes[i].copies().length == 1 ? $("catStrings").getString('staff.cat.spine_labels.copy') : $("catStrings").getString('staff.cat.spine_labels.copies')) ) );
100 ds2.setAttribute('style','color: green');
101 var hb = document.createElement('hbox'); vb.appendChild(hb);
103 var gb = document.createElement('groupbox');
105 gb.setAttribute('id','acn_' + g.volumes[i].id());
107 generate_labels(g.volumes[i], gb, lw, ll, plw, pll, override);
113 g.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.spine_labels.generate.std_unexpeceted_err'),E);
117 function generate_labels(volume, label_node, spine_width, spine_max_lines, pocket_width, pocket_max_lines, override) {
120 if (override && volume.id() == override.acn) {
121 /* If we're calling ourself, we'll have an altered label */
122 names = String(override.label).split(/\s+/);
124 /* take the call number and split it on whitespace */
125 names = String(volume.label()).split(/\s+/);
128 while (j < spine_max_lines || j < pocket_max_lines) {
129 var hb2 = document.createElement('hbox'); label_node.appendChild(hb2);
132 if (j < spine_max_lines) {
133 var tb = document.createElement('textbox'); hb2.appendChild(tb);
135 tb.setAttribute('class','plain');
136 tb.setAttribute('style','font-family: monospace');
137 tb.setAttribute('size',spine_width+1);
138 tb.setAttribute('maxlength',spine_width);
139 tb.setAttribute('name','spine');
140 var spine_row_id = 'acn_' + volume.id() + '_spine_' + j;
141 tb.setAttribute('id',spine_row_id);
142 var name = names.shift();
144 name = String( name );
145 /* if the name is greater than the label width... */
146 if (name.length > spine_width) {
147 /* then try to split it on periods */
148 var sname = name.split(/\./);
149 if (sname.length > 1) {
150 /* if we can, then put the periods back in on each splitted element */
151 if (name.match(/^\./)) sname[0] = '.' + sname[0];
152 for (var k = 1; k < sname.length; k++) sname[k] = '.' + sname[k];
153 /* and put all but the first one back into the names array */
154 names = sname.slice(1).concat( names );
155 /* if the name fragment is still greater than the label width... */
156 if (sname[0].length > spine_width) {
157 /* then just truncate and throw the rest back into the names array */
158 tb.value = sname[0].substr(0,spine_width);
159 names = [ sname[0].substr(spine_width) ].concat( names );
161 /* otherwise we're set */
165 /* if we can't split on periods, then just truncate and throw the rest back into the names array */
166 tb.value = name.substr(0,spine_width);
167 names = [ name.substr(spine_width) ].concat( names );
170 /* otherwise we're set */
174 dojo.connect($(spine_row_id), 'onkeypress', 'spine_label_key_events');
178 if ($('pl').checked && j < pocket_max_lines) {
179 var tb2 = document.createElement('textbox'); hb2.appendChild(tb2);
181 tb2.setAttribute('class','plain'); tb2.setAttribute('style','font-family: monospace');
182 tb2.setAttribute('size',pocket_width+1); tb2.setAttribute('maxlength',pocket_width);
183 tb2.setAttribute('name','pocket');
184 if ($('title').checked && $('title_line').value == j + 1 && instanceOf(volume.record(),mvr)) {
185 if (volume.record().title()) {
186 tb2.value = util.text.wrap_on_space( volume.record().title(), pocket_width )[0];
191 if ($('title_r').checked && $('title_r_line').value == j + 1 && instanceOf(volume.record(),mvr)) {
192 if (volume.record().title()) {
193 tb2.value = ( ($('title_r_indent').checked ? ' ' : '') + util.text.wrap_on_space( volume.record().title(), pocket_width )[1]).substr(0,pocket_width);
198 if ($('author').checked && $('author_line').value == j + 1 && instanceOf(volume.record(),mvr)) {
199 if (volume.record().author()) {
200 tb2.value = volume.record().author().substr(0,pocket_width);
205 if ($('call_number').checked && $('call_number_line').value == j + 1) {
206 tb2.value = volume.label().substr(0,pocket_width);
208 if ($('owning_lib_shortname').checked && $('owning_lib_shortname_line').value == j + 1) {
209 var lib = volume.owning_lib();
210 if (!instanceOf(lib,aou)) lib = g.data.hash.aou[ lib ];
211 tb2.value = lib.shortname().substr(0,pocket_width);
213 if ($('owning_lib').checked && $('owning_lib_line').value == j + 1) {
214 var lib = volume.owning_lib();
215 if (!instanceOf(lib,aou)) lib = g.data.hash.aou[ lib ];
216 tb2.value = lib.name().substr(0,pocket_width);
218 if ($('shelving_location').checked && $('shelving_location_line').value == j + 1) {
219 tb2.value = '%location%';
221 if ($('barcode').checked && $('barcode_line').value == j + 1) {
222 tb2.value = '%barcode%';
224 if ($('custom1').checked && $('custom1_line').value == j + 1) {
225 tb2.value = $('custom1_tb').value;
227 if ($('custom2').checked && $('custom2_line').value == j + 1) {
228 tb2.value = $('custom2_tb').value;
230 if ($('custom3').checked && $('custom3_line').value == j + 1) {
231 tb2.value = $('custom3_tb').value;
233 if ($('custom4').checked && $('custom4_line').value == j + 1) {
234 tb2.value = $('custom4_tb').value;
242 function spine_label_key_events (event) {
244 /* Current value of the inpux box */
245 var line_value = event.target.value;
247 /* Cursor positions */
248 var sel_start = event.target.selectionStart;
249 var sel_end = event.target.selectionEnd;
251 /* Identifiers for this row: "acn_ID_spine_ROW" */
252 var atts = event.target.id.split('_');
256 "prefix": 'acn_' + atts[1] + '_spine_'
259 switch (event.charOrCode) {
260 case dojo.keys.ENTER : {
261 /* Create a new row by inserting a space at the
262 * current cursor point, then regenerating the
265 if (sel_start == sel_end) {
266 /* Special case if the cursor is at the start of the line */
267 if (sel_start == 0) {
268 line_value = ' ' + line_value;
270 line_value = line_value.substr(0, sel_start) + ' ' + line_value.substr(sel_end);
273 line_value = line_value.substr(0, sel_start) + ' ' + line_value.substr(sel_end);
275 event.target.value = line_value;
277 /* Recreate the label */
281 while (chunk = $(row_id.prefix + x)) {
283 new_label += ' ' + chunk.value;
285 new_label = chunk.value;
289 generate({"acn": row_id.acn, "label": new_label});
290 $(row_id.prefix + row_id.spine).focus();
294 case dojo.keys.BACKSPACE : {
295 /* Delete line if at the start of an input box */
296 if (sel_start == 0) {
300 while (x <= (row_id.spine - 1) && (chunk = $(row_id.prefix + x))) {
302 new_label += ' ' + chunk.value;
304 new_label = chunk.value;
309 if (chunk = $(row_id.prefix + x)) {
310 new_label += chunk.value;
314 while (chunk = $(row_id.prefix + x)) {
315 new_label += ' ' + chunk.value;
318 generate({"acn": row_id.acn, "label": new_label});
319 $(row_id.prefix + row_id.spine).focus();
324 case dojo.keys.DELETE : {
325 /* Delete line if at the end of an input box */
326 if (sel_start == event.target.textLength) {
330 while (x <= row_id.spine && (chunk = $(row_id.prefix + x))) {
332 new_label += ' ' + chunk.value;
334 new_label = chunk.value;
339 if (chunk = $(row_id.prefix + x)) {
340 new_label += chunk.value;
344 while (chunk = $(row_id.prefix + x)) {
345 new_label += ' ' + chunk.value;
348 generate({"acn": row_id.acn, "label": new_label});
349 $(row_id.prefix + row_id.spine).focus();
354 case dojo.keys.UP_ARROW : {
355 /* Move to the previous row */
356 var prev_row = $(row_id.prefix + (parseInt(row_id.spine) - 1));
363 case dojo.keys.DOWN_ARROW : {
364 /* Move to the next row */
365 var next_row = $(row_id.prefix + (parseInt(row_id.spine) + 1));
374 function expand_macros(text,copy,volume,record) {
375 var my = { 'acp' : copy, 'acn' : volume, 'mvr' : record };
376 var obj = { 'data' : g.data };
377 for (var i in g.col_map) {
378 var re = g.col_map[i].regex;
379 if (text.match(re)) {
381 text = text.replace(re, (typeof g.col_map[i].render == 'function' ? g.col_map[i].render(my) : eval( g.col_map[i].render ) ) );
383 g.error.sdump('D_ERROR','spine_labels.js, expand_macros() = ' + E);
390 function preview(idx) {
392 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
393 var pt = Number( $('pt').value ) || 10; /* font size */
394 var lm = Number($('lm').value); if (lm == NaN) lm = 11; /* left margin */
395 var mm = Number($('mm').value); if (mm == NaN) mm = 2; /* middle margin */
396 var lw = Number($('lw').value) || 8; var ll = Number($('ll').value) || 9; /* spine label width and length */
397 var plw = Number($('plw').value) || 28; var pll = Number($('pll').value) || 9; /* pocket label width and length */
398 var html = "<html><head>";
399 html += "<link type='text/css' rel='stylesheet' href='" + xulG.url_prefix('/xul/server/skin/print.css') + "'></link>"
400 html += "<link type='text/css' rel='stylesheet' href='data:text/css,pre{font-size:" + pt + "pt;}'></link>";
401 html += "<title>Spine Labels</title></head><body>\n";
402 var nl = document.getElementsByAttribute('name','template');
403 for (var i = 0; i < nl.length; i++) {
404 if (typeof idx == 'undefined' || idx == null) { } else {
405 if (idx != i) continue;
407 var volume = g.volumes[ nl[i].getAttribute('acn_id') ];
409 for (var j = 0; j < volume.copies().length; j++) {
410 var copy = volume.copies()[j];
411 if (i == 0 && j == 0) {
412 html += '<pre class="first_pre">\n';
414 html += '<pre class="not_first_pre">\n';
416 var gb = nl[i].getElementsByTagName('groupbox')[0];
417 var nl2 = gb.getElementsByAttribute('name','spine');
418 for (var k = 0; k < nl2.length; k++) {
419 for (var m = 0; m < lm; m++) html += ' ';
420 html += util.text.preserve_string_in_html(expand_macros( nl2[k].value, copy, volume, volume.record() ).substr(0,lw));
421 if ($('pl').checked) {
422 var sib = nl2[k].nextSibling;
424 for (var m = 0; m < lw - nl2[k].value.length; m++) html += ' ';
425 for (var m = 0; m < mm; m++) html += ' ';
426 html += util.text.preserve_string_in_html(expand_macros( sib.value, copy, volume, volume.record() ).substr(0,plw));
431 html += '</pre hex="0C">\n';
434 html += '</body></html>';
435 var loc = ( urls.XUL_BROWSER );
439 'tab_name' : $("catStrings").getString('staff.cat.spine_labels.preview.title')
442 'url' : 'data:text/html;charset=utf-8,'+window.escape(html),
443 'html_source' : html,
444 'show_print_button' : 1,
449 g.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.spine_labels.preview.std_unexpected_err'),E);