s4

view s4-main.js @ 824:fcf485cbabc6

At migration, translate direct-link to attachment files
author HIROSE Yuuji <yuuji@gentei.org>
date Sat, 20 Jun 2020 18:18:17 +0900
parents 9c54908814be
children a6462eea48be
line source
1 (function (){
2 function collectElementsByAttr(elm, attr, val) {
3 var e = document.getElementsByTagName(elm);
4 if (!e) return null;
5 var list = [];
6 for (var i of e) {
7 if (i.getAttribute(attr) == val)
8 list.push(i)
9 }
10 return list;
11 }
12 function nthChildOf(parent, n, elem) { // Return Nth child of type ELEM
13 // N begins with 1
14 var i=0;
15 var le = elem.toLowerCase();
16 for (var c of parent.childNodes) {
17 if (!c.tagName) continue;
18 if (c.tagName.toLowerCase() == le) {
19 if (++i >= n) return c;
20 }
21 }
22 return null;
23 }
24 function insertRedirect(e) {
25 var articleId, textarea = document.getElementById("text");
26 var p = e.target, checked = p.checked;
27 while (p = p.parentNode)
28 if (p.nodeName.match(/^td$/i)) break;
29 if (!p) return;
30 while (p = p.nextSibling)
31 if (p.nodeName.match(/^td$/i)) break;
32 if (!p) return;
33 articleId = p.getAttribute("id");
34 if (textarea && articleId) {
35 var tv = textarea.value, lines;
36 if (tv)
37 lines = tv.split("\n");
38 else
39 lines = [""];
40 var re = new RegExp("[, ]*#"+articleId+"(?![0-9])");
41 checked = (p.nodeName.match(/^input$/)
42 ? p.checked // checkbox obeys its status
43 : !lines[0].match(re)) // a-elment toggles redirection
44 if (checked) {
45 if (!lines[0].match(re)) {
46 var re2 = new RegExp(/>#[#0-9, ]+[0-9]/);
47 if (lines[0].match(re2))
48 lines[0] = lines[0].replace(
49 re2, '$&, '+'#'+articleId);
50 else {
51 if (lines[0] > "") lines[0] = " "+lines[0];
52 lines[0] = ">#"+articleId+lines[0];
53 }
54 }
55 } else { // Remove #xxxxx
56 if (lines[0].match(/^>#[0-9 ,]+#/)) // 2 or more #id's
57 lines[0] = lines[0].replace(
58 new RegExp("^>#"+articleId+"[ ,]*"), ">").replace(
59 new RegExp("[ ,]*#"+articleId), "");
60 else {
61 lines[0] = lines[0].replace(
62 new RegExp(">#"+articleId+"[ ,]*"), "");
63 }
64 }
65 lines[0] = lines[0].replace(/^> *$/, '');
66 textarea.value = lines.join("\n");
67 }
68 }
69 function reverseChecks() {
70 var names = collectElementsByAttr("input", "name", "usel");
71 for (let u of names) {
72 u.checked = !u.checked;
73 }
74 }
75 function initBlogs() {
76 var el, check = collectElementsByAttr("input", "name", "notifyto");
77 if (check)
78 for (let i of check) {
79 i.addEventListener("click", insertRedirect, null);
80 }
81 for (let i of document.getElementsByTagName("a"))
82 if (i.getAttribute("href").match(/^#[0-9]+$/))
83 if (RegExp.lastMatch == i.innerHTML)
84 i.addEventListener("click", insertRedirect, null)
85 }
86 function initGrpAction() {
87 var rev = document.getElementById("reverse");
88 if (!rev) return; // Is not grpAction page
89 if (rev.tagName.match(/span/i)) {
90 rev.textContent = " 反転 ";
91 rev.addEventListener("click", reverseChecks, null);
92 }
93 var emailbtn = document.getElementById("email");
94 emailbtn.addEventListener("click", function(ev){
95 // Enlarge box and Select user's checkbox
96 if (!ev.target.checked) return;
97 var x = collectElementsByAttr("div", "class", "foldtabs");
98 if (x && x[0] && x[0].style) {
99 x[0].style.height = "10em";
100 }
101 let myuid = document.getElementById("myuid");
102 if (myuid) {
103 let usel = collectElementsByAttr("input", "name", "usel");
104 if (usel) {
105 for (u of usel) {
106 if (u.value == myuid.value)
107 u.checked = true;
108 }
109 }
110 }
111 }, null);
112 var teamsel = document.getElementById("selteam");
113 if (teamsel) {
114 var usel, p, team;
115 // Select all members of the team
116 teamsel.addEventListener("change", function(ev) {
117 var teamname = teamsel.value,
118 selected = new RegExp('(^| )'+teamname+"($|,)");
119 usel = collectElementsByAttr("input", "name", "usel");
120 if (!usel) return;
121 for (u of usel) {
122 p = u.parentNode; // should be label
123 if (!p) continue;
124 if (teamname == "TEAM") { // Reset all checks
125 u.checked = false; // when "TEAM" is selected
126 } else {
127 p = p.parentNode.parentNode;// should be tr
128 team = nthChildOf(p, 3, "td")
129 if (team && team.textContent
130 && team.textContent.match(selected)) {
131 u.checked = true;
132 }
133 }
134 }
135 }, null);
136 }
137 }
138 function init() {
139 initGrpAction();
140 initBlogs();
141 }
142 document.addEventListener('DOMContentLoaded', init, null);
143 })();