Mercurial > hgrepos > hgweb.cgi > s4
view s4-main.js @ 846:9c4e16c173db
Add support for small help on describing markdown
author | HIROSE Yuuji <yuuji@gentei.org> |
---|---|
date | Fri, 26 Jun 2020 19:55:14 +0900 |
parents | a6462eea48be |
children | a9e147e355fd |
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 helpMarkdown(e) { //alert(e.keyCode); if (e.keyCode == 13) { e.preventDefault(); var area = e.target; var pos = area.selectionStart, text = area.value; var last = text.lastIndexOf("\n", pos-1); var line = last ? text.substring(last+1) : text; var tail = text.substring(pos-2, pos); var add = "", offset = 0; if (line.startsWith("* ")) { add = (tail==" ") ? " " : "* "; } else if (line.match(/^([1-9][0-9]*)\. /)) { ln = parseInt(RegExp.$1); add = (tail==" ") ? " ".repeat(RegExp.$1.length+2) : (ln+1)+". "; } else if (line.match(/^\|( *).+\|/)) { add = "|" + RegExp.$1 + " |"; offset = -2; } area.value = text.substring(0, pos) + "\n" + add; //area.setSelectionRange(pos+length(add)); area.selectionStart=area.selectionEnd = (area.selectionStart + offset); } } /* Init event listeners */ function addFileInput() { var inpfile = collectElementsByAttr("input", "name", "image"); if (!inpfile) return; var filled = true; var i, ih; for (i of inpfile) { if (! i.value) filled=false; } if (filled) { ih = i.parentNode.innerHTML; if (ih) { var inpf = ih.substring(ih.indexOf("<input")), newi = "<br>"+inpf.substring(0, inpf.indexOf(">")+1); i.insertAdjacentHTML("afterend", newi) // alert(newi); } } } function initFileInput() { // Multiplies "input type=file" var el, morefile = document.getElementById("morefile"); if (morefile) { for (el of collectElementsByAttr("input", "name", "image")) { el.addEventListener("change", function(ev) { if (ev.target.value > "" && ev.target.files.length == 1) morefile.style.visibility = "visible"; // No need to hide again, sure? }); } morefile.addEventListener("click", addFileInput, null); } // When renaming, select basename part for (el of collectElementsByAttr("input", "class", "mv")) { el.addEventListener("focus", function(ev) { var i = ev.target; if (i) { i.setSelectionRange(0, i.value.lastIndexOf(".")); } }); } } function initTextarea() { var te = collectElementsByAttr("textarea", "name", "text"); if (!te || !te[0]) return; te[0].addEventListener("keydown", helpMarkdown, false); } function initBlogs() { // Auto-complete #xxxx var 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(); initFileInput(); initTextarea(); } document.addEventListener('DOMContentLoaded', init, null); })();