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

CLDR-16048 New forum post window sometimes disappears #2821

Merged
merged 1 commit into from
Mar 30, 2023
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
42 changes: 42 additions & 0 deletions tools/cldr-apps/js/src/esm/cldrCache.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* cldrCache: a simple least-recently-used cache for the Survey Tool front end
*
* Based partly on: https://stackoverflow.com/questions/996505/lru-cache-implementation-in-javascript
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense and is clear.

there's probably more places that could use caching, could use a package such as https://github.com/tusharf5/runtime-memcache (linked from the SO comment)

also https://www.npmjs.com/package/sls-cache can use the localstorage, which gives us a persistent cache between browser access. Could be good for truly readonly data such as xpath hash to xpath string.

* post by odinho - Velmont
*/
export class LRU {
constructor(max = 100) {
this._max = max;
this._cache = new Map();
}

clear() {
this._cache.clear();
}

get(key) {
const val = this._cache.get(key);
if (val) {
// delete and set again so it's most recent
this._cache.delete(key);
this._cache.set(key, val);
}
return val;
}

set(key, val) {
if (this._cache.has(key)) {
// delete before setting again so it's most recent
this._cache.delete(key);
} else if (this._cache.size == this._max) {
this._cache.delete(this._oldest());
}
this._cache.set(key, val);
}

_oldest() {
// Iteration happens in insertion order (chronologically), so the first is the oldest
// Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map
return this._cache.keys().next().value;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

didn't know that, nice!

}
}
31 changes: 18 additions & 13 deletions tools/cldr-apps/js/src/esm/cldrForum.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ import * as cldrStatus from "./cldrStatus.js";
import * as cldrSurvey from "./cldrSurvey.js";
import * as cldrText from "./cldrText.js";

/**
* Encapsulate this class name -- caution: it's used literally in surveytool.css
*/
const FORUM_DIV_CLASS = "forumDiv";

const SUMMARY_CLASS = "getForumSummary";

const FORUM_DEBUG = false;
Expand Down Expand Up @@ -66,7 +71,7 @@ let userCanPost = false;
*/
let displayUtc = false;

// called as special.load
// called as special.load; this is for the full-page Forum, not for posts shown in the Info Panel
function load() {
const curLocale = cldrStatus.getCurrentLocale();
if (!curLocale) {
Expand Down Expand Up @@ -287,7 +292,7 @@ function makePostHtml(
html += "</form>\n";

html += '<div class="post"></div>\n';
html += '<div class="forumDiv"></div>\n';
html += '<div class="' + FORUM_DIV_CLASS + '"></div>\n';
return html;
}

Expand Down Expand Up @@ -345,9 +350,11 @@ function openPostWindow(html, text, parentPost) {
postModal.find(".modal-body").html(html);
$("#post-form textarea[name=text]").val(text);
if (parentPost) {
const forumDiv = parseContent([parentPost], "parent");
const postHolder = postModal.find(".modal-body").find(".forumDiv");
postHolder[0].appendChild(forumDiv);
const div = parseContent([parentPost], "parent");
const postHolder = postModal
.find(".modal-body")
.find("." + FORUM_DIV_CLASS);
postHolder[0].appendChild(div);
}
postModal.modal();
autosize(postModal.find("textarea"));
Expand Down Expand Up @@ -378,6 +385,7 @@ function submitPost(event) {
*/
function reallySubmitPost(text) {
$("#post-form button").fadeOut();
cldrForumPanel.clearCache();
const form = getFormValues(text);
sendPostRequest(form);
}
Expand Down Expand Up @@ -871,10 +879,6 @@ function makeOneNewPostButton(
code,
value
) {
const buttonTitle = couldFlag
? "forumNewPostFlagButton"
: "forumNewPostButton";

const buttonClass = couldFlag
? "addPostButton forumNewPostFlagButton btn btn-default btn-sm"
: "addPostButton forumNewButton btn btn-default btn-sm";
Expand Down Expand Up @@ -1201,11 +1205,11 @@ function filterAndAssembleForumThreads(
threadHash,
applyFilter
);
const forumDiv = document.createDocumentFragment();
const div = document.createDocumentFragment();
let countEl = null;
if (showThreadCount) {
countEl = document.createElement("h4");
forumDiv.append(countEl);
div.append(countEl);
}
let threadCount = 0;
posts.forEach(function (post) {
Expand All @@ -1216,15 +1220,15 @@ function filterAndAssembleForumThreads(
* from filteredArray to prevent appending the same div again
* (which would move the div to the bottom, not duplicate it).
*/
forumDiv.append(topicDivs[post.threadId]);
div.append(topicDivs[post.threadId]);
filteredArray = filteredArray.filter((id) => id !== post.threadId);
}
});
if (showThreadCount) {
countEl.innerHTML =
threadCount + (threadCount === 1 ? " topic" : " topics");
}
return forumDiv;
return div;
}

/**
Expand Down Expand Up @@ -1462,6 +1466,7 @@ function parseHash(pieces) {
}

export {
FORUM_DIV_CLASS,
SUMMARY_CLASS,
addNewPostButtons,
handleIdChanged,
Expand Down
Loading