summaryrefslogtreecommitdiffstats
path: root/webapp/src/components/ConfiguratorModuleConfig.vue
blob: 654f49391ca3ae5652745ff9ff2490764c5ea1d8 (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<i18n>
{
  "en": {
    "name": "Name",
    "description": "Description",
    "timeout": "Timeout",
    "expertMode": "Expert Mode",
    "script": "iPXE Script",
    "titleNew": "Create config",
    "titleExisting": "Edit config",
    "entries": "Entries",
    "customName": "Custom Name",
    "keyBind": "Key Bind",
    "defaultEntry": "Mark this entry as default",
    "entry": "Entry",
    "spacer": "[Spacer]",
    "noEntries": "No Entries"
  },
  "de": {
    "name": "Name",
    "description": "Beschreibung",
    "timeout": "Timeout",
    "expertMode": "Expertenmodus",
    "script": "iPXE Skript",
    "titleNew": "Konfiguration erstellen",
    "titleExisting": "Konfiguration bearbeiten",
    "entries": "Einträge",
    "customName": "Alternativer Name",
    "keyBind": "Tastenbelegung",
    "defaultEntry": "Diesen Eintrag als Standard markieren",
    "entry": "Eintrag",
    "spacer": "[Abstandshalter]",
    "noEntries": "Keine Einträge"
  }
}
</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>
          <v-text-field prepend-icon="label" :label="$t('name')" color="primary" v-model="name"></v-text-field>
        </v-flex>
        <v-flex class="text-xs-center">
          <div v-if="!expertMode" style="display: inline-block; max-width: 180px">
            <v-text-field prepend-icon="timer" :label="$t('timeout')" color="primary" v-model="timeout" mask="######" suffix="ms"></v-text-field>
          </div>
        </v-flex>
        <v-flex class="text-xs-center">
          <div style="display: inline-block"><v-switch v-model="expertMode" color="primary" :label="$t('expertMode')"></v-switch></div>
        </v-flex>
      </v-layout>
      <v-textarea prepend-icon="description" rows="1" :label="$t('description')" color="primary" v-model="description"></v-textarea>
      <div v-if="expertMode">
        <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>
      </div>
      <div v-else class="text-xs-right">
        <v-subheader>{{ $t('entries') }}</v-subheader>
        <v-list>
          <draggable v-if="items.length" v-model="items" :options="{ handle:'.handle' }">
            <v-list-tile v-for="item in items" :key="item.entry.id" @click.stop>
              <v-list-tile-action class="handle">
                <v-icon>drag_handle</v-icon>
              </v-list-tile-action>
              <v-list-tile-action>
                <v-tooltip top open-delay="800">
                  <v-radio-group slot="activator" v-model="defaultEntry">
                    <v-radio v-if="item.entry.id" color="primary" :value="item.entry.id"></v-radio>
                  </v-radio-group>
                <span>{{ $t('defaultEntry') }}</span></v-tooltip>
              </v-list-tile-action>
              <v-list-tile-content class="item-content">
                <v-select return-object class="entry-input" item-text="name" menu-props="offsetY" :label="$t('entry')" color="primary" v-model="item.entry"
                :items="item.entry.id ? [item.entry, ...availableEntries] : availableEntries" hide-details single-line></v-select>
                <v-text-field v-if="item.entry.id" class="custom-name-input" :label="$t('customName')" color="primary" v-model="item.customName" hide-details single-line></v-text-field>
                <v-text-field v-if="item.entry.id" class="keybind-input" prepend-icon="keyboard" color="primary" v-model="item.keyBind" maxlength="1"  hide-details single-line></v-text-field>
              </v-list-tile-content>
              <v-list-tile-action>
                <v-btn @click="removeItem(item)" icon><v-icon>clear</v-icon></v-btn>
              </v-list-tile-action>
            </v-list-tile>
          </draggable>
          <div v-else class="text-xs-center">{{ $t('noEntries') }}</div>
        </v-list>
        <v-btn @click="addItem" color="success" fab small><v-icon dark>add</v-icon></v-btn>
      </div>
    </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="saveConfig">{{ dialog.info.id ? $t('save') : $t('create') }}</v-btn>
    </v-card-actions>
  </v-card>
</template>

<script>
import draggable from 'vuedraggable'
import { mapState } from 'vuex'

export default {
  name: 'ConfiguratorModuleConfig',
  components: {
    draggable
  },
  data () {
    return {
      name: '',
      description: '',
      defaultEntry: null,
      timeout: '',
      script: '',
      expertMode: false,
      items: [],
      interval: {
        id: null,
        counter: 0
      }
    }
  },
  computed: {
    ...mapState('configurator', ['dialog', 'entries']),
    availableEntries () {
      var selectedEntryIds = this.items.map(x => x.entry.id)
      return this.entries.filter(x => !selectedEntryIds.includes(x.id))
    }
  },
  watch: {
    dialog: {
      immediate: true,
      deep: true,
      handler (value) {
        if (value.type === 'config' && value.show) {
          this.name = value.info.name || ''
          this.description = value.info.description || ''
          this.defaultEntry = value.info.defaultEntry
          this.timeout = value.info.timeout
          this.script = value.info.script || ''
          if (this.script) this.expertMode = true
          else this.expertMode = false
          this.items = []
          if (value.info.id) {
            this.$http.get('/api/ipxeconfigs/' + value.info.id + '/entries').then(response => {
              this.items = response.data.map(entry => ({
                entry: { id: entry.id, name: entry.name },
                customName: entry.config_x_entry.customName,
                keyBind: entry.config_x_entry.keyBind
              }))
            })
          } else {
            this.items = []
          }
          this.interval.id = setInterval(this.refreshEditor, 50)
          this.interval.counter = 0
        }
      }
    }
  },
  methods: {
    setDialog (data) {
      this.$store.commit('configurator/setDialog', data)
    },
    addItem () {
      this.items.push({ entry: {} })
    },
    removeItem (item) {
      this.items.splice(this.items.indexOf(item), 1)
    },
    async saveConfig () {
      let url = '/api/ipxeconfigs'
      if (this.dialog.info.id !== undefined) url += '/' + this.dialog.info.id
      await this.$http.post(url, {
        data: {
          name: this.name,
          description: this.description,
          defaultEntry: this.defaultEntry,
          timeout: this.timeout,
          script: this.script
        },
        entries: this.items.map(x => ({
          id: x.entry.id,
          customName: x.customName,
          keyBind: x.keyBind
        }))
      })
      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;
}
.item-content {
  display: flex;
  flex-direction: row;
  justify-content: flex-start;
  align-items: center;
}
.custom-name-input {
  margin: 0 20px;
  padding: 0;
}

.entry-input {
  width: 280px;
  margin: 0;
  padding: 0;
}

.keybind-input {
  width: 60px;
  margin: 0;
  padding: 0;
}

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

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

.script-editor {
  margin-top: 8px;
}

.script-editor >>> .CodeMirror {
  height: calc(90vh - 340px);
}

</style>