Blame view

web/static/js/swapp.js 40.3 KB
438929a4   Goutte   Rewrite the orbit...
1
2
// Generated by LiveScript 1.5.0
(function(){
b60e7acd   Goutte   Rename "source" i...
3
  var GOLDEN_RATIO, Target, SpaceWeather, TimeSeries, Orbits, out$ = typeof exports != 'undefined' && exports || this;
438929a4   Goutte   Rewrite the orbit...
4
  GOLDEN_RATIO = 2 / (1 + Math.sqrt(5));
b60e7acd   Goutte   Rename "source" i...
5
6
7
8
9
10
11
  Target = (function(){
    Target.displayName = 'Target';
    var prototype = Target.prototype, constructor = Target;
    function Target(slug, name, config){
      this.slug = slug;
      this.name = name;
      this.config = config;
2c0e1515   Goutte   Refactor loading ...
12
      this.active = this.config.active;
b60e7acd   Goutte   Rename "source" i...
13
14
15
    }
    return Target;
  }());
ae0aa7d2   Goutte   Add an x axis lab...
16
  out$.SpaceWeather = SpaceWeather = (function(){
cdf79b23   Goutte   Give the future d...
17
    "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)";
ae0aa7d2   Goutte   Add an x axis lab...
18
    SpaceWeather.displayName = 'SpaceWeather';
243cd8a4   Goutte   Timestamp party c...
19
    var API_TIME_FORMAT, INPUT_TIME_FORMAT, prototype = SpaceWeather.prototype, constructor = SpaceWeather;
2c0e1515   Goutte   Refactor loading ...
20
    API_TIME_FORMAT = "YYYY-MM-DDTHH:mm:ss";
243cd8a4   Goutte   Timestamp party c...
21
    INPUT_TIME_FORMAT = "YYYY-MM-DD";
ae0aa7d2   Goutte   Add an x axis lab...
22
    function SpaceWeather(configuration){
fe3132dd   Goutte   Refactor even more.
23
      var configs, res$, k, this$ = this;
f75faf5f   Goutte   WIP
24
      this.configuration = configuration;
05e269d1   Goutte   Show an error mes...
25
      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");
b60e7acd   Goutte   Rename "source" i...
26
      this.targets = {};
fe3132dd   Goutte   Refactor even more.
27
      res$ = [];
b60e7acd   Goutte   Rename "source" i...
28
29
      for (k in this.configuration.targets) {
        res$.push(this.configuration.targets[k]);
fe3132dd   Goutte   Refactor even more.
30
31
      }
      configs = res$;
b60e7acd   Goutte   Rename "source" i...
32
      configs.forEach(function(target_config){
cad44b6d   Goutte   Finish adding the...
33
        return this$.addTarget(new Target(target_config.slug, target_config.name, target_config));
fe3132dd   Goutte   Refactor even more.
34
      });
b7fe650c   Goutte   Misc bundle of ol...
35
36
37
38
      this.parameters = {};
      this.configuration['parameters'].forEach(function(p){
        return this$.parameters[p['id']] = p;
      });
3c2b15fc   Goutte   Make the Y-Axis o...
39
      this.orbits = null;
80352490   Goutte   Multi model suppo...
40
      this.time_series = [];
ae0aa7d2   Goutte   Add an x axis lab...
41
    }
b60e7acd   Goutte   Rename "source" i...
42
43
    SpaceWeather.prototype.init = function(){
      "This is called by the inline bootstrap javascript code.\nThis ain't in the constructor because it might return a Promise later on.\n(for the loader, for example)";
2c0e1515   Goutte   Refactor loading ...
44
      var started_at, stopped_at, this$ = this;
780828a8   Goutte   Prepare horizonta...
45
46
      started_at = moment().subtract(2, 'month').hours(0).minutes(0).seconds(0);
      stopped_at = moment().add(1, 'month').hours(0).minutes(0).seconds(0);
7994cf1a   Goutte   Hunt bugs.
47
      this.setStartAndStop(started_at, stopped_at);
2c0e1515   Goutte   Refactor loading ...
48
      this.loadAndCreatePlots(started_at, stopped_at);
fb383448   Goutte   Implement the cac...
49
      window.addEventListener('resize', function(){
b60e7acd   Goutte   Rename "source" i...
50
51
        return this$.resize();
      });
fb383448   Goutte   Implement the cac...
52
      return this;
b60e7acd   Goutte   Rename "source" i...
53
54
    };
    SpaceWeather.prototype.buildDataUrlForTarget = function(target_slug, started_at, stopped_at){
f75faf5f   Goutte   WIP
55
56
      var url;
      url = this.configuration['api']['data_for_interval'];
b60e7acd   Goutte   Rename "source" i...
57
      url = url.replace('<target>', target_slug);
a4a9ef03   Goutte   Cache generated C...
58
59
      url = url.replace('<started_at>', started_at);
      url = url.replace('<stopped_at>', stopped_at);
f75faf5f   Goutte   WIP
60
61
      return url;
    };
6b149919   Goutte   Add a Download bu...
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
    SpaceWeather.prototype.buildDownloadUrl = function(){
      var ref$, started_at, stopped_at, targets, t, url;
      ref$ = this.getDomain(), started_at = ref$[0], stopped_at = ref$[1];
      targets = (function(){
        var results$ = [];
        for (t in this.targets) {
          if (this.targets[t].active) {
            results$.push(t);
          }
        }
        return results$;
      }.call(this)).sort().join('-');
      url = this.configuration['api']['download'];
      url = url.replace('<targets>', targets);
      url = url.replace('<started_at>', started_at.format(API_TIME_FORMAT));
      url = url.replace('<stopped_at>', stopped_at.format(API_TIME_FORMAT));
      return url;
    };
cad44b6d   Goutte   Finish adding the...
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
    SpaceWeather.prototype.buildSampUrl = function(){
      var ref$, started_at, stopped_at, targets, t, parameters, p, url;
      ref$ = this.getDomain(), started_at = ref$[0], stopped_at = ref$[1];
      targets = (function(){
        var results$ = [];
        for (t in this.targets) {
          if (this.targets[t].active) {
            results$.push(t);
          }
        }
        return results$;
      }.call(this)).sort().join('-');
      parameters = (function(){
        var results$ = [];
        for (p in this.parameters) {
          if (this.parameters[p].active) {
            results$.push(p);
          }
        }
        return results$;
      }.call(this)).sort().join('-');
      url = this.configuration['api']['samp'];
      url = url.replace('<targets>', targets);
      url = url.replace('<params>', parameters);
      url = url.replace('<started_at>', started_at.format(API_TIME_FORMAT));
      url = url.replace('<stopped_at>', stopped_at.format(API_TIME_FORMAT));
      return url;
    };
    SpaceWeather.prototype.buildSampName = function(){
      var ref$, started_at, stopped_at, targets, t;
      ref$ = this.getDomain(), started_at = ref$[0], stopped_at = ref$[1];
      targets = (function(){
        var i$, ref$, len$, results$ = [];
        for (i$ = 0, len$ = (ref$ = this.getEnabledTargets()).length; i$ < len$; ++i$) {
          t = ref$[i$];
          results$.push(t.name);
        }
        return results$;
      }.call(this)).sort().join(', ');
      return "Heliopropa for " + targets + " from " + started_at.format(API_TIME_FORMAT) + " to " + stopped_at.format(API_TIME_FORMAT) + ".";
    };
b60e7acd   Goutte   Rename "source" i...
121
122
    SpaceWeather.prototype.addTarget = function(target){
      this.targets[target.slug] = target;
f75faf5f   Goutte   WIP
123
124
      return this;
    };
cad44b6d   Goutte   Finish adding the...
125
126
127
128
129
130
131
132
133
134
    SpaceWeather.prototype.getEnabledTargets = function(){
      var slug, ref$, target, results$ = [];
      for (slug in ref$ = this.targets) {
        target = ref$[slug];
        if (target.active) {
          results$.push(target);
        }
      }
      return results$;
    };
6bb225d6   Goutte   Link the time ser...
135
    SpaceWeather.prototype.enableTarget = function(target_slug){
17c52617   Goutte   Make the orbits p...
136
      var ref$, this$ = this;
80352490   Goutte   Multi model suppo...
137
      this.time_series.forEach(function(ts){
b60e7acd   Goutte   Rename "source" i...
138
        if (ts.target.slug === target_slug && this$.parameters[ts.parameter].active) {
6bb225d6   Goutte   Link the time ser...
139
          return ts.show();
4cf497e0   Goutte   Make the targets ...
140
141
        }
      });
b60e7acd   Goutte   Rename "source" i...
142
      this.targets[target_slug].active = true;
17c52617   Goutte   Make the orbits p...
143
144
145
      if ((ref$ = this.orbits) != null) {
        ref$.showOrbiter(target_slug);
      }
f75faf5f   Goutte   WIP
146
147
      return this;
    };
6bb225d6   Goutte   Link the time ser...
148
    SpaceWeather.prototype.disableTarget = function(target_slug){
17c52617   Goutte   Make the orbits p...
149
      var ref$;
80352490   Goutte   Multi model suppo...
150
      this.time_series.forEach(function(ts){
b60e7acd   Goutte   Rename "source" i...
151
        if (ts.target.slug === target_slug) {
6bb225d6   Goutte   Link the time ser...
152
          return ts.hide();
4cf497e0   Goutte   Make the targets ...
153
154
        }
      });
b60e7acd   Goutte   Rename "source" i...
155
      this.targets[target_slug].active = false;
17c52617   Goutte   Make the orbits p...
156
157
158
      if ((ref$ = this.orbits) != null) {
        ref$.hideOrbiter(target_slug);
      }
f75faf5f   Goutte   WIP
159
160
      return this;
    };
fe3132dd   Goutte   Refactor even more.
161
    SpaceWeather.prototype.resize = function(){
a21f81d9   Goutte   Enable Venus and ...
162
      var ref$;
d49a163c   Goutte   Fix the resize an...
163
164
165
      if ((ref$ = this.orbits) != null) {
        ref$.resize();
      }
80352490   Goutte   Multi model suppo...
166
      return this.time_series.forEach(function(ts){
d49a163c   Goutte   Fix the resize an...
167
168
        return ts.resize();
      });
fe3132dd   Goutte   Refactor even more.
169
    };
2c0e1515   Goutte   Refactor loading ...
170
    SpaceWeather.prototype.showLoader = function(){
05e269d1   Goutte   Show an error mes...
171
      return $('#plots_loader').show();
2c0e1515   Goutte   Refactor loading ...
172
    };
9c0c4509   Goutte   Add a loader to t...
173
    SpaceWeather.prototype.hideLoader = function(){
05e269d1   Goutte   Show an error mes...
174
      return $('#plots_loader').hide();
9c0c4509   Goutte   Add a loader to t...
175
    };
b60e7acd   Goutte   Rename "source" i...
176
177
    SpaceWeather.prototype.loadData = function(target_slug, started_at, stopped_at){
      "Load the data as CSV for the specified target and interval,\nand return it in a Promise.";
2c0e1515   Goutte   Refactor loading ...
178
      var sw;
f75faf5f   Goutte   WIP
179
      sw = this;
2c0e1515   Goutte   Refactor loading ...
180
      return new Promise(function(resolve, reject){
f75faf5f   Goutte   WIP
181
        var url;
b60e7acd   Goutte   Rename "source" i...
182
        url = sw.buildDataUrlForTarget(target_slug, started_at, stopped_at);
a4a9ef03   Goutte   Cache generated C...
183
        return d3.csv(url, function(csv){
f75faf5f   Goutte   WIP
184
          var timeFormat, data;
60b73eb1   Goutte   Change temperatur...
185
          console.debug("Requested CSV for " + target_slug + "…", csv);
f75faf5f   Goutte   WIP
186
187
          timeFormat = d3.timeParse('%Y-%m-%dT%H:%M:%S%Z');
          data = {
6491a1f1   Goutte   Fix up the bugs l...
188
            'hee': []
f75faf5f   Goutte   WIP
189
190
191
192
          };
          configuration['parameters'].forEach(function(parameter){
            return data[parameter['id']] = [];
          });
a245c676   Goutte   Commit generated ...
193
          if (!csv) {
80352490   Goutte   Multi model suppo...
194
            reject('invalid');
a245c676   Goutte   Commit generated ...
195
          }
9bfa6c42   Goutte   More bug hunting.
196
          if (!csv.length) {
80352490   Goutte   Multi model suppo...
197
            reject('empty');
9bfa6c42   Goutte   More bug hunting.
198
          }
f75faf5f   Goutte   WIP
199
200
201
202
          csv.forEach(function(d){
            var dtime;
            dtime = timeFormat(d['time']);
            configuration['parameters'].forEach(function(parameter){
d1c44c51   Goutte   Enable Earth
203
              var id, val;
f75faf5f   Goutte   WIP
204
              id = parameter['id'];
d1c44c51   Goutte   Enable Earth
205
206
207
208
209
210
211
              val = parseFloat(d[id]);
              if (!isNaN(val)) {
                return data[id].push({
                  x: dtime,
                  y: val
                });
              }
f75faf5f   Goutte   WIP
212
            });
6491a1f1   Goutte   Fix up the bugs l...
213
214
            if (d['xhee'] && d['yhee']) {
              return data['hee'].push({
f75faf5f   Goutte   WIP
215
                t: dtime,
6491a1f1   Goutte   Fix up the bugs l...
216
217
                x: parseFloat(d['xhee']),
                y: parseFloat(d['yhee'])
f75faf5f   Goutte   WIP
218
219
220
221
222
223
              });
            }
          });
          return resolve(data);
        });
      });
2c0e1515   Goutte   Refactor loading ...
224
225
    };
    SpaceWeather.prototype.loadAndCreatePlots = function(started_at, stopped_at){
80352490   Goutte   Multi model suppo...
226
      "started_at: moment(.js) datetime object\nstopped_at: moment(.js) datetime object";
60b73eb1   Goutte   Change temperatur...
227
      var targets, res$, k, handleTarget, this$ = this;
2c0e1515   Goutte   Refactor loading ...
228
      this.showLoader();
7994cf1a   Goutte   Hunt bugs.
229
230
231
      this.started_at = started_at;
      this.stopped_at = stopped_at;
      this.orbits = new Orbits(this.configuration.orbits_container, this.configuration);
243cd8a4   Goutte   Timestamp party c...
232
233
      started_at = started_at.format(API_TIME_FORMAT);
      stopped_at = stopped_at.format(API_TIME_FORMAT);
60b73eb1   Goutte   Change temperatur...
234
235
236
237
238
239
      res$ = [];
      for (k in this.targets) {
        res$.push(this.targets[k]);
      }
      targets = res$;
      targets.forEach(function(target){
2c0e1515   Goutte   Refactor loading ...
240
        var targetButton;
4900d232   Goutte   Add the time inte...
241
        targetButton = $(".targets-filters .target." + target.slug);
2c0e1515   Goutte   Refactor loading ...
242
        targetButton.addClass('loading');
60b73eb1   Goutte   Change temperatur...
243
244
245
246
247
248
249
250
251
252
        return targetButton.removeClass('failed error empty');
      });
      handleTarget = function(i){
        var target, targetButton;
        if (i >= targets.length) {
          return;
        }
        target = targets[i];
        console.info("Loading CSV data of " + target.name + "…");
        targetButton = $(".targets-filters .target." + target.slug);
2c0e1515   Goutte   Refactor loading ...
253
        return this$.loadData(target.slug, started_at, stopped_at).then(function(data){
9bfa6c42   Goutte   More bug hunting.
254
          console.info("Loaded CSV data of " + target.name + ".", data);
2c0e1515   Goutte   Refactor loading ...
255
          this$.createTimeSeries(target, data);
6491a1f1   Goutte   Fix up the bugs l...
256
          this$.orbits.initOrbiter(target.slug, target.config, data['hee']);
2c0e1515   Goutte   Refactor loading ...
257
          targetButton.removeClass('loading');
5e099488   Goutte   Fix that loading ...
258
          if (target.active) {
60b73eb1   Goutte   Change temperatur...
259
            this$.hideLoader();
5e099488   Goutte   Fix that loading ...
260
          } else {
60b73eb1   Goutte   Change temperatur...
261
            this$.disableTarget(target.slug);
5e099488   Goutte   Fix that loading ...
262
          }
60b73eb1   Goutte   Change temperatur...
263
          return handleTarget(i + 1);
2c0e1515   Goutte   Refactor loading ...
264
        }, function(error){
80352490   Goutte   Multi model suppo...
265
266
267
268
          var msg;
          switch (error) {
          case 'invalid':
            console.error("Failed loading CSV data of " + target.name + ".");
60b73eb1   Goutte   Change temperatur...
269
            targetButton.addClass('error');
80352490   Goutte   Multi model suppo...
270
271
272
273
274
275
276
277
            this$.is_invalid = true;
            break;
          case 'empty':
            msg = "No data for " + target.name + "\n during interval from \n" + started_at + " to " + stopped_at + ".";
            console.warn(msg);
            targetButton.addClass('empty');
            break;
          }
27097d87   Goutte   Change the error ...
278
          targetButton.addClass('failed');
9bfa6c42   Goutte   More bug hunting.
279
          targetButton.removeClass('loading');
60b73eb1   Goutte   Change temperatur...
280
281
          this$.hideLoader();
          return handleTarget(i + 1);
2c0e1515   Goutte   Refactor loading ...
282
        });
60b73eb1   Goutte   Change temperatur...
283
284
285
      };
      handleTarget(0);
      return this;
2c0e1515   Goutte   Refactor loading ...
286
287
    };
    SpaceWeather.prototype.clearPlots = function(){
2c0e1515   Goutte   Refactor loading ...
288
      this.orbits.clear();
80352490   Goutte   Multi model suppo...
289
      this.time_series.forEach(function(ts){
2c0e1515   Goutte   Refactor loading ...
290
291
292
        return ts.clear();
      });
      this.orbits = null;
80352490   Goutte   Multi model suppo...
293
      this.time_series = [];
2c0e1515   Goutte   Refactor loading ...
294
      return this;
f75faf5f   Goutte   WIP
295
    };
b60e7acd   Goutte   Rename "source" i...
296
    SpaceWeather.prototype.createTimeSeries = function(target, data){
7d6dee0f   Goutte   Continue refacto ...
297
      var this$ = this;
4816cef4   Goutte   Refactor some more.
298
299
      this.configuration['parameters'].forEach(function(parameter){
        var container, id, title;
b7fe650c   Goutte   Misc bundle of ol...
300
        container = this$.configuration['time_series_container'];
4816cef4   Goutte   Refactor some more.
301
302
303
304
305
        id = parameter['id'];
        title = parameter['title'];
        if (!(id in data)) {
          console.error("No data for id '" + id + "'.", data);
        }
d1c44c51   Goutte   Enable Earth
306
307
308
309
310
311
312
        console.log(target['name'], id, data[id]);
        if (data[id].length) {
          return this$.time_series.push(new TimeSeries(id, title, target, data[id], this$.parameters[id].active, container, {
            'started_at': this$.started_at,
            'stopped_at': this$.stopped_at
          }));
        }
4816cef4   Goutte   Refactor some more.
313
      });
80352490   Goutte   Multi model suppo...
314
      this.time_series.forEach(function(ts){
4816cef4   Goutte   Refactor some more.
315
        ts.options['onMouseOver'] = function(){
9c0c4509   Goutte   Add a loader to t...
316
          return true;
4816cef4   Goutte   Refactor some more.
317
318
        };
        ts.options['onMouseOut'] = function(){
80352490   Goutte   Multi model suppo...
319
          this$.time_series.forEach(function(ts2){
4816cef4   Goutte   Refactor some more.
320
321
            return ts2.hideCursor();
          });
9c0c4509   Goutte   Add a loader to t...
322
          return true;
4816cef4   Goutte   Refactor some more.
323
        };
c3008fb2   Goutte   Clean up and refa...
324
        ts.options['onMouseMove'] = function(t){
fe3132dd   Goutte   Refactor even more.
325
          var ref$;
80352490   Goutte   Multi model suppo...
326
          this$.time_series.forEach(function(ts2){
4816cef4   Goutte   Refactor some more.
327
328
            return ts2.moveCursor(t);
          });
9c0c4509   Goutte   Add a loader to t...
329
330
331
332
          if ((ref$ = this$.orbits) != null) {
            ref$.moveToDate(t);
          }
          return true;
4816cef4   Goutte   Refactor some more.
333
        };
c3008fb2   Goutte   Clean up and refa...
334
        ts.options['onBrushEnd'] = function(sta, sto){
243cd8a4   Goutte   Timestamp party c...
335
          this$.resizeDomain(moment(sta), moment(sto));
9c0c4509   Goutte   Add a loader to t...
336
          return true;
c3008fb2   Goutte   Clean up and refa...
337
338
        };
        return ts.options['onDblClick'] = function(){
9c0c4509   Goutte   Add a loader to t...
339
340
341
342
343
344
          var ref$;
          this$.resetZoom();
          if ((ref$ = $("#zoom_controls_help")) != null) {
            ref$.remove();
          }
          return true;
c3008fb2   Goutte   Clean up and refa...
345
        };
4816cef4   Goutte   Refactor some more.
346
      });
80352490   Goutte   Multi model suppo...
347
      return this.time_series;
4816cef4   Goutte   Refactor some more.
348
    };
cad44b6d   Goutte   Finish adding the...
349
350
351
352
353
354
355
356
357
358
359
    SpaceWeather.prototype.getEnabledParameters = function(){
      var i$, ref$, len$, p, slug, results$ = [];
      for (i$ = 0, len$ = (ref$ = this.parameters).length; i$ < len$; ++i$) {
        p = i$;
        slug = ref$[i$];
        if (p.active) {
          results$.push(p);
        }
      }
      return results$;
    };
b7fe650c   Goutte   Misc bundle of ol...
360
    SpaceWeather.prototype.enableParameter = function(parameter_slug){
4cf497e0   Goutte   Make the targets ...
361
      var this$ = this;
b7fe650c   Goutte   Misc bundle of ol...
362
363
364
      if (!(parameter_slug in this.parameters)) {
        console.error("Unknown parameter " + parameter_slug + ".");
      }
b7fe650c   Goutte   Misc bundle of ol...
365
      this.parameters[parameter_slug].active = true;
80352490   Goutte   Multi model suppo...
366
      this.time_series.forEach(function(ts){
b60e7acd   Goutte   Rename "source" i...
367
        if (ts.parameter === parameter_slug && this$.targets[ts.target.slug].active) {
6bb225d6   Goutte   Link the time ser...
368
          return ts.show();
4cf497e0   Goutte   Make the targets ...
369
370
        }
      });
b7fe650c   Goutte   Misc bundle of ol...
371
372
373
374
375
376
      return this;
    };
    SpaceWeather.prototype.disableParameter = function(parameter_slug){
      if (!(parameter_slug in this.parameters)) {
        console.error("Unknown parameter " + parameter_slug + ".");
      }
b7fe650c   Goutte   Misc bundle of ol...
377
      this.parameters[parameter_slug].active = false;
80352490   Goutte   Multi model suppo...
378
      this.time_series.forEach(function(ts){
4cf497e0   Goutte   Make the targets ...
379
        if (ts.parameter === parameter_slug) {
6bb225d6   Goutte   Link the time ser...
380
          return ts.hide();
4cf497e0   Goutte   Make the targets ...
381
382
        }
      });
b7fe650c   Goutte   Misc bundle of ol...
383
384
      return this;
    };
a06a0a67   Goutte   Prepare the time ...
385
386
387
388
389
390
    SpaceWeather.prototype.getDomain = function(){
      if (this.current_started_at != null && this.current_stopped_at != null) {
        return [this.current_started_at, this.current_stopped_at];
      }
      return [this.started_at, this.stopped_at];
    };
8cb213b9   Goutte   Clean up, and pre...
391
    SpaceWeather.prototype.resizeDomain = function(started_at, stopped_at){
1754789b   Goutte   Decorate and clea...
392
      var ref$, max_stopped_at, formatted_started_at, formatted_stopped_at;
8cb213b9   Goutte   Clean up, and pre...
393
      if (stopped_at < started_at) {
7994cf1a   Goutte   Hunt bugs.
394
        ref$ = [stopped_at, started_at], started_at = ref$[0], stopped_at = ref$[1];
8cb213b9   Goutte   Clean up, and pre...
395
396
      }
      if (started_at === stopped_at) {
6bb225d6   Goutte   Link the time ser...
397
        console.warn("Please provide distinct start and stop dates.");
8cb213b9   Goutte   Clean up, and pre...
398
399
        return;
      }
1754789b   Goutte   Decorate and clea...
400
401
402
403
404
      max_stopped_at = started_at.clone().add(2, 'years');
      if (stopped_at > max_stopped_at) {
        console.warn("The time interval was truncated beacuse it was bigger than two years.");
        stopped_at = max_stopped_at;
      }
243cd8a4   Goutte   Timestamp party c...
405
406
407
      this.setStartAndStop(started_at, stopped_at);
      formatted_started_at = started_at.format();
      formatted_stopped_at = stopped_at.format();
5ef50583   Goutte   Clean up.
408
      if ((this.started_at <= started_at && started_at <= this.stopped_at) && (this.started_at <= stopped_at && stopped_at <= this.stopped_at)) {
243cd8a4   Goutte   Timestamp party c...
409
        console.info("Resizing the temporal domain from " + formatted_started_at + " to " + formatted_stopped_at + " without fetching new data…");
80352490   Goutte   Multi model suppo...
410
        this.time_series.forEach(function(ts){
6bb225d6   Goutte   Link the time ser...
411
412
413
414
          if (!ts.visible) {
            return ts.zoomIn(started_at, stopped_at);
          }
        });
80352490   Goutte   Multi model suppo...
415
        this.time_series.forEach(function(ts){
6bb225d6   Goutte   Link the time ser...
416
417
418
          if (ts.visible) {
            return ts.zoomIn(started_at, stopped_at);
          }
8cb213b9   Goutte   Clean up, and pre...
419
420
        });
        this.orbits.resizeDomain(started_at, stopped_at);
243cd8a4   Goutte   Timestamp party c...
421
      } else {
80352490   Goutte   Multi model suppo...
422
        this.is_invalid = false;
243cd8a4   Goutte   Timestamp party c...
423
424
425
426
        console.info("Resizing the temporal domain from " + formatted_started_at + " to " + formatted_stopped_at + " and fetching new data…");
        console.warn("This might take a while… Why not see what else we're up to on http://cdpp.eu while you're waiting?");
        this.clearPlots();
        this.loadAndCreatePlots(started_at, stopped_at);
8cb213b9   Goutte   Clean up, and pre...
427
      }
243cd8a4   Goutte   Timestamp party c...
428
      return this;
8cb213b9   Goutte   Clean up, and pre...
429
    };
6bb225d6   Goutte   Link the time ser...
430
    SpaceWeather.prototype.resetZoom = function(){
80352490   Goutte   Multi model suppo...
431
      this.time_series.forEach(function(ts){
6bb225d6   Goutte   Link the time ser...
432
433
        return ts.resetZoom();
      });
243cd8a4   Goutte   Timestamp party c...
434
435
436
437
438
      this.orbits.resetZoom();
      this.setStartAndStop(this.started_at, this.stopped_at);
      return this;
    };
    SpaceWeather.prototype.setStartAndStop = function(started_at, stopped_at){
7994cf1a   Goutte   Hunt bugs.
439
      console.info("Setting time interval from " + started_at + " to " + stopped_at + "…");
243cd8a4   Goutte   Timestamp party c...
440
441
442
443
444
      this.current_started_at = started_at;
      this.current_stopped_at = stopped_at;
      $("#started_at").val(started_at.format(INPUT_TIME_FORMAT));
      $("#stopped_at").val(stopped_at.format(INPUT_TIME_FORMAT));
      return this;
6bb225d6   Goutte   Link the time ser...
445
    };
ae0aa7d2   Goutte   Add an x axis lab...
446
447
    return SpaceWeather;
  }());
438929a4   Goutte   Rewrite the orbit...
448
449
  out$.TimeSeries = TimeSeries = (function(){
    TimeSeries.displayName = 'TimeSeries';
123313cb   Goutte   Clip the paths of...
450
    var RATIO, prototype = TimeSeries.prototype, constructor = TimeSeries;
c3008fb2   Goutte   Clean up and refa...
451
    function TimeSeries(parameter, title, target, data, visible, container, options){
cdf79b23   Goutte   Give the future d...
452
      var now, res$, i$, ref$, len$, d;
4cf497e0   Goutte   Make the targets ...
453
      this.parameter = parameter;
438929a4   Goutte   Rewrite the orbit...
454
      this.title = title;
b60e7acd   Goutte   Rename "source" i...
455
      this.target = target;
7b5642ae   Goutte   Normalize time in...
456
      this.data = data;
6bb225d6   Goutte   Link the time ser...
457
      this.visible = visible;
438929a4   Goutte   Rewrite the orbit...
458
459
460
461
      this.container = container;
      this.options = options != null
        ? options
        : {};
08569a6b   Goutte   Add a zooming bru...
462
      this.onBrushEnd = bind$(this, 'onBrushEnd', prototype);
2038c9fb   Goutte   Add a zoom reset ...
463
      this.onDoubleClick = bind$(this, 'onDoubleClick', prototype);
541e2936   Goutte   Synchronize the t...
464
465
466
      this.onMouseOut = bind$(this, 'onMouseOut', prototype);
      this.onMouseOver = bind$(this, 'onMouseOver', prototype);
      this.onMouseMove = bind$(this, 'onMouseMove', prototype);
cdf79b23   Goutte   Give the future d...
467
468
469
470
471
472
473
474
475
      now = moment();
      res$ = [];
      for (i$ = 0, len$ = (ref$ = this.data).length; i$ < len$; ++i$) {
        d = ref$[i$];
        if (moment(d.x) >= now) {
          res$.push(d);
        }
      }
      this.predictiveData = res$;
438929a4   Goutte   Rewrite the orbit...
476
477
      this.init();
    }
2038c9fb   Goutte   Add a zoom reset ...
478
479
480
    TimeSeries.prototype.toString = function(){
      return this.title + " of " + this.target.name;
    };
7b5642ae   Goutte   Normalize time in...
481
    TimeSeries.prototype.init = function(){
780828a8   Goutte   Prepare horizonta...
482
      var formatMillisecond, formatSecond, formatMinute, formatHour, formatDay, formatWeek, formatMonth, formatYear, multiFormat, clipId, i$, ref$, len$, line, lineElement, dx, this$ = this;
7b5642ae   Goutte   Normalize time in...
483
      console.info("Initializing plot of " + this + "…");
6bb225d6   Goutte   Link the time ser...
484
485
486
      this.xDataExtent = d3.extent(this.data, function(d){
        return d.x;
      });
7b5642ae   Goutte   Normalize time in...
487
      this.yDataExtent = d3.extent(this.data, function(d){
6bb225d6   Goutte   Link the time ser...
488
489
        return d.y;
      });
7b5642ae   Goutte   Normalize time in...
490
491
492
493
494
495
      if (this.options['started_at']) {
        this.xDataExtent[0] = this.options['started_at'];
      }
      if (this.options['stopped_at']) {
        this.xDataExtent[1] = this.options['stopped_at'];
      }
438929a4   Goutte   Rewrite the orbit...
496
497
498
499
      this.margin = {
        top: 30,
        right: 20,
        bottom: 30,
fe3132dd   Goutte   Refactor even more.
500
        left: 80
438929a4   Goutte   Rewrite the orbit...
501
      };
6bb225d6   Goutte   Link the time ser...
502
503
      this.xScale = d3.scaleTime().domain(this.xDataExtent);
      this.yScale = d3.scaleLinear().domain(this.yDataExtent);
6491a1f1   Goutte   Fix up the bugs l...
504
505
      formatMillisecond = d3.timeFormat(".%L");
      formatSecond = d3.timeFormat(":%S");
32a46001   Goutte   Set time format t...
506
507
      formatMinute = d3.timeFormat("%H:%M");
      formatHour = d3.timeFormat("%H:%M");
6491a1f1   Goutte   Fix up the bugs l...
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
      formatDay = d3.timeFormat("%a %d");
      formatWeek = d3.timeFormat("%b %d");
      formatMonth = d3.timeFormat("%B");
      formatYear = d3.timeFormat("%Y");
      multiFormat = function(date){
        if (date > d3.timeSecond(date)) {
          return formatMillisecond(date);
        }
        if (date > d3.timeMinute(date)) {
          return formatSecond(date);
        }
        if (date > d3.timeHour(date)) {
          return formatMinute(date);
        }
        if (date > d3.timeDay(date)) {
          return formatHour(date);
        }
        if (date > d3.timeMonth(date)) {
          if (date > d3.timeWeek(date)) {
            return formatDay(date);
          } else {
            return formatWeek(date);
          }
        }
        if (date > d3.timeYear(date)) {
          return formatMonth(date);
        }
        return formatYear(date);
      };
      this.xAxis = d3.axisBottom().tickFormat(multiFormat).ticks(7);
438929a4   Goutte   Rewrite the orbit...
538
539
540
541
542
543
544
      this.yAxis = d3.axisLeft().ticks(10);
      this.line = d3.line().x(function(d){
        return this$.xScale(d.x);
      }).y(function(d){
        return this$.yScale(d.y);
      });
      this.svg = d3.select(this.container).append('svg');
b60e7acd   Goutte   Rename "source" i...
545
      this.svg.attr("class", this.parameter + " " + this.target.slug);
438929a4   Goutte   Rewrite the orbit...
546
547
      this.plotWrapper = this.svg.append('g');
      this.plotWrapper.attr('transform', 'translate(' + this.margin.left + ',' + this.margin.top + ')');
123313cb   Goutte   Clip the paths of...
548
549
550
551
552
      clipId = "ts-clip-" + this.parameter + "-" + this.target.slug;
      this.clip = this.svg.append("defs").append("svg:clipPath").attr("id", clipId).append("svg:rect").attr("x", 0).attr("y", 0);
      this.pathWrapper = this.plotWrapper.append('g');
      this.pathWrapper.attr("clip-path", "url(#" + clipId + ")");
      this.path = this.pathWrapper.append('path').datum(this.data).classed('line', true);
cdf79b23   Goutte   Give the future d...
553
      this.predictiveDataPath = this.pathWrapper.append('path').datum(this.predictiveData).classed('predictive-line', true);
780828a8   Goutte   Prepare horizonta...
554
555
556
557
558
559
560
561
562
563
564
      this.horizontalLines = [];
      if (this.options['horizontalLines']) {
        for (i$ = 0, len$ = (ref$ = this.options['horizontalLines']).length; i$ < len$; ++i$) {
          line = ref$[i$];
          lineElement = this.svg.append("line").attr("class", "line horitonal-line").style("stroke", "orange").style("stroke-dasharray", "3, 2");
          this.horizontalLines.push({
            'element': lineElement,
            'config': line
          });
        }
      }
08569a6b   Goutte   Add a zooming bru...
565
566
      this.brush = this.plotWrapper.append("g").attr("class", "brush");
      this.mouseCanvas = this.plotWrapper.append("rect").style("fill", "none");
438929a4   Goutte   Rewrite the orbit...
567
568
569
      this.plotWrapper.append('g').classed('x axis', true);
      this.plotWrapper.append('g').classed('y axis', true);
      this.yAxisText = this.plotWrapper.append("text").attr("transform", "rotate(-90)").attr("dy", "1em").style("text-anchor", "middle").text(this.title);
b60e7acd   Goutte   Rename "source" i...
570
      this.yAxisTextTarget = this.plotWrapper.append("text").attr("transform", "rotate(-90)").attr("dy", "1em").style("text-anchor", "middle").style("font-style", "oblique").text(this.target.name);
81c9b2e8   Goutte   Add the values to...
571
572
      this.focus = this.plotWrapper.append('g').style("display", "none");
      this.cursorCircle = this.focus.append("circle").attr("class", "cursor-circle").attr("r", 3);
541e2936   Goutte   Synchronize the t...
573
574
575
576
577
      dx = 8;
      this.cursorValueShadow = this.focus.append("text").attr("class", "cursor-text cursor-text-shadow").attr("dx", dx).attr("dy", "-.3em");
      this.cursorValue = this.focus.append("text").attr("class", "cursor-text cursor-value").attr("dx", dx).attr("dy", "-.3em");
      this.cursorDateShadow = this.focus.append("text").attr("class", "cursor-text cursor-text-shadow").attr("dx", dx).attr("dy", "1em");
      this.cursorDate = this.focus.append("text").attr("class", "cursor-text cursor-date").attr("dx", dx).attr("dy", "1em");
438929a4   Goutte   Rewrite the orbit...
578
579
      return this.resize();
    };
123313cb   Goutte   Clip the paths of...
580
    RATIO = GOLDEN_RATIO * GOLDEN_RATIO * GOLDEN_RATIO * GOLDEN_RATIO;
438929a4   Goutte   Rewrite the orbit...
581
    TimeSeries.prototype.resize = function(){
780828a8   Goutte   Prepare horizonta...
582
      var width, height, i$, ref$, len$, line, lineValue;
123313cb   Goutte   Clip the paths of...
583
584
      width = Math.ceil($(this.container).width() - this.margin.left - this.margin.right);
      height = Math.ceil(RATIO * width);
541e2936   Goutte   Synchronize the t...
585
586
      this.plotWidth = width;
      this.plotHeight = height;
123313cb   Goutte   Clip the paths of...
587
      console.debug("Resizing " + this + ": " + width + " x " + height + "…");
438929a4   Goutte   Rewrite the orbit...
588
589
590
      this.xScale.range([0, width]);
      this.yScale.range([height, 0]);
      this.svg.attr('width', width + this.margin.right + this.margin.left).attr('height', height + this.margin.top + this.margin.bottom);
123313cb   Goutte   Clip the paths of...
591
      this.clip.attr("width", width).attr("height", height);
438929a4   Goutte   Rewrite the orbit...
592
      this.path.attr('d', this.line);
cdf79b23   Goutte   Give the future d...
593
      this.predictiveDataPath.attr('d', this.line);
780828a8   Goutte   Prepare horizonta...
594
595
596
597
598
      for (i$ = 0, len$ = (ref$ = this.horizontalLines).length; i$ < len$; ++i$) {
        line = ref$[i$];
        lineValue = this.yScale(line['config']['value']) + this.margin.top;
        line['element'].attr("x1", this.margin.left).attr("y1", lineValue).attr("x2", this.margin.left + width).attr("y2", lineValue);
      }
438929a4   Goutte   Rewrite the orbit...
599
600
      this.xAxis.scale(this.xScale);
      this.yAxis.scale(this.yScale);
034132c6   Goutte   More changes from...
601
      this.xAxis.ticks(Math.floor(width / 80.0));
d49a163c   Goutte   Fix the resize an...
602
      this.yAxis.ticks(Math.floor(height / 18.0));
438929a4   Goutte   Rewrite the orbit...
603
604
      this.svg.select('.x.axis').attr('transform', 'translate(0,' + height + ')').call(this.xAxis);
      this.svg.select('.y.axis').call(this.yAxis);
fe3132dd   Goutte   Refactor even more.
605
      this.yAxisText.attr("y", 20 - this.margin.left).attr("x", 0 - height / 2);
b60e7acd   Goutte   Rename "source" i...
606
      this.yAxisTextTarget.attr("y", 0 - this.margin.left).attr("x", 0 - height / 2);
2463bd16   Goutte   Add a circle foll...
607
      this.mouseCanvas.attr("width", width).attr("height", height);
08569a6b   Goutte   Add a zooming bru...
608
      if (this.brushFunction == null) {
c3008fb2   Goutte   Clean up and refa...
609
        console.debug("Creating the zooming brush for " + this + "…");
08569a6b   Goutte   Add a zooming bru...
610
611
        this.brushFunction = d3.brushX().extent([[0, 0], [width, height]]).handleSize(0).on("end", this.onBrushEnd);
        this.brush.call(this.brushFunction);
c3008fb2   Goutte   Clean up and refa...
612
        this.svg.select(".brush .overlay").on("mouseover.swapp", this.onMouseOver).on("mouseout.swapp", this.onMouseOut).on("mousemove.swapp", this.onMouseMove).on("dblclick.swapp", this.onDoubleClick);
08569a6b   Goutte   Add a zooming bru...
613
      }
6bb225d6   Goutte   Link the time ser...
614
615
      if (!this.visible) {
        this.hide();
b7fe650c   Goutte   Misc bundle of ol...
616
      }
2463bd16   Goutte   Add a circle foll...
617
618
      return this;
    };
2c0e1515   Goutte   Refactor loading ...
619
620
621
622
    TimeSeries.prototype.clear = function(){
      $(this.svg.node()).remove();
      return this.visible = false;
    };
6bb225d6   Goutte   Link the time ser...
623
624
625
626
627
628
629
630
    TimeSeries.prototype.show = function(){
      $(this.svg.node()).show();
      return this.visible = true;
    };
    TimeSeries.prototype.hide = function(){
      $(this.svg.node()).hide();
      return this.visible = false;
    };
541e2936   Goutte   Synchronize the t...
631
632
633
634
    TimeSeries.prototype.onMouseMove = function(){
      var x;
      x = this.xScale.invert(d3.mouse(this.mouseCanvas.node())[0]);
      if (this.options.onMouseMove != null) {
2038c9fb   Goutte   Add a zoom reset ...
635
        return this.options.onMouseMove(x);
541e2936   Goutte   Synchronize the t...
636
      } else {
2038c9fb   Goutte   Add a zoom reset ...
637
        return this.moveCursor(x);
541e2936   Goutte   Synchronize the t...
638
      }
541e2936   Goutte   Synchronize the t...
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
    };
    TimeSeries.prototype.onMouseOver = function(){
      if (this.options.onMouseOver != null) {
        return this.options.onMouseOver();
      } else {
        return this.showCursor();
      }
    };
    TimeSeries.prototype.onMouseOut = function(){
      if (this.options.onMouseOut != null) {
        return this.options.onMouseOut();
      } else {
        return this.hideCursor();
      }
    };
2038c9fb   Goutte   Add a zoom reset ...
654
    TimeSeries.prototype.onDoubleClick = function(){
c3008fb2   Goutte   Clean up and refa...
655
656
657
658
659
      if (this.options.onDblClick != null) {
        return this.options.onDblClick();
      } else {
        return this.resetZoom();
      }
2038c9fb   Goutte   Add a zoom reset ...
660
    };
08569a6b   Goutte   Add a zooming bru...
661
662
663
    TimeSeries.prototype.onBrushEnd = function(){
      var s, minmax;
      s = d3.event.selection;
08569a6b   Goutte   Add a zooming bru...
664
665
      if (s) {
        minmax = [s[0], s[1]].map(this.xScale.invert, this.xScale);
08569a6b   Goutte   Add a zooming bru...
666
        this.brush.call(this.brushFunction.move, null);
c3008fb2   Goutte   Clean up and refa...
667
668
669
670
671
        if (this.options.onBrushEnd != null) {
          return this.options.onBrushEnd(minmax[0], minmax[1]);
        } else {
          return this.zoomIn(minmax[0], minmax[1]);
        }
2038c9fb   Goutte   Add a zoom reset ...
672
673
674
675
676
      }
    };
    TimeSeries.prototype.zoomIn = function(startDate, stopDate){
      var ref$, minDate, maxDate;
      console.debug("Zooming in " + this + " from " + startDate + " to " + stopDate + ".");
6bb225d6   Goutte   Link the time ser...
677
      ref$ = this.xDataExtent, minDate = ref$[0], maxDate = ref$[1];
2038c9fb   Goutte   Add a zoom reset ...
678
679
680
681
682
      if (startDate < minDate) {
        startDate = minDate;
      }
      if (stopDate > maxDate) {
        stopDate = maxDate;
08569a6b   Goutte   Add a zooming bru...
683
      }
2038c9fb   Goutte   Add a zoom reset ...
684
      this.xScale.domain([startDate, stopDate]);
2d8a5753   Goutte   Make the Y-Axis o...
685
686
687
688
689
690
691
692
      this.yScale.domain(d3.extent(this.data, function(d){
        var ref$;
        if (startDate <= (ref$ = d.x) && ref$ <= stopDate) {
          return d.y;
        } else {
          return 0;
        }
      }));
2038c9fb   Goutte   Add a zoom reset ...
693
694
695
      return this.applyZoom();
    };
    TimeSeries.prototype.resetZoom = function(){
6bb225d6   Goutte   Link the time ser...
696
697
      this.xScale.domain(this.xDataExtent);
      this.yScale.domain(this.yDataExtent);
2038c9fb   Goutte   Add a zoom reset ...
698
      return this.applyZoom();
08569a6b   Goutte   Add a zooming bru...
699
    };
2038c9fb   Goutte   Add a zoom reset ...
700
    TimeSeries.prototype.applyZoom = function(){
08569a6b   Goutte   Add a zooming bru...
701
      var t;
6bb225d6   Goutte   Link the time ser...
702
      if (this.visible) {
c3008fb2   Goutte   Clean up and refa...
703
        console.debug("Applying zoom to visible " + this + "…");
6bb225d6   Goutte   Link the time ser...
704
705
706
        t = this.svg.transition().duration(750);
        this.svg.select('.x.axis').transition(t).call(this.xAxis);
        this.svg.select('.y.axis').transition(t).call(this.yAxis);
cdf79b23   Goutte   Give the future d...
707
708
        this.path.transition(t).attr('d', this.line);
        return this.predictiveDataPath.transition(t).attr('d', this.line);
6bb225d6   Goutte   Link the time ser...
709
      } else {
c3008fb2   Goutte   Clean up and refa...
710
        console.debug("Applying zoom to hidden " + this + "…");
6bb225d6   Goutte   Link the time ser...
711
712
        this.svg.select('.x.axis').call(this.xAxis);
        this.svg.select('.y.axis').call(this.yAxis);
cdf79b23   Goutte   Give the future d...
713
714
        this.path.attr('d', this.line);
        return this.predictiveDataPath.attr('d', this.line);
6bb225d6   Goutte   Link the time ser...
715
      }
08569a6b   Goutte   Add a zooming bru...
716
    };
541e2936   Goutte   Synchronize the t...
717
718
719
720
721
722
    TimeSeries.prototype.showCursor = function(){
      return this.focus.style("display", null);
    };
    TimeSeries.prototype.hideCursor = function(){
      return this.focus.style("display", "none");
    };
2463bd16   Goutte   Add a circle foll...
723
724
725
    TimeSeries.prototype.bisectDate = d3.bisector(function(d){
      return d.x;
    }).left;
6491a1f1   Goutte   Fix up the bugs l...
726
    TimeSeries.prototype.timeFormat = d3.timeFormat("%Y-%m-%d %H:%M");
541e2936   Goutte   Synchronize the t...
727
    TimeSeries.prototype.moveCursor = function(x0){
8cb213b9   Goutte   Clean up, and pre...
728
      var i, d0, d1, d, xx, yy, mirrored, dx, transform;
2463bd16   Goutte   Add a circle foll...
729
730
731
      i = this.bisectDate(this.data, x0, 1);
      d0 = this.data[i - 1];
      d1 = this.data[i];
b4abddb7   Goutte   Fix a small issue...
732
733
      if (!d1 || !d0) {
        this.hideCursor();
541e2936   Goutte   Synchronize the t...
734
735
        return;
      }
2463bd16   Goutte   Add a circle foll...
736
737
738
      d = x0 - d0.x > d1.x - x0 ? d1 : d0;
      xx = this.xScale(d.x);
      yy = this.yScale(d.y);
541e2936   Goutte   Synchronize the t...
739
740
741
742
743
      mirrored = this.plotWidth != null && xx > this.plotWidth / 2 ? true : false;
      dx = 8;
      if (mirrored) {
        dx = -1 * dx;
      }
8cb213b9   Goutte   Clean up, and pre...
744
      transform = "translate(" + xx + ", " + yy + ")";
81c9b2e8   Goutte   Add the values to...
745
      this.cursorCircle.attr("transform", transform);
541e2936   Goutte   Synchronize the t...
746
747
748
749
      this.cursorValue.attr("transform", transform).text(d.y).attr('text-anchor', mirrored ? 'end' : 'start').attr("dx", dx);
      this.cursorValueShadow.attr("transform", transform).text(d.y).attr('text-anchor', mirrored ? 'end' : 'start').attr("dx", dx);
      this.cursorDate.attr("transform", transform).text(this.timeFormat(d.x)).attr('text-anchor', mirrored ? 'end' : 'start').attr("dx", dx);
      this.cursorDateShadow.attr("transform", transform).text(this.timeFormat(d.x)).attr('text-anchor', mirrored ? 'end' : 'start').attr("dx", dx);
b4abddb7   Goutte   Fix a small issue...
750
      this.showCursor();
438929a4   Goutte   Rewrite the orbit...
751
752
753
754
755
      return this;
    };
    return TimeSeries;
  }());
  out$.Orbits = Orbits = (function(){
8cb213b9   Goutte   Clean up, and pre...
756
    "View of the solar system from above, with orbits segments for selected time\ninterval, from real data.";
438929a4   Goutte   Rewrite the orbit...
757
758
    Orbits.displayName = 'Orbits';
    var prototype = Orbits.prototype, constructor = Orbits;
a21f81d9   Goutte   Enable Venus and ...
759
    function Orbits(container, options){
438929a4   Goutte   Rewrite the orbit...
760
761
762
763
      this.container = container;
      this.options = options != null
        ? options
        : {};
438929a4   Goutte   Rewrite the orbit...
764
765
766
      this.init();
    }
    Orbits.prototype.init = function(){
c3008fb2   Goutte   Clean up and refa...
767
      console.log("Initializing plot of orbits…");
438929a4   Goutte   Rewrite the orbit...
768
769
770
      this.margin = {
        top: 30,
        right: 20,
11662eed   Goutte   Add Y axis label ...
771
        bottom: 42,
438929a4   Goutte   Rewrite the orbit...
772
773
        left: 60
      };
a21f81d9   Goutte   Enable Venus and ...
774
775
      this.data = {};
      this.orbiters = {};
7994cf1a   Goutte   Hunt bugs.
776
      this.orbitersElements = {};
17c52617   Goutte   Make the orbits p...
777
778
779
780
      this.orbitersExtrema = {};
      this.lastOrbiterData = {};
      this.xScale = d3.scaleLinear().domain([-1, 1]);
      this.yScale = d3.scaleLinear().domain([1, -1]);
438929a4   Goutte   Rewrite the orbit...
781
782
783
784
785
      this.xAxis = d3.axisBottom().ticks(10);
      this.yAxis = d3.axisLeft().ticks(10);
      this.svg = d3.select(this.container).append('svg');
      this.plotWrapper = this.svg.append('g');
      this.plotWrapper.attr('transform', 'translate(' + this.margin.left + ',' + this.margin.top + ')');
ae0aa7d2   Goutte   Add an x axis lab...
786
787
788
789
      this.xAxisLine = this.plotWrapper.append('g').classed('x axis', true);
      this.yAxisLine = this.plotWrapper.append('g').classed('y axis', true);
      this.xAxisTitle = this.xAxisLine.append('text').attr('fill', '#000');
      this.xAxisTitle.style("text-anchor", "middle");
b500e561   Goutte   Invert the orbits...
790
      this.xAxisTitle.append('tspan').text('Y');
11662eed   Goutte   Add Y axis label ...
791
      this.xAxisTitle.append('tspan').attr('dy', '3px').text('HEE').attr('font-size', '8px');
ae0aa7d2   Goutte   Add an x axis lab...
792
      this.xAxisTitle.append('tspan').attr('dy', '-3px').text('   (AU)');
11662eed   Goutte   Add Y axis label ...
793
794
      this.yAxisTitle = this.yAxisLine.append('text').attr('fill', '#000');
      this.yAxisTitle.style("text-anchor", "middle");
b500e561   Goutte   Invert the orbits...
795
      this.yAxisTitle.append('tspan').text('X');
11662eed   Goutte   Add Y axis label ...
796
797
798
      this.yAxisTitle.append('tspan').attr('dy', '3px').text('HEE').attr('font-size', '8px');
      this.yAxisTitle.append('tspan').attr('dy', '-3px').text('   (AU)');
      this.yAxisTitle.attr('transform', 'rotate(-90)');
8bd715ad   Goutte   Use a pixel art i...
799
      this.sun = this.plotWrapper.append("svg:image").attr('xlink:href', this.options.sun.img).attr('width', '32px').attr('height', '32px');
6491a1f1   Goutte   Fix up the bugs l...
800
      this.sun.append('svg:title').text("Sun");
3ee0b596   Goutte   Fix an annoying b...
801
      $(this.svg.node()).hide();
438929a4   Goutte   Rewrite the orbit...
802
803
      return this.resize();
    };
a21f81d9   Goutte   Enable Venus and ...
804
    Orbits.prototype.initOrbiter = function(slug, config, data){
438929a4   Goutte   Rewrite the orbit...
805
      var orbit_ellipse, orbiter, orbit_line, orbit_section, this$ = this;
dc0be992   Goutte   Support having no...
806
807
808
809
810
811
812
813
      this.data[slug] = data;
      this.orbiters[slug] = config;
      if (data.length) {
        console.info("Initializing orbit of " + config.name + "…");
      } else {
        console.warn("No orbit data for " + config.name + "…");
        return;
      }
438929a4   Goutte   Rewrite the orbit...
814
815
816
      if (slug in this.orbitersElements) {
        throw new Error("Second init of " + slug);
      }
438929a4   Goutte   Rewrite the orbit...
817
818
      orbit_ellipse = this.plotWrapper.append("svg:ellipse").classed('orbit orbit_ellipse', true);
      orbiter = this.plotWrapper.append("svg:image").attr('xlink:href', config['img']).attr('width', '32px').attr('height', '32px');
6491a1f1   Goutte   Fix up the bugs l...
819
      orbiter.append('svg:title').text(config.name);
438929a4   Goutte   Rewrite the orbit...
820
      orbit_line = d3.line().x(function(d){
b500e561   Goutte   Invert the orbits...
821
        return this$.xScale(d.y);
438929a4   Goutte   Rewrite the orbit...
822
      }).y(function(d){
b500e561   Goutte   Invert the orbits...
823
        return this$.yScale(d.x);
438929a4   Goutte   Rewrite the orbit...
824
      });
a21f81d9   Goutte   Enable Venus and ...
825
      orbit_section = this.plotWrapper.append('path').datum(data).classed('orbit orbit_section', true);
438929a4   Goutte   Rewrite the orbit...
826
827
828
829
830
831
      this.orbitersElements[slug] = {
        orbiter: orbiter,
        orbit_ellipse: orbit_ellipse,
        orbit_section: orbit_section,
        orbit_line: orbit_line
      };
17c52617   Goutte   Make the orbits p...
832
833
834
      this.orbitersExtrema[slug] = d3.max(data, function(d){
        return Math.max(Math.abs(d.x), Math.abs(d.y));
      });
3ee0b596   Goutte   Fix an annoying b...
835
      $(this.svg.node()).show();
17c52617   Goutte   Make the orbits p...
836
      this.resize(true);
438929a4   Goutte   Rewrite the orbit...
837
838
      return this;
    };
17c52617   Goutte   Make the orbits p...
839
    Orbits.prototype.showOrbiter = function(slug){
dc0be992   Goutte   Support having no...
840
841
842
      if (!this.data[slug].length) {
        return;
      }
17c52617   Goutte   Make the orbits p...
843
844
845
846
847
848
849
      this.orbiters[slug].hidden = false;
      this.orbitersElements[slug].orbiter.style("display", null);
      this.orbitersElements[slug].orbit_ellipse.style("display", null);
      this.orbitersElements[slug].orbit_section.style("display", null);
      return this.resize(true);
    };
    Orbits.prototype.hideOrbiter = function(slug){
dc0be992   Goutte   Support having no...
850
851
852
      if (!this.data[slug].length) {
        return;
      }
17c52617   Goutte   Make the orbits p...
853
854
855
856
857
858
      this.orbiters[slug].hidden = true;
      this.orbitersElements[slug].orbiter.style("display", "none");
      this.orbitersElements[slug].orbit_ellipse.style("display", "none");
      this.orbitersElements[slug].orbit_section.style("display", "none");
      return this.resize(true);
    };
2c0e1515   Goutte   Refactor loading ...
859
860
861
    Orbits.prototype.clear = function(){
      return $(this.svg.node()).remove();
    };
3c2b15fc   Goutte   Make the Y-Axis o...
862
863
    Orbits.prototype.resize = function(animate, extremum){
      var width, height, s, o, slug, ref$, config, t, t1;
17c52617   Goutte   Make the orbits p...
864
      animate == null && (animate = false);
3c2b15fc   Goutte   Make the Y-Axis o...
865
      extremum == null && (extremum = null);
667eeb24   Goutte   Resize the domain...
866
867
868
      width = Math.ceil($(this.container).width() - this.margin.left - this.margin.right);
      height = Math.ceil(1.0 * width);
      console.debug("Resizing orbits : " + width + " × " + height + "…");
3c2b15fc   Goutte   Make the Y-Axis o...
869
870
871
872
873
874
875
876
      if (extremum === null) {
        extremum = 1.1 * d3.max((function(){
          var ref$, results$ = [];
          for (s in ref$ = this.orbiters) {
            o = ref$[s];
            if (!o.hidden) {
              results$.push(this.orbitersExtrema[s]);
            }
17c52617   Goutte   Make the orbits p...
877
          }
3c2b15fc   Goutte   Make the Y-Axis o...
878
879
880
          return results$;
        }.call(this)));
      }
17c52617   Goutte   Make the orbits p...
881
882
      this.xScale = d3.scaleLinear().domain([-1 * extremum, extremum]);
      this.yScale = d3.scaleLinear().domain([extremum, -1 * extremum]);
438929a4   Goutte   Rewrite the orbit...
883
      this.xScale.range([0, width]);
17c52617   Goutte   Make the orbits p...
884
      this.yScale.range([height, 0]);
438929a4   Goutte   Rewrite the orbit...
885
      this.svg.attr('width', width + this.margin.right + this.margin.left).attr('height', height + this.margin.top + this.margin.bottom);
8bd715ad   Goutte   Use a pixel art i...
886
      this.sun.attr("x", width / 2 - 16).attr("y", height / 2 - 16);
438929a4   Goutte   Rewrite the orbit...
887
888
      for (slug in ref$ = this.orbiters) {
        config = ref$[slug];
17c52617   Goutte   Make the orbits p...
889
        this.resizeOrbiter(slug, config, width, height, animate);
438929a4   Goutte   Rewrite the orbit...
890
891
892
      }
      this.xAxis.scale(this.xScale);
      this.yAxis.scale(this.yScale);
17c52617   Goutte   Make the orbits p...
893
894
895
896
897
898
899
900
901
902
      this.svg.select('.x.axis').attr('transform', 'translate(0,' + height + ')');
      if (animate) {
        t = this.svg.transition().duration(750);
        t1 = this.svg.transition().duration(4750);
        this.svg.select('.x.axis').transition(t).call(this.xAxis);
        this.svg.select('.y.axis').transition(t).call(this.yAxis);
      } else {
        this.svg.select('.x.axis').call(this.xAxis);
        this.svg.select('.y.axis').call(this.yAxis);
      }
11662eed   Goutte   Add Y axis label ...
903
904
      this.xAxisTitle.attr("x", width / 2).attr("y", 37);
      this.yAxisTitle.attr("x", -1 * height / 2).attr("y", -30);
438929a4   Goutte   Rewrite the orbit...
905
906
      return this;
    };
17c52617   Goutte   Make the orbits p...
907
    Orbits.prototype.resizeOrbiter = function(slug, config, width, height, animate){
dc0be992   Goutte   Support having no...
908
      var data, tt, el, orbit_section, t, a, b, c, cx, cy, orbit_ellipse;
17c52617   Goutte   Make the orbits p...
909
      animate == null && (animate = false);
dc0be992   Goutte   Support having no...
910
911
912
913
      data = this.data[slug];
      if (!data.length) {
        return;
      }
667eeb24   Goutte   Resize the domain...
914
      console.debug("Resizing orbit of " + slug + "…");
17c52617   Goutte   Make the orbits p...
915
      tt = this.svg.transition().duration(750);
438929a4   Goutte   Rewrite the orbit...
916
      el = this.orbitersElements[slug];
17c52617   Goutte   Make the orbits p...
917
918
919
920
921
922
      orbit_section = el['orbit_section'];
      if (animate) {
        t = this.svg.transition().duration(750);
        orbit_section = orbit_section.transition(tt);
      }
      orbit_section.attr('d', el['orbit_line']);
438929a4   Goutte   Rewrite the orbit...
923
924
925
926
927
      a = config['orbit']['a'];
      b = config['orbit']['b'];
      c = Math.sqrt(a * a - b * b);
      cx = width / 2 - c;
      cy = height / 2;
17c52617   Goutte   Make the orbits p...
928
929
930
931
932
933
934
      orbit_ellipse = el['orbit_ellipse'];
      if (animate) {
        t = this.svg.transition().duration(750);
        orbit_ellipse = orbit_ellipse.transition(t);
      }
      orbit_ellipse.attr('cx', cx).attr('cy', cy).attr('rx', this.xScale(a) - this.xScale(0)).attr('ry', this.yScale(b) - this.yScale(0));
      this.repositionOrbiter(slug, null, true);
438929a4   Goutte   Rewrite the orbit...
935
936
      return this;
    };
3c2b15fc   Goutte   Make the Y-Axis o...
937
938
939
    Orbits.prototype.zoomToTarget = function(slug){
      return this.resize(true, 1.1 * this.orbitersExtrema[slug]);
    };
17c52617   Goutte   Make the orbits p...
940
941
942
    Orbits.prototype.repositionOrbiter = function(slug, datum, animate){
      var data, el, t;
      animate == null && (animate = false);
a21f81d9   Goutte   Enable Venus and ...
943
      data = this.data[slug];
dc0be992   Goutte   Support having no...
944
945
946
      if (!data.length) {
        return;
      }
17c52617   Goutte   Make the orbits p...
947
      datum == null && (datum = this.lastOrbiterData[slug]);
ae0aa7d2   Goutte   Add an x axis lab...
948
      datum == null && (datum = data[data.length - 1]);
17c52617   Goutte   Make the orbits p...
949
950
951
952
953
954
955
956
      this.lastOrbiterData[slug] = datum;
      el = this.orbitersElements[slug]['orbiter'];
      if (animate) {
        t = this.svg.transition().duration(750);
        el = el.transition(t);
      }
      el.attr('x', this.xScale(datum.y) - 16);
      el.attr('y', this.yScale(datum.x) - 16);
ae0aa7d2   Goutte   Add an x axis lab...
957
958
959
960
961
962
      return this;
    };
    Orbits.prototype.bisectDate = d3.bisector(function(d){
      return d.t;
    }).left;
    Orbits.prototype.moveToDate = function(t){
a21f81d9   Goutte   Enable Venus and ...
963
964
      var slug, ref$, el, data, i, d0, d1, d;
      if (!t) {
667eeb24   Goutte   Resize the domain...
965
        console.warn("Trying to move to an undefined date !");
a21f81d9   Goutte   Enable Venus and ...
966
      }
ae0aa7d2   Goutte   Add an x axis lab...
967
968
      for (slug in ref$ = this.orbitersElements) {
        el = ref$[slug];
a21f81d9   Goutte   Enable Venus and ...
969
        data = this.data[slug];
ae0aa7d2   Goutte   Add an x axis lab...
970
971
972
973
974
975
976
        i = this.bisectDate(data, t, 1);
        d0 = data[i - 1];
        d1 = data[i];
        if (!(d1 && d0)) {
          continue;
        }
        d = t - d0.t > d1.t - t ? d1 : d0;
a21f81d9   Goutte   Enable Venus and ...
977
        this.repositionOrbiter(slug, d);
ae0aa7d2   Goutte   Add an x axis lab...
978
      }
a21f81d9   Goutte   Enable Venus and ...
979
      return this;
ae0aa7d2   Goutte   Add an x axis lab...
980
    };
667eeb24   Goutte   Resize the domain...
981
    Orbits.prototype.resizeDomain = function(started_at, stopped_at){
dc0be992   Goutte   Support having no...
982
      var slug, ref$, config, el, data;
667eeb24   Goutte   Resize the domain...
983
984
985
986
      for (slug in ref$ = this.orbiters) {
        config = ref$[slug];
        el = this.orbitersElements[slug];
        data = this.data[slug].filter(fn$);
dc0be992   Goutte   Support having no...
987
988
989
        if (!data.length) {
          return;
        }
667eeb24   Goutte   Resize the domain...
990
        el['orbit_section'].datum(data);
dc0be992   Goutte   Support having no...
991
        el['orbit_section'].attr('d', el['orbit_line']);
667eeb24   Goutte   Resize the domain...
992
      }
667eeb24   Goutte   Resize the domain...
993
994
995
996
997
998
      function fn$(d){
        var ref$;
        return started_at <= (ref$ = d.t) && ref$ <= stopped_at;
      }
    };
    Orbits.prototype.resetZoom = function(){
dc0be992   Goutte   Support having no...
999
      var slug, ref$, config, el;
667eeb24   Goutte   Resize the domain...
1000
1001
1002
      for (slug in ref$ = this.orbiters) {
        config = ref$[slug];
        el = this.orbitersElements[slug];
dc0be992   Goutte   Support having no...
1003
1004
1005
        if (!this.data[slug].length) {
          return;
        }
667eeb24   Goutte   Resize the domain...
1006
        el['orbit_section'].datum(this.data[slug]);
dc0be992   Goutte   Support having no...
1007
        el['orbit_section'].attr('d', el['orbit_line']);
667eeb24   Goutte   Resize the domain...
1008
      }
667eeb24   Goutte   Resize the domain...
1009
    };
438929a4   Goutte   Rewrite the orbit...
1010
1011
    return Orbits;
  }());
2463bd16   Goutte   Add a circle foll...
1012
1013
1014
  function bind$(obj, key, target){
    return function(){ return (target || obj)[key].apply(obj, arguments) };
  }
438929a4   Goutte   Rewrite the orbit...
1015
}).call(this);