From 40418dd16411484ed80553c78e85de0c7d80c0c7 Mon Sep 17 00:00:00 2001
From: alecpl <alec@alec.pl>
Date: Sat, 09 May 2009 03:52:29 -0400
Subject: [PATCH] - Correct caret position in message form with signature (#1485833)

---
 program/js/app.js |   26 ++++++++++++++++----------
 1 files changed, 16 insertions(+), 10 deletions(-)

diff --git a/program/js/app.js b/program/js/app.js
index e844403..2fb6ce5 100644
--- a/program/js/app.js
+++ b/program/js/app.js
@@ -472,7 +472,7 @@
     else if (input_subject.val() == '')
       input_subject.focus();
     else if (input_message)
-      this.set_caret2start(input_message);
+      input_message.focus();
 
     // get summary of all field values
     this.compose_field_hash(true);
@@ -2188,7 +2188,7 @@
     var input_message = $("[name='_message']");
     var message = input_message.val();
     var is_html = ($("input[name='_is_html']").val() == '1');
-    var sig, p;
+    var sig, p, len;
 
     if (!this.env.identity)
       this.env.identity = id
@@ -2212,7 +2212,8 @@
         }
 
       message = message.replace(/[\r\n]+$/, '');
-      
+      len = message.length;
+
       // add the new signature string
       if (this.env.signatures && this.env.signatures[id])
         {
@@ -2224,6 +2225,7 @@
         if (sig.indexOf('-- ')!=0)
           sig = '-- \n'+sig;
         message += '\n\n'+sig;
+	if (len) len += 1;
         }
       }
     else
@@ -2269,6 +2271,10 @@
       }
 
     input_message.val(message);
+
+    // move cursor before the signature
+    if (!is_html)
+      this.set_caret_pos(input_message.get(0), len);
 
     this.env.identity = id;
     return true;
@@ -4011,19 +4017,19 @@
       return obj.value.length;
     };
 
-  this.set_caret2start = function(obj)
+  this.set_caret_pos = function(obj, pos)
     {
-    if (obj.createTextRange)
+    if (obj.setSelectionRange)
+      obj.setSelectionRange(pos, pos);
+    else if (obj.createTextRange)
       {
       var range = obj.createTextRange();
       range.collapse(true);
+      range.moveEnd('character', pos);
+      range.moveStart('character', pos);
       range.select();
       }
-    else if (obj.setSelectionRange)
-      obj.setSelectionRange(0,0);
-
-    obj.focus();
-    };
+    }
 
   // set all fields of a form disabled
   this.lock_form = function(form, lock)

--
Gitblit v1.9.1