-
Notifications
You must be signed in to change notification settings - Fork 0
/
astrotest.js
925 lines (802 loc) · 25 KB
/
astrotest.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
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
/* Logic for astrotest.html */
var fso;
function init() {
clearError();
clearOut( );
try {
fso = new ActiveXObject("Scripting.FileSystemObject");
} catch (e) {
showError("ActiveX Objects could not be created - Internet Explorer and ActiveX authorization required for this page!");
}
// Fill select options from astrotest_settings.js
fill_select_options(houseSystems)(byId("hsys"));
fill_select_options(dateFunctions)(byId("rand-date-fn"));
// Define the reactions on clicks and changes
registerFor( "compute", "click", compute );
// Score function - new rows
registerFor( byClass("add_row"), "click", on_click_score_fn_add_row );
// Random date functions
registerFor( "rand-date-fn", "change", on_change_rand_date_fn );
registerFor( "generate-seed-values", "click", generate_seed_values );
registerFor( byClass("juldate"), "change", input_juldate );
// Handle the execution plan
registerFor( "save-plan", "click", on_click_save_plan );
registerFor( "read-plan", "click", on_click_read_plan );
// AAF - CSV conversion
registerFor( "switch-to-aaf", "click", switch_csv_aaf(true) );
registerFor( "switch-to-csv", "click", switch_csv_aaf(false) );
registerFor( "convert-from-aaf", "click", convert_from_aaf );
// Different input fields for file names
var file_chooser = fileChooser( );
registerFor( byClass("choose-file"), "click", file_chooser.open );
registerFor( "file-dialog", "change", file_chooser.take );
// Showing and hiding the help window
helpDisplay( );
}
// Adapt the additional parameter input on change of random date function
function on_change_rand_date_fn() {
var fn = getText("rand-date-fn");
byId("R-dates").style.display = (fn == 'R' ? "" : "none" );
byId("Q-quantiles").style.display = (fn == 'Q' ? "" : "none" );
}
// Adapt the input field visibility for a score function
function on_change_score_fn( ) {
var row = getTagNamedParent(this,"TR");
adaptScoreFunctionInputFields( row );
}
// Generate plan from selections, and save it to file
function on_click_save_plan() {
var plan = generatePlan();
savePlan( getText("plan-file"), plan );
}
// Read plan from file, and fill selections from it
function on_click_read_plan() {
var plan = readPlan( getText("plan-file") );
fillSelectionsFromPlan( plan );
}
// Toggle input field for csv versus aaf
function switch_csv_aaf( to_aaf ) {
return function() {
byId("row-csv").style.display = (to_aaf ? "none" : "");
byId("row-aaf").style.display = (to_aaf ? "" : "none");
}
}
// Calls the conversion function from aaf2csv.js
// Errors are shown in the output area
function convert_from_aaf() {
try {
clearError( );
clearOut( );
setText("source-file","");
var file_csv = convert_aaf_to_csv( );
out("File successfully converted to '"+file_csv+"'");
setText("source-file",file_csv);
byId("row-csv").style.display="";
byId("row-aaf").style.display="none";
} catch (e) {
showError(e);
}
}
function on_click_score_fn_add_row(event) {
score_fn_add_row(event.target.id.replace(/^add-/,""))();
}
// Different functions to add score function terms
function score_fn_add_row( id ) {
var fn = {};
if (!(id in fn)) {
var cells = parseCellsFromTemplate( "row-template-"+id );
fn[id] = function() {
var row = byId("score-fn").insertRow(-1);
setClass(row,id.replace(/^add\-/,""));
cells.each( function( cell ) {
var td = row.insertCell();
td.innerHTML = cell;
});
byClass("fn",row).each( function() {
registerFor( this, "change", on_change_score_fn );
});
byClass("delete_row",row).each( function() {
registerFor( this, "click", delete_row );
});
return row;
};
}
return fn[id];
function parseCellsFromTemplate( template_id ) {
var tmpl = document.createElement("div");
tmpl.innerHTML = "<table><tr>"+byId(template_id).innerHTML+"</tr></table>";
fill_listboxes( tmpl ) ;
return byTagName( "td", tmpl ).map( function(c) { return c.innerHTML });
}
}
// For a given score function: which fields are visible
function adaptScoreFunctionInputFields( row ) {
var fn = byClass("fn",row)[0].value;
byClass( "pl2", row ).each( function() {
this.style.display = takesTwoPlanets( fn ) ? "":"none";
})
}
// Describes which functions need two planets as arguments
function takesTwoPlanets( fn ) {
return (fn == "zll") || (fn == "mpp");
}
// On click: Delete a term of the score function
function delete_row( event ) {
var row = getTagNamedParent( event.target, "tr" );
row.parentElement.removeChild(row);
}
// Fill all listboxes of a row
function fill_listboxes( element ) {
[
{ type:"planet", options:planets },
{ type:"fn", options:scoreFunctions },
{ type:"aspect", options:aspects },
{ type:"house", options:houses },
{ type:"sign", options:signs },
{ type:"reference", options:references },
{ type:"direction", options:directions }
].each( function() {
byClass( this.type, element).each(
fill_select_options( this.options )
);
});
}
// General function to fill a listbox with data
function fill_select_options( data ) {
return function( selectElement ) {
for (var key in data) {
add_option( selectElement, key, data[key] );
}
}
}
// General function to add a select option
function add_option( selectElement, key, value ) {
var option = document.createElement("option");
option.value = key;
option.textContent = value;
if ( selectElement.hasAttribute( "value" ) &&
(key === selectElement.getAttribute("value"))
) {
option.setAttribute("selected", true);
}
selectElement.appendChild(option);
}
// Perform the computation, if everything is OK
function compute() {
try {
if (checkInput()) {
var plan = generatePlan();
savePlan( getText("plan-file"), plan );
clearOut( );
executePlan();
}
} catch (e) {
out (e);
}
}
// Output text to output area (Log)
function out( text ) {
byId("output").value += text + "\n";
}
// Clear output area (Log)
function clearOut() {
setText("output","");
}
// All input rules to check before computation
function checkInput( ) {
clearError();
try {
if (is_initial("source-file")) throw {
msg:"Please specify source file",
field:byId("source-file")
};
if (!fso.FileExists(fullPath(getText("source-file")))) throw {
msg:"File '" + fullPath(getText("source-file")) + "' not found",
field:byId("source-file")
};
byTagName("input")
.subset( function() {
return (this.getAttribute("type") == "number") &&
isVisible( this );
})
.each( check_numeric );
if (byTagName("tr",byId("score-fn")).length === 0) {
throw {
msg: "No score function defined!"
}
}
if (getText("test-runs")<=0) {
throw {
msg:"Please specify a positive number of runs",
field:byId("test-runs")
}
}
if (getText("rand-date-fn")=="R") {
var jd1 = check_numeric("jd1"),
jd2 = check_numeric("jd2"),
jd3 = check_numeric("jd3");
if (jd1 >= jd2) throw {
msg:"The julian date values have to increase",
field: byId("jd2")
};
if (jd2 >= jd3) throw {
msg:"The julian date values have to increase",
field: byId("jd3")
}
}
if (getText("rand-date-fn")=="Q") {
var q = check_numeric("quantiles");
if (q <=1) throw {
msg:"Please choose at least two 2-quantile (=median)",
field:"quantiles"
};
if (q != Math.floor(q)) throw {
msg:"Please choose an integer value for quantile number",
field:"quantiles"
};
}
return true;
} catch(e) {
showError(e);
return false;
}
}
// Display an exception object in error message area,
// Mark a field as erroneous
function showError( e ) {
var msg = (typeof e == "string") ? e : e.msg,
field;
setText("msg",e.msg || e);
if ((typeof e == "object") &&
e.field &&
(field = getElement( e.field ))) {
field.select();
field.focus();
setClass(field,"error");
}
}
// Clear error message area,
// clear all field error markers
function clearError() {
setText("msg","");
byTagName("input").concat(byTagName("select")).each( function() {
resetClass(this,"error");
});
}
// Checks whether element is currently visible
function isVisible( element ) {
// Quick tests
if (!element) element = this;
if (element.offsetParent === null) return false;
if (element.style.display == "none") return false;
var row = getTagNamedParent(element,"TR");
if (row && row.style.display == "none") return false;
// Slow, but definitive
var s = window.getComputedStyle( element );
return (s.display != "none");
}
// Throws an exception if field content is not numeric
// If numeric, the field content is returned as a number
function check_numeric( field ) {
if (!field) field = this;
if (typeof field == "string") field = byId(field);
var v = field.value;
if (!is_numeric(v)) {
throw {
msg:"Please enter a number",
field:field
};
}
return 1*v;
}
// Checks whether "value" contains a number
function is_numeric( value ) {
return !isNaN(parseFloat((value))) && isFinite(value)
}
// Returns true if field is initial
// i.e. contains only whitespace, or,
// in the numeric case, is 0
function is_initial( field ) {
if (!field) field = this;
if (typeof field == "string") field = byId(field);
var v = field.value;
if (is_numeric(v)) return ( v == 0);
else return !v.match(/\S/);
}
// For validation purposes: Throws an exception
// if field contains only whitespace (non-numeric case)
// or 0 (numeric case)
function check_not_initial( field ) {
if (!field) field = this;
var v = field.value;
if (is_numeric(v)) {
if ( 1*v === 0) throw {
msg:"Please enter non-zero value",
field:field
}
}
else {
if (!v.match(/\S/)) throw {
msg:"Please enter a value",
field:field
}
}
}
// Build the full execution plan from the HTML page input
function generatePlan( ) {
// Build the plan - as an array of strings (to become the lines of the file)
var plan = ["version:0.9"];
plan.push( "source-file:"+getText("source-file") );
plan.push( "test-runs:"+1*getText("test-runs") );
plan.push( "hsys:"+byId("hsys").value );
plan.push( "rand-date-fn:"+byId("rand-date-fn").value );
switch (getText("rand-date-fn")) {
case 'Q':
plan.push( "quantiles:"+1*getText("quantiles") );
break;
case 'R':
plan.push( "jd:"+1*getText("jd1")+","+1*getText("jd2")+","+1*getText("jd3") );
break;
}
plan.push( "seed:"+1*getText("seed1")+","+1*getText("seed2") );
plan.push( "dbg-level:"+getText("dbg-level") );
plan.push( "cont-with-mosh:"+ (byId("cont-with-mosh").checked ? 1 : 0 ) );
var ephepath = getText("ephepath");
if (ephepath.match(/\S/)) {
plan.push("ephepath:"+ephepath);
}
generateScoreFunction( plan );
return plan;
}
// Build the score function terms from the input in the HTML page
function generateScoreFunction( plan ) {
var orb = getText("orb")*1; // Needed for aspect resolution
var translate = {
"aspect":function(row,plan) {
var val = getInputValues(row);
var fn = val[5] == 'z' ? raw_fn.zll : raw_fn.mpp;
if (val[2]===0) {
plan.push( fn( val[0], val[1], val[3], -orb, orb ) );
}
else if (val[2] == 180) {
plan.push( fn( val[0], val[1], val[3], 180-orb, 180 ) );
plan.push( fn( val[0], val[3], val[1], -180, -180+orb ) );
}
else {
if (val[4]!='-')
plan.push( fn( val[0], val[1], val[3], val[2]-orb, val[2]+orb ) );
if (val[4]!='+')
plan.push( fn( val[0], val[3], val[1], val[2]-orb, val[2]+orb ) );
}
},
"house-position":function(row,plan) {
var val = getInputValues(row);
plan.push( raw_fn.mp( val[0],val[1],null,val[2],val[2]+30) );
},
"sign-position":function(row,plan) {
var val = getInputValues(row);
plan.push( raw_fn.zl( val[0],val[1],null,val[2],val[2]+30) );
},
"raw-term":function(row,plan) {
var values = getInputValues(row);
var fn = values[2];
values.splice(2,1);
plan.push( raw_fn[fn].apply(this,values) );
}
};
var raw_fn = {
"zl":function(coeff,pl1,pl2,arg1,arg2){
return coeff+"*zl("+pl1+","+arg1+","+arg2+")";
},
"zll":function(coeff,pl1,pl2,arg1,arg2){
return coeff+"*zll("+pl1+","+pl2+","+arg1+","+arg2+")";
},
"mp":function(coeff,pl1,pl2,arg1,arg2){
return coeff+"*mp("+pl1+","+arg1+","+arg2+")";
},
"mpp":function(coeff,pl1,pl2,arg1,arg2){
return coeff+"*mpp("+pl1+","+pl2+","+arg1+","+arg2+")";
}
};
plan.push("score-source-begin");
plan.push("orb:"+getText("orb"));
byTagName("tr",byId("score-fn")).each( function(tr) {
plan.push(tr.className+":"+getInputValues(tr).join(","))
});
plan.push("score-source-end");
plan.push("score-begin");
byTagName("tr",byId("score-fn")).each( function(tr) {
translate[tr.className](tr,plan);
});
plan.push("score-end");
}
// Retrieves an array of all the inputs in a row
function getInputValues(tr) {
var values = [];
getInputFields(tr).each( function() {
var v = this.value;
var value;
if (v==="") value = 0;
else if (is_numeric(v)) value = v * 1;
else value = v;
values.push( value );
})
return values;
}
// Collect all the input fields and listboxes of a row in order
function getInputFields(tr) {
return byCondition( function() {
return this.tagName.match(/SELECT|INPUT/)
}, tr )
}
// Execute the C program "astrotest.exe" from the command line
function executePlan( ) {
out('cd "'+readPathFromURL()+'"');
out('astrotest "'+getText("plan-file")+'" "'+getText("source-file")+'"');
var output = executeCommand({
cmd:'astrotest "'+getText("plan-file")+'" "'+getText("source-file")+'"',
path:readPathFromURL()
});
out(output);
}
// Generate two seed-values randomly
function generate_seed_values( ) {
setText("seed1", (Math.random( )*10000000).toFixed(0) );
setText("seed2", (Math.random( )*10000000).toFixed(0) );
}
// Input fields which expect a julian date
// As a hint, the corresponding calendar date will be showed
function input_juldate(event) {
var inputField = event.target;
// If calendar date given: Convert to Julian date
var m = inputField.value.match(/(\d{1,2})\.(\d{1,2})\.(\-?\d{1,4})/);
if (m) {
var jd = swe_julday( m[3],m[2],m[1], 0, m[3] >= 1582 );
inputField.value = jd;
}
// Make calendar date quickinfo
inputField.setAttribute("title",
is_numeric(inputField.value) ? swe_revjul( 1*inputField.value ) : "" );
}
// Save plan to file
function savePlan( file, plan ) {
var fs = fso.OpenTextFile( fullPath(file),
2, // for Writing
1 // Create if not existing
);
plan.each( function(line) {
fs.WriteLine( line );
});
fs.Close( );
}
// Read plan from file
function readPlan( file ) {
var plan = [];
var fs = fso.OpenTextFile( fullPath(file),
1 // for Reading
);
while (!fs.AtEndOfStream) {
plan.push( fs.ReadLine() );
}
fs.Close();
return plan;
}
// After having read the plan from file, the lines will
// be used to propagate the input fields of the HTML page
function fillSelectionsFromPlan( plan ) {
deleteScoreFunction( );
var readingScoreSource = false;
var hasScoreSource = false;
var readingScore = false;
var score = [];
plan.each( function(line) {
if (/^#/.test(line)) return;
var m = line.match( /^\s*([\w-]+):\s*(.*)/ );
if (m && !readingScoreSource) {
switch(m[1]) {
case "test-runs":
case "hsys":
case "rand-date-fn":
case "quantiles":
case "dbg-level":
case "source-file":
case "ephepath":
setText(m[1],m[2]);
return;
case "cont-with-mosh":
byId("cont-with-mosh").checked = (m[2] != "0");
return;
case "seed":
var seeds = m[2].split(",");
setText("seed1",seeds[0]);
setText("seed2",seeds[1]);
return;
case "jd":
var dates = m[2].split(",");
setText("jd1",dates[0]);
setText("jd2",dates[1]);
setText("jd3",dates[2]);
return;
}
}
else if (/score\-source\-begin/.test(line)) {
readingScoreSource = true;
}
else if (/score\-source\-end/.test(line)) {
readingScoreSource = false;
}
else if (/score\-begin/.test(line)) {
readingScore = true;
}
else if (/score\-end/.test(line)) {
readingScore = false;
}
else if (readingScoreSource) {
if (/^\s*orb\s*:\s*(.*)/.test(line)) {
setText("orb",RegExp.$1);
}
else {
hasScoreSource = true;
addScoreSelectionFromPlan(line);
}
}
else if (readingScore) {
score.push( line );
}
});
// Adapt visibility of rand-date-fn parameters
rand_date_fn_changed();
// No score-source found? Use score
if (!hasScoreSource && (score.length > 0)) {
score.each( function(line) {
var args = line.
// Replace trailing ")"
replace(/\s*\)\s*$/,"").
// Split into value array, separated by comma, ), or *
split(/\s*[*,()]\s*/);
// Add "raw-term" type row to score function
var row = score_fn_add_row( "raw-term" )();
// Build an array for providing with values
// coeff pl1 fn
var values = [ args[0], args[2], args[1] ];
// Second planet (pl2), if function requires two
values.push( takesTwoPlanets(args[1]) ? args[3] : "");
// Append two numeric arguments
values.push.apply( values, args.slice(-2) );
// Fill values into row's input/select fields
getInputFields(row).each( function(field,i) {
field.value = values[i];
});
adaptScoreFunctionInputFields( row );
});
}
}
// Deletes all terms of the score function
function deleteScoreFunction( ) {
var score_fn = byId("score-fn");
byTagName("tr",score_fn).each( function() {
score_fn.deleteRow(-1);
});
}
// Use score-source to propagate a term of the score function in UI
function addScoreSelectionFromPlan(line) {
var m = line.match(/([\w-]+)\s*:\s*(.*)/);
if (m) {
var row = score_fn_add_row( m[1] )();
var value = m[2].split(",");
getInputFields(row).each( function(field,i) {
field.value = value[i];
});
}
}
// Convert .aaf file to .csv file, extracting the three relevant data
// (julian date, geographical longitude, geographical latitude)
function convert_aaf_to_csv() {
var file_aaf = fullPath(getText("source-file-aaf"));
if (!file_aaf.match(/\S/)) {
throw {
msg:"Please specify AAF source file",
field:"aaf-source-file"
};
}
if (file_aaf.match(/\.csv/)) {
throw {
msg:"AAF source file should not end with .csv",
field:"aaf-source-file"
};
}
if (!fso.FileExists(file_aaf)) {
throw {
msg: "File '" + file_aaf + "' could not be found",
field:"aaf-source-file"
};
}
var aaf = fso.OpenTextFile( fullPath(file_aaf),
1 // for Reading
);
var file_csv = (file_aaf.indexOf(".")>=0) ?
file_aaf.replace(/\.([^.]*)$/,".csv")
: file_aaf + ".csv";
var csv = fso.OpenTextFile( fullPath(file_csv),
2, // for Writing
1 // Create if not existing
);
var rc = aaf2csv(aaf,csv,stderr());
csv.Close( );
aaf.Close( );
if (rc != 0) {
throw {
msg:"Errors while trying to convert AAF file (see log)",
field:"aaf-source-file"
}
}
else return file_csv;
function stderr() {
return {
WriteLine:function(line) {
out(line);
}
}
}
}
function fullPath( file ) {
return (file.indexOf('\\')>=0) ? file : readPathFromURL()+"\\"+file;
}
function readPathFromURL() {
return decodeURIComponent(
document.location.pathname.
replace(/^\/(.*)\/.*\.html/,"$1").
replace(/\//g,"\\")
);
}
// General function to execute a command from within an HTML page
function executeCommand( opt ) {
var sh = new ActiveXObject("Wscript.Shell");
if (opt.path) sh.CurrentDirectory = opt.path;
var process = sh.exec( opt.cmd );
if (opt.input) {
process.StdIn.Write(opt.input);
process.StdIn.Close();
}
waitUntilFinished( );
var errors = process.StdErr.ReadAll();
if (process.ExitCode != 0 || errors.match(/\S/)) {
throw "Error\n" +
opt.cmd + "\n" +
"Command terminated with exit code " + process.ExitCode + "\n" +
errors ;
}
return process.StdOut.ReadAll( ).replace(/\s*$/,"");;
function waitUntilFinished( ) {
if (process.status === 0) window.setTimeout( waitUntilFinished, 200);
}
}
// Julian Date <--> Calendar Date conversions,
// Copied from Swiss Ephemeris (swedate.c) and adapted to JavaScript
function swe_julday(year, month, day, ut, useJulianCalendar) {
var jd;
var u,u0,u1,u2;
u = year*1;
if (1*month < 3) u -=1;
u0 = u + 4712.0;
u1 = month*1 + 1.0;
if (u1 < 4) u1 += 12.0;
jd = Math.floor(u0*365.25)
+ Math.floor(30.6*u1+0.000001)
+ 1*day + ut/24.0 - 63.5;
if (!useJulianCalendar) {
u2 = Math.floor(Math.abs(u) / 100) - Math.floor(Math.abs(u) / 400);
if (u < 0.0) u2 = -u2;
jd = jd - u2 + 2;
if ((u < 0.0) && (u/100 == floor(u/100)) && (u/400 != floor(u/400)))
jd -=1;
}
return jd;
}
function swe_revjul(jd, julianCalendar)
{
var u0,u1,u2,u3,u4;
u0 = 1*jd + 32082.5;
if (!julianCalendar) {
u1 = u0 + Math.floor (u0/36525.0) - Math.floor (u0/146100.0) - 38.0;
if (jd >= 1830691.5) u1 +=1;
u0 = u0 + Math.floor (u1/36525.0) - Math.floor (u1/146100.0) - 38.0;
}
u2 = Math.floor (u0 + 123.0);
u3 = Math.floor ( (u2 - 122.2) / 365.25);
u4 = Math.floor ( (u2 - Math.floor (365.25 * u3) ) / 30.6001);
var month = Math.floor(u4 - 1.0);
if (month > 12) month -= 12;
var day = Math.floor((u2 - Math.floor (365.25 * u3) - Math.floor (30.6001 * u4)));
var year = Math.floor((u3 + Math.floor ( (u4 - 2.0) / 12.0) - 4800));
return day + "." + month + "." + year;
}
// --- Manage displaying and hiding of help window
function helpDisplay() {
var helpBox = byId("help-text");
var showingId = "";
registerHelpDisplay();
function registerHelpDisplay() {
registerFor( document.body, "mouseover", helpMouseOver );
registerFor( document.body, "mouseout", helpMouseOut );
registerFor( document.body, "keyup", function(event) {
if (getKeyCode(event) == 27) hideHelpImmediately( ); // 27 = ESC
});
registerFor( document.body, "focus", hideHelpImmediately );
}
function helpMouseOver(event) {
var src = getSource( event );
if (hasClass(src,"with-help")) {
displayHelp(src);
}
else if (src.nodeName == "INPUT" || src.nodeName == "SELECT") {
hideHelpImmediately( );
}
}
function helpMouseOut(event) {
var src = getSource( event );
if (hasClass(src,"with-help")){
var leavingId = getHelpClassId( src );
hideHelp( leavingId );
}
}
function displayHelp(src) {
var id = getHelpClassId(src);
if (!id) return;
var helpText = help_text( id );
if (!helpText) return;
showingId = id;
var offset = getOffset( src );
helpBox.style.top = offset.top+"px";
helpBox.style.left = offset.left+"px";
helpBox.style.display = "";
helpBox.innerHTML = helpText;
}
function hideHelp(leavingId) {
if (helpBox.style.display == "none") return; // done
window.setTimeout( function() {
if (showingId == leavingId) {
hideHelpImmediately( );
}
},
1200);
}
function hideHelpImmediately() {
helpBox.style.display = "none";
showingId = "";
}
function getOffset( src ) {
var bodyRect = document.body.getBoundingClientRect( );
var srcRect = src.getBoundingClientRect( );
return { "left":srcRect.left - bodyRect.left + srcRect.width,
"top":srcRect.top - bodyRect.top };
}
function help_text( id ) {
if (!id) return null;
var container =
byId("help-texts").
contentWindow.
document.
getElementById(id);
return container ? container.innerHTML : null;
}
function getHelpClassId(src) {
if (!src.hasAttribute("data-help-id")) return null;
return src.getAttribute("data-help-id");
}
}
// Function for opening the file explorer value help,
// and transporting the chosen file name to another input field
function fileChooser() {
var id;
return {
open:function(event) {
id = event.target.getAttribute("data-for");
byId("file-dialog").click( );
},
take:function(event) {
setText(id,event.target.value);
}
};
}
registerFor(window,"DOMContentLoaded",init);