]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/server/cat/spine_labels.js
Commit large i18n patch from Craig Ricciuto
[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( $("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');
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 + (
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);
90
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+/);
94                                         var j = 0;
95                                         while (j < ll || j < pll) {
96                                                 var hb2 = document.createElement('hbox'); gb.appendChild(hb2);
97                                                 
98                                                 /* spine */
99                                                 if (j < ll) {
100                                                         var tb = document.createElement('textbox'); hb2.appendChild(tb); 
101                                                         tb.value = '';
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 );
122                                                                                 } else {
123                                                                                         /* otherwise we're set */
124                                                                                         tb.value = sname[0];
125                                                                                 }
126                                                                         } else {
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 );
130                                                                         }
131                                                                 } else {
132                                                                         /* otherwise we're set */
133                                                                         tb.value = name;
134                                                                 }
135                                                         }
136                                                 }
137
138                                                 /* pocket */
139                                                 if ($('pl').checked && j < pll) {
140                                                         var tb2 = document.createElement('textbox'); hb2.appendChild(tb2); 
141                                                         tb2.value = '';
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];
148                                                                 } else {
149                                                                         tb2.value = '';
150                                                                 }
151                                                         }
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);
155                                                                 } else {
156                                                                         tb2.value = '';
157                                                                 }
158                                                         }
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);
162                                                                 } else {
163                                                                         tb2.value = '';
164                                                                 }
165                                                         }
166                                                         if ($('call_number').checked && $('call_number_line').value == j + 1) {
167                                                                 tb2.value = g.volumes[i].label().substr(0,plw);
168                                                         }
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);
173                                                         }
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);
178                                                         }
179                                                         if ($('shelving_location').checked && $('shelving_location_line').value == j + 1) {
180                                                                 tb2.value = '%location%';
181                                                         }
182                                                         if ($('barcode').checked && $('barcode_line').value == j + 1) {
183                                                                 tb2.value = '%barcode%';
184                                                         }
185                                                         if ($('custom1').checked && $('custom1_line').value == j + 1) {
186                                                                 tb2.value = $('custom1_tb').value;
187                                                         }
188                                                         if ($('custom2').checked && $('custom2_line').value == j + 1) {
189                                                                 tb2.value = $('custom2_tb').value;
190                                                         }
191                                                         if ($('custom3').checked && $('custom3_line').value == j + 1) {
192                                                                 tb2.value = $('custom3_tb').value;
193                                                         }
194                                                         if ($('custom4').checked && $('custom4_line').value == j + 1) {
195                                                                 tb2.value = $('custom4_tb').value;
196                                                         }
197                                                 }
198
199                                                 j++;
200                                         }
201
202                                         idx++;
203                                 }
204                         } catch(E) {
205                                 g.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.spine_labels.generate.std_unexpeceted_err'),E);
206                         }
207                 }
208
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)) {
215                                         try {
216                                                 text = text.replace(re, (typeof g.col_map[i].render == 'function' ? g.col_map[i].render(my) : eval( g.col_map[i].render ) ) );
217                                         } catch(E) {
218                                                 g.error.sdump('D_ERROR','spine_labels.js, expand_macros() = ' + E);
219                                         }
220                                 }
221                         }
222                         return text;
223                 }
224
225                 function preview(idx) {
226                         try {
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;
239                                                 }
240                                                 var volume = g.volumes[ nl[i].getAttribute('acn_id') ];
241
242                                                 for (var j = 0; j < volume.copies().length; j++) {
243                                                         var copy = volume.copies()[j];
244                                                         html += '<pre>\n';
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;
252                                                                         if (sib) {
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));
256                                                                         }
257                                                                 }
258                                                                 html += '\n';
259                                                         }
260                                                         html += '</pre>\n';
261                                                 }
262                                         }
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');
268                                         w.xulG = { 
269                                                 'url' : 'about:blank',
270                                                 'show_print_button' : 1,
271                                                 'alternate_print' : 1,
272                                                 'no_xulG' : 1,
273                                                 'title' : $("catStrings").('staff.cat.spine_labels.preview.title'),
274                                                 'on_url_load' : function(b) { 
275                                                         try { 
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();
281                                                                 }
282                                                         } catch(E) {
283                                                                 alert(E);
284                                                         }
285                                                 }
286                                         };
287                         } catch(E) {
288                                 g.error.standard_unexpected_error_alert($("catStrings").('staff.cat.spine_labels.preview.std_unexpected_err'),E);
289                         }
290                 }
291
292