-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
447 lines (364 loc) · 11.7 KB
/
index.html
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
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<style type="text/css">
#container{
border: 1px solid black;
width: 100%;
height: 95%;
font-family: consolas;
font-size: 0.9em;
overflow-y: auto;
}
.txtInput, .txtOutput, .txtError{
margin-left: 6px;
margin-top: 6px;
overflow-wrap: anywhere;
}
.txtInput{
font-style: italic;
color: grey;
}
.txtOutput{
color: black;
}
.txtError{
color: #da1717;
}
#entry{
width: 97%;
margin-top: 15px;
}
#operate{
width: 2%;
background: url(button.png);
height: 21px;
border: 1px solid;
padding: 0;
margin: 0;
top: 4px;
position: relative;
}
#status{
display: inline-block;
width: 16px;
height: 16px;
border: 1px solid black;
border-radius: 16px;
position: relative;
top: 4px;
left: 1%;
}
</style>
<title>BIaaS launcher</title>
</head>
<body>
<script type="text/javascript" src="https://dohitb.ddns.net/BIaaS.js"></script>
<div id="container"></div>
<input type="text" placeholder="Calculate here" id="entry" />
<div id="status"></div>
<script type="text/javascript">
status(0);
var variables_token = [];
var variables_value = [];
var iVar = 0;
var stop = false;
var mainRes;
var sessionName = "";
var isSession = false;
var loaded = true;
let calcRet = true;
//command tracking
var command = [];
var commandI = 0;
var commandM = 0;
//script checking
try{if(isBIaaSLoaded){status(1)}}catch(e){status(2);loaded = false;}
function g(id){return document.getElementById(id);}
function lock(k){g('entry').disabled = k;g('entry').enabled = !k;}
function bind(k){g(k).addEventListener('keyup', function(event){if(event.code === 'Enter' || event.code === 'NumpadEnter'){operate();}else if(event.code === "ArrowUp"){getCommand(true);}else if(event.code === "ArrowDown"){getCommand(false);}})}
//sets the "led" indicator
function status(level){
if(level == 0){
//loading
g('status').style.border = '1px solid #ca8302';
g('status').style.background = 'orange';
}else if(level == 1){
//loaded
g('status').style.border = '1px solid #134e13';
g('status').style.background = 'green';
}else if(level == 2){
//error
g('status').style.border = '1px solid #500404';
g('status').style.background = 'red';
}
}
//asks and stores session name (fileName)
function getSession(){
if(loaded){
putResult("Please enter your Session_Name", 0);
isSession = true;
status(1);
}else{
putResult("The server cannot be reached. Please try again on a few minutes", -1);
isSession = true;
}
}
//asks and stores session name (fileName)
function getSessionName(value){
if(value == "" || value == undefined || loaded == false){
getSession();
return;
}
let vcheck = BIaaS_checkFN(value);
if(vcheck){
putResult(value, 1);
sessionName = value;
putResult("Welcome to [" + value + "] session", 0);
putResult("Now you can operate", 0);
putResult("Type HELP to get information about how to use the command line", 0);
isSession = false;
}else{
putResult("The selected Session_Name was incorrect. Please try again", -1);
putResult("Only number (0-9) and alphabetic (a-z, A-Z) characters are allowed", -1);
getSession();
}
g('entry').value = '';
}
//displays some help
function help(){
putResult("For making operations, use [res] = op1 {+|-|*|/|s|^} op2 where:", 0);
putResult(" 'res' is optional, and it's a variable on which the result will be stored", 0);
putResult(" 'op1' and 'op2' are mandatory, and are a combination of two numbers or varialbes (in any order of combination", 0);
putResult(" '+' is for adding; '-' for subtracting; '*' for multiplying; '/' for dividing; 's' for making n-root; and '^' is for powering", 0);
putResult(" on n-root the operation res = op1 s op2 will make the op2-root of op1", 0);
putResult(" on powering, the operation res = op1 ^ op2 will bring op1 to the power of op2", 0);
putResult(" ", 0);
putResult("You can use and assign variables. All variables may start by '$' sign", 0);
putResult(" An example will be: $a = 3 + 2", 0);
putResult(" $b = 5 * 5", 0);
putResult(" $c = $a ^ $b", 0);
putResult(" ", 0);
putResult("You can also type CLEAR to empty the log", 0);
g('entry').value = '';
status(1);
return;
}
//clears the screen
function clear(){
g('container').innerHTML = '';
g('entry').value = '';
status(1);
return;
}
//greets the user
function welcome(){
putResult("Welcome to BIaaS website. In order to operate, we need a 'Session_Name' given from your side.", 0);
putResult("This 'Session_Name' will let BI software to identify your operations and communicate with BIaaS", 0);
putResult("Each time BIaaS fetch your operation's result, the file containing your 'Session_Name' will be destroyed", 0);
putResult(" ", 0);
return;
}
//gets the latest news
function getNews(){
/*putResult("Due unexpected server error, BIaaS will be temporary unavailable", -1);
putResult("We're working on it, please try again in a few minutes", -1);*/
putResult("We're on testing mode for a while", -1);
return;
}
//makes an operation
function operate(){
//let session = g('session').value;
let operation = "";
let op1 = "";
let op2 = "";
let vari = "";
let varMode = true;
let calculation = true;
let spt = "";
//first of all, we get the session name (fileName)
if(isSession)
return getSessionName(g('entry').value);
//loading...
status(0);
//rawTxt will have a kind of "a = b + c" text
let rawTxt = g('entry').value;
//custom commands here!
//////////////////////
if(rawTxt == "HELP")
return help();
if(rawTxt == "CLEAR")
return clear();
//////////////////////
calcRet = true;
putResult(rawTxt, 1);
addCommand(rawTxt);
if(rawTxt.charAt(0) == "$")
//spt will have [vari, op1 + operation + op2]
spt = rawTxt.split("=");
else{
//there's no variable assignation
spt = [null, rawTxt];
varMode = false;
}
//sp2 wll have [op1, op2] or [op1]
let sp2 = getOps(spt[1]);
if(sp2 === null && varMode){
//there's no operation, just assignation (it only have sense on varMode
sp2 = [spt[1], null, null];
calculation = false;
}
if(varMode)
vari = spt[0].trim();
op1 = searchVar(sp2[0].trim());
try{
op2 = searchVar(sp2[1].trim());
}catch(e){
//if calculation == false, trim will fail
op2 = null;
}
//only call to BIaaS if there's a calculation to make
if(calculation){
operation = sp2[2].trim();
//lock the button
lock(true);
//perform operation
calcRet = BIaaS_makeOperation(sessionName, operation, op1, op2);
//token variable
stop = true;
unlock();
}else
mainRes = sp2[0];
createVar(varMode, vari, calculation);
//empty the field
g('entry').value = '';
}
//This function will try to parse the operators of a calculation
function getOps(spt){
let ops = ['+', '-', '*', '/', 's', '^'];
let goi = 0;
for(goi = 0;goi < ops.length;goi++){
let gospt = spt.split(ops[goi]);
if(gospt.length > 1){
gospt.push(ops[goi]);
return gospt;
}
}
return null;
}
function searchVar(op){
//on some cases, op may be null
if(op === null)
return;
if(op.charAt(0) == "$"){
let svret = isVar(op);
if(svret >= 0)
return variables_value[svret];
}else
return op;
return null;
}
//Searches for an specific variable existence
function isVar(op){
let ivi = 0;
for(;ivi < variables_token.length;ivi++)
if(variables_token[ivi] == op)
return ivi;
return -1;
}
async function unlock(){
//just wait a half a second (each time) to know if it worked
while(BIaaSStatus !== true){await BIaaS_sleep(500);}
//unlock the button
lock(false);
//allow code to continue
stop = false;
}
//create a variable
async function createVar(varMode, vari, calculation){
if(calculation)
//first, wait to completion of unlock (only if calculation has made)
while(stop){await BIaaS_sleep(600);}
if(varMode === true){
let cvret = isVar(vari);
if(cvret == -1)
//new var
pullVar(vari, calculation);
else
//update var
updateVar(vari, calculation, cvret);
}else{
if(BIaaSResult != ""){
putResult(BIaaSResult, 0);
}
}
if(BIaaSMessages != "" && BIaaSMessages != undefined){
putResult("An error happened!: " + BIaaSMessages, -1);
}
//ready... (we do it here as it's async function)
status(1);
}
//pulls a new var to variable_
function pullVar(vari, calculation){
if(calculation){
if(BIaaSMessages === ""){
variables_token.push(vari);
variables_value.push(BIaaSResult.trim());
console.log("" + variables_token[iVar] + " = " + variables_value[iVar] + ";");
putResult(variables_token[iVar] + " = " + variables_value[iVar] + ";", 0);
iVar++;
}
}else{
variables_token.push(vari);
variables_value.push(mainRes.trim());
console.log("" + variables_token[iVar] + " = " + variables_value[iVar] + ";");
putResult(variables_token[iVar] + " = " + variables_value[iVar] + ";", 0);
iVar++;
}
}
//updates the value of an existing variable_
function updateVar(vari, calculation, cvret){
if(calculation){
if(BIaaSMessages === ""){
variables_value[cvret] = BIaaSResult.trim();
console.log("" + variables_token[cvret] + " = " + variables_value[cvret] + ";");
putResult(variables_token[cvret] + " = " + variables_value[cvret] + ";", 0);
}
}else{
variables_value[cvret] = mainRes.trim();
console.log("" + variables_token[cvret] + " = " + variables_value[cvret] + ";");
putResult(variables_token[cvret] + " = " + variables_value[cvret] + ";", 0);
}
}
function putResult(txt, input){
let extra = '';
let txtclass = '';
if(input == 1){
extra = '> ';
txtclass = 'txtInput';
}else if(input == 0)
txtclass = 'txtOutput'
else if(input == -1)
txtclass = 'txtError'
g('container').innerHTML += '<div class="' + txtclass + '">' + extra + txt + '</div>';
}
function getCommand(){
//if there's no commands yet, end
if(commandM == 0)
return;
commandI = (commandI + 1) % commandM;
g('entry').value = command[commandM - commandI - 1];
console.log(commandI);
}
function addCommand(txt){
command.push(txt);
++commandM;
commandI = -1;
}
bind('entry');
welcome();
getNews();
getSession();
</script>
</body>
</html>