Rodhos Soft

備忘録を兼ねた技術的なメモです。Rofhos SoftではiOSアプリ開発を中心としてAndroid, Webサービス等の開発を承っております。まずはご相談下さい。

関数とコメント2

更に色々書いてみた。

/**
 *  @type{HTMLDivElement}
 */
const listBox = document.getElementById("listBox");


const toStr = (t) => {
    if (t == null) {
        return "null";
    } else {
        return t.toString();
    }
}

/**
 * 
 * @param {Node} node 
 * @returns {string}
 */
function nodeDesc(node) {
    const list = [node.nodeName,
    node.nodeType,
    node.nodeValue,
    node.hasChildNodes()];
    const listStrings = list.map(x => toStr(x));
    return listStrings.join(",");
}

/**
 * 
 * @param {string} txt 
 */
function log(txt) {
    console.log(txt);
}


/**
 * @param {Node} node 
 * @returns {(txt:string) => void}
 */
var logNode = (node) => (txt) => log(txt + nodeDesc(node));

/**
 * 
 * @param {Node} e 
 * @returns {HTMLDivElement}
 */
const div = (e) => {
    if (e.nodeName == "DIV") {
        return e;
    }
    return null;
};

/*
    nodes.find(x => x.nodeName == nodeName)
*/

/**
 * 
 * @param {Node} node 
 * @return {(Node) => Node}
 */
var findTag = (node) => (tagFunc) => (index) => {
    let t = node.firstChild;
    let count = 0;
    do {
        const x = tagFunc(t);
        if (x != null) {
            if (count == index) {
                return x;
            }
            count++;            
        }
        t = t.nextSibling;
    } while (t != null);

    return null;
}

var findDiv = (node) => (index) => {
    return findTag(node)(div)(index);
}


/// IDを一括変更する
var cloneNode = (node) => (index) => {
    const dc = d.cloneNode(true);
    changeID(dc, index);
    return dc;
};

/**
 * @param {Node} node 
 * @param {number} index 
 */
function changeID(node, index) {
    // console.log("node.id=",node.id);
    node.id = node.id +""+ index;
    // console.log(" => node.id=",node.id);
    for (let n of node.childNodes) {
        changeID(n,index);
    }
}


const id_cell = "cell";
const id_cell_title = "cell_title";
const id_cell_amount = "cell_amount";
const id_cell_icon = "cell_icon";


/**
 *  @type{HTMLDivElement}
 */
const d = document.getElementById(id_cell);

/**
 *  @type{HTMLDivElement}
 */
for (let i = 0; i < 10; i++) {
    /// 複製
    const dc = cloneNode(d)(i);
    dc.style.visibility = "visible";    

    /// 
    listBox.appendChild(dc);

    /**
     * @type{HTMLImageElement}
     */    
    const icon = document.getElementById(id_cell_icon+i);
    icon.src = "menu/icon_" + ('00'+i).slice(-2) + ".png";    

    const title = document.getElementById(id_cell_title+i);
    title.innerText = "hoge" + i;
    const amount = document.getElementById(id_cell_amount+i);
    amount.innerText = "" + (i * 100) + "Yen";

    // const found = findDiv(dc)(1);
    // if (found != null) {
    //     found.innerText = "hoge!";
    // }

}