1 document.addEventListener('DOMContentLoaded', () => {
2 document.querySelectorAll('#search').forEach(p => {
3 let [input, button] = p.children;
4 button.onclick = e => {
5 if (input.value && input.offsetWidth > 50) {
6 return true; // bubble to submit
15 document.querySelectorAll('.multiinput > input[id]').forEach(el => {
17 if (e.target.value == '') return;
18 // insert another empty input element option
19 let add = e.target.cloneNode(true);
21 add.oninput = e.target.oninput;
22 e.target.parentNode.appendChild(add);
23 e.target.oninput = undefined;
24 e.target.removeAttribute('id');
28 let wpinput = document.getElementById('wptitle');
30 let wpbutton = wpinput.parentNode.appendChild(document.createElement('button'));
31 wpbutton.type = 'button';
32 wpbutton.append('Download');
33 wpbutton.onclick = () => {
34 let wptitle = wpinput.value || document.getElementById('form').value;
35 let wplang = document.getElementById('lang').value;
36 if (wplang == 'la') wplang = 'en'; // most likely presence of scientific names
37 let wpapi = `https://${wplang}.wikipedia.org/w/api.php`;
38 let wppage = wpapi+'?action=parse&format=json&origin=*&prop=text|langlinks&page='+wptitle;
39 fetch(wppage).then(res => res.json()).then(json => {
40 if (json.error) throw `error returned: ${json.error.info}`;
41 wpinput.value = json.parse.title;
43 let wptext = json.parse.text['*'];
44 let transrow = document.getElementById('trans-la');
45 if (transrow && !transrow.value && wptext) {
46 const binom = wptext.match(/ class="binomial">.*?<i>(.*?)<\/i>/);
48 transrow.value = binom[1]
52 // translations from language links
53 let wplangs = json.parse.langlinks;
54 if (wplangs) wplangs.forEach(wptrans => {
55 let transrow = document.getElementById('trans-' + wptrans.lang);
56 if (!transrow || transrow.value) return;
57 transrow.value = wptrans['*'].replace(/([^,(]*).*/, (link, short) => {
58 return short.toLocaleLowerCase(wptrans.lang).trimEnd() + ' [' + link + ']';
62 // copy first paragraph to story
63 let storyinput = document.getElementById('story');
64 if (storyinput && !storyinput.value && wptext) {
65 storyinput.value = wptext
66 .replace(/<h2.*/s, '') // prefix
67 .replace(/<table.*?<\/table>/sg, '') // ignore infobox
68 .match(/<p>(.*?)<\/p>/s)[0] // first paragraph
69 .replace(/<[^>]*>/g, '') // strip html tags
72 // list images in article html
73 let imginput = document.getElementById('source');
74 if (!imginput || imginput.value) return;
75 let wpimages = wptext.match(/<img\s[^>]+>/g);
76 let wpselect = wpinput.parentNode.appendChild(document.createElement('ul'));
77 wpselect.className = 'popup';
78 wpimages.forEach(img => {
79 let selectitem = wpselect.appendChild(document.createElement('li'));
80 selectitem.insertAdjacentHTML('beforeend', img);
81 selectitem.onclick = e => {
82 let imgsrc = e.target.src
83 .replace(/^(?=\/\/)/, 'https:')
84 .replace(/\/thumb(\/.+)\/[^\/]+$/, '$1');
85 imginput.value = imgsrc;
90 }).catch(error => alert(error));
93 wpbutton = wpinput.parentNode.appendChild(document.createElement('button'));
94 wpbutton.type = 'button';
95 wpbutton.append('Visit');
96 wpbutton.onclick = () => {
97 let wptitle = wpinput.value || document.getElementById('form').value;
98 let wplang = document.getElementById('lang').value;
100 wplang == 'la' ? `https://species.wikimedia.org/wiki/${wptitle}` :
101 `https://${wplang}.wikipedia.org/wiki/${wptitle}`;
102 window.open(wpurl, 'sheet-wikipedia').focus();
107 let imgpreview = document.getElementById('sourcepreview');
109 let imginput = document.getElementById('source');
110 imginput.parentNode.parentNode.append(imgpreview); // separate row
111 let previewbutton = imginput.parentNode.appendChild(document.createElement('button'));
112 previewbutton.type = 'button';
113 previewbutton.append('View');
114 previewbutton.onclick = () => {
115 previewbutton.childNodes[0].nodeValue = imgpreview.hidden ? 'Hide' : 'View';
116 imgpreview.hidden = !imgpreview.hidden;
120 let thumbpreview = document.getElementById('convertpreview');
121 if (thumbpreview && imgpreview) {
122 thumbpreview.onclick = e => {
123 thumbpreview.onclick = null; // setup once
124 const cropinput = document.getElementById('crop32');
125 const imgselect = thumbpreview.children[0];
126 const canvas = [thumbpreview.clientWidth, thumbpreview.clientHeight];
127 const border = [canvas[0], canvas[0] * imgpreview.height / imgpreview.width];
128 const minscale = Math.max(1, canvas[1] / border[1]); // 100% or fit width
129 let crop = cropinput.value.split(/[^0-9.]/).map(pos => pos / 1000);
130 let scale = 1 / (crop[2] - crop[0]) || minscale;
131 crop.push(0); // defined y dimension
132 crop.splice(2); // end coordinates applied to zoom
133 crop = crop.map((rel, axis) => rel * border[axis % 2] * scale);
136 function applydrag(e) {
137 const touch = e.touches ? e.touches[0] : e;
138 let pos = [touch.pageX, touch.pageY];
139 if (e.type === 'touchmove' && e.touches.length > 1) {
140 // distance to second point
141 pos[0] -= e.touches[1].pageX;
142 pos[1] -= e.touches[1].pageY;
143 const span = Math.sqrt(pos[0]**2 + pos[1]**2);
145 cropzoom(.01 * (span - pinch));
151 // apply drag delta to crop position
152 crop[0] += drag[0] - pos[0];
153 crop[1] += drag[1] - pos[1];
160 [0, 1].forEach(axis => {
161 if (crop[axis] > border[axis] * scale - canvas[axis]) {
162 crop[axis] = border[axis] * scale - canvas[axis]; // max bound
164 if (crop[axis] < 0) {
165 crop[axis] = 0; // min bound
168 imgselect.style.left = -crop[0]+'px';
169 imgselect.style.top = -crop[1]+'px';
170 imgselect.style.width = (scale * 100)+'%';
174 (crop[0] + canvas[0]) / border[0],
175 (crop[1] + canvas[1]) / border[1],
176 ].map(pos => Math.round(1000 * pos / scale));
179 function cropzoom(delta) {
180 if (scale + delta < minscale) {
181 delta = minscale - scale; // scale = 1
183 [0, 1].forEach(axis => {
184 // same area center at altered scale
185 crop[axis] += (crop[axis] + border[axis] / 2) / scale * delta;
191 imgselect.src = imgpreview.src;
192 imgselect.style.cursor = 'grab';
193 imgselect.style.position = 'absolute';
196 imgselect.ontouchstart =
197 imgselect.onmousedown = e => {
199 drag = pinch = false;
201 imgselect.style.cursor = 'grabbing';
203 window.onmousemove = e => {
208 window.onmouseup = e => {
210 imgselect.style.cursor = 'grab';
211 window.ontouchmove = window.ontouchend =
212 window.onmousemove = window.onmouseup = null;
216 imgselect.onwheel = e => {
218 let delta = (-e.deltaY || e.wheelDelta) * .001 * scale;
219 if (e.deltaMode == 1) { // DOM_DELTA_LINE
220 delta *= 18; // convert number of lines to pixels
227 let translist = document.getElementById('trans');
229 let langoptions = Array.prototype.filter.call(document.getElementById('lang').options, opt => {
230 if (document.getElementById('trans-' + opt.value)) return;
231 if (document.getElementById('lang').value == opt.value) return;
234 if (!langoptions.length) return;
236 let transadd = translist.appendChild(document.createElement('li'));
237 let transselect = transadd.appendChild(document.createElement('select'));
238 transselect.appendChild(document.createElement('option'));
239 for (let langoption of langoptions) {
240 let transoption = document.createElement('option');
241 transoption.value = langoption.value;
242 transoption.append(langoption.label);
243 transselect.appendChild(transoption);
245 transselect.onchange = e => {
246 let inputlang = e.target.selectedOptions[0];
247 let transadded = translist.insertBefore(document.createElement('li'), transadd);
248 let translabel = transadded.appendChild(document.createElement('label'));
249 translabel.append(inputlang.label.replace(/ (.+)/, ' ')); //TODO title = $1
250 let transinput = transadded.appendChild(document.createElement('input'));
251 transinput.name = 'trans-'+inputlang.value;
252 translabel.setAttribute('for', transinput.id = transinput.name);
254 if (e.target.length <= 1) e.target.remove();