<commit_before>import Ember from 'ember';

import BarogramComponent from './base-barogram';

import safeComputed from '../computed/safe-computed';

export default BarogramComponent.extend({
  fixCalc: Ember.inject.service(),
  flightPhase: Ember.inject.service(),

  time: null,
  defaultTime: null,

  flights: Ember.computed.readOnly('fixCalc.flights'),

  selection: null,

  activeFlights: Ember.computed('flights.[]', 'selection', function() {
    let { flights, selection } = this.getProperties('flights', 'selection');
    return flights.filter(flight => (!selection || flight.get('id') === selection));
  }),

  passiveFlights: Ember.computed('flights.[]', 'selection', function() {
    let { flights, selection } = this.getProperties('flights', 'selection');
    return flights.filter(flight => (selection && flight.get('id') !== selection));
  }),

  active: Ember.computed('activeFlights.@each.{flot_h,color}', function() {
    return this.get('activeFlights').map(flight => ({
      data: flight.get('flot_h'),
      color: flight.get('color'),
    }));
  }),

  passive: Ember.computed('passiveFlights.@each.{flot_h,color}', function() {
    return this.get('passiveFlights').map(flight => ({
      data: flight.get('flot_h'),
      color: flight.get('color'),
    }));
  }),

  enls: Ember.computed('activeFlights.@each.{flot_enl,color}', function() {
    return this.get('activeFlights').map(flight => ({
      data: flight.get('flot_enl'),
      color: flight.get('color'),
    }));
  }),

  selectedFlight: Ember.computed('flights.[]', 'selection', function() {
    let { flights, selection } = this.getProperties('flights', 'selection');
    if (flights.get('length') === 1) {
      return flights.get('firstObject');
    } else if (selection) {
      return flights.findBy('id', selection);
    }
  }),

  contests: safeComputed('selectedFlight', flight => flight.get('contests')),
  elevations: safeComputed('selectedFlight', flight => flight.get('flot_elev')),

  timeHighlight: Ember.computed.readOnly('flightPhase.selection'),
  timeHighlightObserver: Ember.observer('timeHighlight', function() {
    Ember.run.once(this, 'draw');
  }),

  hoverMode: Ember.computed.not('fixCalc.isRunning'),
  hoverModeObserver: Ember.observer('hoverMode', function() {
    Ember.run.once(this, 'onHoverModeUpdate');
  }),

  timeInterval: null,
  timeIntervalObserver: Ember.observer('timeInterval', function() {
    this.updateInterval();
  }),

  didInsertElement() {
    this._super(...arguments);
    this.onHoverModeUpdate();

    this.get('placeholder').on('plotclick', (event, pos) => {
      this.getWithDefault('onTimeChange', Ember.K)(pos.x / 1000);
    });
  },

  didUpdateAttrs() {
    let selection = this.get('selection');
    let timeInterval = this.get('timeInterval');

    if (selection !== this.get('oldSelection')) {
      this.draw();
    } else if (timeInterval !== this.get('oldTimeInterval')) {
      this.draw();
    } else {
      this.updateCrosshair();
    }

    this.set('oldSelection', selection);
    this.set('oldTimeInterval', timeInterval);
  },

  update() {
    this.updateTimeHighlight();
    this._super(...arguments);
  },

  draw() {
    this.updateCrosshair();
    this._super(...arguments);
  },

  updateCrosshair() {
    let { flot, time } = this.getProperties('flot', 'time');

    if (time === null) {
      flot.clearCrosshair();
    } else if (time == -1) {
      flot.lockCrosshair({ x: 999999999 });
    } else {
      flot.lockCrosshair({ x: time * 1000 });
    }
  },

  updateInterval() {
    let { flot, timeInterval: interval } = this.getProperties('flot', 'timeInterval');
    let opt = flot.getOptions();

    if (!interval) {
      opt.xaxes[0].min = opt.xaxes[0].max = null;
    } else {
      let [start, end] = interval;
      opt.xaxes[0].min = start * 1000;
      opt.xaxes[0].max = end * 1000;
    }
  },

  onHoverModeUpdate() {
    let placeholder = this.get('placeholder');

    if (this.get('hoverMode')) {
      placeholder.on('plothover', (event, pos) => {
        this.getWithDefault('onTimeChange', Ember.K)(pos.x / 1000);
      });

      placeholder.on('mouseout', () => {
        let defaultTime = this.get('defaultTime');
        this.getWithDefault('onTimeChange', Ember.K)(defaultTime);
      });
    } else {
      placeholder.off('plothover');
      placeholder.off('mouseout');
    }
  },

  updateTimeHighlight() {
    // There is no flot.setOptions(), so we modify them in-place.
    let options = this.get('flot').getOptions();

    // Clear the markings if there is no time highlight
    let time_highlight = this.get('timeHighlight');
    if (!time_highlight) {
      options.grid.markings = [];
      return;
    }

    // Add time highlight as flot markings
    options.grid.markings = [{
      color: '#fff083',
      xaxis: {
        from: time_highlight.start * 1000,
        to: time_highlight.end * 1000,
      },
    }];
  },
});
<commit_msg>components/flight-barogram: Replace timeIntervalObserver<commit_after>import Ember from 'ember';

import BarogramComponent from './base-barogram';

