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', [ 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 + (
87 g.volumes[i].copies().length == 1 ? $("catStrings").getString('staff.cat.spine_labels.copy') : $("catStrings").getString('staff.cat.spine_labels.copies')) ) );
88 ds2.setAttribute('style','color: green');
89 var hb = document.createElement('hbox'); vb.appendChild(hb);
91 var gb = document.createElement('groupbox'); hb.appendChild(gb);
92 /* take the call number and split it on whitespace */
93 var names = String(g.volumes[i].label()).split(/\s+/);
95 while (j < ll || j < pll) {
96 var hb2 = document.createElement('hbox'); gb.appendChild(hb2);
100 var tb = document.createElement('textbox'); hb2.appendChild(tb);
102 tb.setAttribute('class','plain'); tb.setAttribute('style','font-family: monospace');
103 tb.setAttribute('size',lw+1); tb.setAttribute('maxlength',lw);
104 tb.setAttribute('name','spine');
105 var name = names.shift(); if (name) {
106 name = String( name );
107 /* if the name is greater than the label width... */
108 if (name.length > lw) {
109 /* then try to split it on periods */
110 var sname = name.split(/\./);
111 if (sname.length > 1) {
112 /* if we can, then put the periods back in on each splitted element */
113 if (name.match(/^\./)) sname[0] = '.' + sname[0];
114 for (var k = 1; k < sname.length; k++) sname[k] = '.' + sname[k];
115 /* and put all but the first one back into the names array */
116 names = sname.slice(1).concat( names );
117 /* if the name fragment is still greater than the label width... */
118 if (sname[0].length > lw) {
119 /* then just truncate and throw the rest back into the names array */
120 tb.value = sname[0].substr(0,lw);
121 names = [ sname[0].substr(lw) ].concat( names );
123 /* otherwise we're set */
127 /* if we can't split on periods, then just truncate and throw the rest back into the names array */
128 tb.value = name.substr(0,lw);
129 names = [ name.substr(lw) ].concat( names );
132 /* otherwise we're set */
139 if ($('pl').checked && j < pll) {
140 var tb2 = document.createElement('textbox'); hb2.appendChild(tb2);
142 tb2.setAttribute('class','plain'); tb2.setAttribute('style','font-family: monospace');
143 tb2.setAttribute('size',plw+1); tb2.setAttribute('maxlength',plw);
144 tb2.setAttribute('name','pocket');
145 if ($('title').checked && $('title_line').value == j + 1 && instanceOf(g.volumes[i].record(),mvr)) {
146 if (g.volumes[i].record().title()) {
147 tb2.value = util.text.wrap_on_space( g.volumes[i].record().title(), plw )[0];
152 if ($('title_r').checked && $('title_r_line').value == j + 1 && instanceOf(g.volumes[i].record(),mvr)) {
153 if (g.volumes[i].record().title()) {
154 tb2.value = ( ($('title_r_indent').checked ? ' ' : '') + util.text.wrap_on_space( g.volumes[i].record().title(), plw )[1]).substr(0,plw);
159 if ($('author').checked && $('author_line').value == j + 1 && instanceOf(g.volumes[i].record(),mvr)) {
160 if (g.volumes[i].record().author()) {
161 tb2.value = g.volumes[i].record().author().substr(0,plw);
166 if ($('call_number').checked && $('call_number_line').value == j + 1) {
167 tb2.value = g.volumes[i].label().substr(0,plw);
169 if ($('owning_lib_shortname').checked && $('owning_lib_shortname_line').value == j + 1) {
170 var lib = g.volumes[i].owning_lib();
171 if (!instanceOf(lib,aou)) lib = g.data.hash.aou[ lib ];
172 tb2.value = lib.shortname().substr(0,plw);
174 if ($('owning_lib').checked && $('owning_lib_line').value == j + 1) {
175 var lib = g.volumes[i].owning_lib();
176 if (!instanceOf(lib,aou)) lib = g.data.hash.aou[ lib ];
177 tb2.value = lib.name().substr(0,plw);
179 if ($('shelving_location').checked && $('shelving_location_line').value == j + 1) {
180 tb2.value = '%location%';
182 if ($('barcode').checked && $('barcode_line').value == j + 1) {
183 tb2.value = '%barcode%';
185 if ($('custom1').checked && $('custom1_line').value == j + 1) {
186 tb2.value = $('custom1_tb').value;
188 if ($('custom2').checked && $('custom2_line').value == j + 1) {
189 tb2.value = $('custom2_tb').value;
191 if ($('custom3').checked && $('custom3_line').value == j + 1) {
192 tb2.value = $('custom3_tb').value;
194 if ($('custom4').checked && $('custom4_line').value == j + 1) {
195 tb2.value = $('custom4_tb').value;
205 g.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.spine_labels.generate.std_unexpeceted_err'),E);
209 function expand_macros(text,copy,volume,record) {
210 var my = { 'acp' : copy, 'acn' : volume, 'mvr' : record };
211 var obj = { 'data' : g.data };
212 for (var i in g.col_map) {
213 var re = g.col_map[i].regex;
214 if (text.match(re)) {
216 text = text.replace(re, (typeof g.col_map[i].render == 'function' ? g.col_map[i].render(my) : eval( g.col_map[i].render ) ) );
218 g.error.sdump('D_ERROR','spine_labels.js, expand_macros() = ' + E);
225 function preview(idx) {
227 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
228 var pt = Number( $('pt').value ) || 10; /* font size */
229 var lm = Number($('lm').value); if (lm == NaN) lm = 11; /* left margin */
230 var mm = Number($('mm').value); if (mm == NaN) mm = 2; /* middle margin */
231 var lw = Number($('lw').value) || 8; var ll = Number($('ll').value) || 9; /* spine label width and length */
232 var plw = Number($('plw').value) || 28; var pll = Number($('pll').value) || 9; /* pocket label width and length */
233 var html = "<html><head><link type='text/css' rel='stylesheet' href='data:text/css,pre{font-size:" + pt;
234 html += "pt; page-break-after: always;}'></link><title>Spine Labels</title></head><body>\n";
235 var nl = document.getElementsByAttribute('name','template');
236 for (var i = 0; i < nl.length; i++) {
237 if (typeof idx == 'undefined' || idx == null) { } else {
238 if (idx != i) continue;
240 var volume = g.volumes[ nl[i].getAttribute('acn_id') ];
242 for (var j = 0; j < volume.copies().length; j++) {
243 var copy = volume.copies()[j];
245 var gb = nl[i].getElementsByTagName('groupbox')[0];
246 var nl2 = gb.getElementsByAttribute('name','spine');
247 for (var k = 0; k < nl2.length; k++) {
248 for (var m = 0; m < lm; m++) html += ' ';
249 html += util.text.preserve_string_in_html(expand_macros( nl2[k].value, copy, volume, volume.record() ).substr(0,lw));
250 if ($('pl').checked) {
251 var sib = nl2[k].nextSibling;
253 for (var m = 0; m < lw - nl2[k].value.length; m++) html += ' ';
254 for (var m = 0; m < mm; m++) html += ' ';
255 html += util.text.preserve_string_in_html(expand_macros( sib.value, copy, volume, volume.record() ).substr(0,plw));
263 html += '</body></html>';
264 JSAN.use('util.window'); var win = new util.window();
265 var loc = ( urls.XUL_REMOTE_BROWSER );
266 //+ '?url=' + window.escape('about:blank') + '&show_print_button=1&alternate_print=1&no_xulG=1&title=' + window.escape('Spine Labels');
267 var w = win.open( loc, 'spine_preview', 'chrome,resizable,width=750,height=550');
269 'url' : 'about:blank',
270 'show_print_button' : 1,
271 'alternate_print' : 1,
273 'title' : $("catStrings").('staff.cat.spine_labels.preview.title'),
274 'on_url_load' : function(b) {
276 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
277 if (typeof w.xulG.written == 'undefined') {
278 w.xulG.written = true;
279 w.g.browser.get_content().document.write(html);
280 w.g.browser.get_content().document.close();
288 g.error.standard_unexpected_error_alert($("catStrings").('staff.cat.spine_labels.preview.std_unexpected_err'),E);