]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/server/cat/spine_labels.js
requirement for item price
[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', [ copy.call_number() ] );
38                                                 if (typeof volume.ilsevent != 'undefined') throw(volume);
39                                                 var record = g.network.simple_request('MODS_SLIM_RECORD_RETRIEVE', [ 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 $(id) { return document.getElementById(id); }
66
67                 function generate() {
68                         try {
69                                 var idx = 0;
70                                 JSAN.use('util.text'); JSAN.use('util.money');
71                                 JSAN.use('util.widgets'); util.widgets.remove_children('panel'); var pn = $('panel'); $('preview').disabled = false;
72                                 var lw = Number($('lw').value) || 8; /* spine label width */
73                                 var ll = Number($('ll').value) || 9; /* spine label length */
74                                 var plw = Number($('plw').value) || 28; /* pocket label width */
75                                 var pll = Number($('pll').value) || 9; /* pocket label length */
76                                 for (var i in g.volumes) {
77                                         var vb = document.createElement('vbox'); pn.appendChild(vb); vb.setAttribute('name','template'); vb.setAttribute('acn_id',g.volumes[i].id());
78                                         var ds = document.createElement('description'); vb.appendChild(ds);
79                                         ds.appendChild( document.createTextNode( g.volumes[i].label() ) );
80                                         var ds2 = document.createElement('description'); vb.appendChild(ds2);
81                                         ds2.appendChild( document.createTextNode( g.volumes[i].copies().length + ( g.volumes[i].copies().length == 1 ? ' copy' : ' copies') ) );
82                                         ds2.setAttribute('style','color: green');
83                                         var hb = document.createElement('hbox'); vb.appendChild(hb);
84
85                                         var gb = document.createElement('groupbox'); hb.appendChild(gb); 
86                                         /* take the call number and split it on whitespace */
87                                         var names = String(g.volumes[i].label()).split(/\s+/);
88                                         var j = 0;
89                                         while (j < ll || j < pll) {
90                                                 var hb2 = document.createElement('hbox'); gb.appendChild(hb2);
91                                                 
92                                                 /* spine */
93                                                 if (j < ll) {
94                                                         var tb = document.createElement('textbox'); hb2.appendChild(tb); 
95                                                         tb.value = '';
96                                                         tb.setAttribute('class','plain'); tb.setAttribute('style','font-family: monospace');
97                                                         tb.setAttribute('size',lw+1); tb.setAttribute('maxlength',lw);
98                                                         tb.setAttribute('name','spine');
99                                                         var name = names.shift(); if (name) {
100                                                                 name = String( name );
101                                                                 /* if the name is greater than the label width... */
102                                                                 if (name.length > lw) {
103                                                                         /* then try to split it on periods */
104                                                                         var sname = name.split(/\./);
105                                                                         if (sname.length > 1) {
106                                                                                 /* if we can, then put the periods back in on each splitted element */
107                                                                                 if (name.match(/^\./)) sname[0] = '.' + sname[0];
108                                                                                 for (var k = 1; k < sname.length; k++) sname[k] = '.' + sname[k];
109                                                                                 /* and put all but the first one back into the names array */
110                                                                                 names = sname.slice(1).concat( names );
111                                                                                 /* if the name fragment is still greater than the label width... */
112                                                                                 if (sname[0].length > lw) {
113                                                                                         /* then just truncate and throw the rest back into the names array */
114                                                                                         tb.value = sname[0].substr(0,lw);
115                                                                                         names = [ sname[0].substr(lw) ].concat( names );
116                                                                                 } else {
117                                                                                         /* otherwise we're set */
118                                                                                         tb.value = sname[0];
119                                                                                 }
120                                                                         } else {
121                                                                                 /* if we can't split on periods, then just truncate and throw the rest back into the names array */
122                                                                                 tb.value = name.substr(0,lw);
123                                                                                 names = [ name.substr(lw) ].concat( names );
124                                                                         }
125                                                                 } else {
126                                                                         /* otherwise we're set */
127                                                                         tb.value = name;
128                                                                 }
129                                                         }
130                                                 }
131
132                                                 /* pocket */
133                                                 if ($('pl').checked && j < pll) {
134                                                         var tb2 = document.createElement('textbox'); hb2.appendChild(tb2); 
135                                                         tb2.value = '';
136                                                         tb2.setAttribute('class','plain'); tb2.setAttribute('style','font-family: monospace');
137                                                         tb2.setAttribute('size',plw+1); tb2.setAttribute('maxlength',plw);
138                                                         tb2.setAttribute('name','pocket');
139                                                         if ($('title').checked && $('title_line').value == j + 1 && instanceOf(g.volumes[i].record(),mvr)) {
140                                                                 if (g.volumes[i].record().title()) {
141                                                                         tb2.value = util.text.wrap_on_space( g.volumes[i].record().title(), plw )[0];
142                                                                 } else {
143                                                                         tb2.value = '';
144                                                                 }
145                                                         }
146                                                         if ($('title_r').checked && $('title_r_line').value == j + 1 && instanceOf(g.volumes[i].record(),mvr)) {
147                                                                 if (g.volumes[i].record().title()) {
148                                                                         tb2.value = ( ($('title_r_indent').checked ? ' ' : '') + util.text.wrap_on_space( g.volumes[i].record().title(), plw )[1]).substr(0,plw);
149                                                                 } else {
150                                                                         tb2.value = '';
151                                                                 }
152                                                         }
153                                                         if ($('author').checked && $('author_line').value == j + 1 && instanceOf(g.volumes[i].record(),mvr)) {
154                                                                 if (g.volumes[i].record().author()) {
155                                                                         tb2.value = g.volumes[i].record().author().substr(0,plw);
156                                                                 } else {
157                                                                         tb2.value = '';
158                                                                 }
159                                                         }
160                                                         if ($('call_number').checked && $('call_number_line').value == j + 1) {
161                                                                 tb2.value = g.volumes[i].label().substr(0,plw);
162                                                         }
163                                                         if ($('owning_lib_shortname').checked && $('owning_lib_shortname_line').value == j + 1) {
164                                                                 var lib = g.volumes[i].owning_lib();
165                                                                 if (!instanceOf(lib,aou)) lib = g.data.hash.aou[ lib ];
166                                                                 tb2.value = lib.shortname().substr(0,plw);
167                                                         }
168                                                         if ($('owning_lib').checked && $('owning_lib_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.name().substr(0,plw);
172                                                         }
173                                                         if ($('shelving_location').checked && $('shelving_location_line').value == j + 1) {
174                                                                 tb2.value = '%location%';
175                                                         }
176                                                         if ($('barcode').checked && $('barcode_line').value == j + 1) {
177                                                                 tb2.value = '%barcode%';
178                                                         }
179                                                         if ($('custom1').checked && $('custom1_line').value == j + 1) {
180                                                                 tb2.value = $('custom1_tb').value;
181                                                         }
182                                                         if ($('custom2').checked && $('custom2_line').value == j + 1) {
183                                                                 tb2.value = $('custom2_tb').value;
184                                                         }
185                                                         if ($('custom3').checked && $('custom3_line').value == j + 1) {
186                                                                 tb2.value = $('custom3_tb').value;
187                                                         }
188                                                         if ($('custom4').checked && $('custom4_line').value == j + 1) {
189                                                                 tb2.value = $('custom4_tb').value;
190                                                         }
191                                                 }
192
193                                                 j++;
194                                         }
195
196                                         idx++;
197                                 }
198                         } catch(E) {
199                                 g.error.standard_unexpected_error_alert('Generate',E);
200                         }
201                 }
202
203                 function expand_macros(text,copy) {
204                         var my = { 'acp' : copy };
205                         for (var i in g.col_map) {
206                                 var re = g.col_map[i].regex;
207                                 if (text.match(re)) {
208                                         try {
209                                                 text = text.replace(re, eval( g.col_map[i].render ));
210                                         } catch(E) {
211                                                 g.error.sdump('D_ERROR','spine_labels.js, expand_macros() = ' + E);
212                                         }
213                                 }
214                         }
215                         return text;
216                 }
217
218                 function preview(idx) {
219                         try {
220                                         netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
221                                         var pt = Number( $('pt').value ) || 10;  /* font size */
222                                         var lm = Number($('lm').value); if (lm == NaN) lm = 11; /* left margin */
223                                         var mm = Number($('mm').value); if (mm == NaN) mm = 2; /* middle margin */
224                                         var lw = Number($('lw').value) || 8; var ll = Number($('ll').value) || 9; /* spine label width and length */
225                                         var plw = Number($('plw').value) || 28; var pll = Number($('pll').value) || 9; /* pocket label width and length */
226                                         var html = "<html><head><link type='text/css' rel='stylesheet' href='data:text/css,pre{font-size:" + pt;
227                                         html += "pt; page-break-after: always;}'></link><title>Spine Labels</title></head><body>\n";
228                                         var nl = document.getElementsByAttribute('name','template');
229                                         for (var i = 0; i < nl.length; i++) {
230                                                 if (typeof idx == 'undefined' || idx == null) { } else {
231                                                         if (idx != i) continue;
232                                                 }
233                                                 var volume = g.volumes[ nl[i].getAttribute('acn_id') ];
234
235                                                 for (var j = 0; j < volume.copies().length; j++) {
236                                                         var copy = volume.copies()[j];
237                                                         html += '<pre>\n';
238                                                         var gb = nl[i].getElementsByTagName('groupbox')[0];
239                                                         var nl2 = gb.getElementsByAttribute('name','spine');
240                                                         for (var k = 0; k < nl2.length; k++) {
241                                                                 for (var m = 0; m < lm; m++) html += ' ';
242                                                                 html += expand_macros( nl2[k].value, copy ).substr(0,lw);
243                                                                 if ($('pl').checked) {
244                                                                         var sib = nl2[k].nextSibling;
245                                                                         if (sib) {
246                                                                                 for (var m = 0; m < lw - nl2[k].value.length; m++) html += ' ';
247                                                                                 for (var m = 0; m < mm; m++) html += ' ';
248                                                                                 html += expand_macros( sib.value, copy ).substr(0,plw);
249                                                                         }
250                                                                 }
251                                                                 html += '\n';
252                                                         }
253                                                         html += '</pre>\n';
254                                                 }
255                                         }
256                                         html += '</body></html>';
257                                         JSAN.use('util.window'); var win = new util.window();
258                                         var loc = ( urls.XUL_REMOTE_BROWSER ) + '?url=' + window.escape('about:blank') + '&show_print_button=1&alternate_print=1&no_xulG=1&title=' + window.escape('Spine Labels');
259                                         var w = win.open( loc, 'spine_preview', 'chrome,resizable,width=750,height=550');
260                                         w.xulG = { 'on_url_load' : function(b) { 
261                                                 try { 
262                                                         netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
263                                                         if (typeof w.xulG.written == 'undefined') {
264                                                                 w.xulG.written = true;
265                                                                 w.g.browser.get_content().document.write(html);
266                                                                 w.g.browser.get_content().document.close();
267                                                         }
268                                                 } catch(E) {
269                                                         alert(E);
270                                                 }
271                                         } };
272                         } catch(E) {
273                                 g.error.standard_unexpected_error_alert('Preview and Print',E);
274                         }
275                 }
276
277