Skip to content

Commit

Permalink
HTMX comments and new components
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelfolaron committed Jul 4, 2024
1 parent 34f7ae8 commit ccfdb4f
Show file tree
Hide file tree
Showing 34 changed files with 397 additions and 364 deletions.
2 changes: 1 addition & 1 deletion app/Core/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function __construct()

Facade::setFacadeApplication($this);

Events::discover_listeners();
Events::discoverListeners();
}
/**
* Check if application has been bootstrapped
Expand Down
14 changes: 9 additions & 5 deletions app/Core/Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public static function dispatch_filter(
* @return void
* @throws BindingResolutionException
*/
public static function discover_listeners(): void
public static function discoverListeners(): void
{
static $discovered;
$discovered ??= false;
Expand All @@ -170,14 +170,19 @@ public static function discover_listeners(): void
$customModules = collect(glob(APP_ROOT . '/custom/Domain' . '/*', GLOB_ONLYDIR));
$domainModules = collect(glob(APP_ROOT . "/app/Domain" . '/*', GLOB_ONLYDIR));

$testers = $customModules->map(fn ($path) => str_replace('/custom/', '/app/', $path));
$testers = $customModules->map(
fn ($path) => str_replace('/custom/', '/app/', $path)
);

$filteredModules = $domainModules->filter(fn ($path) => ! $testers->contains($path));
$filteredModules = $domainModules->filter(
fn ($path) => ! $testers->contains($path)
);

return $customModules->concat($filteredModules)->all();
});

