Last active
August 29, 2015 14:07
-
-
Save oozzal/3ea0224f5fccd716ba9f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var QFX = {}; | |
QFX.Seat = function(row, column, weight, id, selected) { | |
this.row = row; | |
this.column = column; | |
this.weight = weight; | |
this.id = id; | |
this.selected = selected || false; | |
this.select = function() { | |
var cell = document.querySelector('#cell' + this.row + '' + this.column); | |
cell && (cell.style.background = 'red'); | |
this.selected = true; | |
return this; | |
}, | |
this.setWeight = function(weight) { | |
this.weight = weight; | |
var cell = document.querySelector('#cell' + this.row + '' + this.column); | |
if (cell) { | |
cell.style.background = 'rgb(' + (weight) + ',200,200)'; | |
cell.innerHTML = weight; | |
} | |
return this; | |
} | |
} | |
QFX.Hall = { | |
rows: 13, | |
cols: 18, | |
average: null, | |
seatMatrix: [], | |
seats: [], | |
avg: function() { | |
this.average = this.average || parseInt((this.rows + this.cols) / 2, 10); | |
return this.average; | |
}, | |
buildTable: function(rows, cols) { | |
this.rows = rows || this.rows; | |
this.cols = cols || this.cols; | |
var table = null, | |
tr = null, | |
td = null, | |
i = null, | |
j = null; | |
var seatMatrixRow = []; | |
table = document.createElement('table'); | |
for (i = 0; i < this.rows; i++) { | |
tr = table.insertRow(); | |
seatMatrixRow = []; | |
for (j = 0; j < this.cols; j++) { | |
var seat = new QFX.Seat(i, j, 0, i + j); | |
td = tr.insertCell(); | |
td.appendChild(document.createTextNode(i + j)); | |
td.appendChild(document.createElement('br')); | |
td.setAttribute('id', 'cell' + i + j); | |
seatMatrixRow.push(seat); | |
this.seats.push(seat); | |
} | |
this.seatMatrix.push(seatMatrixRow); | |
} | |
return table; | |
} | |
} | |
var body = document.body; | |
body.appendChild(QFX.Hall.buildTable()); | |
// Assign weights radially | |
var centerRow = parseInt(QFX.Hall.rows / 2) - 1; | |
var centerCol = parseInt(QFX.Hall.cols / 2); | |
var weight = 255; | |
var decFactor = 4; | |
var distance = 0; | |
var left, right, up, down = null; | |
for (var i = centerRow; i >= 0; i--) { | |
left = centerCol - distance; | |
up = centerRow - distance; | |
right = centerCol + distance; | |
down = centerRow + distance; | |
// up-down | |
for (var ur = up, dr = down, c = left; c <= right; c++) { | |
// up | |
QFX.Hall.seatMatrix[ur][c].setWeight(weight - distance * distance * distance); | |
// down | |
QFX.Hall.seatMatrix[dr][c].setWeight(weight - distance * distance * distance - 20); | |
} | |
// left-right | |
for (var r = up, lc = left, rc = right; r <= down; r++) { | |
// left | |
QFX.Hall.seatMatrix[r][lc].setWeight(weight - distance * distance * distance); | |
// right | |
QFX.Hall.seatMatrix[r][rc].setWeight(weight - distance * distance * distance); | |
} | |
distance += 1; | |
} | |
QFX.Hall.seatMatrix[centerRow][centerCol].select(); | |
// Sort the seats by their weight | |
QFX.Hall.seats.sort(function(a, b) { | |
return parseInt(b.weight - a.weight); | |
}); | |
/* | |
QFX Seat Matrix: | |
.seatRows | |
>.seatRow | |
>18 seats with some id | |
var QFX = {}; | |
QFX.seat = { id: 0, row: 0, column: 0, weight: 0 }; | |
var seatMatrix = []; | |
var seatMatrixRow = []; | |
$('.seatRow').each(function(i, seatRow) { | |
seatMatrixRow = []; | |
$(seatRow).find('[id]').each(function(j, seat) { | |
seatMatrixRow.push(seat.id); | |
}); | |
seatMatrix.push(seatMatrixRow); | |
}); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment