]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/util/widgets.js
358519a09ab544d11265910b51b2ac60488b3d94
[working/Evergreen.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     menulist.setAttribute('value',dvalue);
138     return menulist;
139 }
140
141 util.widgets.make_grid = function( cols ) {
142     var grid = document.createElement('grid');
143     var columns = document.createElement('columns'); grid.appendChild(columns);
144     for (var i = 0; i < cols.length; i++) {
145         var column = document.createElement('column'); columns.appendChild(column);
146         for (var j in cols[i]) {
147             column.setAttribute(j,cols[i][j]);
148         }
149     }
150     var rows = document.createElement('rows'); grid.appendChild(rows);
151     return grid;
152 }
153
154 util.widgets.get_list_from_tree_selection = function(tree_w) {
155     var hitlist;
156     var tree = util.widgets.get(tree_w);
157     var list = [];
158     var start = new Object();
159     var end = new Object();
160     var numRanges = tree.view.selection.getRangeCount();
161     for (var t=0; t<numRanges; t++){
162         tree.view.selection.getRangeAt(t,start,end);
163         for (var v=start.value; v<=end.value; v++){
164             var i = tree.contentView.getItemAtIndex(v);
165             list.push( i );
166         }
167     }
168     return list;
169 }
170
171 util.widgets.remove_children = function() {
172     for (var i = 0; i < arguments.length; i++) {
173         var e = util.widgets.get( arguments[i] );
174         while(e.lastChild) e.removeChild( e.lastChild );
175     }
176 }
177
178 util.widgets.disable_accesskeys_in_node_and_children = function( node ) {
179     return; /* what was I doing here? */
180     if (node.getAttribute('accesskey')) {
181         node.setAttribute('oldaccesskey', node.getAttribute('accesskey'));
182         node.setAttribute('accesskey',''); node.accessKey = '';
183     }
184     for (var i = 0; i < node.childNodes.length; i++) {
185         util.widgets.disable_accesskeys_in_node_and_children( node.childNodes[i] );
186     }
187     dump('- node = <' + node.id + '> accesskey = <' + node.accessKey + '> accesskey = <' + node.getAttribute('accesskey') + '> oldaccesskey = <' + node.getAttribute('oldaccesskey') + '>\n');
188 }
189
190 util.widgets.enable_accesskeys_in_node_and_children = function( node ) {
191     return; /* what was I doing here? */
192     if (node.getAttribute('oldaccesskey')) {
193         node.setAttribute('accesskey', node.getAttribute('oldaccesskey'));
194         node.accessKey = node.getAttribute('oldaccesskey'); 
195         node.setAttribute('oldaccesskey','');
196     }
197     for (var i = 0; i < node.childNodes.length; i++) {
198         util.widgets.enable_accesskeys_in_node_and_children( node.childNodes[i] );
199     }
200     dump('+ node = <' + node.id + '> accesskey = <' + node.accessKey + '> accesskey = <' + node.getAttribute('accesskey') + '> oldaccesskey = <' + node.getAttribute('oldaccesskey') + '>\n');
201 }
202
203 util.widgets.insertAfter = function(parent_node,new_node,sibling_node) {
204     sibling_node = sibling_node.nextSibling;
205     if (sibling_node) {
206         parent_node.insertBefore(new_node,sibling_node);
207     } else {
208         parent_node.appendChild(new_node);
209     }
210 }
211
212 util.widgets.apply_vertical_tab_on_enter_handler = function(node,onfailure) {
213     try {
214         node.addEventListener(
215             'keypress',
216             function(ev) {
217                 dump('keypress: ev.target.tagName = ' + ev.target.tagName 
218                     + ' ev.target.nodeName = ' + ev.target.nodeName 
219                     + ' ev.keyCode = ' + ev.keyCode 
220                     + ' ev.charCode = ' + ev.charCode + '\n');
221                 if (ev.keyCode == 13) {
222                     dump('trying vertical tab\n');
223                     if (util.widgets.vertical_tab(ev.target)) {
224                         ev.preventDefault(); ev.stopPropagation();
225                         return true;
226                     } else {
227                         if (typeof onfailure == 'function') return onfailure(ev);
228                         return false;
229                     }
230                 }
231             },
232             false
233         );
234     } catch(E) {
235         alert(E);
236     }
237 }
238
239 util.widgets.vertical_tab = function(node) {
240     try {
241         var rel_vert_pos = node.getAttribute('rel_vert_pos') || 0;
242         dump('vertical_tab -> node = ' + node.nodeName + ' rel_vert_pos = ' + rel_vert_pos + '\n');
243
244         var nl = document.getElementsByTagName( node.nodeName );
245
246         var found_self = false; var next_node; var max_rel_vert_pos = 0;
247         for (var i = 0; i < nl.length; i++) {
248
249             var candidate_node = nl[i];
250             var test_rel_vert_pos = candidate_node.getAttribute('rel_vert_pos') || 0;
251
252             if (found_self && !next_node && (test_rel_vert_pos == rel_vert_pos) && !candidate_node.disabled) {
253             
254                 next_node = candidate_node;
255
256             }
257             if (candidate_node == node) found_self = true;
258
259             if (test_rel_vert_pos > max_rel_vert_pos) max_rel_vert_pos = test_rel_vert_pos;
260         }
261
262         dump('intermediate: next_node = ' + next_node + ' max_rel_vert_pos = ' + max_rel_vert_pos + '\n');
263
264         if (!next_node) {
265
266             found_self = false;
267             for (var next_pos = rel_vert_pos; next_pos <= max_rel_vert_pos; next_pos++) {
268
269                 for (var i = 0; i < nl.length; i++) {
270                     var candidate_node = nl[i];
271                     var test_rel_vert_pos = candidate_node.getAttribute('rel_vert_pos') || 0;
272
273                     if (found_self && !next_node && (test_rel_vert_pos == next_pos) && !candidate_node.disabled ) {
274                         next_node = candidate_node;
275                     }
276
277                     if (candidate_node == node) found_self = true;
278                 }
279
280             }
281
282         }
283
284         if (next_node) {
285             dump('focusing\n');
286             next_node.focus();
287         }
288         return next_node;
289     } catch(E) {
290         alert(E);
291     }
292 }
293
294 util.widgets.stop_event = function(ev) {
295     ev.preventDefault();
296     return false;
297 }
298
299 util.widgets.set_text = function(n,t) {
300     n = util.widgets.get(n);
301     switch(n.nodeName) {
302         case 'button' :
303         case 'caption' :
304             n.setAttribute('label',t);
305         break;
306         case 'label' : 
307             n.setAttribute('value',t); 
308         break;
309         case 'description' : 
310         case 'H1': case 'H2': case 'H3': case 'H4': case 'SPAN': case 'P': case 'BLOCKQUOTE':
311             util.widgets.remove_children(n); 
312             n.appendChild( document.createTextNode(t) );
313         break;
314         case 'textbox' :
315             n.value = t; n.setAttribute('value',t);
316         break;
317         default:
318             alert("FIXME: util.widgets.set_text doesn't know how to handle " + n.nodeName);
319         break;
320     }
321 }
322
323 util.widgets.get_text = function(n) {
324     n = util.widgets.get(n);
325     switch(n.nodeName) {
326         case 'button' :
327         case 'caption' :
328             return n.getAttribute('label');
329         break;
330         case 'label' : 
331             return n.getAttribute('value'); 
332         break;
333         case 'description' : 
334         case 'H1': case 'H2': case 'H3': case 'H4': case 'SPAN': case 'P': case 'BLOCKQUOTE':
335             return n.textContent;
336         break;
337         case 'textbox' :
338             return n.value;
339         break;
340         default:
341             alert("FIXME: util.widgets.get_text doesn't know how to handle " + n.nodeName);
342             return null;
343         break;
344     }
345 }
346
347 util.widgets.save_attributes = function (file,ids_attrs) {
348     try {
349         var blob = {};
350         for (var element_id in ids_attrs) {
351             var attribute_list = ids_attrs[ element_id ];
352             if (! blob[ element_id ] ) blob[ element_id ] =  {};
353             var x = document.getElementById( element_id );
354             if (x) {
355                 for (var j = 0; j < attribute_list.length; j++) {
356                     blob[ element_id ][ attribute_list[j] ] = x.getAttribute( attribute_list[j] );
357                 }
358             } else {
359                 dump('Error in util.widgets.save_attributes('+file._file.path+','+js2JSON(ids_attrs)+'):\n');
360                 dump('\telement_id = ' + element_id + '\n');
361             }
362         }
363         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
364         //FIXME - WHY DOES THIS NOT WORK?// JSAN.use('util.file'); var file = new util.file(filename);
365         file.set_object(blob); file.close();
366     } catch(E) {
367         alert('Error saving preferences: ' + E);
368     }
369 }
370
371 util.widgets.load_attributes = function (file) {        
372     try {
373         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
374         //FIXME - WHY DOES THIS NOT WORK?// JSAN.use('util.file'); var file = new util.file(filename);
375         if (file._file.exists()) {
376             var blob = file.get_object(); file.close();
377             for (var element_id in blob) {
378                 for (var attribute in blob[ element_id ]) {
379                     var x = document.getElementById( element_id );
380                     if (x) {
381                         if (x.nodeName == 'menulist' && attribute == 'value') {
382                             var popup = x.firstChild;
383                             var children = popup.childNodes;
384                             for (var i = 0; i < children.length; i++) {
385                                 if (children[i].getAttribute('value') == blob[ element_id ][ attribute ]) {
386                                     dump('setting ' + x.nodeName + ' ' + element_id + ' @value to ' + blob[ element_id ][ attribute ] + '\n' );
387                                     x.setAttribute(attribute, blob[ element_id ][ attribute ]);
388                                 }
389                             }
390                         } else {
391                             dump('setting ' + x.nodeName + ' ' + element_id + ' @value to ' + blob[ element_id ][ attribute ] + '\n');
392                             x.setAttribute(attribute, blob[ element_id ][ attribute ]);
393                         }
394                     } else {
395                         dump('Error in util.widgets.load_attributes('+file._file.path+'):\n');
396                         dump('\telement_id = ' + element_id + '\n');
397                         dump('\tattribute = ' + attribute + '\n');
398                         dump('\tblob[id][attr] = ' + blob[element_id][attribute] + '\n');
399                     }
400                 }
401             }
402             return blob;
403         }
404         return {};
405     } catch(E) {
406         alert('Error loading preferences: ' + E);
407     }
408 }
409
410 util.widgets.addProperty = function(e,c) {
411         if(!e || !c) return;
412
413         var prop_class_string = e.getAttribute('properties');
414         var prop_class_array;
415
416         if(prop_class_string)
417                 prop_class_array = prop_class_string.split(/\s+/);
418
419         var string_ip = ""; /*strip out nulls*/
420         for (var prop_class in prop_class_array) {
421                 if (prop_class_array[prop_class] == c) { return; }
422                 if(prop_class_array[prop_class] !=null)
423                         string_ip += prop_class_array[prop_class] + " ";
424         }
425         string_ip += c;
426         e.setAttribute('properties',string_ip);
427 }
428
429 util.widgets.removeProperty = function(e, c) {
430         if(!e || !c) return;
431
432         var prop_class_string = '';
433
434         var prop_class_array = e.getAttribute('properties');
435         if( prop_class_array )
436                 prop_class_array = prop_class_array.split(/\s+/);
437
438         var first = 1;
439         for (var prop_class in prop_class_array) {
440                 if (prop_class_array[prop_class] != c) {
441                         if (first == 1) {
442                                 prop_class_string = prop_class_array[prop_class];
443                                 first = 0;
444                         } else {
445                                 prop_class_string = prop_class_string + ' ' +
446                                         prop_class_array[prop_class];
447                         }
448                 }
449         }
450         e.setAttribute('properties', prop_class_string);
451 }
452
453 util.widgets.find_descendants_by_name = function(top_node,name) {
454     top_node = util.widgets.get(top_node);
455     if (!top_node) { return []; }
456     return top_node.getElementsByAttribute('name',name);
457 }
458
459 dump('exiting util/widgets.js\n');