Allow more advanced element focusing
[squirrelmail.git] / templates / default / js / default.js
index 44c0c0ebc8363802071420f9c7f61c9c4a1dada5..f55517262958133507495129c55269f1dac338ec 100644 (file)
@@ -1,7 +1,7 @@
 /**
  * This array is used to remember mark status of rows in browse mode
  *
- * @copyright © 2005-2006 The SquirrelMail Project Team
+ * @copyright 2005-2018 The SquirrelMail Project Team
  * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  * @version $Id$
  */
@@ -12,16 +12,29 @@ var orig_row_colors = new Array();
  * (un)Checks checkbox for the row that the current table cell is in
  * when it gets clicked.
  *
- * @param   string   the name of the checkbox that should be (un)checked
+ * @param string                  The (internal) name of the checkbox
+ *                                that should be (un)checked
+ * @param JavaScript event object The JavaScript event associated
+ *                                with this mouse click
+ * @param string                  The name of the encapsulating form
+ * @param string                  The (real) name of the checkbox
+ *                                that should be (un)checked
+ * @param string                  Any extra JavaScript (that will
+ *                                be executed herein if non-empty);
+ *                                must be valid JavaScript expression(s)
  */
-function row_click(chkboxName) {
-    chkbox = document.getElementById(chkboxName);
+function row_click(chkboxName, event, formName, checkboxRealName, extra) {
+    var chkbox = document.getElementById(chkboxName);
     if (chkbox) {
         // initialize orig_row_color if not defined already
         if (!orig_row_colors[chkboxName]) {
             orig_row_colors[chkboxName] = chkbox.parentNode.getAttribute('bgcolor');
+            if (orig_row_colors[chkboxName].indexOf("clicked_") == 0)
+                orig_row_colors[chkboxName] = orig_row_colors[chkboxName].substring(8, orig_row_colors[chkboxName].length);
         }
         chkbox.checked = (chkbox.checked ? false : true);
+
+        if (extra != '') eval(extra);
     }
 }
 
@@ -31,6 +44,7 @@ function row_click(chkboxName) {
  */
 function getCSSClass (theRow)
 {
+    var rowClass;
        // 3.1 ... with DOM compatible browsers except Opera that does not return
        //         valid values with "getAttribute"
        if (typeof(window.opera) == 'undefined'
@@ -63,22 +77,25 @@ function setCSSClass (obj, newClass) {
  * need to predefine the entire array
  */
 function rowOver(chkboxName) {
-    chkbox = document.getElementById(chkboxName);
+    var chkbox = document.getElementById(chkboxName);
+    var rowClass, rowNum, overClass, clickedClass;
     if (chkbox) {
         if (!orig_row_colors[chkboxName]) {
-                       rowClass = getCSSClass(chkbox.parentNode.parentNode);
+            rowClass = getCSSClass(chkbox.parentNode.parentNode);
+            if (rowClass.indexOf("clicked_") == 0)
+                rowClass = rowClass.substring(8, rowClass.length);
             orig_row_colors[chkboxName] = rowClass;
         } else {
             rowClass = orig_row_colors[chkboxName];
         }
-        j = chkbox.name.length - 1
+        rowNum = chkboxName.substring(chkboxName.length - 1, chkboxName.length);
 
 /*
  * The mouseover and clicked CSS classes are always the same name!
  */        
         overClass = 'mouse_over';
         clickedClass = 'clicked';
-        setPointer(chkbox.parentNode.parentNode, j,'over' , rowClass, overClass, clickedClass);
+        setPointer(chkbox.parentNode.parentNode, rowNum, 'over' , rowClass, overClass, clickedClass);
     }
 }
 
@@ -87,50 +104,51 @@ function rowOver(chkboxName) {
  * when it gets clicked.
  *
  * @param   string   the id of the form where all checkboxes should be (un)checked
+ * @param   string   the first three characters of target checkboxes, if any
  * @param   boolean  use fancy row coloring when a checkbox is checked
  * @param   string   new color of the checked rows
  */
-function toggle_all(formname, fancy) {
-     TargetForm = document.getElementById(formname);
-     j = 0;
-     for (var i = 0; i < TargetForm.elements.length; i++) {
-         if (TargetForm.elements[i].type == 'checkbox' && TargetForm.elements[i].name.substring(0,3) == 'msg') {
-             if (fancy) {
+function toggle_all(formname, name_prefix, fancy) {
+    var TargetForm = document.getElementById(formname);
+    var j = 0;
+    for (var i = 0; i < TargetForm.elements.length; i++) {
+        if (TargetForm.elements[i].type == 'checkbox' && (name_prefix == '' || TargetForm.elements[i].name.substring(0,3) == name_prefix)) {
+            if (fancy) {
                 array_key = TargetForm.elements[i].getAttribute('id');
-                if (TargetForm.elements[i].checked == false) {
-                    // initialize orig_row_color if not defined already
-                    if (!orig_row_colors[array_key]) {
-                                               rowClass = getCSSClass(TargetForm.elements[i].parentNode.parentNode);
-                               orig_row_colors[array_key] = rowClass;
-                    }
+                // initialize orig_row_color if not defined already
+                if (!orig_row_colors[array_key]) {
+                    rowClass = getCSSClass(TargetForm.elements[i].parentNode.parentNode);
+                    if (rowClass.indexOf("clicked_") == 0)
+                        rowClass = rowClass.substring(8, rowClass.length);
+                    orig_row_colors[array_key] = rowClass;
                 }
                 origClass = orig_row_colors[array_key];
-                       clickedClass = 'clicked';
+                clickedClass = 'clicked';
                 setPointer(TargetForm.elements[i].parentNode.parentNode, j,'click' , origClass, origClass, clickedClass);
                 j++
             }
             TargetForm.elements[i].checked = !(TargetForm.elements[i].checked);
-         }
-     }
+        }
+    }
 }
 
 /*
  * Sets/unsets the pointer and marker in browse mode
  *
- * @param   object    the table row
- * @param   integer  the row number
- * @param   string    the action calling this script (over, out or click)
- * @param   string    the default background CSS class
- * @param   string    the CSS class to use for mouseover
- * @param   string    the CSS class to use for marking a row
+ * @param object  theRow         the table row
+ * @param integer theRowNum      the row number
+ * @param string  theAction      the action calling this script (over, out or click)
+ * @param string  defaultClass   the default background CSS class
+ * @param string  mouseoverClass the CSS class to use for mouseover
+ * @param string  clickedClass   the CSS class to use for marking a row
  *
  * @return  boolean  whether pointer is set or not
  */
-function setPointer(theRow, theRowNum, theAction, theDefaultClass, thePointerClass, theMarkClass)
+function setPointer(theRow, theRowNum, theAction, defaultClass, mouseoverClass, clickedClass)
 {
     // 1. Pointer and mark feature are disabled or the browser can't get the
     //    row -> exits
-    if ((thePointerClass == '' && theMarkClass == '')
+    if ((mouseoverClass == '' && clickedClass == '')
         || typeof(theRow.className) == 'undefined') {
         return false;
     }
@@ -149,16 +167,18 @@ function setPointer(theRow, theRowNum, theAction, theDefaultClass, thePointerCla
     // 3. Gets the current CSS class...
     var newClass     = null;
     var currentClass = getCSSClass(theRow);
+    if (currentClass.indexOf("clicked_") == 0)
+        currentClass = 'clicked';
     
     // 4. Defines the new class
     // 4.1 Current class is the default one
     if (currentClass == ''
-        || currentClass.toLowerCase() == theDefaultClass.toLowerCase()) {
-        if (theAction == 'over' && thePointerClass != '') {
-            newClass = thePointerClass;
+        || currentClass.toLowerCase() == defaultClass.toLowerCase()) {
+        if (theAction == 'over' && mouseoverClass != '') {
+            newClass = mouseoverClass;
         }
-        else if (theAction == 'click' && theMarkClass != '') {
-            newClass = theMarkClass;
+        else if (theAction == 'click' && clickedClass != '') {
+            newClass = clickedClass;
             marked_row[theRowNum] = true;
             // deactivated onclick marking of the checkbox because it's also executed
             // when an action (clicking on the checkbox itself) on a single item is
@@ -168,27 +188,25 @@ function setPointer(theRow, theRowNum, theAction, theDefaultClass, thePointerCla
             //document.getElementById('msg[' + theRowNum + ']').checked = true;
         }
     }
-    // 4.1.2 Current class is the pointer one
-    else if (currentClass.toLowerCase() == thePointerClass.toLowerCase()
+    // 4.1.2 Current class is the mouseover one
+    else if (currentClass.toLowerCase() == mouseoverClass.toLowerCase()
              && (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])) {
         if (theAction == 'out') {
-            newClass = theDefaultClass;
+            newClass = defaultClass;
         }
-        else if (theAction == 'click' && theMarkClass != '') {
-            newClass = theMarkClass;
+        else if (theAction == 'click' && clickedClass != '') {
+            newClass = clickedClass;
             marked_row[theRowNum] = true;
             //document.getElementById('msg[' + theRowNum + ']').checked = true;
         }
     }
-    // 4.1.3 Current color is the marker one
-    else if (currentClass.toLowerCase() == theMarkClass.toLowerCase()) {
+    // 4.1.3 Current color is the clicked one
+    else if (currentClass.toLowerCase() == clickedClass.toLowerCase()) {
         if (theAction == 'click') {
-            newClass              = (thePointerClass != '')
-                                  ? thePointerClass
-                                  : theDefaultClass;
-            marked_row[theRowNum] = (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])
-                                  ? true
-                                  : null;
+            newClass              = (mouseoverClass != '')
+                                  ? mouseoverClass
+                                  : defaultClass;
+            marked_row[theRowNum] = false;
             //document.getElementById('msg[' + theRowNum + ']').checked = false;
         }
     } // end 4
@@ -209,7 +227,7 @@ function comp_in_new_form(comp_uri, button, myform, iWidth, iHeight) {
     }
     if (!iWidth) iWidth   =  640;
     if (!iHeight) iHeight =  550;
-    sArg = "width=" + iWidth + ",height=" + iHeight + ",scrollbars=yes,resizable=yes,status=yes";
+    var sArg = "width=" + iWidth + ",height=" + iHeight + ",scrollbars=yes,resizable=yes,status=yes";
     var newwin = window.open(comp_uri, "_blank", sArg);
 }
 
@@ -224,7 +242,7 @@ function comp_in_new(comp_uri, iWidth, iHeight) {
  * Reload the read_body screen on sending an mdn receipt
  */
 function sendMDN() {
-    mdnuri=window.location+'&sendreceipt=1';
+    var mdnuri=window.location+'&sendreceipt=1';
     window.location = mdnuri; 
 }
 
@@ -248,13 +266,20 @@ function checkForm(smaction) {
      */
         var f = document.forms.length;
         var i = 0;
+        var remembered_form = -1;
         var pos = -1;
+        var remembered_pos = -1;
         while( pos == -1 && i < f ) {
             var e = document.forms[i].elements.length;
             var j = 0;
             while( pos == -1 && j < e ) {
-                if ( document.forms[i].elements[j].type == 'text' || document.forms[i].elements[j].type == 'password' ) {
-                    pos = j;
+                if ( document.forms[i].elements[j].type == 'text' || document.forms[i].elements[j].type == 'password' || document.forms[i].elements[j].type == 'textarea' ) {
+                    if ( document.forms[i].elements[j].id.substring(0, 13) == '__lastfocus__' ) {
+                        remembered_pos = j;
+                        remembered_form = i;
+                    } else if ( document.forms[i].elements[j].id.substring(0, 11) != '__nofocus__' ) {
+                        pos = j;
+                    }
                 }
                 j++;
             }
@@ -262,6 +287,28 @@ function checkForm(smaction) {
         }
         if( pos >= 0 ) {
             document.forms[i-1].elements[pos].focus();
+        } else if ( remembered_pos >= 0 ) {
+            document.forms[remembered_form].elements[remembered_pos].focus();
         }
     }
 }
+
+function printThis()
+{
+    parent.frames['right'].focus();
+    parent.frames['right'].print();
+}
+
+/* JS implementation of more/less links in To/CC. Could later be extended
+ * show/hide other interface items */
+function showhide (item, txtmore, txtless) {
+    var oTemp=document.getElementById("recpt_tail_" + item);
+    var oClick=document.getElementById("toggle_" + item);
+    if (oTemp.style.display=="inline") {
+        oTemp.style.display="none";
+        oClick.innerHTML=txtmore;
+    } else {
+        oTemp.style.display="inline";
+        oClick.innerHTML=txtless;
+    }
+}