summaryrefslogtreecommitdiffstats
path: root/webapp/src/components/RegistrationModuleEdit.vue
blob: f90295a3a09607f259d411c573150aa42275cd00 (plain) (blame)
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<i18n>
{
  "en": {
    "name": "Name",
    "description": "Description",
    "type": "Script type",
    "groups": "Groups",
    "script": "Script",
    "titleNew": "Create hook",
    "titleExisting": "Edit hook",
    "groupRestricted": "Restrict to groups"
  },
  "de": {
    "name": "Name",
    "description": "Beschreibung",
    "type": "Skript Art",
    "groups": "Gruppen",
    "script": "Skript",
    "titleNew": "Hook erstellen",
    "titleExisting": "Hook bearbeiten",
    "groupRestricted": "Auf Gruppen beschränken"
  }
}
</i18n>

<template>
  <v-card style="overflow: hidden">
    <v-card-title primary-title class="dialog-title elevation-3">
      <div class="headline">{{ dialog.info.id ? $t('titleExisting') : $t('titleNew') }}</div>
    </v-card-title>
    <v-card-text style="height: 100%;">
      <v-layout row wrap>
        <v-flex xs12 sm6>
          <v-text-field prepend-icon="label" :label="$t('name')" color="primary" v-model="name"></v-text-field>
        </v-flex>
        <v-flex xs12 sm2 offset-sm1 style="display: flex; justify-content: center">
          <v-tooltip top open-delay="800">
            <v-menu offset-y left :close-on-content-click="false" lazy slot="activator">
              <v-btn slot="activator" class="info-heading-button">
                <v-icon :left="groups.length > 0">device_hub</v-icon>{{ groups.length > 0 ? groups.length : '' }}
              </v-btn>
              <v-card>
                <data-table v-model="groups" :headers="headers" :items="groupList" slim :row-count="6"></data-table>
              </v-card>
            </v-menu>
            <span>{{ $t('groupRestricted') }}</span>
          </v-tooltip>
        </v-flex>
        <v-flex xs12 sm2 offset-sm1>
          <v-select prepend-icon="label" color="primary" :items="types" :label="$t('type')" v-model="type" menu-props="offsetY"></v-select>
        </v-flex>
      </v-layout>
      <v-textarea prepend-icon="description" rows="3" :label="$t('description')" color="primary" v-model="description"></v-textarea>
      <div class="body-1 script-heading"><v-icon>code</v-icon><span>{{ $t('script') }}</span></div>
      <codemirror class="script-editor" ref="editor" v-model="script"></codemirror>
    </v-card-text>
    <v-divider></v-divider>
    <v-card-actions>
      <v-spacer></v-spacer>
      <v-btn flat="flat" @click="setDialog({ show: false })">{{ $t('cancel') }}</v-btn>
      <v-btn :color="dialog.info.id ? 'primary' : 'success'" @click="saveHook">{{ dialog.info.id ? $t('save') : $t('create') }}</v-btn>
    </v-card-actions>
  </v-card>
</template>

<script>
import DataTable from '@/components/DataTable'
import { mapState } from 'vuex'

export default {
  name: 'RegistrationModuleEdit',
  components: {
    DataTable
  },
  data () {
    return {
      name: '',
      description: '',
      type: '',
      groups: [],
      script: '',
      types: ['BASH', 'IPXE'],
      interval: {
        id: null,
        counter: 0
      }
    }
  },
  computed: {
    ...mapState('registration', ['dialog', 'groupList']),
    headers () {
      return [
        { key: 'name', text: this.$t('name') }
      ]
    }
  },
  watch: {
    dialog: {
      immediate: true,
      deep: true,
      handler (value) {
        if (value.type === 'edit' && value.show) {
          this.name = value.info.name || ''
          this.description = value.info.description || ''
          this.type = value.info.type || 'BASH'
          this.groups = value.info.groups ? value.info.groups.map(x => x.id) : []
          this.script = value.info.script || ''
          this.interval.id = setInterval(this.refreshEditor, 50)
          this.interval.counter = 0
        }
      }
    }
  },
  methods: {
    setDialog (data) {
      this.$store.commit('registration/setDialog', data)
    },
    async saveHook () {
      await this.$http.post('/api/registration/hooks/' + this.dialog.info.id, {
        name: this.name,
        description: this.description,
        type: this.type,
        groups: this.groups.map(x => x.id),
        script: this.script
      })
      this.$store.dispatch('registration/loadHooks')
      this.setDialog({ show: false })
    },
    refreshEditor () {
      this.interval.counter++
      if (this.$refs.editor) this.$refs.editor.codemirror.refresh()
      if (this.interval.counter >= 15) clearInterval(this.interval.id)
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.dialog-title {
  z-index: 1;
}

.script-heading {
  display: flex;
  align-items: center;
}

.script-heading > span {
  margin-left: 9px;
}

.script-editor {
  margin-top: 8px;
}
</style>