Skip to content

Commit 891ea12

Browse files
authored
Merge pull request #641 from topcoder-platform/mfe-forums-app-6
Mfe forums app #6
2 parents dbf42a2 + 01a4d41 commit 891ea12

File tree

1 file changed

+396
-0
lines changed

1 file changed

+396
-0
lines changed

vanilla/js/embed.js

Lines changed: 396 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,396 @@
1+
/* eslint-disable */
2+
//export default () => {
3+
if (window.vanilla == undefined) window.vanilla = {};
4+
window.vanilla_lazy_load = undefined
5+
6+
if (window.vanilla.embeds == undefined) window.vanilla.embeds = {};
7+
8+
window.vanilla.embed = function (host) {
9+
var scripts = document.getElementsByTagName("script"),
10+
id = Math.floor(Math.random() * 100000).toString(),
11+
embedUrl = window.location.href.split("#")[0],
12+
jsPath = "/js/embed.js",
13+
currentPath = window.location.hash.substr(1),
14+
disablePath = window != top;
15+
16+
var optStr = function (name, defaultValue, definedValue) {
17+
if (window["vanilla_" + name]) {
18+
if (definedValue == undefined) return window["vanilla_" + name];
19+
else return definedValue.replace("%s", window["vanilla_" + name]);
20+
}
21+
return defaultValue;
22+
};
23+
24+
if (!currentPath || disablePath) currentPath = "/";
25+
26+
if (currentPath.substr(0, 1) != "/") currentPath = "/" + currentPath;
27+
28+
if (window.gadgets) embedUrl = "";
29+
30+
if (typeof host == "undefined") {
31+
host = "";
32+
window.host_base_url = "";
33+
for (var i = 0; i < scripts.length; i++) {
34+
if (scripts[i].src.indexOf(jsPath) > 0) {
35+
host = scripts[i].src;
36+
host = host.replace("http://", "").replace("https://", "");
37+
host = host.substr(0, host.indexOf(jsPath));
38+
39+
host_base_url = scripts[i].src;
40+
host_base_url = host_base_url.substr(
41+
0,
42+
host_base_url.indexOf(jsPath)
43+
);
44+
if (host_base_url.substring(host_base_url.length - 1) != "/")
45+
host_base_url += "/";
46+
}
47+
}
48+
}
49+
50+
window.vanilla.embeds[id] = this;
51+
if (window.postMessage) {
52+
var onMessage = function (e) {
53+
console.log("---- message ----", e);
54+
55+
// Check that we're getting a vanilla message
56+
if (typeof e.data === "string") {
57+
var message = e.data.split(":");
58+
var frame = document.getElementById("vanilla" + id);
59+
60+
// Unload event's source is undefined
61+
var isUnload = message[0] == "unload";
62+
if (!frame || (!isUnload && frame.contentWindow != e.source)) {
63+
return;
64+
}
65+
processMessage(message);
66+
}
67+
};
68+
if (window.addEventListener) {
69+
window.addEventListener("message", onMessage, false);
70+
} else {
71+
window.attachEvent("onmessage", onMessage);
72+
}
73+
} else {
74+
var messageId = null;
75+
setInterval(function () {
76+
try {
77+
var vid = "vanilla" + id;
78+
var hash = window.frames[vid].frames["messageFrame"].location.hash;
79+
hash = hash.substr(6);
80+
} catch (e) {
81+
return;
82+
}
83+
84+
var message = hash.split(":");
85+
var newMessageId = message[0];
86+
if (newMessageId == messageId) {
87+
return;
88+
}
89+
90+
messageId = newMessageId;
91+
message.splice(0, 1);
92+
processMessage(message);
93+
}, 200);
94+
}
95+
96+
const checkHash = function () {
97+
debugger;
98+
var path = window.location.hash.substr(1) || "/";
99+
if (path != currentPath) {
100+
currentPath = path;
101+
window.frames["vanilla" + id].location.replace(vanillaUrl(path));
102+
}
103+
};
104+
105+
if (!window.gadgets) {
106+
if (!disablePath) {
107+
if ("onhashchange" in window) {
108+
if (window.addEventListener)
109+
window.addEventListener("hashchange", checkHash, false);
110+
else window.attachEvent("onhashchange", checkHash);
111+
} else {
112+
setInterval(checkHash, 300);
113+
}
114+
}
115+
}
116+
117+
// Strip param out of str if it exists
118+
const stripParam = function (str, param) {
119+
var pIndex = str.indexOf(param);
120+
if (pIndex > -1) {
121+
var pStr = str.substr(pIndex);
122+
var tIndex = pStr.indexOf("&");
123+
var trail = tIndex > -1 ? pStr.substr(tIndex + 1) : "";
124+
var pre = currentPath.substr(pIndex - 1, 1);
125+
if (pre == "&" || pre == "?") pIndex--;
126+
127+
return str.substr(0, pIndex) + (trail.length > 0 ? pre : "") + trail;
128+
}
129+
return str;
130+
};
131+
132+
const processMessage = function (message) {
133+
var iframe = document.getElementById("vanilla" + id);
134+
135+
if (message[0] == "height") {
136+
setHeight(message[1]);
137+
138+
if (message[1] > 0) {
139+
iframe.style.visibility = "visible";
140+
}
141+
} else if (message[0] == "location") {
142+
if (message[1] != "") {
143+
iframe.style.visibility = "visible";
144+
}
145+
146+
if (disablePath) {
147+
//currentPath = cmd[1];
148+
} else {
149+
currentPath = window.location.hash.substr(1);
150+
if (currentPath != message[1]) {
151+
currentPath = message[1];
152+
// Strip off the values that this script added
153+
currentPath = stripParam(currentPath, "remote="); // 1
154+
currentPath = stripParam(currentPath, "locale="); // 2
155+
//window.location.hash = currentPath;
156+
}
157+
}
158+
} else if (message[0] == "unload") {
159+
// Scroll to the top of the IFRAME if the top position is not in the view.
160+
var currentScrollAmount =
161+
window.pageYOffset || document.documentElement.scrollTop;
162+
var offsetTop = offsetFromTop("vanilla" + id);
163+
if (offsetTop - currentScrollAmount < 0) {
164+
window.scrollTo(0, offsetTop);
165+
}
166+
167+
iframe.style.visibility = "hidden";
168+
} else if (message[0] == "scrolltop") {
169+
window.scrollTo(0, document.getElementById("vanilla" + id).offsetTop);
170+
} else if (message[0] == "scrollto") {
171+
window.scrollTo(
172+
0,
173+
document.getElementById("vanilla" + id).offsetTop -
174+
40 +
175+
message[1] * 1
176+
);
177+
} else if (message[0] == "unembed") {
178+
document.location = "http://" + host + window.location.hash.substr(1);
179+
}
180+
};
181+
182+
const offsetFromTop = function (id) {
183+
var node = document.getElementById(id),
184+
top = 0,
185+
topScroll = 0;
186+
if (node.offsetParent) {
187+
do {
188+
top += node.offsetTop;
189+
topScroll += node.offsetParent ? node.offsetParent.scrollTop : 0;
190+
} while ((node = node.offsetParent));
191+
return top - topScroll;
192+
}
193+
return -1;
194+
};
195+
196+
const setHeight = function (height) {
197+
if (optStr("height")) return;
198+
// if (document.getElementById("vanilla" + id).style["height"] === ( height - 21 ) + 'px') {
199+
// return
200+
// }
201+
202+
// document.getElementById("vanilla" + id).style["height"] = height + "px";
203+
document.getElementById("vanilla" + id).style["height"] = 'calc(100vh - 76px)'
204+
if (window.gadgets && gadgets.window && gadgets.window.adjustHeight) {
205+
try {
206+
gadgets.window.adjustHeight();
207+
} catch (ex) {
208+
// Do nothing...
209+
}
210+
}
211+
};
212+
213+
const vanillaUrl = function (path) {
214+
// What type of embed are we performing?
215+
var embed_type =
216+
typeof vanilla_embed_type == "undefined"
217+
? "standard"
218+
: vanilla_embed_type;
219+
// Are we loading a particular discussion based on discussion_id?
220+
var discussion_id =
221+
typeof vanilla_discussion_id == "undefined" ? 0 : vanilla_discussion_id;
222+
// Are we loading a particular discussion based on foreign_id?
223+
var foreign_id =
224+
typeof vanilla_identifier == "undefined" ? "" : vanilla_identifier;
225+
// Is there a foreign type defined? Possibly used to render the discussion
226+
// body a certain way in the forum? Also used to filter down to foreign
227+
// types so that matching foreign_id's across type don't clash.
228+
var foreign_type =
229+
typeof vanilla_type == "undefined" ? "page" : vanilla_type;
230+
// If embedding comments, should the newly created discussion be placed in a specific category?
231+
var category_id =
232+
typeof vanilla_category_id == "undefined" ? "" : vanilla_category_id;
233+
// If embedding comments, this value will be used to reference the foreign content. Defaults to the url of the page this file is included in.
234+
var foreign_url =
235+
typeof vanilla_url == "undefined"
236+
? document.URL.split("#")[0]
237+
: vanilla_url;
238+
// Are we forcing a locale via Multilingual plugin?
239+
var embed_locale =
240+
typeof vanilla_embed_locale == "undefined" ? "" : vanilla_embed_locale;
241+
if (typeof vanilla_lazy_load == "undefined") vanilla_lazy_load = true;
242+
243+
// If path was defined, and we're sitting at app root, use the defined path instead.
244+
if (typeof vanilla_path != "undefined" && path == "/")
245+
path = vanilla_path;
246+
247+
// Force type based on incoming variables
248+
if (discussion_id != "" || foreign_id != "") embed_type = "comments";
249+
250+
var result = "";
251+
252+
if (embed_type == "comments") {
253+
result =
254+
"//" +
255+
host +
256+
"/discussion/embed/" +
257+
// Break the cache. /embed/ gets cached, looks like you are not logged in.
258+
"&c=" +
259+
new Date().getTime() +
260+
"&vanilla_identifier=" +
261+
encodeURIComponent(foreign_id) +
262+
"&vanilla_url=" +
263+
encodeURIComponent(foreign_url);
264+
265+
if (typeof vanilla_type != "undefined")
266+
result += "&vanilla_type=" + encodeURIComponent(vanilla_type);
267+
268+
if (typeof vanilla_discussion_id != "undefined")
269+
result +=
270+
"&vanilla_discussion_id=" +
271+
encodeURIComponent(vanilla_discussion_id);
272+
273+
if (typeof vanilla_category_id != "undefined")
274+
result +=
275+
"&vanilla_category_id=" + encodeURIComponent(vanilla_category_id);
276+
277+
if (typeof vanilla_title != "undefined")
278+
result += "&title=" + encodeURIComponent(vanilla_title);
279+
} else {
280+
result =
281+
"//" +
282+
host +
283+
path +
284+
"&remote=" +
285+
encodeURIComponent(embedUrl) +
286+
"&locale=" +
287+
encodeURIComponent(embed_locale);
288+
}
289+
290+
if (window.vanilla_sso) {
291+
result += "&sso=" + encodeURIComponent(vanilla_sso);
292+
}
293+
294+
return result.replace(/\?/g, "&").replace("&", "?"); // Replace the first occurrence of amp with question.
295+
};
296+
var vanillaIframe = document.createElement("iframe");
297+
vanillaIframe.id = "vanilla" + id;
298+
vanillaIframe.name = "vanilla" + id;
299+
vanillaUrl(currentPath);
300+
//vanillaIframe.src = "https://vanilla.topcoder-dev.com/?remote=https://local.topcoder-dev.com/forums&locale=";
301+
vanillaIframe.scrolling = "yes";
302+
vanillaIframe.frameBorder = "0";
303+
vanillaIframe.allowTransparency = true;
304+
vanillaIframe.border = "0";
305+
vanillaIframe.width = "100%";
306+
vanillaIframe.style.width = "100%";
307+
vanillaIframe.style.border = "0";
308+
vanillaIframe.style.display = "block"; // must be block
309+
310+
if (window.postMessage) {
311+
vanillaIframe.height = "0";
312+
vanillaIframe.style.height = "0";
313+
} else {
314+
vanillaIframe.height = "300";
315+
vanillaIframe.style.height = "300px";
316+
}
317+
318+
var img = document.createElement("div");
319+
img.className = "vn-loading";
320+
img.style.textAlign = "center";
321+
img.innerHTML = window.vanilla_loadinghtml
322+
? vanilla_loadinghtml
323+
: '<img src="https://images.v-cdn.net/progress.gif" />';
324+
325+
var container = document.getElementById("vanilla-comments");
326+
// Couldn't find the container, so dump it out and try again.
327+
if (!container) document.write('<div id="vanilla-comments"></div>');
328+
container = document.getElementById("vanilla-comments");
329+
330+
if (container) {
331+
var loaded = function () {
332+
if (img) {
333+
container.removeChild(img);
334+
img = null;
335+
}
336+
vanillaIframe.style.visibility = "visible";
337+
};
338+
339+
if (vanillaIframe.addEventListener) {
340+
vanillaIframe.addEventListener("load", loaded, true);
341+
} else if (vanillaIframe.attachEvent) {
342+
vanillaIframe.attachEvent("onload", loaded);
343+
} else setTimeout(2000, loaded);
344+
345+
container.appendChild(img);
346+
347+
// If jQuery is present in the page, include our defer-until-visible script
348+
if (vanilla_lazy_load && typeof jQuery != "undefined") {
349+
jQuery.ajax({
350+
url: host_base_url + "js/library/jquery.appear.js",
351+
dataType: "script",
352+
cache: true,
353+
success: function () {
354+
// setTimeout(function() {
355+
356+
if (jQuery.fn.appear)
357+
jQuery("#vanilla-comments").appear(function () {
358+
container.appendChild(vanillaIframe);
359+
});
360+
else container.appendChild(vanillaIframe); // fallback
361+
// }, 10000);
362+
},
363+
});
364+
} else {
365+
container.appendChild(vanillaIframe); // fallback: just load it
366+
}
367+
}
368+
369+
// Include our embed css into the page
370+
var vanilla_embed_css = document.createElement("link");
371+
vanilla_embed_css.rel = "stylesheet";
372+
vanilla_embed_css.type = "text/css";
373+
vanilla_embed_css.href =
374+
host_base_url +
375+
(host_base_url.substring(host_base_url.length - 1) == "/" ? "" : "/") +
376+
"applications/dashboard/design/embed.css";
377+
(
378+
document.getElementsByTagName("head")[0] ||
379+
document.getElementsByTagName("body")[0]
380+
).appendChild(vanilla_embed_css);
381+
382+
return this;
383+
};
384+
try {
385+
if (window.location.hash.substr(0, 6) != "#poll:") window.vanilla.embed();
386+
} catch (e) {
387+
var error = document.createElement("div");
388+
error.style.padding = "10px";
389+
error.style.fontSize = "12px";
390+
error.style.fontFamily = "lucida grande";
391+
error.style.background = "#ffffff";
392+
error.style.color = "#000000";
393+
error.appendChild(document.createTextNode("Failed to embed Vanilla: " + e));
394+
document.getElementById("vanilla-comments").appendChild(error);
395+
}
396+
//};

0 commit comments

Comments
 (0)