aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/components/common/modals.js
blob: 931ca8e4604a5293aa60e7291ea36d883a8c6c52 (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import $ from 'jquery';
import Marionette from 'backbone.marionette';

const EVENT_SCOPE = 'modal';

export default Marionette.ItemView.extend({
  className: 'modal',
  overlayClassName: 'modal-overlay',
  htmlClassName: 'modal-open',

  events: function () {
    return {
      'click .js-modal-close': 'onCloseClick'
    };
  },

  onRender: function () {
    var that = this;
    this.$el.detach().appendTo($('body'));
    $('html').addClass(this.htmlClassName);
    this.renderOverlay();
    this.keyScope = key.getScope();
    key.setScope('modal');
    key('escape', 'modal', function () {
      that.destroy();
      return false;
    });
    this.show();
    if (!!this.options.large) {
      this.$el.addClass('modal-large');
    }
  },

  show: function () {
    var that = this;
    setTimeout(function () {
      that.$el.addClass('in');
      $('.' + that.overlayClassName).addClass('in');
    }, 0);
  },

  onDestroy: function () {
    $('html').removeClass(this.htmlClassName);
    this.removeOverlay();
    key.deleteScope('modal');
    key.setScope(this.keyScope);
  },

  onCloseClick: function (e) {
    e.preventDefault();
    this.destroy();
  },

  renderOverlay: function () {
    var overlay = $('.' + this.overlayClassName);
    if (overlay.length === 0) {
      $('<div class="' + this.overlayClassName + '"></div>').appendTo($('body'));
    }
  },

  removeOverlay: function () {
    $('.' + this.overlayClassName).remove();
  },

  attachCloseEvents: function () {
    var that = this;
    $('body').on('click.' + EVENT_SCOPE, function () {
      $('body').off('click.' + EVENT_SCOPE);
      that.destroy();
    });
  }
});