Skip to content

Commit 2788c14

Browse files
committed
refactor: split 'settings' Vuex store into 'categories' and 'views'
1 parent 1beade2 commit 2788c14

7 files changed

Lines changed: 121 additions & 90 deletions

File tree

src/components/CategoryEditTree.vue

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export default {
7878
},
7979
computed: {
8080
allCategories: function () {
81-
const categories = this.$store.getters['settings/all_categories'];
81+
const categories = this.$store.getters['categories/all_categories'];
8282
const entries = categories.map(c => {
8383
return { text: c.join('->'), value: c };
8484
});
@@ -94,7 +94,7 @@ export default {
9494
},
9595
methods: {
9696
addSubclass: function (parent) {
97-
this.$store.commit('settings/addClass', {
97+
this.$store.commit('categories/addClass', {
9898
name: parent.name.concat(['New class']),
9999
rule: { type: 'regex', regex: 'FILL ME' },
100100
});
@@ -103,7 +103,7 @@ export default {
103103
// TODO: Show a confirmation dialog
104104
// TODO: Remove children as well?
105105
// TODO: Move button to edit modal?
106-
this.$store.commit('settings/removeClass', _class);
106+
this.$store.commit('categories/removeClass', _class);
107107
},
108108
showEditModal() {
109109
this.$refs.edit.show();
@@ -130,7 +130,7 @@ export default {
130130
name: this.editing.parent.concat(this.editing.name),
131131
rule: this.editing.rule.type !== null ? this.editing.rule : { type: null },
132132
};
133-
this.$store.commit('settings/updateClass', new_class);
133+
this.$store.commit('categories/updateClass', new_class);
134134
135135
// Hide the modal manually
136136
this.$nextTick(() => {

src/store/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import Vue from 'vue';
22
import Vuex from 'vuex';
33
import activity from './modules/activity';
44
import buckets from './modules/buckets';
5-
import settings from './modules/settings';
5+
import categories from './modules/categories';
6+
import views from './modules/views';
67
//import createLogger from '../../../src/plugins/logger';
78

89
Vue.use(Vuex);
@@ -13,7 +14,8 @@ export default new Vuex.Store({
1314
modules: {
1415
activity,
1516
buckets,
16-
settings,
17+
categories,
18+
views,
1719
},
1820
strict: debug,
1921
// plugins: debug ? [createLogger()] : [],
Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -6,47 +6,8 @@ import {
66
build_category_hierarchy,
77
} from '~/util/classes';
88

9-
const defaultViews = [
10-
{
11-
id: 'summary',
12-
name: 'Summary',
13-
elements: [
14-
{ type: 'top_apps', size: 3 },
15-
{ type: 'top_titles', size: 3 },
16-
{ type: 'top_domains', size: 3 },
17-
{ type: 'top_categories', size: 3 },
18-
{ type: 'category_tree', size: 3 },
19-
{ type: 'category_sunburst', size: 3 },
20-
],
21-
},
22-
{
23-
id: 'window',
24-
name: 'Window',
25-
elements: [
26-
{ type: 'top_apps', size: 3 },
27-
{ type: 'top_titles', size: 3 },
28-
],
29-
},
30-
{
31-
id: 'browser',
32-
name: 'Browser',
33-
elements: [
34-
{ type: 'top_domains', size: 3 },
35-
{ type: 'top_urls', size: 3 },
36-
],
37-
},
38-
{
39-
id: 'editor',
40-
name: 'Editor',
41-
elements: [
42-
// TODO: Migrate ActivityEditor to ActivityView
43-
],
44-
},
45-
];
46-
479
// initial state
4810
const _state = {
49-
views: [],
5011
classes: [],
5112
classes_unsaved_changes: false,
5213
};
@@ -76,7 +37,6 @@ const getters = {
7637
// actions
7738
const actions = {
7839
async load({ commit }) {
79-
commit('loadViews');
8040
commit('loadClasses', await loadClasses());
8141
},
8242
async save({ state, commit }) {
@@ -88,29 +48,6 @@ const actions = {
8848

8949
// mutations
9050
const mutations = {
91-
loadViews(state) {
92-
const views_json = localStorage.views;
93-
if (views_json && views_json.length >= 1) {
94-
state.views = JSON.parse(views_json);
95-
} else {
96-
state.views = defaultViews;
97-
}
98-
console.log('Loaded views:', state.views);
99-
},
100-
addView(state, { view_id }) {
101-
state.views.push({ id: view_id, name: view_id, elements: [] });
102-
},
103-
editView(state, { view_id, el_id, type }) {
104-
console.log(view_id, el_id, type);
105-
console.log(state.views);
106-
state.views.find(v => v.id == view_id).elements[el_id].type = type;
107-
},
108-
addVisualization(state, { view_id, type }) {
109-
state.views.find(v => v.id == view_id).elements.push({ type: type });
110-
},
111-
removeVisualization(state, { view_id, el_id }) {
112-
state.views.find(v => v.id == view_id).elements.splice(el_id, 1);
113-
},
11451
loadClasses(state, classes) {
11552
let i = 0;
11653
state.classes = classes.map(c => Object.assign(c, { id: i++ }));

src/store/modules/views.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
const defaultViews = [
2+
{
3+
id: 'summary',
4+
name: 'Summary',
5+
elements: [
6+
{ type: 'top_apps', size: 3 },
7+
{ type: 'top_titles', size: 3 },
8+
{ type: 'top_domains', size: 3 },
9+
{ type: 'top_categories', size: 3 },
10+
{ type: 'category_tree', size: 3 },
11+
{ type: 'category_sunburst', size: 3 },
12+
],
13+
},
14+
{
15+
id: 'window',
16+
name: 'Window',
17+
elements: [
18+
{ type: 'top_apps', size: 3 },
19+
{ type: 'top_titles', size: 3 },
20+
],
21+
},
22+
{
23+
id: 'browser',
24+
name: 'Browser',
25+
elements: [
26+
{ type: 'top_domains', size: 3 },
27+
{ type: 'top_urls', size: 3 },
28+
],
29+
},
30+
{
31+
id: 'editor',
32+
name: 'Editor',
33+
elements: [
34+
// TODO: Migrate ActivityEditor to ActivityView
35+
],
36+
},
37+
];
38+
39+
// initial state
40+
const _state = {
41+
views: [],
42+
};
43+
44+
// getters
45+
const getters = {};
46+
47+
// actions
48+
const actions = {
49+
async load({ commit }) {
50+
commit('loadViews');
51+
},
52+
// TODO
53+
//async save({ state, commit }) {
54+
// ...
55+
//},
56+
};
57+
58+
// mutations
59+
const mutations = {
60+
loadViews(state) {
61+
const views_json = localStorage.views;
62+
if (views_json && views_json.length >= 1) {
63+
state.views = JSON.parse(views_json);
64+
} else {
65+
state.views = defaultViews;
66+
}
67+
console.log('Loaded views:', state.views);
68+
},
69+
addView(state, { view_id }) {
70+
state.views.push({ id: view_id, name: view_id, elements: [] });
71+
},
72+
editView(state, { view_id, el_id, type }) {
73+
console.log(view_id, el_id, type);
74+
console.log(state.views);
75+
state.views.find(v => v.id == view_id).elements[el_id].type = type;
76+
},
77+
addVisualization(state, { view_id, type }) {
78+
state.views.find(v => v.id == view_id).elements.push({ type: type });
79+
},
80+
removeVisualization(state, { view_id, el_id }) {
81+
state.views.find(v => v.id == view_id).elements.splice(el_id, 1);
82+
},
83+
};
84+
85+
export default {
86+
namespaced: true,
87+
state: _state,
88+
getters,
89+
actions,
90+
mutations,
91+
};

src/views/activity/Activity.vue

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ export default {
139139
},
140140
computed: {
141141
views: function () {
142-
return this.$store.state.settings.views;
142+
return this.$store.state.views.views;
143143
},
144144
currentView: function () {
145145
return this.views.find(v => v.id == this.$route.params.view_id) || this.views[0];
@@ -151,7 +151,7 @@ export default {
151151
return this.$route.meta.subview;
152152
},
153153
categories: function () {
154-
const cats = this.$store.getters['settings/all_categories'];
154+
const cats = this.$store.getters['categories/all_categories'];
155155
const entries = cats.map(c => {
156156
return { text: c.join(' > '), value: c };
157157
});
@@ -162,7 +162,7 @@ export default {
162162
},
163163
filterCategories: function () {
164164
if (this.filterCategory) {
165-
const cats = this.$store.getters['settings/all_categories'];
165+
const cats = this.$store.getters['categories/all_categories'];
166166
const isChild = p => c => c.length > p.length && _.isEqual(p, c.slice(0, p.length));
167167
const children = _.filter(cats, isChild(this.filterCategory));
168168
return [this.filterCategory].concat(children);
@@ -209,16 +209,17 @@ export default {
209209
},
210210
211211
mounted: async function () {
212-
this.$store.dispatch('settings/load');
212+
this.$store.dispatch('views/load');
213+
this.$store.dispatch('categories/load');
213214
await this.refresh();
214215
},
215216
216217
methods: {
217218
addView: function () {
218219
// TODO: Open modal to ask for options like id, and name
219220
// FIXME: view_id is not guaranteed to be unique
220-
this.$store.commit('settings/addView', {
221-
view_id: this.$store.state.settings.views.length + 1,
221+
this.$store.commit('views/addView', {
222+
view_id: this.$store.state.views.views.length + 1,
222223
});
223224
},
224225
previousPeriod: function () {

src/views/activity/ActivityView.vue

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ export default {
4444
computed: {
4545
view: function () {
4646
if (this.view_id == 'default') {
47-
return this.$store.state.settings.views[0];
47+
return this.$store.state.views.views[0];
4848
} else {
49-
return this.$store.state.settings.views.find(v => v.id == this.view_id);
49+
return this.$store.state.views.views.find(v => v.id == this.view_id);
5050
}
5151
},
5252
},
@@ -59,19 +59,19 @@ export default {
5959
},
6060
addVisualization: function () {
6161
const view_id =
62-
this.view_id == 'default' ? this.$store.state.settings.views[0].id : this.view_id;
63-
this.$store.commit('settings/addVisualization', { view_id, type: 'top_apps' });
62+
this.view_id == 'default' ? this.$store.state.views.views[0].id : this.view_id;
63+
this.$store.commit('views/addVisualization', { view_id, type: 'top_apps' });
6464
},
6565
async onTypeChange(id, type) {
6666
const view_id =
67-
this.view_id == 'default' ? this.$store.state.settings.views[0].id : this.view_id;
68-
await this.$store.commit('settings/editView', { view_id: view_id, el_id: id, type });
67+
this.view_id == 'default' ? this.$store.state.views.views[0].id : this.view_id;
68+
await this.$store.commit('views/editView', { view_id: view_id, el_id: id, type });
6969
},
7070
async onRemove(id) {
7171
console.log('rem');
7272
const view_id =
73-
this.view_id == 'default' ? this.$store.state.settings.views[0].id : this.view_id;
74-
await this.$store.commit('settings/removeVisualization', { view_id: view_id, el_id: id });
73+
this.view_id == 'default' ? this.$store.state.views.views[0].id : this.view_id;
74+
await this.$store.commit('views/removeVisualization', { view_id: view_id, el_id: id });
7575
},
7676
},
7777
};

src/views/settings/CategorizationSettings.vue

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,27 @@ export default {
3737
CategoryEditTree,
3838
},
3939
computed: {
40-
...mapGetters('settings', ['classes_hierarchy']),
41-
...mapState('settings', ['classes_unsaved_changes']),
40+
...mapGetters('categories', ['classes_hierarchy']),
41+
...mapState('categories', ['classes_unsaved_changes']),
4242
},
4343
mounted() {
44-
this.$store.dispatch('settings/load');
44+
this.$store.dispatch('categories/load');
4545
},
4646
methods: {
4747
addClass: function () {
48-
this.$store.commit('settings/addClass', {
48+
this.$store.commit('categories/addClass', {
4949
name: ['New class'],
5050
rule: { type: 'regex', regex: 'FILL ME' },
5151
});
5252
},
5353
saveClasses: async function () {
54-
await this.$store.dispatch('settings/save');
54+
await this.$store.dispatch('categories/save');
5555
},
5656
resetClasses: async function () {
57-
await this.$store.dispatch('settings/load');
57+
await this.$store.dispatch('categories/load');
5858
},
5959
restoreDefaultClasses: async function () {
60-
await this.$store.commit('settings/restoreDefaultClasses');
60+
await this.$store.commit('categories/restoreDefaultClasses');
6161
},
6262
},
6363
};

0 commit comments

Comments
 (0)