-cases.js 5.7 KB
Newer Older
S
sushuang 已提交
1 2 3 4 5 6 7 8 9 10
(function () {

    var testHelper = window.testHelper;
    var encodeHTML = testHelper.encodeHTML;
    var resolve = testHelper.resolve;

    var SELECTOR_CASES_LIST_CONTAINER = '.cases-list ul';
    var SELECTOR_CASES_ITEM = 'li a';
    var SELECTOR_CONTENT_IFRAME = '.page-content iframe';
    var SELECTOR_RENDERER = '.renderer-selector input';
S
sushuang 已提交
11 12 13
    var SELECTOR_CURRENT = '.info-panel .current';

    var pagePaths;
S
sushuang 已提交
14 15 16 17 18

    run();

    function run() {
        // Init list
S
sushuang 已提交
19
        var url = dir() + '/';
S
sushuang 已提交
20
        $.ajax({
S
sushuang 已提交
21
            url: url
S
sushuang 已提交
22 23
        }).then(
            function (content) {
S
sushuang 已提交
24 25 26 27 28 29 30 31
                pagePaths = fetchPagePaths(content);
                if (pagePaths.length) {
                    renderList();
                    reset();
                }
                else {
                    renderFailInfo(url);
                }
S
sushuang 已提交
32 33 34 35 36
            },
            function () {
                renderFailInfo(url);
            }
        );
S
sushuang 已提交
37 38 39 40 41 42 43 44 45

        $(window).on('hashchange', function () {
            reset();
        });

        $(SELECTOR_RENDERER).on('click', function (e) {
            changeRenderer(e.target.value);
        });

S
sushuang 已提交
46 47 48 49 50 51 52
        $(SELECTOR_CURRENT).on('mouseover', function (e) {
            setPageHintFull(getPagePathFromPageURL(getCurrentPageURL()));
            this.select();
        });
        $(SELECTOR_CURRENT).on('mouseout', function (e) {
            setPageHintShort(getPagePathFromPageURL(getCurrentPageURL()));
        });
S
sushuang 已提交
53 54
    }

S
sushuang 已提交
55 56 57 58 59 60
    function renderFailInfo(url) {
        url = encodeHTML(url);
        document.body.innerHTML = 'Error: This page requires a server that is able to list files when visiting'
            + ' <a target="_blank" href="' + url + '">' + url + '</a>.';
    }

S
sushuang 已提交
61 62 63 64 65 66
    function reset() {
        var pageURL = getCurrentPageURL();
        resetRendererSelector(pageURL);
        enterPage(pageURL, true); // Init page from hash if exists
    }

S
sushuang 已提交
67
    function renderList() {
S
sushuang 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
        var html = [];

        for (var i = 0; i < pagePaths.length; i++) {
            var path = pagePaths[i];
            html.push('<li><a href="' + encodeHTML(path) + '">' + encodeHTML(path) + '</a></li>');
        }

        var caseListContainer = $(SELECTOR_CASES_LIST_CONTAINER);

        caseListContainer[0].innerHTML = html.join('');

        caseListContainer.on('click', SELECTOR_CASES_ITEM, function (e) {
            enterPage(makePageURL(
                e.currentTarget.innerHTML, getCurrentRenderer()
            ));
            return false;
        });
    }

    function getCurrentPageURL() {
        return decodeURIComponent(
            (location.hash || '').replace(/^#/, '')
        );
    }

    function getCurrentRenderer(pagePath, renderer) {
        var renderer;
        $(SELECTOR_RENDERER).each(function (index, el) {
            if (el.checked) {
                renderer = el.value;
            }
        });
        return renderer;
    }

    function changeRenderer(renderer) {
        var pageURL = getCurrentPageURL();
        if (pageURL) {
            enterPage(replaceRendererOnPageURL(pageURL, renderer));
        }
    }

    function makePageURL(pagePath, renderer) {
S
tweak  
sushuang 已提交
111
        return pagePath + '?__RENDERER__=' + renderer;
S
sushuang 已提交
112 113 114 115 116 117 118 119 120
    }

    function enterPage(pageURL, dontUpdateHash) {
        if (!pageURL) {
            return;
        }
        if (!dontUpdateHash) {
            location.hash = '#' + encodeURIComponent(pageURL);
        }
S
sushuang 已提交
121 122 123 124 125 126 127 128 129

        var pagePathInfo = getPagePathFromPageURL(pageURL);

        setPageHintShort(pagePathInfo);

        $(SELECTOR_CASES_LIST_CONTAINER + ' li').each(function (index, el) {
            el.style.background = pagePathInfo.index === index ? 'rgb(170, 224, 245)' : 'none';
        });

S
sushuang 已提交
130 131 132 133
        var contentIframe = $(SELECTOR_CONTENT_IFRAME);
        contentIframe.attr('src', pageURL);
    }

S
sushuang 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
    function setPageHintShort(pagePathInfo) {
        $(SELECTOR_CURRENT).val(pagePathInfo
            ? (pagePathInfo.index != null ? (pagePathInfo.index + 1) + '. ' : '')
                + (pagePathInfo.pagePath || '')
            : ''
        );
    }

    function setPageHintFull(pagePathInfo) {
        $(SELECTOR_CURRENT).val(pagePathInfo
            ? dir() + '/' + pagePathInfo.pagePath
            : ''
        );
    }

S
sushuang 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
    function dir() {
        return location.origin + resolve(location.pathname, '..');
    }

    function fetchPagePaths(content) {
        var pageList = [];

        singleFetch(/"([^"]+\.html)\s*"/g);
        singleFetch(/'([^']+\.html)\s*'/g);

        function singleFetch(pattern) {
            var result;
            while ((result = pattern.exec(content)) != null) {
                pageList.push(result[1]);
            }
        }

        return pageList;
    }

    function resetRendererSelector(pageURL) {
        var renderer = getRendererFromPageURL(pageURL) || 'canvas';

        $(SELECTOR_RENDERER).each(function (index, el) {
            el.checked = el.value === renderer;
        });
    }

    function getRendererFromPageURL(pageURL) {
        if (pageURL) {
S
tweak  
sushuang 已提交
179
            var matchResult = pageURL.match(/[?&]__RENDERER__=(canvas|svg)(&|$)/);
S
sushuang 已提交
180 181 182 183
            return matchResult && matchResult[1];
        }
    }

S
sushuang 已提交
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
    function getPagePathFromPageURL(pageURL) {
        if (pageURL) {
            var matchResult = pageURL.match(/^[^?&]*/);
            var pagePath = matchResult && matchResult[0];
            if (pagePath) {
                var index;
                for (var i = 0; i < pagePaths.length; i++) {
                    if (pagePaths[i] === pagePath) {
                        index = i;
                    }
                }
                return {index: index, pagePath: pagePath};
            }
        }
    }

S
sushuang 已提交
200
    function replaceRendererOnPageURL(pageURL, renderer) {
S
tweak  
sushuang 已提交
201
        return pageURL.replace(/([?&]__RENDERER__=)([^&]*)(&|$)/, '$1' + renderer + '$3');
S
sushuang 已提交
202 203 204
    }

})();