From 2b0dec5d0ae1a784e9d8370af90116f74b2bce16 Mon Sep 17 00:00:00 2001 From: brunosimon Date: Mon, 14 Aug 2017 22:43:50 +0200 Subject: [PATCH] :art: Site > Store > Separate into modules --- site/src/components/application/script.js | 8 +- site/src/components/application/template.html | 2 +- site/src/components/chat/script.js | 4 +- site/src/components/connexion/script.js | 9 +- site/src/components/files-tree/script.js | 30 +--- site/src/components/project/script.js | 5 +- .../{projects-hub => projects}/index.vue | 0 .../{projects-hub => projects}/script.js | 2 +- .../{projects-hub => projects}/style.styl | 0 .../{projects-hub => projects}/template.html | 0 site/src/components/viewer/script.js | 2 +- site/src/main.js | 2 +- site/src/store.js | 140 ------------------ site/src/store/index.js | 17 +++ site/src/store/modules/chat.js | 32 ++++ site/src/store/modules/files.js | 58 ++++++++ site/src/store/modules/projects.js | 51 +++++++ 17 files changed, 178 insertions(+), 184 deletions(-) rename site/src/components/{projects-hub => projects}/index.vue (100%) rename site/src/components/{projects-hub => projects}/script.js (84%) rename site/src/components/{projects-hub => projects}/style.styl (100%) rename site/src/components/{projects-hub => projects}/template.html (100%) delete mode 100644 site/src/store.js create mode 100644 site/src/store/index.js create mode 100644 site/src/store/modules/chat.js create mode 100644 site/src/store/modules/files.js create mode 100644 site/src/store/modules/projects.js diff --git a/site/src/components/application/script.js b/site/src/components/application/script.js index a0389d0..52c96c2 100644 --- a/site/src/components/application/script.js +++ b/site/src/components/application/script.js @@ -1,5 +1,5 @@ import Connexion from '@/components/connexion' -import ProjectsHub from '@/components/projects-hub' +import Projects from '@/components/projects' import Project from '@/components/project' export default @@ -9,7 +9,7 @@ export default components: { Connexion, - ProjectsHub, + Projects, Project }, @@ -24,12 +24,12 @@ export default { projects() { - return this.$store.state.projects + return this.$store.state.projects.all }, project() { - return this.$store.state.project + return this.$store.state.projects.current } }, diff --git a/site/src/components/application/template.html b/site/src/components/application/template.html index 0a05315..e6976c8 100644 --- a/site/src/components/application/template.html +++ b/site/src/components/application/template.html @@ -1,6 +1,6 @@
{{ counter }} - +
\ No newline at end of file diff --git a/site/src/components/chat/script.js b/site/src/components/chat/script.js index ac9d7bc..7686108 100644 --- a/site/src/components/chat/script.js +++ b/site/src/components/chat/script.js @@ -13,12 +13,12 @@ export default { messages() { - return this.$store.state.chatMessages + return this.$store.state.chat.messages }, user() { - return this.$store.state.user + return this.$store.state.chat.user } }, diff --git a/site/src/components/connexion/script.js b/site/src/components/connexion/script.js index e3c5126..aca7e55 100644 --- a/site/src/components/connexion/script.js +++ b/site/src/components/connexion/script.js @@ -8,12 +8,12 @@ export default { project() { - return this.$store.state.project + return this.$store.state.projects.current }, pendingUser() { - return this.$store.state.pendingUser + return this.$store.state.chat.pendingUser } }, @@ -101,7 +101,10 @@ export default onPendingUser(value) { - this.chatSocket.emit('update_user', value) + if(value) + { + this.chatSocket.emit('update_user', value) + } } } } \ No newline at end of file diff --git a/site/src/components/files-tree/script.js b/site/src/components/files-tree/script.js index faa2f74..94ecf02 100644 --- a/site/src/components/files-tree/script.js +++ b/site/src/components/files-tree/script.js @@ -1,20 +1,5 @@ import FilesTreeFile from '@/components/files-tree-file' import FilesTreeFolder from '@/components/files-tree-folder' -import FileTree from '@/utils/FileTree' - -// const fileTree = new FileTree({ autoWash: true }) -// fileTree.addFolder('./toto', { hey: 'hoy' }) -// fileTree.addFolder('./toto/uhuh', { hey: 'a' }) -// fileTree.addFolder('./lorem') -// fileTree.addFile('./test-1.txt') -// fileTree.addFile('./test-2.txt') - -// window.setInterval(() => -// { -// fileTree.addFile('./toto/uhuh/' + new Date()) -// fileTree.removeFolder('./lorem') -// fileTree.removeFile('./test-1.txt') -// }, 1000) export default { @@ -26,24 +11,11 @@ export default FilesTreeFolder }, - data() - { - return {} - }, - computed: { files() { - return this.$store.state.files + return this.$store.state.files.tree } - }, - - created() - { - }, - - methods: - { } } \ No newline at end of file diff --git a/site/src/components/project/script.js b/site/src/components/project/script.js index 97abff5..c9f0daf 100644 --- a/site/src/components/project/script.js +++ b/site/src/components/project/script.js @@ -17,11 +17,12 @@ export default { project() { - return this.$store.state.project + return this.$store.state.projects.all }, + file() { - return this.$store.state.file + return this.$store.state.files.current } }, diff --git a/site/src/components/projects-hub/index.vue b/site/src/components/projects/index.vue similarity index 100% rename from site/src/components/projects-hub/index.vue rename to site/src/components/projects/index.vue diff --git a/site/src/components/projects-hub/script.js b/site/src/components/projects/script.js similarity index 84% rename from site/src/components/projects-hub/script.js rename to site/src/components/projects/script.js index d00e5c0..d001419 100644 --- a/site/src/components/projects-hub/script.js +++ b/site/src/components/projects/script.js @@ -6,7 +6,7 @@ export default { projects() { - return this.$store.state.projects + return this.$store.state.projects.all } }, diff --git a/site/src/components/projects-hub/style.styl b/site/src/components/projects/style.styl similarity index 100% rename from site/src/components/projects-hub/style.styl rename to site/src/components/projects/style.styl diff --git a/site/src/components/projects-hub/template.html b/site/src/components/projects/template.html similarity index 100% rename from site/src/components/projects-hub/template.html rename to site/src/components/projects/template.html diff --git a/site/src/components/viewer/script.js b/site/src/components/viewer/script.js index 9e33a26..1ed2e4b 100644 --- a/site/src/components/viewer/script.js +++ b/site/src/components/viewer/script.js @@ -10,7 +10,7 @@ export default { version() { - return this.$store.state.version + return this.$store.state.files.currentVersion } } } \ No newline at end of file diff --git a/site/src/main.js b/site/src/main.js index 6f3d431..9d3e3a2 100644 --- a/site/src/main.js +++ b/site/src/main.js @@ -2,7 +2,7 @@ // (runtime-only or standalone) has been set in webpack.base.conf with an alias. import Vue from 'vue' import App from '@/components/application/' -import store from '@/store.js' +import store from '@/store/' Vue.config.productionTip = false diff --git a/site/src/store.js b/site/src/store.js deleted file mode 100644 index 88a4e64..0000000 --- a/site/src/store.js +++ /dev/null @@ -1,140 +0,0 @@ -import Vue from 'vue' -import Vuex from 'vuex' -import FileTree from '@/utils/FileTree' - -Vue.use(Vuex) - -export default new Vuex.Store({ - state: - { - projects: {}, - project: null, - files: new FileTree({ autoWash: true }), - file: null, - version: null, - chatMessages: [], - user: null, - pendingUser: null - }, - - mutations: - { - /** - * Projects - */ - updateProjects(state, data) - { - state.projects = data - - // Has a current project - if(state.project) - { - // Current project has been removed - if(typeof state.projects[data] === 'undefined') - { - state.project = null - } - } - - // No current project - else - { - const projectKeys = Object.keys(state.projects) - - // If only one project, set has current - if(projectKeys.length === 1) - { - state.project = state.projects[projectKeys[0]] - } - } - }, - - addProject(state, data) - { - state.projects.push(data) - }, - - setProject(state, data) - { - // Test of project exist - if(typeof state.projects[data] !== 'undefined') - { - state.project = state.projects[data] - } - }, - - /** - * Files - */ - updateFiles(state, data) - { - state.files = new FileTree({ autoWash: true }) - - for(const file of data) - { - state.files.addFile(file.path.full, file) - } - }, - - createFile(state, data) - { - state.files.addFile(data.path.full, data) - }, - - deleteFile(state, data) - { - state.files.removeFile(data.path.full) - }, - - setFile(state, data) - { - const file = state.files.getFile(data) - - if(file) - { - state.file = file - } - }, - - createVersion(state, data) - { - const file = state.files.getFile(data.file) - - if(file) - { - file.data.versions.push(data.version) - } - }, - - setVersion(state, data) - { - state.version = data - }, - - /** - * Messages - */ - createMessage(state, data) - { - state.chatMessages.push(data) - - if(state.chatMessages.length > 100) - { - state.chatMessages.splice(0, state.chatMessages.length - 100) - } - }, - - /** - * User - */ - updateUser(state, data) - { - state.user = data - }, - - askUpdateUser(state, data) - { - state.pendingUser = data - } - } -}) \ No newline at end of file diff --git a/site/src/store/index.js b/site/src/store/index.js new file mode 100644 index 0000000..1d2a011 --- /dev/null +++ b/site/src/store/index.js @@ -0,0 +1,17 @@ +import Vue from 'vue' +import Vuex from 'vuex' + +import chatModule from './modules/chat' +import projectsModule from './modules/projects' +import filesModule from './modules/files' + +Vue.use(Vuex) + +export default new Vuex.Store({ + modules: + { + chat: chatModule, + projects: projectsModule, + files: filesModule + } +}) \ No newline at end of file diff --git a/site/src/store/modules/chat.js b/site/src/store/modules/chat.js new file mode 100644 index 0000000..d57584e --- /dev/null +++ b/site/src/store/modules/chat.js @@ -0,0 +1,32 @@ +export default { + state: + { + user: null, + pendingUser: null, + messages: [], + pendingMessage: null + }, + + mutations: + { + createMessage(state, data) + { + state.messages.push(data) + + if(state.messages.length > 100) + { + state.messages.splice(0, state.messages.length - 100) + } + }, + + updateUser(state, data) + { + state.user = data + }, + + askUpdateUser(state, data) + { + state.pendingUser = data + } + } +} \ No newline at end of file diff --git a/site/src/store/modules/files.js b/site/src/store/modules/files.js new file mode 100644 index 0000000..ad5f30f --- /dev/null +++ b/site/src/store/modules/files.js @@ -0,0 +1,58 @@ +import FileTree from '@/utils/FileTree' + +export default { + state: + { + tree: new FileTree({ autoWash: true }), + current: null, + currentVersion: null + }, + + mutations: + { + updateFiles(state, data) + { + state.tree = new FileTree({ autoWash: true }) + + for(const file of data) + { + state.tree.addFile(file.path.full, file) + } + }, + + createFile(state, data) + { + state.tree.addFile(data.path.full, data) + }, + + deleteFile(state, data) + { + state.tree.removeFile(data.path.full) + }, + + setFile(state, data) + { + const file = state.tree.getFile(data) + + if(file) + { + state.current = file + } + }, + + createVersion(state, data) + { + const file = state.tree.getFile(data.file) + + if(file) + { + file.data.versions.push(data.version) + } + }, + + setVersion(state, data) + { + state.currentVersion = data + } + } +} \ No newline at end of file diff --git a/site/src/store/modules/projects.js b/site/src/store/modules/projects.js new file mode 100644 index 0000000..795df4a --- /dev/null +++ b/site/src/store/modules/projects.js @@ -0,0 +1,51 @@ +export default { + state: + { + all: {}, + current: null + }, + + mutations: + { + updateProjects(state, data) + { + state.all = data + + // Has a current project + if(state.current) + { + // Current project has been removed + if(typeof state.all[data] === 'undefined') + { + state.current = null + } + } + + // No current project + else + { + const projectKeys = Object.keys(state.all) + + // If only one project, set has current + if(projectKeys.length === 1) + { + state.current = state.all[projectKeys[0]] + } + } + }, + + addProject(state, data) + { + state.all.push(data) + }, + + setProject(state, data) + { + // Test of project exist + if(typeof state.all[data] !== 'undefined') + { + state.current = state.all[data] + } + }, + } +} \ No newline at end of file