nieuws: append create link to navigation block
[minimedit.git] / nieuws / edit.js
1 document.addEventListener('DOMContentLoaded', () => {
2         var nav = document.querySelector('#news .nav');
3         if (nav) {
4                 var editlink = document.createElement('a');
5                 editlink.appendChild(document.createTextNode('Nieuw artikel'));
6                 editlink.onclick = function () {
7                         var today = new Date().toJSON().slice(0, 10).split('-');
8                         var input = prompt('Paginalink (beknopte titel)', '');
9                         if (!input) return false;
10                         var url = today[0] + '/' + today[1] + '-' + today[2] + '-' +
11                                 input.toLowerCase().replace(/[^a-z0-9]+/g, '-').trim();
12                         var title = encodeURIComponent(input.trim());
13                         window.location = window.location.pathname+'/'+url+'?edit='+title+'#edit';
14                         return false;
15                 };
16                 nav.appendChild(document.createTextNode(' '));
17                 nav.appendChild(editlink);
18         }
19
20         var editlink = document.querySelector('a[href="#edit"]');
21         if (!editlink) return;
22         editlink.addEventListener('click', () => {
23                 document.querySelectorAll('.tags input').forEach(tagoption => {
24                         tagoption.removeAttribute('disabled');
25                         tagoption.addEventListener('change', () => {
26                                 let editpost = '/edit/nieuws/tag' + window.location.pathname;
27                                 let params = new URLSearchParams;
28                                 params.append('tag', tagoption.value);
29                                 params.append('value', tagoption.checked ? 1 : 0);
30                                 fetch(editpost, {
31                                         method: 'POST',
32                                         body: params,
33                                         credentials: 'same-origin',
34                                 })
35                                 .then(res => {
36                                         if (res.status != 200) {
37                                                 return res.text().then(body => {
38                                                         throw `foutcode ${res.status}: ${body}`;
39                                                 });
40                                         }
41                                 })
42                                 .catch(error => {
43                                         alert(`Tag aanpassen mislukt: ${error}`);
44                                 });
45                         });
46                 });
47         });
48 });