Commit e73fef68 authored by Doris Tam's avatar Doris Tam 🌷 Committed by Robert Lyon
Browse files

Bug 1940798: Scroll to 'showcomment' param in URL

If the 'showcomment' parameter exists in the URL,
scroll to where comment with that ID sits on the page.

This includes page comments and artefact comments.
Block comments have not been considered.
Change-Id: Iac3fe5beab59afb11e465fba13797320acff9dc2
parent fc14017b
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -2126,6 +2126,9 @@ class ActivityTypeArtefactCommentFeedback extends ActivityTypePlugin {
        );

        $this->url .= '&showcomment=' . $comment->get('id');
        if ($onartefact) {
            $this->url .= '&modal=1&artefact='. $artefactinstance->get('id');
        }

        // Email
        $author = $comment->get('author');
+8 −0
Original line number Diff line number Diff line
@@ -253,6 +253,14 @@ function open_modal(e) {
        params['blockid'] = $(e.target).closest('a').data('blockid');
    }

    // When getting comments artefacts inside a block, configure the showcomment option
    if (window.location.search.match(/showcomment=([^&]*)/)) {
        const showcommentid = window.location.search.match(/showcomment=([^&]*)/)[1];
        if (showcommentid) {
            params['showcomment'] = showcommentid;
        }
    }

    sendjsonrequest(config['wwwroot'] + 'view/viewblocks.json.php',  params, 'POST', function(data) {
        block.find('.modal-title').text(data.title);
        $('.blockinstance-content').html(data.html);
+59 −0
Original line number Diff line number Diff line
@@ -557,9 +557,20 @@ $smarty = smarty(
    )
);

$commentonartefact = param_integer('artefact', null);
// doublecheck it's a comment on  artefact in case is old email
if ($showcomment) {
    $tmpcomment = new ArtefactTypeComment($showcomment);
    if ($tmpcomment->get('onartefact') && !$commentonartefact) {
        redirect(get_config('wwwroot') . 'view/view.php?id=' . $viewid . '&showcomment=' . $showcomment . '&modal=1&artefact=' . $tmpcomment->get('onartefact'));
    }
}

$javascript = <<<EOF
var viewid = {$viewid};
var showmore = {$showmore};
var commentonartefact = '{$commentonartefact}';
var showcommentid = '{$showcomment}';

jQuery(function () {
    paginator = {$feedback->pagination_js}
@@ -615,8 +626,50 @@ jQuery(window).on('blocksloaded', {}, function() {
    $('.feedbacktable.modal-docked form').each(function() {
        initTinyMCE($(this).prop('id'));
    });

    // Focus on a page comment
    if (showcommentid && !commentonartefact) {
        focusOnShowComment();
    }
});

/**
 * Focus on the comment matching the id provided by the showcomment param value
 *
 * Note: First wait for blocks to load
 */
function focusOnShowComment() {
    setTimeout(function() {
        const commentIdString = 'comment' + showcommentid;

        // Identify the comment by focusing on the author link
        $(".comment-container button.collapsed").trigger('click');

        const author_link = $("#" + commentIdString + " a")[1];
        author_link.focus();
        scrollToComment(commentIdString);
    }, 500);
}

/**
 * Scroll down to the commentIdString
 *
 * Note: First wait for the dropdown to open
 */
function scrollToComment(commentIdString) {
    setTimeout(function() {
        const element = document.getElementById(commentIdString);
        const headerOffset = $('header').height();
        const sitemessagesOffset = $('.site-messages').height();

        // Scroll down for page comments
        if (!commentonartefact) {
            const y = element.getBoundingClientRect().top + window.pageYOffset - headerOffset - sitemessagesOffset;
            window.scrollTo({top: y, behavior: 'smooth'});
        }
    }, 500);
}

function activateModalLinks() {
    $('.commentlink').off('click');
    $('.commentlink').on('click', function(e) {
@@ -676,6 +729,12 @@ EOF;
                $('#configureblock').addClass('active').removeClass('closed');
            });
            $('a#tmp_modal_link').click();

            // Focus on an artefact comment
            const commentonartefact = {$artefact};
            if (showcommentid && commentonartefact) {
                focusOnShowComment();
            }
        });
EOF;
    }
+2 −0
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ safe_require('artefact', 'comment');
$viewid = param_integer('viewid');
$blockid = param_variable('blockid', null);
$artefactid = param_integer('artefactid', null);
$showcomment = param_integer('showcomment', null);

if (!can_view_view($viewid)) {
    json_reply('local', get_string('accessdenied', 'error'));
@@ -88,6 +89,7 @@ else {
        $commentoptions = ArtefactTypeComment::get_comment_options();
        $commentoptions->view = $view;
        $commentoptions->artefact = $artefact;
        $commentoptions->showcomment = $showcomment;
        if ($blockid) {
            $commentoptions->blockid = $blockid;
        }