home · contact · privacy
Improve todo accounting.
[misc] / todo_templates / watch_form.html
1 <script>
2 var inputs_to_ignore = ['and_tag', 'not_tag', 'filter'];
3 var update_button_names = ['update'];
4 var sending_commits = false;
5 var changes_to_commit = false;
6 var form_to_watch = document.getElementById('form_to_watch');
7 for (let i = 0; i < form_to_watch.elements.length; i++) {
8     let el = form_to_watch.elements[i];
9     if (['INPUT', 'TEXTAREA', 'SELECT'].includes(el.tagName)) {
10         el.addEventListener('change', function(event) {
11             if (!inputs_to_ignore.includes(event.target.name)) { 
12                changes_to_commit = true; 
13             };
14         });
15     };
16 };
17 form_to_watch.addEventListener('keydown', function(event) {
18     let el = document.activeElement; 
19     if ('Enter' === event.key
20             && ['INPUT', 'SELECT'].includes(el.tagName)
21             && (el.tagName !== 'INPUT' || el.type !== 'submit')) {
22         event.preventDefault();
23         if (inputs_to_ignore.includes(el.name)) {
24             document.getElementById('filter_button').click(); 
25         } else {
26             document.getElementById('update_button').click(); 
27         };
28     };
29 });
30 form_to_watch.addEventListener('submit', function(event) {
31     if (update_button_names.includes(event.submitter.name)) {
32        sending_commits = true; 
33     };
34 });
35 window.addEventListener('beforeunload', function(event) {
36     if (changes_to_commit && !sending_commits) {
37         var warning = 'about to lose unsaved changes, continue?';
38         event.returnValue = warning;
39         return warning;
40     }
41 });
42 </script> 
43