From 7d153816f9abb5a9b235511ce46f6acea3c5bcb0 Mon Sep 17 00:00:00 2001 From: Thorsten Witteler Date: Fri, 20 Oct 2017 14:06:02 +0200 Subject: [PATCH] prepare ajax --- assets/comments.js | 83 ++++++++++++++++++++++++++++++++++++++++++++++ comments.php | 26 +++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 assets/comments.js diff --git a/assets/comments.js b/assets/comments.js new file mode 100644 index 0000000..bd499bd --- /dev/null +++ b/assets/comments.js @@ -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(); + +}); diff --git a/comments.php b/comments.php index 4771e57..832e6a9 100644 --- a/comments.php +++ b/comments.php @@ -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. *