s4

changeset 906:9f237a8f550d

Add eventListener to newly added TR contents
author HIROSE Yuuji <yuuji@gentei.org>
date Mon, 04 Jan 2021 09:29:37 +0900
parents 5acef432b1de
children ce1a355e7cb1
files s4-main.js
diffstat 1 files changed, 16 insertions(+), 9 deletions(-) [+]
line diff
     1.1 --- a/s4-main.js	Sun Jan 03 14:44:56 2021 +0900
     1.2 +++ b/s4-main.js	Mon Jan 04 09:29:37 2021 +0900
     1.3 @@ -79,7 +79,6 @@
     1.4  	}
     1.5      }
     1.6      function registPjaxViewers(aHrefList) {
     1.7 -	// if (isOlderJS) return;
     1.8  	let apos=art_m_list.length;
     1.9  	for (let a of aHrefList) {
    1.10  	    let href = a.getAttribute("href");
    1.11 @@ -108,6 +107,12 @@
    1.12  	    }
    1.13  	}
    1.14      }
    1.15 +    function registInsertDirect(aHrefList) {
    1.16 +	for (i of aHrefList)
    1.17 +	    if (i.getAttribute("href").match(/^#[0-9]+$/))
    1.18 +		if (RegExp.lastMatch == i.innerHTML)
    1.19 +		    i.addEventListener("click", insertRedirect, false)
    1.20 +    }
    1.21      var ajaxSubmit;
    1.22      function replAddNews(newtable) {
    1.23  	let newids = [], idlist=[];
    1.24 @@ -128,6 +133,13 @@
    1.25  	current = document.querySelectorAll('td[class="repl"]');
    1.26  	let last=current[current.length-1],
    1.27  	    tbody = last.parentNode.parentNode;
    1.28 +	let addEventsToNewTr = function(tr) {
    1.29 +	    let td  = tr.getElementsByTagName("td"),
    1.30 +		td0 = td[0], td1 = td[1];
    1.31 +	    td0.classList.add("new");
    1.32 +	    registInsertDirect(td0.querySelectorAll("a[href]"));
    1.33 +	    registPjaxViewers(td1.querySelectorAll("a[href]"));
    1.34 +	}
    1.35  	// Now reconstruct articles with merge-sort like method
    1.36  	outer: for (; ncur<newids.length; ncur++) {
    1.37  	    n = newids[ncur];
    1.38 @@ -141,7 +153,7 @@
    1.39  		oid = getArticleID(o);
    1.40  		if (!oid || oid=="") continue;
    1.41  		if (oid >= nid) {
    1.42 -		    ntr.getElementsByTagName("td")[0].classList.add("new");
    1.43 +		    addEventsToNewTr(ntr);
    1.44  		    tbody.insertBefore(ntr, otr);
    1.45  		    if (oid==nid) otr.remove();
    1.46  		    cnt++;
    1.47 @@ -150,10 +162,8 @@
    1.48  	    }
    1.49  	    // Append absolutely new articles.
    1.50  	    ntr = n.parentNode;
    1.51 -	    ntr.getElementsByTagName("td")[0].classList.add("new");
    1.52 +	    addEventsToNewTr(ntr)
    1.53  	    tbody.appendChild(ntr);
    1.54 -	    ntr.scrollIntoView({behavior: "smooth"});
    1.55 -	    registPjaxViewers(ntr.querySelectorAll("a[href]"));
    1.56  	    ntr.classList.add("dissolving");
    1.57  	    let localntr = ntr;
    1.58  	    setTimeout(() => {
    1.59 @@ -598,10 +608,7 @@
    1.60  	    for (i of check) {
    1.61  		i.addEventListener("click", insertRedirect, false);
    1.62  	    }
    1.63 -	for (i of document.querySelectorAll("a[href]"))
    1.64 -	    if (i.getAttribute("href").match(/^#[0-9]+$/))
    1.65 -		if (RegExp.lastMatch == i.innerHTML)
    1.66 -		    i.addEventListener("click", insertRedirect, false)
    1.67 +	registInsertDirect(document.querySelectorAll("a[href]"));
    1.68  	for (i of document.querySelectorAll('input#c[value="送信"]')) {
    1.69  	    let b = document.createElement("button");
    1.70  	    b.textContent = "送信!";