aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/common/modals.js
blob: 9ca6cd56b1fdbb9e754072768f16b047736bd907 (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
define(['backbone.marionette'], function (Marionette) {

  var $ = jQuery,
      EVENT_SCOPE = 'modal';

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

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

    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.close();
        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);
    },

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

    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.close();
      });
    }
  });

});