prepare ajax

This commit is contained in:
Thorsten Witteler 2017-10-20 15:08:26 +02:00
parent 0c00764ef5
commit de3a683cbe
4 changed files with 54 additions and 10 deletions

View File

@ -11,7 +11,7 @@ $(document).ready(function () {
}
PhpComment.prototype.setupVariables = function () {
this.commentForm = this.element.find(".comment-form");
this.commentForm = this.element.find(".comments-form");
this.titleField = this.element.find("#comment_title");
this.bodyField = this.element.find("#comment_body");
}
@ -35,8 +35,8 @@ $(document).ready(function () {
title = phpComment.titleField.val(),
body = phpComment.bodyField.val();
if(phpComment.commentForm.parents(".media").length > 0){
parentId = phpComment.commentForm.closest(".media").attr("data-Id");
if(phpComment.commentForm.parents(".comment").length > 0){
parentId = phpComment.commentForm.closest(".comment").attr("data-Id");
}
$.ajax({
@ -63,8 +63,7 @@ $(document).ready(function () {
$(document).on("click", ".comment-add-new", function (e) {
e.preventDefault();
var media = $(this).closest(".comments");
media.find(">.comment-body>.comment-text").after(phpComment.commentForm);
$(this).find(".comments").before(phpComment.commentForm);
});
$(document).on("click", ".comment-add-reply", function (e) {
e.preventDefault();

View File

@ -56,6 +56,13 @@ class CommentsPlugin extends Plugin
public function onTwigSiteVariables() {
$this->grav['twig']->enable_comments_plugin = $this->enable;
$this->grav['twig']->comments = $this->fetchComments();
if ($this->config->get('plugins.comments.built_in_css')) {
$this->grav['assets']
->addCss('plugin://comments/assets/comments.css');
}
$this->grav['assets']
->add('jquery', 101)
->addJs('plugin://comments/assets/comments.js');
}
/**
@ -166,15 +173,52 @@ class CommentsPlugin extends Plugin
// $this->total_stars = $this->config->get('plugins.star-ratings.total_stars');
// $this->only_full_stars = $this->config->get('plugins.star-ratings.only_full_stars');
// Process vote if required
// Process comment if required
if ($this->callback === $this->grav['uri']->path()) {
// try to add the vote
$result = $this->addVote();
// try to add the comment
$result = $this->addComment();
echo json_encode(['status' => $result[0], 'message' => $result[1], 'data' => ['score' => $result[2][0], 'count' => $result[2][1]]]);
exit();
}
}
public function addComment()
{
$nonce = $this->grav['uri']->param('nonce');
if (!Utils::verifyNonce($nonce, 'comments')) {
return [false, 'Invalid security nonce', [0, 0]];
}
$language = $this->grav['language'];
// get and filter the data
$parent_id = filter_input(INPUT_POST, 'parent_id', FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION);
$email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_STRING);
$text = filter_input(INPUT_POST, 'text', FILTER_SANITIZE_STRING);
$title = filter_input(INPUT_POST, 'title', FILTER_SANITIZE_STRING);
$name = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING);
//$data = $this->getStars($id);
$data = array(
['parent_id'] => $parent_id,
['email'] => $email,
['text'] => $text,
['title'] => $title,
['name'] => $name,
);
// ensure both values are sent
if (is_null($title) || is_null($text)) {
return [false, 'missing either text or title', [0, 0]];
//return [false, $language->translate('PLUGIN_COMMENTS.FAIL'), $data];
}
// sanity checks for parents
if ($parent_id < 0) {
$parent_id = 0;
} elseif ($parent_id > 999 ) { //TODO: Change to 'exists in list of comment ids
$parent_id = 0;
}
//$this->saveVoteData($id, $rating);
//$data = $this->getStars($id);
return [true, $language->translate('PLUGIN_COMMENTS.SUCCESS'), $data];
}
/**
* Handle form processing instructions.

View File

@ -1,5 +1,5 @@
<h3>{{'PLUGIN_COMMENTS.ADD_COMMENT'|t}}</h3>
<form name="{{ grav.config.plugins.comments.form.name }}"
<form name="{{ grav.config.plugins.comments.form.name }}" class="comments-form"
action="{{ grav.config.plugins.comments.form.action ? base_url ~ grav.config.plugins.comments.form.action : page.url }}"
method="{{ grav.config.plugins.comments.form.method|upper|default('POST') }}">

View File

@ -6,6 +6,7 @@
{% if grav.twig.comments|length %}
<h3>{{'PLUGIN_COMMENTS.COMMENTS'|t}}</h3>
<a class="comment-add-new" href="#">{{'PLUGIN_COMMENTS.ADD_NEW'|t}}</a>
<div class="row comments">
{% for comment in grav.twig.comments|array_reverse %}
<div class="comment comment-level-{{comment.level|e}}" data-Id="{{comment.id}}" >
@ -17,7 +18,7 @@
<div class="comment-body">
<div class="comment-heading">
<div class="comment-title"><h4>{{comment.title}}</h4></div>
<div class="comment-reply"><a class="reply-link" href="#">Reply</a></div>
<div class="comment-reply"><a class="comment-add-reply" href="#">{{'PLUGIN_COMMENTS.ADD_REPLY'|t}}</a></div>
<div class="comment-meta">{{'PLUGIN_COMMENTS.WRITTEN_ON'|t}} {{comment.date|e}} {{'PLUGIN_COMMENTS.BY'|t}} {{comment.author}}</div>
</div>
<div class="comment-text" >