6fc79ebcc19c8af97bd9e56164ff00154f0f49ee
[sheet.git] / writer.js
1 document.addEventListener('DOMContentLoaded', () => {
2         document.querySelectorAll('.multiinput > input[id]').forEach(el => {
3                 el.oninput = e => {
4                         if (e.target.value == '') return;
5                         // insert another empty input element option
6                         let add = e.target.cloneNode(true);
7                         add.value = '';
8                         add.oninput = e.target.oninput;
9                         e.target.parentNode.appendChild(add);
10                         e.target.oninput = undefined;
11                         e.target.removeAttribute('id');
12                 };
13         });
14
15         let wpinput = document.getElementById('wptitle');
16         if (wpinput) {
17                 let wpbutton = wpinput.parentNode.appendChild(document.createElement('button'));
18                 wpbutton.type = 'button';
19                 wpbutton.append('Download');
20                 wpbutton.onclick = () => {
21                         let wptitle = wpinput.value || document.getElementById('form').value;
22                         let wplang = document.getElementById('lang').value;
23                         if (wplang == 'la') wplang = 'en'; // most likely presence of scientific names
24                         let wpapi = `https://${wplang}.wikipedia.org/w/api.php`;
25                         let wppage = wpapi+'?action=parse&format=json&origin=*&prop=text|langlinks&page='+wptitle;
26                         fetch(wppage).then(res => res.json()).then(json => {
27                                 if (json.error) throw `error returned: ${json.error.info}`;
28                                 wpinput.value = json.parse.title;
29
30                                 // translations from language links
31                                 let wplangs = json.parse.langlinks;
32                                 if (wplangs) wplangs.forEach(wptrans => {
33                                         let transrow = document.getElementById('trans-' + wptrans.lang);
34                                         if (!transrow || transrow.value) return;
35                                         transrow.value = wptrans['*'].replace(/([^,(]*).*/, (link, short) => {
36                                                 return short.toLocaleLowerCase(wptrans.lang).trimEnd() + ' [' + link + ']';
37                                         });
38                                 });
39
40                                 // copy first paragraph to story
41                                 let wptext = json.parse.text['*'];
42                                 let storyinput = document.getElementById('story');
43                                 if (storyinput && !storyinput.value && wptext) {
44                                         storyinput.value = wptext
45                                                 .replace(/<h2.*/s, '') // prefix
46                                                 .replace(/<table.*?<\/table>/sg, '') // ignore infobox
47                                                 .match(/<p>(.*?)<\/p>/s)[0] // first paragraph
48                                                 .replace(/<[^>]*>/g, '') // strip html tags
49                                 }
50
51                                 // list images in article html
52                                 let imginput = document.getElementById('source');
53                                 if (!imginput || imginput.value) return;
54                                 let wpimages = wptext.match(/<img\s[^>]+>/g);
55                                 let wpselect = wpinput.parentNode.appendChild(document.createElement('ul'));
56                                 wpselect.className = 'popup';
57                                 wpimages.forEach(img => {
58                                         let selectitem = wpselect.appendChild(document.createElement('li'));
59                                         selectitem.insertAdjacentHTML('beforeend', img);
60                                         selectitem.onclick = e => {
61                                                 let imgsrc = e.target.src
62                                                         .replace(/^(?=\/\/)/, 'https:')
63                                                         .replace(/\/thumb(\/.+)\/[^\/]+$/, '$1');
64                                                 imginput.value = imgsrc;
65                                                 wpselect.remove();
66                                                 return false;
67                                         };
68                                 });
69                         }).catch(error => alert(error));
70                         return false;
71                 };
72                 wpbutton = wpinput.parentNode.appendChild(document.createElement('button'));
73                 wpbutton.type = 'button';
74                 wpbutton.append('Visit');
75                 wpbutton.onclick = () => {
76                         let wptitle = wpinput.value || document.getElementById('form').value;
77                         let wplang = document.getElementById('lang').value;
78                         let wpurl =
79                                 wplang == 'la' ? `https://species.wikimedia.org/wiki/${wptitle}` :
80                                 `https://${wplang}.wikipedia.org/wiki/${wptitle}`;
81                         window.open(wpurl, 'sheet-wikipedia').focus();
82                         return false;
83                 };
84         }
85
86         let imgpreview = document.getElementById('sourcepreview');
87         if (imgpreview) {
88                 let imginput = document.getElementById('source');
89                 imginput.parentNode.parentNode.append(imgpreview); // separate row
90                 let previewbutton = imginput.parentNode.appendChild(document.createElement('button'));
91                 previewbutton.type = 'button';
92                 previewbutton.append('View');
93                 previewbutton.onclick = () => {
94                         previewbutton.childNodes[0].nodeValue = imgpreview.hidden ? 'Hide' : 'View';
95                         imgpreview.hidden = !imgpreview.hidden;
96                 };
97         }
98
99         let thumbpreview = document.getElementById('thumbpreview');
100         if (thumbpreview && imgpreview) {
101                 thumbpreview.onclick = e => {
102                         let imgselect = imgpreview; /* TODO clone */
103                         imgselect.hidden = false;
104                         imgselect.classList.add('popup');
105                         imgselect.onmousemove = e => {
106                                 let border = imgselect.getBoundingClientRect();
107                                 let pos = [
108                                         Math.round(1000 * (e.clientX - border.x) / border.width),
109                                         Math.round(1000 * (e.clientY - border.y) / border.height)
110                                 ];
111                                 return pos;
112                         };
113                         imgselect.onclick = e => {
114                                 let imgoption = document.getElementById('thumb');
115                                 imgoption.value += (imgoption.value && '-') + imgselect.onmousemove(e);
116                                 imgselect.hidden = true;
117                                 imgselect.classList.remove('popup');
118                         };
119                 };
120         }
121
122         let translist = document.getElementById('trans');
123         if (translist) {
124                 let langoptions = Array.prototype.filter.call(document.getElementById('lang').options, opt => {
125                         if (document.getElementById('trans-' + opt.value)) return;
126                         if (document.getElementById('lang').value == opt.value) return;
127                         return true;
128                 });
129                 if (!langoptions.length) return;
130
131                 let transadd = translist.appendChild(document.createElement('li'));
132                 let transselect = transadd.appendChild(document.createElement('select'));
133                 transselect.appendChild(document.createElement('option'));
134                 for (let langoption of langoptions) {
135                         let transoption = document.createElement('option');
136                         transoption.value = langoption.value;
137                         transoption.append(langoption.label);
138                         transselect.appendChild(transoption);
139                 }
140                 transselect.onchange = e => {
141                         let inputlang = e.target.selectedOptions[0];
142                         let transadded = translist.insertBefore(document.createElement('li'), transadd);
143                         let translabel = transadded.appendChild(document.createElement('label'));
144                         translabel.append(inputlang.label.replace(/ (.+)/, ' ')); //TODO title = $1
145                         let transinput = transadded.appendChild(document.createElement('input'));
146                         transinput.name = 'trans-'+inputlang.value;
147                         translabel.setAttribute('for', transinput.id = transinput.name);
148                         inputlang.remove();
149                         if (e.target.length <= 1) e.target.remove();
150                         transinput.focus();
151                 };
152         }
153 });