X-Git-Url: https://git.openstreetmap.org./osqa.git/blobdiff_plain/c579fa70d5febab89dcb5b3811ed8d926127bff1..ac7428c4b191d718cf8171fcb57ddab881f61b25:/forum/skins/default/media/js/osqa.main.js diff --git a/forum/skins/default/media/js/osqa.main.js b/forum/skins/default/media/js/osqa.main.js index 2de5ecf..55a3f1e 100644 --- a/forum/skins/default/media/js/osqa.main.js +++ b/forum/skins/default/media/js/osqa.main.js @@ -1,3 +1,32 @@ +/** + * We do not want the CSRF protection enabled for the AJAX post requests, it causes only trouble. + * Get the csrftoken cookie and pass it to the X-CSRFToken HTTP request property. + */ + +$('html').ajaxSend(function(event, xhr, settings) { + function getCookie(name) { + var cookieValue = null; + if (document.cookie && document.cookie != '') { + var cookies = document.cookie.split(';'); + for (var i = 0; i < cookies.length; i++) { + var cookie = jQuery.trim(cookies[i]); + // Does this cookie string begin with the name we want? + if (cookie.substring(0, name.length + 1) == (name + '=')) { + cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); + break; + } + } + } + return cookieValue; + } + try { + if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) { + // Only send the token to relative URLs i.e. locally. + xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken')); + } + } catch (e) {} +}); + var response_commands = { refresh_page: function() { window.location.reload(true) @@ -75,7 +104,7 @@ var response_commands = { alert('ok'); }, - insert_comment: function(post_id, comment_id, comment, username, profile_url, delete_url, edit_url, convert_url) { + insert_comment: function(post_id, comment_id, comment, username, profile_url, delete_url, edit_url, convert_url, can_convert, show_latest_comments_first) { var $container = $('#comments-container-' + post_id); var skeleton = $('#new-comment-skeleton-' + post_id).html().toString(); @@ -86,8 +115,16 @@ var response_commands = { .replace(new RegExp('%DELETE_URL%', 'g'), delete_url) .replace(new RegExp('%EDIT_URL%', 'g'), edit_url) .replace(new RegExp('%CONVERT_URL%', 'g'), convert_url); + if (show_latest_comments_first) { + $container.prepend(skeleton); + } else { + $container.append(skeleton); + } - $container.append(skeleton); + // Show the convert comment to answer tool only if the current comment can be converted + if (can_convert == true) { + $('#comment-' + comment_id + '-convert').show(); + } $('#comment-' + comment_id).slideDown('slow'); }, @@ -128,7 +165,6 @@ var response_commands = { }, copy_url: function(url) { - $.copy(url); } } @@ -137,7 +173,7 @@ function show_dialog (extern) { $diag.fadeOut('fast', function() { $diag.remove(); }); - } + }; var options = { extra_class: '', @@ -150,26 +186,39 @@ function show_dialog (extern) { yes_callback: default_close_function, no_text: messages.cancel, show_no: false, - close_on_clickoutside: false + close_on_clickoutside: false, + copy: false } $.extend(options, extern); - if (options.event != undefined) { + var copy_id = ''; + if (options.copy) { + copy_id = ' id="copy_clip_button"' + } + + if (options.event != undefined && options.event.pageX != undefined && options.event.pageY != undefined) { options.pos = {x: options.event.pageX, y: options.event.pageY}; + } else if (options.event.currentTarget != undefined) { + var el = jQuery("#" + options.event.currentTarget.id); + var position = el.offset(); + options.pos = { + x: position.left, + y: position.top + } } - var html = '
'; + + var $dialog = $(html); - $dialog = $(html); $('body').append($dialog); var message = $('.dialog-content')[0]; message.style.visibility = "hidden"; @@ -222,14 +271,20 @@ function show_dialog (extern) { message.style.visibility = "visible"; }); - $dialog.find('.dialog-no').click(function() { - default_close_function($dialog); - }); - $dialog.find('.dialog-yes').click(function() { options.yes_callback($dialog); }); + if (options.hasOwnProperty("no_callback")) { + $dialog.find('.dialog-no:first-child').click(function() { + options.no_callback($dialog); + }); + } else { + $dialog.find('.dialog-no:first-child').click(function() { + default_close_function($dialog); + }); + } + if (options.close_on_clickoutside) { $dialog.one('clickoutside', function() { default_close_function($dialog); @@ -273,7 +328,12 @@ function load_prompt(evt, el, url) { process_ajax_response(data, evt); }, 'json'); }, - show_no: true + show_no: true, + copy: false + } + + if (el.hasClass('copy')) { + $.extend(doptions, { yes_text : 'Copy', copy: true}); } if (!el.is('.centered')) { @@ -288,7 +348,8 @@ function process_ajax_response(data, evt, callback) { if (!data.success && data['error_message'] != undefined) { show_message(evt, data.error_message, function() {if (callback) callback(true);}); end_command(false); - } else if (typeof data['commands'] != undefined){ + } + if (typeof data['commands'] != undefined){ for (var command in data.commands) { response_commands[command].apply(null, data.commands[command]) @@ -324,7 +385,28 @@ function end_command(success) { } } +var comment_box_cursor_position = 0; +function canned_comment(post_id, comment) { + textarea = $('#comment-' + post_id + '-form textarea') + + // Get the text from the beginning to the caret + textarea_start = textarea.val().substr(0, comment_box_cursor_position) + + // Get the text from the caret to the end + textarea_end = textarea.val().substr(comment_box_cursor_position, textarea.val().length) + + textarea.val(textarea_start + comment + textarea_end); +} + $(function() { + $('textarea.commentBox').bind('keydown keyup mousedown mouseup mousemove', function(evt) { + comment_box_cursor_position = $(this).caret().start; + }); + + $('textarea.commentBox').blur(function() { + //alert(comment_box_cursor_position); + }); + $('a.ajax-command').live('click', function(evt) { if (running) return false; @@ -361,8 +443,14 @@ $(function() { var $dialog = show_dialog(doptions); } else { start_command(); - $.getJSON(ajax_url, function(data) { - process_ajax_response(data, evt); + $.ajax({ + url: ajax_url, + type: "POST", + dataType: "json", + contentType: "application/json; charset=utf-8", + success: function(data) { + process_ajax_response(data, evt); + } }); } @@ -545,6 +633,14 @@ $(function() { return false; }); + // Submit comment with CTRL + Enter + $textarea.keydown(function(e) { + if (e.ctrlKey && e.keyCode == 13 && !$button.attr('disabled')) { + // console.log('submit'); + $(this).parent().find('input.comment-submit').click(); + } + }); + $cancel.click(function(event) { if (confirm("You will lose all of your changes in this comment. Do you still wish to proceed?")){ if (comment_in_form) { @@ -635,14 +731,15 @@ function pickedTags(){ var deleteTagLocally = function(){ from_target[tagname].remove(); delete from_target[tagname]; + $(".tags.interesting").trigger('contentchanged'); }; + if (send_ajax){ sendAjax(tagname,reason,'remove',deleteTagLocally); } else { deleteTagLocally(); } - }; var setupTagDeleteEvents = function(obj,tag_store,tagname,reason,send_ajax){ @@ -688,7 +785,7 @@ function pickedTags(){ tag_link.attr('rel','tag'); tag_link.attr('href', scriptUrl + $.i18n._('tags/') + tagname + '/'); tag_link.html(tagname); - var del_link = $(''); + var del_link = $(''); del_link.addClass('delete-icon'); del_link.attr('src', mediaUrl('media/images/close-small-dark.png')); @@ -699,6 +796,8 @@ function pickedTags(){ to_tag_container.append(new_tag); to_target[tagname] = new_tag; + + to_tag_container.trigger('contentchanged'); }); } };