-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
84 lines (69 loc) · 2.43 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const CONFIGURATION_PROPS = {
PLATFORM: 'platform',
HEAD: 'head',
RAW_HTML_BEFORE: 'raw-html-before',
RAW_HTML_AFTER: 'raw-html-after',
TASK: 'task',
LINK: 'link',
CODE: 'code'
};
const SELECTORS = {
HEAD: 'head',
TASK: 'task',
SOLUTION: 'solution-link',
CODE: 'code'
};
const KOTLIN_PLAYGROUND_ATTRIBUTES = {
PLATFORM: 'data-target-platform',
THEME: 'theme'
};
const DEFAULT_THEME = 'idea';
const DEFAULT_LINK_TEXT = '[Solution]';
function initiateApp(configuration) {
if (configuration[CONFIGURATION_PROPS.HEAD]) buildHeader(configuration[CONFIGURATION_PROPS.HEAD]);
if (configuration[CONFIGURATION_PROPS.TASK]) buildTask(configuration[CONFIGURATION_PROPS.TASK]);
if (configuration[CONFIGURATION_PROPS.RAW_HTML_BEFORE]) appendRawHtml(configuration[CONFIGURATION_PROPS.RAW_HTML_BEFORE]);
const platform = configuration[CONFIGURATION_PROPS.PLATFORM];
const code = configuration[CONFIGURATION_PROPS.CODE];
if (platform && code) buildPlayground(code, platform);
if (configuration[CONFIGURATION_PROPS.LINK]) addLink(configuration[CONFIGURATION_PROPS.LINK]);
if (configuration[CONFIGURATION_PROPS.RAW_HTML_AFTER]) appendRawHtml(configuration[CONFIGURATION_PROPS.RAW_HTML_AFTER]);
KotlinPlayground(SELECTORS.CODE);
}
function buildHeader(name) {
return buildNode(name, SELECTORS.HEAD)
}
function addLink(link) {
const node = document.createElement('a');
node.href = link;
node.className = SELECTORS.SOLUTION;
node.target = '_blank';
node.textContent = DEFAULT_LINK_TEXT;
document.body.appendChild(node)
}
function buildTask(text) {
return buildNode(text, SELECTORS.TASK)
}
function appendRawHtml(stringHtml) {
const node = document.createElement('div');
node.innerHTML = stringHtml.trim();
document.body.appendChild(node)
}
function buildPlayground(code, platform) {
const node = document.createElement(SELECTORS.CODE);
node.setAttribute(KOTLIN_PLAYGROUND_ATTRIBUTES.THEME, DEFAULT_THEME);
node.setAttribute(KOTLIN_PLAYGROUND_ATTRIBUTES.PLATFORM, platform);
node.textContent = code;
document.body.appendChild(node);
}
function buildNode(text, selector) {
const node = document.createElement('div');
node.className = selector;
node.textContent = text;
document.body.appendChild(node);
}
courseraApi.callMethod({
type: "GET_SESSION_CONFIGURATION",
onSuccess: initiateApp,
onError: console.log
});