-
Notifications
You must be signed in to change notification settings - Fork 0
/
sges.js
138 lines (114 loc) · 3.22 KB
/
sges.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
function getProjectNameAndLang(el) {
var name = el.querySelector('#name').textContent;
var parts = name.split(" ");
var appName = parts[0];
var lang = parts[1];
return {
name: name,
app: appName,
lang: lang,
el: el
}
}
function setClassNames(template) {
template.el.classList.add(`lang_${template.lang}`);
template.el.classList.add(`type_${template.app}`);
}
function getWidgetFilters(templates) {
var apps = [];
var languages = [];
templates.forEach(template => {
if (apps.indexOf(template.app) === -1) {
apps.push(template.app);
}
if (languages.indexOf(template.lang) === -1) {
languages.push(template.lang);
}
});
return {
apps,
languages
}
}
var filterRenderer = (filter) => {
return `
<div>
<label>
<input value="${filter}" class="filter__value" type="checkbox" />
<span>${filter}</span>
</label>
</div>
`;
};
function renderWidget(filters) {
var appNameFilters = filters.apps.map(filterRenderer).join('');
var langFilters = filters.languages.map(filterRenderer).join('');
var widget = document.createElement('div');
widget.id = 'sges';
widget.innerHTML = `
<div>
<div id="appname__filter">
<strong>App name</strong>
${appNameFilters}
</div>
<div id="language__filter">
<strong>Language</strong>
${langFilters}
</div>
</div>
`;
document.body.appendChild(widget);
}
function getActiveFilters(elements) {
return elements
.filter(filter => filter.checked)
.map(filter => filter.value)
}
function toArray(nodeList) {
return [].slice.call(nodeList);
}
function init() {
var attachFilterChangeListener = (filter) => {
filter.addEventListener('change', onFilterChange);
};
var onFilterChange = (e) => {
setTimeout(() => {
var appnameFilters = getActiveFilters(toArray(document.querySelectorAll("#appname__filter .filter__value")));
var languageFilters = getActiveFilters(toArray(document.querySelectorAll("#language__filter .filter__value")));
if (appnameFilters.length === 0 && languageFilters.length === 0) {
return templates.forEach(template => {
template.el.dataset.visible = true;
});
}
templates.forEach(template => {
if (appnameFilters.indexOf(template.app) >= 0) {
if (languageFilters.length === 0 || languageFilters.indexOf(template.lang) >= 0) {
template.el.dataset.visible = true;
} else {
template.el.dataset.visible = false;
}
} else {
template.el.dataset.visible = false;
}
});
}, 0);
};
var templateElements = toArray(document.querySelectorAll('.template-version-list'));
var templates = templateElements.map(getProjectNameAndLang);
templates.forEach(setClassNames)
renderWidget(getWidgetFilters(templates));
var filterElements = toArray(document.querySelectorAll(".filter__value"));
filterElements.forEach(attachFilterChangeListener);
}
function retry() {
if (document.querySelectorAll('.template-version-list').length > 0) {
console.clear();
init();
} else {
setTimeout(function() {
console.log('retry');
retry();
}, 250);
}
}
retry();