Skip to content

Commit 0ddc94a

Browse files
committed
Retire jQuery
1 parent 611fd95 commit 0ddc94a

File tree

1 file changed

+56
-52
lines changed

1 file changed

+56
-52
lines changed

www/index.html.tmpl

Lines changed: 56 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
</div>
3737
</div>
3838

39-
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
4039
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
4140
<script>
4241
(function() {
@@ -49,36 +48,42 @@
4948
};
5049

5150
function updateText() {
51+
const textField = document.getElementById("text");
52+
5253
// By default, assume the variables are filled and
5354
// text selection of the template is allowed
54-
$('#text').unbind('mousedown', preventSelection);
55-
$('#text').prop('readonly', false);
55+
textField.removeEventListener("mousedown", preventSelection);
56+
textField.readonly = false;
5657

5758
var content = text,
5859
vars = _.map(_.uniq(content.match(/{.+?}/g)), function(str) {
5960
return str.substring(1, str.length - 1);
6061
});
6162

62-
if (!$('#variables').children().length) {
63+
if (!document.getElementById('variables').children.length) {
6364
_.each(vars, function(v) {
64-
$('<div>')
65-
.addClass('form-group')
66-
.append(
67-
$('<label>')
68-
.text(v)
69-
)
70-
.append(
71-
$('<input type="text">')
72-
.addClass('form-control error')
73-
.val(values[v] || '')
74-
.keyup(function() {
75-
values[v] = $(this).val();
76-
// If the field is empty, show it as errored
77-
$(this).toggleClass('error', values[v] === "");
78-
updateText();
79-
})
80-
)
81-
.appendTo('#variables');
65+
const formGroupDiv = document.createElement('div');
66+
formGroupDiv.classList.add('form-group');
67+
68+
const label = document.createElement('label');
69+
label.textContent = v;
70+
71+
const input = document.createElement('input');
72+
input.type = 'text';
73+
input.classList.add('form-control', 'error');
74+
input.value = values[v] || '';
75+
76+
input.addEventListener('keyup', function () {
77+
values[v] = this.value;
78+
// Toggle the 'error' class based on the value
79+
this.classList.toggle('error', values[v] === '');
80+
updateText();
81+
});
82+
83+
formGroupDiv.appendChild(label);
84+
formGroupDiv.appendChild(input);
85+
86+
document.getElementById('variables').appendChild(formGroupDiv);
8287
});
8388
}
8489

@@ -90,15 +95,15 @@
9095
// out to make it clear that not all the variables have
9196
// been filled
9297
if (!(v in values) || values[v] === "") {
93-
$('#text').bind('mousedown', preventSelection);
94-
$('#text').prop('readonly', true);
98+
textField.addEventListener("mousedown", preventSelection);
99+
textField.readonly = true;
95100
}
96101

97102
// fuck JavaScript
98103
content = content.split(search).join(replace);
99104
});
100105

101-
$('#text').text(content);
106+
textField.textContent = content;
102107
}
103108

104109
function updatePage() {
@@ -108,40 +113,39 @@
108113
return;
109114
}
110115

111-
$('#title').text(page);
112-
$('#nav > li')
113-
.removeClass('active')
114-
.each(function() {
115-
el = $(this);
116-
if (el.data('template') === page) {
117-
$('#variables').empty();
118-
el.addClass('active');
116+
const navItems = document.querySelectorAll('#nav > li');
117+
navItems.forEach(item => item.classList.remove('active'));
118+
navItems.forEach(item => {
119+
if (item.dataset.template === page) {
120+
document.getElementById('variables').innerHTML = '';
121+
item.classList.add('active');
119122

120-
$.get('/templates/' + encodeURI(page), function(newText) {
123+
fetch('/templates/' + encodeURIComponent(page))
124+
.then(response => response.text())
125+
.then(newText => {
121126
text = newText;
122127
updateText();
123128
});
124-
}
125-
});
126-
129+
}
130+
});
127131
}
128132

129-
$(document).ready(function() {
130-
var templates = {templates};
131-
132-
_.each(templates, function(template) {
133-
$('<li>')
134-
.attr('role', 'presentation')
135-
.data('template', template)
136-
.append(
137-
$('<a>')
138-
.text(template)
139-
.attr('href', '#' + encodeURI(template))
140-
)
141-
.appendTo('#nav');
142-
});
133+
document.addEventListener('DOMContentLoaded', () => {
134+
const templates = {templates};
143135

144-
$(window).on('hashchange', updatePage);
136+
templates.forEach(template => {
137+
const li = document.createElement('li');
138+
li.setAttribute('role', 'presentation');
139+
li.dataset.template = template;
140+
141+
const a = document.createElement('a');
142+
a.textContent = template;
143+
a.href = '#' + encodeURIComponent(template);
144+
145+
li.appendChild(a);
146+
document.getElementById('nav').appendChild(li);
147+
});
148+
window.addEventListener('hashchange', updatePage);
145149
updateText();
146150
updatePage();
147151
});

0 commit comments

Comments
 (0)