103 lines
3.0 KiB
JavaScript
103 lines
3.0 KiB
JavaScript
import { readFile, writeFile } from 'fs'
|
|
import Vue from 'vue'
|
|
import path from 'path'
|
|
import yaml from 'js-yaml'
|
|
|
|
export default {
|
|
namespaced: true,
|
|
state: {
|
|
data_dir: '/home/lafrite/scripts/comptes/data/',
|
|
config_dir: '/home/lafrite/scripts/comptes/config/',
|
|
config_filename: 'config.yml',
|
|
tags: {},
|
|
categories: {}
|
|
},
|
|
getters: {
|
|
data_dir: (state) => {
|
|
return state.data_dir
|
|
},
|
|
config_dir: (state) => {
|
|
return state.config_dir
|
|
},
|
|
config_filename: (state) => {
|
|
return state.config_filename
|
|
},
|
|
tags: (state) => {
|
|
return state.tags
|
|
},
|
|
tag: (state) => (tagname) => {
|
|
return state.tags[tagname.toLowerCase()]
|
|
},
|
|
categories: (state) => {
|
|
return state.categories
|
|
},
|
|
categorie: (state) => (categorieName) => {
|
|
return state.categories[categorieName.toLowerCase()]
|
|
}
|
|
},
|
|
mutations: {
|
|
SET_TAGS: (state, { tags }) => {
|
|
// Set tags list
|
|
state.tags = Object.keys(tags)
|
|
.reduce((c, k) => (c[k.toLowerCase()] = tags[k], c), {})
|
|
},
|
|
APPEND_TAG: (state, { tag }) => {
|
|
// Append or replace et tag
|
|
Vue.set(state.tags, tag.name.toLowerCase(), tag)
|
|
},
|
|
DELETE_TAG: (state, { tagname }) => {
|
|
// delete tag by its name
|
|
Vue.delete(state.tags, tagname.toLowerCase())
|
|
},
|
|
SET_CATEGORIES: (state, { categories }) => {
|
|
state.categories = Object.keys(categories)
|
|
.reduce((c, k) => (c[k.toLowerCase()] = categories[k], c), {})
|
|
},
|
|
},
|
|
actions: {
|
|
load (context) {
|
|
// load config file
|
|
readFile(path.join(context.getters.config_dir, context.getters.config_filename), 'utf8', (err, content) => {
|
|
if (err) {
|
|
console.log(err)
|
|
} else {
|
|
var parseConfig = {
|
|
header: true
|
|
}
|
|
var parsed = yaml.safeLoad(content, parseConfig)
|
|
context.commit('SET_TAGS', { tags: parsed.tags })
|
|
context.commit('SET_CATEGORIES', { categories: parsed.categories })
|
|
}
|
|
})
|
|
},
|
|
save (context) {
|
|
// save config file
|
|
var config = {
|
|
categories: context.state.categories,
|
|
tags: context.state.tags
|
|
}
|
|
var yamlConfig = yaml.safeDump(config)
|
|
writeFile(path.join(context.getters.config_dir, context.getters.config_filename), yamlConfig, (err) => {
|
|
if (err) throw err
|
|
console.log('The file has been saved!')
|
|
})
|
|
context.dispatch('datas/compute_tags', null, { root: true })
|
|
},
|
|
edit_tag (context, tag) {
|
|
// Edit or append a tag to config
|
|
context.commit('APPEND_TAG', { tag: tag })
|
|
context.dispatch('save')
|
|
},
|
|
delete_tag (context, tagname) {
|
|
// Revome a tag from the config
|
|
context.commit('DELETE_TAG', { tagname: tagname })
|
|
context.dispatch('save')
|
|
},
|
|
append_keywords (context, { tagName, keyword }) {
|
|
var tag = context.getters.tag(tagName)
|
|
tag.words.push(keyword)
|
|
context.dispatch('edit_tag', tag)
|
|
}
|
|
}
|
|
}
|