foreach ($modules as $module) {
//File exists is not expensive and builds it's own cache to speed up performance
if (file_exists($moduleEventsPath = "$module/register.php")) {
include_once $moduleEventsPath;
}
Expand Down Expand Up @@ -205,7 +210,6 @@ public static function discover_listeners(): void
$enabledPlugins = $pluginService->getEnabledPlugins();

foreach ($enabledPlugins as $plugin) {

//Catch issue when plugins are cached on load but autoloader is not quite done loading.
//Only happens because the plugin objects are stored in session and the unserialize is not keeping up.
//Clearing session cache in that case.
Expand Down Expand Up @@ -473,7 +477,7 @@ private static function executeHandlers(
continue;
}

if($index == 0) {
if ($index == 0) {
$filteredPayload = $payload;
}
}
Expand Down
12 changes: 5 additions & 7 deletions app/Domain/Comments/Hxcontrollers/CommentList.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php


namespace Leantime\Domain\Comments\Hxcontrollers;

use Illuminate\Support\Facades\Lang;
Expand Down Expand Up @@ -49,15 +48,16 @@ public function save(): void
$module = $this->incomingRequest->input("module");
$moduleId = $this->incomingRequest->input("moduleId");
$includeStatus = $this->incomingRequest->input("includeStatus");
$editComment = filter_var($this->incomingRequest->input("editComment"), FILTER_SANITIZE_NUMBER_INT);


if ($this->commentService->addComment($_POST, $module, $moduleId)) {
$this->tpl->setNotification($this->language->__("notifications.comment_create_success"), "success");
if ($editComment > 0 && $this->commentService->editComment($_POST, $editComment)) {
$this->tpl->setNotification($this->language->__("notifications.comment_saved_success"), "success");
} elseif ($editComment == "" && $this->commentService->addComment($_POST, $module, $moduleId)) {
$this->tpl->setNotification($this->language->__("notifications.comment_create_success"), "success");
} else {
$this->tpl->setNotification($this->language->__("notifications.comment_create_error"), "error");
}


$comments = $this->commentService->getComments($module, $moduleId);

$this->tpl->assign("module", $module);
Expand All @@ -66,7 +66,6 @@ public function save(): void
$this->tpl->assign("comments", $comments);

$this->setHTMXEvent("HTMX.ShowNotification");

}

/**
Expand All @@ -89,7 +88,6 @@ public function get(): void
$this->tpl->assign("moduleId", $moduleId);
$this->tpl->assign("includeStatus", $includeStatus);
$this->tpl->assign("comments", $comments);

}

public function delete()
Expand Down
95 changes: 95 additions & 0 deletions app/Domain/Comments/Js/commentsComponent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
leantime.commentsComponent = (function () {

var enableCommenterForms = function () {

jQuery(".commentBox").show();

//Hide reply comment boxes
jQuery("#comments .replies .commentBox").hide();
jQuery(".deleteComment, .replyButton").show();

jQuery(".commentReply .tinymceSimple").tinymce().getBody().setAttribute('contenteditable', "true");
jQuery(".commentReply .tox-editor-header").show();

jQuery(".commenterFields input").prop("readonly", false);
jQuery(".commenterFields input").prop("disabled", false);

jQuery(".commenterFields textarea").prop("readonly", false);
jQuery(".commenterFields textarea").prop("disabled", false);

};

var toggleCommentBoxes = function (parentId, formHash, commentId, editComment = false, isReply = false) {

this.resetForm(parentId, formHash);

if (parseInt(parentId, 10) === 0) {
jQuery('.mainToggler-'+formHash).hide();
} else {
jQuery('.mainToggler-'+formHash).show();
}

let content = "";

if (editComment) {
content = jQuery("#commentText-"+formHash+"-"+commentId).html();

//Top level comment edit
if(parentId == commentId) {
jQuery('#commentReplyBox-'+formHash+'-' + parentId + ' > .commentImage').hide();
jQuery('#comment-'+formHash+'-' + parentId + ' > .commentMain > .replies > form').insertBefore(jQuery('#comment-'+formHash+'-' + parentId + ' .replies > div:first-child'));
} else {
jQuery('#comment-'+formHash+'-' + commentId + ' > .commentImage').hide();
}
jQuery('#comment-'+formHash+'-' + commentId + ' > .commentMain > .commentContent').hide();
jQuery('#comment-'+formHash+'-' + commentId + ' > .commentMain > .commentLinks').hide();
jQuery('#edit-comment-' + formHash + '-' + parentId +'').val(commentId);
}

jQuery('.commentBox-'+formHash+' textarea').remove();

jQuery('.commentBox-'+formHash+'').hide();

jQuery('#commentReplyBox-'+formHash+'-' + parentId + ' .commentReply').prepend('<textarea rows="5" cols="75" name="text" id="editor-'+formHash+'-' + parentId + '" class="tinymceSimple">'+ content +'</textarea>');
leantime.editorController.initSimpleEditor();

jQuery('#commentReplyBox-' + formHash + '-' + parentId + '').show();

jQuery('#father-'+formHash).val(parentId);

setTimeout(function () { // you may not need the timeout
jQuery('#commentReplyBox-' + formHash + '-' + parentId + '')[0].scrollIntoView();
tinyMCE.get('editor-'+formHash+'-' + parentId + '').focus();
}, 75);



};

var resetForm = function(id, formHash) {

jQuery('.mainToggler-'+formHash).show();
jQuery('#comments-'+formHash+' .commentImage').show();
jQuery('#comments-'+formHash+' .commentContent').show();
jQuery('#comments-'+formHash+' .commentLinks').show();

jQuery('.commentReplyBox-'+formHash+'').hide();

jQuery('.commentReplyBox-'+formHash+' textarea').each(function(){
if(jQuery(this).tinymce()) {
jQuery(this).tinymce().remove();
}
});

jQuery('.commentReplyBox-'+formHash+' textarea').remove();

}

// Make public what you want to have public, everything else is private
return {
enableCommenterForms:enableCommenterForms,
toggleCommentBoxes:toggleCommentBoxes,
resetForm:resetForm
};

})();
51 changes: 0 additions & 51 deletions app/Domain/Comments/Js/commentsController.js

This file was deleted.

10 changes: 4 additions & 6 deletions app/Domain/Comments/Services/Comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,25 +73,24 @@ public function getComments(string $module, int $moduleId, int $commentOrder = 0

//Create an array of comment parents
foreach ($comments as $comment) {

$commentObject = app()->make(Comment::class);
$commentObject->mapRootDbArray($comment);

if ($comment['commentParent'] == $parent && !isset($commentsArray[$commentObject->id])) {

$commentsArray[$commentObject->id] = $commentObject;
}
}

//Now add replies
foreach ($comments as $comment) {

$commentObject = app()->make(Comment::class);
$commentObject->mapRepliesDbArray($comment);
if($commentObject !== false){
if (
$commentObject->mapRepliesDbArray($comment) !== false
&& isset($commentsArray[$commentObject->commentParent])
) {
$commentsArray[$commentObject->commentParent]->replies[] = $commentObject;
}

}

return $commentsArray;
Expand Down Expand Up @@ -186,7 +185,6 @@ public function deleteComment($commentId): bool

return $this->commentRepository->deleteComment($commentId);
}

}

}
30 changes: 17 additions & 13 deletions app/Domain/Comments/Templates/components/input.blade.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
<div class="commentBox tw-hidden" id="comment{!! $commentId !!}">
<div class="commentImage">
<x-users::profile-image :user="$user" />
<form hx-post="{{ BASE_URL }}/comments/comment-list/save?module={{ $module }}&moduleId={{ $moduleId }}"
hx-target="#comments-{{ $module }}-{{ $moduleId }}">
<div id="commentReplyBox-{{ $formHash }}-{{ $parentId }}" class="commentBox-{{ $formHash }} commentReplyBox-{{ $formHash }} commenterFields tw-hidden tw-mb-sm" >
<div class="commentImage">
<x-users::profile-image :user="array('id'=> session('userdata.id'), 'modified' => session('userdata.modified'))" ></x-users::profile-image>
</div>
<div class="commentReply">
<x-global::forms.submit-button name="{{ __('links.save') }}" />
<x-global::forms.reset-button name="{{ __('links.cancel') }}" onclick="leantime.commentsComponent.resetForm(-1, '{{ $formHash }}')" />
</div>
<input type="hidden" name="saveComment" class="commenterField" value="1"/>
<input type="hidden" name="editComment" class="commenterField" id="edit-comment-{{ $formHash }}-{{ $parentId }}" value="" />
<input type="hidden" name="father" class="commenterField" id="father-{{ $formHash }}" value="{{ $parentId }}"/>

<div class="clearall"></div>
<br/>
</div>
<div class="commentReply">
<input
type="submit"
value="{{ __('links.reply') }}"
name="comment"
class="btn btn-default"
/>
</div>
<div class="clearall"></div>
</div>
</form>
5 changes: 2 additions & 3 deletions app/Domain/Comments/Templates/components/reply.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,14 @@
$tpl->escape($comment['firstname']),
$tpl->escape($comment['lastname'])
) !!}</span>

<div class="text">
{!! $tpl->escapeMinimal($comment['text']) !!}
</div>

</div>
<div class="commentLinks">
@if ($login::userIsAtLeast(\Leantime\Domain\Auth\Models\Roles::$commenter))
<a href="javascript:void(0);"
onclick="leantime.commentsController.toggleCommentBoxes({{ $comment['commentParent'] }})">
onclick="leantime.commentsComponent.toggleCommentBoxes({{ $comment['commentParent'] }})">
<span class="fa fa-reply"></span> {{ __('links.reply') }}
</a>
@if($comment['userId'] == session("userdata.id"));
Expand All @@ -36,6 +34,7 @@ class="deleteComment">
@endif
@endif
</div>
<div class="commentReply"></div>
</div>
<div class="clearall"></div>
</div>
Loading

0 comments on commit ccfdb4f

Please sign in to comment.