3 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
4 if (typeof JSAN == 'undefined') { throw( "The JSAN library object is 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', [ 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 document.getElementById(id); }
75 JSAN.use('util.text'); JSAN.use('util.money');
76 JSAN.use('util.widgets'); util.widgets.remove_children('panel'); var pn = $('panel'); $('preview').disabled = false;
77 var lw = Number($('lw').value) || 8; /* spine label width */
78 var ll = Number($('ll').value) || 9; /* spine label length */
79 var plw = Number($('plw').value) || 28; /* pocket label width */
80 var pll = Number($('pll').value) || 9; /* pocket label length */
81 for (var i in g.volumes) {
82 var vb = document.createElement('vbox'); pn.appendChild(vb); vb.setAttribute('name','template'); vb.setAttribute('acn_id',g.volumes[i].id());
83 var ds = document.createElement('description'); vb.appendChild(ds);
84 ds.appendChild( document.createTextNode( g.volumes[i].label() ) );
85 var ds2 = document.createElement('description'); vb.appendChild(ds2);
86 ds2.appendChild( document.createTextNode( g.volumes[i].copies().length + ( g.volumes[i].copies().length == 1 ? ' copy' : ' copies') ) );
87 ds2.setAttribute('style','color: green');
88 var hb = document.createElement('hbox'); vb.appendChild(hb);
90 var gb = document.createElement('groupbox'); hb.appendChild(gb);
91 /* take the call number and split it on whitespace */
92 var names = String(g.volumes[i].label()).split(/\s+/);
94 while (j < ll || j < pll) {
95 var hb2 = document.createElement('hbox'); gb.appendChild(hb2);
99 var tb = document.createElement('textbox'); hb2.appendChild(tb);
101 tb.setAttribute('class','plain'); tb.setAttribute('style','font-family: monospace');
102 tb.setAttribute('size',lw+1); tb.setAttribute('maxlength',lw);
103 tb.setAttribute('name','spine');
104 var name = names.shift(); if (name) {
105 name = String( name );
106 /* if the name is greater than the label width... */
107 if (name.length > lw) {
108 /* then try to split it on periods */
109 var sname = name.split(/\./);
110 if (sname.length > 1) {
111 /* if we can, then put the periods back in on each splitted element */
112 if (name.match(/^\./)) sname[0] = '.' + sname[0];
113 for (var k = 1; k < sname.length; k++) sname[k] = '.' + sname[k];
114 /* and put all but the first one back into the names array */
115 names = sname.slice(1).concat( names );
116 /* if the name fragment is still greater than the label width... */
117 if (sname[0].length > lw) {
118 /* then just truncate and throw the rest back into the names array */
119 tb.value = sname[0].substr(0,lw);
120 names = [ sname[0].substr(lw) ].concat( names );
122 /* otherwise we're set */
126 /* if we can't split on periods, then just truncate and throw the rest back into the names array */
127 tb.value = name.substr(0,lw);
128 names = [ name.substr(lw) ].concat( names );
131 /* otherwise we're set */
138 if ($('pl').checked && j < pll) {
139 var tb2 = document.createElement('textbox'); hb2.appendChild(tb2);
141 tb2.setAttribute('class','plain'); tb2.setAttribute('style','font-family: monospace');
142 tb2.setAttribute('size',plw+1); tb2.setAttribute('maxlength',plw);
143 tb2.setAttribute('name','pocket');
144 if ($('title').checked && $('title_line').value == j + 1 && instanceOf(g.volumes[i].record(),mvr)) {
145 if (g.volumes[i].record().title()) {
146 tb2.value = util.text.wrap_on_space( g.volumes[i].record().title(), plw )[0];
151 if ($('title_r').checked && $('title_r_line').value == j + 1 && instanceOf(g.volumes[i].record(),mvr)) {
152 if (g.volumes[i].record().title()) {
153 tb2.value = ( ($('title_r_indent').checked ? ' ' : '') + util.text.wrap_on_space( g.volumes[i].record().title(), plw )[1]).substr(0,plw);
158 if ($('author').checked && $('author_line').value == j + 1 && instanceOf(g.volumes[i].record(),mvr)) {
159 if (g.volumes[i].record().author()) {
160 tb2.value = g.volumes[i].record().author().substr(0,plw);
165 if ($('call_number').checked && $('call_number_line').value == j + 1) {
166 tb2.value = g.volumes[i].label().substr(0,plw);
168 if ($('owning_lib_shortname').checked && $('owning_lib_shortname_line').value == j + 1) {
169 var lib = g.volumes[i].owning_lib();
170 if (!instanceOf(lib,aou)) lib = g.data.hash.aou[ lib ];
171 tb2.value = lib.shortname().substr(0,plw);
173 if ($('owning_lib').checked && $('owning_lib_line').value == j + 1) {
174 var lib = g.volumes[i].owning_lib();
175 if (!instanceOf(lib,aou)) lib = g.data.hash.aou[ lib ];
176 tb2.value = lib.name().substr(0,plw);
178 if ($('shelving_location').checked && $('shelving_location_line').value == j + 1) {
179 tb2.value = '%location%';
181 if ($('barcode').checked && $('barcode_line').value == j + 1) {
182 tb2.value = '%barcode%';
184 if ($('custom1').checked && $('custom1_line').value == j + 1) {
185 tb2.value = $('custom1_tb').value;
187 if ($('custom2').checked && $('custom2_line').value == j + 1) {
188 tb2.value = $('custom2_tb').value;
190 if ($('custom3').checked && $('custom3_line').value == j + 1) {
191 tb2.value = $('custom3_tb').value;
193 if ($('custom4').checked && $('custom4_line').value == j + 1) {
194 tb2.value = $('custom4_tb').value;
204 g.error.standard_unexpected_error_alert('Generate',E);
208 function expand_macros(text,copy,volume,record) {
209 var my = { 'acp' : copy, 'acn' : volume, 'mvr' : record };
210 var obj = { 'data' : g.data };
211 for (var i in g.col_map) {
212 var re = g.col_map[i].regex;
213 if (text.match(re)) {
215 text = text.replace(re, (typeof g.col_map[i].render == 'function' ? g.col_map[i].render(my) : eval( g.col_map[i].render ) ) );
217 g.error.sdump('D_ERROR','spine_labels.js, expand_macros() = ' + E);
224 function preview(idx) {
226 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
227 var pt = Number( $('pt').value ) || 10; /* font size */
228 var lm = Number($('lm').value); if (lm == NaN) lm = 11; /* left margin */
229 var mm = Number($('mm').value); if (mm == NaN) mm = 2; /* middle margin */
230 var lw = Number($('lw').value) || 8; var ll = Number($('ll').value) || 9; /* spine label width and length */
231 var plw = Number($('plw').value) || 28; var pll = Number($('pll').value) || 9; /* pocket label width and length */
232 var html = "<html><head><link type='text/css' rel='stylesheet' href='data:text/css,pre{font-size:" + pt;
233 html += "pt; page-break-after: always;}'></link><title>Spine Labels</title></head><body>\n";
234 var nl = document.getElementsByAttribute('name','template');
235 for (var i = 0; i < nl.length; i++) {
236 if (typeof idx == 'undefined' || idx == null) { } else {
237 if (idx != i) continue;
239 var volume = g.volumes[ nl[i].getAttribute('acn_id') ];
241 for (var j = 0; j < volume.copies().length; j++) {
242 var copy = volume.copies()[j];
244 var gb = nl[i].getElementsByTagName('groupbox')[0];
245 var nl2 = gb.getElementsByAttribute('name','spine');
246 for (var k = 0; k < nl2.length; k++) {
247 for (var m = 0; m < lm; m++) html += ' ';
248 html += util.text.preserve_string_in_html(expand_macros( nl2[k].value, copy, volume, volume.record() ).substr(0,lw));
249 if ($('pl').checked) {
250 var sib = nl2[k].nextSibling;
252 for (var m = 0; m < lw - nl2[k].value.length; m++) html += ' ';
253 for (var m = 0; m < mm; m++) html += ' ';
254 html += util.text.preserve_string_in_html(expand_macros( sib.value, copy, volume, volume.record() ).substr(0,plw));
262 html += '</body></html>';
263 JSAN.use('util.window'); var win = new util.window();
264 var loc = ( urls.XUL_REMOTE_BROWSER );
265 //+ '?url=' + window.escape('about:blank') + '&show_print_button=1&alternate_print=1&no_xulG=1&title=' + window.escape('Spine Labels');
266 var w = win.open( loc, 'spine_preview', 'chrome,resizable,width=750,height=550');
268 'url' : 'about:blank',
269 'show_print_button' : 1,
270 'alternate_print' : 1,
272 'title' : 'Spine Labels',
273 'on_url_load' : function(b) {
275 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
276 if (typeof w.xulG.written == 'undefined') {
277 w.xulG.written = true;
278 w.g.browser.get_content().document.write(html);
279 w.g.browser.get_content().document.close();
287 g.error.standard_unexpected_error_alert('Preview and Print',E);