diff s4-main.js @ 586:a03bfd5d242a

First trial of js interaction. Reply checkbox inserts/removes redirection mark.
author HIROSE Yuuji <yuuji@gentei.org>
date Fri, 16 Aug 2019 09:48:19 +0900
parents
children f480fa97fba6
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/s4-main.js	Fri Aug 16 09:48:19 2019 +0900
@@ -0,0 +1,62 @@
+(function (){
+    function collectElementsByNameAttr(elm, name) {
+	var e = document.getElementsByTagName(elm);
+	if (!e) return null;
+	var list = [];
+	for (var i of e) {
+	    if (i.getAttribute("name") == name)
+		list.push(i)
+	}
+	return list;
+    }
+    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])");
+	    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 init(){
+	var el, check = collectElementsByNameAttr("input", "notifyto");
+	if (check)
+	    for (var i of check) {
+		i.addEventListener("click", insertRedirect, null);
+	    }
+    }
+    document.addEventListener('DOMContentLoaded', init, null);
+})();

yatex.org