]> git.openstreetmap.org Git - osqa.git/commitdiff
be able to use the cursor caret when using the canned comments feature, include the...
authorjordan <jordan@0cfe37f9-358a-4d5e-be75-b63607b5c754>
Thu, 7 Jul 2011 16:21:02 +0000 (16:21 +0000)
committerjordan <jordan@0cfe37f9-358a-4d5e-be75-b63607b5c754>
Thu, 7 Jul 2011 16:21:02 +0000 (16:21 +0000)
git-svn-id: http://svn.osqa.net/svnroot/osqa/trunk@1112 0cfe37f9-358a-4d5e-be75-b63607b5c754

forum/skins/default/media/js/jquery.caret.js [new file with mode: 0644]
forum/skins/default/media/js/osqa.main.js
forum/skins/default/templates/question.html

diff --git a/forum/skins/default/media/js/jquery.caret.js b/forum/skins/default/media/js/jquery.caret.js
new file mode 100644 (file)
index 0000000..d22d511
--- /dev/null
@@ -0,0 +1,10 @@
+/*\r
+ *\r
+ * Copyright (c) 2010 C. F., Wong (<a href="http://cloudgen.w0ng.hk">Cloudgen Examplet Store</a>)\r
+ * Licensed under the MIT License:\r
+ * http://www.opensource.org/licenses/mit-license.php\r
+ *\r
+ */\r
+(function(k,e,i,j){k.fn.caret=function(b,l){var a,c,f=this[0],d=k.browser.msie;if(typeof b==="object"&&typeof b.start==="number"&&typeof b.end==="number"){a=b.start;c=b.end}else if(typeof b==="number"&&typeof l==="number"){a=b;c=l}else if(typeof b==="string")if((a=f.value.indexOf(b))>-1)c=a+b[e];else a=null;else if(Object.prototype.toString.call(b)==="[object RegExp]"){b=b.exec(f.value);if(b!=null){a=b.index;c=a+b[0][e]}}if(typeof a!="undefined"){if(d){d=this[0].createTextRange();d.collapse(true);\r
+d.moveStart("character",a);d.moveEnd("character",c-a);d.select()}else{this[0].selectionStart=a;this[0].selectionEnd=c}this[0].focus();return this}else{if(d){c=document.selection;if(this[0].tagName.toLowerCase()!="textarea"){d=this.val();a=c[i]()[j]();a.moveEnd("character",d[e]);var g=a.text==""?d[e]:d.lastIndexOf(a.text);a=c[i]()[j]();a.moveStart("character",-d[e]);var h=a.text[e]}else{a=c[i]();c=a[j]();c.moveToElementText(this[0]);c.setEndPoint("EndToEnd",a);g=c.text[e]-a.text[e];h=g+a.text[e]}}else{g=\r
+f.selectionStart;h=f.selectionEnd}a=f.value.substring(g,h);return{start:g,end:h,text:a,replace:function(m){return f.value.substring(0,g)+m+f.value.substring(h,f.value[e])}}}}})(jQuery,"length","createRange","duplicate");
\ No newline at end of file
index 0c532e37adbfc7da84bd61d7c26c9066311e55ad..7d6294f69c91c90b5ec12b8e4af5f77287bc6266 100644 (file)
@@ -25,10 +25,6 @@ $('html').ajaxSend(function(event, xhr, settings) {
     }\r
 });\r
 \r
-function canned_comment(post_id, comment) {\r
-    $('#comment-' + post_id + '-form textarea').val(comment);\r
-}\r
-\r
 var response_commands = {\r
     refresh_page: function() {\r
         window.location.reload(true)\r
@@ -369,7 +365,28 @@ function end_command(success) {
     }\r
 }\r
 \r
+var comment_box_cursor_position = 0;\r
+function canned_comment(post_id, comment) {\r
+    textarea = $('#comment-' + post_id + '-form textarea')\r
+\r
+    // Get the text from the beginning to the caret\r
+    textarea_start = textarea.val().substr(0, comment_box_cursor_position)\r
+\r
+    // Get the text from the caret to the end\r
+    textarea_end = textarea.val().substr(comment_box_cursor_position, textarea.val().length)\r
+\r
+    textarea.val(textarea_start + comment + textarea_endsvn );\r
+}\r
+\r
 $(function() {\r
+    $('textarea.commentBox').bind('keydown keyup mousedown mouseup mousemove', function(evt) {\r
+        comment_box_cursor_position = $(this).caret().start;\r
+    });\r
+\r
+    $('textarea.commentBox').blur(function() {\r
+        //alert(comment_box_cursor_position);\r
+    });\r
+\r
     $('a.ajax-command').live('click', function(evt) {\r
         if (running) return false;\r
 \r
index 4583518159f6053731476b76cb64a5ce4984408a..aff082b9f98a53d3733e782ce0ef81b9776a9ece 100644 (file)
@@ -18,6 +18,7 @@
 {% block forejs %}\r
         {% if not question.nis.closed %}\r
         <script type='text/javascript' src='{% media  "/media/js/osqa.question.js" %}'></script>\r
+        <script type='text/javascript' src='{% media  "/media/js/jquery.caret.js" %}'></script>\r
         <script type='text/javascript' src='{% media  "/media/js/wmd/showdown.js" %}'></script>\r
         <script type='text/javascript' src='{% media  "/media/js/wmd/wmd.js" %}'></script>\r
         <link rel="stylesheet" type="text/css" href="{% media  "/media/js/wmd/wmd.css" %}" />\r