Commit db69e5a792bf5d1f5d1af265d9fac0924c6de322

Authored by Goutte
1 parent 8d4f8bc2

Continue implementing THE GREAT REFACTOR to try to decrease the CPU load of Catalogs.

Showing 2 changed files with 74 additions and 51 deletions   Show diff stats
web/static/js/main.js
1   -// Generated by LiveScript 1.5.0
  1 +// ES6
  2 +//
  3 +//
  4 +
  5 +/**
  6 + *
  7 + * All the javascript code for canvases is in this file,
  8 + * and inline scripts in templates, such as in `home.html.jinja2` contain the
  9 + * DOM listeners (the UI stuff, not the business logic, which is here).
  10 + *
  11 + * Note: We use Promises and ES6.
  12 + *
  13 + * D3.js
  14 + * -----
  15 + *
  16 + * You also WILL NEED d3js v4 documentation : https://d3js.org/
  17 + * We're using a custom build of 4.9.1, one line changed, see d3-custom.js
  18 + * Event bubbling cannot trigger two rects unless we make an event dispatcher,
  19 + * and d3's brush is stopping propagation, as it should by default.
  20 + *
  21 + *
  22 + * Code is weird, with ref$
  23 + * ------------------------
  24 + *
  25 + * Lots of machine-generated micro-optimizations. Most are unconsequential.
  26 + * The complex exporting pattern is ~ technical debt. Simplify it if you can.
  27 + */
  28 +
2 29 (function () {
3 30 let Target;
4   - let SpaceWeather;
  31 + // let SpaceWeather;
5 32 let TimeSeries;
6 33 let Orbits;
7   - const out$ = typeof exports != 'undefined' && exports || this;
8   - console.debug(out$);
  34 + const global = typeof exports !== 'undefined' && exports || this;
9 35 const GOLDEN_RATIO = 2 / (1 + Math.sqrt(5));
10 36 Target = ((() => {
11 37 // Target.displayName = 'Target';
... ... @@ -20,11 +46,11 @@
20 46  
21 47 return Target;
22 48 })());
23   - out$.SpaceWeather = SpaceWeather = ((() => {
  49 +
  50 +
  51 + global.SpaceWeather = ((() => {
24 52 // "The main app, instanciated from an inline script.\nIt defaults to an interval starting two months ago, and ending in a month.\n(both at midnight)";
25 53 // SpaceWeather.displayName = 'SpaceWeather';
26   - // let API_TIME_FORMAT;
27   - // let INPUT_TIME_FORMAT;
28 54 // const prototype = SpaceWeather.prototype;
29 55 // const constructor = SpaceWeather;
30 56 const API_TIME_FORMAT = "YYYY-MM-DDTHH:mm:ss";
... ... @@ -32,21 +58,19 @@
32 58  
33 59 class SpaceWeather {
34 60 constructor(configuration) {
35   - let configs;
36   - let res$;
37   - let k;
38   - const this$ = this;
  61 + const that = this;
39 62 this.configuration = configuration;
40   - console.info("ยฉ2017\n _ _ _ _ ____\n | | | | ___| (_) ___ | _ \\ _ __ ___ _ __ __ _\n | |_| |/ _ \\ | |/ _ \\| |_) | '__/ _ \\| '_ \\ / _` |\n | _ | __/ | | (_) | __/| | | (_) | |_) | (_| |\n |_| |_|\\___|_|_|\\___/|_|_ |_|_ \\___/| .__/ \\__,_|\n | |__ _ _ / ___| _ \\| _ \\| _ \\_|\n | '_ \\| | | | | | | | | | |_) | |_) |\n | |_) | |_| | | |___| |_| | __/| __/\n |_.__/ \\__, | \\____|____/|_| |_|\n |___/\n\nThe full source of this website is available at :\nhttps://gitlab.irap.omp.eu/CDPP/SPACEWEATHERONLINE");
  63 + this.parameters = {};
41 64 this.targets = {};
42   - res$ = [];
43   - for (k in this.configuration.targets) {
44   - res$.push(this.configuration.targets[k]);
  65 + console.info("ยฉ2017\n _ _ _ _ ____\n | | | | ___| (_) ___ | _ \\ _ __ ___ _ __ __ _\n | |_| |/ _ \\ | |/ _ \\| |_) | '__/ _ \\| '_ \\ / _` |\n | _ | __/ | | (_) | __/| | | (_) | |_) | (_| |\n |_| |_|\\___|_|_|\\___/|_|_ |_|_ \\___/| .__/ \\__,_|\n | |__ _ _ / ___| _ \\| _ \\| _ \\_|\n | '_ \\| | | | | | | | | | |_) | |_) |\n | |_) | |_| | | |___| |_| | __/| __/\n |_.__/ \\__, | \\____|____/|_| |_|\n |___/\n\nThe full source of this website is available at :\nhttps://gitlab.irap.omp.eu/CDPP/SPACEWEATHERONLINE");
  66 +
  67 + let targets_configs = [];
  68 + for (let k in this.configuration.targets) {
  69 + targets_configs.push(this.configuration.targets[k]);
45 70 }
46   - configs = res$;
47   - configs.forEach(target_config => this$.addTarget(new Target(target_config.slug, target_config.name, target_config)));
48   - this.parameters = {};
49   - this.configuration['parameters'].forEach(p => this$.parameters[p['id']] = p);
  71 + targets_configs.forEach(tc => that.addTarget(new Target(tc.slug, tc.name, tc)));
  72 +
  73 + this.configuration['parameters'].forEach(p => that.parameters[p['id']] = p);
50 74 this.orbits = null;
51 75 this.time_series = [];
52 76 }
... ... @@ -218,7 +242,7 @@
218 242 }
219 243  
220 244 loadData(target_slug, started_at, stopped_at) {
221   - "Load the data as CSV for the specified target and interval,\nand return it in a Promise.";
  245 + // "Load the data as CSV for the specified target and interval,\nand return it in a Promise.";
222 246 let sw;
223 247 sw = this;
224 248 return new Promise((resolve, reject) => {
... ... @@ -268,7 +292,7 @@
268 292 }
269 293  
270 294 loadAndCreatePlots(started_at, stopped_at) {
271   - "started_at: moment(.js) datetime object\nstopped_at: moment(.js) datetime object";
  295 + // "started_at: moment(.js) datetime object\nstopped_at: moment(.js) datetime object";
272 296 let targets;
273 297 let res$;
274 298 let k;
... ... @@ -406,6 +430,7 @@
406 430 results$.push(p);
407 431 }
408 432 }
  433 +
409 434 return results$;
410 435 }
411 436  
... ... @@ -420,6 +445,7 @@
420 445 return ts.show();
421 446 }
422 447 });
  448 +
423 449 return this;
424 450 }
425 451  
... ... @@ -433,6 +459,7 @@
433 459 return ts.hide();
434 460 }
435 461 });
  462 +
436 463 return this;
437 464 }
438 465  
... ... @@ -516,6 +543,7 @@
516 543 this.time_series.forEach(ts => ts.resetZoom());
517 544 this.orbits.resetZoom();
518 545 this.setStartAndStop(this.started_at, this.stopped_at);
  546 +
519 547 return this;
520 548 }
521 549  
... ... @@ -525,6 +553,7 @@
525 553 this.current_stopped_at = stopped_at;
526 554 $("#started_at").val(started_at.format(INPUT_TIME_FORMAT));
527 555 $("#stopped_at").val(stopped_at.format(INPUT_TIME_FORMAT));
  556 +
