script.js 9.4 KB
Newer Older
J
Jason Park 已提交
1 2
$.ajaxSetup({cache: false, dataType: "text"});

J
Jason Park 已提交
3 4 5 6 7 8 9 10 11
$(document).on('click', 'a', function (e) {
    e.preventDefault();

    var win = window.open($(this).attr('href'), '_blank');
    if (!win) {
        alert('Please allow popups for this site');
    }
});

J
Jason Park 已提交
12
var $module_container = $('.module_container');
J
Jason Park 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25
var _tracer = new Tracer();
var initEditor = function (id) {
    var editor = ace.edit(id);
    editor.setTheme("ace/theme/tomorrow_night_eighties");
    editor.session.setMode("ace/mode/javascript");
    editor.$blockScrolling = Infinity;
    return editor;
};
var dataEditor = initEditor('data');
var codeEditor = initEditor('code');
dataEditor.on('change', function () {
    try {
        eval(dataEditor.getValue());
J
Jason Park 已提交
26
        lastModule = tracer && tracer.module;
J
Jason Park 已提交
27 28 29 30 31 32 33
        _tracer = tracer;
    } catch (err) {
    }
    _tracer.reset();
});

var loadFile = function (category, algorithm, file, explanation) {
J
tree  
Jason Park 已提交
34
    lastData = null;
J
Jason Park 已提交
35
    $('#explanation').html(explanation);
J
tree  
Jason Park 已提交
36
    dataEditor.setValue('');
J
Jason Park 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
    codeEditor.setValue('');

    var dir = './algorithm/' + category + '/' + algorithm + '/' + file + '/';
    $.get(dir + 'data.js', function (data) {
        dataEditor.setValue(data, -1);

        $.get(dir + 'code.js', function (data) {
            codeEditor.setValue(data, -1);
        });
    }).fail(function (jqXHR, textStatus, errorThrown) {
        alert("AJAX call failed: " + textStatus + ", " + errorThrown);
    });
};
var loadAlgorithm = function (category, algorithm) {
    $('#list > button').removeClass('active');
    $('[data-category="' + category + '"][data-algorithm="' + algorithm + '"]').addClass('active');
J
Jason Park 已提交
53
    $('#btn_desc').click();
J
Jason Park 已提交
54 55 56

    $('#category').text(list[category].name);
    $('#algorithm, #desc_title').text(list[category].list[algorithm]);
J
Jason Park 已提交
57
    $('#tab_desc > .wrapper').empty();
J
Jason Park 已提交
58 59
    $('.files_bar').empty();
    $('#explanation').html('');
J
tree  
Jason Park 已提交
60
    dataEditor.setValue('');
J
Jason Park 已提交
61 62 63
    codeEditor.setValue('');

    var dir = './algorithm/' + category + '/' + algorithm + '/';
J
Jason Park 已提交
64
    $.getJSON(dir + 'desc.json', function (data) {
J
Jason Park 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
        var files = data.files;
        delete data.files;

        var $container = $('#tab_desc > .wrapper');
        $container.empty();
        for (var key in data) {
            if (key) $container.append($('<h3>').html(key));
            var value = data[key];
            if (typeof value === "string") {
                $container.append($('<p>').html(value));
            } else if (Array.isArray(value)) {
                var $ul = $('<ul>');
                $container.append($ul);
                value.forEach(function (li) {
                    $ul.append($('<li>').html(li));
                });
            } else if (typeof value === "object") {
                var $ul = $('<ul>');
                $container.append($ul);
                for (var prop in value) {
                    $ul.append($('<li>').append($('<strong>').html(prop)).append(' ' + value[prop]));
                }
            }
        }

J
Jason Park 已提交
90 91
        $('.files_bar').empty();
        var init = false;
J
Jason Park 已提交
92
        for (var file in files) {
J
Jason Park 已提交
93 94 95 96 97 98 99 100 101 102 103
            (function (file, explanation) {
                var $file = $('<button>').append(file).click(function () {
                    loadFile(category, algorithm, file, explanation);
                    $('.files_bar > button').removeClass('active');
                    $(this).addClass('active');
                });
                $('.files_bar').append($file);
                if (!init) {
                    init = true;
                    $file.click();
                }
J
Jason Park 已提交
104
            })(file, files[file]);
J
Jason Park 已提交
105 106 107 108 109 110 111 112 113
        }
    });
};
var list = {};
$.getJSON('./algorithm/category.json', function (data) {
    list = data;
    var init = false;
    for (var category in list) {
        (function (category) {
J
Jason Park 已提交
114
            var $category = $('<button class="category">').append(list[category].name);
J
Jason Park 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
            $('#list').append($category);
            var subList = list[category].list;
            for (var algorithm in subList) {
                (function (category, subList, algorithm) {
                    var $algorithm = $('<button class="indent">')
                        .append(subList[algorithm])
                        .attr('data-algorithm', algorithm)
                        .attr('data-category', category)
                        .click(function () {
                            loadAlgorithm(category, algorithm);
                        });
                    $('#list').append($algorithm);
                    if (!init) {
                        init = true;
                        $algorithm.click();
                    }
                })(category, subList, algorithm);
            }
        })(category);
    }
});

