]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/server/cat/spine_labels.js
Split spine labels between the alpha and numeric part of the LC subclass
[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.authoritative', [ 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 dojo.byId(id); }
71
72         function generate(override) {
73             try {
74                 var idx = 0;
75                 JSAN.use('util.text');
76                 JSAN.use('util.money');
77                 JSAN.use('util.widgets');
78                 var pn = $('panel');
79                 $('preview').disabled = false;
80
81                 /* Grab from OU settings, then fall back to hardcoded defaults */
82                 var label_cfg = {};
83                 label_cfg.spine_width = Number($('lw').value); /* spine label width */
84                 if (!label_cfg.spine_width) {
85                     label_cfg.spine_width = g.data.hash.aous['cat.spine.line.width'] || 8;
86                     $('lw').value = label_cfg.spine_width;
87                 }
88                 label_cfg.spine_length = Number($('ll').value); /* spine label length */
89                 if (!label_cfg.spine_length) {
90                     label_cfg.spine_length = g.data.hash.aous['cat.spine.line.height'] || 9;
91                     $('ll').value = label_cfg.spine_length;
92                 }
93                 label_cfg.spine_left_margin = Number($('lm').value); /* left margin */
94                 if (!label_cfg.spine_left_margin) {
95                     label_cfg.spine_left_margin = g.data.hash.aous['cat.spine.line.margin'] || 11;
96                     $('lm').value = label_cfg.spine_left_margin;
97                 }
98                 label_cfg.font_size = Number( $('pt').value );  /* font size */
99                 if (!label_cfg.font_size) {
100                     label_cfg.font_size = g.data.hash.aous['cat.label.font.size'] || 10;
101                     $('pt').value = label_cfg.font_size;
102                 }
103                 label_cfg.font_weight = $('font_weight').value;  /* font weight */
104                 if (!label_cfg.font_weight) {
105                     label_cfg.font_weight = g.data.hash.aous['cat.label.font.weight'] || 'normal';
106                     $('font_weight').value = label_cfg.font_weight;
107                 }
108                 label_cfg.font_family = g.data.hash.aous['cat.label.font.family'] || 'monospace';
109                 label_cfg.pocket_width = Number($('plw').value) || 28; /* pocket label width */
110                 label_cfg.pocket_length = Number($('pll').value) || 9; /* pocket label length */
111
112                 if (override) {
113                     var gb = $('acn_' + g.volumes[override.acn].id());
114                     util.widgets.remove_children('acn_' + g.volumes[override.acn].id());
115                     generate_labels(g.volumes[override.acn], gb, label_cfg, override);
116                 } else {
117                     util.widgets.remove_children('panel');
118                     for (var i in g.volumes) {
119                         var vb = document.createElement('vbox'); pn.appendChild(vb);
120                         vb.setAttribute('name','template');
121                         vb.setAttribute('acn_id',g.volumes[i].id());
122                         var ds = document.createElement('description'); vb.appendChild(ds);
123                         ds.appendChild( document.createTextNode( g.volumes[i].label() ) );
124                         var ds2 = document.createElement('description'); vb.appendChild(ds2);
125                         ds2.appendChild( document.createTextNode( g.volumes[i].copies().length + ' ' + (
126                             g.volumes[i].copies().length == 1 ? $("catStrings").getString('staff.cat.spine_labels.copy') : $("catStrings").getString('staff.cat.spine_labels.copies')) ) );
127                         ds2.setAttribute('style','color: green');
128                         var hb = document.createElement('hbox'); vb.appendChild(hb);
129
130                         var gb = document.createElement('groupbox');
131                         hb.appendChild(gb); 
132                         gb.setAttribute('id','acn_' + g.volumes[i].id());
133                         gb.setAttribute('style','border: solid black 2px');
134
135                         generate_labels(g.volumes[i], gb, label_cfg, override);
136
137                         idx++;
138                     }
139                 }
140             } catch(E) {
141                 g.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.spine_labels.generate.std_unexpeceted_err'),E);
142             }
143         }
144
145         function generate_labels(volume, label_node, label_cfg, override) {
146             var names;
147
148             if (override && volume.id() == override.acn) {
149                 /* If we're calling ourself, we'll have an altered label */
150                 names = String(override.label).split(/\s+/);
151             } else {
152                 /* take the call number and split it on whitespace */
153                 names = String(volume.label()).split(/\s+/);
154             }
155             var j = 0;
156             var name_cnt = 0;
157             /* for LC, split between classification subclass letters and numbers */
158             var lc_class_re = /^([A-Z]{1,3})([0-9]+.*?$)/i;
159             while (j < label_cfg.spine_length || j < label_cfg.pocket_length) {
160                 var hb2 = document.createElement('hbox'); label_node.appendChild(hb2);
161                 
162                 /* spine */
163                 if (j < label_cfg.spine_length) {
164                     var tb = document.createElement('textbox'); hb2.appendChild(tb); 
165                     tb.value = '';
166                     tb.setAttribute('class','plain');
167                     tb.setAttribute('style',
168                         'font-family: ' + label_cfg.font_family
169                         + '; font-size: ' + label_cfg.font_size
170                         + '; font-weight: ' + label_cfg.font_weight
171                     );
172                     tb.setAttribute('size',label_cfg.spine_width+1);
173                     tb.setAttribute('maxlength',label_cfg.spine_width);
174                     tb.setAttribute('name','spine');
175                     var spine_row_id = 'acn_' + volume.id() + '_spine_' + j;
176                     tb.setAttribute('id',spine_row_id);
177
178                     var name = names.shift();
179                     if (name) {
180                         name = String( name );
181
182                         /* Split LC subclass between alpha and numeric part */
183                         if (name_cnt == 0) {
184                             var lc_class_match = lc_class_re.exec(name);
185                             if (lc_class_match && lc_class_match.length > 1) {
186                                 name = lc_class_match[1];
187                                 names = [lc_class_match[2]].concat(names);
188                             }
189                             name_cnt = 1;
190                         }
191
192                         /* if the name is greater than the label width... */
193                         if (name.length > label_cfg.spine_width) {
194                             /* then try to split it on periods */
195                             var sname = name.split(/\./);
196                             if (sname.length > 1) {
197                                 /* if we can, then put the periods back in on each splitted element */
198                                 if (name.match(/^\./)) sname[0] = '.' + sname[0];
199                                 for (var k = 1; k < sname.length; k++) sname[k] = '.' + sname[k];
200                                 /* and put all but the first one back into the names array */
201                                 names = sname.slice(1).concat( names );
202                                 /* if the name fragment is still greater than the label width... */
203                                 if (sname[0].length > label_cfg.spine_width) {
204                                     /* then just truncate and throw the rest back into the names array */
205                                     tb.value = sname[0].substr(0,label_cfg.spine_width);
206                                     names = [ sname[0].substr(label_cfg.spine_width) ].concat( names );
207                                 } else {
208                                     /* otherwise we're set */
209                                     tb.value = sname[0];
210                                 }
211                             } else {
212                                 /* if we can't split on periods, then just truncate and throw the rest back into the names array */
213                                 tb.value = name.substr(0,label_cfg.spine_width);
214                                 names = [ name.substr(label_cfg.spine_width) ].concat( names );
215                             }
216                         } else {
217                             /* otherwise we're set */
218                             tb.value = name;
219                         }
220                     }
221                     dojo.connect($(spine_row_id), 'onkeypress', 'spine_label_key_events');
222                 }
223
224                 /* pocket */
225                 if ($('pl').checked && j < label_cfg.pocket_length) {
226                     var tb2 = document.createElement('textbox'); hb2.appendChild(tb2); 
227                     tb2.value = '';
228                     tb2.setAttribute('class','plain');
229                     tb2.setAttribute('style',
230                         'font-family: ' + label_cfg.font_family
231                         + '; font-size: ' + label_cfg.font_size
232                         + '; font-weight: ' + label_cfg.font_weight
233                     );
234                     tb2.setAttribute('size',label_cfg.pocket_width+1); tb2.setAttribute('maxlength',label_cfg.pocket_width);
235                     tb2.setAttribute('name','pocket');
236                     if ($('title').checked && $('title_line').value == j + 1 && instanceOf(volume.record(),mvr)) {
237                         if (volume.record().title()) {
238                             tb2.value = util.text.wrap_on_space( volume.record().title(), label_cfg.pocket_width )[0];
239                         } else {
240                             tb2.value = '';
241                         }
242                     }
243                     if ($('title_r').checked && $('title_r_line').value == j + 1 && instanceOf(volume.record(),mvr)) {
244                         if (volume.record().title()) {
245                             tb2.value = ( ($('title_r_indent').checked ? ' ' : '') + util.text.wrap_on_space( volume.record().title(), label_cfg.pocket_width )[1]).substr(0,label_cfg.pocket_width);
246                         } else {
247                             tb2.value = '';
248                         }
249                     }
250                     if ($('author').checked && $('author_line').value == j + 1 && instanceOf(volume.record(),mvr)) {
251                         if (volume.record().author()) {
252                             tb2.value = volume.record().author().substr(0,label_cfg.pocket_width);
253                         } else {
254                             tb2.value = '';
255                         }
256                     }
257                     if ($('call_number').checked && $('call_number_line').value == j + 1) {
258                         tb2.value = volume.label().substr(0,label_cfg.pocket_width);
259                     }
260                     if ($('owning_lib_shortname').checked && $('owning_lib_shortname_line').value == j + 1) {
261                         var lib = volume.owning_lib();
262                         if (!instanceOf(lib,aou)) lib = g.data.hash.aou[ lib ];
263                         tb2.value = lib.shortname().substr(0,label_cfg.pocket_width);
264                     }
265                     if ($('owning_lib').checked && $('owning_lib_line').value == j + 1) {
266                         var lib = volume.owning_lib();
267                         if (!instanceOf(lib,aou)) lib = g.data.hash.aou[ lib ];
268                         tb2.value = lib.name().substr(0,label_cfg.pocket_width);
269                     }
270                     if ($('shelving_location').checked && $('shelving_location_line').value == j + 1) {
271                         tb2.value = '%location%';
272                     }
273                     if ($('barcode').checked && $('barcode_line').value == j + 1) {
274                         tb2.value = '%barcode%';
275                     }
276                     if ($('custom1').checked && $('custom1_line').value == j + 1) {
277                         tb2.value = $('custom1_tb').value;
278                     }
279                     if ($('custom2').checked && $('custom2_line').value == j + 1) {
280                         tb2.value = $('custom2_tb').value;
281                     }
282                     if ($('custom3').checked && $('custom3_line').value == j + 1) {
283                         tb2.value = $('custom3_tb').value;
284                     }
285                     if ($('custom4').checked && $('custom4_line').value == j + 1) {
286                         tb2.value = $('custom4_tb').value;
287                     }
288                 }
289
290                 j++;
291             }
292         }
293
294         function spine_label_key_events (event) {
295
296             /* Current value of the inpux box */
297             var line_value = event.target.value;
298
299             /* Cursor positions */
300             var sel_start = event.target.selectionStart;
301             var sel_end = event.target.selectionEnd;
302
303             /* Identifiers for this row: "acn_ID_spine_ROW" */
304             var atts = event.target.id.split('_');
305             var row_id = {
306                 "acn": atts[1],
307                 "spine": atts[3],
308                 "prefix": 'acn_' + atts[1] + '_spine_'
309             };
310
311             switch (event.charOrCode) {
312                 case dojo.keys.ENTER : {
313                     /* Create a new row by inserting a space at the
314                      * current cursor point, then regenerating the
315                      * label
316                      */
317                     if (sel_start == sel_end) {
318                         if (sel_start == 0) {
319                             /* If the cursor is at the start of the line:
320                              * insert new line
321                              */
322                             line_value = ' ' + line_value;
323                         } else if (sel_start == line_value.length) {
324                             /* Special case if the cursor is at the end of the line:
325                              * move to next line
326                              */
327                             var next_row = $(row_id.prefix + (parseInt(row_id.spine) + 1));
328                             if (next_row) {
329                                 next_row.focus();
330                             }
331                             break;
332                         } else {
333                             line_value = line_value.substr(0, sel_start) + ' ' + line_value.substr(sel_end);
334                         }
335                     } else {
336                         line_value = line_value.substr(0, sel_start) + ' ' + line_value.substr(sel_end);
337                     }
338                     event.target.value = line_value;
339
340                     /* Recreate the label */
341                     var new_label = '';
342                     var chunk;
343                     var x = 0;
344                     while (chunk = $(row_id.prefix + x)) {
345                         if (x > 0) {
346                             new_label += ' ' + chunk.value;
347                         } else {
348                             new_label = chunk.value;
349                         }
350                         x++;
351                     }
352                     generate({"acn": row_id.acn, "label": new_label});
353                     $(row_id.prefix + row_id.spine).focus();
354                     break;
355                 }
356
357                 case dojo.keys.BACKSPACE : {
358                     /* Delete line if at the start of an input box */
359                     if (sel_start == 0) {
360                         var new_label = '';
361                         var chunk;
362                         var x = 0;
363                         while (x <= (row_id.spine - 1) && (chunk = $(row_id.prefix + x))) {
364                             if (x > 0) {
365                                 new_label += ' ' + chunk.value;
366                             } else {
367                                 new_label = chunk.value;
368                             }
369                             x++;
370                         }
371
372                         if (chunk = $(row_id.prefix + x)) {
373                             new_label += chunk.value;
374                             x++;
375                         }
376
377                         while (chunk = $(row_id.prefix + x)) {
378                             new_label += ' ' + chunk.value;
379                             x++;
380                         }
381                         generate({"acn": row_id.acn, "label": new_label});
382                         $(row_id.prefix + row_id.spine).focus();
383                     }
384                     break;
385                 }
386
387                 case dojo.keys.DELETE : {
388                     /* Delete line if at the end of an input box */
389                     if (sel_start == event.target.textLength) {
390                         var new_label = '';
391                         var chunk;
392                         var x = 0;
393                         while (x <= row_id.spine && (chunk = $(row_id.prefix + x))) {
394                             if (x > 0) {
395                                 new_label += ' ' + chunk.value;
396                             } else {
397                                 new_label = chunk.value;
398                             }
399                             x++;
400                         }
401
402                         if (chunk = $(row_id.prefix + x)) {
403                             new_label += chunk.value;
404                             x++;
405                         }
406
407                         while (chunk = $(row_id.prefix + x)) {
408                             new_label += ' ' + chunk.value;
409                             x++;
410                         }
411                         generate({"acn": row_id.acn, "label": new_label});
412                         $(row_id.prefix + row_id.spine).focus();
413                     }
414                     break;
415                 }
416
417                 case dojo.keys.UP_ARROW : {
418                     /* Move to the previous row */
419                     var prev_row = $(row_id.prefix + (parseInt(row_id.spine) - 1));
420                     if (prev_row) {
421                         prev_row.focus();
422                     }
423                     break;
424                 }
425
426                 case dojo.keys.DOWN_ARROW : {
427                     /* Move to the next row */
428                     var next_row = $(row_id.prefix + (parseInt(row_id.spine) + 1));
429                     if (next_row) {
430                         next_row.focus();
431                     }
432                     break;
433                 }
434
435                 default : {
436                     break;
437                 }
438             }
439         }
440
441         function expand_macros(text,copy,volume,record) {
442             var my = { 'acp' : copy, 'acn' : volume, 'mvr' : record };
443             var obj = { 'data' : g.data };
444             for (var i in g.col_map) {
445                 var re = g.col_map[i].regex;
446                 if (text.match(re)) {
447                     try {
448                         text = text.replace(re, (typeof g.col_map[i].render == 'function' ? g.col_map[i].render(my) : eval( g.col_map[i].render ) ) );
449                     } catch(E) {
450                         g.error.sdump('D_ERROR','spine_labels.js, expand_macros() = ' + E);
451                     }
452                 }
453             }
454             return text;
455         }
456
457         function preview(idx) {
458             try {
459                     netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
460                     var pt = Number( $('pt').value );  /* font size */
461                     if (!pt) {
462                         pt = g.data.hash.aous['cat.spine.font.size'] || 10;
463                         $('pt').value = pt;
464                     }
465                     var ff = g.data.hash.aous['cat.spine.font.family'] || 'monospace';
466                     var fw = $('font_weight').value;  /* font weight */
467                     if (!fw) {
468                         fw = g.data.hash.aous['cat.label.font.weight'] || 'normal';
469                     }
470                     var lm = Number($('lm').value); /* left margin */
471                     if (!lm) {
472                         lm = g.data.hash.aous['cat.spine.line.margin'] || 11;
473                     }
474                     var mm = Number($('mm').value); if (mm == NaN) mm = 2; /* middle margin */
475                     var lw = Number($('lw').value); /* spine label width */
476                     if (!lw) {
477                         lw = g.data.hash.aous['cat.spine.line.width'] || 8;
478                         $('lw').value = lw;
479                     }
480                     var ll = Number($('ll').value); /* spine label length */
481                     if (!ll) {
482                         ll = g.data.hash.aous['cat.spine.line.height'] || 9;
483                         $('ll').value = ll;
484                     }
485                     var plw = Number($('plw').value) || 28; var pll = Number($('pll').value) || 9; /* pocket label width and length */
486                     var html = "<html><head>";
487                     html += "<link type='text/css' rel='stylesheet' href='" + xulG.url_prefix('/xul/server/skin/print.css') + "'></link>"
488                     html += "<link type='text/css' rel='stylesheet' href='data:text/css,pre{font-family:" + ff + ";font-size:" + pt + "pt; font-weight: " + fw + ";}'></link>";
489                     html += "<title>Spine Labels</title></head><body>\n";
490                     var nl = document.getElementsByAttribute('name','template');
491                     for (var i = 0; i < nl.length; i++) {
492                         if (typeof idx == 'undefined' || idx == null) { } else {
493                             if (idx != i) continue;
494                         }
495                         var volume = g.volumes[ nl[i].getAttribute('acn_id') ];
496
497                         for (var j = 0; j < volume.copies().length; j++) {
498                             var copy = volume.copies()[j];
499                             if (i == 0 && j == 0) {
500                                 html += '<pre class="first_pre">\n';
501                             } else {
502                                 html += '<pre class="not_first_pre">\n';
503                             }
504                             var gb = nl[i].getElementsByTagName('groupbox')[0];
505                             var nl2 = gb.getElementsByAttribute('name','spine');
506                             for (var k = 0; k < nl2.length; k++) {
507                                 for (var m = 0; m < lm; m++) html += ' ';
508                                 html += util.text.preserve_string_in_html(expand_macros( nl2[k].value, copy, volume, volume.record() ).substr(0,lw));
509                                 if ($('pl').checked) {
510                                     var sib = nl2[k].nextSibling;
511                                     if (sib) {
512                                         for (var m = 0; m < lw - nl2[k].value.length; m++) html += ' ';
513                                         for (var m = 0; m < mm; m++) html += ' ';
514                                         html += util.text.preserve_string_in_html(expand_macros( sib.value, copy, volume, volume.record() ).substr(0,plw));
515                                     }
516                                 }
517                                 html += '\n';
518                             }
519                             html += '</pre hex="0C">\n';
520                         }
521                     }
522                     html += '</body></html>';
523                     var loc = ( urls.XUL_BROWSER );
524                     xulG.new_tab(
525                         loc,
526                         {
527                             'tab_name' : $("catStrings").getString('staff.cat.spine_labels.preview.title')
528                         },
529                         { 
530                             'url' : 'data:text/html;charset=utf-8,'+window.escape(html),
531                             'html_source' : html,
532                             'show_print_button' : 1,
533                             'no_xulG' : 1
534                         }
535                     );
536             } catch(E) {
537                 g.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.spine_labels.preview.std_unexpected_err'),E);
538             }
539         }
540
541