Skip to content

Commit a92b294

Browse files
author
Hoang Nguyen
authored
ui: Save and auto-expand list domain when reloading (#4769)
* save and auto-expand list domain when reloading * fix for simplify the code * clear variables, prop of common component * fix lint errors found
1 parent 467a1e7 commit a92b294

5 files changed

Lines changed: 92 additions & 6 deletions

File tree

ui/src/components/view/TreeView.vue

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@
3636
:loadData="onLoadData"
3737
:expandAction="false"
3838
:showIcon="true"
39-
:defaultSelectedKeys="defaultSelected"
39+
:selectedKeys="defaultSelected"
4040
:checkStrictly="true"
4141
@select="onSelect"
4242
@expand="onExpand"
43-
:defaultExpandedKeys="arrExpand">
43+
:expandedKeys="arrExpand">
4444
<a-icon slot="parent" type="folder" />
4545
<a-icon slot="leaf" type="block" />
4646
</a-tree>
@@ -122,6 +122,12 @@ export default {
122122
default () {
123123
return []
124124
}
125+
},
126+
treeStore: {
127+
type: Object,
128+
default () {
129+
return {}
130+
}
125131
}
126132
},
127133
data () {
@@ -213,6 +219,39 @@ export default {
213219
}
214220
215221
this.reloadTreeData(newData)
222+
},
223+
treeVerticalData () {
224+
if (!this.treeStore.isExpand) {
225+
return
226+
}
227+
if (this.treeStore.expands && this.treeStore.expands.length > 0) {
228+
for (const expandKey of this.treeStore.expands) {
229+
if (this.arrExpand.includes(expandKey)) {
230+
continue
231+
}
232+
const keyVisible = this.treeVerticalData.findIndex(item => item.key === expandKey)
233+
if (keyVisible > -1) this.arrExpand.push(expandKey)
234+
}
235+
}
236+
237+
if (this.treeStore.selected) {
238+
this.selectedTreeKey = this.treeStore.selected
239+
this.defaultSelected = [this.selectedTreeKey]
240+
241+
const resource = this.treeVerticalData.filter(item => item.id === this.selectedTreeKey)
242+
if (resource.length > 0) {
243+
this.resource = resource[0]
244+
this.$emit('change-resource', this.resource)
245+
} else {
246+
const rootResource = this.treeVerticalData[0]
247+
if (rootResource) {
248+
this.resource = rootResource
249+
this.selectedTreeKey = this.resource.key
250+
this.defaultSelected = [this.selectedTreeKey]
251+
this.$emit('change-resource', this.resource)
252+
}
253+
}
254+
}
216255
}
217256
},
218257
methods: {
@@ -273,10 +312,21 @@ export default {
273312
this.selectedTreeKey = selectedKeys[0]
274313
}
275314
315+
this.defaultSelected = []
316+
this.defaultSelected.push(this.selectedTreeKey)
317+
318+
this.treeStore.expands = this.arrExpand
319+
this.treeStore.selected = this.selectedTreeKey
320+
this.$emit('change-tree-store', this.treeStore)
321+
276322
this.getDetailResource(this.selectedTreeKey)
277323
},
278324
onExpand (treeExpand) {
279325
this.arrExpand = treeExpand
326+
this.treeStore.isExpand = true
327+
this.treeStore.expands = this.arrExpand
328+
this.treeStore.selected = this.selectedTreeKey
329+
this.$emit('change-tree-store', this.treeStore)
280330
},
281331
onSearch (value) {
282332
if (this.searchQuery === '' && this.oldSearchQuery === '') {
@@ -303,6 +353,7 @@ export default {
303353
this.arrExpand = []
304354
this.treeViewData = []
305355
this.loadingSearch = true
356+
this.$emit('change-tree-store', {})
306357
307358
api(this.apiList, params).then(json => {
308359
const listDomains = this.getResponseJsonData(json)

ui/src/store/getters.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ const getters = {
3535
cloudian: state => state.user.cloudian,
3636
zones: state => state.user.zones,
3737
timezoneoffset: state => state.user.timezoneoffset,
38-
usebrowsertimezone: state => state.user.usebrowsertimezone
38+
usebrowsertimezone: state => state.user.usebrowsertimezone,
39+
domainStore: state => state.user.domainStore
3940
}
4041

4142
export default getters

ui/src/store/modules/user.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,17 @@ import router from '@/router'
2424
import store from '@/store'
2525
import { login, logout, api } from '@/api'
2626
import { i18n } from '@/locales'
27-
import { ACCESS_TOKEN, CURRENT_PROJECT, DEFAULT_THEME, APIS, ASYNC_JOB_IDS, ZONES, TIMEZONE_OFFSET, USE_BROWSER_TIMEZONE } from '@/store/mutation-types'
27+
import {
28+
ACCESS_TOKEN,
29+
CURRENT_PROJECT,
30+
DEFAULT_THEME,
31+
APIS,
32+
ZONES,
33+
TIMEZONE_OFFSET,
34+
USE_BROWSER_TIMEZONE,
35+
ASYNC_JOB_IDS,
36+
DOMAIN_STORE
37+
} from '@/store/mutation-types'
2838

2939
const user = {
3040
state: {
@@ -40,7 +50,8 @@ const user = {
4050
cloudian: {},
4151
zones: {},
4252
timezoneoffset: 0.0,
43-
usebrowsertimezone: false
53+
usebrowsertimezone: false,
54+
domainStore: {}
4455
},
4556

4657
mutations: {
@@ -91,6 +102,10 @@ const user = {
91102
SET_ZONES: (state, zones) => {
92103
state.zones = zones
93104
Vue.ls.set(ZONES, zones)
105+
},
106+
SET_DOMAIN_STORE (state, domainStore) {
107+
state.domainStore = domainStore
108+
Vue.ls.set(DOMAIN_STORE, domainStore)
94109
}
95110
},
96111

@@ -126,6 +141,7 @@ const user = {
126141
commit('SET_FEATURES', {})
127142
commit('SET_LDAP', {})
128143
commit('SET_CLOUDIAN', {})
144+
commit('SET_DOMAIN_STORE', {})
129145

130146
notification.destroy()
131147

@@ -142,7 +158,10 @@ const user = {
142158
const cachedZones = Vue.ls.get(ZONES, [])
143159
const cachedTimezoneOffset = Vue.ls.get(TIMEZONE_OFFSET, 0.0)
144160
const cachedUseBrowserTimezone = Vue.ls.get(USE_BROWSER_TIMEZONE, false)
161+
const domainStore = Vue.ls.get(DOMAIN_STORE, {})
145162
const hasAuth = Object.keys(cachedApis).length > 0
163+
164+
commit('SET_DOMAIN_STORE', domainStore)
146165
if (hasAuth) {
147166
console.log('Login detected, using cached APIs')
148167
commit('SET_ZONES', cachedZones)
@@ -250,6 +269,7 @@ const user = {
250269
commit('SET_LDAP', {})
251270
commit('SET_CLOUDIAN', {})
252271
commit('RESET_THEME')
272+
commit('SET_DOMAIN_STORE', {})
253273
Vue.ls.remove(CURRENT_PROJECT)
254274
Vue.ls.remove(ACCESS_TOKEN)
255275
Vue.ls.remove(ASYNC_JOB_IDS)
@@ -304,6 +324,10 @@ const user = {
304324
reject(error)
305325
})
306326
})
327+
},
328+
329+
SetDomainStore ({ commit }, domainStore) {
330+
commit('SET_DOMAIN_STORE', domainStore)
307331
}
308332
}
309333
}

ui/src/store/mutation-types.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export const ZONES = 'ZONES'
3232
export const ASYNC_JOB_IDS = 'ASYNC_JOB_IDS'
3333
export const TIMEZONE_OFFSET = 'TIMEZONE_OFFSET'
3434
export const USE_BROWSER_TIMEZONE = 'USE_BROWSER_TIMEZONE'
35+
export const DOMAIN_STORE = 'DOMAIN_STORE'
3536

3637
export const CONTENT_WIDTH_TYPE = {
3738
Fluid: 'Fluid',

ui/src/views/iam/DomainView.vue

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,11 @@
6262
v-else
6363
:treeData="treeData"
6464
:treeSelected="treeSelected"
65+
:treeStore="domainStore"
6566
:loading="loading"
6667
:tabs="$route.meta.tabs"
6768
@change-resource="changeResource"
69+
@change-tree-store="changeDomainStore"
6870
:actionData="actionData"/>
6971
</div>
7072

@@ -108,7 +110,8 @@ export default {
108110
treeSelected: {},
109111
showAction: false,
110112
action: {},
111-
dataView: false
113+
dataView: false,
114+
domainStore: {}
112115
}
113116
},
114117
computed: {
@@ -129,9 +132,11 @@ export default {
129132
next()
130133
},
131134
beforeRouteLeave (to, from, next) {
135+
this.changeDomainStore({})
132136
next()
133137
},
134138
created () {
139+
this.domainStore = store.getters.domainStore
135140
this.fetchData()
136141
},
137142
watch: {
@@ -306,6 +311,10 @@ export default {
306311
this.treeSelected = resource
307312
this.resource = this.treeSelected
308313
},
314+
changeDomainStore (domainStore) {
315+
this.domainStore = domainStore
316+
store.dispatch('SetDomainStore', domainStore)
317+
},
309318
closeAction () {
310319
this.showAction = false
311320
},

0 commit comments

Comments
 (0)