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