summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/performance_bar.js
blob: c22598ee66577c55f67195b712575472320a887d (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
46
47
48
49
50
51
52
53
54
55
56
57
import $ from 'jquery';
import 'vendor/peek';
import 'vendor/peek.performance_bar';
import { getParameterValues } from './lib/utils/url_utility';

export default class PerformanceBar {
  constructor(opts) {
    if (!PerformanceBar.singleton) {
      this.init(opts);
      PerformanceBar.singleton = this;
    }
    return PerformanceBar.singleton;
  }

  init(opts) {
    const $container = $(opts.container);
    this.$lineProfileLink = $container.find('.js-toggle-modal-peek-line-profile');
    this.$lineProfileModal = $('#modal-peek-line-profile');
    this.initEventListeners();
    this.showModalOnLoad();
  }

  initEventListeners() {
    this.$lineProfileLink.on('click', e => this.handleLineProfileLink(e));
    $(document).on('click', '.js-lineprof-file', PerformanceBar.toggleLineProfileFile);
  }

  showModalOnLoad() {
    // When a lineprofiler query-string param is present, we show the line
    // profiler modal upon page load
    if (/lineprofiler/.test(window.location.search)) {
      PerformanceBar.toggleModal(this.$lineProfileModal);
    }
  }

  handleLineProfileLink(e) {
    const lineProfilerParameter = getParameterValues('lineprofiler');
    const lineProfilerParameterRegex = new RegExp(`lineprofiler=${lineProfilerParameter[0]}`);
    const shouldToggleModal = lineProfilerParameter.length > 0 &&
      lineProfilerParameterRegex.test(e.currentTarget.href);

    if (shouldToggleModal) {
      e.preventDefault();
      PerformanceBar.toggleModal(this.$lineProfileModal);
    }
  }

  static toggleModal($modal) {
    if ($modal.length) {
      $modal.modal('toggle');
    }
  }

  static toggleLineProfileFile(e) {
    $(e.currentTarget).parents('.peek-rblineprof-file').find('.data').toggle();
  }
}