X-Git-Url: https://git.openstreetmap.org./osqa.git/blobdiff_plain/1a949f7c97dc2f34c135f5cdf088df2927d3d652..17b2ea541c2ed1aa4edb28cc7b23c08510fab736:/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 a3ea374..8326eb0 100644
--- a/forum/skins/default/media/js/osqa.main.js
+++ b/forum/skins/default/media/js/osqa.main.js
@@ -1,12 +1,20 @@
var response_commands = {
- update_post_score: function(type, id, inc) {
- var $score_board = $('#' + type + '-' + id + '-score');
- $score_board.html(parseInt($score_board.html()) + inc)
+ refresh_page: function() {
+ window.location.reload(true)
+ },
+
+ update_post_score: function(id, inc) {
+ var $score_board = $('#post-' + id + '-score');
+ var current = parseInt($score_board.html())
+ if (isNaN(current)){
+ current = 0;
+ }
+ $score_board.html(current + inc)
},
- update_user_post_vote: function(type, id, vote_type) {
- var $upvote_button = $('#' + type + '-' + id + '-upvote');
- var $downvote_button = $('#' + type + '-' + id + '-downvote');
+ update_user_post_vote: function(id, vote_type) {
+ var $upvote_button = $('#post-' + id + '-upvote');
+ var $downvote_button = $('#post-' + id + '-downvote');
$upvote_button.removeClass('on');
$downvote_button.removeClass('on');
@@ -56,31 +64,6 @@ var response_commands = {
$answer.find('.accept-answer').removeClass('on');
},
- update_comment_score: function(id, inc) {
- var $comment_score = $('#comment-' + id + '-score');
- var count = parseInt($comment_score.html());
-
- if (isNaN(count))
- count = 0;
-
- count += inc;
-
- if (count == 0)
- count = '';
-
- $comment_score.html(count);
- },
-
- update_likes_comment_mark: function(id, like_type) {
- var $comment_like_mark = $("#comment-" + id + "-like");
-
- if (like_type == "on") {
- $comment_like_mark.addClass("on");
- } else {
- $comment_like_mark.removeClass("on");
- }
- },
-
remove_comment: function(id) {
var $comment = $('#comment-' + id);
$comment.css('background', 'red')
@@ -89,15 +72,16 @@ var response_commands = {
});
},
- insert_comment: function(post_type, post_id, comment_id, comment, username, profile_url, delete_url) {
- var $container = $('#comments-container-' + post_type + '-' + post_id);
- var skeleton = $('#new-comment-skeleton-' + post_type + '-' + post_id).html().toString();
+ insert_comment: function(post_id, comment_id, comment, username, profile_url, delete_url, edit_url) {
+ var $container = $('#comments-container-' + post_id);
+ var skeleton = $('#new-comment-skeleton-' + post_id).html().toString();
skeleton = skeleton.replace(new RegExp('%ID%', 'g'), comment_id)
.replace(new RegExp('%COMMENT%', 'g'), comment)
.replace(new RegExp('%USERNAME%', 'g'), username)
.replace(new RegExp('%PROFILE_URL%', 'g'), profile_url)
- .replace(new RegExp('%DELETE_URL%', 'g'), delete_url);
+ .replace(new RegExp('%DELETE_URL%', 'g'), delete_url)
+ .replace(new RegExp('%EDIT_URL%', 'g'), edit_url);
$container.append(skeleton);
@@ -121,6 +105,16 @@ var response_commands = {
}
},
+ unmark_deleted: function(post_type, post_id) {
+ if (post_type == 'answer') {
+ var $answer = $('#answer-container-' + post_id);
+ $answer.removeClass('deleted');
+ } else {
+ var $container = $('#question-table');
+ $container.removeClass('deleted');
+ }
+ },
+
set_subscription_button: function(text) {
$('.subscription_switch').html(text);
},
@@ -130,90 +124,324 @@ var response_commands = {
}
}
-function show_message(object, msg) {
- var div = $('
' + msg + '
(' +
- 'click to close' + ')');
+function show_dialog (extern) {
+ var default_close_function = function($diag) {
+ $diag.fadeOut('fast', function() {
+ $diag.remove();
+ });
+ }
+
+ var options = {
+ extra_class: '',
+ pos: {
+ x: ($(window).width() / 2) + $(window).scrollLeft(),
+ y: ($(window).height() / 2) + $(window).scrollTop()
+ },
+ dim: false,
+ yes_text: messages.ok,
+ yes_callback: default_close_function,
+ no_text: messages.cancel,
+ show_no: false,
+ close_on_clickoutside: false
+ }
+
+ $.extend(options, extern);
- div.click(function(event) {
- $(".vote-notification").fadeOut("fast", function() { $(this).remove(); });
+ if (options.event != undefined) {
+ options.pos = {x: options.event.pageX, y: options.event.pageY};
+ }
+
+ var html = '';
+
+ $dialog = $(html);
+ $('body').append($dialog);
+
+ if (options.dim === false) {
+ $dialog.css({
+ visibility: 'hidden',
+ display: 'block'
+ });
+
+ options.dim = {w: $dialog.width(), h: $dialog.height()};
+
+ $dialog.css({
+ width: 1,
+ height: 1,
+ visibility: 'visible'
+ });
+ }
+
+ $dialog.css({
+ top: options.pos.y,
+ left: options.pos.x
+ });
+
+ $dialog.animate({
+ top: "-=" + (options.dim.h / 2),
+ left: "-=" + (options.dim.w / 2),
+ width: options.dim.w,
+ height: options.dim.h
+ }, 200);
+
+ $dialog.find('.dialog-no').click(function() {
+ default_close_function($dialog);
});
- object.parent().append(div);
- div.fadeIn("fast");
+ $dialog.find('.dialog-yes').click(function() {
+ options.yes_callback($dialog);
+ });
+
+ if (options.close_on_clickoutside) {
+ $dialog.one('clickoutside', function() {
+ default_close_function($dialog);
+ });
+ }
+
+ return $dialog;
+}
+
+function show_message(evt, msg, callback) {
+ var $dialog = show_dialog({
+ html: msg,
+ extra_class: 'warning',
+ event: evt,
+ yes_callback: function() {
+ $dialog.fadeOut('fast', function() {
+ $dialog.remove();
+ });
+ if (callback) {
+ callback();
+ }
+ },
+ close_on_clickoutside: true
+ });
+}
+
+function load_prompt(evt, url) {
+ $.get(url, function(data) {
+ var $dialog = show_dialog({
+ html: data,
+ extra_class: 'prompt',
+ event: evt,
+ yes_callback: function() {
+ var postvars = {};
+ $dialog.find('input, textarea, select').each(function() {
+ postvars[$(this).attr('name')] = $(this).val();
+ });
+ $.post(url, postvars, function(data) {
+ $dialog.fadeOut('fast', function() {
+ $dialog.remove();
+ });
+ process_ajax_response(data, evt);
+ }, 'json');
+ },
+ show_no: true
+ });
+ });
}
-function process_ajax_response(data, el) {
+function process_ajax_response(data, evt, callback) {
if (!data.success && data['error_message'] != undefined) {
- show_message(el, data.error_message)
+ show_message(evt, data.error_message, function() {if (callback) callback(true);});
+ end_command(false);
} else if (typeof data['commands'] != undefined){
for (var command in data.commands) {
response_commands[command].apply(null, data.commands[command])
}
if (data['message'] != undefined) {
- show_message(el, data.message)
+ show_message(evt, data.message, function() {if (callback) callback(false);})
+ } else {
+ if (callback) callback(false);
}
+ end_command(true);
+ }
+}
+
+var running = false;
+
+function start_command() {
+ $('body').append($(''));
+ running = true;
+}
+
+function end_command(success) {
+ if (success) {
+ $('#command-loader').addClass('success');
+ $('#command-loader').fadeOut("slow", function() {
+ $('#command-loader').remove();
+ running = false;
+ });
+ } else {
+ $('#command-loader').remove();
+ running = false;
}
}
$(function() {
- $('a.ajax-command').live('click', function() {
+ $('a.ajax-command').live('click', function(evt) {
+ if (running) return false;
+
var el = $(this);
- $.getJSON(el.attr('href'), function(data) {
- process_ajax_response(data, el);
- });
+
+ if (el.is('.withprompt')) {
+ load_prompt(evt, el.attr('href'));
+ } else if(el.is('.confirm')) {
+ $dialog = show_dialog({
+ html: messages.confirm,
+ extra_class: 'confirm',
+ event: evt,
+ yes_callback: function() {
+ start_command();
+ $.getJSON(el.attr('href'), function(data) {
+ process_ajax_response(data, evt);
+ $dialog.fadeOut('fast', function() {
+ $dialog.remove();
+ });
+ });
+ },
+ yes_text: messages.yes,
+ show_no: true,
+ no_text: messages.no
+ });
+ } else {
+ start_command();
+ $.getJSON(el.attr('href'), function(data) {
+ process_ajax_response(data, evt);
+ });
+ }
return false
});
+ $('.context-menu').each(function() {
+ var $menu = $(this);
+ var $trigger = $menu.find('.context-menu-trigger');
+ var $dropdown = $menu.find('.context-menu-dropdown');
+
+ $trigger.click(function() {
+ $dropdown.slideToggle('fast', function() {
+ if ($dropdown.is(':visible')) {
+ $dropdown.one('clickoutside', function() {
+ $dropdown.slideUp('fast')
+ });
+ }
+ });
+ });
+ });
+
$('div.comment-form-container').each(function() {
var $container = $(this);
var $form = $container.find('form');
var $textarea = $container.find('textarea');
- var $button = $container.find('input[type="submit"]');
- var $chars_left_message = $('.comment-chars-left');
+ var textarea = $textarea.get(0);
+ var $button = $container.find('.comment-submit');
+ var $cancel = $container.find('.comment-cancel');
+ var $chars_left_message = $container.find('.comments-chars-left-msg');
+ var $chars_togo_message = $container.find('.comments-chars-togo-msg');
var $chars_counter = $container.find('.comments-char-left-count');
var $comment_tools = $container.parent().find('.comment-tools');
var $add_comment_link = $comment_tools.find('.add-comment-link');
var $comments_container = $container.parent().find('.comments-container');
- var max_length = parseInt($chars_counter.html());
+ var chars_limits = $chars_counter.html().split('|');
+
+ var min_length = parseInt(chars_limits[0]);
+ var max_length = parseInt(chars_limits[1]);
+
+ var warn_length = max_length - 30;
+ var current_length = 0;
var comment_in_form = false;
+ var interval = null;
+
+ var hcheck = !($.browser.msie || $.browser.opera);
+
+ $textarea.css("padding-top", 0).css("padding-bottom", 0).css("resize", "none");
+ textarea.style.overflow = 'hidden';
+
function cleanup_form() {
$textarea.val('');
+ $textarea.css('height', 80);
$chars_counter.html(max_length);
$chars_left_message.removeClass('warn');
comment_in_form = false;
+ current_length = 0;
+
+ $chars_left_message.hide();
+ $chars_togo_message.show();
+
+ $chars_counter.removeClass('warn');
+ $chars_counter.html(min_length);
+ $button.attr("disabled","disabled");
+
+ interval = null;
}
cleanup_form();
- function calculate_chars_left() {
- var length = $textarea.val().length;
- var allow = true;
+ function process_form_changes() {
+ var length = $textarea.val().replace(/[ ]{2,}/g," ").length;
- if (length < max_length) {
- if (length < max_length * 0.75) {
- $chars_left_message.removeClass('warn');
- } else {
- $chars_left_message.addClass('warn');
- }
+ if (current_length == length)
+ return;
+
+ if (length < warn_length && current_length >= warn_length) {
+ $chars_counter.removeClass('warn');
+ } else if (current_length < warn_length && length >= warn_length){
+ $chars_counter.addClass('warn');
+ }
+
+ if (length < min_length) {
+ $chars_left_message.hide();
+ $chars_togo_message.show();
+ $chars_counter.html(min_length - length);
} else {
- allow = false;
+ length = $textarea.val().length;
+ $chars_togo_message.hide();
+ $chars_left_message.show();
+ $chars_counter.html(max_length - length);
}
- $chars_counter.html(max_length - length);
- return allow;
+ if (length > max_length || length < min_length) {
+ $button.attr("disabled","disabled");
+ } else {
+ $button.removeAttr("disabled");
+ }
+
+ var current_height = textarea.style.height;
+ if (hcheck)
+ textarea.style.height = "0px";
+
+ var h = Math.max(80, textarea.scrollHeight);
+ textarea.style.height = current_height;
+ $textarea.animate({height: h + 'px'}, 50);
+
+ current_length = length;
}
function show_comment_form() {
$container.slideDown('slow');
$add_comment_link.fadeOut('slow');
+ window.setInterval(function() {
+ process_form_changes();
+ }, 200);
}
function hide_comment_form() {
+ if (interval != null) {
+ window.clearInterval(interval);
+ interval = null;
+ }
$container.slideUp('slow');
$add_comment_link.fadeIn('slow');
}
@@ -234,37 +462,48 @@ $(function() {
$('#' + $comments_container.attr('id') + ' .comment-edit').live('click', function() {
var $link = $(this);
var comment_id = /comment-(\d+)-edit/.exec($link.attr('id'))[1];
- var $comment = $link.parents('.comment');
- var comment_text = $comment.find('.comment-text').text().trim();
+ var $comment = $('#comment-' + comment_id);
comment_in_form = comment_id;
- $textarea.val(comment_text);
- calculate_chars_left();
+
+ $.get($link.attr('href'), function(data) {
+ $textarea.val(data);
+ });
+
$comment.slideUp('slow');
show_comment_form();
return false;
});
- $textarea.keyup(calculate_chars_left);
-
- $button.click(function() {
- if ($textarea.val().length > max_length) {
- show_message($button, "Your comment exceeds the max number of characters allowed.");
- } else {
- var post_data = {
- comment: $textarea.val()
- }
+ $button.click(function(evt) {
+ if (running) return false;
- if (comment_in_form) {
- post_data['id'] = comment_in_form;
- }
+ var post_data = {
+ comment: $textarea.val()
+ }
- $.post($form.attr('action'), post_data, function(data) {
- process_ajax_response(data, $button);
- cleanup_form();
- }, "json")
+ if (comment_in_form) {
+ post_data['id'] = comment_in_form;
}
+ start_command();
+ $.post($form.attr('action'), post_data, function(data) {
+ process_ajax_response(data, evt, function(error) {
+ if (!error) {
+ cleanup_form();
+ hide_comment_form();
+ }
+ });
+
+ }, "json");
+
+ return false;
+ });
+
+ $cancel.click(function() {
+ if (comment_in_form) {
+ $comment = $('#comment-' + comment_in_form).slideDown('slow');
+ }
hide_comment_form();
return false;
});
@@ -421,17 +660,19 @@ function pickedTags(){
init: function(){
collectPickedTags();
setupHideIgnoredQuestionsControl();
- $("#interestingTagInput, #ignoredTagInput").autocomplete(tags, {
+ $("#interestingTagInput, #ignoredTagInput").autocomplete("/matching_tags", {
minChars: 1,
matchContains: true,
max: 20,
- multiple: true,
- multipleSeparator: " ",
- formatItem: function(row, i, max) {
- return row.n + " ("+ row.c +")";
+ /*multiple: false, - the favorite tags and ignore tags don't let you do multiple tags
+ multipleSeparator: " "*/
+
+ formatItem: function(row, i, max, value) {
+ return row[1].split(".")[0] + " (" + row[1].split(".")[1] + ")";
},
- formatResult: function(row, i, max){
- return row.n;
+
+ formatResult: function(row, i, max, value){
+ return row[0];
}
});
@@ -442,7 +683,7 @@ function pickedTags(){
}
$(document).ready( function(){
- if (window.tags != undefined)
+ //if (window.tags != undefined)
pickedTags().init();
});
@@ -774,14 +1015,20 @@ var notify = function() {
visible = true;
},
close: function(doPostback) {
- if (doPostback) {
- $.post(scriptUrl + $.i18n._("messages/") +
- $.i18n._("markread/"), { formdata: "required" });
- }
$(".notify").fadeOut("fast");
$("body").css("margin-top", "0");
visible = false;
},
isVisible: function() { return visible; }
};
-} ();
\ No newline at end of file
+} ();
+
+/*
+ * jQuery outside events - v1.1 - 3/16/2010
+ * http://benalman.com/projects/jquery-outside-events-plugin/
+ *
+ * Copyright (c) 2010 "Cowboy" Ben Alman
+ * Dual licensed under the MIT and GPL licenses.
+ * http://benalman.com/about/license/
+ */
+(function($,c,b){$.map("click dblclick mousemove mousedown mouseup mouseover mouseout change select submit keydown keypress keyup".split(" "),function(d){a(d)});a("focusin","focus"+b);a("focusout","blur"+b);$.addOutsideEvent=a;function a(g,e){e=e||g+b;var d=$(),h=g+"."+e+"-special-event";$.event.special[e]={setup:function(){d=d.add(this);if(d.length===1){$(c).bind(h,f)}},teardown:function(){d=d.not(this);if(d.length===0){$(c).unbind(h)}},add:function(i){var j=i.handler;i.handler=function(l,k){l.target=k;j.apply(this,arguments)}}};function f(i){$(d).each(function(){var j=$(this);if(this!==i.target&&!j.has(i.target).length){j.triggerHandler(e,[i.target])}})}}})(jQuery,document,"outside");
\ No newline at end of file