s4

view s4-main.js @ 902:d9ecb727edcd

Code cleaning
author HIROSE Yuuji <yuuji@gentei.org>
date Sun, 03 Jan 2021 09:51:46 +0900
parents 1cff36303150
children 9f237a8f550d
line source
1 // 愛
2 (function (){
3 var isOlderJS; // Set in init();
4 var hasTouchPad =
5 (navigator.maxTouchPoints && navigator.maxTouchPoints >0);
6 var myurl = document.URL,
7 mypath = myurl.substring(myurl.lastIndexOf("/"));
8 var art_m_list = [];
9 if (mypath.match(/(.*)\/(.*)/)) {
10 mypath = RegExp.$2;
11 mypath = mypath.substring(0, mypath.lastIndexOf("?"));
12 //alert("mypath="+mypath);
13 }
14 function collectElementsByAttr(elm, attr, val) {
15 var e = document.getElementsByTagName(elm);
16 if (!e) return null;
17 var list = [];
18 for (var i of e) {
19 if (i.getAttribute(attr) == val)
20 list.push(i)
21 }
22 return list;
23 }
24 function nthChildOf(parent, n, elem) { // Return Nth child of type ELEM
25 // N begins with 1
26 var i=0;
27 var le = elem.toLowerCase();
28 for (var c of parent.childNodes) {
29 if (!c.tagName) continue;
30 if (c.tagName.toLowerCase() == le) {
31 if (++i >= n) return c;
32 }
33 }
34 return null;
35 }
36 function insertRedirect(e) {
37 var articleId, textarea = document.getElementById("text");
38 var p = e.target, checked = p.checked;
39 while (p = p.parentNode)
40 if (p.nodeName.match(/^td$/i)) break;
41 if (!p) return;
42 while (p = p.nextSibling)
43 if (p.nodeName.match(/^td$/i)) break;
44 if (!p) return;
45 articleId = p.getAttribute("id");
46 if (textarea && articleId) {
47 var tv = textarea.value, lines;
48 if (tv)
49 lines = tv.split("\n");
50 else
51 lines = [""];
52 var re = new RegExp("[, ]*#"+articleId+"(?![0-9])");
53 checked = (p.nodeName.match(/^input$/)
54 ? p.checked // checkbox obeys its status
55 : !lines[0].match(re)) // a-elment toggles redirection
56 if (checked) {
57 if (!lines[0].match(re)) {
58 var re2 = new RegExp(/>#[#0-9, ]+[0-9]/);
59 if (lines[0].match(re2))
60 lines[0] = lines[0].replace(
61 re2, '$&, '+'#'+articleId);
62 else {
63 if (lines[0] > "") lines[0] = " "+lines[0];
64 lines[0] = ">#"+articleId+lines[0];
65 }
66 }
67 } else { // Remove #xxxxx
68 if (lines[0].match(/^>#[0-9 ,]+#/)) // 2 or more #id's
69 lines[0] = lines[0].replace(
70 new RegExp("^>#"+articleId+"[ ,]*"), ">").replace(
71 new RegExp("[ ,]*#"+articleId), "");
72 else {
73 lines[0] = lines[0].replace(
74 new RegExp(">#"+articleId+"[ ,]*"), "");
75 }
76 }
77 lines[0] = lines[0].replace(/^> *$/, '');
78 textarea.value = lines.join("\n");
79 }
80 }
81 function registPjaxViewers(aHrefList) {
82 // if (isOlderJS) return;
83 let apos=art_m_list.length;
84 for (let a of aHrefList) {
85 let href = a.getAttribute("href");
86 let localvar = apos;
87 let td = a.parentNode,
88 tr = td.parentNode,
89 id = td.id,
90 text = td.textContent,
91 author = tr.getElementsByTagName("a");
92 if (author) author = author[0].getAttribute("title");
93 if (href.match(/\?showattc\+article_m\+([0-9+])/)) {
94 if (td.innerHTML.match(/x読み取り不可/)) {
95 a.removeAttribute("href");
96 continue;
97 }
98 let url = RegExp.lastMatch;
99 // console.log("pjaxView(e, "+href+", "+apos+")");
100 a.addEventListener("click", function(e) {
101 // Shoud use closure local variable: localvar
102 pjaxView(e, href, localvar);
103 }, false);
104 apos++;
105 art_m_list.push({
106 url: href, id: id, author: author, text: text
107 });
108 }
109 }
110 }
111 var ajaxSubmit;
112 function replAddNews(newtable) {
113 let newids = [], idlist=[];
114 let getArticleID = function (td) {
115 return parseInt(td.parentNode.getElementsByTagName("td")[1].id);
116 }
117 for (let i of newtable.querySelectorAll("td.repl"))
118 newids.push(i);
119 newids = newids.sort((a,b)=> {
120 return (getArticleID(a) - getArticleID(b));
121 });
122 for (i of newids)
123 idlist.push(getArticleID(i));
124 console.log("IDList="+idlist.join());
125 let cnt=0, ntr;
126 let current = collectElementsByAttr("td", "class", "repl"),
127 ncur=0, n, icur=0, o, oid, nid, otr;
128 current = document.querySelectorAll('td[class="repl"]');
129 let last=current[current.length-1],
130 tbody = last.parentNode.parentNode;
131 // Now reconstruct articles with merge-sort like method
132 outer: for (; ncur<newids.length; ncur++) {
133 n = newids[ncur];
134 if (!n.id) continue;
135 nid = parseInt(n.id);
136 if (nid<=0) continue;
137 ntr = n.parentNode;
138 for (; icur<current.length; icur++) {
139 o = current[icur];
140 otr = o.parentNode;
141 oid = getArticleID(o);
142 if (!oid || oid=="") continue;
143 if (oid >= nid) {
144 ntr.getElementsByTagName("td")[0].classList.add("new");
145 tbody.insertBefore(ntr, otr);
146 if (oid==nid) otr.remove();
147 cnt++;
148 continue outer;
149 }
150 }
151 // Append absolutely new articles.
152 ntr = n.parentNode;
153 ntr.getElementsByTagName("td")[0].classList.add("new");
154 tbody.appendChild(ntr);
155 ntr.scrollIntoView({behavior: "smooth"});
156 registPjaxViewers(ntr.querySelectorAll("a[href]"));
157 ntr.classList.add("dissolving");
158 let localntr = ntr;
159 setTimeout(() => {
160 localntr.classList.remove("dissolving");
161 localntr.classList.add("emerging");
162 }, 100);
163 cnt++;
164 }
165 ajaxSubmit.textContent = ajaxSubmit.back;
166 ajaxSubmit.disabled = false;
167 console.log("Update "+cnt+"rows");
168 if (cnt>0 && ntr.scrollIntoView) {
169 let option = {behavior: "smooth"};
170 if (!isOlderJS) option.block = "center";
171 try { // Scroll to last updated row
172 ntr.scrollIntoView(option);
173 } catch (e1) {}
174 }
175 return cnt;
176 }
178 function warnFileSize(form) {
179 let szmax = form.querySelector('input[name="filesize_max"]').value;
180 if (!szmax || szmax=="") return;
181 szmax = parseInt(szmax);
182 if (szmax <= 0) return;
183 // szmax = 10000
184 let ng = "", rcval=false, fileexists=false;
185 for (let f of form.querySelectorAll('input[type="file"]')) {
186 let thiserr = false
187 for (let i of f.files) {
188 fileexists = true;
189 let fn = i.name, sz = i.size;
190 console.log("max="+szmax+", fn="+fn+", sz="+sz);
191 if (sz > szmax) {
192 thiserr = true;
193 ng += ((ng>"" ? ", " : "")+fn)
194 }
195 }
196 thiserr ? f.classList.add("warnbg") : f.classList.remove("warnbg");
197 }
198 if (ng>"") {
199 rcval = "File-size Limit Error: "+ng+"\n"+
200 "Should be less than "+szmax+"bytes.\n"+
201 szmax+"バイト未満にしてください"
202 alert(rcval);
203 }
204 if (form.text.value == "") {
205 let w;
206 if (fileexists)
207 w = "Fill the text area\n" +
208 "添付したファイルに関する説明を入れてください。";
209 else
210 w = "Enter your comment!\n何か書き込んでね!";
211 alert(w);
212 rcval = (rcval || w);
213 form.text.classList.add("warnbg");
214 setTimeout(() => {form.text.classList.remove("warnbg");}, 2000)
215 }
216 return rcval;
217 }
218 function ajaxPost(e) {
219 e.preventDefault();
220 let rowid;
221 if (!myurl.match(/replyblog\+([0-9]+)/)) return;
222 rowid = RegExp.$1
223 let myform = document.querySelector("form.replyblog");
224 let data = new FormData(myform),
225 fetchtime = data.get("fetchtime");
226 if (!fetchtime || fetchtime=="") return;
227 ///*XX*/fetchtime = "2020-06-14T00:00:00";data.set("fetchtime", fetchtime)
229 ajaxSubmit = e.target;
230 ajaxSubmit.back = ajaxSubmit.textContent;
231 if (ajaxSubmit.id == "reload") {
232 ajaxSubmit.textContent = "更新中"
233 data.set("text", "")
234 } else {
235 if (warnFileSize(myform)) return;
236 ajaxSubmit.textContent = "送信中";
237 }
238 ajaxSubmit.blur();
239 ajaxSubmit.disabled = true;
240 let act = mypath+"?blog_fetch+"+rowid+"+f:"+fetchtime;
242 function respUpdate(tbody) {
243 let div = document.createElement("div"), form, newform;
244 try {
245 div.innerHTML = tbody;
246 form = div.querySelector("form");
247 } catch (er) {
248 alert("Cannot parse fetch data");
249 return;
250 }
251 let update = replAddNews(form);
252 let dispelem = myform.querySelector("textarea").parentNode;
253 newform = new FormData(form);
254 if (data.get("text") > "") { // Called by submit button
255 myform.reset();
256 // myform.text.value = '';
257 }
258 myform.fetchtime.value = newform.get("fetchtime");
259 myform.id.value = newform.get("id");
260 if (update && update > 0) {
261 let s = update + " new article" +
262 (update>1 ? "s" : "") + " posted";
263 dispInfoMomentary(s, dispelem);
264 }
265 }
266 fetch(act, {
267 method: "POST", body: data,
268 credentials: "include" // For older firefox
269 }).then((resp) => {
270 return resp.text();
271 }).then((tbody) => {
272 respUpdate(tbody);
273 })
274 }
275 function pjaxView(ev, url, mynum) {
276 ev.preventDefault();
277 let box = document.createElement("div")
278 box.setAttribute("class", "pjaxview");
279 let p1 = document.createElement("p"),
280 bt = document.createElement("button"),
281 sl = document.createElement("button"),
282 sr = document.createElement("button"),
283 loading = document.createElement("span"),
284 info = document.createElement("p");
285 info1 = document.createElement("span");
286 info2 = document.createElement("span");
287 iframe = document.createElement("iframe");
288 var curpos = mynum;
289 var historyBase = history.length;
291 function _setPjaxCurposInfo() {
292 let len = art_m_list.length;
293 let cur = art_m_list[curpos]
294 info1.textContent = (1+curpos)+" of "+len+" article #"+cur.id+
295 (cur.author ? " by "+cur.author : "") + ":";
296 info2.textContent = cur.text.trim();
297 info2.setAttribute("class", "border textdigest");
298 }
299 function _resetPjax() {
300 // All we can do surely is to back 1 page,
301 // because we cannot move to desirable entry of history list.
302 history.back();
303 }
304 function setSwipeAct(iframe) {
305 // We cannot use DOMContentLoaded nor iframe.contentWindow here.
306 // PDF.js does not construct contentWindow...?
307 iframe.addEventListener("load", () => {
308 loading.classList.remove("loading");
309 if (!hasTouchPad) return;
310 let ifm = iframe.contentDocument;
311 let startX, moveX, thresh = 100;
312 ifm.addEventListener("touchstart", (e) => {
313 e.preventDefault();
314 startX = e.touches[0].pageX;
315 }, false);
316 ifm.addEventListener("touchmove", (e) => {
317 e.preventDefault();
318 moveX = e.touches[0].pageX;
319 }, false);
320 ifm.addEventListener("touchend", (e) => {
321 if (startX < moveX && startX + thresh < moveX) {
322 switchTo(e, -1);
323 } else if (startX > moveX && startX - thresh > moveX) {
324 switchTo(e, +1);
325 }
326 }, false);
327 }, false);
329 }
330 function switchTo(e, direction) {
331 e.preventDefault();
332 let len = art_m_list.length, cur, newpos, url;
333 newpos = (curpos+len+direction)%len;
334 if (curpos == newpos) return; // No need to switch to same one
335 curpos = newpos;
336 cur = art_m_list[curpos];
337 url = cur.url;
338 // We should remove iframe once to preserve history Object
339 // https://inthetechpit.com/2019/04/20/update-iframe-without-affecting-browser-history/
340 let parent = iframe.parentNode;
341 // alert("D = "+direction);
342 iframe.remove();
343 parent.appendChild(iframe);
344 try {
345 loading.classList.add("loading");
346 iframe.src = url;
347 // iframe.contentDocument.location.replace(url);
348 // location.replace cannot be used because PDF viewer.js
349 // does not have iframe.contentDocument
350 } catch (err) {
351 alert("Cannot load "+src+" : "+err.name);
352 }
353 _setPjaxCurposInfo();
354 setSwipeAct(iframe);
355 }
356 function switchToByKey(e) {
357 // alert("KEY="+e.key);
358 switch (e.key) {
359 case "ArrowLeft":
360 switchTo(e, -1); break;
361 case "ArrowRight":
362 switchTo(e, +1); break;
363 case "Escape":
364 history.back();
365 }
366 }
367 // <div><p>
368 // <button> << </button><button>Dismiss</button><button> >> </button>
369 // </p><p><span> info1 </span> <span> info2 </span></p>
370 // <iframe src="..."></iframe>
371 // </div>
372 // ==> [ << ][Dissmiss][ >> ]
373 // ==> ## of ## article #xxx by AUTHOR
374 sl.textContent = " << ";
375 sr.textContent = " >> ";
376 sl.addEventListener("click", (e) => {switchTo(e, -1);});
377 sr.addEventListener("click", (e) => {switchTo(e, +1);});
378 sl.setAttribute("title", "to="+(mynum-1));
379 sr.setAttribute("title", "to="+(mynum+1));
380 document.body.appendChild(box);
381 bt.textContent = "Click to dismiss / もどる"+mynum;
383 box.appendChild(p1);
384 p1.appendChild(sl); p1.appendChild(bt); p1.appendChild(sr);
385 p1.appendChild(loading);
386 info.appendChild(info1); info.appendChild(info2);
387 loading.textContent=" Loading...";
388 loading.classList.add("hidden");
389 loading.classList.add("loading");
390 box.appendChild(info);
391 iframe.src = url;
393 document.addEventListener("keydown", switchToByKey);
394 //box.addEventListener("click", (e) => {_resetPjax();});
395 bt.addEventListener("click", (e) => {_resetPjax();});
396 // dp.addEventListener("click", (e) => {_resetPjax();});
397 info.addEventListener("click", (e) => {_resetPjax();});
398 box.appendChild(iframe);
400 setSwipeAct(iframe);
402 _setPjaxCurposInfo();
403 bt.focus();
404 setTimeout(() => {box.classList.add("pjaxview2");}, 10);
405 // Finally update history stack
406 if (history.pushState) {
407 let h = location.href.replace(/#.*/, '')+"#pjaxview";
408 history.pushState({url: h}, null, h);
409 window.addEventListener("popstate", (e) => {
410 if (box) {
411 box.remove(); box = null;
412 }
413 }, false);
414 }
415 }
416 function reverseChecks() {
417 var names = collectElementsByAttr("input", "name", "usel");
418 for (let u of names) {
419 u.checked = !u.checked;
420 }
421 }
422 function renumberOL(str, start) {
423 var stra = str.split("\n");
424 for (var i=1; i<stra.length; i++) {
425 if (stra[i].match(/^[1-9][0-9]*\. /)) {
426 let orig=stra[i];
427 stra[i] = (++start)+". "+RegExp.rightContext;
428 } else if (stra[i].match(/^ /)) {
429 continue;
430 } else
431 break;
432 }
433 return stra.join("\n");
434 }
435 function submitThisForm(input) {
436 for (var elm=input.parentNode; elm; elm = elm.parentNode) {
437 if (elm.nodeName.match(/form/i)) {
438 elm.submit();
439 return true;
440 }
441 }
442 return false;
443 }
444 function helpMarkdownBS(e) {
445 var area = e.target, pos = area.selectionStart, text = area.value;
446 if (area.selectionStart != area.selectionEnd) return;
447 if (pos<2) return;
448 if (text.substr(pos-1, 2)=="\n\n") return;
449 var bol = text.lastIndexOf("\n", pos-1),
450 eol = text.indexOf("\n", pos);
451 if (bol<=0 || bol==eol) return;
452 var thisline = text.substring(bol+1, eol==-1 ? text.length : eol);
453 thisline = text.substring(bol+1, pos);
454 if (thisline == "* ") {
455 area.setSelectionRange(pos-2, pos);
456 } else if (thisline.match(/^[1-9][0-9]*\. $/)) {
457 area.setSelectionRange(pos-RegExp.lastMatch.length, pos);
458 }
459 }
460 function helpMarkdownEnter(e) {
461 if (e.keyCode == 13 && !e.shiftKey) {
462 if (e.metaKey && submitThisForm(e.target)) {
463 e.preventDefault();
464 return;
465 }
466 var area = e.target;
467 var pos = area.selectionStart, text = area.value;
468 if (pos==0) return;
469 var last = text.lastIndexOf("\n", pos-1);
470 var rest = text.substring(pos), rest0=rest;
471 var line = last ? text.substring(last+1, pos) : text;
472 var next = rest.substring(rest.indexOf("\n"))||rest;
473 next=next.substring(1);
474 var tail = text.substring(pos-2, pos), br = (tail==" ");
475 var add = "", offset = 1;
476 if (line.startsWith("* ")) {
477 add = "* ";
478 offset += add.length;
479 if (br) {
480 add = " " + "\n" + add;
481 }
482 } else if (line.match(/^([1-9][0-9]*)\. /)) {
483 var ln = parseInt(RegExp.$1), nn=ln+1,
484 len = RegExp.lastMatch.length;
485 add = nn+". ";
486 let toeol = text.substr(pos, text.indexOf("\n"));
487 if (br) {
488 if (next.startsWith(add)) {
489 add=" ".repeat(len);
490 nn = ln;
491 } else {
492 add = " ".repeat(len)+ "\n" + add;
493 offset -= len+1;
494 }
495 }
496 if (next.match(/^[1-9][0-9]*\. /))
497 rest = renumberOL(rest, nn);
498 offset += add.length;
499 } else if (line.match(/^\|( *).+\|/)) {
500 add = "|" + RegExp.$1 + " |";
501 offset += add.length-2;
502 } else {
503 return;
504 }
505 e.preventDefault();
506 if (!document.execCommand("insertText", false, "\n"+add)) {
507 //Firefox
508 area.selectionEnd = area.value.length;
509 area.setRangeText("\n"+add+rest);
510 area.selectionEnd = null;
511 } else {
512 area.selectionEnd = area.value.length;
513 area.setSelectionRange(area.selectionStart, area.value.length);
514 document.execCommand("insertText", false, rest);
515 area.selectionEnd = null;
516 area.focus();
517 }
518 area.selectionStart = pos+offset;
519 return;
520 if (document.execCommand("insertText", false, "\n"+add)) {
521 //area.setSelectionRange(area.selectionStart, text.length);
522 // alert("rest=["+rest+"], add=["+add+"]");
523 alert(text.substring(pos, area.value.length));
524 if (rest != rest0) {
525 area.setSelectionRange(pos, area.value.length);
526 return;
527 document.execCommand("delete");
528 }
529 document.execCommand("insertText", false, rest);
530 } else {
531 // Firefox cannot use insertText in textarea...
532 area.value = text.substring(0, pos) + "\n" + add + rest;
533 }
534 //area.setSelectionRange(pos+length(add));
535 area.selectionStart=area.selectionEnd = (pos + offset);
537 }
538 }
539 function helpMarkdown(e) {
540 switch (e.key) {
541 case "Backspace": helpMarkdownBS(e); break;
542 case "Enter": helpMarkdownEnter(e); break;
543 }
544 }
545 /* Init event listeners */
546 function addFileInput() {
547 var inpfile = collectElementsByAttr("input", "name", "image");
548 if (!inpfile) return;
549 var filled = true;
550 var i, ih;
551 for (i of inpfile) {
552 if (! i.value) filled=false;
553 }
554 if (filled) {
555 ih = i.parentNode.innerHTML;
556 if (ih) {
557 var inpf = ih.substring(ih.indexOf("<input")),
558 newi = "<br>"+inpf.substring(0, inpf.indexOf(">")+1);
559 i.insertAdjacentHTML("afterend", newi)
560 i.nextSibling.nextSibling.addEventListener('change', () => {
561 // next==br next.next==input[type=file]
562 warnFileSize(document.forms[0]);
563 });
564 }
565 }
566 }
567 function initFileInput() { // Multiplies "input type=file"
568 var el, morefile = document.getElementById("morefile");
569 if (morefile) {
570 for (el of collectElementsByAttr("input", "name", "image")) {
571 el.addEventListener("change", function(ev) {
572 if (ev.target.value > "" && ev.target.files.length == 1)
573 morefile.style.visibility = "visible";
574 // No need to hide again, sure?
575 });
576 }
577 morefile.addEventListener("click", addFileInput, null);
578 }
579 // When renaming, select basename part
580 for (el of collectElementsByAttr("input", "class", "mv")) {
581 el.addEventListener("focus", function(ev) {
582 var i = ev.target;
583 if (i) {
584 i.setSelectionRange(0, i.value.lastIndexOf("."));
585 }
586 });
587 }
588 }
589 function initTextarea() {
590 var te = collectElementsByAttr("textarea", "name", "text");
591 if (!te || !te[0]) return;
592 te[0].addEventListener("keydown", helpMarkdown, false);
593 }
594 function initBlogs() {
595 // Auto-complete #xxxx
596 let i, check = collectElementsByAttr("input", "name", "notifyto");
597 if (check)
598 for (i of check) {
599 i.addEventListener("click", insertRedirect, false);
600 }
601 for (i of document.querySelectorAll("a[href]"))
602 if (i.getAttribute("href").match(/^#[0-9]+$/))
603 if (RegExp.lastMatch == i.innerHTML)
604 i.addEventListener("click", insertRedirect, false)
605 for (i of document.querySelectorAll('input#c[value="送信"]')) {
606 let b = document.createElement("button");
607 b.textContent = "送信!";
608 console.log("b="+b+", tc="+b.textContent);
609 b.addEventListener("click", ajaxPost, false);
610 // i.insertAdjacentElement('afterend', b);
611 b.id = i.id;
612 i.parentNode.replaceChild(b, i);
613 i.remove();
614 }
615 i = document.getElementById("reload");
616 if (i) i.addEventListener("click", ajaxPost, false);
617 for (i of document.querySelectorAll('input[type="file"]')) {
618 i.addEventListener('change', (e) => {
619 warnFileSize(document.forms[0]);
620 }, false)
621 }
622 // Hack article_m links
623 registPjaxViewers(document.querySelectorAll("a[href]"));
624 }
625 function initGrpAction() {
626 var rev = document.getElementById("reverse");
627 if (!rev) return; // Is not grpAction page
628 if (rev.tagName.match(/span/i)) {
629 rev.textContent = " 反転 ";
630 rev.addEventListener("click", reverseChecks, null);
631 }
632 var emailbtn = document.getElementById("email");
633 emailbtn.addEventListener("click", function(ev){
634 // Enlarge box and Select user's checkbox
635 if (!ev.target.checked) return;
636 var x = collectElementsByAttr("div", "class", "foldtabs");
637 if (x && x[0] && x[0].style) {
638 x[0].style.height = "10em";
639 }
640 let myuid = document.getElementById("myuid");
641 if (myuid) {
642 let usel = collectElementsByAttr("input", "name", "usel");
643 if (usel) {
644 for (u of usel) {
645 if (u.value == myuid.value)
646 u.checked = true;
647 }
648 }
649 }
650 }, null);
651 var teamsel = document.getElementById("selteam");
652 if (teamsel) {
653 var usel, p, team;
654 // Select all members of the team
655 teamsel.addEventListener("change", function(ev) {
656 var teamname = teamsel.value,
657 selected = new RegExp('(^| )'+teamname+"($|,)");
658 usel = collectElementsByAttr("input", "name", "usel");
659 if (!usel) return;
660 for (u of usel) {
661 p = u.parentNode; // should be label
662 if (!p) continue;
663 if (teamname == "TEAM") { // Reset all checks
664 u.checked = false; // when "TEAM" is selected
665 } else {
666 p = p.parentNode.parentNode;// should be tr
667 team = nthChildOf(p, 3, "td")
668 if (team && team.textContent
669 && team.textContent.match(selected)) {
670 u.checked = true;
671 }
672 }
673 }
674 }, null);
675 }
676 }
677 function dispInfoMomentary(msg, elem) {
678 // Momentarily display MSG in tooltip-baloon relative to ELEM element.
679 let help = document.createElement("p");
680 elem.style.position = 'relative';
681 elem.style.overflow = 'visible';
682 help.setAttribute("class", "info-tooltip");
683 help.innerHTML = msg;
684 elem.appendChild(help);
685 setTimeout(() => {
686 help.classList.add("dissolving");
687 setTimeout(() => help.remove(), 3000);
688 }, 1000);
689 }
690 function initGrphome() {
691 console.log("initGrphome");
692 // (1)Setup Frozen State Changing Button
693 var ja = navigator.language.match(/ja/i);
695 function toggleFrozen(e, rowid) {
696 let tgt = mypath+"?blog_setfrozen+"+rowid;
697 let td = e.target.parentNode;
698 let tr = td.parentNode;
699 fetch(tgt, {
700 method: "POST",
701 headers: {'Content-Type': 'text/html; charset=utf-8'},
702 credentials: "include"
703 }).then(function(resp) {
704 return resp.text();
705 }).then(function(tbody) {
706 try {
707 var json = JSON.parse(tbody);
708 } catch (e) {
709 return;
710 }
711 let state = json.state, newstate, info;
712 if (json.alert) {
713 alert(json.alert)
714 }
715 if (state.match(/frozen/i)) {
716 newstate = "凍結";
717 info = ja ? newstate+"に設定しました" : 'Set Frozen';
718 } else {
719 newstate = null;
720 info = ja ? '稼動に設定しました' : 'Set Running';
721 }
722 tr.setAttribute("class", newstate);
723 dispInfoMomentary(info, td);
724 });
725 }
726 let btn = document.querySelectorAll("button.toggle-frozen");
727 for (let b of btn) {
728 let rowid = null;
729 let td=b.parentNode, tr = td.parentNode, fr, ru;
730 ru = ja ? "動" : "Running";
731 fr = ja ? "凍" : "Frozen";
732 b.setAttribute('frozen-marker', fr);
733 b.setAttribute('running-marker', ru);
734 for (let a of tr.querySelectorAll("a[href]")) {
735 if (a.getAttribute("href").match(/\?replyblog\+([0-9]+)/)) {
736 rowid = parseInt(RegExp.$1);
737 break;
738 }
739 }
740 if (rowid && rowid>0) {
741 b.addEventListener("click", function(e) {
742 if (!btn) return;
743 toggleFrozen(e, rowid);
744 }, false);
745 b.setAttribute("title", "稼動/凍結をその場で切り替えます\n\
746 Toggle Running/Frozen ("+rowid+")");
747 }
748 }
749 // (2)Setup Column Collapse Button
750 // INCOMPLETE: Cannot restore original state, but it's enough...
751 function toggleColmnWidth(th) {
752 let tbl = document.querySelector("table.dumpblogs");
753 let colname = th.textContent, newwidth;
754 if (th.style.width) {
755 newwidth = null
756 // https://developer.mozilla.org/ja/docs/Web/CSS/table-layout
757 tbl.style.tableLayout = 'auto';
758 tbl.style.width = null;
759 } else {
760 newwidth = "2em";
761 tbl.style.tableLayout = 'fixed';
762 tbl.style.width = '100%';
763 }
764 th.style.width = newwidth;
765 th.style.overflow = "hidden";
766 for (let td of document.querySelectorAll("td."+colname)) {
767 console.log(td.tagName);
768 td.style.width = newwidth;
769 console.log(td.style.width);
770 }
771 }
772 let row1 = document.querySelector("table.dumpblogs tr:first-child");
773 if (row1) {
774 let heads = row1.querySelectorAll("th");
775 for (let h of heads) {
776 h.addEventListener("click", function(e) {
777 toggleColmnWidth(h);
778 }, false);
779 h.setAttribute("title", "Click to shrink these columns");
780 }
781 }
782 }
783 function init() {
784 isOlderJS = !("insertAdjacentElement" in document.body);
785 initGrpAction();
786 initBlogs();
787 initFileInput();
788 initTextarea();
789 initGrphome();
790 }
791 document.addEventListener('DOMContentLoaded', init, null);
792 })();