-
Notifications
You must be signed in to change notification settings - Fork 4
/
example.js
91 lines (77 loc) · 2.22 KB
/
example.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
85
86
87
88
89
90
91
var SeeThreepio = require('./'),
crel = require('crel'),
swig = require('swig');
var languages;
var seeThreepio = new SeeThreepio();
var termsBox = crel('textarea'),
input = crel('textarea'),
output = crel('div'),
languageSelect = crel('select');
function updateOutput(){
var result;
try{
result = swig.render(input.value, {locals:{
'__': function(){
var args = Array.prototype.slice.call(arguments);
return seeThreepio.get(args.shift(), args);
}}
});
}catch(e){
console.error(e);
input.classList.add('invalid');
}
if(result){
input.classList.remove('invalid');
output.innerHTML = result;
}
}
function updateTerms(){
var terms;
try{
terms = JSON.parse(termsBox.value);
}catch(e){
console.error(e);
termsBox.classList.add('invalid');
}
if(terms){
termsBox.classList.remove('invalid');
seeThreepio.replaceTerms(terms);
}
}
function updateLanguageSelect(){
languages = JSON.parse(document.querySelector('[type="text/json"]').textContent.trim());
for(var key in languages){
crel(languageSelect,
crel('option', {value: key}, key)
);
}
languageSelect.addEventListener('change', function(){
termsBox.value = JSON.stringify(languages[languageSelect.value], null, ' ');
updateTerms();
updateOutput();
});
termsBox.value = JSON.stringify(languages['en'], null, ' ');
}
termsBox.addEventListener('change', updateTerms);
termsBox.addEventListener('change', updateOutput);
input.addEventListener('change', updateOutput);
window.addEventListener('load', function(){
var template = document.getElementsByTagName('template')[0];
input.value = template.innerHTML;
template.parentElement.removeChild(template);
updateLanguageSelect();
updateTerms();
updateOutput();
crel(document.body,
crel('div', {'class':'inputArea'},
crel('h2', 'Terms'),
languageSelect,
termsBox
),
crel('div', {'class':'inputArea'},
crel('h2', 'Input'),
input
),
output
);
});