rev |
line source |
yuuji@889
|
1 // 愛
|
yuuji@586
|
2 (function (){
|
yuuji@898
|
3 var isOlderJS; // Set in init();
|
yuuji@972
|
4 var hoverTextLines = 10;
|
yuuji@898
|
5 var hasTouchPad =
|
yuuji@898
|
6 (navigator.maxTouchPoints && navigator.maxTouchPoints >0);
|
yuuji@898
|
7 var myurl = document.URL,
|
yuuji@898
|
8 mypath = myurl.substring(myurl.lastIndexOf("/"));
|
yuuji@898
|
9 var art_m_list = [];
|
yuuji@946
|
10 var mathjax = false;
|
yuuji@915
|
11 let input_pdfsw = 'input[name="comppdf"]';
|
yuuji@898
|
12 if (mypath.match(/(.*)\/(.*)/)) {
|
yuuji@898
|
13 mypath = RegExp.$2;
|
yuuji@898
|
14 mypath = mypath.substring(0, mypath.lastIndexOf("?"));
|
yuuji@898
|
15 //alert("mypath="+mypath);
|
yuuji@898
|
16 }
|
yuuji@667
|
17 function collectElementsByAttr(elm, attr, val) {
|
yuuji@586
|
18 var e = document.getElementsByTagName(elm);
|
yuuji@586
|
19 if (!e) return null;
|
yuuji@586
|
20 var list = [];
|
yuuji@586
|
21 for (var i of e) {
|
yuuji@667
|
22 if (i.getAttribute(attr) == val)
|
yuuji@586
|
23 list.push(i)
|
yuuji@586
|
24 }
|
yuuji@586
|
25 return list;
|
yuuji@586
|
26 }
|
yuuji@675
|
27 function nthChildOf(parent, n, elem) { // Return Nth child of type ELEM
|
yuuji@675
|
28 // N begins with 1
|
yuuji@675
|
29 var i=0;
|
yuuji@675
|
30 var le = elem.toLowerCase();
|
yuuji@675
|
31 for (var c of parent.childNodes) {
|
yuuji@675
|
32 if (!c.tagName) continue;
|
yuuji@675
|
33 if (c.tagName.toLowerCase() == le) {
|
yuuji@675
|
34 if (++i >= n) return c;
|
yuuji@675
|
35 }
|
yuuji@675
|
36 }
|
yuuji@675
|
37 return null;
|
yuuji@675
|
38 }
|
yuuji@586
|
39 function insertRedirect(e) {
|
yuuji@586
|
40 var articleId, textarea = document.getElementById("text");
|
yuuji@586
|
41 var p = e.target, checked = p.checked;
|
yuuji@586
|
42 while (p = p.parentNode)
|
yuuji@586
|
43 if (p.nodeName.match(/^td$/i)) break;
|
yuuji@586
|
44 if (!p) return;
|
yuuji@586
|
45 while (p = p.nextSibling)
|
yuuji@586
|
46 if (p.nodeName.match(/^td$/i)) break;
|
yuuji@586
|
47 if (!p) return;
|
yuuji@586
|
48 articleId = p.getAttribute("id");
|
yuuji@586
|
49 if (textarea && articleId) {
|
yuuji@586
|
50 var tv = textarea.value, lines;
|
yuuji@586
|
51 if (tv)
|
yuuji@586
|
52 lines = tv.split("\n");
|
yuuji@586
|
53 else
|
yuuji@586
|
54 lines = [""];
|
yuuji@586
|
55 var re = new RegExp("[, ]*#"+articleId+"(?![0-9])");
|
yuuji@590
|
56 checked = (p.nodeName.match(/^input$/)
|
yuuji@590
|
57 ? p.checked // checkbox obeys its status
|
yuuji@590
|
58 : !lines[0].match(re)) // a-elment toggles redirection
|
yuuji@586
|
59 if (checked) {
|
yuuji@586
|
60 if (!lines[0].match(re)) {
|
yuuji@586
|
61 var re2 = new RegExp(/>#[#0-9, ]+[0-9]/);
|
yuuji@586
|
62 if (lines[0].match(re2))
|
yuuji@586
|
63 lines[0] = lines[0].replace(
|
yuuji@586
|
64 re2, '$&, '+'#'+articleId);
|
yuuji@586
|
65 else {
|
yuuji@586
|
66 if (lines[0] > "") lines[0] = " "+lines[0];
|
yuuji@586
|
67 lines[0] = ">#"+articleId+lines[0];
|
yuuji@586
|
68 }
|
yuuji@586
|
69 }
|
yuuji@586
|
70 } else { // Remove #xxxxx
|
yuuji@586
|
71 if (lines[0].match(/^>#[0-9 ,]+#/)) // 2 or more #id's
|
yuuji@586
|
72 lines[0] = lines[0].replace(
|
yuuji@586
|
73 new RegExp("^>#"+articleId+"[ ,]*"), ">").replace(
|
yuuji@586
|
74 new RegExp("[ ,]*#"+articleId), "");
|
yuuji@586
|
75 else {
|
yuuji@586
|
76 lines[0] = lines[0].replace(
|
yuuji@586
|
77 new RegExp(">#"+articleId+"[ ,]*"), "");
|
yuuji@586
|
78 }
|
yuuji@586
|
79 }
|
yuuji@586
|
80 lines[0] = lines[0].replace(/^> *$/, '');
|
yuuji@586
|
81 textarea.value = lines.join("\n");
|
yuuji@586
|
82 }
|
yuuji@586
|
83 }
|
yuuji@898
|
84 function registPjaxViewers(aHrefList) {
|
yuuji@898
|
85 let apos=art_m_list.length;
|
yuuji@898
|
86 for (let a of aHrefList) {
|
yuuji@898
|
87 let href = a.getAttribute("href");
|
yuuji@898
|
88 let localvar = apos;
|
yuuji@898
|
89 let td = a.parentNode,
|
yuuji@898
|
90 tr = td.parentNode,
|
yuuji@898
|
91 id = td.id,
|
yuuji@898
|
92 text = td.textContent,
|
yuuji@898
|
93 author = tr.getElementsByTagName("a");
|
yuuji@898
|
94 if (author) author = author[0].getAttribute("title");
|
yuuji@939
|
95 if (href.match(/\?showattc\+article_m\+([0-9]+)$/)) {
|
yuuji@939
|
96 if (td.innerHTML.match(/読み取り不可/)) {
|
yuuji@898
|
97 a.removeAttribute("href");
|
yuuji@898
|
98 continue;
|
yuuji@898
|
99 }
|
yuuji@898
|
100 let url = RegExp.lastMatch;
|
yuuji@898
|
101 // console.log("pjaxView(e, "+href+", "+apos+")");
|
yuuji@898
|
102 a.addEventListener("click", function(e) {
|
yuuji@898
|
103 // Shoud use closure local variable: localvar
|
yuuji@898
|
104 pjaxView(e, href, localvar);
|
yuuji@898
|
105 }, false);
|
yuuji@898
|
106 apos++;
|
yuuji@898
|
107 art_m_list.push({
|
yuuji@898
|
108 url: href, id: id, author: author, text: text
|
yuuji@898
|
109 });
|
yuuji@898
|
110 }
|
yuuji@898
|
111 }
|
yuuji@898
|
112 }
|
yuuji@906
|
113 function registInsertDirect(aHrefList) {
|
yuuji@906
|
114 for (i of aHrefList)
|
yuuji@906
|
115 if (i.getAttribute("href").match(/^#[0-9]+$/))
|
yuuji@906
|
116 if (RegExp.lastMatch == i.innerHTML)
|
yuuji@906
|
117 i.addEventListener("click", insertRedirect, false)
|
yuuji@906
|
118 }
|
yuuji@946
|
119 function mathjaxUpdate(arg) {
|
yuuji@946
|
120 try {
|
yuuji@946
|
121 if (MathJax && MathJax.typesetPromise) {
|
yuuji@946
|
122 MathJax.texReset(); // Reset Math counters
|
yuuji@946
|
123 MathJax.typesetPromise(arg); // MathJax v3
|
yuuji@946
|
124 }
|
yuuji@946
|
125 } catch (err) {console.log(err);}
|
yuuji@946
|
126 }
|
yuuji@898
|
127 var ajaxSubmit;
|
yuuji@898
|
128 function replAddNews(newtable) {
|
yuuji@898
|
129 let newids = [], idlist=[];
|
yuuji@898
|
130 let getArticleID = function (td) {
|
yuuji@898
|
131 return parseInt(td.parentNode.getElementsByTagName("td")[1].id);
|
yuuji@898
|
132 }
|
yuuji@898
|
133 for (let i of newtable.querySelectorAll("td.repl"))
|
yuuji@898
|
134 newids.push(i);
|
yuuji@898
|
135 newids = newids.sort((a,b)=> {
|
yuuji@898
|
136 return (getArticleID(a) - getArticleID(b));
|
yuuji@898
|
137 });
|
yuuji@898
|
138 for (i of newids)
|
yuuji@898
|
139 idlist.push(getArticleID(i));
|
yuuji@898
|
140 console.log("IDList="+idlist.join());
|
yuuji@900
|
141 let cnt=0, ntr;
|
yuuji@898
|
142 let current = collectElementsByAttr("td", "class", "repl"),
|
yuuji@898
|
143 ncur=0, n, icur=0, o, oid, nid, otr;
|
yuuji@898
|
144 current = document.querySelectorAll('td[class="repl"]');
|
yuuji@898
|
145 let last=current[current.length-1],
|
yuuji@898
|
146 tbody = last.parentNode.parentNode;
|
yuuji@906
|
147 let addEventsToNewTr = function(tr) {
|
yuuji@906
|
148 let td = tr.getElementsByTagName("td"),
|
yuuji@906
|
149 td0 = td[0], td1 = td[1];
|
yuuji@906
|
150 td0.classList.add("new");
|
yuuji@906
|
151 registInsertDirect(td0.querySelectorAll("a[href]"));
|
yuuji@906
|
152 registPjaxViewers(td1.querySelectorAll("a[href]"));
|
yuuji@906
|
153 }
|
yuuji@919
|
154 // Erase all "new article" flags before merging
|
yuuji@919
|
155 for (let i of document.querySelectorAll("td.new"))
|
yuuji@919
|
156 i.classList.remove("new");
|
yuuji@898
|
157 // Now reconstruct articles with merge-sort like method
|
yuuji@898
|
158 outer: for (; ncur<newids.length; ncur++) {
|
yuuji@898
|
159 n = newids[ncur];
|
yuuji@898
|
160 if (!n.id) continue;
|
yuuji@898
|
161 nid = parseInt(n.id);
|
yuuji@898
|
162 if (nid<=0) continue;
|
yuuji@900
|
163 ntr = n.parentNode;
|
yuuji@898
|
164 for (; icur<current.length; icur++) {
|
yuuji@898
|
165 o = current[icur];
|
yuuji@898
|
166 otr = o.parentNode;
|
yuuji@898
|
167 oid = getArticleID(o);
|
yuuji@898
|
168 if (!oid || oid=="") continue;
|
yuuji@898
|
169 if (oid >= nid) {
|
yuuji@906
|
170 addEventsToNewTr(ntr);
|
yuuji@898
|
171 tbody.insertBefore(ntr, otr);
|
yuuji@898
|
172 if (oid==nid) otr.remove();
|
yuuji@898
|
173 cnt++;
|
yuuji@898
|
174 continue outer;
|
yuuji@898
|
175 }
|
yuuji@898
|
176 }
|
yuuji@898
|
177 // Append absolutely new articles.
|
yuuji@898
|
178 ntr = n.parentNode;
|
yuuji@906
|
179 addEventsToNewTr(ntr)
|
yuuji@964
|
180 tbody.appendChild(atMarkView(ntr));
|
yuuji@898
|
181 ntr.classList.add("dissolving");
|
yuuji@900
|
182 let localntr = ntr;
|
yuuji@898
|
183 setTimeout(() => {
|
yuuji@900
|
184 localntr.classList.remove("dissolving");
|
yuuji@900
|
185 localntr.classList.add("emerging");
|
yuuji@898
|
186 }, 100);
|
yuuji@975
|
187 initReplyHover(ntr);
|
yuuji@898
|
188 cnt++;
|
yuuji@898
|
189 }
|
yuuji@946
|
190 mathjaxUpdate(newids);
|
yuuji@898
|
191 console.log("Update "+cnt+"rows");
|
yuuji@901
|
192 if (cnt>0 && ntr.scrollIntoView) {
|
yuuji@901
|
193 let option = {behavior: "smooth"};
|
yuuji@901
|
194 if (!isOlderJS) option.block = "center";
|
yuuji@900
|
195 try { // Scroll to last updated row
|
yuuji@901
|
196 ntr.scrollIntoView(option);
|
yuuji@901
|
197 } catch (e1) {}
|
yuuji@901
|
198 }
|
yuuji@898
|
199 return cnt;
|
yuuji@898
|
200 }
|
yuuji@898
|
201
|
yuuji@898
|
202 function warnFileSize(form) {
|
yuuji@898
|
203 let szmax = form.querySelector('input[name="filesize_max"]').value;
|
yuuji@898
|
204 if (!szmax || szmax=="") return;
|
yuuji@898
|
205 szmax = parseInt(szmax);
|
yuuji@898
|
206 if (szmax <= 0) return;
|
yuuji@898
|
207 // szmax = 10000
|
yuuji@915
|
208 let ng = "", rcval=false, fileexists=false,
|
yuuji@915
|
209 pdfsw = form.querySelector(input_pdfsw),
|
yuuji@915
|
210 pdfmsg = "Try compressing PDF?\nPDFを圧縮してみますか?\n" +
|
yuuji@915
|
211 "(それでも収まらない場合もあります)";
|
yuuji@898
|
212 for (let f of form.querySelectorAll('input[type="file"]')) {
|
yuuji@915
|
213 let thiserr = false;
|
yuuji@898
|
214 for (let i of f.files) {
|
yuuji@898
|
215 fileexists = true;
|
yuuji@898
|
216 let fn = i.name, sz = i.size;
|
yuuji@898
|
217 console.log("max="+szmax+", fn="+fn+", sz="+sz);
|
yuuji@898
|
218 if (sz > szmax) {
|
yuuji@915
|
219 if (fn.match(/\.pdf/i)
|
yuuji@915
|
220 && sz < szmax*3 // XXX : x3 reasonable?
|
yuuji@915
|
221 && (pdfsw || confirm(pdfmsg))) {
|
yuuji@915
|
222 if (!pdfsw) {
|
yuuji@915
|
223 pdfsw = document.createElement("input");
|
yuuji@915
|
224 pdfsw.name = "comppdf";
|
yuuji@916
|
225 pdfsw.type = "hidden";
|
yuuji@915
|
226 f.parentNode.insertBefore(pdfsw, f);
|
yuuji@915
|
227 pdfsw.value = "yes";
|
yuuji@915
|
228 }
|
yuuji@915
|
229 } else {
|
yuuji@915
|
230 thiserr = true;
|
yuuji@915
|
231 ng += ((ng>"" ? ", " : "")+fn)
|
yuuji@915
|
232 }
|
yuuji@898
|
233 }
|
yuuji@898
|
234 }
|
yuuji@898
|
235 thiserr ? f.classList.add("warnbg") : f.classList.remove("warnbg");
|
yuuji@898
|
236 }
|
yuuji@898
|
237 if (ng>"") {
|
yuuji@898
|
238 rcval = "File-size Limit Error: "+ng+"\n"+
|
yuuji@898
|
239 "Should be less than "+szmax+"bytes.\n"+
|
yuuji@898
|
240 szmax+"バイト未満にしてください"
|
yuuji@898
|
241 alert(rcval);
|
yuuji@898
|
242 }
|
yuuji@898
|
243 if (form.text.value == "") {
|
yuuji@898
|
244 let w;
|
yuuji@898
|
245 if (fileexists)
|
yuuji@898
|
246 w = "Fill the text area\n" +
|
yuuji@898
|
247 "添付したファイルに関する説明を入れてください。";
|
yuuji@898
|
248 else
|
yuuji@898
|
249 w = "Enter your comment!\n何か書き込んでね!";
|
yuuji@898
|
250 alert(w);
|
yuuji@898
|
251 rcval = (rcval || w);
|
yuuji@898
|
252 form.text.classList.add("warnbg");
|
yuuji@898
|
253 setTimeout(() => {form.text.classList.remove("warnbg");}, 2000)
|
yuuji@898
|
254 }
|
yuuji@898
|
255 return rcval;
|
yuuji@898
|
256 }
|
yuuji@898
|
257 function ajaxPost(e) {
|
yuuji@898
|
258 e.preventDefault();
|
yuuji@898
|
259 let rowid;
|
yuuji@898
|
260 if (!myurl.match(/replyblog\+([0-9]+)/)) return;
|
yuuji@898
|
261 rowid = RegExp.$1
|
yuuji@898
|
262 let myform = document.querySelector("form.replyblog");
|
yuuji@898
|
263 let data = new FormData(myform),
|
yuuji@898
|
264 fetchtime = data.get("fetchtime");
|
yuuji@898
|
265 if (!fetchtime || fetchtime=="") return;
|
yuuji@898
|
266 ///*XX*/fetchtime = "2020-06-14T00:00:00";data.set("fetchtime", fetchtime)
|
yuuji@900
|
267
|
yuuji@900
|
268 ajaxSubmit = e.target;
|
yuuji@900
|
269 ajaxSubmit.back = ajaxSubmit.textContent;
|
yuuji@900
|
270 if (ajaxSubmit.id == "reload") {
|
yuuji@900
|
271 ajaxSubmit.textContent = "更新中"
|
yuuji@900
|
272 data.set("text", "")
|
yuuji@900
|
273 } else {
|
yuuji@900
|
274 if (warnFileSize(myform)) return;
|
yuuji@900
|
275 ajaxSubmit.textContent = "送信中";
|
yuuji@900
|
276 }
|
yuuji@900
|
277 ajaxSubmit.blur();
|
yuuji@900
|
278 ajaxSubmit.disabled = true;
|
yuuji@898
|
279 let act = mypath+"?blog_fetch+"+rowid+"+f:"+fetchtime;
|
yuuji@898
|
280
|
yuuji@898
|
281 function respUpdate(tbody) {
|
yuuji@907
|
282 ajaxSubmit.textContent = ajaxSubmit.back;
|
yuuji@907
|
283 ajaxSubmit.disabled = false;
|
yuuji@898
|
284 let div = document.createElement("div"), form, newform;
|
yuuji@898
|
285 try {
|
yuuji@898
|
286 div.innerHTML = tbody;
|
yuuji@898
|
287 form = div.querySelector("form");
|
yuuji@898
|
288 } catch (er) {
|
yuuji@898
|
289 alert("Cannot parse fetch data");
|
yuuji@898
|
290 return;
|
yuuji@898
|
291 }
|
yuuji@898
|
292 let update = replAddNews(form);
|
yuuji@898
|
293 let dispelem = myform.querySelector("textarea").parentNode;
|
yuuji@907
|
294 if (div.querySelector('input[name="user"]')) { // is login form
|
yuuji@907
|
295 dispInfoMomentary("Login Again Please", dispelem)
|
yuuji@907
|
296 return;
|
yuuji@907
|
297 }
|
yuuji@898
|
298 newform = new FormData(form);
|
yuuji@900
|
299 if (data.get("text") > "") { // Called by submit button
|
yuuji@900
|
300 myform.reset();
|
yuuji@915
|
301 let pdfsw = myform.querySelector(input_pdfsw);
|
yuuji@915
|
302 if (pdfsw) pdfsw.remove();
|
yuuji@900
|
303 // myform.text.value = '';
|
yuuji@900
|
304 }
|
yuuji@898
|
305 myform.fetchtime.value = newform.get("fetchtime");
|
yuuji@898
|
306 myform.id.value = newform.get("id");
|
yuuji@898
|
307 if (update && update > 0) {
|
yuuji@898
|
308 let s = update + " new article" +
|
yuuji@898
|
309 (update>1 ? "s" : "") + " posted";
|
yuuji@898
|
310 dispInfoMomentary(s, dispelem);
|
yuuji@898
|
311 }
|
yuuji@898
|
312 }
|
yuuji@898
|
313 fetch(act, {
|
yuuji@898
|
314 method: "POST", body: data,
|
yuuji@898
|
315 credentials: "include" // For older firefox
|
yuuji@898
|
316 }).then((resp) => {
|
yuuji@898
|
317 return resp.text();
|
yuuji@898
|
318 }).then((tbody) => {
|
yuuji@898
|
319 respUpdate(tbody);
|
yuuji@898
|
320 })
|
yuuji@898
|
321 }
|
yuuji@898
|
322 function pjaxView(ev, url, mynum) {
|
yuuji@928
|
323 if (ev.ctrlKey||ev.shiftKey) return;
|
yuuji@898
|
324 ev.preventDefault();
|
yuuji@898
|
325 let box = document.createElement("div")
|
yuuji@898
|
326 box.setAttribute("class", "pjaxview");
|
yuuji@898
|
327 let p1 = document.createElement("p"),
|
yuuji@898
|
328 bt = document.createElement("button"),
|
yuuji@898
|
329 sl = document.createElement("button"),
|
yuuji@898
|
330 sr = document.createElement("button"),
|
yuuji@898
|
331 loading = document.createElement("span"),
|
yuuji@898
|
332 info = document.createElement("p");
|
yuuji@898
|
333 info1 = document.createElement("span");
|
yuuji@898
|
334 info2 = document.createElement("span");
|
yuuji@898
|
335 iframe = document.createElement("iframe");
|
yuuji@898
|
336 var curpos = mynum;
|
yuuji@898
|
337 var historyBase = history.length;
|
yuuji@898
|
338
|
yuuji@898
|
339 function _setPjaxCurposInfo() {
|
yuuji@898
|
340 let len = art_m_list.length;
|
yuuji@898
|
341 let cur = art_m_list[curpos]
|
yuuji@898
|
342 info1.textContent = (1+curpos)+" of "+len+" article #"+cur.id+
|
yuuji@898
|
343 (cur.author ? " by "+cur.author : "") + ":";
|
yuuji@898
|
344 info2.textContent = cur.text.trim();
|
yuuji@898
|
345 info2.setAttribute("class", "border textdigest");
|
yuuji@898
|
346 }
|
yuuji@898
|
347 function _resetPjax() {
|
yuuji@898
|
348 // All we can do surely is to back 1 page,
|
yuuji@898
|
349 // because we cannot move to desirable entry of history list.
|
yuuji@898
|
350 history.back();
|
yuuji@898
|
351 }
|
yuuji@898
|
352 function setSwipeAct(iframe) {
|
yuuji@898
|
353 // We cannot use DOMContentLoaded nor iframe.contentWindow here.
|
yuuji@898
|
354 // PDF.js does not construct contentWindow...?
|
yuuji@898
|
355 iframe.addEventListener("load", () => {
|
yuuji@898
|
356 loading.classList.remove("loading");
|
yuuji@898
|
357 if (!hasTouchPad) return;
|
yuuji@898
|
358 let ifm = iframe.contentDocument;
|
yuuji@898
|
359 let startX, moveX, thresh = 100;
|
yuuji@898
|
360 ifm.addEventListener("touchstart", (e) => {
|
yuuji@898
|
361 e.preventDefault();
|
yuuji@898
|
362 startX = e.touches[0].pageX;
|
yuuji@898
|
363 }, false);
|
yuuji@898
|
364 ifm.addEventListener("touchmove", (e) => {
|
yuuji@898
|
365 e.preventDefault();
|
yuuji@898
|
366 moveX = e.touches[0].pageX;
|
yuuji@898
|
367 }, false);
|
yuuji@898
|
368 ifm.addEventListener("touchend", (e) => {
|
yuuji@898
|
369 if (startX < moveX && startX + thresh < moveX) {
|
yuuji@898
|
370 switchTo(e, -1);
|
yuuji@898
|
371 } else if (startX > moveX && startX - thresh > moveX) {
|
yuuji@898
|
372 switchTo(e, +1);
|
yuuji@898
|
373 }
|
yuuji@898
|
374 }, false);
|
yuuji@898
|
375 }, false);
|
yuuji@898
|
376
|
yuuji@898
|
377 }
|
yuuji@898
|
378 function switchTo(e, direction) {
|
yuuji@898
|
379 e.preventDefault();
|
yuuji@898
|
380 let len = art_m_list.length, cur, newpos, url;
|
yuuji@898
|
381 newpos = (curpos+len+direction)%len;
|
yuuji@898
|
382 if (curpos == newpos) return; // No need to switch to same one
|
yuuji@898
|
383 curpos = newpos;
|
yuuji@898
|
384 cur = art_m_list[curpos];
|
yuuji@898
|
385 url = cur.url;
|
yuuji@898
|
386 // We should remove iframe once to preserve history Object
|
yuuji@898
|
387 // https://inthetechpit.com/2019/04/20/update-iframe-without-affecting-browser-history/
|
yuuji@898
|
388 let parent = iframe.parentNode;
|
yuuji@898
|
389 // alert("D = "+direction);
|
yuuji@898
|
390 iframe.remove();
|
yuuji@898
|
391 parent.appendChild(iframe);
|
yuuji@898
|
392 try {
|
yuuji@898
|
393 loading.classList.add("loading");
|
yuuji@898
|
394 iframe.src = url;
|
yuuji@898
|
395 // iframe.contentDocument.location.replace(url);
|
yuuji@898
|
396 // location.replace cannot be used because PDF viewer.js
|
yuuji@898
|
397 // does not have iframe.contentDocument
|
yuuji@898
|
398 } catch (err) {
|
yuuji@898
|
399 alert("Cannot load "+src+" : "+err.name);
|
yuuji@898
|
400 }
|
yuuji@898
|
401 _setPjaxCurposInfo();
|
yuuji@898
|
402 setSwipeAct(iframe);
|
yuuji@898
|
403 }
|
yuuji@898
|
404 function switchToByKey(e) {
|
yuuji@898
|
405 // alert("KEY="+e.key);
|
yuuji@898
|
406 switch (e.key) {
|
yuuji@898
|
407 case "ArrowLeft":
|
yuuji@898
|
408 switchTo(e, -1); break;
|
yuuji@898
|
409 case "ArrowRight":
|
yuuji@898
|
410 switchTo(e, +1); break;
|
yuuji@898
|
411 case "Escape":
|
yuuji@898
|
412 history.back();
|
yuuji@898
|
413 }
|
yuuji@898
|
414 }
|
yuuji@898
|
415 // <div><p>
|
yuuji@898
|
416 // <button> << </button><button>Dismiss</button><button> >> </button>
|
yuuji@898
|
417 // </p><p><span> info1 </span> <span> info2 </span></p>
|
yuuji@898
|
418 // <iframe src="..."></iframe>
|
yuuji@898
|
419 // </div>
|
yuuji@898
|
420 // ==> [ << ][Dissmiss][ >> ]
|
yuuji@898
|
421 // ==> ## of ## article #xxx by AUTHOR
|
yuuji@898
|
422 sl.textContent = " << ";
|
yuuji@898
|
423 sr.textContent = " >> ";
|
yuuji@898
|
424 sl.addEventListener("click", (e) => {switchTo(e, -1);});
|
yuuji@898
|
425 sr.addEventListener("click", (e) => {switchTo(e, +1);});
|
yuuji@898
|
426 sl.setAttribute("title", "to="+(mynum-1));
|
yuuji@898
|
427 sr.setAttribute("title", "to="+(mynum+1));
|
yuuji@898
|
428 document.body.appendChild(box);
|
yuuji@898
|
429 bt.textContent = "Click to dismiss / もどる"+mynum;
|
yuuji@898
|
430
|
yuuji@898
|
431 box.appendChild(p1);
|
yuuji@898
|
432 p1.appendChild(sl); p1.appendChild(bt); p1.appendChild(sr);
|
yuuji@930
|
433 { // TEST: Normal mode
|
yuuji@930
|
434 let only = document.createElement("button"),
|
yuuji@930
|
435 h = location.href;
|
yuuji@930
|
436 only.textContent = ".oO□";
|
yuuji@930
|
437 only.setAttribute("title", "Open in Normal Window");
|
yuuji@930
|
438 only.addEventListener("click", function() {
|
yuuji@930
|
439 location.replace(iframe.src);
|
yuuji@930
|
440 });
|
yuuji@930
|
441 p1.appendChild(only);
|
yuuji@930
|
442 }
|
yuuji@898
|
443 p1.appendChild(loading);
|
yuuji@898
|
444 info.appendChild(info1); info.appendChild(info2);
|
yuuji@898
|
445 loading.textContent=" Loading...";
|
yuuji@898
|
446 loading.classList.add("hidden");
|
yuuji@898
|
447 loading.classList.add("loading");
|
yuuji@898
|
448 box.appendChild(info);
|
yuuji@898
|
449 iframe.src = url;
|
yuuji@898
|
450
|
yuuji@925
|
451 box.addEventListener("keydown", switchToByKey);
|
yuuji@898
|
452 //box.addEventListener("click", (e) => {_resetPjax();});
|
yuuji@898
|
453 bt.addEventListener("click", (e) => {_resetPjax();});
|
yuuji@898
|
454 // dp.addEventListener("click", (e) => {_resetPjax();});
|
yuuji@898
|
455 info.addEventListener("click", (e) => {_resetPjax();});
|
yuuji@898
|
456 box.appendChild(iframe);
|
yuuji@898
|
457
|
yuuji@898
|
458 setSwipeAct(iframe);
|
yuuji@898
|
459
|
yuuji@898
|
460 _setPjaxCurposInfo();
|
yuuji@898
|
461 bt.focus();
|
yuuji@898
|
462 setTimeout(() => {box.classList.add("pjaxview2");}, 10);
|
yuuji@898
|
463 // Finally update history stack
|
yuuji@946
|
464 pjaxHistoryPush(box);
|
yuuji@946
|
465 }
|
yuuji@946
|
466 function pjaxHistoryPush(box) {
|
yuuji@898
|
467 if (history.pushState) {
|
yuuji@898
|
468 let h = location.href.replace(/#.*/, '')+"#pjaxview";
|
yuuji@898
|
469 history.pushState({url: h}, null, h);
|
yuuji@898
|
470 window.addEventListener("popstate", (e) => {
|
yuuji@898
|
471 if (box) {
|
yuuji@898
|
472 box.remove(); box = null;
|
yuuji@898
|
473 }
|
yuuji@898
|
474 }, false);
|
yuuji@898
|
475 }
|
yuuji@898
|
476 }
|
yuuji@659
|
477 function reverseChecks() {
|
yuuji@667
|
478 var names = collectElementsByAttr("input", "name", "usel");
|
yuuji@659
|
479 for (let u of names) {
|
yuuji@659
|
480 u.checked = !u.checked;
|
yuuji@659
|
481 }
|
yuuji@659
|
482 }
|
yuuji@852
|
483 function renumberOL(str, start) {
|
yuuji@852
|
484 var stra = str.split("\n");
|
yuuji@852
|
485 for (var i=1; i<stra.length; i++) {
|
yuuji@852
|
486 if (stra[i].match(/^[1-9][0-9]*\. /)) {
|
yuuji@852
|
487 let orig=stra[i];
|
yuuji@852
|
488 stra[i] = (++start)+". "+RegExp.rightContext;
|
yuuji@852
|
489 } else if (stra[i].match(/^ /)) {
|
yuuji@852
|
490 continue;
|
yuuji@852
|
491 } else
|
yuuji@852
|
492 break;
|
yuuji@852
|
493 }
|
yuuji@852
|
494 return stra.join("\n");
|
yuuji@852
|
495 }
|
yuuji@934
|
496 function submitThisForm(e) {
|
yuuji@934
|
497 var input = e.target, ajaxpost = document.getElementById("c");
|
yuuji@852
|
498 for (var elm=input.parentNode; elm; elm = elm.parentNode) {
|
yuuji@934
|
499 if (ajaxpost) {
|
yuuji@934
|
500 ajaxpost.click();
|
yuuji@934
|
501 return true;
|
yuuji@934
|
502 } else if (elm.nodeName.match(/form/i)) {
|
yuuji@852
|
503 elm.submit();
|
yuuji@852
|
504 return true;
|
yuuji@852
|
505 }
|
yuuji@852
|
506 }
|
yuuji@852
|
507 return false;
|
yuuji@852
|
508 }
|
yuuji@852
|
509 function helpMarkdownBS(e) {
|
yuuji@852
|
510 var area = e.target, pos = area.selectionStart, text = area.value;
|
yuuji@852
|
511 if (area.selectionStart != area.selectionEnd) return;
|
yuuji@852
|
512 if (pos<2) return;
|
yuuji@852
|
513 if (text.substr(pos-1, 2)=="\n\n") return;
|
yuuji@852
|
514 var bol = text.lastIndexOf("\n", pos-1),
|
yuuji@852
|
515 eol = text.indexOf("\n", pos);
|
yuuji@852
|
516 if (bol<=0 || bol==eol) return;
|
yuuji@852
|
517 var thisline = text.substring(bol+1, eol==-1 ? text.length : eol);
|
yuuji@852
|
518 thisline = text.substring(bol+1, pos);
|
yuuji@852
|
519 if (thisline == "* ") {
|
yuuji@852
|
520 area.setSelectionRange(pos-2, pos);
|
yuuji@852
|
521 } else if (thisline.match(/^[1-9][0-9]*\. $/)) {
|
yuuji@852
|
522 area.setSelectionRange(pos-RegExp.lastMatch.length, pos);
|
yuuji@852
|
523 }
|
yuuji@852
|
524 }
|
yuuji@852
|
525 function helpMarkdownEnter(e) {
|
yuuji@852
|
526 if (e.keyCode == 13 && !e.shiftKey) {
|
yuuji@934
|
527 if (e.ctrlKey && submitThisForm(e)) {
|
yuuji@852
|
528 e.preventDefault();
|
yuuji@852
|
529 return;
|
yuuji@852
|
530 }
|
yuuji@846
|
531 var area = e.target;
|
yuuji@846
|
532 var pos = area.selectionStart, text = area.value;
|
yuuji@847
|
533 if (pos==0) return;
|
yuuji@846
|
534 var last = text.lastIndexOf("\n", pos-1);
|
yuuji@852
|
535 var rest = text.substring(pos), rest0=rest;
|
yuuji@852
|
536 var line = last ? text.substring(last+1, pos) : text;
|
yuuji@852
|
537 var next = rest.substring(rest.indexOf("\n"))||rest;
|
yuuji@852
|
538 next=next.substring(1);
|
yuuji@847
|
539 var tail = text.substring(pos-2, pos), br = (tail==" ");
|
yuuji@847
|
540 var add = "", offset = 1;
|
yuuji@846
|
541 if (line.startsWith("* ")) {
|
yuuji@847
|
542 add = "* ";
|
yuuji@847
|
543 offset += add.length;
|
yuuji@847
|
544 if (br) {
|
yuuji@847
|
545 add = " " + "\n" + add;
|
yuuji@847
|
546 }
|
yuuji@846
|
547 } else if (line.match(/^([1-9][0-9]*)\. /)) {
|
yuuji@852
|
548 var ln = parseInt(RegExp.$1), nn=ln+1,
|
yuuji@852
|
549 len = RegExp.lastMatch.length;
|
yuuji@852
|
550 add = nn+". ";
|
yuuji@852
|
551 let toeol = text.substr(pos, text.indexOf("\n"));
|
yuuji@852
|
552 if (br) {
|
yuuji@852
|
553 if (next.startsWith(add)) {
|
yuuji@852
|
554 add=" ".repeat(len);
|
yuuji@852
|
555 nn = ln;
|
yuuji@852
|
556 } else {
|
yuuji@852
|
557 add = " ".repeat(len)+ "\n" + add;
|
yuuji@852
|
558 offset -= len+1;
|
yuuji@852
|
559 }
|
yuuji@852
|
560 }
|
yuuji@852
|
561 if (next.match(/^[1-9][0-9]*\. /))
|
yuuji@852
|
562 rest = renumberOL(rest, nn);
|
yuuji@847
|
563 offset += add.length;
|
yuuji@852
|
564 } else if (line.match(/^\|( *).+\|/)) {
|
yuuji@846
|
565 add = "|" + RegExp.$1 + " |";
|
yuuji@847
|
566 offset += add.length-2;
|
yuuji@847
|
567 } else {
|
yuuji@847
|
568 return;
|
yuuji@846
|
569 }
|
yuuji@847
|
570 e.preventDefault();
|
yuuji@852
|
571 if (!document.execCommand("insertText", false, "\n"+add)) {
|
yuuji@852
|
572 //Firefox
|
yuuji@852
|
573 area.selectionEnd = area.value.length;
|
yuuji@852
|
574 area.setRangeText("\n"+add+rest);
|
yuuji@852
|
575 area.selectionEnd = null;
|
yuuji@852
|
576 } else {
|
yuuji@852
|
577 area.selectionEnd = area.value.length;
|
yuuji@852
|
578 area.setSelectionRange(area.selectionStart, area.value.length);
|
yuuji@852
|
579 document.execCommand("insertText", false, rest);
|
yuuji@852
|
580 area.selectionEnd = null;
|
yuuji@852
|
581 area.focus();
|
yuuji@852
|
582 }
|
yuuji@852
|
583 area.selectionStart = pos+offset;
|
yuuji@852
|
584 return;
|
yuuji@852
|
585 if (document.execCommand("insertText", false, "\n"+add)) {
|
yuuji@852
|
586 //area.setSelectionRange(area.selectionStart, text.length);
|
yuuji@852
|
587 // alert("rest=["+rest+"], add=["+add+"]");
|
yuuji@852
|
588 alert(text.substring(pos, area.value.length));
|
yuuji@852
|
589 if (rest != rest0) {
|
yuuji@852
|
590 area.setSelectionRange(pos, area.value.length);
|
yuuji@852
|
591 return;
|
yuuji@852
|
592 document.execCommand("delete");
|
yuuji@852
|
593 }
|
yuuji@852
|
594 document.execCommand("insertText", false, rest);
|
yuuji@852
|
595 } else {
|
yuuji@852
|
596 // Firefox cannot use insertText in textarea...
|
yuuji@852
|
597 area.value = text.substring(0, pos) + "\n" + add + rest;
|
yuuji@852
|
598 }
|
yuuji@846
|
599 //area.setSelectionRange(pos+length(add));
|
yuuji@847
|
600 area.selectionStart=area.selectionEnd = (pos + offset);
|
yuuji@847
|
601
|
yuuji@846
|
602 }
|
yuuji@846
|
603 }
|
yuuji@946
|
604 var helpParenPreview = 0;
|
yuuji@946
|
605 function helpMarkdownParen(e) {
|
yuuji@946
|
606 if (!mathjax) return;
|
yuuji@946
|
607 var area = e.target, pos = area.selectionStart, text = area.value;
|
yuuji@946
|
608 if (pos<2) return;
|
yuuji@946
|
609 if (text[pos-1] == "\\") {
|
yuuji@946
|
610 let ins="( \\)";
|
yuuji@946
|
611 if (text[pos-2] == "\\") ins="( \\\\)";
|
yuuji@946
|
612 area.setRangeText(ins, pos, pos);
|
yuuji@946
|
613 area.selectionStart = pos+2;
|
yuuji@946
|
614 if (helpParenPreview++ < 1) {
|
yuuji@946
|
615 dispInfoMomentary("Preview formula by Meta-p\n"+
|
yuuji@946
|
616 "Meta-p で数式プレビュー", e.target.parentNode);
|
yuuji@946
|
617 }
|
yuuji@946
|
618 e.preventDefault();
|
yuuji@946
|
619 }
|
yuuji@946
|
620 }
|
yuuji@947
|
621 function textInsert(area, string, pos1, pos2) {
|
yuuji@947
|
622 console.log("str="+string);
|
yuuji@947
|
623 area.setRangeText(string, pos1||area.selectionStart,
|
yuuji@947
|
624 pos2||pos1||area.selectionStart);
|
yuuji@947
|
625 area.selectionStart += string.length;
|
yuuji@947
|
626 }
|
yuuji@947
|
627 function beginningOfLine(area, pos) {
|
yuuji@947
|
628 pos = pos||area.selectionStart;
|
yuuji@947
|
629 let b = area.value.lastIndexOf("\n", pos);
|
yuuji@947
|
630 if (pos>1 && area.value.charCodeAt(pos)==10)
|
yuuji@947
|
631 b = area.value.lastIndexOf("\n", pos-1);;
|
yuuji@947
|
632 return b>=0 ? b : 0;
|
yuuji@947
|
633 }
|
yuuji@947
|
634 function isInBeginEnd(area, pos){
|
yuuji@947
|
635 pos = pos||area.selectionStart;
|
yuuji@947
|
636 let bol = beginningOfLine(area, pos);
|
yuuji@947
|
637 let thisline = area.value.substr(bol);
|
yuuji@947
|
638 console.log("curchar="+area.value.charCodeAt(pos));
|
yuuji@947
|
639 console.log("prechar="+area.value.charCodeAt(pos-1));
|
yuuji@947
|
640 console.log("bol="+bol+", thisline="+thisline);
|
yuuji@947
|
641 let match = thisline.search(/\\(begin|end){([A-Za-z]*)/), lm, be;
|
yuuji@947
|
642 if (match >= 0) {
|
yuuji@947
|
643 lm = RegExp.lastMatch;
|
yuuji@947
|
644 be = RegExp.$1;
|
yuuji@947
|
645 return RegExp.$2
|
yuuji@947
|
646 }
|
yuuji@947
|
647 return null;
|
yuuji@946
|
648 }
|
yuuji@946
|
649 function helpMarkdownBrace(e) {
|
yuuji@946
|
650 if (!mathjax) return;
|
yuuji@946
|
651 var area = e.target, pos = area.selectionStart, text = area.value,
|
yuuji@946
|
652 begin = "\\begin", end = "\\end";
|
yuuji@946
|
653 if (pos < end.length) return;
|
yuuji@946
|
654 if (text.substr(pos-end.length).startsWith(end)) {
|
yuuji@946
|
655 let beg = text.lastIndexOf(begin, pos);
|
yuuji@946
|
656 if (beg >= 0) {
|
yuuji@946
|
657 let env = text.substr(beg).search(/\\begin{(.*?)}/);
|
yuuji@946
|
658 if (env >= 0) {
|
yuuji@947
|
659 textInsert(area, "{"+RegExp.$1+"}", pos);
|
yuuji@946
|
660 e.preventDefault();
|
yuuji@946
|
661 }
|
yuuji@946
|
662 }
|
yuuji@946
|
663 }
|
yuuji@946
|
664 }
|
yuuji@947
|
665 function helpMarkdownBraceClose(e) {
|
yuuji@947
|
666 if (!mathjax) return;
|
yuuji@947
|
667 let area = e.target, pos = area.selectionStart, text = area.value,
|
yuuji@947
|
668 begin = "\\begin", end = "\\end";
|
yuuji@947
|
669 if (text.substr(pos).startsWith("}")) {
|
yuuji@947
|
670 area.setRangeText("", pos, pos+1);
|
yuuji@947
|
671 // e.preventDefault();
|
yuuji@947
|
672 }
|
yuuji@947
|
673 let inbegend = isInBeginEnd(area, pos);
|
yuuji@948
|
674 if (!inbegend) return;
|
yuuji@947
|
675 let nextendpos = text.substr(pos).indexOf("\\end{");
|
yuuji@947
|
676 let nextcurend = text.substr(pos).indexOf("\\end{"+inbegend+"}");
|
yuuji@947
|
677 if (nextcurend<0 || nextendpos!=nextcurend) {
|
yuuji@947
|
678 area.setRangeText("}\n\n\\end{"+inbegend+"}", pos, pos);
|
yuuji@947
|
679 area.selectionStart = pos+2;
|
yuuji@947
|
680 e.preventDefault();
|
yuuji@947
|
681 }
|
yuuji@947
|
682 console.log(inbegend);
|
yuuji@947
|
683
|
yuuji@947
|
684 }
|
yuuji@946
|
685 function helpMarkdownPreview(area) {
|
yuuji@946
|
686 if (!mathjax) {
|
yuuji@946
|
687 alert("no"+e.target)
|
yuuji@946
|
688 dispInfoMomentary("This board has no MathJax mode.\n"+
|
yuuji@946
|
689 "この掲示板は数式モードOFFです。",
|
yuuji@946
|
690 e.target.parentNode);
|
yuuji@946
|
691 return;
|
yuuji@946
|
692 }
|
yuuji@946
|
693 let text = area.value;
|
yuuji@946
|
694 let preview = document.createElement("div");
|
yuuji@946
|
695 let bp = document.createElement("p");
|
yuuji@946
|
696 let btn = document.createElement("button");
|
yuuji@946
|
697 btn.innerText = "Click or ESC to Dissmiss / クリックかESCで戻る";
|
yuuji@946
|
698 bp.classList.add("c");
|
yuuji@946
|
699 preview.classList.add("pjaxview");
|
yuuji@946
|
700 preview.classList.add("pjaxview2");
|
yuuji@946
|
701 let pre = document.createElement("p");
|
yuuji@946
|
702 bp.appendChild(btn);
|
yuuji@946
|
703 preview.appendChild(bp);
|
yuuji@946
|
704 preview.appendChild(pre);
|
yuuji@946
|
705 pre.innerText = text;
|
yuuji@946
|
706 document.body.appendChild(preview);
|
yuuji@946
|
707 function dismiss(t) {
|
yuuji@946
|
708 history.back();
|
yuuji@946
|
709 preview.remove();
|
yuuji@946
|
710 area.focus();
|
yuuji@946
|
711 }
|
yuuji@946
|
712 preview.addEventListener("click", dismiss, false);
|
yuuji@946
|
713 preview.addEventListener("keydown", dismiss, false);
|
yuuji@946
|
714 MathJax.typesetPromise([pre]);
|
yuuji@946
|
715 pjaxHistoryPush(preview);
|
yuuji@946
|
716 btn.focus();
|
yuuji@946
|
717 }
|
yuuji@960
|
718 function helpMarkdownAt(e) {
|
yuuji@960
|
719 var area = e.target, pos = area.selectionStart;
|
yuuji@960
|
720 if (pos == 0) {
|
yuuji@960
|
721 area.value = "@all" + area.value;
|
yuuji@960
|
722 area.selectionStart = area.selectionEnd = 4;
|
yuuji@960
|
723 dispInfoMomentary("@all で全員に通知します", area.parentNode);
|
yuuji@960
|
724 e.preventDefault();
|
yuuji@960
|
725 }
|
yuuji@960
|
726 }
|
yuuji@852
|
727 function helpMarkdown(e) {
|
yuuji@898
|
728 switch (e.key) {
|
yuuji@898
|
729 case "Backspace": helpMarkdownBS(e); break;
|
yuuji@898
|
730 case "Enter": helpMarkdownEnter(e); break;
|
yuuji@946
|
731 case "(": helpMarkdownParen(e); break;
|
yuuji@946
|
732 case "p": if (e.metaKey) helpMarkdownPreview(e.target); break;
|
yuuji@946
|
733 case "{": helpMarkdownBrace(e); break;
|
yuuji@947
|
734 case "}": helpMarkdownBraceClose(e); break;
|
yuuji@960
|
735 case "@": helpMarkdownAt(e); break;
|
yuuji@852
|
736 }
|
yuuji@852
|
737 }
|
yuuji@846
|
738 /* Init event listeners */
|
yuuji@837
|
739 function addFileInput() {
|
yuuji@837
|
740 var inpfile = collectElementsByAttr("input", "name", "image");
|
yuuji@837
|
741 if (!inpfile) return;
|
yuuji@837
|
742 var filled = true;
|
yuuji@837
|
743 var i, ih;
|
yuuji@837
|
744 for (i of inpfile) {
|
yuuji@837
|
745 if (! i.value) filled=false;
|
yuuji@837
|
746 }
|
yuuji@837
|
747 if (filled) {
|
yuuji@837
|
748 ih = i.parentNode.innerHTML;
|
yuuji@837
|
749 if (ih) {
|
yuuji@837
|
750 var inpf = ih.substring(ih.indexOf("<input")),
|
yuuji@837
|
751 newi = "<br>"+inpf.substring(0, inpf.indexOf(">")+1);
|
yuuji@837
|
752 i.insertAdjacentHTML("afterend", newi)
|
yuuji@898
|
753 i.nextSibling.nextSibling.addEventListener('change', () => {
|
yuuji@898
|
754 // next==br next.next==input[type=file]
|
yuuji@898
|
755 warnFileSize(document.forms[0]);
|
yuuji@898
|
756 });
|
yuuji@837
|
757 }
|
yuuji@837
|
758 }
|
yuuji@837
|
759 }
|
yuuji@837
|
760 function initFileInput() { // Multiplies "input type=file"
|
yuuji@837
|
761 var el, morefile = document.getElementById("morefile");
|
yuuji@837
|
762 if (morefile) {
|
yuuji@837
|
763 for (el of collectElementsByAttr("input", "name", "image")) {
|
yuuji@837
|
764 el.addEventListener("change", function(ev) {
|
yuuji@837
|
765 if (ev.target.value > "" && ev.target.files.length == 1)
|
yuuji@837
|
766 morefile.style.visibility = "visible";
|
yuuji@837
|
767 // No need to hide again, sure?
|
yuuji@837
|
768 });
|
yuuji@837
|
769 }
|
yuuji@837
|
770 morefile.addEventListener("click", addFileInput, null);
|
yuuji@837
|
771 }
|
yuuji@837
|
772 // When renaming, select basename part
|
yuuji@837
|
773 for (el of collectElementsByAttr("input", "class", "mv")) {
|
yuuji@837
|
774 el.addEventListener("focus", function(ev) {
|
yuuji@837
|
775 var i = ev.target;
|
yuuji@837
|
776 if (i) {
|
yuuji@837
|
777 i.setSelectionRange(0, i.value.lastIndexOf("."));
|
yuuji@837
|
778 }
|
yuuji@837
|
779 });
|
yuuji@837
|
780 }
|
yuuji@837
|
781 }
|
yuuji@846
|
782 function initTextarea() {
|
yuuji@846
|
783 var te = collectElementsByAttr("textarea", "name", "text");
|
yuuji@846
|
784 if (!te || !te[0]) return;
|
yuuji@846
|
785 te[0].addEventListener("keydown", helpMarkdown, false);
|
yuuji@846
|
786 }
|
yuuji@964
|
787 function atMarkView(elem) {
|
yuuji@964
|
788 // Enclose "@all" with span
|
yuuji@964
|
789 for (i of elem.querySelectorAll("td.repl")) {
|
yuuji@964
|
790 let ii = i.innerHTML;
|
yuuji@964
|
791 if (ii.startsWith("@all")) {
|
yuuji@964
|
792 ii = ii.replace(/^@all/,'<div class="atall">@all</div>');
|
yuuji@964
|
793 i.innerHTML = ii;
|
yuuji@964
|
794 }
|
yuuji@964
|
795 }
|
yuuji@964
|
796 return elem;
|
yuuji@964
|
797 }
|
yuuji@969
|
798 var quizwarnVisible = false;
|
yuuji@969
|
799 function toggleAuthorVisibility(e) {
|
yuuji@969
|
800 // In QUIZ mode, click to quizwarn line toggles visibility of author
|
yuuji@969
|
801 e.stopPropagation();
|
yuuji@969
|
802 if (quizwarnVisible) {
|
yuuji@969
|
803 for (let i of document.querySelectorAll("td.repatt")) {
|
yuuji@969
|
804 i.classList.remove("hideauthor");
|
yuuji@969
|
805 }
|
yuuji@969
|
806 quizwarnVisible = false;
|
yuuji@969
|
807 } else {
|
yuuji@969
|
808 for (let i of document.querySelectorAll("td.repatt")) {
|
yuuji@969
|
809 i.classList.add("hideauthor");
|
yuuji@969
|
810 }
|
yuuji@969
|
811 quizwarnVisible = true;
|
yuuji@969
|
812 }
|
yuuji@969
|
813 }
|
yuuji@659
|
814 function initBlogs() {
|
yuuji@837
|
815 // Auto-complete #xxxx
|
yuuji@900
|
816 let i, check = collectElementsByAttr("input", "name", "notifyto");
|
yuuji@586
|
817 if (check)
|
yuuji@900
|
818 for (i of check) {
|
yuuji@898
|
819 i.addEventListener("click", insertRedirect, false);
|
yuuji@586
|
820 }
|
yuuji@906
|
821 registInsertDirect(document.querySelectorAll("a[href]"));
|
yuuji@911
|
822 if (myurl.match(/replyblog\+[0-9]/)
|
yuuji@911
|
823 && document.querySelector("td.repl")) {
|
yuuji@911
|
824 // There's no need to provide ajax posting when
|
yuuji@911
|
825 // no replies written to the blog. Therefore we
|
yuuji@911
|
826 // assign ajax post when td.repl exists.
|
yuuji@909
|
827 for (i of document.querySelectorAll('input#c[value="送信"]')) {
|
yuuji@909
|
828 let b = document.createElement("button");
|
yuuji@909
|
829 b.textContent = "送信!";
|
yuuji@909
|
830 console.log("b="+b+", tc="+b.textContent);
|
yuuji@909
|
831 b.addEventListener("click", ajaxPost, false);
|
yuuji@909
|
832 // i.insertAdjacentElement('afterend', b);
|
yuuji@910
|
833 b.setAttribute("class", i.getAttribute("class"))
|
yuuji@910
|
834 b.setAttribute("title", i.getAttribute("title"))
|
yuuji@910
|
835 i.parentNode.replaceChild(b, i);
|
yuuji@909
|
836 b.id = i.id;
|
yuuji@914
|
837 // i.remove();
|
yuuji@914
|
838 i.classList.add("aux");
|
yuuji@914
|
839 i.value = "送信(予備)"
|
yuuji@914
|
840 b.parentNode.appendChild(i);
|
yuuji@909
|
841 }
|
yuuji@911
|
842 i = document.getElementById("reload");
|
yuuji@911
|
843 if (i) i.addEventListener("click", ajaxPost, false);
|
yuuji@911
|
844 }
|
yuuji@902
|
845 for (i of document.querySelectorAll('input[type="file"]')) {
|
yuuji@902
|
846 i.addEventListener('change', (e) => {
|
yuuji@902
|
847 warnFileSize(document.forms[0]);
|
yuuji@898
|
848 }, false)
|
yuuji@898
|
849 }
|
yuuji@969
|
850 if (i=document.getElementById("quizwarn")) {
|
yuuji@969
|
851 i.addEventListener('click', toggleAuthorVisibility, false);
|
yuuji@969
|
852 }
|
yuuji@898
|
853 // Hack article_m links
|
yuuji@898
|
854 registPjaxViewers(document.querySelectorAll("a[href]"));
|
yuuji@964
|
855 atMarkView(document);
|
yuuji@586
|
856 }
|
yuuji@659
|
857 function initGrpAction() {
|
yuuji@659
|
858 var rev = document.getElementById("reverse");
|
yuuji@667
|
859 if (!rev) return; // Is not grpAction page
|
yuuji@667
|
860 if (rev.tagName.match(/span/i)) {
|
yuuji@659
|
861 rev.textContent = " 反転 ";
|
yuuji@659
|
862 rev.addEventListener("click", reverseChecks, null);
|
yuuji@659
|
863 }
|
yuuji@667
|
864 var emailbtn = document.getElementById("email");
|
yuuji@667
|
865 emailbtn.addEventListener("click", function(ev){
|
yuuji@675
|
866 // Enlarge box and Select user's checkbox
|
yuuji@667
|
867 if (!ev.target.checked) return;
|
yuuji@673
|
868 var x = collectElementsByAttr("div", "class", "foldtabs");
|
yuuji@673
|
869 if (x && x[0] && x[0].style) {
|
yuuji@673
|
870 x[0].style.height = "10em";
|
yuuji@673
|
871 }
|
yuuji@667
|
872 let myuid = document.getElementById("myuid");
|
yuuji@667
|
873 if (myuid) {
|
yuuji@667
|
874 let usel = collectElementsByAttr("input", "name", "usel");
|
yuuji@667
|
875 if (usel) {
|
yuuji@667
|
876 for (u of usel) {
|
yuuji@667
|
877 if (u.value == myuid.value)
|
yuuji@667
|
878 u.checked = true;
|
yuuji@667
|
879 }
|
yuuji@667
|
880 }
|
yuuji@667
|
881 }
|
yuuji@667
|
882 }, null);
|
yuuji@675
|
883 var teamsel = document.getElementById("selteam");
|
yuuji@675
|
884 if (teamsel) {
|
yuuji@675
|
885 var usel, p, team;
|
yuuji@675
|
886 // Select all members of the team
|
yuuji@675
|
887 teamsel.addEventListener("change", function(ev) {
|
yuuji@675
|
888 var teamname = teamsel.value,
|
yuuji@676
|
889 selected = new RegExp('(^| )'+teamname+"($|,)");
|
yuuji@675
|
890 usel = collectElementsByAttr("input", "name", "usel");
|
yuuji@675
|
891 if (!usel) return;
|
yuuji@675
|
892 for (u of usel) {
|
yuuji@675
|
893 p = u.parentNode; // should be label
|
yuuji@675
|
894 if (!p) continue;
|
yuuji@675
|
895 if (teamname == "TEAM") { // Reset all checks
|
yuuji@675
|
896 u.checked = false; // when "TEAM" is selected
|
yuuji@675
|
897 } else {
|
yuuji@675
|
898 p = p.parentNode.parentNode;// should be tr
|
yuuji@938
|
899 team = nthChildOf(p, 4, "td")
|
yuuji@675
|
900 if (team && team.textContent
|
yuuji@675
|
901 && team.textContent.match(selected)) {
|
yuuji@675
|
902 u.checked = true;
|
yuuji@675
|
903 }
|
yuuji@675
|
904 }
|
yuuji@675
|
905 }
|
yuuji@675
|
906 }, null);
|
yuuji@675
|
907 }
|
yuuji@659
|
908 }
|
yuuji@893
|
909 function dispInfoMomentary(msg, elem) {
|
yuuji@893
|
910 // Momentarily display MSG in tooltip-baloon relative to ELEM element.
|
yuuji@893
|
911 let help = document.createElement("p");
|
yuuji@893
|
912 elem.style.position = 'relative';
|
yuuji@893
|
913 elem.style.overflow = 'visible';
|
yuuji@893
|
914 help.setAttribute("class", "info-tooltip");
|
yuuji@893
|
915 help.innerHTML = msg;
|
yuuji@893
|
916 elem.appendChild(help);
|
yuuji@893
|
917 setTimeout(() => {
|
yuuji@893
|
918 help.classList.add("dissolving");
|
yuuji@893
|
919 setTimeout(() => help.remove(), 3000);
|
yuuji@893
|
920 }, 1000);
|
yuuji@893
|
921 }
|
yuuji@889
|
922 function initGrphome() {
|
yuuji@889
|
923 console.log("initGrphome");
|
yuuji@894
|
924 // (1)Setup Frozen State Changing Button
|
yuuji@893
|
925 var ja = navigator.language.match(/ja/i);
|
yuuji@889
|
926
|
yuuji@889
|
927 function toggleFrozen(e, rowid) {
|
yuuji@889
|
928 let tgt = mypath+"?blog_setfrozen+"+rowid;
|
yuuji@893
|
929 let td = e.target.parentNode;
|
yuuji@893
|
930 let tr = td.parentNode;
|
yuuji@889
|
931 fetch(tgt, {
|
yuuji@889
|
932 method: "POST",
|
yuuji@889
|
933 headers: {'Content-Type': 'text/html; charset=utf-8'},
|
yuuji@898
|
934 credentials: "include"
|
yuuji@889
|
935 }).then(function(resp) {
|
yuuji@889
|
936 return resp.text();
|
yuuji@889
|
937 }).then(function(tbody) {
|
yuuji@889
|
938 try {
|
yuuji@889
|
939 var json = JSON.parse(tbody);
|
yuuji@889
|
940 } catch (e) {
|
yuuji@889
|
941 return;
|
yuuji@889
|
942 }
|
yuuji@893
|
943 let state = json.state, newstate, info;
|
yuuji@889
|
944 if (json.alert) {
|
yuuji@889
|
945 alert(json.alert)
|
yuuji@889
|
946 }
|
yuuji@889
|
947 if (state.match(/frozen/i)) {
|
yuuji@889
|
948 newstate = "凍結";
|
yuuji@893
|
949 info = ja ? newstate+"に設定しました" : 'Set Frozen';
|
yuuji@889
|
950 } else {
|
yuuji@889
|
951 newstate = null;
|
yuuji@893
|
952 info = ja ? '稼動に設定しました' : 'Set Running';
|
yuuji@889
|
953 }
|
yuuji@889
|
954 tr.setAttribute("class", newstate);
|
yuuji@893
|
955 dispInfoMomentary(info, td);
|
yuuji@889
|
956 });
|
yuuji@889
|
957 }
|
yuuji@894
|
958 let btn = document.querySelectorAll("button.toggle-frozen");
|
yuuji@889
|
959 for (let b of btn) {
|
yuuji@889
|
960 let rowid = null;
|
yuuji@893
|
961 let td=b.parentNode, tr = td.parentNode, fr, ru;
|
yuuji@893
|
962 ru = ja ? "動" : "Running";
|
yuuji@893
|
963 fr = ja ? "凍" : "Frozen";
|
yuuji@893
|
964 b.setAttribute('frozen-marker', fr);
|
yuuji@893
|
965 b.setAttribute('running-marker', ru);
|
yuuji@889
|
966 for (let a of tr.querySelectorAll("a[href]")) {
|
yuuji@889
|
967 if (a.getAttribute("href").match(/\?replyblog\+([0-9]+)/)) {
|
yuuji@889
|
968 rowid = parseInt(RegExp.$1);
|
yuuji@889
|
969 break;
|
yuuji@889
|
970 }
|
yuuji@889
|
971 }
|
yuuji@889
|
972 if (rowid && rowid>0) {
|
yuuji@889
|
973 b.addEventListener("click", function(e) {
|
yuuji@889
|
974 if (!btn) return;
|
yuuji@889
|
975 toggleFrozen(e, rowid);
|
yuuji@889
|
976 }, false);
|
yuuji@889
|
977 b.setAttribute("title", "稼動/凍結をその場で切り替えます\n\
|
yuuji@889
|
978 Toggle Running/Frozen ("+rowid+")");
|
yuuji@889
|
979 }
|
yuuji@889
|
980 }
|
yuuji@894
|
981 // (2)Setup Column Collapse Button
|
yuuji@894
|
982 // INCOMPLETE: Cannot restore original state, but it's enough...
|
yuuji@894
|
983 function toggleColmnWidth(th) {
|
yuuji@894
|
984 let tbl = document.querySelector("table.dumpblogs");
|
yuuji@894
|
985 let colname = th.textContent, newwidth;
|
yuuji@894
|
986 if (th.style.width) {
|
yuuji@894
|
987 newwidth = null
|
yuuji@894
|
988 // https://developer.mozilla.org/ja/docs/Web/CSS/table-layout
|
yuuji@894
|
989 tbl.style.tableLayout = 'auto';
|
yuuji@894
|
990 tbl.style.width = null;
|
yuuji@894
|
991 } else {
|
yuuji@894
|
992 newwidth = "2em";
|
yuuji@894
|
993 tbl.style.tableLayout = 'fixed';
|
yuuji@894
|
994 tbl.style.width = '100%';
|
yuuji@894
|
995 }
|
yuuji@894
|
996 th.style.width = newwidth;
|
yuuji@894
|
997 th.style.overflow = "hidden";
|
yuuji@894
|
998 for (let td of document.querySelectorAll("td."+colname)) {
|
yuuji@894
|
999 console.log(td.tagName);
|
yuuji@894
|
1000 td.style.width = newwidth;
|
yuuji@894
|
1001 console.log(td.style.width);
|
yuuji@894
|
1002 }
|
yuuji@894
|
1003 }
|
yuuji@894
|
1004 let row1 = document.querySelector("table.dumpblogs tr:first-child");
|
yuuji@894
|
1005 if (row1) {
|
yuuji@894
|
1006 let heads = row1.querySelectorAll("th");
|
yuuji@894
|
1007 for (let h of heads) {
|
yuuji@894
|
1008 h.addEventListener("click", function(e) {
|
yuuji@894
|
1009 toggleColmnWidth(h);
|
yuuji@894
|
1010 }, false);
|
yuuji@894
|
1011 h.setAttribute("title", "Click to shrink these columns");
|
yuuji@894
|
1012 }
|
yuuji@894
|
1013 }
|
yuuji@889
|
1014 }
|
yuuji@946
|
1015 function initMath() {
|
yuuji@959
|
1016 mathjax = window.MathJax||document.getElementById("mathjax");
|
yuuji@946
|
1017 if (!mathjax) return;
|
yuuji@946
|
1018 let ta = document.querySelector("textarea");
|
yuuji@946
|
1019 if (!ta) return;
|
yuuji@946
|
1020 let btn = document.createElement("button");
|
yuuji@954
|
1021 btn.setAttribute("title", "\\( と \\) で数式利用\n"+
|
yuuji@954
|
1022 "\\[ と \\] で段組み数式モード\n"+
|
yuuji@954
|
1023 "便利なマクロ:\n"+
|
yuuji@954
|
1024 " \\boxed{aaa}, \\fcolorbox{framecolor}{bgcolor}{text}\n"+
|
yuuji@954
|
1025 " \\underline{aaa}, \\fcolorbox{framecolor}{bgcolor}{text}\n"+
|
yuuji@954
|
1026 "独自定義マクロ:\n"+
|
yuuji@954
|
1027 " \\warn{xxx} 注意喚起用色付き枠\n"+
|
yuuji@954
|
1028 " \\Warn{xxx} 大きな文字で注意喚起")
|
yuuji@946
|
1029 btn.innerHTML = "MathJax<br>Preview";
|
yuuji@946
|
1030 btn.addEventListener('click', (e) => {
|
yuuji@946
|
1031 e.preventDefault();
|
yuuji@946
|
1032 ta.focus();
|
yuuji@946
|
1033 helpMarkdownPreview(ta);
|
yuuji@946
|
1034 });
|
yuuji@949
|
1035 ta.parentNode.appendChild(btn);
|
yuuji@946
|
1036 }
|
yuuji@975
|
1037 function initReplyHover(unit) {
|
yuuji@973
|
1038 function getTextById(id) {
|
yuuji@974
|
1039 let repltd = document.getElementById(id);
|
yuuji@974
|
1040 if (repltd) {
|
yuuji@974
|
1041 let txt = repltd.innerText,
|
yuuji@974
|
1042 authtd = repltd.parentNode.getElementsByTagName("td")[0],
|
yuuji@976
|
1043 author = authtd.querySelector("a.author").innerText,
|
yuuji@974
|
1044 digest = txt.split("\n").splice(0, hoverTextLines).join("\n");
|
yuuji@974
|
1045 return "[[ "+author+" ]]\n"+digest;
|
yuuji@974
|
1046 } else
|
yuuji@974
|
1047 return "";
|
yuuji@972
|
1048 }
|
yuuji@975
|
1049 unit = unit||document;
|
yuuji@975
|
1050 for (let td of unit.querySelectorAll("td.repl")) {
|
yuuji@972
|
1051 let text = td.innerHTML;
|
yuuji@972
|
1052 if (text.startsWith("\>#")) {
|
yuuji@972
|
1053 let newline = text.indexOf("\n");
|
yuuji@978
|
1054 let first, rest;
|
yuuji@979
|
1055 if (newline > 0) {
|
yuuji@977
|
1056 first = text.substring(0, newline);
|
yuuji@977
|
1057 rest = text.substring(newline);
|
yuuji@977
|
1058 } else {
|
yuuji@977
|
1059 first = text;
|
yuuji@977
|
1060 rest = "";
|
yuuji@977
|
1061 }
|
yuuji@972
|
1062 td.innerHTML = first.replace(
|
yuuji@972
|
1063 /#([0-9]+)/g,
|
yuuji@972
|
1064 (match, start, whole) => {
|
yuuji@972
|
1065 let id = RegExp.$1
|
yuuji@975
|
1066 return '<a title="' + getTextById(id)
|
yuuji@975
|
1067 + '" href="' + match
|
yuuji@975
|
1068 + '">' + match + '</a>';
|
yuuji@972
|
1069 }
|
yuuji@972
|
1070 ) + rest;
|
yuuji@972
|
1071 }
|
yuuji@972
|
1072 }
|
yuuji@972
|
1073 }
|
yuuji@659
|
1074 function init() {
|
yuuji@898
|
1075 isOlderJS = !("insertAdjacentElement" in document.body);
|
yuuji@659
|
1076 initGrpAction();
|
yuuji@659
|
1077 initBlogs();
|
yuuji@837
|
1078 initFileInput();
|
yuuji@846
|
1079 initTextarea();
|
yuuji@889
|
1080 initGrphome();
|
yuuji@946
|
1081 initMath();
|
yuuji@980
|
1082 if (mathjax && MathJax.startup)
|
yuuji@980
|
1083 MathJax.startup.promise.then(()=>initReplyHover());
|
yuuji@980
|
1084 else
|
yuuji@980
|
1085 initReplyHover();
|
yuuji@659
|
1086 }
|
yuuji@586
|
1087 document.addEventListener('DOMContentLoaded', init, null);
|
yuuji@586
|
1088 })();
|