summaryrefslogtreecommitdiffstats
path: root/webapp/src/components/GroupModuleGroupView.vue
blob: bda2922436de2580b75c8d5856777af04a80a75b (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
<i18n>
{
  "en": {
    "info": "Info",
    "subgroups": "Subgroups",
    "clients": "Clients",
    "name": "Name",
    "description": "Description",
    "parents": "Parents"
  },
  "de": {
    "info": "Info",
    "subgroups": "Untergruppen",
    "clients": "Clienten",
    "name": "Name",
    "description": "Beschreibung",
    "parents": "Übergruppen"
  }
}
</i18n>

<template>
  <div>
    <v-subheader>Info</v-subheader>
    <v-card>
      <v-card-text>
        <v-layout wrap>
          <v-flex lg4 md6 xs12 order-lg1 order-xs2>
            <v-layout column>
              <v-flex>
                <v-text-field prepend-icon="label" v-if="editMode" class="info-input" :label="$t('name')" color="primary" v-model="info.name"></v-text-field>
                <div v-else class="info-input">
                  <div class="body-2 info-heading"><v-icon>label</v-icon><span>{{ $t('name') }}</span></div>
                  <div class="info-text">{{ group.name || '-' }}</div>
                </div>
              </v-flex>
              <v-flex>
                <v-autocomplete
                  prepend-icon="device_hub"
                  v-if="editMode"
                  class="info-input"
                  :items="$store.state.groups.groupList"
                  v-model="parentIds"
                  offset-y
                  :label="$t('parents')"
                  color="primary"
                  multiple
                  item-value="id"
                  item-text="name"
                  small-chips
                  deletable-chips
                >
                </v-autocomplete>
                <div v-else class="info-input">
                  <div class="body-2 info-heading"><v-icon>device_hub</v-icon><span>{{ $t('parents') }}</span></div>
                  <div class="info-text">
                    <template v-if="group.parents && group.parents.length > 0">
                      <v-chip v-for="parent in group.parents" :key="parent.id" small>
                        {{ parent.name || parent.id }}
                      </v-chip>
                    </template>
                    <span v-else>-</span>
                  </div>
                </div>
              </v-flex>
            </v-layout>
          </v-flex>
          <v-flex lg4 md6 xs12 order-lg2 order-xs3>
            <v-textarea prepend-icon="description" v-if="editMode" rows="1" auto-grow class="info-input" :label="$t('description')" color="primary" v-model="info.description"></v-textarea>
            <div v-else class="info-input">
              <div class="body-2 info-heading"><v-icon>description</v-icon><span>{{ $t('description') }}</span></div>
              <pre class="info-text">{{ group.description || '-' }}</pre>
            </div>
          </v-flex>
          <v-flex lg4 xs12 order-lg3 order-xs1 class="text-xs-right">
            <div class="info-input">
              <v-btn v-if="!editMode" color="primary" flat @click="editInfo" class="info-buttons">
                <v-icon left>create</v-icon>{{ $t('edit') }}
              </v-btn>
              <div v-else>
                <v-btn color="primary" flat @click="cancelEdit" class="info-buttons">{{ $t('cancel') }}</v-btn>
                <v-btn color="primary" @click="saveInfo" class="info-buttons">
                  <v-icon left>save</v-icon>{{ $t('save') }}
                </v-btn>
              </div>
            </div>
          </v-flex>
        </v-layout>
      </v-card-text>
    </v-card>
    <template v-if="group.id">
      <v-subheader>{{ $t('subgroups') }}</v-subheader>
      <group-module-group-list :tabIndex="tabIndex" :groupId="group.id" :groups="group.subgroups" />
      <v-subheader>{{ $t('clients') }}</v-subheader>
      <group-module-client-list :tabIndex="tabIndex" :groupId="group.id" :clients="group.clients" />
    </template>
  </div>
</template>

<script>
import GroupModuleGroupList from '@/components/GroupModuleGroupList'
import GroupModuleClientList from '@/components/GroupModuleClientList'

export default {
  name: 'GroupModuleGroupView',
  props: ['tabIndex', 'group'],
  components: {
    GroupModuleGroupList,
    GroupModuleClientList
  },
  data () {
    return {
      editMode: false,
      info: {
        name: '',
        description: ''
      },
      parentIds: []
    }
  },
  created () {
    if (!this.group.id) this.editInfo()
  },
  watch: {
    group (newValue, oldValue) {
      if (!newValue.id) this.editInfo()
      else if (newValue.id !== oldValue.id) this.editMode = false
    }
  },
  methods: {
    removeParent (id) {
      this.parentIds.splice(this.parentIds.indexOf(id), 1)
    },
    editInfo () {
      this.editMode = true
      this.info.name = this.group.name
      this.info.description = this.group.description
      this.parentIds = this.group.parents ? this.group.parents.map(x => x.id) : []
    },
    cancelEdit () {
      this.editMode = false
      if (this.group.id) return
      this.$store.commit('groups/deleteFromTabChain', { index: this.tabIndex, count: 1 })
      this.$store.commit('groups/setActiveTab', this.tabIndex - 1)
    },
    saveInfo () {
      this.$store.dispatch('groups/saveGroup', {
        id: this.group.id,
        info: this.info,
        parentIds: this.parentIds,
        tabIndex: this.tabIndex,
        callback: this.updateUrl
      })
      this.editMode = false
    },
    updateUrl (id) {
      console.log(id)
      this.$router.replace({
        name: 'GroupModule.group',
        params: { id, noReload: true }
      })
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.info-buttons {
  margin: 0;
}
.info-input {
  margin: 20px;
}
.info-heading {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
}
.info-heading > span {
  margin-left: 10px;
}
.info-text {
  margin-left: 34px;
}
</style>