sourceafFancordion::ClassicSkin.fan

using concurrent

** A Fancordion Skin that mimics the style of the classic Java Concordion library.
class ClassicSkin : FancordionSkin {
    private  Int    buttonId    := 0
    private  Str    cmdElement  := "span"

    // ---- Setup / Tear Down -------------------

    @NoDoc
    override Void tearDown() {
        super.tearDown
        buttonId = 0
    }

    // ---- HTML Methods ------------------------

    @NoDoc
    override This head() {
        addCss(`fan://afFancordion/res/classicSkin/fancordion.css`.get)
        return (ClassicSkin) super.head
    }

    @NoDoc
    override This footer() {
        ver := Pod.of(this).version
        now := DateTime.now(1sec).toLocale("D MMM YYYY, k:mmaa zzzz 'Time'")
        dur := DateTime.now(null) - fixtureMeta.StartTime
        write("<footer>\n")
        write("""\tResults generated by <a href="http://www.fantomfactory.org/pods/afFancordion">Fancordion v${ver}</a>\n""")
        write("""\t<div class="testTime">in ${dur.toLocale} on ${now}</div>\n""")
        write("</footer>\n")
        return this
    }

    @NoDoc
    override This cmdErr(Str cmdUrl, Str cmdText, Err err) {
        addScript(`fan://afFancordion/res/classicSkin/visibility-toggler.js`.get)

        buttonId++

        stack := err.traceToStr.splitLines.join("") { "<span class=\"stackTraceEntry\">${it.toXml}</span>\n" }
        exp := """<del class="expected">${cmdText.toXml}</del>"""
        html :=
        """<span class="exceptionMessage">${err.typeof.name.toXml}: ${firstLine2(err.msg).toXml}</span>
           <input id="stackTraceButton${buttonId}" type="button" class="stackTraceButton" onclick="javascript:toggleStackTrace('${buttonId}')" value="View Stack" />
           <span class="stackTrace" id="stackTrace${buttonId}">
             <span>While evaluating command: <code>${cmdUrl}</code></span>
             <span class="stackTraceExceptionMessage">${err.typeof} : ${err.msg.toXml}</span>
             ${stack}
           </span>
           """
        if (inTable)
            html = """<td class="error">${exp}\n${html}</td>"""
        else
            html = """<span class="error">${exp}</span>\n${html}"""
        return write(html)
    }
    
    private Str firstLine2(Str? txt) {
        txt?.splitLines?.exclude { it.trim.isEmpty }?.first ?: Str.defVal
    }   
}