Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CDPT-2079 Focus first new result after (infinite) pagination. #744

Merged
merged 5 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 41 additions & 46 deletions public/app/themes/clarity/src/globals/js/ajax-templating.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,24 @@
* @see https://stackoverflow.com/a/39065147/6671505
*
* @example
* Template:
* <script type="text/template" data-template="results-template">
* <div class="result">
* <h2>${title}</h2>
* <p>${content}</p>
* ${?imgSrc}
* <img src="${imgSrc}" alt="${title}" />
* ${/?imgSrc}
* </div>
*
* Implementation:
* const template = new AjaxTemplating("results-template");
* const html = template.renderHtml({
* title: "Hello World",
* content: "This is a test."
* content: "This is a test.",
* imgSrc: "https://example.com/image.jpg",
* });
*
*/

export default class AjaxTemplating {
Expand All @@ -19,23 +32,16 @@ export default class AjaxTemplating {
* @param {string} templateName
*/
constructor(templateName) {
this.resultsTemplate = this.loadTemplate(templateName);
}

/**
* Load the template from the DOM.
*
* Returns an array of strings where:
* - every odd index is a template variable
* - every even is a string of html text
*
* @param {string} templateName
* @returns {string[]}
*/

loadTemplate(templateName) {
// do this without jQuery
return document
/**
* The template from the DOM.
*
* An array of strings where:
* - every odd index is a template variable
* - every even is a string of html text
*
* @type {string[]}
*/
this.resultsTemplate = document
.querySelector(`script[data-template="${templateName}"]`)
.textContent.split(/\$\{(.+?)\}/g);
}
Expand All @@ -50,39 +56,28 @@ export default class AjaxTemplating {
renderHtml(props) {
// Keep track of the conditional blocks
// If a conditional block is not met, we skip the block
let skip = false;

let parts = [];

for (let i = 0; i < this.resultsTemplate.length; i++) {
const tok = this.resultsTemplate[i];

// Handle the html text - even indexes

if (i % 2 === 0 && !skip) {
parts.push(tok);
continue;
}
let skip = 0;

if (i % 2 === 0) {
continue;
}
return this.resultsTemplate
.map((tok, i) => {
// Handle the html text - even indexes

// Handle the template variables - odd indexes
if (i % 2 === 0) {
return skip ? "" : tok;
}

if (tok.startsWith("?") && !props[tok.substring(1)]) {
skip = true;
}
// Handle the template variables - odd indexes

if (tok.startsWith("/?") && !props[tok.substring(2)]) {
skip = false;
}
if (tok.startsWith("?") && !props[tok.substring(1)]) {
skip++;
}

if (!skip) {
parts.push(props[tok]);
}
}
if (tok.startsWith("/?") && !props[tok.substring(2)]) {
skip--;
}

return parts.join("");
return skip ? "" : props[tok];
})
.join("");
}
}
18 changes: 14 additions & 4 deletions public/app/themes/clarity/src/globals/js/ajax-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@

export const renderResults = ({
results: { posts, templateName },
aggregates: { currentPage, totalResults },
aggregates: { currentPage, resultsPerPage, totalResults },
}) => {
// Remove all articles if page is 1.
if (currentPage === 1) {
Expand All @@ -88,24 +88,34 @@
const resultsHtml = posts.map((props) => t.renderHtml(props));

// Append the html to the content section.
$("#content").append(resultsHtml);

Check warning

Code scanning / CodeQL

DOM text reinterpreted as HTML Medium

DOM text
is reinterpreted as HTML without escaping meta-characters.

// Update the title.
$("#title-section").text(`${totalResults} search results`);

// If we are on a page greater than 1, focus on the first new result.
if (currentPage > 1) {
const index = (currentPage - 1) * resultsPerPage;
$("#content").children().eq(index).find("div.content a").focus();
}
};

/**
* Render pagination to the page.
*
*
* @param {Object} props
* @param {number} props.currentPage
* @param {number} props.resultsPerPage
* @param {number} props.totalResults
*
*
* @returns {void}
*/

export const renderPagination = ({ currentPage, resultsPerPage, totalResults }) => {
export const renderPagination = ({
currentPage,
resultsPerPage,
totalResults,
}) => {
if (resultsPerPage === undefined || resultsPerPage === -1) {
return;
}
Expand Down Expand Up @@ -135,7 +145,7 @@
totalPages: !totalResults ? 1 : Math.ceil(totalResults / resultsPerPage),
});

$(".c-pagination").html(paginationHtml);

Check warning

Code scanning / CodeQL

DOM text reinterpreted as HTML Medium

DOM text
is reinterpreted as HTML without escaping meta-characters.

// Update the page number on the pagination element.
if (!isLastPage) {
Expand Down
Loading