var sidemenu_percent;
$('#navigation').click(function () {
    var $sidemenu = $('.sidemenu');
    var $workspace = $('.workspace');
    $sidemenu.toggleClass('active');
    $('.nav-dropdown').toggleClass('fa-caret-down fa-caret-up');
    if ($sidemenu.hasClass('active')) {
        $sidemenu.css('right', (100 - sidemenu_percent) + '%');
        $workspace.css('left', sidemenu_percent + '%');
    } else {
        sidemenu_percent = $workspace.position().left / $('body').width() * 100;
        $sidemenu.css('right', 0);
        $workspace.css('left', 0);
    }
    _tracer.resize();
});

$('#btn_run').click(function () {
    try {
        eval(dataEditor.getValue());
J
Jason Park 已提交
157
        lastModule = tracer && tracer.module;
J
Jason Park 已提交
158 159 160 161 162
        _tracer = tracer;
        _tracer.reset();
        eval(codeEditor.getValue());
        _tracer.visualize();
    } catch (err) {
163
        console.error(err);
J
Jason Park 已提交
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
        var $toast = $('<div class="toast error">').append(err);
        $('.toast_container').append($toast);
        setTimeout(function () {
            $toast.fadeOut(function () {
                $toast.remove();
            });
        }, 3000);
    }
});
$('#btn_pause').click(function () {
    if (_tracer.isPause()) {
        _tracer.resumeStep();
    } else {
        _tracer.pauseStep();
    }
});
$('#btn_prev').click(function () {
    _tracer.pauseStep();
    _tracer.prevStep();
});
$('#btn_next').click(function () {
    _tracer.pauseStep();
    _tracer.nextStep();
});

J
Jason Park 已提交
189
$('#btn_desc').click(function () {
J
Jason Park 已提交
190
    $('.tab_container > .tab').removeClass('active');
J
Jason Park 已提交
191
    $('#tab_desc').addClass('active');
J
Jason Park 已提交
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
    $('.tab_bar > button').removeClass('active');
    $(this).addClass('active');
});
$('#btn_trace').click(function () {
    $('.tab_container > .tab').removeClass('active');
    $('#tab_trace').addClass('active');
    $('.tab_bar > button').removeClass('active');
    $(this).addClass('active');
});

$(window).resize(_tracer.resize);

var dividers = [
    ['v', $('.sidemenu'), $('.workspace')],
    ['v', $('.viewer_container'), $('.editor_container')],
J
Jason Park 已提交
207
    ['h', $('.module_container'), $('.tab_container')],
J
Jason Park 已提交
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
    ['h', $('.data_container'), $('.code_container')]
];
for (var i = 0; i < dividers.length; i++) {
    var divider = dividers[i];
    (function (divider) {
        var vertical = divider[0] == 'v';
        var $first = divider[1];
        var $second = divider[2];
        var $parent = $first.parent();
        var thickness = 5;

        var $divider = $('<div class="divider">');
        if (vertical) {
            $divider.addClass('vertical');
            var _left = -thickness / 2;
            $divider.css({
                top: 0,
                bottom: 0,
                left: _left,
                width: thickness
            });
            var x, dragging = false;
            $divider.mousedown(function (e) {
                x = e.pageX;
                dragging = true;
            });
            $(document).mousemove(function (e) {
                if (dragging) {
                    var new_left = $second.position().left + e.pageX - x;
                    var percent = new_left / $parent.width() * 100;
                    percent = Math.min(90, Math.max(10, percent));
                    $first.css('right', (100 - percent) + '%');
                    $second.css('left', percent + '%');
                    x = e.pageX;
                    _tracer.resize();
                }
            });
            $(document).mouseup(function (e) {
                dragging = false;
            });
        } else {
            $divider.addClass('horizontal');
            var _top = -thickness / 2;
            $divider.css({
                top: _top,
                height: thickness,
                left: 0,
                right: 0
            });
            var y, dragging = false;
            $divider.mousedown(function (e) {
                y = e.pageY;
                dragging = true;
            });
            $(document).mousemove(function (e) {
                if (dragging) {
                    var new_top = $second.position().top + e.pageY - y;
                    var percent = new_top / $parent.height() * 100;
                    percent = Math.min(90, Math.max(10, percent));
                    $first.css('bottom', (100 - percent) + '%');
                    $second.css('top', percent + '%');
                    y = e.pageY;
                    _tracer.resize();
                }
            });
            $(document).mouseup(function (e) {
                dragging = false;
            });
        }

        $second.append($divider);
    })(divider);
J
Jason Park 已提交
280 281 282 283 284 285 286 287 288 289 290 291 292 293
}

$module_container.mousedown(function (e) {
    _tracer.mousedown(e);
});
$module_container.mousemove(function (e) {
    _tracer.mousemove(e);
});
$(document).mouseup(function (e) {
    _tracer.mouseup(e);
});
$module_container.bind('DOMMouseScroll mousewheel', function (e) {
    _tracer.mousewheel(e);
});