{"size": 1249, "ext": "js", "max_stars_repo_path": "src/app/components/harc-dashboard/harc-dashboard-controller.js", "max_stars_repo_name": "kwypchlo/harc", "max_stars_repo_stars_event_min_datetime": "2016-07-19T09:03:24.000Z", "max_stars_repo_stars_event_max_datetime": "2016-07-19T09:03:24.000Z", "max_issues_repo_path": "src/app/components/harc-dashboard/harc-dashboard-controller.js", "max_issues_repo_name": "kwypchlo/harc", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/app/components/harc-dashboard/harc-dashboard-controller.js", "max_forks_repo_name": "kwypchlo/harc", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 26.0208333333, "max_line_length": 112, "alphanum_fraction": 0.6333066453}
export default class HarcDashboardController {

  constructor($mdToast, harcDashboardApiService) {
    this.$mdToast = $mdToast;
    this.api = harcDashboardApiService;
    this.data = {};
    this.widgets = [];
  }

  $onInit() {
    this.refreshAll();
  }

  /**
   * Refresh chosen widget (fetch new data from the server and assign it to component)
   */
  refresh(widget) {
    this.api.get([widget]).then(
      (data) => (this.data[widget] = data[widget]),
      () => this.$mdToast.show(this.$mdToast.simple().textContent('Unhandled API error!'))
    );
  }

  /**
   * Refresh all the widgets (fetch new data from the server and assign it to component)
   */
  refreshAll() {
    this.api.get(this.widgets).then(
      (data) => (this.data = data),
      () => this.$mdToast.show(this.$mdToast.simple().textContent('Unhandled API error!'))
    );
  }

  /**
   * Register widget component instance in the dashboard. All widgets that are displayed, have to be registered.
   *
   * @param {string} widget - widget identifier (eg. 'lights')
   */
  registerWidget(widget) {
    if (this.widgets.indexOf(widget) === -1) {
      this.widgets.push(widget);
    }
  }

}

HarcDashboardController.$inject = ['$mdToast', 'harcDashboardApiService'];
