nieuws: apply tag changes to article links in edit mode
[minimedit.git] / nieuws / edit.js
index 6f3f54fa7856e7dc3a93072bd4c8208da0f45621..947de7b6a3b22001a3a365d9a484a577d2b17e92 100644 (file)
@@ -1,14 +1,48 @@
-var editlink = document.createElement('a');
-editlink.className = 'nav';
-editlink.appendChild(document.createTextNode('Nieuw artikel'));
-editlink.onclick = function () {
-       var today = new Date().toJSON().slice(0, 10).split('-');
-       var input = prompt('Paginalink (beknopte titel)', '');
-       if (!input) return false;
-       var url = today[0] + '/' + today[1] + '-' + today[2] + '-' +
-               input.toLowerCase().replace(/[^a-z0-9]+/g, '-').trim();
-       var title = encodeURIComponent(input.trim());
-       window.location = window.location.pathname+'/'+url+'?edit='+title+'#edit';
-       return false;
-};
-document.getElementById('news').appendChild(editlink);
+document.addEventListener('DOMContentLoaded', () => {
+       var overview = document.getElementById('news');
+       if (overview) {
+               var editlink = document.createElement('a');
+               editlink.className = 'nav';
+               editlink.appendChild(document.createTextNode('Nieuw artikel'));
+               editlink.onclick = function () {
+                       var today = new Date().toJSON().slice(0, 10).split('-');
+                       var input = prompt('Paginalink (beknopte titel)', '');
+                       if (!input) return false;
+                       var url = today[0] + '/' + today[1] + '-' + today[2] + '-' +
+                               input.toLowerCase().replace(/[^a-z0-9]+/g, '-').trim();
+                       var title = encodeURIComponent(input.trim());
+                       window.location = window.location.pathname+'/'+url+'?edit='+title+'#edit';
+                       return false;
+               };
+               overview.appendChild(editlink);
+       }
+
+       var editlink = document.querySelector('a[href="#edit"]');
+       if (!editlink) return;
+       editlink.addEventListener('click', () => {
+               document.querySelectorAll('.tags input').forEach(tagoption => {
+                       tagoption.removeAttribute('disabled');
+                       tagoption.addEventListener('change', () => {
+                               let editpost = '/edit/nieuws/tag' + window.location.pathname;
+                               let params = new URLSearchParams;
+                               params.append('tag', tagoption.value);
+                               params.append('value', tagoption.checked ? 1 : 0);
+                               fetch(editpost, {
+                                       method: 'POST',
+                                       body: params,
+                                       credentials: 'same-origin',
+                               })
+                               .then(res => {
+                                       if (res.status != 200) {
+                                               return res.text().then(body => {
+                                                       throw `foutcode ${res.status}: ${body}`;
+                                               });
+                                       }
+                               })
+                               .catch(error => {
+                                       alert(`Tag aanpassen mislukt: ${error}`);
+                               });
+                       });
+               });
+       });
+});