Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Table everything #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions JS/helpers/calculator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*global console, Rent */

function Calculator(home) {
'use strict';
this.home = home;
this.calculateRents();

console.log(this.home.rooms);
}

Calculator.prototype.calculateRents = function () {
'use strict';
var room_id;
for (room_id in this.home.rooms) {
if (this.home.rooms.hasOwnProperty(room_id)) {
this.calculateRent(room_id);
}
}
};

Calculator.prototype.calculateRent = function (room_id) {
'use strict';
var room = this.home.rooms[room_id],
occupants = room.occupiedBy.length,
baseCost = this.home.commonShareCost * occupants,
roomCost = room.sqft * this.home.privateCostPerSqft * room.percentUsable,
price = baseCost + roomCost;

room.rent = new Rent(price, room_id, this.home);

this.applyFeesAndDiscounts(room);
if (this.home.roundDollar) {
room.rent.price += parseInt(room.rent.price, 10);
}
};

Calculator.prototype.applyFeesAndDiscounts = function (room) {
'use strict';
room.rent = (
room.hasWindow ?
parseFloat(this.home.goodLightingFee) : parseFloat(this.home.badLightingDeduction)
);
};
62 changes: 62 additions & 0 deletions JS/helpers/defaultRooms.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
var defaultRooms = {
"B-R1": {
"sqft": 169,
"hasWindow": true,
"percentUsable": 100,
"occupiedBy": ["Conrad"]
},
"B-R2": {
"sqft": 165,
"hasWindow": false,
"percentUsable": 100,
"occupiedBy": ["A. Kern", "Potluck"]
},
"B-R3": {
"sqft": 151,
"hasWindow": false,
"percentUsable": 80,
"occupiedBy": ["Renee"]
},
"B-R4": {
"sqft": 146,
"hasWindow": true,
"percentUsable": 100,
"occupiedBy": ["Hallie"]
},
"B-R5": {
"sqft": 146,
"hasWindow": true,
"percentUsable": 100,
"occupiedBy": ["Jack"]
},
"C-R1": {
"sqft": 191,
"hasWindow": true,
"percentUsable": 100,
"occupiedBy": ["Marco", "Alex C."]
},
"C-R2": {
"sqft": 191,
"hasWindow": true,
"percentUsable": 100,
"occupiedBy": ["Ashley", "Robin"]
},
"C-R3": {
"sqft": 178,
"hasWindow": false,
"percentUsable": 100,
"occupiedBy": ["Ari"]
},
"C-R4": {
"sqft": 166,
"hasWindow": true,
"percentUsable": 100,
"occupiedBy": ["Nick"]
},
"C-R5": {
"sqft": 167,
"hasWindow": true,
"percentUsable": 100,
"occupiedBy": ["Fouad"]
}
};
91 changes: 70 additions & 21 deletions JS/helpers/home.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*global rooms, console, moneyRound */
/*global rooms, console, defaultRooms, dataType, getPropertyName, getDisplayName, moneyRound, convertItem */

