s4

view s4-main.js @ 945:29342754a2d0

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