|
49 | 49 | </div> |
50 | 50 | </div> |
51 | 51 |
|
52 | | - <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script> |
53 | 52 | <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script> |
54 | 53 | <script> |
55 | 54 | (function() { |
|
62 | 61 | }; |
63 | 62 |
|
64 | 63 | function updateText() { |
| 64 | + const textField = document.getElementById("text"); |
| 65 | + |
65 | 66 | // By default, assume the variables are filled and |
66 | 67 | // 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; |
69 | 70 |
|
70 | 71 | var content = text, |
71 | 72 | vars = _.map(_.uniq(content.match(/{.+?}/g)), function(str) { |
72 | 73 | return str.substring(1, str.length - 1); |
73 | 74 | }); |
74 | 75 |
|
75 | | - if (!$('#variables').children().length) { |
| 76 | + if (!document.getElementById('variables').children.length) { |
76 | 77 | _.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); |
96 | 101 | }); |
97 | 102 | } |
98 | 103 |
|
|
104 | 109 | // out to make it clear that not all the variables have |
105 | 110 | // been filled |
106 | 111 | 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; |
109 | 114 | buttonNotReady(); |
110 | 115 | } |
111 | 116 |
|
112 | 117 | // fuck JavaScript |
113 | 118 | content = content.split(search).join(replace); |
114 | 119 | }); |
115 | 120 |
|
116 | | - $('#text').text(content); |
| 121 | + textField.textContent = content; |
117 | 122 | } |
118 | 123 |
|
119 | 124 | function updatePage() { |
|
123 | 128 | return; |
124 | 129 | } |
125 | 130 |
|
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'); |
134 | 137 |
|
135 | | - $.get('/templates/' + encodeURI(page), function(newText) { |
| 138 | + fetch('/templates/' + encodeURIComponent(page)) |
| 139 | + .then(response => response.text()) |
| 140 | + .then(newText => { |
136 | 141 | text = newText; |
137 | 142 | refreshButton(); |
138 | 143 | updateText(); |
139 | 144 | }); |
140 | | - } |
141 | | - }); |
142 | | - |
| 145 | + } |
| 146 | + }); |
143 | 147 | } |
144 | 148 |
|
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}; |
159 | 151 |
|
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); |
161 | 165 | refreshButton(); |
162 | 166 | updateText(); |
163 | 167 | updatePage(); |
|
0 commit comments