summaryrefslogblamecommitdiffstats
path: root/webapp/src/components/RegistrationModuleEdit.vue
blob: f90295a3a09607f259d411c573150aa42275cd00 (plain) (tree)
1
2
3
4
5
6
7
8
9








                                 

                                           





                                  
                       
                                 

                                                 




          
                                   


                                                                                             
                                       
                         
                         

                                                                                                               

                                                                                   
                                                                                        



                                                                                                                 
                                                                                                                    




                                                    



                                                                                                                                           
                                                                                                                                    

                                                                                                   










                                                                                                                                       
                                              



                                 


               






                      




                              


             





                                                         






                                                  

                                                         

                                                                                 


                                                                








                                                        
                                                                               


                                      
                                           



                                                    




                                                                      









                                                                   












                        
        
<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>