discourse/app/assets/javascripts/embed-application.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

68 lines
1.8 KiB
JavaScript
Raw Normal View History

// discourse-skip-module
(function () {
const referer = document.getElementById("data-embedded").dataset.referer;
function postUp(msg) {
if (parent) {
parent.postMessage(msg, referer);
}
}
function clickPostLink(e) {
2021-01-27 06:39:20 -05:00
let postId = e.target.getAttribute("data-link-to-post");
if (postId) {
2021-01-27 06:39:20 -05:00
let postElement = document.getElementById("post-" + postId);
if (postElement) {
2021-01-27 06:39:20 -05:00
let rect = postElement.getBoundingClientRect();
if (rect && rect.top) {
postUp({ type: "discourse-scroll", top: rect.top });
e.preventDefault();
return false;
}
}
}
}
window.onload = function () {
// get state info from data attribute
2021-01-27 06:39:20 -05:00
let embedState = document.querySelector("[data-embed-state]");
let state = "unknown";
let embedId = null;
if (embedState) {
state = embedState.getAttribute("data-embed-state");
embedId = embedState.getAttribute("data-embed-id");
}
// Send a post message with our loaded height and state
postUp({
type: "discourse-resize",
height: document["body"].offsetHeight,
state,
embedId,
});
2021-01-27 06:39:20 -05:00
let postLinks = document.querySelectorAll("a[data-link-to-post]"),
i;
for (i = 0; i < postLinks.length; i++) {
postLinks[i].onclick = clickPostLink;
}
// Make sure all links in the iframe point to _blank
2021-01-27 06:39:20 -05:00
let cookedLinks = document.querySelectorAll(".cooked a");
for (i = 0; i < cookedLinks.length; i++) {
cookedLinks[i].target = "_blank";
}
// Adjust all names
2021-01-27 06:39:20 -05:00
let names = document.querySelectorAll(".username a");
for (i = 0; i < names.length; i++) {
2021-01-27 06:39:20 -05:00
let username = names[i].innerHTML;
if (username) {
/* global BreakString */
names[i].innerHTML = new BreakString(username).break();
}
}
};
})();