Monograph Parts; Unified vol/copy wizard; Call Number affixes; Instant Detail
[evergreen-equinox.git] / Open-ILS / xul / staff_client / chrome / content / util / widgets.js
1 dump('entering util/widgets.js\n');
2
3 if (typeof util == 'undefined') var util = {};
4 util.widgets = {};
5
6 util.widgets.EXPORT_OK    = [ 
7     'get',
8     'apply',
9     'save_xml',
10     'serialize_node',
11     'xul_from_string',
12     'store_disable',
13     'restore_disable',
14     'disable',
15     'get_list_from_tree_selection',
16     'disable_accesskeys_in_node_and_children', 
17     'enable_accesskeys_in_node_and_children', 
18     'remove_children',
19     'make_grid',
20     'make_menulist',
21     'insertAfter',
22     'apply_vertical_tab_on_enter_handler',
23     'vertical_tab',
24     'click',
25     'dispatch',
26     'stop_event',
27     'set_text',
28     'save_attributes',
29     'load_attributes',
30     'find_descendants_by_name'
31 ];
32 util.widgets.EXPORT_TAGS    = { ':all' : util.widgets.EXPORT_OK };
33
34 util.widgets.get = function(e) {
35     if (typeof e == 'object') {
36         return e;
37     } else {
38         return document.getElementById(e);
39     }
40 }
41
42 util.widgets.apply = function(e,attr,attr_value,f) {
43     var node = util.widgets.get(e);
44     var nl = node.getElementsByAttribute(attr,attr_value);
45     for (var i = 0; i < nl.length; i++) {
46         f( nl[i] );
47     }
48 }
49
50 util.widgets.save_xml = function (filename,node) {
51     try { 
52         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
53
54         JSAN.use('util.file'); var file = new util.file(filename);
55
56         node = util.widgets.get(node);
57         var xml = util.widgets.serialize_node(node);
58
59         file.write_content('truncate',xml);
60         file.close();
61     } catch(E) {
62         alert('Error in util.widgets.save_xml: ' + E);
63     }
64 }
65
66 util.widgets.serialize_node = function(node) {
67     var serializer = new XMLSerializer();
68     var xml = serializer.serializeToString(node);
69     return xml;
70 }
71
72 util.widgets.xul_from_string = function(xml) {
73     var parser = new DOMParser(); 
74     var doc = parser.parseFromString(xml, "text/xml"); 
75     var node = doc.documentElement;
76     return node;
77 }
78
79 util.widgets.store_disable = function() {
80     for (var i = 0; i < arguments.length; i++) {
81         var e = util.widgets.get( arguments[i] );
82         e.setAttribute('_disabled',e.getAttribute('disabled'));
83     }
84 }
85
86 util.widgets.restore_disable = function() {
87     for (var i = 0; i < arguments.length; i++) {
88         var e = util.widgets.get( arguments[i] );
89         e.setAttribute('disabled',e.getAttribute('_disabled'));
90     }
91 }
92
93 util.widgets.disable = function() {
94     for (var i = 0; i < arguments.length; i++) {
95         var e = util.widgets.get( arguments[i] );
96         e.setAttribute('disabled',true);
97     }
98 }
99
100 util.widgets.click = function(e) {
101     var evt = document.createEvent("MouseEvent");
102     evt.initMouseEvent( "click", true, true, window, 0, 0, 0, 0, 0, false,false,false,false,0,null);
103     util.widgets.get(e).dispatchEvent(evt);
104 }
105
106 util.widgets.dispatch = function(ev,el) {
107     var evt = document.createEvent("Events");
108     //var evt = document.createEvent();
109     evt.initEvent( ev, true, true );
110     util.widgets.get(el).dispatchEvent(evt);
111 }
112
113 util.widgets.make_menulist = function( items, dvalue ) {
114     var menulist = document.createElement('menulist');
115     var menupopup = document.createElement('menupopup'); menulist.appendChild(menupopup);
116     for (var i = 0; i < items.length; i++) {
117         if (typeof items[i] == 'undefined') { continue; }
118         var label = items[i][0]; var value = items[i][1]; var disabled = items[i][2]; var indent = items[i][3];
119         if (indent) {
120             for (var j = 0; j < Number(indent); j++) {
121                 //label = ' ' + label;
122             }
123         }
124         var menuitem = document.createElement('menuitem'); menupopup.appendChild(menuitem);
125         menuitem.setAttribute('label',label);
126         menuitem.setAttribute('value',value);
127         if (indent) {
128             menuitem.setAttribute('style','font-family: monospace; padding-left: ' + indent + 'em;');
129         } else {
130             menuitem.setAttribute('style','font-family: monospace;');
131         }
132         if ( (disabled == true) || (disabled == "true") ) {
133             menuitem.disabled = true;
134             menuitem.setAttribute('disabled','true');
135         }
136     }
137     if (typeof dvalue != 'undefined') {
138         menulist.setAttribute('value',dvalue);
139     }
140     return menulist;
141 }
142
143 util.widgets.make_grid = function( cols ) {
144     var grid = document.createElement('grid');
145     var columns = document.createElement('columns'); grid.appendChild(columns);
146     for (var i = 0; i < cols.length; i++) {
147         var column = document.createElement('column'); columns.appendChild(column);
148         for (var j in cols[i]) {
149             column.setAttribute(j,cols[i][j]);
150         }
151     }
152     var rows = document.createElement('rows'); grid.appendChild(rows);
153     return grid;
154 }
155
156 util.widgets.get_list_from_tree_selection = function(tree_w) {
157     var hitlist;
158     var tree = util.widgets.get(tree_w);
159     var list = [];
160     var start = new Object();
161     var end = new Object();
162     var numRanges = tree.view.selection.getRangeCount();
163     for (var t=0; t<numRanges; t++){
164         tree.view.selection.getRangeAt(t,start,end);
165         for (var v=start.value; v<=end.value; v++){
166             var i = tree.contentView.getItemAtIndex(v);
167             list.push( i );
168         }
169     }
170     return list;
171 }
172
173 util.widgets.remove_children = function() {
174     for (var i = 0; i < arguments.length; i++) {
175         var e = util.widgets.get( arguments[i] );
176         while(e.lastChild) e.removeChild( e.lastChild );
177     }
178 }
179
180 util.widgets.disable_accesskeys_in_node_and_children = function( node ) {
181     return; /* what was I doing here? */
182     if (node.getAttribute('accesskey')) {
183         node.setAttribute('oldaccesskey', node.getAttribute('accesskey'));
184         node.setAttribute('accesskey',''); node.accessKey = '';
185     }
186     for (var i = 0; i < node.childNodes.length; i++) {
187         util.widgets.disable_accesskeys_in_node_and_children( node.childNodes[i] );
188     }
189     dump('- node = <' + node.id + '> accesskey = <' + node.accessKey + '> accesskey = <' + node.getAttribute('accesskey') + '> oldaccesskey = <' + node.getAttribute('oldaccesskey') + '>\n');
190 }
191
192 util.widgets.enable_accesskeys_in_node_and_children = function( node ) {
193     return; /* what was I doing here? */
194     if (node.getAttribute('oldaccesskey')) {
195         node.setAttribute('accesskey', node.getAttribute('oldaccesskey'));
196         node.accessKey = node.getAttribute('oldaccesskey'); 
197         node.setAttribute('oldaccesskey','');
198     }
199     for (var i = 0; i < node.childNodes.length; i++) {
200         util.widgets.enable_accesskeys_in_node_and_children( node.childNodes[i] );
201     }
202     dump('+ node = <' + node.id + '> accesskey = <' + node.accessKey + '> accesskey = <' + node.getAttribute('accesskey') + '> oldaccesskey = <' + node.getAttribute('oldaccesskey') + '>\n');
203 }
204
205 util.widgets.insertAfter = function(parent_node,new_node,sibling_node) {
206     sibling_node = sibling_node.nextSibling;
207     if (sibling_node) {
208         parent_node.insertBefore(new_node,sibling_node);
209     } else {
210         parent_node.appendChild(new_node);
211     }
212 }
213
214 util.widgets.apply_vertical_tab_on_enter_handler = function(node,onfailure,no_enter_func) {
215     try {
216         node.addEventListener(
217             'keypress',
218             function(ev) {
219                 dump('keypress: ev.target.tagName = ' + ev.target.tagName 
220                     + ' ev.target.nodeName = ' + ev.target.nodeName 
221                     + ' ev.keyCode = ' + ev.keyCode 
222                     + ' ev.charCode = ' + ev.charCode + '\n');
223                 if (ev.keyCode == 13) {
224                     dump('trying vertical tab\n');
225                     if (util.widgets.vertical_tab(ev.target)) {
226                         ev.preventDefault(); ev.stopPropagation();
227                         return true;
228                     } else {
229                         dump('keypress: attempting onfailure\n');
230                         if (typeof onfailure == 'function') return onfailure(ev);
231                         return false;
232                     }
233                 } else {
234                     if (typeof no_enter_func == 'function') {
235                         no_enter_func(ev);
236                     }
237                 }
238             },
239             false
240         );
241     } catch(E) {
242         alert(E);
243     }
244 }
245
246 util.widgets.vertical_tab = function(node) {
247     try {
248         var rel_vert_pos = node.getAttribute('rel_vert_pos') || 0;
249         dump('vertical_tab -> node = ' + node.nodeName + ' rel_vert_pos = ' + rel_vert_pos + '\n');
250
251         var nl = document.getElementsByTagName( node.nodeName );
252
253         var found_self = false; var next_node; var max_rel_vert_pos = 0;
254         for (var i = 0; i < nl.length; i++) {
255
256             var candidate_node = nl[i];
257             var test_rel_vert_pos = candidate_node.getAttribute('rel_vert_pos') || 0;
258
259             if (found_self && !next_node && (test_rel_vert_pos == rel_vert_pos) && !candidate_node.disabled) {
260             
261                 next_node = candidate_node;
262
263             }
264             if (candidate_node == node) found_self = true;
265
266             if (test_rel_vert_pos > max_rel_vert_pos) max_rel_vert_pos = test_rel_vert_pos;
267         }
268
269         dump('intermediate: next_node = ' + next_node + ' max_rel_vert_pos = ' + max_rel_vert_pos + '\n');
270
271         if (!next_node) {
272
273             found_self = false;
274             for (var next_pos = rel_vert_pos; next_pos <= max_rel_vert_pos; next_pos++) {
275
276                 for (var i = 0; i < nl.length; i++) {
277                     var candidate_node = nl[i];
278                     var test_rel_vert_pos = candidate_node.getAttribute('rel_vert_pos') || 0;
279
280                     if (found_self && !next_node && (test_rel_vert_pos == next_pos) && !candidate_node.disabled ) {
281                         next_node = candidate_node;
282                     }
283
284                     if (candidate_node == node) found_self = true;
285                 }
286
287             }
288
289         }
290
291         if (next_node) {
292             dump('focusing\n');
293             next_node.focus();
294         }
295         return next_node;
296     } catch(E) {
297         alert(E);
298     }
299 }
300
301 util.widgets.stop_event = function(ev) {
302     ev.preventDefault();
303     return false;
304 }
305
306 util.widgets.set_text = function(n,t) {
307     n = util.widgets.get(n);
308     switch(n.nodeName) {
309         case 'button' :
310         case 'caption' :
311             n.setAttribute('label',t);
312         break;
313         case 'label' : 
314             n.setAttribute('value',t); 
315         break;
316         case 'description' : 
317         case 'H1': case 'H2': case 'H3': case 'H4': case 'SPAN': case 'P': case 'BLOCKQUOTE':
318             util.widgets.remove_children(n); 
319             n.appendChild( document.createTextNode(t) );
320         break;
321         case 'textbox' :
322             n.value = t; n.setAttribute('value',t);
323         break;
324         default:
325             alert("FIXME: util.widgets.set_text doesn't know how to handle " + n.nodeName);
326         break;
327     }
328 }
329
330 util.widgets.get_text = function(n) {
331     n = util.widgets.get(n);
332     switch(n.nodeName) {
333         case 'button' :
334         case 'caption' :
335             return n.getAttribute('label');
336         break;
337         case 'label' : 
338             return n.getAttribute('value'); 
339         break;
340         case 'description' : 
341         case 'H1': case 'H2': case 'H3': case 'H4': case 'SPAN': case 'P': case 'BLOCKQUOTE':
342             return n.textContent;
343         break;
344         case 'textbox' :
345             return n.value;
346         break;
347         default:
348             alert("FIXME: util.widgets.get_text doesn't know how to handle " + n.nodeName);
349             return null;
350         break;
351     }
352 }
353
354 util.widgets.save_attributes = function (file,ids_attrs) {
355     try {
356         var blob = {};
357         for (var element_id in ids_attrs) {
358             var attribute_list = ids_attrs[ element_id ];
359             if (! blob[ element_id ] ) blob[ element_id ] =  {};
360             var x = document.getElementById( element_id );
361             if (x) {
362                 for (var j = 0; j < attribute_list.length; j++) {
363                     blob[ element_id ][ attribute_list[j] ] = x.getAttribute( attribute_list[j] );
364                 }
365             } else {
366                 dump('Error in util.widgets.save_attributes('+file._file.path+','+js2JSON(ids_attrs)+'):\n');
367                 dump('\telement_id = ' + element_id + '\n');
368             }
369         }
370         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
371         //FIXME - WHY DOES THIS NOT WORK?// JSAN.use('util.file'); var file = new util.file(filename);
372         file.set_object(blob); file.close();
373     } catch(E) {
374         alert('Error saving preferences: ' + E);
375     }
376 }
377
378 util.widgets.load_attributes = function (file) {        
379     try {
380         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
381         //FIXME - WHY DOES THIS NOT WORK?// JSAN.use('util.file'); var file = new util.file(filename);
382         if (file._file.exists()) {
383             var blob = file.get_object(); file.close();
384             for (var element_id in blob) {
385                 for (var attribute in blob[ element_id ]) {
386                     var x = document.getElementById( element_id );
387                     if (x) {
388                         if (x.nodeName == 'menulist' && attribute == 'value') {
389                             var popup = x.firstChild;
390                             var children = popup.childNodes;
391                             for (var i = 0; i < children.length; i++) {
392                                 if (children[i].getAttribute('value') == blob[ element_id ][ attribute ]) {
393                                     dump('setting ' + x.nodeName + ' ' + element_id + ' @value to ' + blob[ element_id ][ attribute ] + '\n' );
394                                     x.setAttribute(attribute, blob[ element_id ][ attribute ]);
395                                 }
396                             }
397                         } else {
398                             dump('setting ' + x.nodeName + ' ' + element_id + ' @value to ' + blob[ element_id ][ attribute ] + '\n');
399                             x.setAttribute(attribute, blob[ element_id ][ attribute ]);
400                         }
401                     } else {
402                         dump('Error in util.widgets.load_attributes('+file._file.path+'):\n');
403                         dump('\telement_id = ' + element_id + '\n');
404                         dump('\tattribute = ' + attribute + '\n');
405                         dump('\tblob[id][attr] = ' + blob[element_id][attribute] + '\n');
406                     }
407                 }
408             }
409             return blob;
410         }
411         return {};
412     } catch(E) {
413         alert('Error loading preferences: ' + E);
414     }
415 }
416
417 util.widgets.addProperty = function(e,c) {
418         if(!e || !c) return;
419
420         var prop_class_string = e.getAttribute('properties');
421         var prop_class_array;
422
423         if(prop_class_string)
424                 prop_class_array = prop_class_string.split(/\s+/);
425
426         var string_ip = ""; /*strip out nulls*/
427         for (var prop_class in prop_class_array) {
428                 if (prop_class_array[prop_class] == c) { return; }
429                 if(prop_class_array[prop_class] !=null)
430                         string_ip += prop_class_array[prop_class] + " ";
431         }
432         string_ip += c;
433         e.setAttribute('properties',string_ip);
434 }
435
436 util.widgets.removeProperty = function(e, c) {
437         if(!e || !c) return;
438
439         var prop_class_string = '';
440
441         var prop_class_array = e.getAttribute('properties');
442         if( prop_class_array )
443                 prop_class_array = prop_class_array.split(/\s+/);
444
445         var first = 1;
446         for (var prop_class in prop_class_array) {
447                 if (prop_class_array[prop_class] != c) {
448                         if (first == 1) {
449                                 prop_class_string = prop_class_array[prop_class];
450                                 first = 0;
451                         } else {
452                                 prop_class_string = prop_class_string + ' ' +
453                                         prop_class_array[prop_class];
454                         }
455                 }
456         }
457         e.setAttribute('properties', prop_class_string);
458 }
459
460 util.widgets.find_descendants_by_name = function(top_node,name) {
461     top_node = util.widgets.get(top_node);
462     if (!top_node) { return []; }
463     return top_node.getElementsByAttribute('name',name);
464 }
465
466 dump('exiting util/widgets.js\n');