array2d.js 3.8 KB
Newer Older
K
Kevin Nadro 已提交
1 2 3 4 5
'use strict';

const modules = require('../module');
const util = require('./util');

K
Kevin Nadro 已提交
6 7 8 9 10

const getTracerName = () =>{
    return document.getElementById("tracerName-2D").value;
}

K
Kevin Nadro 已提交
11
const getNumRows = () => {
K
Kevin Nadro 已提交
12
    var row_field = document.getElementById('numRows-2D');
K
Kevin Nadro 已提交
13 14 15 16
    return row_field.value;
}

const getNumColumns = () => {
K
Kevin Nadro 已提交
17
    var column_field = document.getElementById('numColumns-2D');
K
Kevin Nadro 已提交
18 19 20 21
    return column_field.value;
}

const fauxData = (r, c) => {
K
Kevin Nadro 已提交
22
    var D = [];
K
Kevin Nadro 已提交
23
    for (var i = 0; i < r; i++) {
K
Kevin Nadro 已提交
24 25 26 27
      D.push([]);
      for (var j = 0; j < c; j++) {
        D[i].push(Math.floor(Math.random()* 10 + 1));
      }
K
Kevin Nadro 已提交
28
    }
K
Kevin Nadro 已提交
29
    return D;
K
Kevin Nadro 已提交
30 31
}

K
Kevin Nadro 已提交
32
const tableToInputFields = (numRows, numColumns) => {
K
Kevin Nadro 已提交
33 34 35 36 37
    var table = document.querySelector('.mtbl-table');

    for(var i = 0; i < numRows; i++){
        for(var j = 0; j < numColumns; j++){
            var elem = document.createElement('input');
K
Kevin Nadro 已提交
38
            elem.type = 'Text';
K
Kevin Nadro 已提交
39 40 41 42 43 44 45 46
            elem.value = Math.floor(Math.random() * 10 + 1);
            elem.classList.add('mtbl-col','inputField');
            table.childNodes[i].childNodes[j].innerHTML = '';
            table.childNodes[i].childNodes[j].appendChild(elem);
        }
    }
}

K
Kevin Nadro 已提交
47
const generateJS = (logger, tracer, tracerName) => {
K
Kevin Nadro 已提交
48 49
    if(!logger) return;

K
Kevin Nadro 已提交
50 51 52 53 54 55 56 57 58
    logger.clear();
    var table = document.querySelector('.mtbl-table');

    var numRows = table.childNodes.length;
    var numColumns = table.childNodes[0].childNodes.length;

    logger.print('Copy and paste this code in your data.js file!');
    logger.print('');

K
Kevin Nadro 已提交
59 60 61
    if(numRows > 1) {
        logger.print('let myTable = [');
    }
K
Kevin Nadro 已提交
62

K
Kevin Nadro 已提交
63
    var line = 'let myTable = [';
K
Kevin Nadro 已提交
64 65 66 67
    var i;
    var j;
    var comma = ',';
    for(i = 0; i < numRows; i++){
K
Kevin Nadro 已提交
68 69 70
        if(numRows > 1){
            line = '[';
        }
K
Kevin Nadro 已提交
71 72 73 74 75 76 77
        for(j = 0; j < numColumns-1; j++){
            line += table.childNodes[i].childNodes[j].childNodes[0].value + ',';
        }
        if(i === numRows - 1){comma = '';}
        line += table.childNodes[i].childNodes[j++].childNodes[0].value + ']' + comma;
        logger.print(line);
    }
K
Kevin Nadro 已提交
78 79 80
    if(numRows > 1){
        logger.print(']');
    }
K
Kevin Nadro 已提交
81 82


K
Kevin Nadro 已提交
83
    logger.print("let myTableTracer = new "+ tracer +" ('"+tracerName+"')");
K
Kevin Nadro 已提交
84
    logger.print('myTableTracer._setData (myTable)');
K
Kevin Nadro 已提交
85 86

    util.enabledHightlighting();
K
Kevin Nadro 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
}

const mousescroll = (e) =>{
    var colmElem = document.querySelector('.mtbl-col');
    var delta = (e.wheelDelta !== undefined && e.wheelDelta) ||
      (e.detail !== undefined && -e.detail);

    var inputFields = document.getElementsByClassName("inputField");
    for (var i = 0; i < inputFields.length; i++) {
        inputFields[i].style.width = (parseFloat(colmElem.style.fontSize) * 2.5) + "px";
     }

}

const setup = () => {
    var button_2DMatrix = document.getElementById("button-2DMatrix");
    var logger;
    var arr2DTracer;
    button_2DMatrix.addEventListener('click',function(){
        util.clearModules();
        arr2DTracer = new modules.Array2DTracer();
        var arrElem = document.querySelector('.module_wrapper');
        arrElem.addEventListener("mousewheel", mousescroll, false);
        arrElem.addEventListener("DOMMouseScroll", mousescroll, false);
        logger = new modules.LogTracer('Generated Javascript');

        var numRows = getNumRows();
        var numColumns = getNumColumns();
        var data = fauxData(numRows, numColumns);

        arr2DTracer.setData(data);
K
Kevin Nadro 已提交
118
        tableToInputFields(numRows, numColumns);
K
Kevin Nadro 已提交
119 120 121
        util.positionModules();
        arr2DTracer.refresh();
    },false);
K
Kevin Nadro 已提交
122
    var button_JS = document.getElementById('button-generateJS-2D');
K
Kevin Nadro 已提交
123
    button_JS.addEventListener('click',function(){
K
Kevin Nadro 已提交
124
        generateJS(logger, 'Array2DTracer', getTracerName());
K
Kevin Nadro 已提交
125 126 127 128
    },false);
}

module.exports = {
K
Kevin Nadro 已提交
129 130 131 132 133
    setup,
    mousescroll,
    fauxData,
    tableToInputFields,
    generateJS
K
Kevin Nadro 已提交
134
};