array2d.js 6.4 KB
Newer Older
J
Jason Park 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
var $table = null;

function Array2DTracer(module) {
    if (Tracer.call(this, module || Array2DTracer)) {
        initTable();
        return true;
    }
    return false;
}

Array2DTracer.prototype = Object.create(Tracer.prototype);
Array2DTracer.prototype.constructor = Array2DTracer;

// Override
Array2DTracer.prototype.resize = function () {
    Tracer.prototype.resize.call(this);

18
    this.refresh();
J
Jason Park 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
};

// Override
Array2DTracer.prototype.clear = function () {
    Tracer.prototype.clear.call(this);

    clearTableColor();
};

Array2DTracer.prototype.createRandomData = function (N, M, min, max) {
    if (!N) N = 10;
    if (!M) M = 10;
    if (min === undefined) min = 1;
    if (max === undefined) max = 9;
    var D = [];
    for (var i = 0; i < N; i++) {
        D.push([]);
        for (var j = 0; j < M; j++) {
            D[i].push((Math.random() * (max - min + 1) | 0) + min);
        }
    }
    return D;
};

// Override
Array2DTracer.prototype.setData = function (D) {
45
    this.D = D;
J
Jason Park 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
    if (Tracer.prototype.setData.call(this, arguments)) return true;

    $table.empty();
    for (var i = 0; i < D.length; i++) {
        var $row = $('<div class="mtbl-row">');
        $table.append($row);
        for (var j = 0; j < D[i].length; j++) {
            var $cell = $('<div class="mtbl-cell">').text(D[i][j]);
            $row.append($cell);
        }
    }
    this.resize();

    return false;
};

J
Jason Park 已提交
62 63 64 65 66 67
Array2DTracer.prototype._notify = function (x1, y1, x2, y2) {
    var second = x2 !== undefined && y2 !== undefined;
    this.pushStep({type: 'notifying', x: x1, y: y1, value: this.D[x1][y1]}, !second);
    if (second) this.pushStep({type: 'notifying', x: x2, y: y2, value: this.D[x2][y2]}, true);
    this.pushStep({type: 'notified', x: x1, y: y1}, false);
    if (second) this.pushStep({type: 'notified', x: x2, y: y2}, false);
J
Jason Park 已提交
68 69 70
};

Array2DTracer.prototype._select = function (sx, sy, ex, ey) {
71
    this.pushSelectingStep('select', null, arguments);
J
Jason Park 已提交
72 73 74
};

Array2DTracer.prototype._selectRow = function (x, sy, ey) {
75
    this.pushSelectingStep('select', 'row', arguments);
J
Jason Park 已提交
76 77 78
};

Array2DTracer.prototype._selectCol = function (y, sx, ex) {
79 80 81 82 83
    this.pushSelectingStep('select', 'col', arguments);
};

Array2DTracer.prototype._selectSet = function (coords) {
    this.pushSelectingStep('select', 'set', arguments);
J
Jason Park 已提交
84 85 86
};

Array2DTracer.prototype._deselect = function (sx, sy, ex, ey) {
87
    this.pushSelectingStep('deselect', null, arguments);
J
Jason Park 已提交
88 89 90
};

Array2DTracer.prototype._deselectRow = function (x, sy, ey) {
91
    this.pushSelectingStep('deselect', 'row', arguments);
J
Jason Park 已提交
92 93 94
};

Array2DTracer.prototype._deselectCol = function (y, sx, ex) {
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
    this.pushSelectingStep('deselect', 'col', arguments);
};

Array2DTracer.prototype._deselectSet = function (coords) {
    this.pushSelectingStep('deselect', 'set', arguments);
};

