]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/server/cat/marcedit.js
selecting the contents of a fixed field when clicking the label
[working/Evergreen.git] / Open-ILS / xul / staff_client / server / cat / marcedit.js
1 var marcns = new Namespace("http://www.loc.gov/MARC21/slim");
2 var xulns = new Namespace("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul");
3 default xml namespace = marcns;
4
5 var tooltip_hash = {};
6 var current_focus;
7 var _record_type;
8 var bib_data;
9
10 function createComplexHTMLElement (e, attrs, objects, text) {
11         var l = document.createElementNS('http://www.w3.org/1999/xhtml',e);
12
13         if (attrs) {
14                 for (var i in attrs) l.setAttribute(i,attrs[i]);
15         }
16
17         if (objects) {
18                 for ( var i in objects ) l.appendChild( objects[i] );
19         }
20
21         if (text) {
22                 l.appendChild( document.createTextNode(text) )
23         }
24
25         return l;
26 }
27
28 function createComplexXULElement (e, attrs, objects) {
29         var l = document.createElementNS('http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul',e);
30
31         if (attrs) {
32                 for (var i in attrs) l.setAttribute(i,attrs[i]);
33         }
34
35         if (objects) {
36                 for ( var i in objects ) l.appendChild( objects[i] );
37         }
38
39         return l;
40 }
41
42 function createDescription (attrs) {
43         return createComplexXULElement('description', attrs, Array.prototype.slice.apply(arguments, [1]) );
44 }
45
46 function createTooltip (attrs) {
47         return createComplexXULElement('tooltip', attrs, Array.prototype.slice.apply(arguments, [1]) );
48 }
49
50 function createLabel (attrs) {
51         return createComplexXULElement('label', attrs, Array.prototype.slice.apply(arguments, [1]) );
52 }
53
54 function createVbox (attrs) {
55         return createComplexXULElement('vbox', attrs, Array.prototype.slice.apply(arguments, [1]) );
56 }
57
58 function createHbox (attrs) {
59         return createComplexXULElement('hbox', attrs, Array.prototype.slice.apply(arguments, [1]) );
60 }
61
62 function createRow (attrs) {
63         return createComplexXULElement('row', attrs, Array.prototype.slice.apply(arguments, [1]) );
64 }
65
66 function createTextbox (attrs) {
67         return createComplexXULElement('textbox', attrs, Array.prototype.slice.apply(arguments, [1]) );
68 }
69
70 function createPopup (attrs) {
71         return createComplexXULElement('popup', attrs, Array.prototype.slice.apply(arguments, [1]) );
72 }
73
74 function createMenuitem (attrs) {
75         return createComplexXULElement('menuitem', attrs, Array.prototype.slice.apply(arguments, [1]) );
76 }
77
78 function createMARCTextbox (element,attrs) {
79
80         var box = createComplexXULElement('textbox', attrs, Array.prototype.slice.apply(arguments, [2]) );
81         box.onkeypress = function (event) {
82                 var root_node;
83                 var node = element;
84                 while(node = node.parent()) {
85                         root_node = node;
86                 }
87
88                 var row = event.target;
89                 while (row.tagName != 'row') row = row.parentNode;
90
91                 if (element.nodeKind() == 'attribute') element[0]=box.value;
92                 else element.setChildren( box.value );
93
94                 if (event.charCode == 100 && event.ctrlKey) { // ctrl+d
95
96                         var index_sf, target, move_data;
97                         if (element.localName() == 'subfield') {
98                                 index_sf = element;
99                                 target = event.target.parentNode;
100
101                                 var start = event.target.selectionStart;
102                                 var end = event.target.selectionEnd - event.target.selectionStart ?
103                                                 event.target.selectionEnd :
104                                                 event.target.value.length;
105
106                                 move_data = event.target.value.substring(start,end);
107                                 event.target.value = event.target.value.substring(0,start) + event.target.value.substring(end);
108                                 event.target.setAttribute('size', event.target.value.length + 2);
109
110                                 element.setChildren( event.target.value );
111
112                         } else if (element.localName() == 'code') {
113                                 index_sf = element.parent();
114                                 target = event.target.parentNode;
115                         } else if (element.localName() == 'tag' || element.localName() == 'ind1' || element.localName() == 'ind2') {
116                                 index_sf = element.parent().children()[element.parent().children().length() - 1];
117                                 target = event.target.parentNode.lastChild.lastChild;
118                         }
119
120                         var sf = <subfield code="" xmlns="http://www.loc.gov/MARC21/slim">{ move_data }</subfield>;
121
122                         index_sf.parent().insertChildAfter( index_sf, sf );
123
124                         var new_sf = marcSubfield(sf);
125
126                         if (target === target.parentNode.lastChild) {
127                                 target.parentNode.appendChild( new_sf );
128                         } else {
129                                 target.parentNode.insertBefore( new_sf, target.nextSibling );
130                         }
131
132                         new_sf.firstChild.nextSibling.focus();
133
134                         event.preventDefault();
135                         return false;
136
137                 } else if (event.keyCode == 13 || event.keyCode == 77) {
138                         if (event.ctrlKey) { // ctrl+enter
139
140                                 var index;
141                                 if (element.localName() == 'subfield') index = element.parent();
142                                 if (element.localName() == 'code') index = element.parent().parent();
143                                 if (element.localName() == 'tag') index = element.parent();
144                                 if (element.localName() == 'ind1') index = element.parent();
145                                 if (element.localName() == 'ind2') index = element.parent();
146
147                                 var df = <datafield tag="" ind1="" ind2="" xmlns="http://www.loc.gov/MARC21/slim"><subfield code="" /></datafield>;
148
149                                 index.parent().insertChildAfter( index, df );
150
151                                 var new_df = marcDatafield(df);
152
153                                 if (row.parentNode.lastChild === row) {
154                                         row.parentNode.appendChild( new_df );
155                                 } else {
156                                         row.parentNode.insertBefore( new_df, row.nextSibling );
157                                 }
158
159                                 new_df.firstChild.focus();
160
161                                 event.preventDefault();
162                                 return false;
163
164                         } else if (event.shiftKey) {
165                                 if (row.previousSibling.className.match('marcDatafieldRow'))
166                                         row.previousSibling.firstChild.focus();
167                         } else {
168                                 row.nextSibling.firstChild.focus();
169                         }
170
171                 } else if (event.keyCode == 46 && event.ctrlKey) { // ctrl+del
172
173                         var index;
174                         if (element.localName() == 'subfield') index = element.parent();
175                         if (element.localName() == 'code') index = element.parent().parent();
176                         if (element.localName() == 'tag') index = element.parent();
177                         if (element.localName() == 'ind1') index = element.parent();
178                         if (element.localName() == 'ind2') index = element.parent();
179
180                         for (var i in index.parent().children()) {
181                                 if (index === index.parent().children()[i]) {
182                                         delete index.parent().children()[i];
183                                         break;
184                                 }
185                         }
186
187                         row.previousSibling.firstChild.focus();
188                         row.parentNode.removeChild(row);
189
190                         event.preventDefault();
191                         return false;
192
193                 } else if (event.keyCode == 46 && event.shiftKey) { // shift+del
194
195                         var index;
196                         if (element.localName() == 'subfield') index = element;
197                         if (element.localName() == 'code') index = element.parent();
198
199                         if (index) {
200                                 for (var i in index.parent().children()) {
201                                         if (index === index.parent().children()[i]) {
202                                                 delete index.parent().children()[i];
203                                                 break;
204                                         }
205                                 }
206
207                                 if (event.target.parentNode === event.target.parentNode.parentNode.lastChild) {
208                                         event.target.parentNode.previousSibling.lastChild.focus();
209                                 } else {
210                                         event.target.parentNode.nextSibling.firstChild.nextSibling.focus();
211                                 }
212
213                                 event.target.parentNode.parentNode.removeChild(event.target.parentNode);
214
215                                 event.preventDefault();
216                                 return false;
217                         }
218                 }
219                 return true;
220         };
221
222         box.addEventListener(
223                 'keypress', 
224                 function () {
225                         if (element.nodeKind() == 'attribute') element[0]=box.value;
226                         else element.setChildren( box.value );
227                         return true;
228                 },
229                 false
230         );
231
232         box.addEventListener(
233                 'change', 
234                 function () {
235                         if (element.nodeKind() == 'attribute') element[0]=box.value;
236                         else element.setChildren( box.value );
237                         return true;
238                 },
239                 false
240         );
241
242         box.addEventListener(
243                 'keypress', 
244                 function () {
245                         if (element.nodeKind() == 'attribute') element[0]=box.value;
246                         else element.setChildren( box.value );
247                         return true;
248                 },
249                 true
250         );
251
252         return box;
253 }
254
255 var rec_type = {
256         BKS : { Type : /[at]{1}/,       BLvl : /[acdm]{1}/ },
257         SER : { Type : /[a]{1}/,        BLvl : /[bs]{1}/ },
258         VIS : { Type : /[gkro]{1}/,     BLvl : /[abcdms]{1}/ },
259         MIX : { Type : /[p]{1}/,        BLvl : /[cd]{1}/ },
260         MAP : { Type : /[ef]{1}/,       BLvl : /[abcdms]{1}/ },
261         SCO : { Type : /[cd]{1}/,       BLvl : /[abcdms]{1}/ },
262         REC : { Type : /[ij]{1}/,       BLvl : /[abcdms]{1}/ },
263         COM : { Type : /[m]{1}/,        BLvl : /[abcdms]{1}/ }
264 };
265
266 var ff_pos = {
267         Ctry : {
268                 _8 : {
269                         BKS : {start : 15, len : 3, def : ' ' },
270                         SER : {start : 15, len : 3, def : ' ' },
271                         VIS : {start : 15, len : 3, def : ' ' },
272                         MIX : {start : 15, len : 3, def : ' ' },
273                         MAP : {start : 15, len : 3, def : ' ' },
274                         SCO : {start : 15, len : 3, def : ' ' },
275                         REC : {start : 15, len : 3, def : ' ' },
276                         COM : {start : 15, len : 3, def : ' ' },
277                 }
278         },
279         Lang : {
280                 _8 : {
281                         BKS : {start : 35, len : 3, def : ' ' },
282                         SER : {start : 35, len : 3, def : ' ' },
283                         VIS : {start : 35, len : 3, def : ' ' },
284                         MIX : {start : 35, len : 3, def : ' ' },
285                         MAP : {start : 35, len : 3, def : ' ' },
286                         SCO : {start : 35, len : 3, def : ' ' },
287                         REC : {start : 35, len : 3, def : ' ' },
288                         COM : {start : 35, len : 3, def : ' ' },
289                 }
290         },
291         MRec : {
292                 _8 : {
293                         BKS : {start : 38, len : 1, def : ' ' },
294                         SER : {start : 38, len : 1, def : ' ' },
295                         VIS : {start : 38, len : 1, def : ' ' },
296                         MIX : {start : 38, len : 1, def : ' ' },
297                         MAP : {start : 38, len : 1, def : ' ' },
298                         SCO : {start : 38, len : 1, def : ' ' },
299                         REC : {start : 38, len : 1, def : ' ' },
300                         COM : {start : 38, len : 1, def : ' ' },
301                 }
302         },
303         DtSt : {
304                 _8 : {
305                         BKS : {start : 6, len : 1, def : ' ' },
306                         SER : {start : 6, len : 1, def : 'c' },
307                         VIS : {start : 6, len : 1, def : ' ' },
308                         MIX : {start : 6, len : 1, def : ' ' },
309                         MAP : {start : 6, len : 1, def : ' ' },
310                         SCO : {start : 6, len : 1, def : ' ' },
311                         REC : {start : 6, len : 1, def : ' ' },
312                         COM : {start : 6, len : 1, def : ' ' },
313                 }
314         },
315         Type : {
316                 ldr : {
317                         BKS : {start : 6, len : 1, def : 'a' },
318                         SER : {start : 6, len : 1, def : 'a' },
319                         VIS : {start : 6, len : 1, def : 'g' },
320                         MIX : {start : 6, len : 1, def : 'p' },
321                         MAP : {start : 6, len : 1, def : 'e' },
322                         SCO : {start : 6, len : 1, def : 'c' },
323                         REC : {start : 6, len : 1, def : 'i' },
324                         COM : {start : 6, len : 1, def : 'm' },
325                 }
326         },
327         Ctrl : {
328                 ldr : {
329                         BKS : {start : 8, len : 1, def : ' ' },
330                         SER : {start : 8, len : 1, def : ' ' },
331                         VIS : {start : 8, len : 1, def : ' ' },
332                         MIX : {start : 8, len : 1, def : ' ' },
333                         MAP : {start : 8, len : 1, def : ' ' },
334                         SCO : {start : 8, len : 1, def : ' ' },
335                         REC : {start : 8, len : 1, def : ' ' },
336                         COM : {start : 8, len : 1, def : ' ' },
337                 }
338         },
339         BLvl : {
340                 ldr : {
341                         BKS : {start : 7, len : 1, def : 'm' },
342                         SER : {start : 7, len : 1, def : 's' },
343                         VIS : {start : 7, len : 1, def : 'm' },
344                         MIX : {start : 7, len : 1, def : 'c' },
345                         MAP : {start : 7, len : 1, def : 'm' },
346                         SCO : {start : 7, len : 1, def : 'm' },
347                         REC : {start : 7, len : 1, def : 'm' },
348                         COM : {start : 7, len : 1, def : 'm' },
349                 }
350         },
351         Desc : {
352                 ldr : {
353                         BKS : {start : 18, len : 1, def : ' ' },
354                         SER : {start : 18, len : 1, def : ' ' },
355                         VIS : {start : 18, len : 1, def : ' ' },
356                         MIX : {start : 18, len : 1, def : ' ' },
357                         MAP : {start : 18, len : 1, def : ' ' },
358                         SCO : {start : 18, len : 1, def : ' ' },
359                         REC : {start : 18, len : 1, def : ' ' },
360                         COM : {start : 18, len : 1, def : ' ' },
361                 }
362         },
363         ELvl : {
364                 ldr : {
365                         BKS : {start : 17, len : 1, def : ' ' },
366                         SER : {start : 17, len : 1, def : ' ' },
367                         VIS : {start : 17, len : 1, def : ' ' },
368                         MIX : {start : 17, len : 1, def : ' ' },
369                         MAP : {start : 17, len : 1, def : ' ' },
370                         SCO : {start : 17, len : 1, def : ' ' },
371                         REC : {start : 17, len : 1, def : ' ' },
372                         COM : {start : 17, len : 1, def : ' ' },
373                 }
374         },
375         Indx : {
376                 _8 : {
377                         BKS : {start : 31, len : 1, def : '0' },
378                         MAP : {start : 31, len : 1, def : '0' },
379                 },
380                 _6 : {
381                         BKS : {start : 14, len : 1, def : '0' },
382                         MAP : {start : 14, len : 1, def : '0' },
383                 }
384         },
385         Date1 : {
386                 _8 : {
387                         BKS : {start : 7, len : 4, def : ' ' },
388                         SER : {start : 7, len : 4, def : ' ' },
389                         VIS : {start : 7, len : 4, def : ' ' },
390                         MIX : {start : 7, len : 4, def : ' ' },
391                         MAP : {start : 7, len : 4, def : ' ' },
392                         SCO : {start : 7, len : 4, def : ' ' },
393                         REC : {start : 7, len : 4, def : ' ' },
394                         COM : {start : 7, len : 4, def : ' ' },
395                 },
396         },
397         Date2 : {
398                 _8 : {
399                         BKS : {start : 11, len : 4, def : ' ' },
400                         SER : {start : 11, len : 4, def : '9' },
401                         VIS : {start : 11, len : 4, def : ' ' },
402                         MIX : {start : 11, len : 4, def : ' ' },
403                         MAP : {start : 11, len : 4, def : ' ' },
404                         SCO : {start : 11, len : 4, def : ' ' },
405                         REC : {start : 11, len : 4, def : ' ' },
406                         COM : {start : 11, len : 4, def : ' ' },
407                 },
408         },
409         LitF : {
410                 _8 : {
411                         BKS : {start : 33, len : 1, def : '0' },
412                 },
413                 _6 : {
414                         BKS : {start : 16, len : 1, def : '0' },
415                 }
416         },
417         Biog : {
418                 _8 : {
419                         BKS : {start : 34, len : 1, def : ' ' },
420                 },
421                 _6 : {
422                         BKS : {start : 17, len : 1, def : ' ' },
423                 }
424         },
425         Ills : {
426                 _8 : {
427                         BKS : {start : 18, len : 4, def : ' ' },
428                 },
429                 _6 : {
430                         BKS : {start : 1, len : 4, def : ' ' },
431                 }
432         },
433         Fest : {
434                 _8 : {
435                         BKS : {start : 30, len : 1, def : '0' },
436                 },
437                 _6 : {
438                         BKS : {start : 13, len : 1, def : '0' },
439                 }
440         },
441         Conf : {
442                 _8 : {
443                         BKS : {start : 24, len : 4, def : ' ' },
444                         SER : {start : 25, len : 3, def : ' ' },
445                 },
446                 _6 : {
447                         BKS : {start : 7, len : 4, def : ' ' },
448                         SER : {start : 8, len : 3, def : ' ' },
449                 }
450         },
451         GPub : {
452                 _8 : {
453                         BKS : {start : 28, len : 1, def : ' ' },
454                         SER : {start : 28, len : 1, def : ' ' },
455                         VIS : {start : 28, len : 1, def : ' ' },
456                         MAP : {start : 28, len : 1, def : ' ' },
457                         COM : {start : 28, len : 1, def : ' ' },
458                 },
459                 _6 : {
460                         BKS : {start : 11, len : 1, def : ' ' },
461                         SER : {start : 11, len : 1, def : ' ' },
462                         VIS : {start : 11, len : 1, def : ' ' },
463                         MAP : {start : 11, len : 1, def : ' ' },
464                         COM : {start : 11, len : 1, def : ' ' },
465                 }
466         },
467         Audn : {
468                 _8 : {
469                         BKS : {start : 22, len : 1, def : ' ' },
470                         SER : {start : 22, len : 1, def : ' ' },
471                         VIS : {start : 22, len : 1, def : ' ' },
472                         SCO : {start : 22, len : 1, def : ' ' },
473                         REC : {start : 22, len : 1, def : ' ' },
474                         COM : {start : 22, len : 1, def : ' ' },
475                 },
476                 _6 : {
477                         BKS : {start : 5, len : 1, def : ' ' },
478                         SER : {start : 5, len : 1, def : ' ' },
479                         VIS : {start : 5, len : 1, def : ' ' },
480                         SCO : {start : 5, len : 1, def : ' ' },
481                         REC : {start : 5, len : 1, def : ' ' },
482                         COM : {start : 5, len : 1, def : ' ' },
483                 }
484         },
485         Form : {
486                 _8 : {
487                         BKS : {start : 23, len : 1, def : ' ' },
488                         SER : {start : 23, len : 1, def : ' ' },
489                         VIS : {start : 29, len : 1, def : ' ' },
490                         MIX : {start : 23, len : 1, def : ' ' },
491                         MAP : {start : 29, len : 1, def : ' ' },
492                         SCO : {start : 23, len : 1, def : ' ' },
493                         REC : {start : 23, len : 1, def : ' ' },
494                 },
495                 _6 : {
496                         BKS : {start : 6, len : 1, def : ' ' },
497                         SER : {start : 6, len : 1, def : ' ' },
498                         VIS : {start : 12, len : 1, def : ' ' },
499                         MIX : {start : 6, len : 1, def : ' ' },
500                         MAP : {start : 12, len : 1, def : ' ' },
501                         SCO : {start : 6, len : 1, def : ' ' },
502                         REC : {start : 6, len : 1, def : ' ' },
503                 }
504         },
505         'S/L' : {
506                 _8 : {
507                         SER : {start : 34, len : 1, def : '0' },
508                 },
509                 _6 : {
510                         SER : {start : 17, len : 1, def : '0' },
511                 }
512         },
513         'Alph' : {
514                 _8 : {
515                         SER : {start : 33, len : 1, def : ' ' },
516                 },
517                 _6 : {
518                         SER : {start : 16, len : 1, def : ' ' },
519                 }
520         },
521 };
522
523 function recordType (rec) {
524         var _l = rec.leader.toString();
525
526         var _t = _l.substr(ff_pos.Type.ldr.BKS.start, ff_pos.Type.ldr.BKS.len);
527         var _b = _l.substr(ff_pos.BLvl.ldr.BKS.start, ff_pos.BLvl.ldr.BKS.len);
528
529         for (var t in rec_type) {
530                 if (_t.match(rec_type[t].Type) && _b.match(rec_type[t].BLvl)) {
531                         document.getElementById('recordTypeLabel').value = t;
532                         _record_type = t;
533                         return t;
534                 }
535         }
536 }
537
538 function toggleFFE () {
539         var grid = document.getElementById('leaderGrid');
540         if (grid.hidden) {
541                 grid.hidden = false;
542         } else {
543                 grid.hidden = true;
544         }
545         return true;
546 }
547
548 function changeFFEditor (type) {
549         var grid = document.getElementById('leaderGrid');
550         grid.setAttribute('type',type);
551 }
552
553 function fillFixedFields (rec) {
554         var grid = document.getElementById('leaderGrid');
555
556         var rtype = _record_type;
557
558         var _l = rec.leader.toString();
559         var _6 = rec.controlfield.(@tag=='006').toString();
560         var _7 = rec.controlfield.(@tag=='007').toString();
561         var _8 = rec.controlfield.(@tag=='008').toString();
562
563         var list = [];
564         var pre_list = grid.getElementsByTagName('label');
565         for (var i in pre_list) {
566                 if ( pre_list[i].getAttribute && pre_list[i].getAttribute('set').indexOf(grid.getAttribute('type')) > -1 ) {
567                         list.push( pre_list[i] );
568                 }
569         }
570
571         for (var i in list) {
572                 var name = list[i].getAttribute('name');
573
574                 if (!ff_pos[name])
575                         continue;
576
577                 var value = '';
578                 if ( ff_pos[name].ldr && ff_pos[name].ldr[rtype] )
579                         value = _l.substr(ff_pos[name].ldr[rtype].start, ff_pos[name].ldr[rtype].len);
580
581                 if ( ff_pos[name]._8 && ff_pos[name]._8[rtype] )
582                         value = _8.substr(ff_pos[name]._8[rtype].start, ff_pos[name]._8[rtype].len);
583
584                 if ( !value && ff_pos[name]._6 && ff_pos[name]._6[rtype] )
585                         value = _6.substr(ff_pos[name]._6[rtype].start, ff_pos[name]._6[rtype].len);
586
587                 if ( ff_pos[name]._7 && ff_pos[name]._7[rtype] )
588                         value = _7.substr(ff_pos[name]._7[rtype].start, ff_pos[name]._7[rtype].len);
589                 
590                 if (!value) {
591                         var d;
592                         var p;
593                         if (ff_pos[name].ldr && ff_pos[name].ldr[rtype]) {
594                                 d = ff_pos[name].ldr[rtype].def;
595                                 p = 'ldr';
596                         }
597
598                         if (ff_pos[name]._8 && ff_pos[name]._8[rtype]) {
599                                 d = ff_pos[name]._8[rtype].def;
600                                 p = '_8';
601                         }
602
603                         if (!value && ff_pos[name]._6 && ff_pos[name]._6[rtype]) {
604                                 d = ff_pos[name]._6[rtype].def;
605                                 p = '_6';
606                         }
607
608                         if (ff_pos[name]._7 && ff_pos[name]._7[rtype]) {
609                                 d = ff_pos[name]._7[rtype].def;
610                                 p = '_7';
611                         }
612
613                         if (!value) {
614                                 for (var j = 0; j < ff_pos[name][p][rtype].len; j++) {
615                                         value += d;
616                                 }
617                         }
618                 }
619
620                 list[i].nextSibling.value = value;
621         }
622
623         return true;
624 }
625
626 function updateFixedFields (element) {
627         var grid = document.getElementById('leaderGrid');
628         var recGrid = document.getElementById('recGrid');
629
630         var rtype = _record_type;
631         var new_value = element.value;
632
633         var parts = {
634                 ldr : _record.leader,
635                 _6 : _record.controlfield.(@tag=='006'),
636                 _7 : _record.controlfield.(@tag=='007'),
637                 _8 : _record.controlfield.(@tag=='008'),
638         };
639
640         var name = element.getAttribute('name');
641         for (var i in ff_pos[name]) {
642
643                 if (!ff_pos[name][i][rtype]) continue;
644                 if (!parts[i]) continue;
645
646                 var before = parts[i].substr(0, ff_pos[name][i][rtype].start);
647                 var after = parts[i].substr(ff_pos[name][i][rtype].start + ff_pos[name][i][rtype].len);
648
649                 for (var j = 0; new_value.length < ff_pos[name][i][rtype].len; j++) {
650                         new_value += ff_pos[name][i][rtype].def;
651                 }
652
653                 parts[i].setChildren( before + new_value + after );
654                 recGrid.getElementsByAttribute('tag',i)[0].lastChild.value = parts[i].toString();
655         }
656
657         return true;
658 }
659
660 function marcLeader (leader) {
661         var row = createRow(
662                 { class : 'marcLeaderRow',
663                   tag : 'ldr' },
664                 createLabel(
665                         { value : 'LDR',
666                           class : 'marcTag',
667                           tooltiptext : "MARC Leader" } ),
668                 createLabel(
669                         { value : '',
670                           class : 'marcInd1' } ),
671                 createLabel(
672                         { value : '',
673                           class : 'marcInd2' } ),
674                 createLabel(
675                         { value : leader.text(),
676                           class : 'marcLeader' } )
677         );
678
679         return row;
680 }
681
682 function marcControlfield (field) {
683         tagname = field.@tag.toString().substr(2);
684         var row = createRow(
685                 { class : 'marcControlfieldRow',
686                   tag : '_' + tagname },
687                 createLabel(
688                         { value : field.@tag,
689                           class : 'marcTag',
690                           onmouseover : 'getTooltip(this, "tag");',
691                           tooltipid : 'tag' + field.@tag } ),
692                 createLabel(
693                         { value : field.@ind1,
694                           class : 'marcInd1',
695                           onmouseover : 'getTooltip(this, "ind1");',
696                           tooltipid : 'tag' + field.@tag + 'ind1val' + field.@ind1 } ),
697                 createLabel(
698                         { value : field.@ind2,
699                           class : 'marcInd2',
700                           onmouseover : 'getTooltip(this, "ind2");',
701                           tooltipid : 'tag' + field.@tag + 'ind2val' + field.@ind2 } ),
702                 createLabel(
703                         { value : field.text(),
704                           class : 'marcControlfield' } )
705         );
706
707         return row;
708 }
709
710 function stackSubfields(checkbox) {
711         var list = document.getElementsByAttribute('name','sf_box');
712
713         var o = 'vertical';
714         if (checkbox.checked) o = 'horizontal';
715         
716         for (var i in list) {
717                 if (list[i]) list[i].setAttribute('orient',o);
718         }
719 }
720
721 function marcDatafield (field) {
722         var row = createRow(
723                 { class : 'marcDatafieldRow' },
724                 createMARCTextbox(
725                         field.@tag,
726                         { value : field.@tag,
727                           class : 'plain marcTag',
728                           name : 'marcTag',
729                           context : 'tags_popup',
730                           oninput : 'if (this.value.length == 3) { this.nextSibling.focus(); }',
731                           size : 3,
732                           maxlength : 3,
733                           onmouseover : 'current_focus = this; getTooltip(this, "tag");' } ),
734                 createMARCTextbox(
735                         field.@ind1,
736                         { value : field.@ind1,
737                           class : 'plain marcInd1',
738                           name : 'marcInd1',
739                           oninput : 'if (this.value.length == 1) { this.nextSibling.focus(); }',
740                           size : 1,
741                           maxlength : 1,
742                           onmouseover : 'current_focus = this; getContextMenu(this, "ind1"); getTooltip(this, "ind1");',
743                           oncontextmenu : 'getContextMenu(this, "ind1");' } ),
744                 createMARCTextbox(
745                         field.@ind2,
746                         { value : field.@ind2,
747                           class : 'plain marcInd2',
748                           name : 'marcInd2',
749                           oninput : 'if (this.value.length == 1) { this.nextSibling.firstChild.firstChild.focus(); }',
750                           size : 1,
751                           maxlength : 1,
752                           onmouseover : 'current_focus = this; getContextMenu(this, "ind2"); getTooltip(this, "ind2");',
753                           oncontextmenu : 'getContextMenu(this, "ind2");' } ),
754                 createHbox({ name : 'sf_box' })
755         );
756
757         if (!current_focus && field.@tag == '') current_focus = row.childNodes[0];
758         if (!current_focus && field.@ind1 == '') current_focus = row.childNodes[1];
759         if (!current_focus && field.@ind2 == '') current_focus = row.childNodes[2];
760
761         var sf_box = row.lastChild;
762         if (document.getElementById('stackSubfields').checked)
763                 sf_box.setAttribute('orient','vertical');
764
765         sf_box.addEventListener(
766                 'click',
767                 function (e) {
768                         if (sf_box === e.target) {
769                                 sf_box.lastChild.lastChild.focus();
770                         } else if (e.target.parentNode === sf_box) {
771                                 e.target.lastChild.focus();
772                         }
773                 },
774                 false
775         );
776
777
778         for (var i in field.subfield) {
779                 var sf = field.subfield[i];
780                 sf_box.appendChild(
781                         marcSubfield(sf)
782                 );
783
784                 if (sf.@code == '' && (!current_focus || current_focus.className.match(/Ind/)))
785                         current_focus = sf_box.lastChild.childNodes[1];
786         }
787
788         return row;
789 }
790
791 function marcSubfield (sf) {                    
792         return createHbox(
793                 { class : 'marcSubfieldBox' },
794                 createLabel(
795                         { value : "\u2021",
796                           class : 'plain marcSubfieldDelimiter',
797                           onmouseover : 'getTooltip(this.nextSibling, "subfield");',
798                           oncontextmenu : 'getContextMenu(this.nextSibling, "subfield");',
799                           //onclick : 'this.nextSibling.focus();',
800                           onfocus : 'this.nextSibling.focus();',
801                           size : 2 } ),
802                 createMARCTextbox(
803                         sf.@code,
804                         { value : sf.@code,
805                           class : 'plain marcSubfieldCode',
806                           name : 'marcSubfieldCode',
807                           onmouseover : 'current_focus = this; getContextMenu(this, "subfield"); getTooltip(this, "subfield");',
808                           oncontextmenu : 'getContextMenu(this, "subfield");',
809                           oninput : 'if (this.value.length == 1) { this.nextSibling.focus(); }',
810                           size : 2,
811                           maxlength : 1 } ),
812                 createMARCTextbox(
813                         sf,
814                         { value : sf.text(),
815                           class : 'plain marcSubfield', 
816                           onmouseover : 'getTooltip(this, "subfield");',
817                           size : new String(sf.text()).length + 2,
818                           oninput : "this.setAttribute('size', this.value.length + 2);",
819                         } )
820         );
821 }
822
823 function loadRecord(rec) {
824         _record = rec;
825         var grid_rows = document.getElementById('recGrid').lastChild;
826
827         grid_rows.appendChild( marcLeader( rec.leader ) );
828
829         for (var i in rec.controlfield) {
830                 grid_rows.appendChild( marcControlfield( rec.controlfield[i] ) );
831         }
832
833         for (var i in rec.datafield) {
834                 grid_rows.appendChild( marcDatafield( rec.datafield[i] ) );
835         }
836
837         grid_rows.getElementsByAttribute('class','marcDatafieldRow')[0].firstChild.focus();
838         changeFFEditor(recordType(rec));
839         fillFixedFields(rec);
840 }
841
842 var context_menus = createComplexXULElement('popupset');
843 document.documentElement.appendChild( context_menus );
844
845 var tag_menu = createPopup({position : 'after_start', id : 'tags_popup'});
846 context_menus.appendChild( tag_menu );
847
848 tag_menu.appendChild(
849         createMenuitem(
850                 { label : 'Add Row',
851                   oncommand : 
852                         'var e = document.createEvent("KeyEvents");' +
853                         'e.initKeyEvent("keypress",1,1,null,1,0,0,0,13,0);' +
854                         'current_focus.inputField.dispatchEvent(e);',
855                  }
856         )
857 );
858
859 tag_menu.appendChild(
860         createMenuitem(
861                 { label : 'Remove Row',
862                   oncommand : 
863                         'var e = document.createEvent("KeyEvents");' +
864                         'e.initKeyEvent("keypress",1,1,null,1,0,0,0,46,0);' +
865                         'current_focus.inputField.dispatchEvent(e);',
866                 }
867         )
868 );
869
870 tag_menu.appendChild( createComplexXULElement( 'separator' ) );
871
872
873
874 function genToolTips () {
875         for (var i in bib_data.field) {
876                 var f = bib_data.field[i];
877         
878                 tag_menu.appendChild(
879                         createMenuitem(
880                                 { label : f.@tag,
881                                   oncommand : 
882                                         'current_focus.value = "' + f.@tag + '";' +
883                                         'var e = document.createEvent("MutationEvents");' +
884                                         'e.initMutationEvent("change",1,1,null,0,0,0,0);' +
885                                         'current_focus.inputField.dispatchEvent(e);',
886                                   disabled : f.@tag < '010' ? "true" : "false",
887                                   tooltiptext : f.description }
888                         )
889                 );
890         
891                 var i1_popup = createPopup({position : 'after_start', id : 't' + f.@tag + 'i1' });
892                 context_menus.appendChild( i1_popup );
893         
894                 var i2_popup = createPopup({position : 'after_start', id : 't' + f.@tag + 'i2' });
895                 context_menus.appendChild( i2_popup );
896         
897                 var sf_popup = createPopup({position : 'after_start', id : 't' + f.@tag + 'sf' });
898                 context_menus.appendChild( sf_popup );
899         
900                 tooltip_hash['tag' + f.@tag] = f.description;
901                 for (var j in f.indicator) {
902                         var ind = f.indicator[j];
903                         tooltip_hash['tag' + f.@tag + 'ind' + ind.@position + 'val' + ind.@value] = ind.description;
904         
905                         if (ind.@position == 1) {
906                                 i1_popup.appendChild(
907                                         createMenuitem(
908                                                 { label : ind.@value,
909                                                   oncommand : 
910                                                         'current_focus.value = "' + ind.@value + '";' +
911                                                         'var e = document.createEvent("MutationEvents");' +
912                                                         'e.initMutationEvent("change",1,1,null,0,0,0,0);' +
913                                                         'current_focus.inputField.dispatchEvent(e);',
914                                                   tooltiptext : ind.description }
915                                         )
916                                 );
917                         }
918         
919                         if (ind.@position == 2) {
920                                 i2_popup.appendChild(
921                                         createMenuitem(
922                                                 { label : ind.@value,
923                                                   oncommand : 
924                                                         'current_focus.value = "' + ind.@value + '";' +
925                                                         'var e = document.createEvent("MutationEvents");' +
926                                                         'e.initMutationEvent("change",1,1,null,0,0,0,0);' +
927                                                         'current_focus.inputField.dispatchEvent(e);',
928                                                   tooltiptext : ind.description }
929                                         )
930                                 );
931                         }
932                 }
933         
934                 for (var j in f.subfield) {
935                         var sf = f.subfield[j];
936                         tooltip_hash['tag' + f.@tag + 'sf' + sf.@code] = sf.description;
937         
938                         sf_popup.appendChild(
939                                 createMenuitem(
940                                         { label : sf.@code,
941                                           oncommand : 
942                                                 'current_focus.value = "' + sf.@code + '";' +
943                                                 'var e = document.createEvent("MutationEvents");' +
944                                                 'e.initMutationEvent("change",1,1,null,0,0,0,0);' +
945                                                 'current_focus.inputField.dispatchEvent(e);',
946                                           tooltiptext : sf.description,
947                                         }
948                                 )
949                         );
950                 }
951         }
952 }
953
954 var p = createComplexXULElement('popupset');
955 document.documentElement.appendChild( p );
956
957 function getTooltip (target, type) {
958
959         var tt = '';
960         if (type == 'subfield')
961                 tt = 'tag' + target.parentNode.parentNode.parentNode.firstChild.value + 'sf' + target.parentNode.childNodes[1].value;
962
963         if (type == 'ind1')
964                 tt = 'tag' + target.parentNode.firstChild.value + 'ind1val' + target.value;
965
966         if (type == 'ind2')
967                 tt = 'tag' + target.parentNode.firstChild.value + 'ind2val' + target.value;
968
969         if (type == 'tag')
970                 tt = 'tag' + target.parentNode.firstChild.value;
971
972         if (!document.getElementById( tt )) {
973                 p.appendChild(
974                         createTooltip(
975                                 { id : tt,
976                                   flex : "1",
977                                   orient : 'vertical',
978                                   onpopupshown : 'this.width = this.firstChild.boxObject.width + 10; this.height = this.firstChild.boxObject.height + 10;',
979                                   class : 'tooltip' },
980                                 createDescription({}, document.createTextNode( tooltip_hash[tt] ) )
981                         )
982                 );
983         }
984
985         target.tooltip = tt;
986         return true;
987 }
988
989 function getContextMenu (target, type) {
990
991         var tt = '';
992         if (type == 'subfield')
993                 tt = 't' + target.parentNode.parentNode.parentNode.firstChild.value + 'sf';
994
995         if (type == 'ind1')
996                 tt = 't' + target.parentNode.firstChild.value + 'i1';
997
998         if (type == 'ind2')
999                 tt = 't' + target.parentNode.firstChild.value + 'i2';
1000
1001         target.setAttribute('context', tt);
1002         return true;
1003 }
1004
1005