]> git.openstreetmap.org Git - osqa.git/blobdiff - forum/skins/default/media/js/osqa.main.js
be able to submit comment only with Ctrl + Enter
[osqa.git] / forum / skins / default / media / js / osqa.main.js
index 274ba388f5e0659b359ff39873c9017c9082a916..0c16eeabed6e49166e8ef477d51db2c313947df6 100644 (file)
@@ -1,3 +1,32 @@
+/**\r
+ * 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
+        if (document.cookie && document.cookie != '') {\r
+            var cookies = document.cookie.split(';');\r
+            for (var i = 0; i < cookies.length; i++) {\r
+                var cookie = jQuery.trim(cookies[i]);\r
+                // Does this cookie string begin with the name we want?\r
+                if (cookie.substring(0, name.length + 1) == (name + '=')) {\r
+                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));\r
+                    break;\r
+                }\r
+            }\r
+        }\r
+        return cookieValue;\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
     refresh_page: function() {\r
         window.location.reload(true)\r
@@ -133,7 +162,6 @@ var response_commands = {
     },\r
 \r
     copy_url: function(url) {\r
-        $.copy(url);\r
     }\r
 }\r
 \r
@@ -155,11 +183,17 @@ function show_dialog (extern) {
         yes_callback: default_close_function,\r
         no_text: messages.cancel,\r
         show_no: false,\r
-        close_on_clickoutside: false\r
+        close_on_clickoutside: false,\r
+        copy: false\r
     }\r
 \r
     $.extend(options, extern);\r
 \r
+    var copy_id = '';\r
+    if (options.copy) {\r
+        copy_id = ' id="copy_clip_button"'\r
+    }\r
+\r
     if (options.event != undefined) {\r
         options.pos = {x: options.event.pageX, y: options.event.pageY};\r
     }\r
@@ -171,8 +205,7 @@ function show_dialog (extern) {
         html += '<button class="dialog-no">' + options.no_text + '</button>';\r
     }\r
 \r
-    html += '<button class="dialog-yes">' + options.yes_text + '</button>'\r
-            + '</div></div>';\r
+    html += '<button class="dialog-yes"' + copy_id + '>' + options.yes_text + '</button>' + '</div></div>';\r
 \r
     $dialog = $(html);\r
     $('body').append($dialog);\r
@@ -278,7 +311,12 @@ function load_prompt(evt, el, url) {
                     process_ajax_response(data, evt);\r
                 }, 'json');\r
             },\r
-            show_no: true\r
+            show_no: true,\r
+            copy: false\r
+        }\r
+\r
+        if (el.hasClass('copy')) {\r
+            $.extend(doptions, { yes_text : 'Copy', copy: true});\r
         }\r
 \r
         if (!el.is('.centered')) {\r
@@ -329,7 +367,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
@@ -550,6 +609,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
@@ -693,7 +760,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