Code

Backport from trunk
[gosa.git] / gosa-core / html / include / gosa.js
1 /*
2  * This code is part of GOsa (http://www.gosa-project.org)
3  * Copyright (C) 2003-2010 GONICUS GmbH
4  * 
5  * ID: $$Id: index.php 15301 2010-01-26 09:40:08Z cajus $$
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
22 /* Install event handlers */
23 Event.observe(window, 'resize', resizeHandler);
24 Event.observe(window, 'load', resizeHandler);
25 Event.observe(window, 'load', initProgressPie);
26 Event.observe(window, 'keypress', keyHandler);
29 /* Ask before switching a plugin with this function */
30 function question(text, url)
31 {
32         if(document.mainform.ignore || $('pluginModified') == null || $('pluginModified').value == 0){
33                 location.href= url;
34                 return true;
35         }
36         if(confirm(text)){
37                 location.href= url;
38                 return true;
39         }
40         return false;
41 }
44 /* Toggle checkbox that matches regex */
45 function chk_set_all(regex,value)
46 {
47         for (var i = 0; i < document.mainform.elements.length; i++) {
48                 var _id=document.mainform.elements[i].id;
49                 if(_id.match(regex)) {
50                         document.getElementById(_id).checked= value;
51                 }
52         }
53 }
56 function toggle_all_(regex,state_object)
57 {
58         state = document.getElementById(state_object).checked;
59         chk_set_all(regex, state);
60 }
63 /* Scroll down the body frame */
64 function scrollDown2()
65 {
66         document.body.scrollTop = document.body.scrollHeight - document.body.clientHeight;
67 }
70 /* Toggle checkbox that matches regex */
71 function acl_set_all(regex,value)
72 {
73         for (var i = 0; i < document.mainform.elements.length; i++) {
74                 var _id=document.mainform.elements[i].id;
75                 if(_id.match(regex)) {
76                         document.getElementById(_id).checked= value;
77                 }
78         }
79 }
81 /* Toggle checkbox that matches regex */
82 function acl_toggle_all(regex)
83 {
84     for (var i = 0; i < document.mainform.elements.length; i++) {
85         var _id=document.mainform.elements[i].id;
86         if(_id.match(regex)) {
87             if (document.getElementById(_id).checked == true){
88                 document.getElementById(_id).checked= false;
89             } else {
90                 document.getElementById(_id).checked= true;
91             }
92         }
93     }
94 }
97 /* Global key handler to estimate which element gets the next focus if enter is pressed */
98 function keyHandler(DnEvents) {
100     var element = Event.element(DnEvents);
102     // determines whether Netscape or Internet Explorer
103     k = (Prototype.Browser.Gecko) ? DnEvents.keyCode : window.event.keyCode;
104     if (k == 13 && element.type!='textarea') { // enter key pressed
106         // Stop 'Enter' key-press from beeing processed internally
107         Event.stop(DnEvents);
109         // No nextfield explicitly specified 
110         var next_element = null;
111         if(typeof(nextfield)!='undefined') {
112             next_element = $(nextfield);
113         }
114        
115         // nextfield not given or invalid
116         if(!next_element || typeof(nextfield)=='undefined'){
117             next_element = getNextInputElement(element);
118         }
120         if(element != null && element.type == 'submit'){
122             // If the current element is of type submit, then submit the button else set focus
123             element.click();
124             return(false);
125         }else if(next_element!=null && next_element.type == 'submit'){
126         
127             // If next element is of type submit, then submit the button else set focus
128             next_element.click();
129             return(false);
130         }else if(next_element){
131             next_element.focus();
132             return;
133         }
134     
135     } else if (k==9 && element.type=='textarea') {
136         Event.stop(DnEvents);
137         element.value += "\t";
138         return false;
139     }
142 function getNextInputElement(element)
144     var found = false;
145     for (var e=0;e< document.forms.length; e++){
146         for (var i = 0; i < document.forms[e].elements.length; i++) {           
147             var el = document.forms[e].elements[i]
148             if(found && !el.disabled && el.type!='hidden' && !el.name.match(/^submit_tree_base/) && !el.name.match(/^bs_rebase/)){
149                 return(el);    
150             }                                                           
151             if((el.id != "" && el.id==element.id) || (el.name != "" && el.name==element.name)){        
152                 found =true;
153             }
154         }                                                              
155     }                                                              
158 function changeState() {
159         for (var i = 0; i < arguments.length; i++) { 
160                 var element = $(arguments[i]); 
161                 if (element.hasAttribute('disabled')) { 
162                         element.removeAttribute('disabled'); 
163                 } else { 
164                         element.setAttribute('disabled', 'disabled'); 
165                 } 
166         } 
169 function changeSelectState(triggerField, myField) {
170         if (document.getElementById(triggerField).value != 2){
171                 document.getElementById(myField).disabled= true;
172         } else {
173                 document.getElementById(myField).disabled= false;
174         }
177 function changeSubselectState(triggerField, myField) {
178         if (document.getElementById(triggerField).checked == true){
179                 document.getElementById(myField).disabled= false;
180         } else {
181                 document.getElementById(myField).disabled= true;
182         }
185 function changeTripleSelectState(firstTriggerField, secondTriggerField, myField) {
186         if (
187                         document.getElementById(firstTriggerField).checked == true &&
188                         document.getElementById(secondTriggerField).checked == true){
189                 document.getElementById(myField).disabled= false;
190         } else {
191                 document.getElementById(myField).disabled= true;
192         }
195 <!-- Second field must be non-checked -->
196 function changeTripleSelectState_2nd_neg(firstTriggerField, secondTriggerField, myField) {
197         if (
198                         document.getElementById(firstTriggerField).checked == true &&
199                         document.getElementById(secondTriggerField).checked == false){
200                 document.getElementById(myField).disabled= false;
201         } else {
202                 document.getElementById(myField).disabled= true;
203         }
207 function popup(target, name) {
208         var mypopup= 
209                 window.open(
210                                 target,
211                                 name,
212                                 "width=600,height=700,location=no,toolbar=no,directories=no,menubar=no,status=no,scrollbars=yes"
213                            );
214         mypopup.focus();
215         return false;
218 function js_check(form) {
219         form.javascript.value = 'true';
222 function divGOsa_toggle(element) {
223         var cell;
224         var cellname="tr_"+(element);
226         if (Prototype.Browser.Gecko) {
227                 document.poppedLayer = document.getElementById(element);
228                 cell= document.getElementById(cellname);
230                 if (document.poppedLayer.style.visibility == "visible") {
231                         $(element).hide();
232                         cell.style.height="0px";
233                         document.poppedLayer.style.height="0px";
234                 } else {
235                         $(element).show();
236                         document.poppedLayer.style.height="";
237                         if(document.defaultView) {
238                                 cell.style.height=document.defaultView.getComputedStyle(document.poppedLayer,"").getPropertyValue('height');
239                         }
240                 }
241         } else if (Prototype.Browser.IE) {
242                 document.poppedLayer = document.getElementById(element);
243                 cell= document.getElementById(cellname);
244                 if (document.poppedLayer.style.visibility == "visible") {
245                         $(element).hide();
246                         cell.style.height="0px";
247                         document.poppedLayer.style.height="0px";
248                         document.poppedLayer.style.position="absolute";
249                 } else {
250                         $(element).show();
251                         cell.style.height="";
252                         document.poppedLayer.style.height="";
253                         document.poppedLayer.style.position="relative";
254                 }
255         }
258 function resizeHandler (e) {
259         if (!e) e= window.event;
261         // This works with FF / IE9. If Apples resolves a bug in webkit,
262         // it works with Safari/Chrome, too.
263         if ($("d_scrollbody") && $("t_nscrollbody")) {
264                 var contentHeight= document.viewport.getHeight() - 216;
265                 if ($$('div.plugin-actions').length != 0) {
266                         var height= 0;
267                         $$('div.plugin-actions').each(function(s) {
268                                 height+= s.getHeight();
269                         });
270                         contentHeight-= height + 25;
271                 }
273                 if (Prototype.Browser.IE) {
274                         document.getElementById('d_scrollbody').style.height = contentHeight+23+'px';
275                         document.getElementById('t_nscrollbody').style.height = contentHeight+'px';
276                 } else {
277                         document.getElementById('d_scrollbody').style.minHeight = contentHeight+23+'px';
278                         document.getElementById('t_nscrollbody').style.minHeight = contentHeight+'px';
279                 }
280         }
282         return true;
286 function absTop(e) {
287         return (e.offsetParent)?e.offsetTop+absTop(e.offsetParent) : e.offsetTop;
290 /* Set focus to first valid input field
291    avoid IExplorer warning about hidding or disabled fields
292  */
293 function focus_field()
295         var i     = 0;
296         var e     = 0;
297         var found = false;
298         var element_name = "";
299         var element =null;
301         while(focus_field.arguments[i] && !found){
303                 var tmp = document.getElementsByName(focus_field.arguments[i]);
304                 for(e = 0 ; e < tmp.length ; e ++ ){
306                         if(isVisible(tmp[e])){
307                                 found = true;
308                                 element = tmp[e];
309                                 break;
310                         }
311                 }
312                 i++;
313         }
315         if(element && found){
316                 element.blur();
317                 element.focus();
318         }
322 /*  This function pops up messages from message queue 
323     All messages are hidden in html output (style='display:none;').
324     This function makes single messages visible till there are no more dialogs queued.
326     hidden inputs: 
327     current_msg_dialogs         - Currently visible dialog
328     closed_msg_dialogs          - IDs of already closed dialogs 
329     pending_msg_dialogs         - Queued dialog IDs. 
330  */
331 function next_msg_dialog()
333         var s_pending = "";
334         var a_pending = new Array();
335         var i_id                        = 0;
336         var i                                   = 0;
337         var tmp                         = "";
338         var ele                         = null;
339         var ele2                        = null;
340         var cur_id              = "";
342         if(document.getElementById('current_msg_dialogs')){
343                 cur_id = document.getElementById('current_msg_dialogs').value;
344                 if(cur_id != ""){
345                         ele = document.getElementById('e_layer' + cur_id);
346                         ele.onmousemove = "";
347                         $('e_layer' + cur_id).hide();
348                         document.getElementById('closed_msg_dialogs').value += "," + cur_id;
349                         document.getElementById('current_msg_dialogs').value= ""; 
350                 }
351         }
353         if(document.getElementById('pending_msg_dialogs')){
354                 s_pending = document.getElementById('pending_msg_dialogs').value;
355                 a_pending = s_pending.split(",");
356                 if(a_pending.length){
357                         i_id = a_pending.pop();
358                         for (i = 0 ; i < a_pending.length; ++i){
359                                 tmp = tmp + a_pending[i] + ',';
360                         }
361                         tmp = tmp.replace(/,$/g,"");
362                         if(i_id != ""){
363                                 ele = document.getElementById('e_layer' + i_id);
364                                 ele3 = document.getElementById('e_layerTitle' + i_id);
365                                 ele.style.display= 'block'      ;
366                                 document.getElementById('pending_msg_dialogs').value= tmp;
367                                 document.getElementById('current_msg_dialogs').value= i_id;
368                                 ele2 = document.getElementById('e_layer2') ;
369                                 ele3.onmousedown = start_move_div_by_cursor;
370                                 ele2.onmouseup  = stop_move_div_by_cursor;
371                                 ele2.onmousemove = move_div_by_cursor;
372                         }else{
373                                 ele2 = document.getElementById('e_layer2') ;
374                                 ele2.style.display ="none";
375                         }
376                 }
377         }
381 /* Drag & drop for message dialogs */
382 var enable_move_div_by_cursor = false;          // Indicates wheter the div movement is enabled or not 
383 var mouse_x_on_div      = 0;                                                                    // 
384 var mouse_y_on_div      = 0;
385 var div_offset_x        = 0;
386 var div_offset_y        = 0;
388 /* Activates msg_dialog drag & drop
389  * This function is called when clicking on a displayed msg_dialog 
390  */
391 function start_move_div_by_cursor(e)
393         var x = 0; 
394         var y = 0;      
395         var cur_id = 0;
396         var dialog = null;
397         var event = null;
399         /* Get current msg_dialog position
400          */
401         cur_id = document.getElementById('current_msg_dialogs').value;
402         if(cur_id != ""){
403                 dialog = document.getElementById('e_layer' + cur_id);
404                 x = dialog.style.left;
405                 y = dialog.style.top;
406                 x = x.replace(/[^0-9]/g,"");
407                 y = y.replace(/[^0-9]/g,"");
408                 if(!y) y = 1;
409                 if(!x) x = 1;
410         }
412         /* Get mouse position within msg_dialog 
413          */
414         if(window.event){
415                 event = window.event;
416                 if(event.offsetX){
417                         div_offset_x   = event.clientX -x;
418                         div_offset_y   = event.clientY -y;
419                         enable_move_div_by_cursor = true;
420                 }
421         }else if(e){
422                 event = e;
423                 if(event.layerX){
424                         div_offset_x    = event.screenX -x;
425                         div_offset_y    = event.screenY -y;
426                         enable_move_div_by_cursor = true;
427                 }
428         }
432 /* Deactivate msg_dialog movement 
433  */
434 function stop_move_div_by_cursor()
436         mouse_x_on_div = 0;
437         mouse_y_on_div = 0;
438         div_offset_x = 0;
439         div_offset_y = 0;
440         enable_move_div_by_cursor = false;
444 /* Move msg_dialog with cursor */
445 function move_div_by_cursor(e)
447         var event                               = false;
448         var mouse_pos_x         = 0;
449         var mouse_pos_y         = 0;
450         var     cur_div_x = 0;
451         var cur_div_y = 0;
452         var cur_id      = 0;
453         var dialog = null;
456         if(undefined !== enable_move_div_by_cursor && enable_move_div_by_cursor == true){
458                 if(document.getElementById('current_msg_dialogs')){
460                         /* Get mouse position on screen 
461                          */
462                         if(window.event){
463                                 event = window.event;
464                                 mouse_pos_x  =event.clientX;
465                                 mouse_pos_y  =event.clientY;
466                         }else if (e){
467                                 event = e;
468                                 mouse_pos_x  =event.screenX;
469                                 mouse_pos_y  =event.screenY;
470                         }else{
471                                 return;
472                         }
474                         /* Get id of current msg_dialog */
475                         cur_id = document.getElementById('current_msg_dialogs').value;
476                         if(cur_id != ""){
477                                 dialog = document.getElementById('e_layer' + cur_id);
479                                 /* Calculate new position */
480                                 cur_div_x = mouse_pos_x - div_offset_x;
481                                 cur_div_y = mouse_pos_y - div_offset_y;
483                                 /* Ensure that dialog can't be moved out of screen */
484                                 if(cur_div_x < 0 ) cur_div_x = 0
485                                         if(cur_div_y < 0 ) cur_div_y = 0
487                                                 /* Assign new values */
488                                                 dialog.style.left = (cur_div_x ) + "px";
489                                 dialog.style.top  = (cur_div_y ) + "px";
490                         }
491                 }
492         }
496 function isVisible(obj)
498     if (obj == document) return true
500     if (!obj) return false
501     if (!obj.parentNode) return false
502     if (obj.style) {
503         if (obj.style.display == 'none') return false
504         if (obj.style.visibility == 'hidden') return false
505     }
507     //Try the computed style in a standard way
508     if (window.getComputedStyle) {
509         var style = window.getComputedStyle(obj, "")
510         if (style.display == 'none') return false
511         if (style.visibility == 'hidden') return false
512     }
514     //Or get the computed style using IE's silly proprietary way
515     var style = obj.currentStyle
516     if (style) {
517         if (style['display'] == 'none') return false
518         if (style['visibility'] == 'hidden') return false
519     }
521     return isVisible(obj.parentNode)
525 /* Check if capslock is enabled */
526 function capslock(e) {
527     e = (e) ? e : window.event;
529     var charCode = false;
530     if (e.which) {
531         charCode = e.which;
532     } else if (e.keyCode) {
533         charCode = e.keyCode;
534     }
536     var shifton = false;
537     if (e.shiftKey) {
538         shifton = e.shiftKey;
539     } else if (e.modifiers) {
540         shifton = !!(e.modifiers & 4);
541     }
543     if (charCode >= 97 && charCode <= 122 && shifton) {
544         return true;
545     }
547     if (charCode >= 65 && charCode <= 90 && !shifton) {
548         return true;
549     }
551     return false;
554 function setProgressPie(context, percent)
556     context.clearRect(0, 0, 22, 22);
558     var r = "FF";
559     var g = "FF";
560     var b = "FF";
562     // Fade yellow
563     if (percent > 50) {
564         d = 255 - parseInt((percent-50) * 255 / 50)
565             b = d.toString(16);
566     }
568     // Fade red
569     if (percent > 75) {
570         d = 255 - parseInt((percent-75) * 255 / 25)
571             g = d.toString(16);
572     }
574     context.strokeStyle = "#" + r  + g + b
575         context.fillStyle = context.strokeStyle;
577     context.beginPath();
578     context.moveTo(11,11)
579         context.arc(11,11,8,-Math.PI/2,-Math.PI/2 + Math.PI*percent/50,true);
580     context.closePath();
581     context.fill();
583     context.moveTo(11,11)
584         context.beginPath();
585     context.arc(11,11,8,0,Math.PI*2,false);
586     context.closePath();
587     context.stroke();
590 function initProgressPie(){
591     var canvas = $('sTimeout');
593     // Check the element is in the DOM and the browser supports canvas
594     if(canvas && canvas.getContext) {
595         var percent = 0.01;
596         var context = canvas.getContext('2d');
597         setProgressPie(context, percent);
599         // Extract timeout and title string out out canvas.title
600         var data = canvas.title;
601         var timeout = data.replace(/\|.*$/,'');
602         var title = data.replace(/^.*\|/,'');
603         var interval = 1;
604         var time = 0;
605         setInterval(function() {
607                 // Calculate percentage 
608                 percent+= (interval / timeout) * 100;
610                 // Increase current time by interval
611                 time += interval;
613                 // Generate title
614                 var minutes = parseInt((timeout-time) / 60 );
615                 var seconds = '' + parseInt((timeout-time) % 60);
616                 if(seconds.length == 1) seconds = '0' + seconds ;
617                 minutes = minutes + ':' + seconds;
619                 // Set new  canval title
620                 canvas.title=  title.replace(/%d/ ,minutes);
621                 setProgressPie(context, percent);
623                 if (percent>99) percent= 99;
624                 }, (interval * 1000));
625     }
628 /* Scroll down the body frame */
629 function scrollDown2()
631     document.body.scrollTop = document.body.scrollHeight - document.body.clientHeight;
635 // Global storage for baseSelector timer
636 var rtimer;
638 // vim:ts=2:syntax