s4

view s4-main.js @ 850:f11b80b4d005

When blog_mode is "", fallback to quiz/close
author HIROSE Yuuji <yuuji@gentei.org>
date Tue, 30 Jun 2020 08:33:54 +0900
parents 9c4e16c173db
children 3e09ac711f69
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 helpMarkdown(e) {
76 //alert(e.keyCode);
77 if (e.keyCode == 13) {
78 var area = e.target;
79 var pos = area.selectionStart, text = area.value;
80 if (pos==0) return;
81 var last = text.lastIndexOf("\n", pos-1);
82 var rest = text.substring(pos);
83 var line = last ? text.substring(last+1) : text;
84 var tail = text.substring(pos-2, pos), br = (tail==" ");
85 var add = "", offset = 1;
86 if (line.startsWith("* ")) {
87 add = "* ";
88 offset += add.length;
89 if (br) {
90 add = " " + "\n" + add;
91 }
92 } else if (line.match(/^([1-9][0-9]*)\. /)) {
93 ln = parseInt(RegExp.$1);
94 add = (ln+1)+". ";
95 offset += add.length;
96 if (br) {
97 add = " ".repeat(RegExp.$1.length+2) + "\n" + add;
98 }
99 } else if (line.match(/^\|( *).+\|/)) {
100 alert("table");
101 add = "|" + RegExp.$1 + " |";
102 offset += add.length-2;
103 } else {
104 return;
105 }
106 e.preventDefault();
107 area.value = text.substring(0, pos) + "\n" + add + rest;
108 //area.setSelectionRange(pos+length(add));
109 area.selectionStart=area.selectionEnd = (pos + offset);
111 }
112 }
113 /* Init event listeners */
114 function addFileInput() {
115 var inpfile = collectElementsByAttr("input", "name", "image");
116 if (!inpfile) return;
117 var filled = true;
118 var i, ih;
119 for (i of inpfile) {
120 if (! i.value) filled=false;
121 }
122 if (filled) {
123 ih = i.parentNode.innerHTML;
124 if (ih) {
125 var inpf = ih.substring(ih.indexOf("<input")),
126 newi = "<br>"+inpf.substring(0, inpf.indexOf(">")+1);
127 i.insertAdjacentHTML("afterend", newi)
128 // alert(newi);
129 }
130 }
131 }
132 function initFileInput() { // Multiplies "input type=file"
133 var el, morefile = document.getElementById("morefile");
134 if (morefile) {
135 for (el of collectElementsByAttr("input", "name", "image")) {
136 el.addEventListener("change", function(ev) {
137 if (ev.target.value > "" && ev.target.files.length == 1)
138 morefile.style.visibility = "visible";
139 // No need to hide again, sure?
140 });
141 }
142 morefile.addEventListener("click", addFileInput, null);
143 }
144 // When renaming, select basename part
145 for (el of collectElementsByAttr("input", "class", "mv")) {
146 el.addEventListener("focus", function(ev) {
147 var i = ev.target;
148 if (i) {
149 i.setSelectionRange(0, i.value.lastIndexOf("."));
150 }
151 });
152 }
153 }
154 function initTextarea() {
155 var te = collectElementsByAttr("textarea", "name", "text");
156 if (!te || !te[0]) return;
157 te[0].addEventListener("keydown", helpMarkdown, false);
158 }
159 function initBlogs() {
160 // Auto-complete #xxxx
161 var check = collectElementsByAttr("input", "name", "notifyto");
162 if (check)
163 for (let i of check) {
164 i.addEventListener("click", insertRedirect, null);
165 }
166 for (let i of document.getElementsByTagName("a"))
167 if (i.getAttribute("href").match(/^#[0-9]+$/))
168 if (RegExp.lastMatch == i.innerHTML)
169 i.addEventListener("click", insertRedirect, null)
170 }
171 function initGrpAction() {
172 var rev = document.getElementById("reverse");
173 if (!rev) return; // Is not grpAction page
174 if (rev.tagName.match(/span/i)) {
175 rev.textContent = " 反転 ";
176 rev.addEventListener("click", reverseChecks, null);
177 }
178 var emailbtn = document.getElementById("email");
179 emailbtn.addEventListener("click", function(ev){
180 // Enlarge box and Select user's checkbox
181 if (!ev.target.checked) return;
182 var x = collectElementsByAttr("div", "class", "foldtabs");
183 if (x && x[0] && x[0].style) {
184 x[0].style.height = "10em";
185 }
186 let myuid = document.getElementById("myuid");
187 if (myuid) {
188 let usel = collectElementsByAttr("input", "name", "usel");
189 if (usel) {
190 for (u of usel) {
191 if (u.value == myuid.value)
192 u.checked = true;
193 }
194 }
195 }
196 }, null);
197 var teamsel = document.getElementById("selteam");
198 if (teamsel) {
199 var usel, p, team;
200 // Select all members of the team
201 teamsel.addEventListener("change", function(ev) {
202 var teamname = teamsel.value,
203 selected = new RegExp('(^| )'+teamname+"($|,)");
204 usel = collectElementsByAttr("input", "name", "usel");
205 if (!usel) return;
206 for (u of usel) {
207 p = u.parentNode; // should be label
208 if (!p) continue;
209 if (teamname == "TEAM") { // Reset all checks
210 u.checked = false; // when "TEAM" is selected
211 } else {
212 p = p.parentNode.parentNode;// should be tr
213 team = nthChildOf(p, 3, "td")
214 if (team && team.textContent
215 && team.textContent.match(selected)) {
216 u.checked = true;
217 }
218 }
219 }
220 }, null);
221 }
222 }
223 function init() {
224 initGrpAction();
225 initBlogs();
226 initFileInput();
227 initTextarea();
228 }
229 document.addEventListener('DOMContentLoaded', init, null);
230 })();