Skip to content

Commit 705c8a2

Browse files
committed
Retire jQuery
1 parent 0808e6a commit 705c8a2

File tree

1 file changed

+57
-53
lines changed

1 file changed

+57
-53
lines changed

www/index.html.tmpl

Lines changed: 57 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
</div>
5050
</div>
5151

52-
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
5352
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
5453
<script>
5554
(function() {
@@ -62,37 +61,43 @@
6261
};
6362

6463
function updateText() {
64+
const textField = document.getElementById("text");
65+
6566
// By default, assume the variables are filled and
6667
// text selection of the template is allowed
67-
$('#text').unbind('mousedown', preventSelection);
68-
$('#text').prop('readonly', false);
68+
textField.removeEventListener("mousedown", preventSelection);
69+
textField.readonly = false;
6970

7071
var content = text,
7172
vars = _.map(_.uniq(content.match(/{.+?}/g)), function(str) {
7273
return str.substring(1, str.length - 1);
7374
});
7475

75-
if (!$('#variables').children().length) {
76+
if (!document.getElementById('variables').children.length) {
7677
_.each(vars, function(v) {
77-
$('<div>')
78-
.addClass('form-group')
79-
.append(
80-
$('<label>')
81-
.text(v)
82-
)
83-
.append(
84-
$('<input type="text">')
85-
.addClass('form-control error')
86-
.val(values[v] || '')
87-
.keyup(function() {
88-
values[v] = $(this).val();
89-
// If the field is empty, show it as errored
90-
$(this).toggleClass('error', values[v] === "");
91-
refreshButton();
92-
updateText();
93-
})
94-
)
95-
.appendTo('#variables');
78+
const formGroupDiv = document.createElement('div');
79+
formGroupDiv.classList.add('form-group');
80+
81+
const label = document.createElement('label');
82+
label.textContent = v;
83+
84+
const input = document.createElement('input');
85+
input.type = 'text';
86+
input.classList.add('form-control', 'error');
87+
input.value = values[v] || '';
88+
89+
input.addEventListener('keyup', function () {
90+
values[v] = this.value;
91+
// Toggle the 'error' class based on the value
92+
this.classList.toggle('error', values[v] === '');
93+
refreshButton();
94+
updateText();
95+
});
96+
97+
formGroupDiv.appendChild(label);
98+
formGroupDiv.appendChild(input);
99+
100+
document.getElementById('variables').appendChild(formGroupDiv);
96101
});
97102
}
98103

@@ -104,16 +109,16 @@
104109
// out to make it clear that not all the variables have
105110
// been filled
106111
if (!(v in values) || values[v] === "") {
107-
$('#text').bind('mousedown', preventSelection);
108-
$('#text').prop('readonly', true);
112+
textField.addEventListener("mousedown", preventSelection);
113+
textField.readonly = true;
109114
buttonNotReady();
110115
}
111116

112117
// fuck JavaScript
113118
content = content.split(search).join(replace);
114119
});
115120

116-
$('#text').text(content);
121+
textField.textContent = content;
117122
}
118123

119124
function updatePage() {
@@ -123,41 +128,40 @@
123128
return;
124129
}
125130

126-
$('#title').text(page);
127-
$('#nav > li')
128-
.removeClass('active')
129-
.each(function() {
130-
el = $(this);
131-
if (el.data('template') === page) {
132-
$('#variables').empty();
133-
el.addClass('active');
131+
const navItems = document.querySelectorAll('#nav > li');
132+
navItems.forEach(item => item.classList.remove('active'));
133+
navItems.forEach(item => {
134+
if (item.dataset.template === page) {
135+
document.getElementById('variables').innerHTML = '';
136+
item.classList.add('active');
134137

135-
$.get('/templates/' + encodeURI(page), function(newText) {
138+
fetch('/templates/' + encodeURIComponent(page))
139+
.then(response => response.text())
140+
.then(newText => {
136141
text = newText;
137142
refreshButton();
138143
updateText();
139144
});
140-
}
141-
});
142-
145+
}
146+
});
143147
}
144148

145-
$(document).ready(function() {
146-
var templates = {templates};
147-
148-
_.each(templates, function(template) {
149-
$('<li>')
150-
.attr('role', 'presentation')
151-
.data('template', template)
152-
.append(
153-
$('<a>')
154-
.text(template)
155-
.attr('href', '#' + encodeURI(template))
156-
)
157-
.appendTo('#nav');
158-
});
149+
document.addEventListener('DOMContentLoaded', () => {
150+
const templates = {templates};
159151

160-
$(window).on('hashchange', updatePage);
152+
templates.forEach(template => {
153+
const li = document.createElement('li');
154+
li.setAttribute('role', 'presentation');
155+
li.dataset.template = template;
156+
157+
const a = document.createElement('a');
158+
a.textContent = template;
159+
a.href = '#' + encodeURIComponent(template);
160+
161+
li.appendChild(a);
162+
document.getElementById('nav').appendChild(li);
163+
});
164+
window.addEventListener('hashchange', updatePage);
161165
refreshButton();
162166
updateText();
163167
updatePage();

0 commit comments

Comments
 (0)