{"size": 4128, "ext": "js", "max_stars_repo_path": "ui/insight/src/ctrl.js", "max_stars_repo_name": "luanlv/english2", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "ui/insight/src/ctrl.js", "max_issues_repo_name": "luanlv/english2", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "ui/insight/src/ctrl.js", "max_forks_repo_name": "luanlv/english2", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 28.8671328671, "max_line_length": 97, "alphanum_fraction": 0.6271802326}
var m = require('mithril');
var throttle = require('./throttle');

module.exports = function(env, domElement) {

  this.ui = env.ui;
  this.user = env.user;
  this.own = env.myUserId === this.user.id;
  this.dimensions = [].concat.apply([], this.ui.dimensionCategs.map(function(c) {
    return c.items;
  }));
  this.metrics = [].concat.apply([], this.ui.metricCategs.map(function(c) {
    return c.items;
  }));

  var findMetric = function(key) {
    return this.metrics.filter(function(x) {
      return x.key === key;
    })[0];
  }.bind(this);

  var findDimension = function(key) {
    return this.dimensions.filter(function(x) {
      return x.key === key;
    })[0];
  }.bind(this);

  this.vm = {
    metric: findMetric(env.initialQuestion.metric),
    dimension: findDimension(env.initialQuestion.dimension),
    filters: env.initialQuestion.filters,
    loading: true,
    answer: null,
    panel: !!Object.keys(env.initialQuestion.filters).length ? 'filter' : 'preset'
  };

  this.setPanel = function(p) {
    this.vm.panel = p;
    m.redraw();
  }.bind(this);


  var reset = function() {
    this.vm.metric = this.metrics[0];
    this.vm.dimension = this.dimensions[0];
    this.vm.filters = {};
  }.bind(this);

  var askQuestion = throttle(1000, false, function() {
    if (!this.validCombinationCurrent()) reset();
    this.pushState();
    this.vm.loading = true;
    m.redraw();
    setTimeout(function() {
      m.request({
        method: 'post',
        url: env.postUrl,
        data: {
          metric: this.vm.metric.key,
          dimension: this.vm.dimension.key,
          filters: this.vm.filters
        }
      }).then(function(answer) {
        this.vm.answer = answer;
        this.vm.loading = false;
      }.bind(this));
    }.bind(this), 1);
  }.bind(this));

  this.makeUrl = function(dKey, mKey, filters) {
    var url = [env.pageUrl, mKey, dKey].join('/');
    var filters = Object.keys(filters).map(function(filterKey) {
      return filterKey + ':' + filters[filterKey].join(',');
    }).join('/');
    if (filters.length) url += '/' + filters;
    return url;
  };

  this.makeCurrentUrl = function() {
    return this.makeUrl(this.vm.dimension.key, this.vm.metric.key, this.vm.filters)
  }.bind(this);

  this.pushState = function() {
    if (window.history.replaceState)
      window.history.replaceState({}, null, this.makeCurrentUrl());
  }.bind(this);

  this.validCombination = function(dimension, metric) {
    return dimension && metric && (
      dimension.position === 'game' || metric.position === 'move'
    );
  };
  this.validCombinationCurrent = function() {
    return this.validCombination(this.vm.dimension, this.vm.metric);
  }.bind(this);

  this.setMetric = function(key) {
    this.vm.metric = findMetric(key);
    if (!this.validCombinationCurrent()) this.vm.dimension = this.dimensions.filter(function(d) {
      return this.validCombination(d, this.vm.metric);
    }.bind(this))[0];
    this.vm.panel = 'filter';
    askQuestion();
  }.bind(this);

  this.setDimension = function(key) {
    this.vm.dimension = findDimension(key);
    if (!this.validCombinationCurrent()) this.vm.metric = this.metrics.filter(function(m) {
      return this.validCombination(this.vm.dimension, m);
    }.bind(this))[0];
    this.vm.panel = 'filter';
    askQuestion();
  }.bind(this);

  this.setFilter = function(dimensionKey, valueKeys) {
    if (!valueKeys.length) delete this.vm.filters[dimensionKey];
    else this.vm.filters[dimensionKey] = valueKeys;
    askQuestion();
  }.bind(this);

  this.setQuestion = function(q) {
    this.vm.dimension = findDimension(q.dimension);
    this.vm.metric = findMetric(q.metric);
    this.vm.filters = q.filters;
    askQuestion();
    $(domElement).find('select.ms').multipleSelect('open');
    setTimeout(function() {
      $(domElement).find('select.ms').multipleSelect('close');
    }, 1000);
  }.bind(this);

  this.clearFilters = function() {
    if (Object.keys(this.vm.filters).length) {
      this.vm.filters = {};
      askQuestion();
    }
  }.bind(this);

  // this.trans = lichess.trans(env.i18n);

  askQuestion();
};
