-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolar.js
163 lines (141 loc) · 5.58 KB
/
solar.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
var slider = document.getElementById("myRange");
var output = document.getElementById("battery");
var paneloutput = document.getElementById("panelout");
var altOutput = document.getElementById("alternator");
var altSlider = document.getElementById("myAlternator");
var solarOutput = document.getElementById("solarP");
var solarSlider = document.getElementById("mySolarPanel");
var windOutput = document.getElementById("windG");
var windSlider = document.getElementById("myWindGen");
var consumersUWTot = document.getElementById("consumersUWTotal");
var consumersUp = document.getElementById("consumersUpdate");
var addRow = document.getElementById("rowAdd")
output.innerHTML = slider.value; // Display the default slider value
paneloutput.innerHTML = 0;
altOutput.innerHTML = myAlternator.value;
solarOutput.innerHTML = mySolarPanel.value;
windOutput.innerHTML = myWindGen.value;
// Update the current slider value (each time you drag the slider handle)
slider.oninput = function () {
output.innerHTML = this.value;
capacityTotalBatt.innerHTML = (this.value);
capacityUsable.innerHTML = (this.value * 0.3).toFixed(1);
paneloutput.innerHTML = (this.value * 0.8).toFixed(1);
}
altSlider.oninput = function () {
altOutput.innerHTML = this.value;
}
solarSlider.oninput = function () {
solarOutput.innerHTML = this.value;
}
windSlider.oninput = function () {
windOutput.innerHTML = this.value;
}
// add the total current consumed.
consumersUp.onclick = function () {
var arr = document.getElementsByName('consumer');
var arrUway = document.getElementsByName("hoursUWay");
var arrAnc = document.getElementsByName("hoursAnchored");
var rowTot = document.getElementsByName("uWayTotalA")
var tot = 0;
for (var i = 0; i < arr.length; i++) {
// console.log((arr[i].value), (arrUway[i].value));
if (parseFloat(arr[i].value) && (parseFloat(arrUway[i].value) > 0)) {// and corresponding hours is > 0 then do arr[1] * qty * hrs
tot += ((parseFloat(arr[i].value)) * (parseFloat(arrUway[i].value)));
// alert(tot)
rowTot[i].innerHTML = ((parseFloat(arr[i].value)) * (parseFloat(arrUway[i].value))).toFixed(1);
}
}
tot = tot.toFixed(2);
consumersUWTot.innerHTML = tot;
}
// dynamically add and remove rows
// add a delete button to added rows only (don't want a delete button on the first row)
function addRows() {
var table = document.getElementById('dataTable');
var rowCount = table.rows.length;
var cellCount = table.rows[0].cells.length;
var row = table.insertRow(rowCount);
for (var i = 0; i < cellCount; i++) {
var cell = 'cell' + i;
cell = row.insertCell(i);
var copycel = document.getElementById('col' + i).innerHTML;
if (i == cellCount - 1) {
var deleteButton = "<input type=\"button\" value=\"Delete\" onclick = \"deleterow(this)\">"
cell.innerHTML = deleteButton;
}
else {
cell.innerHTML = copycel;
}
}
}
// This is called by the delete button at the end of each added row
function deleterow(r) {
var i = r.parentNode.parentNode.rowIndex;
document.getElementById("dataTable").deleteRow(i);
}
// Fancy table from https://codepen.io/adam-lynch/pen/GaqgXP
const min = 150;
// The max (fr) values for grid-template-columns
const columnTypeToRatioMap = {
numeric: 1,
'text-short': 1.67,
'text-long': 3.33,
};
const table = document.querySelector('table');
/*
The following will soon be filled with column objects containing
the header element and their size value for grid-template-columns
*/
const columns = [];
let headerBeingResized;
// The next three functions are mouse event callbacks
// Where the magic happens. I.e. when they're actually resizing
const onMouseMove = (e) => requestAnimationFrame(() => {
console.log('onMouseMove');
// Calculate the desired width
horizontalScrollOffset = document.documentElement.scrollLeft;
const width = (horizontalScrollOffset + e.clientX) - headerBeingResized.offsetLeft;
// Update the column object with the new size value
const column = columns.find(({header}) => header === headerBeingResized);
column.size = Math.max(min, width) + 'px'; // Enforce our minimum
// For the other headers which don't have a set width, fix it to their computed width
columns.forEach((column) => {
if (column.size.startsWith('minmax')) { // isn't fixed yet (it would be a pixel value otherwise)
column.size = parseInt(column.header.clientWidth, 10) + 'px';
}
});
/*
Update the column sizes
Reminder: grid-template-columns sets the width for all columns in one value
*/
table.style.gridTemplateColumns = columns
.map(({header, size}) => size)
.join(' ');
});
// Clean up event listeners, classes, etc.
const onMouseUp = () => {
console.log('onMouseUp');
window.removeEventListener('mousemove', onMouseMove);
window.removeEventListener('mouseup', onMouseUp);
headerBeingResized.classList.remove('header--being-resized');
headerBeingResized = null;
};
// Get ready, they're about to resize
const initResize = ({target}) => {
console.log('initResize');
headerBeingResized = target.parentNode;
window.addEventListener('mousemove', onMouseMove);
window.addEventListener('mouseup', onMouseUp);
headerBeingResized.classList.add('header--being-resized');
};
// Let's populate that columns array and add listeners to the resize handles
document.querySelectorAll('th').forEach((header) => {
const max = columnTypeToRatioMap[header.dataset.type] + 'fr';
columns.push({
header,
// The initial size value for grid-template-columns:
size: `minmax(${min}px, ${max})`,
});
header.querySelector('.resize-handle').addEventListener('mousedown', initResize);
});