mirror of https://github.com/IoTcat/ushio-img.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
170 lines
5.7 KiB
170 lines
5.7 KiB
// Generated by CoffeeScript 1.8.0 |
|
(function() { |
|
var set_comment_error; |
|
|
|
set_comment_error = function(error) { |
|
$('.comment_error').css('display', 'block'); |
|
return $('.comment_error').html(error); |
|
}; |
|
|
|
this.scroll_to_comment_form = function() { |
|
var comment_text_dom, scroll_to_top_position; |
|
comment_text_dom = $('#new_comment_form textarea'); |
|
scroll_to_top_position = comment_text_dom.offset().top - 80; |
|
if (scroll_to_top_position < 0) { |
|
scroll_to_top_position = 0; |
|
} |
|
$('body,html').animate({ |
|
scrollTop: scroll_to_top_position |
|
}, 800); |
|
return comment_text_dom.focus(); |
|
}; |
|
|
|
this.reply_comment = function(nickname, reply_to) { |
|
var text_dom, new_text, old_text, origin_comment_dom, to_append; |
|
text_dom = $('#new_comment_form textarea'); |
|
old_text = text_dom.val() || ''; |
|
if (nickname.indexOf(' ') === -1) { |
|
to_append = '@' + nickname + ' '; |
|
} else { |
|
to_append = '@' + nickname + ', '; |
|
} |
|
if (old_text.indexOf(to_append) !== -1) { |
|
to_append = ''; |
|
} |
|
new_text = old_text + to_append; |
|
reply_to = reply_to || ''; |
|
$('#reply_to_id').val(reply_to); |
|
if (reply_to) { |
|
origin_comment_dom = $('#' + reply_to); |
|
if (origin_comment_dom.length) { |
|
origin_comment_dom.append($('#new_comment_form')); |
|
} |
|
} |
|
text_dom.click(); |
|
text_dom.val(new_text); |
|
scroll_to_comment_form(); |
|
return false; |
|
}; |
|
|
|
$(document).ready(function() { |
|
var author_dom, text_dom, mail_dom, path_dom, url_dom; |
|
author_dom = $('#new_comment_form input[name="author"]'); |
|
mail_dom = $('#new_comment_form input[name="mail"]'); |
|
url_dom = $('#new_comment_form input[name="url"]'); |
|
path_dom = $('#new_comment_form input[name="path"]'); |
|
text_dom = $('#new_comment_form textarea'); |
|
$(".new_comment").click(function() { |
|
$(".comment_trigger").hide(); |
|
$(".comment_triggered").fadeIn("slow"); |
|
if (author_dom.length) { |
|
author_dom.val(author_dom.val() || Cookies.get('comment_author') || ''); |
|
} |
|
if (mail_dom.length) { |
|
mail_dom.val(mail_dom.val() || Cookies.get('comment_mail') || ''); |
|
} |
|
if (url_dom.length) { |
|
return url_dom.val(url_dom.val() || Cookies.get('comment_url') || ''); |
|
} |
|
}); |
|
$('.new_comment textarea').keyup(function(event) { |
|
var current_height, new_comment_form; |
|
current_height = $(this).height(); |
|
if (current_height < this.scrollHeight && current_height < 350) { |
|
$(this).height(this.scrollHeight); |
|
} |
|
if (event.which === 27) { |
|
new_comment_form = $('#new_comment_form'); |
|
if (!new_comment_form.parent().hasClass('new_comment_form_container')) { |
|
$('.new_comment_form_container').append(new_comment_form); |
|
$('#reply_to_id').val(''); |
|
return scroll_to_comment_form(); |
|
} |
|
} |
|
}); |
|
return $('.comment_submit_button').click(function() { |
|
var author, text, data_to_post, mail, new_comment_form, parent_comment_id, url; |
|
author = author_dom.val() || ''; |
|
mail = mail_dom.val() || ''; |
|
url = url_dom.val() || ''; |
|
text = text_dom.val(); |
|
parent_comment_id = $('#reply_to_id').val() || ''; |
|
new_comment_form = $('#new_comment_form'); |
|
data_to_post = { |
|
author: author, |
|
mail: mail, |
|
url: url, |
|
text: text, |
|
path: path_dom.val(), |
|
reply: parent_comment_id, |
|
}; |
|
if (text.length < 5) { |
|
set_comment_error('评论最少需要5个字!'); |
|
text_dom.focus(); |
|
return false; |
|
} |
|
if (!mail && mail_dom.length) { |
|
set_comment_error('邮箱不能为空'); |
|
text_dom.focus(); |
|
return false; |
|
} |
|
if (author) { |
|
Cookies.set('comment_author', author, { |
|
expires: 9999 |
|
}); |
|
} |
|
if (mail) { |
|
Cookies.set('comment_mail', mail, { |
|
expires: 9999 |
|
}); |
|
} |
|
if (url) { |
|
Cookies.set('comment_url', url, { |
|
expires: 9999 |
|
}); |
|
} |
|
$.ajax({ |
|
url: new_comment_form.attr('action'), |
|
type: 'post', |
|
data: data_to_post, |
|
success: function(data) { |
|
var new_comment_dom, parent_comment_dom, sub_comments_dom, sub_comments_filter; |
|
$('.comment_error').css('display', 'none'); |
|
if (data.error) { |
|
return set_comment_error(data.error); |
|
} else { |
|
if (typeof data === 'string') { |
|
text_dom.val(''); |
|
if (parent_comment_id && $('#' + parent_comment_id).length) { |
|
parent_comment_dom = $('#' + parent_comment_id); |
|
sub_comments_filter = '#' + parent_comment_id + ' ul.sub_comments'; |
|
if (!$(sub_comments_filter).length) { |
|
parent_comment_dom.append('<ul class="sub_comments"></ul>'); |
|
} |
|
sub_comments_dom = $(sub_comments_filter); |
|
sub_comments_dom.append(data); |
|
new_comment_dom = $(sub_comments_filter + ' .comment').last(); |
|
$('.new_comment_form_container').append(new_comment_form); |
|
} else { |
|
$('.comments').append(data); |
|
new_comment_dom = $('.comments .comment').last(); |
|
} |
|
$('html, body').animate({ |
|
scrollTop: new_comment_dom.offset().top |
|
}, 500, 'swing', function() { |
|
return new_comment_dom.fadeIn(150).fadeOut(150).fadeIn(150); |
|
}); |
|
} |
|
return console.log(data); |
|
} |
|
}, |
|
fail: function(data) { |
|
console.log(data); |
|
return console.log('failed'); |
|
} |
|
}); |
|
return false; |
|
}); |
|
}); |
|
|
|
}).call(this);
|
|
|