prepare ajax

This commit is contained in:
Thorsten Witteler 2017-10-20 14:06:02 +02:00
parent 6d458f8fa4
commit 7d153816f9
2 changed files with 109 additions and 0 deletions

83
assets/comments.js Normal file
View File

@ -0,0 +1,83 @@
$(document).ready(function () {
function PhpComment(element) {
this.element = element;
this.init();
}
PhpComment.prototype.init = function () {
this.setupVariables();
this.setupEvents();
}
PhpComment.prototype.setupVariables = function () {
this.commentForm = this.element.find(".comment-form");
this.titleField = this.element.find("#comment_title");
this.bodyField = this.element.find("#comment_body");
}
PhpComment.prototype.setupEvents = function () {
var phpComment = this,
newMedia;
$.ajax({
url: '/media_template.php',
method: 'GET',
dataType: 'html',
success: function (data) {
newMedia = data;
}
});
phpComment.commentForm.on("submit", function (e) {
e.preventDefault();
var parentId = 0,
title = phpComment.titleField.val(),
body = phpComment.bodyField.val();
if(phpComment.commentForm.parents(".media").length > 0){
parentId = phpComment.commentForm.closest(".media").attr("data-Id");
}
$.ajax({
url: phpComment.commentForm.attr("action"),
method: 'POST',
dataType: 'json',
data: {title: title, body: body, parentId: parentId},
success: function (data) {
if(!data.created){
alert("Couldn't create comment");
return;
}
newMedia = newMedia.replace("{{id}}", data.id);
newMedia = newMedia.replace("{{title}}", title);
newMedia = newMedia.replace("{{body}}", body);
newMedia = newMedia.replace("{{nested}}", '');
phpComment.commentForm.before(newMedia);
phpComment.titleField.val("");
phpComment.bodyField.val("");
}
});
});
$(document).on("click", ".comment-add-new", function (e) {
e.preventDefault();
var media = $(this).closest(".comments");
media.find(">.comment-body>.comment-text").after(phpComment.commentForm);
});
$(document).on("click", ".comment-add-reply", function (e) {
e.preventDefault();
var media = $(this).closest(".comment");
media.find(">.comment-body>.comment-text").after(phpComment.commentForm);
});
}
$.fn.phpComment = function (options) {
new PhpComment(this);
return this;
}
$(".comments").phpComment();
});

View File

@ -96,6 +96,7 @@ class CommentsPlugin extends Plugin
if ($this->enable) {
$this->enable([
'onPageInitialized' => ['onPageInitialized', 0],
'onFormProcessed' => ['onFormProcessed', 0],
'onFormPageHeaderProcessed' => ['onFormPageHeaderProcessed', 0],
'onTwigSiteVariables' => ['onTwigSiteVariables', 0]
@ -150,6 +151,31 @@ class CommentsPlugin extends Plugin
}
}
/**
* Handle ajax call.
*/
public function onPageInitialized()
{
// initialize with page settings (post-cache)
// if (!$this->isAdmin() && isset($this->grav['page']->header()->{'star-ratings'})) {
// // if not in admin merge potential page-level configs
// $this->config->set('plugins.star-ratings', $this->mergeConfig($page));
// }
$this->callback = 'nested-comments';
// $this->callback = $this->config->get('plugins.star-ratings.callback');
// $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
if ($this->callback === $this->grav['uri']->path()) {
// try to add the vote
$result = $this->addVote();
echo json_encode(['status' => $result[0], 'message' => $result[1], 'data' => ['score' => $result[2][0], 'count' => $result[2][1]]]);
exit();
}
}
/**
* Handle form processing instructions.
*