var defaults = {
attributeTableId: 'house-attribute-table',
Expand All @@ -16,9 +16,15 @@ var defaults = {
commonWeight: 0.60,
houseSize: 6200,
rentSum: 18000,
rooms: defaultRooms,
roundDollar: true
};

var EDITABLE_FIELDS = [
'calculateAsSingles', 'cheapestRoomMustBeSingle', 'commonWeight',
'rentSum', 'roundDollar'
];

function Home(options) {
'use strict';
this.rooms = {};
Expand All @@ -31,6 +37,7 @@ function Home(options) {
this.commonWeight = options.commonWeight || defaults.commonWeight;
this.houseSize = options.houseSize || defaults.houseSize;
this.rentSum = options.rentSum || defaults.rentSum;
this.rooms = options.rooms || defaults.rooms;
this.roundDollar = options.roundDollar || defaults.roundDollar;
this.feesAndDeducts = options.feesAndDeducts || defaults.feesAndDeducts;

Expand All @@ -51,10 +58,8 @@ function Home(options) {
}
}

this.populateAttributeTable(this.attributeTableId, this);
this.populateAttributeTable('fees-attribute-table', this.feesAndDeducts);

console.log(this);
this.populateAttributeTable(this);
this.populateAttributeTable(this.feesAndDeducts);
}

Home.prototype.setCheapestRoom = function (room) {
Expand Down Expand Up @@ -146,8 +151,8 @@ Home.prototype.setOpposingFeeOrDeduction = function (name, feeDuct) {
Home.prototype.fillHouse = function () {
'use strict';
/* TODO: check/grab rooms from structured screen before falling back on defaults */
if (rooms !== undefined) {
this.rooms = rooms;
if (this.rooms !== undefined) {
this.rooms = defaultRooms;
}
};

Expand All @@ -174,33 +179,77 @@ Home.prototype.calculateRoomSqftCosts = function () {
this.privateCostPerSqft = moneyRound(this.privateCostPerSqft);
};

Home.prototype.populateAttributeTable = function (tableId, attributes) {
Home.prototype.populateAttributeTable = function (attributes) {
'use strict';
var attributeTable = document.getElementById(tableId),
var attributeTable = document.getElementById(this.attributeTableId),
dontPrint = ['attributeTableId'],
row,
cell,
attribute,
subAttribute,
rowIndex = attributeTable.rows.length,
cellIndex;
function insertCell(row, index, item) {
cell = row.insertCell(index);
cell.innerHTML = item;
function createValInput(item) {
var input = document.createElement('INPUT');
input.setAttribute('type', 'text');
input.setAttribute('value', item);
return input;
}
function insertRow(table, index, cells) {
row = table.insertRow(index);
for (cellIndex = 0; cellIndex < cells.length; cellIndex += 1) {
insertCell(row, cellIndex, cells[cellIndex]);
function createValCheckbox(item) {
var input = document.createElement('INPUT');
input.setAttribute('type', 'checkbox');
input.checked = (item === 'true');
return input;
}
function createValCell(row, attrName, attrValue) {
var propertyName = attrName;
attrName = getDisplayName(attrName);
cell = row.insertCell(0);
cell.innerHTML = attrName;
cell = row.insertCell(1);

attrValue = convertItem(propertyName, attrValue);
if (EDITABLE_FIELDS.includes(propertyName)) {
if (dataType[propertyName].type === 'bool') {
attrValue = createValCheckbox(attrValue);
} else {
attrValue = createValInput(attrValue);
}
cell.appendChild(attrValue);
} else {
cell.innerHTML = attrValue;
}
}
console.log(tableId);
function insertRow(table, index, attrName, attrValue) {
row = table.insertRow(index);
createValCell(row, attrName, attrValue);
}

for (attribute in attributes) {
if (attributes.hasOwnProperty(attribute)) {
if (typeof attributes[attribute] !== 'object') {
console.log(attributeTable, tableId, attribute);
insertRow(attributeTable, rowIndex, [attribute, attributes[attribute]]);
if (!dontPrint.includes(attribute) && (typeof attributes[attribute] !== 'object')) {
insertRow(attributeTable, rowIndex, attribute, attributes[attribute]);
rowIndex += 1;
}
}
}
};
};

Home.prototype.getAttributesFromTable = function () {
'use strict';
var attributeTable = document.getElementById(this.attributeTableId),
table,
tr,
tdName,
tdContent;
console.log(attributeTable);
for (tr = 1; tr < attributeTable.rows.length; tr += 1) {
if (attributeTable.rows.hasOwnProperty(tr)) {
tdName = getPropertyName(attributeTable.rows[tr].children[0].textContent);
tdContent = getText(attributeTable.rows[tr].children[1]);
tdContent = parseItem(tdName, tdContent);
this[tdName] = tdContent;
}
}
console.log(this);
};
11 changes: 11 additions & 0 deletions JS/helpers/rent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function Rent(price, room_id, home) {
'use strict';
this.price = price;
this.room_id = room_id;
this.home = home;
}

Rent.prototype.getRoomDeets = function () {
'use strict';
return this.home.rooms[this.room_id];
};
92 changes: 31 additions & 61 deletions JS/helpers/rooms.js
Original file line number Diff line number Diff line change
@@ -1,62 +1,32 @@
var rooms = {
"B-R1": {
"sqft": 169,
"hasWindow": true,
"percentUsable": 1,
"occupiedBy": ["Conrad"]
},
"B-R2": {
"sqft": 165,
"hasWindow": false,
"percentUsable": 1,
"occupiedBy": ["A. Kern", "Potluck"]
},
"B-R3": {
"sqft": 151,
"hasWindow": false,
"percentUsable": 0.8,
"occupiedBy": ["Renee"]
},
"B-R4": {
"sqft": 146,
"hasWindow": true,
"percentUsable": 1,
"occupiedBy": ["Hallie"]
},
"B-R5": {
"sqft": 146,
"hasWindow": true,
"percentUsable": 1,
"occupiedBy": ["Jack"]
},
"C-R1": {
"sqft": 191,
"hasWindow": true,
"percentUsable": 1,
"occupiedBy": ["Marco", "Alex C."]
},
"C-R2": {
"sqft": 191,
"hasWindow": true,
"percentUsable": 1,
"occupiedBy": ["Ashley", "Robin"]
},
"C-R3": {
"sqft": 178,
"hasWindow": false,
"percentUsable": 1,
"occupiedBy": ["Ari"]
},
"C-R4": {
"sqft": 166,
"hasWindow": true,
"percentUsable": 1,
"occupiedBy": ["Nick"]
},
"C-R5": {
"sqft": 167,
"hasWindow": true,
"percentUsable": 1,
"occupiedBy": ["Fouad"]
/*global console, getPropertyName, getText, parseItem */

function createRoomsFromDefaults() {
'use strict';
}

function getRoomsFromTables() {
'use strict';
var tables = document.getElementsByClassName('room'),
table,
tr,
tdName,
tdContent,
rooms = {},
roomId;
for (table in tables) {
if (tables.hasOwnProperty(table)) {
roomId = tables[table].rows[0].textContent;
rooms[roomId] = {};
for (tr = 1; tr < tables[table].rows.length; tr += 1) {
if (tables[table].rows.hasOwnProperty(tr)) {
tdName = getPropertyName(tables[table].rows[tr].children[0].textContent);
tdContent = getText(tables[table].rows[tr].children[1]);
tdContent = parseItem(tdName, tdContent);
rooms[roomId][tdName] = tdContent;
}
}
}
}
};
console.log(rooms);
return rooms;
}
Loading