s4

view s4-main.js @ 593:874e1f59263f

Typo and logic fixed
author HIROSE Yuuji <yuuji@gentei.org>
date Tue, 03 Dec 2019 12:29:32 +0900
parents a03bfd5d242a
children a36a2c3b3056
line source
1 (function (){
2 function collectElementsByNameAttr(elm, name) {
3 var e = document.getElementsByTagName(elm);
4 if (!e) return null;
5 var list = [];
6 for (var i of e) {
7 if (i.getAttribute("name") == name)
8 list.push(i)
9 }
10 return list;
11 }
12 function insertRedirect(e) {
13 var articleId, textarea = document.getElementById("text");
14 var p = e.target, checked = p.checked;
15 while (p = p.parentNode)
16 if (p.nodeName.match(/^td$/i)) break;
17 if (!p) return;
18 while (p = p.nextSibling)
19 if (p.nodeName.match(/^td$/i)) break;
20 if (!p) return;
21 articleId = p.getAttribute("id");
22 if (textarea && articleId) {
23 var tv = textarea.value, lines;
24 if (tv)
25 lines = tv.split("\n");
26 else
27 lines = [""];
28 var re = new RegExp("[, ]*#"+articleId+"(?![0-9])");
29 checked = (p.nodeName.match(/^input$/)
30 ? p.checked // checkbox obeys its status
31 : !lines[0].match(re)) // a-elment toggles redirection
32 if (checked) {
33 if (!lines[0].match(re)) {
34 var re2 = new RegExp(/>#[#0-9, ]+[0-9]/);
35 if (lines[0].match(re2))
36 lines[0] = lines[0].replace(
37 re2, '$&, '+'#'+articleId);
38 else {
39 if (lines[0] > "") lines[0] = " "+lines[0];
40 lines[0] = ">#"+articleId+lines[0];
41 }
42 }
43 } else { // Remove #xxxxx
44 if (lines[0].match(/^>#[0-9 ,]+#/)) // 2 or more #id's
45 lines[0] = lines[0].replace(
46 new RegExp("^>#"+articleId+"[ ,]*"), ">").replace(
47 new RegExp("[ ,]*#"+articleId), "");
48 else {
49 lines[0] = lines[0].replace(
50 new RegExp(">#"+articleId+"[ ,]*"), "");
51 }
52 }
53 lines[0] = lines[0].replace(/^> *$/, '');
54 textarea.value = lines.join("\n");
55 }
56 }
57 function init() {
58 var el, check = collectElementsByNameAttr("input", "notifyto");
59 if (check)
60 for (let i of check) {
61 i.addEventListener("click", insertRedirect, null);
62 }
63 for (let i of document.getElementsByTagName("a"))
64 if (i.getAttribute("href").match(/^#[0-9]+$/))
65 if (RegExp.lastMatch == i.innerHTML)
66 i.addEventListener("click", insertRedirect, null)
67 }
68 document.addEventListener('DOMContentLoaded', init, null);
69 })();