Array2DTracer.prototype.pushSelectingStep = function () {
    var args = Array.prototype.slice.call(arguments);
    var type = args.shift();
    var mode = args.shift();
    args = Array.prototype.slice.call(args.shift());
    var coord;
    switch (mode) {
        case 'row':
            coord = {x: args[0], sy: args[1], ey: args[2]};
            break;
        case 'col':
            coord = {y: args[0], sx: args[1], ex: args[2]};
            break;
        case 'set':
            coord = {coords: args[0]};
            break;
        default:
            if (args[2] === undefined && args[3] === undefined) {
                coord = {x: args[0], y: args[1]};
            } else {
                coord = {sx: args[0], sy: args[1], ex: args[2], ey: args[3]};
            }
    }
    var step = {type: type};
    $.extend(step, coord);
    this.pushStep(step, type == 'select');
J
Jason Park 已提交
128 129 130 131
};

Array2DTracer.prototype.processStep = function (step, options) {
    switch (step.type) {
132 133 134 135
        case 'notifying':
            var $row = $table.find('.mtbl-row').eq(step.x);
            $row.find('.mtbl-cell').eq(step.y).text(step.value);
        case 'notified':
J
Jason Park 已提交
136 137
        case 'select':
        case 'deselect':
138 139
            var colorClass = step.type == 'select' || step.type == 'deselect' ? tableColorClass.selected : tableColorClass.notifying;
            var addClass = step.type == 'select' || step.type == 'notifying';
J
Jason Park 已提交
140 141 142 143
            if (step.coords) {
                step.coords.forEach(function (coord) {
                    var x = coord.x;
                    var y = coord.y;
144
                    paintColor(x, y, x, y, colorClass, addClass);
J
Jason Park 已提交
145 146 147 148 149 150 151 152 153 154
                });
            } else {
                var sx = step.sx;
                var sy = step.sy;
                var ex = step.ex;
                var ey = step.ey;
                if (sx === undefined) sx = step.x;
                if (sy === undefined) sy = step.y;
                if (ex === undefined) ex = step.x;
                if (ey === undefined) ey = step.y;
155
                paintColor(sx, sy, ex, ey, colorClass, addClass);
J
Jason Park 已提交
156 157 158 159 160
            }
            break;
    }
};

161 162 163 164 165
// Override
Array2DTracer.prototype.refresh = function () {
    Tracer.prototype.refresh.call(this);

    var $parent = $table.parent();
J
add dp  
Jason Park 已提交
166 167 168 169 170 171 172 173 174 175 176 177
    var top = $parent.height() / 2 - $table.height() / 2;
    var left = $parent.width() / 2 - $table.width() / 2;
    if (top < 0) {
        $table.css('margin-top', 0);
        $parent.scrollTop(-top);
    }
    else $table.css('margin-top', top);
    if (left < 0) {
        $table.css('margin-left', 0);
        $parent.scrollLeft(-left);
    }
    else $table.css('margin-left', left);
178 179
};

J
Jason Park 已提交
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
// Override
Array2DTracer.prototype.prevStep = function () {
    this.clear();
    $('#tab_trace .wrapper').empty();
    var finalIndex = this.traceIndex - 1;
    if (finalIndex < 0) {
        this.traceIndex = -1;
        return;
    }
    for (var i = 0; i < finalIndex; i++) {
        this.step(i, {virtual: true});
    }
    this.step(finalIndex);
};

var initTable = function () {
    $('.module_container').empty();
    $table = $('<div class="mtbl-table">');
    $('.module_container').append($table);
};

201
var paintColor = function (sx, sy, ex, ey, colorClass, addClass) {
J
Jason Park 已提交
202 203 204
    for (var i = sx; i <= ex; i++) {
        var $row = $table.find('.mtbl-row').eq(i);
        for (var j = sy; j <= ey; j++) {
205 206 207
            var $cell = $row.find('.mtbl-cell').eq(j);
            if (addClass) $cell.addClass(colorClass);
            else $cell.removeClass(colorClass);
J
Jason Park 已提交
208 209 210 211 212 213
        }
    }
};

var clearTableColor = function () {
    $table.find('.mtbl-cell').css('background', '');
214 215 216 217 218
};

var tableColorClass = {
    selected: 'selected',
    notifying: 'notifying'
J
Jason Park 已提交
219
};