import safeComputed from '../computed/safe-computed';

export default BarogramComponent.extend({
  fixCalc: Ember.inject.service(),
  flightPhase: Ember.inject.service(),

  time: null,
  defaultTime: null,

  flights: Ember.computed.readOnly('fixCalc.flights'),

  selection: null,

  activeFlights: Ember.computed('flights.[]', 'selection', function() {
    let { flights, selection } = this.getProperties('flights', 'selection');
    return flights.filter(flight => (!selection || flight.get('id') === selection));
  }),

  passiveFlights: Ember.computed('flights.[]', 'selection', function() {
    let { flights, selection } = this.getProperties('flights', 'selection');
    return flights.filter(flight => (selection && flight.get('id') !== selection));
  }),

  active: Ember.computed('activeFlights.@each.{flot_h,color}', function() {
    return this.get('activeFlights').map(flight => ({
      data: flight.get('flot_h'),
      color: flight.get('color'),
    }));
  }),

  passive: Ember.computed('passiveFlights.@each.{flot_h,color}', function() {
    return this.get('passiveFlights').map(flight => ({
      data: flight.get('flot_h'),
      color: flight.get('color'),
    }));
  }),

  enls: Ember.computed('activeFlights.@each.{flot_enl,color}', function() {
    return this.get('activeFlights').map(flight => ({
      data: flight.get('flot_enl'),
      color: flight.get('color'),
    }));
  }),

  selectedFlight: Ember.computed('flights.[]', 'selection', function() {
    let { flights, selection } = this.getProperties('flights', 'selection');
    if (flights.get('length') === 1) {
      return flights.get('firstObject');
    } else if (selection) {
      return flights.findBy('id', selection);
    }
  }),

  contests: safeComputed('selectedFlight', flight => flight.get('contests')),
  elevations: safeComputed('selectedFlight', flight => flight.get('flot_elev')),

  timeHighlight: Ember.computed.readOnly('flightPhase.selection'),
  timeHighlightObserver: Ember.observer('timeHighlight', function() {
    Ember.run.once(this, 'draw');
  }),

  hoverMode: Ember.computed.not('fixCalc.isRunning'),
  hoverModeObserver: Ember.observer('hoverMode', function() {
    Ember.run.once(this, 'onHoverModeUpdate');
  }),

  timeInterval: null,

  didInsertElement() {
    this._super(...arguments);
    this.onHoverModeUpdate();

    this.get('placeholder').on('plotclick', (event, pos) => {
      this.getWithDefault('onTimeChange', Ember.K)(pos.x / 1000);
    });
  },

  didUpdateAttrs() {
    let selection = this.get('selection');
    let timeInterval = this.get('timeInterval');

    if (timeInterval !== this.get('oldTimeInterval')) {
      this.updateInterval();
    }

    if (selection !== this.get('oldSelection')) {
      this.draw();
    } else if (timeInterval !== this.get('oldTimeInterval')) {
      this.draw();
    } else {
      this.updateCrosshair();
    }

    this.set('oldSelection', selection);
    this.set('oldTimeInterval', timeInterval);
  },

  update() {
    this.updateTimeHighlight();
    this._super(...arguments);
  },

  draw() {
    this.updateCrosshair();
    this._super(...arguments);
  },

  updateCrosshair() {
    let { flot, time } = this.getProperties('flot', 'time');

    if (time === null) {
      flot.clearCrosshair();
    } else if (time == -1) {
      flot.lockCrosshair({ x: 999999999 });
    } else {
      flot.lockCrosshair({ x: time * 1000 });
    }
  },

  updateInterval() {
    let { flot, timeInterval: interval } = this.getProperties('flot', 'timeInterval');
    let opt = flot.getOptions();

    if (!interval) {
      opt.xaxes[0].min = opt.xaxes[0].max = null;
    } else {
      let [start, end] = interval;
      opt.xaxes[0].min = start * 1000;
      opt.xaxes[0].max = end * 1000;
    }
  },

  onHoverModeUpdate() {
    let placeholder = this.get('placeholder');

    if (this.get('hoverMode')) {
      placeholder.on('plothover', (event, pos) => {
        this.getWithDefault('onTimeChange', Ember.K)(pos.x / 1000);
      });

      placeholder.on('mouseout', () => {
        let defaultTime = this.get('defaultTime');
        this.getWithDefault('onTimeChange', Ember.K)(defaultTime);
      });
    } else {
      placeholder.off('plothover');
      placeholder.off('mouseout');
    }
  },

  updateTimeHighlight() {
    // There is no flot.setOptions(), so we modify them in-place.
    let options = this.get('flot').getOptions();

    // Clear the markings if there is no time highlight
    let time_highlight = this.get('timeHighlight');
    if (!time_highlight) {
      options.grid.markings = [];
      return;
    }

    // Add time highlight as flot markings
    options.grid.markings = [{
      color: '#fff083',
      xaxis: {
        from: time_highlight.start * 1000,
        to: time_highlight.end * 1000,
      },
    }];
  },
});

{'fraction_non_alphanumeric': 0.17821881926254282, 'fraction_numerical': 0.00926858754785412, 'mean_word_length': 3.3596837944664033, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '808688', 'n_tokens_mistral': 3320, 'n_tokens_neox': 3125, 'n_words': 564}