|
36 | 36 | </div> |
37 | 37 | </div> |
38 | 38 |
|
39 | | - <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script> |
40 | 39 | <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script> |
41 | 40 | <script> |
42 | 41 | (function() { |
|
49 | 48 | }; |
50 | 49 |
|
51 | 50 | function updateText() { |
| 51 | + const textField = document.getElementById("text"); |
| 52 | + |
52 | 53 | // By default, assume the variables are filled and |
53 | 54 | // 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; |
56 | 57 |
|
57 | 58 | var content = text, |
58 | 59 | vars = _.map(_.uniq(content.match(/{.+?}/g)), function(str) { |
59 | 60 | return str.substring(1, str.length - 1); |
60 | 61 | }); |
61 | 62 |
|
62 | | - if (!$('#variables').children().length) { |
| 63 | + if (!document.getElementById('variables').children.length) { |
63 | 64 | _.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); |
82 | 87 | }); |
83 | 88 | } |
84 | 89 |
|
|
90 | 95 | // out to make it clear that not all the variables have |
91 | 96 | // been filled |
92 | 97 | 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; |
95 | 100 | } |
96 | 101 |
|
97 | 102 | // fuck JavaScript |
98 | 103 | content = content.split(search).join(replace); |
99 | 104 | }); |
100 | 105 |
|
101 | | - $('#text').text(content); |
| 106 | + textField.textContent = content; |
102 | 107 | } |
103 | 108 |
|
104 | 109 | function updatePage() { |
|
108 | 113 | return; |
109 | 114 | } |
110 | 115 |
|
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'); |
119 | 122 |
|
120 | | - $.get('/templates/' + encodeURI(page), function(newText) { |
| 123 | + fetch('/templates/' + encodeURIComponent(page)) |
| 124 | + .then(response => response.text()) |
| 125 | + .then(newText => { |
121 | 126 | text = newText; |
122 | 127 | updateText(); |
123 | 128 | }); |
124 | | - } |
125 | | - }); |
126 | | - |
| 129 | + } |
| 130 | + }); |
127 | 131 | } |
128 | 132 |
|
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}; |
143 | 135 |
|
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); |
145 | 149 | updateText(); |
146 | 150 | updatePage(); |
147 | 151 | }); |
|
0 commit comments