-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
144 lines (114 loc) · 4 KB
/
main.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
$(document).ready(function(){
loadFirstSelectorData();
loadSecondSelectorData();
initHide();
function loadFirstSelectorData() {
var firstSelector = document.getElementById("first-selector");
var selectorData = explorerData['en'];
// 1. append the default option
var defaultOption = document.createElement("option");
defaultOption.text = '...'
defaultOption.value = 'default';
firstSelector.appendChild(defaultOption);
// 2 append the data options
Object.keys(selectorData).forEach(function(key) {
// 2.1 fetch the option data
var option = document.createElement("option");
option.text = selectorData[key]['option_text']
option.value = key;
// 2.2 append option to the selector
firstSelector.appendChild(option);
})
}
function loadSecondSelectorData() {
var secondSelector = document.getElementById("second-selector");
var selectorData = explorerData['en'];
Object.keys(selectorData).forEach(function(key) {
// 1 create the selector div
var div = document.createElement("div");
div.innerHTML = selectorData[key]['action_text']
div.id = key;
// 2 create the selector_id
var selector = document.createElement("select");
selector.id = selectorData[key]['selector_id']
// 3 append the default selector
var defaultOption = document.createElement("option");
defaultOption.text = '...'
defaultOption.value = 'default';
selector.appendChild(defaultOption);
// 4 append the options for selector
var optionData = selectorData[key]['data']
Object.keys(optionData).forEach(function(key) {
// 4.1 fetch the option data
var option = document.createElement("option");
option.text = optionData[key]['option_text']
option.value = key;
// 4.2 append option to the selector
selector.appendChild(option);
})
// 5 append selector to div, div to secondSelector div
div.appendChild(selector);
secondSelector.appendChild(div);
})
}
function initHide() {
hideSecondSelectorChildren();
//hideDocumentation();
}
function hideDocumentation() {
$("#documentation").hide();
}
$("#first-selector").change(function() {
resetFirstSelector(this.value);
});
function resetFirstSelector(selectedVal) {
hideSecondSelectorChildren();
//hideDocumentation();
$("#second-selector #" + selectedVal ).show();
$("#code").html(" # Create a List<br>nums = [2, 3, 4, 5]");
highlightCode();
}
function highlightCode() {
$('pre code').each(function(i, e) {hljs.highlightBlock(e);});
}
function hideSecondSelectorChildren() {
$("#second-selector").children().hide();
}
// Important: need to add event istener whenever new data for first selector is added.
// when #second-selector children change
$("#sel-add").change(function() {
updateInfo('add', this.value);
});
$("#sel-rm").change(function() {
updateInfo('rm', this.value);
});
$("#sel-find").change(function() {
updateInfo('find', this.value);
});
$("#sel-sort").change(function() {
updateInfo('sort', this.value);
});
$("#sel-other").change(function() {
updateInfo('other', this.value);
});
function updateInfo(category, itemName) {
updateCode(category, itemName);
showDocumentation();
highlightCode();
updateDocumentation(category, itemName);
}
function updateCode(category, itemName) {
$("#code").html(explorerData['en'][category]['data'][itemName]['text']);
}
function showDocumentation(){
$("#documentation").show();
}
function updateDocumentation(category, itemName) {
var selectedItem = explorerData['en'][category]['data'][itemName];
var elem = [];
elem[0] = "<h2 id='doc-title'>" + selectedItem['name'] + '</h2>';
elem[1] = '<div>' + selectedItem['desc'] + '</div>';
var text = elem.join('');
$('#documentation').html(text);
}
});