-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlevel2.php
executable file
·486 lines (435 loc) · 14.1 KB
/
level2.php
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
<!--included once in edit.php-->
<style>
span[expanded]{transition:transform 0.15s;}
span[expanded='0']{transform:rotate(-90deg);}
</style>
<script>
//namespace to remember folding of questions (not saved to cookies)
var Expanded={
//"question":1,
}
</script>
<script>
var level2 = {};//namespace
level2.toggleQuestionVisibility=function(btn,question) {
var currentState=Expanded[question];
if(currentState===undefined)currentState=1;//expanded by default
//toggle html attribute
if(currentState) {btn.setAttribute('expanded','0')}
else {btn.setAttribute('expanded','1')}
//modify "Expanded" object
if(currentState) {Expanded[question]=0}
else {Expanded[question]=1}
//hide or show fields
var trs=document.querySelectorAll('tr[field][question='+question+']');
var newDisplay = currentState ? 'none':'';
for(var i=0;i<trs.length;i++)
{
trs[i].style.display=newDisplay;
}
}
/**
* Transform a <td> cell to a <input> to make modifications in the Global object
* @param {element} element - the <td> cell
*/
level2.transformField=function(element) {
element.removeAttribute('onclick');
element.innerHTML="";
var field=element.parentNode.getAttribute('field');
var input=document.createElement('input');
element.appendChild(input);
input.id=field;
input.classList.add('input');
input.autocomplete='off';
//value converted
var multiplier = Units.multiplier(field);
var currentValue = CurrentLevel[field]/multiplier;
input.value=currentValue;
input.onblur=function(){level2.updateField(field,input.value)};
input.onkeypress=function(event) {
if(event.which==13) input.onblur() //somehow creates an error but does not affect to anything
}
input.onkeydown=function(event) {
function updateChart()
{
//problem: this only updates if we are plotting inputs. WHY?
var newValue=parseFloat(input.value);
if(isNaN(newValue))newValue=0;
newValue*=Units.multiplier(field);
//only update real inputs
if(typeof(CurrentLevel[field])!="function")
{
CurrentLevel[field]=newValue;
if(substages.length==1) substages[0][field]=newValue;
}
//try to draw charts
drawCharts();
}
switch(event.which)
{
case 38: //up key
if(!event.shiftKey){input.value++;updateChart();}
break;
case 40: //down key
if(!event.shiftKey){input.value--;updateChart();}
break;
case 9: //TAB
setTimeout(function()
{
var el=document.querySelector('#inputs tr[field='+field+']').nextSibling.childNodes[2];
if(el && el.onclick){el.onclick();}
},100);
break;
}
}
input.select();
}
//draw a dropdown menu in inputs table
level2.createDropdown=function(field,table,question) {
question=question||false; //question it belongs to
var code=field;
var t=table;
//if is an option, continue (will show at the end of the table)
/*new row*/
var newRow=t.insertRow(-1);
newRow.setAttribute('field',code);
//question it belongs to
if(question) {
newRow.setAttribute('question',question)
if(Expanded[question]==0) {
newRow.style.display='none';
}
}
/*1st cell: show code*/
var newCell=document.createElement('th');
newRow.append(newCell);
newCell.classList.add('variableCode');
newCell.innerHTML=(function() {
return "<a style=font-size:10px href=variable.php?id="+code+">"+code+"</a>";
})();
/*2nd cell: variable name*/
var newCell=newRow.insertCell(-1);
newCell.style.textAlign="left";
newCell.setAttribute('title', translate(code+'_expla'));
newCell.innerHTML=(function() {
var warning=(Formulas.outputsPerInput(code).length==0 && Utils.usedInBenchmarks(code).length==0) ?
" <span class=not_used_input caption='Input not used for any equation'></span>" : "";
return translate(code+"_descr")+warning;
})();
//3rd cell: value
var newCell=newRow.insertCell(-1);
newCell.colSpan=2;
(function() {
var select=document.createElement('select');
newCell.appendChild(select)
select.setAttribute('onchange','level2.updateField("'+code+'",this.value)');
for(var op in Tables[code]) {
var option=document.createElement('option');
var value=parseInt(Tables[code][op].value);
select.appendChild(option);
option.value=value;
option.innerHTML="("+value+") "+op;
if(CurrentLevel[code]==value) { option.selected=true; }
}
})();
}
//draw an input in inputs table
level2.createInput=function(field,table,question) {
question=question||false; //code of the question it belongs to
//if dropdown, call create dropdown instead
if(Info[field] && Info[field].magnitude=="Option") {
level2.createDropdown(field,table,question);
return;
};
//bool for if current field is a calculated variable (CV)
var isCV = field.search(/^c_/)+1;
/*if is a function and not a cv, exit*/
if(typeof(CurrentLevel[field])!="number" && !isCV) return;
/*new row*/
var t=table;
var newRow=t.insertRow(-1);
//question it belongs
if(question)
{
newRow.setAttribute('question',question)
if(Expanded[question]==0)
{
newRow.style.display='none';
}
}
/*class*/
if(isCV) newRow.classList.add('isCV');
/*hlInputs for formula and show formula, only if CV*/
if(isCV) {
var formula = CurrentLevel[field].toString();
var prettyFormula = Formulas.prettify(formula);
newRow.setAttribute('onmouseover','Formulas.hlInputs("'+field+'",CurrentLevel,1)');
newRow.setAttribute('onmouseout', 'Formulas.hlInputs("'+field+'",CurrentLevel,0)');
newRow.setAttribute('title',prettyFormula);
}
else {
newRow.setAttribute('onmouseover','Formulas.hlOutputs("'+field+'",CurrentLevel,1)');
newRow.setAttribute('onmouseout', 'Formulas.hlOutputs("'+field+'",CurrentLevel,0)');
}
/*new attribute field=field>*/
newRow.setAttribute('field',field);
/*code*/
var newCell=document.createElement('th');
newRow.append(newCell);
newCell.classList.add('variableCode');
if(isCV)newCell.classList.add('isCV');
newCell.innerHTML=(function() {
//warning for variable not used
var code = "<a style=font-size:10px href=variable.php?id="+field+">"+field+"</a>";
return code;
})();
/*description*/
var newCell=newRow.insertCell(-1);
newCell.setAttribute('title', translate(field+"_expla"));
newCell.innerHTML=(function(){
var warning=(Formulas.outputsPerInput(field).length==0 && Utils.usedInBenchmarks(field).length==0) ?
" <span class=not_used_input caption='Input not used for any equation'></span>" : "";
return translate(field+"_descr")+warning;
})();
//editable cell if not CV
var newCell=newRow.insertCell(-1);
if(!isCV) {
if(typeof(substages)=="object" && substages.length > 1 && !(Level2only.list.indexOf(field)+1))
{
//this means you are in level 2 and you should NOT be able to modify inputs here
newCell.setAttribute('caption',translate("This value is the sum of all substages"));
newCell.classList.add('non-editable');
newCell.onclick=function()
{
//make sure that section is not folded
document.querySelector('#substageInputs_container').classList.remove('folded');
//navigate to substages to modify the input
var f=this.parentNode.getAttribute('field');
var sscon=document.querySelector('#substages').parentNode;
sscon.scrollIntoView();
setTimeout(function(){sscon.classList.remove('folded')},300);
setTimeout(function(){
document.querySelector('#substages tr[field='+f+'] td[substage]').onclick();
},600);
};
}
else //normal case: substages==1, user can modify level2
{
newCell.classList.add("input");
newCell.setAttribute('caption',"<?php write('#edit_click_to_modify')?>");
newCell.setAttribute('onclick','level2.transformField(this)');
}
}
else {
//field is calculated variable, so show formula
newCell.title=Formulas.prettify(CurrentLevel[field].toString());
newCell.style.textAlign="right";
}
/*value*/
newCell.innerHTML=(function() {
var value = isCV ? CurrentLevel[field]() : CurrentLevel[field];
value/=Units.multiplier(field);
value=format(value);
return value
})();
//check if this cv has estimated data
var ed=DQ.hasEstimatedData(field) ?
" <span title='<?php write('#variable_this_equation_contains_estimated_data')?>' class=estimated>⚠</span>"
: "";
newCell.innerHTML+=ed;
//unit
newRow.insertCell(-1).innerHTML=(function() {
if(!Info[field]) return "<span style=color:#ccc>no unit</span>";
if(Info[field].magnitude=="Currency") { return Global.General.Currency; }
if(isCV) {
return Info[field].unit;
}
else {
var str="<select onchange=Units.selectUnit('"+field+"',this.value)>";
if(Units[Info[field].magnitude]===undefined)
{
return Info[field].unit
}
var currentUnit = Global.Configuration.Units[field] || Info[field].unit
for(var unit in Units[Info[field].magnitude])
{
if(unit==currentUnit)
str+="<option selected>"+unit+"</option>";
else
str+="<option>"+unit+"</option>";
}
str+="</select>"
return str
}
})();
//data quality
/*
newRow.insertCell(-1).innerHTML=(function() {
if(isCV) { return "Calculated" }
else
{
var select=document.createElement('select');
select.setAttribute('onchange','DQ.update("'+field+'",this.value)');
['Actual','Estimated'].forEach(function(opt)
{
var option=document.createElement('option');
select.appendChild(option);
option.value=opt;
option.innerHTML=translate(opt);
if(Global.Configuration.DataQuality[field]==opt)
{
option.setAttribute('selected',true);
}
});
return select.outerHTML;
}
})();
*/
}
//create a question in inputs table
level2.createQuestion=function(question,table) {
if(Questions.isHiddenQuestion(question)) return;
var t=table;
var checked = Global.Configuration["Yes/No"][question];
//new row
var newRow=t.insertRow(-1);
newRow.setAttribute('question',question);
newRow.onmouseover=function(){hlQuestionFields(this.getAttribute('question'),1)}
newRow.onmouseout=function(){hlQuestionFields(this.getAttribute('question'),0)}
newRow.style.background = checked ? "lightgreen" : "#eee";
//1st cell: show question
var newCell=newRow.insertCell(-1);
newCell.colSpan=2;
newCell.style.paddingRight="1em";
newCell.style.textAlign="right";
newCell.innerHTML=(function() {
var ret="";
if(checked)
{
var expanded=Expanded[question];
if(expanded===undefined)expanded=1;//expanded by default
ret+="<span style=float:left;cursor:pointer onclick=level2.toggleQuestionVisibility(this,'"+question+"') expanded="+expanded+">▼</span>"
}
ret+=translate(question)+"?";
return ret;
})()
//2nd cell: show yes no
var newCell=newRow.insertCell(-1);
newCell.colSpan=2;
newCell.innerHTML=(function() {
var checked_n = checked ? "" : "checked";
var checked_y = checked ? "checked" : "";
var str = ""+
"<div class=flex style=justify-content:center>"+
" <div>"+
" <label>"+translate('no')+" <input type=radio name='"+question+"' onclick=setQuestion('"+question+"',0) "+checked_n+"></label>"+
" </div>"+
" <div>"+
" <label>"+translate('yes')+" <input type=radio name='"+question+"' onclick=setQuestion('"+question+"',1) "+checked_y+"></label>"+
" </div>"+
"</div>"+
"";
return str;
})();
//show variables
if(checked) {
Questions[question].variables.forEach(function(field) {
/*check if level3 only*/
if((Level3.list.indexOf(field)+1)==0)
{
level2.createInput(field,table,question);
}
});
}
}
/** Redisplay table id=inputs */
level2.updateInputs=function() {
var t=document.getElementById('inputs');
while(t.rows.length>2){t.deleteRow(-1)}
//variables without questions associated
for(var field in CurrentLevel) {
/*check if current field is filtered*/
if(Questions.isInside(field)) continue;
/*first check if function*/
if(typeof(CurrentLevel[field])!="number") {
/*then, check if is calculated variable "c_xxxxxx" */
if(field.search(/^c_/)==-1) continue;
}
/*check if level3 only*/
if(Level3.list.indexOf(field)+1) continue;
//create input
level2.createInput(field,t);
}
//go over questions of this level, advanced=0
Questions.getQuestions(CurrentLevel).forEach(function(question) {
if(!Questions[question].advanced) {
level2.createQuestion(question,t);
}
});
//here check if table is empty (==t.rows.length is 2)
if(t.rows.length<3) {
var newCell=t.insertRow(-1).insertCell(-1);
newCell.colSpan=4;
newCell.innerHTML="<span style=color:#999>~All inputs inactive</span>";
}
//bottom line decoration with the color of W/WW
(function(){
var newRow=t.insertRow(-1);
var newTh=document.createElement('th');
newTh.setAttribute('colspan',4);
newTh.style.borderBottom='none';
newRow.appendChild(newTh);
})();
}
/**
* Update a field from the Global object
* @param {string} field - The field of the CurrentLevel object
* @param {number} newValue - new numeric value of the field
*/
level2.updateField=function(field,newValue) {
newValue=parseFloat(newValue);
if(isNaN(newValue))newValue=0;
CurrentLevel[field]=newValue*Units.multiplier(field);
//if field is not level 2 only
if(!(Level2only.list.indexOf(field)+1))
{
//also update stage if substages==1
if(substages.length==1)
substages[0][field]=newValue*Units.multiplier(field);
}
init();
}
</script>
<table id=inputs style="width:99%">
<tr><th colspan=5 class=tableHeader>
<?php write('#INPUTS')?>
—
<?php write('#Enter values for')?>
<?php if($sublevel){write("#$sublevel");}else{write("#$level");} ?>
<?php write('#stages')?>
<tr>
<th><?php write('#Code')?>
<th style="width:100% !important"><?php write('#Description')?>
<th><?php write('#Current value')?>
<th><?php write('#edit_unit')?>
<tr><td colspan=4 style=color:#ccc><i><?php write('#loading')?>...</i>
</table>
<style>
table#inputs .non-editable {
text-align:right;
cursor:pointer;
transition:background 1s;
}
#inputs .non-editable:hover {
background:#eaeeea;
}
#inputs .non-editable:before {
content:' ∑';
float:left;
color:#999;
}
#inputs .non-editable:hover:before {
color:black;
content:' ∑';
}
</style>