view s4-main.js @ 827:97be0474f268

List belonging groups
author HIROSE Yuuji <yuuji@gentei.org>
date Sun, 21 Jun 2020 16:13:52 +0900
parents cd6f0badcff4
children a6462eea48be
line wrap: on
line source

(function (){
    function collectElementsByAttr(elm, attr, val) {
	var e = document.getElementsByTagName(elm);
	if (!e) return null;
	var list = [];
	for (var i of e) {
	    if (i.getAttribute(attr) == val)
		list.push(i)
	}
	return list;
    }
    function nthChildOf(parent, n, elem) { // Return Nth child of type ELEM
	// N begins with 1
	var i=0;
	var le = elem.toLowerCase();
	for (var c of parent.childNodes) {
	    if (!c.tagName) continue;
	    if (c.tagName.toLowerCase() == le) {
		if (++i >= n) return c;
	    }
	}
	return null;
    }
    function insertRedirect(e) {
	var articleId, textarea = document.getElementById("text");
	var p =	e.target, checked = p.checked;
	while (p = p.parentNode)
	    if (p.nodeName.match(/^td$/i)) break;
	if (!p) return;
	while (p = p.nextSibling)
	    if (p.nodeName.match(/^td$/i)) break;
	if (!p) return;
	articleId = p.getAttribute("id");
	if (textarea && articleId) {
	    var tv = textarea.value, lines;
	    if (tv)
		lines = tv.split("\n");
	    else
		lines = [""];
	    var re = new RegExp("[, ]*#"+articleId+"(?![0-9])");
	    checked = (p.nodeName.match(/^input$/)
		       ? p.checked		// checkbox obeys its status
		       : !lines[0].match(re))	// a-elment toggles redirection
	    if (checked) {
		if (!lines[0].match(re)) {
		    var re2 = new RegExp(/>#[#0-9, ]+[0-9]/);
		    if (lines[0].match(re2))
			lines[0] = lines[0].replace(
			    re2, '$&, '+'#'+articleId);
		    else {
			if (lines[0] > "") lines[0] = " "+lines[0];
			lines[0] = ">#"+articleId+lines[0];
		    }
		}
	    } else {		// Remove #xxxxx
		if (lines[0].match(/^>#[0-9 ,]+#/)) // 2 or more #id's
		    lines[0] = lines[0].replace(
			new RegExp("^>#"+articleId+"[ ,]*"), ">").replace(
			new RegExp("[ ,]*#"+articleId), "");
		else {
		    lines[0] = lines[0].replace(
			new RegExp(">#"+articleId+"[ ,]*"), "");
		}
	    }
	    lines[0] = lines[0].replace(/^> *$/, '');
	    textarea.value = lines.join("\n");
	}
    }
    function reverseChecks() {
	var names = collectElementsByAttr("input", "name", "usel");
	for (let u of names) {
	    u.checked = !u.checked;
	}
    }
    function initBlogs() {
	var el, check = collectElementsByAttr("input", "name", "notifyto");
	if (check)
	    for (let i of check) {
		i.addEventListener("click", insertRedirect, null);
	    }
	for (let i of document.getElementsByTagName("a"))
	    if (i.getAttribute("href").match(/^#[0-9]+$/))
		if (RegExp.lastMatch == i.innerHTML)
		    i.addEventListener("click", insertRedirect, null)
    }
    function initGrpAction() {
	var rev = document.getElementById("reverse");
	if (!rev) return;	// Is not grpAction page
	if (rev.tagName.match(/span/i)) {
	    rev.textContent = " 反転 ";
	    rev.addEventListener("click", reverseChecks, null);
	}
	var emailbtn = document.getElementById("email");
	emailbtn.addEventListener("click", function(ev){
	    // Enlarge box and Select user's checkbox
	    if (!ev.target.checked) return;
	    var x = collectElementsByAttr("div", "class", "foldtabs");
	    if (x && x[0] && x[0].style) {
		x[0].style.height = "10em";
	    }
	    let myuid = document.getElementById("myuid");
	    if (myuid) {
		let usel = collectElementsByAttr("input", "name", "usel");
		if (usel) {
		    for (u of usel) {
			if (u.value == myuid.value)
			    u.checked = true;
		    }
		}
	    }
	}, null);
	var teamsel = document.getElementById("selteam");
	if (teamsel) {
	    var usel, p, team;
	    // Select all members of the team
	    teamsel.addEventListener("change", function(ev) {
		var teamname = teamsel.value,
		    selected = new RegExp('(^| )'+teamname+"($|,)");
		usel = collectElementsByAttr("input", "name", "usel");
		if (!usel) return;
		for (u of usel) {
		    p = u.parentNode;		// should be label
		    if (!p) continue;
		    if (teamname == "TEAM") {	// Reset all checks
			u.checked = false;	// when "TEAM" is selected
		    } else {
			p = p.parentNode.parentNode;// should be tr
			team = nthChildOf(p, 3, "td")
			if (team && team.textContent
			    && team.textContent.match(selected)) {
			    u.checked = true;
			}
		    }
		}
	    }, null);
	}
    }
    function init() {
	initGrpAction();
	initBlogs();
    }
    document.addEventListener('DOMContentLoaded', init, null);
})();

yatex.org