]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/server/cat/spine_labels.js
4dd12990871f580ee6e25c86640267fd5283e666
[Evergreen.git] / Open-ILS / xul / staff_client / server / cat / spine_labels.js
1                 function my_init() {
2                         try {
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');
9
10                                 JSAN.use('util.network'); g.network = new util.network();
11
12                                 g.cgi = new CGI();
13
14                                 g.barcodes = [];
15                                 if (g.cgi.param('barcodes')) {
16                                         g.barcodes = g.barcodes.concat( JSON2js(g.cgi.param('barcodes')) );
17                                 }
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');
22                                 }
23
24                                 JSAN.use('circ.util');
25                                 g.cols = circ.util.columns( {} );
26                                 g.col_map = {};
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 };
29                                 }
30
31                                 g.volumes = {};
32
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;
42                                         }
43                                         if (g.volumes[ copy.call_number() ].copies()) {
44                                                 var copies = g.volumes[ copy.call_number() ].copies();
45                                                 copies.push( copy );
46                                                 g.volumes[ copy.call_number() ].copies( copies );
47                                         } else {
48                                                 g.volumes[ copy.call_number() ].copies( [ copy ] );
49                                         }
50                                 }
51
52                                 generate();
53
54                                 if (typeof xulG != 'undefined') $('close').hidden = true;
55
56                         } catch(E) {
57                                 try {
58                                         g.error.standard_unexpected_error_alert('/xul/server/cat/spine_labels.xul',E);
59                                 } catch(F) {
60                                         alert('FIXME: ' + js2JSON(E));
61                                 }
62                         }
63                 }
64
65                 function show_macros() {
66                         JSAN.use('util.functional');
67                         alert( util.functional.map_list( g.cols, function(o) { return '%' + o.id + '%'; } ).join(" ") );
68                 }
69
70                 function $(id) { return document.getElementById(id); }
71
72                 function generate() {
73                         try {
74                                 var idx = 0;
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);
89
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+/);
93                                         var j = 0;
94                                         while (j < ll || j < pll) {
95                                                 var hb2 = document.createElement('hbox'); gb.appendChild(hb2);
96                                                 
97                                                 /* spine */
98                                                 if (j < ll) {
99                                                         var tb = document.createElement('textbox'); hb2.appendChild(tb); 
100                                                         tb.value = '';
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 );
121                                                                                 } else {
122                                                                                         /* otherwise we're set */
123                                                                                         tb.value = sname[0];
124                                                                                 }
125                                                                         } else {
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 );
129                                                                         }
130                                                                 } else {
131                                                                         /* otherwise we're set */
132                                                                         tb.value = name;
133                                                                 }
134                                                         }
135                                                 }
136
137                                                 /* pocket */
138                                                 if ($('pl').checked && j < pll) {
139                                                         var tb2 = document.createElement('textbox'); hb2.appendChild(tb2); 
140                                                         tb2.value = '';
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];
147                                                                 } else {
148                                                                         tb2.value = '';
149                                                                 }
150                                                         }
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);
154                                                                 } else {
155                                                                         tb2.value = '';
156                                                                 }
157                                                         }
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);
161                                                                 } else {
162                                                                         tb2.value = '';
163                                                                 }
164                                                         }
165                                                         if ($('call_number').checked && $('call_number_line').value == j + 1) {
166                                                                 tb2.value = g.volumes[i].label().substr(0,plw);
167                                                         }
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);
172                                                         }
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);
177                                                         }
178                                                         if ($('shelving_location').checked && $('shelving_location_line').value == j + 1) {
179                                                                 tb2.value = '%location%';
180                                                         }
181                                                         if ($('barcode').checked && $('barcode_line').value == j + 1) {
182                                                                 tb2.value = '%barcode%';
183                                                         }
184                                                         if ($('custom1').checked && $('custom1_line').value == j + 1) {
185                                                                 tb2.value = $('custom1_tb').value;
186                                                         }
187                                                         if ($('custom2').checked && $('custom2_line').value == j + 1) {
188                                                                 tb2.value = $('custom2_tb').value;
189                                                         }
190                                                         if ($('custom3').checked && $('custom3_line').value == j + 1) {
191                                                                 tb2.value = $('custom3_tb').value;
192                                                         }
193                                                         if ($('custom4').checked && $('custom4_line').value == j + 1) {
194                                                                 tb2.value = $('custom4_tb').value;
195                                                         }
196                                                 }
197
198                                                 j++;
199                                         }
200
201                                         idx++;
202                                 }
203                         } catch(E) {
204                                 g.error.standard_unexpected_error_alert('Generate',E);
205                         }
206                 }
207
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)) {
214                                         try {
215                                                 text = text.replace(re, (typeof g.col_map[i].render == 'function' ? g.col_map[i].render(my) : eval( g.col_map[i].render ) ) );
216                                         } catch(E) {
217                                                 g.error.sdump('D_ERROR','spine_labels.js, expand_macros() = ' + E);
218                                         }
219                                 }
220                         }
221                         return text;
222                 }
223
224                 function preview(idx) {
225                         try {
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;
238                                                 }
239                                                 var volume = g.volumes[ nl[i].getAttribute('acn_id') ];
240
241                                                 for (var j = 0; j < volume.copies().length; j++) {
242                                                         var copy = volume.copies()[j];
243                                                         html += '<pre>\n';
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;
251                                                                         if (sib) {
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));
255                                                                         }
256                                                                 }
257                                                                 html += '\n';
258                                                         }
259                                                         html += '</pre>\n';
260                                                 }
261                                         }
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');
267                                         w.xulG = { 
268                                                 'url' : 'about:blank',
269                                                 'show_print_button' : 1,
270                                                 'alternate_print' : 1,
271                                                 'no_xulG' : 1,
272                                                 'title' : 'Spine Labels',
273                                                 'on_url_load' : function(b) { 
274                                                         try { 
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();
280                                                                 }
281                                                         } catch(E) {
282                                                                 alert(E);
283                                                         }
284                                                 },
285                                         };
286                         } catch(E) {
287                                 g.error.standard_unexpected_error_alert('Preview and Print',E);
288                         }
289                 }
290
291