aboutsummaryrefslogtreecommitdiffstats
path: root/web_src/js/components/ScopedAccessTokenSelector.vue
blob: 769f3262b44122b89d9c79de994e8b589d10aee1 (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
133
134
135
136
137
138
<template>
  <div class="scoped-access-token-category">
    <div class="field gt-pl-2">
      <label class="checkbox-label">
        <input
          ref="category"
          v-model="categorySelected"
          class="scope-checkbox scoped-access-token-input"
          type="checkbox"
          name="scope"
          :value="'write:' + category"
          @input="onCategoryInput"
        >
        {{ category }}
      </label>
    </div>
    <div class="field gt-pl-4">
      <div class="inline field">
        <label class="checkbox-label">
          <input
            ref="read"
            v-model="readSelected"
            :disabled="disableIndividual || writeSelected"
            class="scope-checkbox scoped-access-token-input"
            type="checkbox"
            name="scope"
            :value="'read:' + category"
            @input="onIndividualInput"
          >
          read:{{ category }}
        </label>
      </div>
      <div class="inline field">
        <label class="checkbox-label">
          <input
            ref="write"
            v-model="writeSelected"
            :disabled="disableIndividual"
            class="scope-checkbox scoped-access-token-input"
            type="checkbox"
            name="scope"
            :value="'write:' + category"
            @input="onIndividualInput"
          >
          write:{{ category }}
        </label>
      </div>
    </div>
  </div>
</template>

<script>
import {createApp} from 'vue';
import {showElem} from '../utils/dom.js';

const sfc = {
  props: {
    category: {
      type: String,
      required: true,
    },
  },

  data: () => ({
    categorySelected: false,
    disableIndividual: false,
    readSelected: false,
    writeSelected: false,
  }),

  methods: {
    /**
     * When entire category is toggled
     * @param {Event} e
     */
    onCategoryInput(e) {
      e.preventDefault();
      this.disableIndividual = this.$refs.category.checked;
      this.writeSelected = this.$refs.category.checked;
      this.readSelected = this.$refs.category.checked;
    },

    /**
     * When an individual level of category is toggled
     * @param {Event} e
     */
    onIndividualInput(e) {
      e.preventDefault();
      if (this.$refs.write.checked) {
        this.readSelected = true;
      }
      this.categorySelected = this.$refs.write.checked;
    },
  }
};

export default sfc;

/**
 * Initialize category toggle sections
 */
export function initScopedAccessTokenCategories() {
  for (const el of document.getElementsByTagName('scoped-access-token-category')) {
    const category = el.getAttribute('category');
    createApp(sfc, {
      category,
    }).mount(el);
  }

  document.getElementById('scoped-access-submit')?.addEventListener('click', (e) => {
    e.preventDefault();
    // check that at least one scope has been selected
    for (const el of document.getElementsByClassName('scoped-access-token-input')) {
      if (el.checked) {
        document.getElementById('scoped-access-form').submit();
      }
    }
    // no scopes selected, show validation error
    showElem(document.getElementById('scoped-access-warning'));
  });
}

</script>

<style scoped>
.scoped-access-token-category {
  padding-top: 10px;
  padding-bottom: 10px;
}

.checkbox-label {
  cursor: pointer;
}

.scope-checkbox {
  margin: 4px 5px 0 0;
}
</style>