528 557 return this;
529 558 }
530 559 }
... ... @@ -535,17 +564,16 @@
535 564 /////////////////////////////////////////////////////////////////////////////
536 565 //// TIME SERIES ////////////////////////////////////////////////////////////
537 566  
538   - out$.TimeSeries = TimeSeries = ((() => {
  567 + global.TimeSeries = TimeSeries = ((() => {
539 568 const RATIO = GOLDEN_RATIO ** 4;
540 569  
541 570 class TimeSeries {
542 571 constructor(parameter, title, target, data, visible, container, options) {
543   - let now;
544   - let res$;
545   - let i$;
546   - let ref$;
547   - let len$;
548   - let d;
  572 + this.onBrushEnd = this.onBrushEnd.bind(this);
  573 + this.onDoubleClick = this.onDoubleClick.bind(this);
  574 + this.onMouseOut = this.onMouseOut.bind(this);
  575 + this.onMouseOver = this.onMouseOver.bind(this);
  576 + this.onMouseMove = this.onMouseMove.bind(this);
549 577 this.parameter = parameter;
550 578 this.title = title;
551 579 this.target = target;
... ... @@ -553,20 +581,17 @@
553 581 this.visible = visible;
554 582 this.container = container;
555 583 this.options = options || {};
556   - this.onBrushEnd = bind$(this, 'onBrushEnd', this.prototype);
557   - this.onDoubleClick = bind$(this, 'onDoubleClick', this.prototype);
558   - this.onMouseOut = bind$(this, 'onMouseOut', this.prototype);
559   - this.onMouseOver = bind$(this, 'onMouseOver', this.prototype);
560   - this.onMouseMove = bind$(this, 'onMouseMove', this.prototype);
561   - now = moment();
562   - res$ = [];
563   - for (i$ = 0, len$ = (ref$ = this.data).length; i$ < len$; ++i$) {
564   - d = ref$[i$];
  584 + let now = moment();
  585 + const dataLength = this.data.length;
  586 + const predictiveData = [];
  587 + let d;
  588 + for (let i = 0; i < dataLength; ++i) {
  589 + d = this.data[i];
565 590 if (moment(d.x) >= now) {
566   - res$.push(d);
  591 + predictiveData.push(d);
567 592 }
568 593 }
569   - this.predictiveData = res$;
  594 + this.predictiveData = predictiveData;
570 595 this.init();
571 596 }
572 597  
... ... @@ -957,7 +982,6 @@
957 982 let d;
958 983 let xx;
959 984 let yy;
960   - let mirrored;
961 985 let dx;
962 986 let transform;
963 987 i = this.bisectDate(this.data, x0, 1);
... ... @@ -970,7 +994,7 @@
970 994 d = x0 - d0.x > d1.x - x0 ? d1 : d0;
971 995 xx = this.xScale(d.x);
972 996 yy = this.yScale(d.y);
973   - mirrored = this.plotWidth != null && xx > this.plotWidth / 2;
  997 + const mirrored = this.plotWidth != null && xx > this.plotWidth / 2;
974 998 dx = 8;
975 999 if (mirrored) {
976 1000 dx = -1 * dx;
... ... @@ -990,7 +1014,12 @@
990 1014 TimeSeries.prototype.timeFormat = d3.utcFormat("%Y-%m-%d %H:%M");
991 1015 return TimeSeries;
992 1016 })());
993   - out$.Orbits = Orbits = ((() => {
  1017 +
  1018 +
  1019 + ///////////////////////////////////////////////////////////////////////////
  1020 + //// ORBITS PLOT //////////////////////////////////////////////////////////
  1021 +
  1022 + global.Orbits = Orbits = ((() => {
994 1023 // "View of the solar system from above, with orbits segments for selected time\ninterval, from real data.";
995 1024 // Orbits.displayName = 'Orbits';
996 1025 // const prototype = Orbits.prototype;
... ... @@ -1041,11 +1070,11 @@
1041 1070 }
1042 1071  
1043 1072 initOrbiter(slug, config, data) {
  1073 + const thisOrbit = this;
1044 1074 let orbit_ellipse;
1045 1075 let orbiter;
1046 1076 let orbit_line;
1047 1077 let orbit_section;
1048   - const this$ = this;
1049 1078 this.data[slug] = data;
1050 1079 this.orbiters[slug] = config;
1051 1080 if (data.length) {
... ... @@ -1060,7 +1089,7 @@
1060 1089 orbit_ellipse = this.plotWrapper.append("svg:ellipse").classed('orbit orbit_ellipse', true);
1061 1090 orbiter = this.plotWrapper.append("svg:image").attr('xlink:href', config['img']).attr('width', '32px').attr('height', '32px');
1062 1091 orbiter.append('svg:title').text(config.name);
1063   - orbit_line = d3.line().x(d => this$.xScale(d.y)).y(d => this$.yScale(d.x));
  1092 + orbit_line = d3.line().x(d => thisOrbit.xScale(d.y)).y(d => thisOrbit.yScale(d.x));
1064 1093 orbit_section = this.plotWrapper.append('path').datum(data).classed('orbit orbit_section', true);
1065 1094 this.orbitersElements[slug] = {
1066 1095 orbiter,
... ... @@ -1316,10 +1345,4 @@
1316 1345 Orbits.prototype.bisectDate = d3.bisector(d => d.t).left;
1317 1346 return Orbits;
1318 1347 })());
1319   -
1320   - function bind$(obj, key, target) {
1321   - return function () {
1322   - return (target || obj)[key].apply(obj, arguments)
1323   - };
1324   - }
1325 1348 }).call(this);
... ...
web/view/home.html.jinja2
... ... @@ -607,7 +607,7 @@
607 607 <script type="application/javascript" src="{{ static('js/vendor/d3-custom.js') }}"></script>
608 608 <script type="application/javascript" src="{{ static('js/vendor/moment.min.js') }}"></script>
609 609 <script type="application/javascript" src="{{ static('js/vendor/samp.js') }}"></script>
610   -<script type="application/javascript" src="{{ static('js/swapp.js') }}"></script>
  610 +<script type="application/javascript" src="{{ static('js/main.js') }}"></script>
611 611 <script type="application/javascript">
612 612  
613 613 var configuration = {
... ...