summaryrefslogtreecommitdiffstats
path: root/webapp/src/components/ConfiguratorModuleEntry.vue
blob: 6a284ca5e814768b3cae93dc3a0ccf15d89e03c1 (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
<i18n>
{
  "en": {
    "name": "Name",
    "script": "iPXE Script",
    "titleNew": "Create entry",
    "titleExisting": "Edit entry"
  },
  "de": {
    "name": "Name",
    "script": "iPXE Script",
    "titleNew": "Eintrag erstellen",
    "titleExisting": "Eintrag 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-text-field prepend-icon="label" :label="$t('name')" color="primary" v-model="name"></v-text-field>
      <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="saveEntry">{{ dialog.info.id ? $t('save') : $t('create') }}</v-btn>
    </v-card-actions>
  </v-card>
</template>

<script>
import { mapState } from 'vuex'

export default {
  name: 'ConfiguratorModuleEntry',
  data () {
    return {
      name: '',
      script: '',
      interval: {
        id: null,
        counter: 0
      }
    }
  },
  computed: {
    ...mapState('configurator', ['dialog'])
  },
  watch: {
    dialog: {
      immediate: true,
      deep: true,
      handler (value) {
        if (value.type === 'entry' && value.show) {
          this.name = value.info.name || ''
          this.script = value.info.script || ''
          this.interval.id = setInterval(this.refreshEditor, 50)
          this.interval.counter = 0
        }
      }
    }
  },
  methods: {
    setDialog (data) {
      this.$store.commit('configurator/setDialog', data)
    },
    async saveEntry () {
      let url = '/api/ipxeentries'
      if (this.dialog.info.id !== undefined) url += '/' + this.dialog.info.id
      await this.$http.post(url, {
        data: {
          name: this.name,
          script: this.script
        }
      })
      this.$store.dispatch('configurator/loadData')
      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>