summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/vue_shared/components/gl_modal.vue
blob: 986ed6359e72de08159a36039725d2048c2bd7ea (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<script>
  const buttonVariants = [
    'danger',
    'primary',
    'success',
    'warning',
  ];

  export default {
    name: 'GlModal',

    props: {
      id: {
        type: String,
        required: false,
        default: null,
      },
      headerTitleText: {
        type: String,
        required: false,
        default: '',
      },
      footerPrimaryButtonVariant: {
        type: String,
        required: false,
        default: 'primary',
        validator: value => buttonVariants.indexOf(value) !== -1,
      },
      footerPrimaryButtonText: {
        type: String,
        required: false,
        default: '',
      },
      bodyComponent: {
        type: Object,
        required: false,
        default: null,
      },
    },

    data() {
      return {
        canSubmit: true,
      };
    },

    created() {
      this.$on('toggleCanSubmit', this.toggleCanSubmit);
    },

    beforeDestroy() {
      this.$off('toggleCanSubmit', this.toggleCanSubmit);
    },

    methods: {
      emitCancel(event) {
        this.$emit('cancel', event);
        this.$emit('clearInputs', event);
      },
      emitSubmit(event) {
        this.$emit('submit', event);
        this.$emit('clearInputs', event);
      },
      toggleCanSubmit(canSubmit) {
        this.canSubmit = canSubmit;
      },
    },
  };
</script>

<template>
  <div
    :id="id"
    class="modal fade"
    tabindex="-1"
    role="dialog"
  >
    <div
      class="modal-dialog"
      role="document"
    >
      <div class="modal-content">
        <div class="modal-header">
          <slot name="header">
            <button
              type="button"
              class="close"
              data-dismiss="modal"
              :aria-label="s__('Modal|Close')"
              @click="emitCancel($event)"
            >
              <span aria-hidden="true">&times;</span>
            </button>
            <h4 class="modal-title">
              <slot name="title">
                {{ headerTitleText }}
              </slot>
            </h4>
          </slot>
        </div>

        <div class="modal-body">
          <slot></slot>
          <component :is="bodyComponent" />
        </div>

        <div class="modal-footer">
          <slot name="footer">
            <button
              type="button"
              class="btn"
              data-dismiss="modal"
              @click="emitCancel($event)"
            >
              {{ s__('Modal|Cancel') }}
            </button>
            <button
              type="button"
              class="btn"
              :class="`btn-${footerPrimaryButtonVariant}`"
              data-dismiss="modal"
              :disabled="!canSubmit"
              @click="emitSubmit($event)"
            >
              {{ footerPrimaryButtonText }}
            </button>
          </slot>
        </div>
      </div>
    </div>
  </div>
</template>