summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/milestone.js
blob: 280613bda4935074679fd8ab2c9c099d0b23f87d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import $ from 'jquery';
import { deprecatedCreateFlash as flash } from './flash';
import axios from './lib/utils/axios_utils';
import { __ } from './locale';

export default class Milestone {
  constructor() {
    this.bindTabsSwitching();
    this.loadInitialTab();
  }

  bindTabsSwitching() {
    return $('a[data-toggle="tab"]').on('show.bs.tab', (e) => {
      const $target = $(e.target);

      window.location.hash = $target.attr('href');
      this.loadTab($target);
    });
  }

  loadInitialTab() {
    const $target = $(`.js-milestone-tabs a:not(.active)[href="${window.location.hash}"]`);

    if ($target.length) {
      $target.tab('show');
    } else {
      this.loadTab($('.js-milestone-tabs a.active'));
    }
  }
  // eslint-disable-next-line class-methods-use-this
  loadTab($target) {
    const endpoint = $target.data('endpoint');
    const tabElId = $target.attr('href');

    if (endpoint && !$target.hasClass('is-loaded')) {
      axios
        .get(endpoint)
        .then(({ data }) => {
          $(tabElId).html(data.html);
          $target.addClass('is-loaded');
        })
        .catch(() => flash(__('Error loading milestone tab')));
    }
  }
}