s4

view s4-main.js @ 845:5e980a6c7524

Add application/json
author HIROSE Yuuji <yuuji@gentei.org>
date Fri, 26 Jun 2020 11:41:45 +0900
parents cd6f0badcff4
children 9c4e16c173db
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 addFileInput() {
76 var inpfile = collectElementsByAttr("input", "name", "image");
77 if (!inpfile) return;
78 var filled = true;
79 var i, ih;
80 for (i of inpfile) {
81 if (! i.value) filled=false;
82 }
83 if (filled) {
84 ih = i.parentNode.innerHTML;
85 if (ih) {
86 var inpf = ih.substring(ih.indexOf("<input")),
87 newi = "<br>"+inpf.substring(0, inpf.indexOf(">")+1);
88 i.insertAdjacentHTML("afterend", newi)
89 // alert(newi);
90 }
91 }
92 }
93 function initFileInput() { // Multiplies "input type=file"
94 var el, morefile = document.getElementById("morefile");
95 if (morefile) {
96 for (el of collectElementsByAttr("input", "name", "image")) {
97 el.addEventListener("change", function(ev) {
98 if (ev.target.value > "" && ev.target.files.length == 1)
99 morefile.style.visibility = "visible";
100 // No need to hide again, sure?
101 });
102 }
103 morefile.addEventListener("click", addFileInput, null);
104 }
105 // When renaming, select basename part
106 for (el of collectElementsByAttr("input", "class", "mv")) {
107 el.addEventListener("focus", function(ev) {
108 var i = ev.target;
109 if (i) {
110 i.setSelectionRange(0, i.value.lastIndexOf("."));
111 }
112 });
113 }
114 }
115 function initBlogs() {
116 // Auto-complete #xxxx
117 var check = collectElementsByAttr("input", "name", "notifyto");
118 if (check)
119 for (let i of check) {
120 i.addEventListener("click", insertRedirect, null);
121 }
122 for (let i of document.getElementsByTagName("a"))
123 if (i.getAttribute("href").match(/^#[0-9]+$/))
124 if (RegExp.lastMatch == i.innerHTML)
125 i.addEventListener("click", insertRedirect, null)
126 }
127 function initGrpAction() {
128 var rev = document.getElementById("reverse");
129 if (!rev) return; // Is not grpAction page
130 if (rev.tagName.match(/span/i)) {
131 rev.textContent = " 反転 ";
132 rev.addEventListener("click", reverseChecks, null);
133 }
134 var emailbtn = document.getElementById("email");
135 emailbtn.addEventListener("click", function(ev){
136 // Enlarge box and Select user's checkbox
137 if (!ev.target.checked) return;
138 var x = collectElementsByAttr("div", "class", "foldtabs");
139 if (x && x[0] && x[0].style) {
140 x[0].style.height = "10em";
141 }
142 let myuid = document.getElementById("myuid");
143 if (myuid) {
144 let usel = collectElementsByAttr("input", "name", "usel");
145 if (usel) {
146 for (u of usel) {
147 if (u.value == myuid.value)
148 u.checked = true;
149 }
150 }
151 }
152 }, null);
153 var teamsel = document.getElementById("selteam");
154 if (teamsel) {
155 var usel, p, team;
156 // Select all members of the team
157 teamsel.addEventListener("change", function(ev) {
158 var teamname = teamsel.value,
159 selected = new RegExp('(^| )'+teamname+"($|,)");
160 usel = collectElementsByAttr("input", "name", "usel");
161 if (!usel) return;
162 for (u of usel) {
163 p = u.parentNode; // should be label
164 if (!p) continue;
165 if (teamname == "TEAM") { // Reset all checks
166 u.checked = false; // when "TEAM" is selected
167 } else {
168 p = p.parentNode.parentNode;// should be tr
169 team = nthChildOf(p, 3, "td")
170 if (team && team.textContent
171 && team.textContent.match(selected)) {
172 u.checked = true;
173 }
174 }
175 }
176 }, null);
177 }
178 }
179 function init() {
180 initGrpAction();
181 initBlogs();
182 initFileInput();
183 }
184 document.addEventListener('DOMContentLoaded', init, null);
185 })();