s4

view s4-main.js @ 907:ce1a355e7cb1

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