]> git.openstreetmap.org Git - osqa.git/blobdiff - forum/skins/default/media/js/osqa.main.js
check the setting before deciding whether to append or to prepend
[osqa.git] / forum / skins / default / media / js / osqa.main.js
index 8a3dbf9a44b227e90cd333e4404fd95a0ded063b..89a59ef83cc45b892494ff4ea21d0cf2b57936c1 100644 (file)
@@ -2,6 +2,7 @@
  * We do not want the CSRF protection enabled for the AJAX post requests, it causes only trouble.\r
  * Get the csrftoken cookie and pass it to the X-CSRFToken HTTP request property.\r
  */\r
+\r
 $('html').ajaxSend(function(event, xhr, settings) {\r
     function getCookie(name) {\r
         var cookieValue = null;\r
@@ -18,10 +19,12 @@ $('html').ajaxSend(function(event, xhr, settings) {
         }\r
         return cookieValue;\r
     }\r
-    if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {\r
-        // Only send the token to relative URLs i.e. locally.\r
-        xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));\r
-    }\r
+    try {\r
+        if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {\r
+            // Only send the token to relative URLs i.e. locally.\r
+            xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));\r
+        }\r
+    } catch (e) {}\r
 });\r
 \r
 var response_commands = {\r
@@ -101,7 +104,7 @@ var response_commands = {
         alert('ok');\r
     },\r
 \r
-    insert_comment: function(post_id, comment_id, comment, username, profile_url, delete_url, edit_url, convert_url, can_convert) {\r
+    insert_comment: function(post_id, comment_id, comment, username, profile_url, delete_url, edit_url, convert_url, can_convert, show_latest_comments_first) {\r
         var $container = $('#comments-container-' + post_id);\r
         var skeleton = $('#new-comment-skeleton-' + post_id).html().toString();\r
 \r
@@ -112,8 +115,11 @@ var response_commands = {
                 .replace(new RegExp('%DELETE_URL%', 'g'), delete_url)\r
                 .replace(new RegExp('%EDIT_URL%', 'g'), edit_url)\r
                 .replace(new RegExp('%CONVERT_URL%', 'g'), convert_url);\r
-\r
-        $container.append(skeleton);\r
+        if (show_latest_comments_first) {\r
+            $container.prepend(skeleton);\r
+        } else {\r
+            $container.append(skeleton);\r
+        }\r
 \r
         // Show the convert comment to answer tool only if the current comment can be converted\r
         if (can_convert == true) {\r
@@ -123,10 +129,6 @@ var response_commands = {
         $('#comment-' + comment_id).slideDown('slow');\r
     },\r
 \r
-    canned_comment: function(post_id, comment) {\r
-        $('#comment-' + post_id + '-form textarea').val(comment);\r
-    },\r
-\r
     update_comment: function(comment_id, comment_text) {\r
         var $comment = $('#comment-' + comment_id);\r
         $comment.find('.comment-text').html(comment_text);\r
@@ -332,7 +334,8 @@ function process_ajax_response(data, evt, callback) {
     if (!data.success && data['error_message'] != undefined) {\r
         show_message(evt, data.error_message, function() {if (callback) callback(true);});\r
         end_command(false);\r
-    } else if (typeof data['commands'] != undefined){\r
+    }\r
+    if (typeof data['commands'] != undefined){\r
         for (var command in data.commands) {\r
             response_commands[command].apply(null, data.commands[command])\r
 \r
@@ -368,7 +371,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_end);\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
@@ -576,7 +600,7 @@ $(function() {
                 }\r
 \r
                 start_command();\r
-                $.post($form.attr('action'), $form.serialize(), function(data) {\r
+                $.post($form.attr('action'), post_data, function(data) {\r
                     process_ajax_response(data, evt, function(error) {\r
                         if (!error) {\r
                             cleanup_form();\r
@@ -589,6 +613,14 @@ $(function() {
                 return false;\r
             });\r
 \r
+            // Submit comment with CTRL + Enter\r
+            $textarea.keydown(function(e) {\r
+                if (e.ctrlKey && e.keyCode == 13 && !$button.attr('disabled')) {\r
+                    // console.log('submit');\r
+                    $(this).parent().find('input.comment-submit').click();\r
+                }\r
+            });\r
+\r
             $cancel.click(function(event) {\r
                 if (confirm("You will lose all of your changes in this comment.  Do you still wish to proceed?")){\r
                     if (comment_in_form) {\r
@@ -732,7 +764,7 @@ function pickedTags(){
                 tag_link.attr('rel','tag');\r
                 tag_link.attr('href', scriptUrl + $.i18n._('tags/') + tagname + '/');\r
                 tag_link.html(tagname);\r
-                var del_link = $('<img></img>');\r
+                var del_link = $('<img />');\r
                 del_link.addClass('delete-icon');\r
                 del_link.attr('src', mediaUrl('media/images/close-small-dark.png'));\r
 \r