replyToComment()

in static/src/javascripts/projects/common/modules/discussion/comments.js [383:470]


    replyToComment(e) {
        e.preventDefault();

        const replyLink = (e.currentTarget);
        const replyToId = replyLink.getAttribute('data-comment-id');

        if (!replyToId) {
            return;
        }

        const replyTo = document.getElementById(`reply-to-${replyToId}`);

        // There is already a comment box for this on the page
        if (replyTo) {
            replyTo.focus();
            return;
        }

        $('.d-comment-box--response').remove();

        const replyToComment = qwery(`#comment-${replyToId}`)[0];
        const replyToAuthor = replyToComment.getAttribute(
            'data-comment-author'
        );
        const replyToAuthorId = replyToComment.getAttribute(
            'data-comment-author-id'
        );
        const $replyToComment = bonzo(replyToComment);
        const replyToBody = qwery(
            this.getClass('commentBody'),
            replyToComment
        )[0].innerHTML;
        const replyToTimestamp = qwery(
            this.getClass('commentTimestampJs'),
            replyToComment
        )[0].innerHTML;
        const commentBox = new CommentBox({
            discussionId: this.options && this.options.discussionId,
            premod:
                this.user &&
                this.user.privateFields &&
                this.user.privateFields.isPremoderated,
            state: 'response',
            replyTo: {
                commentId: replyToId,
                author: replyToAuthor,
                authorId: replyToAuthorId,
                body: replyToBody,
                timestamp: replyToTimestamp,
            },
            focus: true,
        });

        // this is a bit toffee, but we don't have .parents() in bonzo
        const parentCommentEl = $replyToComment.hasClass(
            this.getClass('topLevelComment', true)
        )
            ? $replyToComment[0]
            : $replyToComment.parent().parent()[0];

        const showRepliesElem = qwery(
            this.getClass('showReplies'),
            parentCommentEl
        );

        if (
            showRepliesElem.length > 0 &&
            !bonzo(showRepliesElem).hasClass('u-h')
        ) {
            showRepliesElem[0].click();
        }

        commentBox.render(parentCommentEl);

        commentBox.on('post:success', (comment) => {
            let responses = qwery('.d-thread--responses', parentCommentEl)[0];

            if (!responses) {
                responses = bonzo.create(
                    '<ul class="d-thread d-thread--responses"></ul>'
                )[0];
                bonzo(parentCommentEl).append(responses);
            }

            commentBox.destroy();
            this.addComment(comment, responses);
        });
    }