summaryrefslogblamecommitdiffstats
path: root/webapp/src/components/ConfiguratorModuleConfig.vue
blob: fdb9047896d560eb9c951ce2cf3620137bcfc208 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15














                                                 

                             





                                  
                            






                                                            

                                  




          
                                   
                                                   

                                                                                             
                                      



                                                                                                               
                                    
                                                                                  


                                                                                                                                                 
                                    



                                                                                                                                       



                                                                                                     
                                     

                                                      


                                                                                                
                                            

                                   
                                                




                                                                                                     
                                                                 









                                                                                                                                                                                                 
                                                                                    

                                   
                      
                                                                     






                                                                                           
                                                                                       





                                                                                                                                         
                                    




                                   
             








                         




                  





                                                            
                                                                       





                      
                       
                                                    

                                                         

                                                     
                                               
                                                 
                                      
                         
                              
                                                                                               








                                                            

                                                                














                                                        
                                  
                                                                             
                                  






                                          







                                                   




                                                                      













                                                                   
                      

                    













                 
 












                        




                                
        
<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-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-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-right">
        <v-subheader>{{ $t('entries') }}</v-subheader>
        <v-list>
          <draggable v-if="items.length" v-model="items" handle=".handle">
            <v-list-item v-for="item in items" :key="item.entry.id" @click.stop :ripple="false">
              <v-list-item-action class="handle">
                <v-icon>drag_handle</v-icon>
              </v-list-item-action>
              <v-list-item-action>
                <v-tooltip top open-delay="800">
                  <template #activator="{ on }">
                    <v-radio-group v-on="on" v-model="defaultEntry">
                      <v-radio v-if="item.entry.id" color="primary" :value="item.entry.id"></v-radio>
                    </v-radio-group>
                  </template>
                <span>{{ $t('defaultEntry') }}</span></v-tooltip>
              </v-list-item-action>
              <v-list-item-content class="item-content">
                <div style="display: flex">
                  <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>
                </div>
              </v-list-item-content>
              <v-list-item-action>
                <v-btn @click="removeItem(item)" icon><v-icon>clear</v-icon></v-btn>
              </v-list-item-action>
            </v-list-item>
          </draggable>
          <div v-else class="text-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 text="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>