Skip to content

Commit 909e13e

Browse files
committed
Move store to /src and remove local functions from store
Signed-off-by: Olga Bulat <obulat@gmail.com>
1 parent ae0004f commit 909e13e

File tree

4 files changed

+168
-339
lines changed

4 files changed

+168
-339
lines changed

src/main.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Buefy from 'buefy'
33
import i18n from './i18n'
44
import VueAnalytics from 'vue-analytics'
55
import App from './App.vue'
6-
import store from '../store'
6+
import store from './store'
77

88
import { library } from '@fortawesome/fontawesome-svg-core'
99
// internal icons
@@ -13,7 +13,6 @@ import {
1313
faEye, faEyeSlash, faCaretDown, faCaretUp, faUpload, faCopy, faCircle
1414
} from '@fortawesome/free-solid-svg-icons'
1515
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome'
16-
import LicenseUtilities from './utils/license-utilities'
1716

1817
library.add(faCheck, faCheckCircle, faInfoCircle, faExclamationTriangle, faExclamationCircle,
1918
faArrowUp, faAngleRight, faAngleLeft, faAngleDown, faUserCircle, faQuestionCircle,
@@ -33,8 +32,6 @@ if (process.env.NODE_ENV === 'production') {
3332
})
3433
}
3534

36-
Vue.use(LicenseUtilities)
37-
3835
new Vue({
3936
store,
4037
i18n,

src/store/index.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import Vue from 'vue'
2+
import Vuex from 'vuex'
3+
import { CCBYAttributes, CC0Attributes, attrToShort, attrToFull, licenseUrl, licenseIconsArr } from '../utils/license-utilities.js'
4+
5+
Vue.use(Vuex)
6+
7+
export default new Vuex.Store({
8+
state: {
9+
currentLicenseAttributes: { ...CCBYAttributes },
10+
attributionDetails: {
11+
creatorName: '',
12+
creatorProfileUrl: '',
13+
workTitle: '',
14+
workUrl: ''
15+
},
16+
currentStepId: 0
17+
},
18+
getters: {
19+
shortName: state => {
20+
return attrToShort(state.currentLicenseAttributes)
21+
},
22+
fullName: state => {
23+
return attrToFull(state.currentLicenseAttributes)
24+
},
25+
licenseUrl: state => {
26+
return licenseUrl(state.currentLicenseAttributes)
27+
},
28+
iconsList: state => {
29+
return licenseIconsArr(state.currentLicenseAttributes)
30+
},
31+
isAttrSelected: (state) => (attrName) => {
32+
return state.currentLicenseAttributes[attrName]
33+
}
34+
},
35+
mutations: {
36+
toggleSelected(state, stepName) {
37+
// Called when a radio button is clicked, either FirstStep or VerticalStep
38+
if (['BY', 'NC', 'ND', 'SA'].indexOf(stepName) > -1) {
39+
state.currentLicenseAttributes = {
40+
...state.currentLicenseAttributes,
41+
[stepName]: !state.currentLicenseAttributes[stepName]
42+
}
43+
} else {
44+
state.knowLicense = !state.knowLicense
45+
}
46+
},
47+
updateAttributesFromShort(state, shortName) {
48+
if (shortName.includes('CC0')) {
49+
state.currentLicenseAttributes = { ...CC0Attributes }
50+
} else {
51+
state.currentLicenseAttributes.NC = !!shortName.includes('NC')
52+
state.currentLicenseAttributes.ND = !!shortName.includes('ND')
53+
state.currentLicenseAttributes.SA = !!shortName.includes('SA')
54+
}
55+
},
56+
setCreatorName(state, newName) {
57+
state.attributionDetails.creatorName = newName
58+
},
59+
setCreatorProfileUrl(state, newName) {
60+
state.attributionDetails.creatorProfileUrl = newName
61+
},
62+
setWorkTitle(state, newName) {
63+
state.attributionDetails.workTitle = newName
64+
},
65+
setWorkUrl(state, newName) {
66+
state.attributionDetails.workUrl = newName
67+
}
68+
}
69+
})

src/utils/license-utilities.js

Lines changed: 98 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,104 @@
1-
const LicenseUtilities = {
1+
const CC0Attributes = { BY: false, NC: false, ND: false, SA: false }
2+
const CCBYAttributes = { BY: true, NC: false, ND: false, SA: false }
3+
const visibleSetters = {
4+
FS: {
5+
true: ['FS', 'DD', 'AD'],
6+
false: ['FS', 'BY', 'NC', 'ND', 'SA', 'AD']
7+
},
8+
BY: {
9+
// Decide if NC/ND/SA should be disabled or removed on CC0
10+
true: ['FS', 'BY', 'NC', 'ND', 'SA', 'AD'],
11+
false: ['FS', 'BY', 'CW', 'NC', 'ND', 'SA', 'AD']
12+
},
13+
ND: {
14+
true: ['FS', 'BY', 'NC', 'ND', 'SA', 'AD']
15+
}
16+
}
17+
const disabledSetters = {
18+
// Steps that should be disabled if other steps are selected/not selected
19+
BY: {
20+
false: ['NC', 'ND', 'SA']
21+
},
22+
ND: {
23+
true: ['SA']
24+
}
25+
}
226

3-
install(Vue) {
4-
const defaultLicenseAttributes = {
5-
BY: true,
6-
NC: false,
7-
ND: false,
8-
SA: false
9-
}
27+
function shortToAttr(shortLicenseName) {
28+
const short = shortLicenseName
29+
if (short.includes('CC0')) {
30+
return { ...CC0Attributes }
31+
}
32+
const nc = short.includes('NC')
33+
const nd = short.includes('ND')
34+
const sa = short.includes('SA')
35+
return { ...CCBYAttributes, NC: nc, ND: nd, SA: sa }
36+
}
1037

11-
Vue.prototype.$shortToAttributes = function(shortLicenseName) {
12-
const short = shortLicenseName
13-
if (short.includes('CC0')) {
14-
return { ...defaultLicenseAttributes, BY: false }
15-
}
16-
const nc = short.includes('NC')
17-
const nd = short.includes('ND')
18-
const sa = short.includes('SA')
19-
return { ...defaultLicenseAttributes, NC: nc, ND: nd, SA: sa }
20-
}
21-
Vue.prototype.$attrToShort = function(attr) {
22-
if (!attr.BY) { return 'CC0 1.0' }
23-
let base = 'CC BY'
24-
if (attr.NC) { base += '-NC' }
25-
if (!attr.ND && attr.SA) {
26-
base += '-SA'
27-
} else if (attr.ND) {
28-
base += '-ND'
29-
}
30-
base += ' 4.0'
31-
return base
32-
}
33-
Vue.prototype.$attrToFull = function(attr) {
34-
if (!attr.BY) { return 'CC0 1.0 Universal' }
35-
let base = 'Attribution'
36-
if (attr.NC) { base += '-NonCommercial' }
37-
if (!attr.ND && attr.SA) {
38-
base += '-ShareAlike'
39-
} else if (attr.ND) {
40-
base += '-NoDerivatives'
41-
}
42-
base += ' 4.0 International'
43-
return base
44-
}
45-
Vue.prototype.$licenseUrl = function(shortLicenseName) {
46-
// Returns url to license
47-
if (shortLicenseName.includes('CC0')) {
48-
return 'https://creativecommons.org/publicdomain/zero/1.0/?ref=ccchooser'
49-
}
50-
const short = shortLicenseName.toLowerCase().slice(3, shortLicenseName.length - 4)
51-
return 'https://creativecommons.org/licenses/' + short + '/4.0/?ref=ccchooser'
52-
}
53-
Vue.prototype.$licenseSlug = function(shortLicenseName) {
54-
// Returns lower case slugified string of license name without the version number
55-
return shortLicenseName
56-
.toLowerCase()
57-
.replace(' ', '-')
58-
.slice(0, shortLicenseName.length - 4)
59-
}
60-
Vue.prototype.$licenseIconsArr = function(shortLicenseName) {
61-
if (shortLicenseName.includes('CC0')) {
62-
return ['zero']
63-
}
64-
return shortLicenseName
65-
.toLowerCase()
66-
.slice(3, shortLicenseName.length - 4)
67-
.split('-')
38+
function attrToShort(attr) {
39+
if (!attr.BY) { return 'CC0 1.0' }
40+
let base = 'CC BY'
41+
if (attr.NC) { base += '-NC' }
42+
if (!attr.ND && attr.SA) {
43+
base += '-SA'
44+
} else if (attr.ND) {
45+
base += '-ND'
46+
}
47+
base += ' 4.0'
48+
return base
49+
}
50+
51+
function attrToFull(attr) {
52+
if (!attr.BY) { return 'CC0 1.0 Universal' }
53+
let base = 'Attribution'
54+
if (attr.NC) { base += '-NonCommercial' }
55+
if (!attr.ND && attr.SA) {
56+
base += '-ShareAlike'
57+
} else if (attr.ND) {
58+
base += '-NoDerivatives'
59+
}
60+
base += ' 4.0 International'
61+
return base
62+
}
63+
// function licenseUrl(shortLicenseName) {
64+
// Returns url to license from short license name with version number (eg. 'CC BY 4.0')
65+
// TODO: check how it works: it doesn't use '-' to join elements ?
66+
// if (shortLicenseName.includes('CC0')) {
67+
// return 'https://creativecommons.org/publicdomain/zero/1.0/?ref=ccchooser'
68+
// }
69+
// const short = shortLicenseName.toLowerCase().slice(3, shortLicenseName.length - 4)
70+
// return 'https://creativecommons.org/licenses/' + short + '/4.0/?ref=ccchooser'
71+
// }
72+
function licenseUrl(attr) {
73+
// Returns url to license from short license name with version number (eg. 'CC BY 4.0')
74+
if (!attr.BY) {
75+
return 'https://creativecommons.org/publicdomain/zero/1.0/?ref=ccchooser'
76+
}
77+
let short = attrToShort(attr).toLowerCase().slice(3)
78+
short = short.slice(0, short.length - 4)
79+
return 'https://creativecommons.org/licenses/' + short + '/4.0/?ref=ccchooser'
80+
}
81+
82+
function licenseSlug(shortLicenseName) {
83+
// Returns lower case slugified string of license name without the version number
84+
// 'CC BY 4.0' -> 'cc-by'
85+
return shortLicenseName
86+
.toLowerCase()
87+
.replace(' ', '-')
88+
.slice(0, shortLicenseName.length - 4)
89+
}
90+
91+
function licenseIconsArr(licenseAttributes) {
92+
if (!licenseAttributes.BY) {
93+
return ['zero']
94+
}
95+
const iconsArray = []
96+
for (const key in licenseAttributes) {
97+
if (licenseAttributes[key]) {
98+
iconsArray.push(key.toLowerCase())
6899
}
69100
}
101+
return iconsArray
70102
}
71103

72-
export default LicenseUtilities
104+
export { CC0Attributes, CCBYAttributes, visibleSetters, disabledSetters, shortToAttr, attrToShort, attrToFull, licenseUrl, licenseSlug, licenseIconsArr }

0 commit comments

Comments
 (0)