summaryrefslogtreecommitdiffstats
path: root/webapp/src/components/RegistrationModuleEdit.vue
blob: b0ad9e92f2173161460d5fb7e9f65c0e9f7d4a64 (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
<i18n>
{
  "en": {
    "name": "Name",
    "description": "Description",
    "type": "Script Type",
    "groups": "Groups",
    "script": "Script",
    "titleNew": "Create Hook",
    "titleExisting": "Edit Hook"
  },
  "de": {
    "name": "Name",
    "description": "Beschreibung",
    "type": "Skript Art",
    "groups": "Gruppen",
    "script": "Skript",
    "titleNew": "Hook erstellen",
    "titleExisting": "Hook bearbeiten"
  }
}
</i18n>

<template>
  <v-card style="overflow: hidden">
    <v-card-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 wrap>
        <v-flex xs12 sm9>
          <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>
          <v-select prepend-icon="file_copy" color="primary" :items="types" :label="$t('type')" v-model="type" menu-props="offsetY"></v-select>
        </v-flex>
        <v-flex xs12 md6>
          <v-textarea prepend-icon="description" rows="1" auto-grow :label="$t('description')" color="primary" v-model="description"></v-textarea>
        </v-flex>
        <v-flex xs12 md5 offset-md1>
          <select-box prepend-icon="category" :label="$t('groups')" v-model="groups" :items="groupList"></select-box>
        </v-flex>
      </v-layout>
      <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 text="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 SelectBox from '@/components/SelectBox'
import { mapState } from 'vuex'

export default {
  name: 'RegistrationModuleEdit',
  components: {
    SelectBox
  },
  data () {
    return {
      name: '',
      description: '',
      type: '',
      groups: [],
      script: '',
      types: ['BASH', 'IPXE'],
      interval: {
        id: null,
        counter: 0
      }
    }
  },
  computed: {
    ...mapState('registration', ['dialog']),
    ...mapState('groups', ['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 : []
          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>