рассчет сетки
by Евгений
HTML
<div id=general>
<input type=number id=width placeholder="ширина поля" value=940 />
<input type=number id=mrgn placeholder="отступ" value=10 />
</div>
<div id=elements>
<input type=number class=widthEl placeholder="ширина поля" value=190 />
</div>
<div>
<button id=add>добавить элемент</button>
<button id=go>Рассчитать</button>
</div>
<div id=log></div>
<div>
<input type=number id=colCnt placeholder="количество колонок" value=14 />
<button id=colCalc>Рассчитать</button>
</div>
<a href="http://ianli.com/blueprinter/" target="_blanc">генератор сетки</a>
CSS
input, button {
width: 200px;
margin: 10px 10px;
padding: 6px;
box-sizing: border-box;
}
#log {
padding-bottom: 20px;
margin-bottom: 20px;
border-bottom: 1px solid #ccc;
}
JavaScript
(function (window, $, undefined) {
function log(txt) {
$('#log').innerHTML += txt + ' ';
}
function gcd(a, b) {
return b == 0 ? a : gcd(b, a%b);
}
function nok2(a, b) {
return a * b / gcd(a, b);
}
function nok(res) {
var start = res[0], current;
for (var i = 0; i < res.length; i++) {
current = res[i];
for (var key in current) {
if (current.hasOwnProperty(key)) {
if (! start[key] || start[key] < current[key]) {
start[key] = current[key];
}
}
}
}
var multi = 1;
for (var key in start) {
if (start.hasOwnProperty(key)) {
multi *= Math.pow(key, start[key]);
}
}
return multi;
}
function fraction(a, b) {
return [a / gcd(a, b), b / gcd(a, b)];
}
function addEl() {
var el = $('.widthEl');
var newElem = $('.widthEl').cloneNode(true)
$('#elements').appendChild(newElem);
}
function simples(n) {
var j = 1;
var i = 2;
var a = [];
do {
if (n % i == 0){
a[j] = i;
j++;
n = n / i;
} else {
i++;
}
} while (i < n);
a[j] = i;
var res = {};
a.forEach(function(e){
res[e] = 1 + ~~res[e];
})
return res;
}
function calc() {
var w = +$('#width').value;
var m = +$('#mrgn').value;
var els = document.querySelectorAll('.widthEl');
var elWs = [];
var res = [];
var res2 = [];
for (var i = 0; i < els.length; i++) {
if (els[i].value != 0) {
var fract = fraction((+els[i].value + m), (w + m));
// log(fract.join('/'));
res.push(simples(fract[1]));
...