Code

Updated mapping patch
[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 next element is of type submit, then submit the button else set focus
121         if(next_element!=null && next_element.type == 'submit'){
122             next_element.click();
123             return(false);
124         }else if(next_element){
125             next_element.focus();
126             return;
127         }
128     
129     } else if (k==9 && element.type=='textarea') {
130         Event.stop(DnEvents);
131         element.value += "\t";
132         return false;
133     }
136 function getNextInputElement(element)
138     var found = false;
139     for (var e=0;e< document.forms.length; e++){
140         for (var i = 0; i < document.forms[e].elements.length; i++) {           
141             var el = document.forms[e].elements[i]
142             if(found && !el.disabled && el.type!='hidden' && !el.name.match(/^submit_tree_base/) && !el.name.match(/^bs_rebase/)){
143                 return(el);    
144             }                                                           
145             if((el.id != "" && el.id==element.id) || (el.name != "" && el.name==element.name)){        
146                 found =true;
147             }
148         }                                                              
149     }                                                              
152 function changeState() {
153         for (var i = 0; i < arguments.length; i++) { 
154                 var element = $(arguments[i]); 
155                 if (element.hasAttribute('disabled')) { 
156                         element.removeAttribute('disabled'); 
157                 } else { 
158                         element.setAttribute('disabled', 'disabled'); 
159                 } 
160         } 
163 function changeSelectState(triggerField, myField) {
164         if (document.getElementById(triggerField).value != 2){
165                 document.getElementById(myField).disabled= true;
166         } else {
167                 document.getElementById(myField).disabled= false;
168         }
171 function changeSubselectState(triggerField, myField) {
172         if (document.getElementById(triggerField).checked == true){
173                 document.getElementById(myField).disabled= false;
174         } else {
175                 document.getElementById(myField).disabled= true;
176         }
179 function changeTripleSelectState(firstTriggerField, secondTriggerField, myField) {
180         if (
181                         document.getElementById(firstTriggerField).checked == true &&
182                         document.getElementById(secondTriggerField).checked == true){
183                 document.getElementById(myField).disabled= false;
184         } else {
185                 document.getElementById(myField).disabled= true;
186         }
189 <!-- Second field must be non-checked -->
190 function changeTripleSelectState_2nd_neg(firstTriggerField, secondTriggerField, myField) {
191         if (
192                         document.getElementById(firstTriggerField).checked == true &&
193                         document.getElementById(secondTriggerField).checked == false){
194                 document.getElementById(myField).disabled= false;
195         } else {
196                 document.getElementById(myField).disabled= true;
197         }
201 function popup(target, name) {
202         var mypopup= 
203                 window.open(
204                                 target,
205                                 name,
206                                 "width=600,height=700,location=no,toolbar=no,directories=no,menubar=no,status=no,scrollbars=yes"
207                            );
208         mypopup.focus();
209         return false;
212 function js_check(form) {
213         form.javascript.value = 'true';
216 function divGOsa_toggle(element) {
217         var cell;
218         var cellname="tr_"+(element);
220         if (Prototype.Browser.Gecko) {
221                 document.poppedLayer = document.getElementById(element);
222                 cell= document.getElementById(cellname);
224                 if (document.poppedLayer.style.visibility == "visible") {
225                         $(element).hide();
226                         cell.style.height="0px";
227                         document.poppedLayer.style.height="0px";
228                 } else {
229                         $(element).show();
230                         document.poppedLayer.style.height="";
231                         if(document.defaultView) {
232                                 cell.style.height=document.defaultView.getComputedStyle(document.poppedLayer,"").getPropertyValue('height');
233                         }
234                 }
235         } else if (Prototype.Browser.IE) {
236                 document.poppedLayer = document.getElementById(element);
237                 cell= document.getElementById(cellname);
238                 if (document.poppedLayer.style.visibility == "visible") {
239                         $(element).hide();
240                         cell.style.height="0px";
241                         document.poppedLayer.style.height="0px";
242                         document.poppedLayer.style.position="absolute";
243                 } else {
244                         $(element).show();
245                         cell.style.height="";
246                         document.poppedLayer.style.height="";
247                         document.poppedLayer.style.position="relative";
248                 }
249         }
252 function resizeHandler (e) {
253         if (!e) e= window.event;
255         // This works with FF / IE9. If Apples resolves a bug in webkit,
256         // it works with Safari/Chrome, too.
257         if ($("d_scrollbody") && $("t_nscrollbody")) {
258                 var contentHeight= document.viewport.getHeight() - 216;
259                 if ($$('div.plugin-actions').length != 0) {
260                         var height= 0;
261                         $$('div.plugin-actions').each(function(s) {
262                                 height+= s.getHeight();
263                         });
264                         contentHeight-= height + 5;
265                 }
267                 if (Prototype.Browser.Gecko || Prototype.Browser.IE) {
268                         document.getElementById('d_scrollbody').style.height = contentHeight+23+'px';
269                         document.getElementById('t_nscrollbody').style.height = contentHeight+'px';
270                 } else {
271                         document.getElementById('d_scrollbody').style.minHeight = contentHeight+23+'px';
272                         document.getElementById('t_nscrollbody').style.minHeight = contentHeight+'px';
273                 }
274         }
276         return true;
280 function absTop(e) {
281         return (e.offsetParent)?e.offsetTop+absTop(e.offsetParent) : e.offsetTop;
284 /* Set focus to first valid input field
285    avoid IExplorer warning about hidding or disabled fields
286  */
287 function focus_field()
289         var i     = 0;
290         var e     = 0;
291         var found = false;
292         var element_name = "";
293         var element =null;
295         while(focus_field.arguments[i] && !found){
297                 var tmp = document.getElementsByName(focus_field.arguments[i]);
298                 for(e = 0 ; e < tmp.length ; e ++ ){
300                         if(isVisible(tmp[e])){
301                                 found = true;
302                                 element = tmp[e];
303                                 break;
304                         }
305                 }
306                 i++;
307         }
309         if(element && found){
310                 element.blur();
311                 element.focus();
312         }
316 /*  This function pops up messages from message queue 
317     All messages are hidden in html output (style='display:none;').
318     This function makes single messages visible till there are no more dialogs queued.
320     hidden inputs: 
321     current_msg_dialogs         - Currently visible dialog
322     closed_msg_dialogs          - IDs of already closed dialogs 
323     pending_msg_dialogs         - Queued dialog IDs. 
324  */
325 function next_msg_dialog()
327         var s_pending = "";
328         var a_pending = new Array();
329         var i_id                        = 0;
330         var i                                   = 0;
331         var tmp                         = "";
332         var ele                         = null;
333         var ele2                        = null;
334         var cur_id              = "";
336         if(document.getElementById('current_msg_dialogs')){
337                 cur_id = document.getElementById('current_msg_dialogs').value;
338                 if(cur_id != ""){
339                         ele = document.getElementById('e_layer' + cur_id);
340                         ele.onmousemove = "";
341                         $('e_layer' + cur_id).hide();
342                         document.getElementById('closed_msg_dialogs').value += "," + cur_id;
343                         document.getElementById('current_msg_dialogs').value= ""; 
344                 }
345         }
347         if(document.getElementById('pending_msg_dialogs')){
348                 s_pending = document.getElementById('pending_msg_dialogs').value;
349                 a_pending = s_pending.split(",");
350                 if(a_pending.length){
351                         i_id = a_pending.pop();
352                         for (i = 0 ; i < a_pending.length; ++i){
353                                 tmp = tmp + a_pending[i] + ',';
354                         }
355                         tmp = tmp.replace(/,$/g,"");
356                         if(i_id != ""){
357                                 ele = document.getElementById('e_layer' + i_id);
358                                 ele3 = document.getElementById('e_layerTitle' + i_id);
359                                 ele.style.display= 'block'      ;
360                                 document.getElementById('pending_msg_dialogs').value= tmp;
361                                 document.getElementById('current_msg_dialogs').value= i_id;
362                                 ele2 = document.getElementById('e_layer2') ;
363                                 ele3.onmousedown = start_move_div_by_cursor;
364                                 ele2.onmouseup  = stop_move_div_by_cursor;
365                                 ele2.onmousemove = move_div_by_cursor;
366                         }else{
367                                 ele2 = document.getElementById('e_layer2') ;
368                                 ele2.style.display ="none";
369                         }
370                 }
371         }
375 /* Drag & drop for message dialogs */
376 var enable_move_div_by_cursor = false;          // Indicates wheter the div movement is enabled or not 
377 var mouse_x_on_div      = 0;                                                                    // 
378 var mouse_y_on_div      = 0;
379 var div_offset_x        = 0;
380 var div_offset_y        = 0;
382 /* Activates msg_dialog drag & drop
383  * This function is called when clicking on a displayed msg_dialog 
384  */
385 function start_move_div_by_cursor(e)
387         var x = 0; 
388         var y = 0;      
389         var cur_id = 0;
390         var dialog = null;
391         var event = null;
393         /* Get current msg_dialog position
394          */
395         cur_id = document.getElementById('current_msg_dialogs').value;
396         if(cur_id != ""){
397                 dialog = document.getElementById('e_layer' + cur_id);
398                 x = dialog.style.left;
399                 y = dialog.style.top;
400                 x = x.replace(/[^0-9]/g,"");
401                 y = y.replace(/[^0-9]/g,"");
402                 if(!y) y = 1;
403                 if(!x) x = 1;
404         }
406         /* Get mouse position within msg_dialog 
407          */
408         if(window.event){
409                 event = window.event;
410                 if(event.offsetX){
411                         div_offset_x   = event.clientX -x;
412                         div_offset_y   = event.clientY -y;
413                         enable_move_div_by_cursor = true;
414                 }
415         }else if(e){
416                 event = e;
417                 if(event.layerX){
418                         div_offset_x    = event.screenX -x;
419                         div_offset_y    = event.screenY -y;
420                         enable_move_div_by_cursor = true;
421                 }
422         }
426 /* Deactivate msg_dialog movement 
427  */
428 function stop_move_div_by_cursor()
430         mouse_x_on_div = 0;
431         mouse_y_on_div = 0;
432         div_offset_x = 0;
433         div_offset_y = 0;
434         enable_move_div_by_cursor = false;
438 /* Move msg_dialog with cursor */
439 function move_div_by_cursor(e)
441         var event                               = false;
442         var mouse_pos_x         = 0;
443         var mouse_pos_y         = 0;
444         var     cur_div_x = 0;
445         var cur_div_y = 0;
446         var cur_id      = 0;
447         var dialog = null;
450         if(undefined !== enable_move_div_by_cursor && enable_move_div_by_cursor == true){
452                 if(document.getElementById('current_msg_dialogs')){
454                         /* Get mouse position on screen 
455                          */
456                         if(window.event){
457                                 event = window.event;
458                                 mouse_pos_x  =event.clientX;
459                                 mouse_pos_y  =event.clientY;
460                         }else if (e){
461                                 event = e;
462                                 mouse_pos_x  =event.screenX;
463                                 mouse_pos_y  =event.screenY;
464                         }else{
465                                 return;
466                         }
468                         /* Get id of current msg_dialog */
469                         cur_id = document.getElementById('current_msg_dialogs').value;
470                         if(cur_id != ""){
471                                 dialog = document.getElementById('e_layer' + cur_id);
473                                 /* Calculate new position */
474                                 cur_div_x = mouse_pos_x - div_offset_x;
475                                 cur_div_y = mouse_pos_y - div_offset_y;
477                                 /* Ensure that dialog can't be moved out of screen */
478                                 if(cur_div_x < 0 ) cur_div_x = 0
479                                         if(cur_div_y < 0 ) cur_div_y = 0
481                                                 /* Assign new values */
482                                                 dialog.style.left = (cur_div_x ) + "px";
483                                 dialog.style.top  = (cur_div_y ) + "px";
484                         }
485                 }
486         }
490 function isVisible(obj)
492     if (obj == document) return true
494     if (!obj) return false
495     if (!obj.parentNode) return false
496     if (obj.style) {
497         if (obj.style.display == 'none') return false
498         if (obj.style.visibility == 'hidden') return false
499     }
501     //Try the computed style in a standard way
502     if (window.getComputedStyle) {
503         var style = window.getComputedStyle(obj, "")
504         if (style.display == 'none') return false
505         if (style.visibility == 'hidden') return false
506     }
508     //Or get the computed style using IE's silly proprietary way
509     var style = obj.currentStyle
510     if (style) {
511         if (style['display'] == 'none') return false
512         if (style['visibility'] == 'hidden') return false
513     }
515     return isVisible(obj.parentNode)
519 /* Check if capslock is enabled */
520 function capslock(e) {
521     e = (e) ? e : window.event;
523     var charCode = false;
524     if (e.which) {
525         charCode = e.which;
526     } else if (e.keyCode) {
527         charCode = e.keyCode;
528     }
530     var shifton = false;
531     if (e.shiftKey) {
532         shifton = e.shiftKey;
533     } else if (e.modifiers) {
534         shifton = !!(e.modifiers & 4);
535     }
537     if (charCode >= 97 && charCode <= 122 && shifton) {
538         return true;
539     }
541     if (charCode >= 65 && charCode <= 90 && !shifton) {
542         return true;
543     }
545     return false;
548 function setProgressPie(context, percent)
550     context.clearRect(0, 0, 22, 22);
552     var r = "FF";
553     var g = "FF";
554     var b = "FF";
556     // Fade yellow
557     if (percent > 50) {
558         d = 255 - parseInt((percent-50) * 255 / 50)
559             b = d.toString(16);
560     }
562     // Fade red
563     if (percent > 75) {
564         d = 255 - parseInt((percent-75) * 255 / 25)
565             g = d.toString(16);
566     }
568     context.strokeStyle = "#" + r  + g + b
569         context.fillStyle = context.strokeStyle;
571     context.beginPath();
572     context.moveTo(11,11)
573         context.arc(11,11,8,-Math.PI/2,-Math.PI/2 + Math.PI*percent/50,true);
574     context.closePath();
575     context.fill();
577     context.moveTo(11,11)
578         context.beginPath();
579     context.arc(11,11,8,0,Math.PI*2,false);
580     context.closePath();
581     context.stroke();
584 function initProgressPie(){
585     var canvas = $('sTimeout');
587     // Check the element is in the DOM and the browser supports canvas
588     if(canvas && canvas.getContext) {
589         var percent = 0.01;
590         var context = canvas.getContext('2d');
591         setProgressPie(context, percent);
593         // Extract timeout and title string out out canvas.title
594         var data = canvas.title;
595         var timeout = data.replace(/\|.*$/,'');
596         var title = data.replace(/^.*\|/,'');
597         var interval = 1;
598         var time = 0;
599         setInterval(function() {
601                 // Calculate percentage 
602                 percent+= (interval / timeout) * 100;
604                 // Increase current time by interval
605                 time += interval;
607                 // Generate title
608                 var minutes = parseInt((timeout-time) / 60 );
609                 var seconds = '' + parseInt((timeout-time) % 60);
610                 if(seconds.length == 1) seconds = '0' + seconds ;
611                 minutes = minutes + ':' + seconds;
613                 // Set new  canval title
614                 canvas.title=  title.replace(/%d/ ,minutes);
615                 setProgressPie(context, percent);
617                 if (percent>99) percent= 99;
618                 }, (interval * 1000));
619     }
622 /* Scroll down the body frame */
623 function scrollDown2()
625     document.body.scrollTop = document.body.scrollHeight - document.body.clientHeight;
629 // Global storage for baseSelector timer
630 var rtimer;
632 // vim:ts=2:syntax