Delegate data around the app
parent
637e0c6cb9
commit
10c7ced0b7
|
@ -29,15 +29,15 @@ limitations under the License.
|
||||||
<div class="main container">
|
<div class="main container">
|
||||||
<div class="tabs">
|
<div class="tabs">
|
||||||
<ul>
|
<ul>
|
||||||
<li v-for="index in domains" :class="index === active ? 'is-active' : undefined">
|
<li v-for="(data, index) in domains" :class="index === active ? 'is-active' : undefined">
|
||||||
<a @click="active = index">{{ domainTitle(index) }}</a>
|
<a @click="active = index">{{ data.python.test.value }}{{ changes(index) }}</a>
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<template v-for="index in domains">
|
<template v-for="(data, index) in domains">
|
||||||
<Domain ref="domains"
|
<Domain :key="index"
|
||||||
:key="index"
|
:data="data"
|
||||||
:style="{ display: index === active ? 'block' : 'none' }"
|
:style="{ display: index === active ? 'block' : 'none' }"
|
||||||
></Domain>
|
></Domain>
|
||||||
</template>
|
</template>
|
||||||
|
@ -63,16 +63,21 @@ limitations under the License.
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
i18n,
|
i18n,
|
||||||
domains: 1,
|
domains: [
|
||||||
active: 1,
|
Domain.delegated,
|
||||||
|
],
|
||||||
|
active: 0,
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
domainTitle(index) {
|
changes(index) {
|
||||||
if (this.$refs.domains) {
|
const data = this.$data.domains[index];
|
||||||
const data = this.$refs.domains[index-1].export();
|
const changes = Object.values(data).reduce((prev, current) => {
|
||||||
return data.Server.domain;
|
prev += Object.values(current).filter(d => d.default !== d.computed).length;
|
||||||
}
|
return prev;
|
||||||
|
}, 0);
|
||||||
|
if (changes) return ` (${changes.toLocaleString()})`;
|
||||||
|
return '';
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
|
@ -18,62 +18,48 @@ limitations under the License.
|
||||||
<div>
|
<div>
|
||||||
<div class="tabs">
|
<div class="tabs">
|
||||||
<ul>
|
<ul>
|
||||||
<li v-for="(_, key) in tabs" :class="key === tab ? 'is-active' : undefined">
|
<li v-for="tab in tabs" :class="active === tab.key ? 'is-active' : undefined">
|
||||||
<a @click="tab = key">{{ key }}{{ sectionChanges(key) }}</a>
|
<a @click="active = tab.key">{{ tab.display }}{{ changes(tab.key) }}</a>
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
<component :is="component"
|
|
||||||
v-for="(component, key) in tabs"
|
<component :is="tab"
|
||||||
:ref="key"
|
v-for="tab in tabs"
|
||||||
:key="key"
|
:key="tab.key"
|
||||||
:style="{ display: key === tab ? 'block' : 'none' }"
|
:data="$props.data[tab.key]"
|
||||||
|
:style="{ display: active === tab.key ? 'block' : 'none' }"
|
||||||
></component>
|
></component>
|
||||||
<a class="button" @click="log">Log export data to console</a>
|
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
import * as Sections from './domain_sections';
|
import { Python } from './domain_sections';
|
||||||
|
|
||||||
|
const tabs = [ Python ];
|
||||||
|
const delegated = tabs.reduce((prev, tab) => {
|
||||||
|
prev[tab.key] = tab.delegated;
|
||||||
|
return prev;
|
||||||
|
}, {});
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: 'Domain',
|
name: 'Domain',
|
||||||
|
delegated, // Data the parent will present here
|
||||||
|
props: {
|
||||||
|
data: Object, // Data delegated back to us from parent
|
||||||
|
},
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
tab: Object.keys(Sections)[0],
|
active: tabs[0].key,
|
||||||
tabs: Sections,
|
tabs,
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
sectionChanges(key) {
|
changes(tab) {
|
||||||
if (key in this.$refs && this.$refs[key] && this.$refs[key][0].changes) {
|
const changes = Object.values(this.$props.data[tab]).filter(d => d.default !== d.computed).length;
|
||||||
const changes = this.$refs[key][0].changes();
|
if (changes) return ` (${changes.toLocaleString()})`;
|
||||||
if (changes) {
|
|
||||||
return ` (${changes.toLocaleString()})`;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return '';
|
return '';
|
||||||
},
|
},
|
||||||
changes() {
|
|
||||||
return Object.keys(Sections).reduce((prev, key) => {
|
|
||||||
if (key in this.$refs && this.$refs[key] && this.$refs[key][0].changes) {
|
|
||||||
prev += this.$refs[key][0].changes();
|
|
||||||
}
|
|
||||||
return prev;
|
|
||||||
}, 0);
|
|
||||||
},
|
|
||||||
exports () {
|
|
||||||
return Object.keys(Sections).reduce((prev, key) => {
|
|
||||||
prev[key] = {};
|
|
||||||
if (key in this.$refs && this.$refs[key] && this.$refs[key][0].exports) {
|
|
||||||
prev[key] = this.$refs[key][0].exports();
|
|
||||||
}
|
|
||||||
return prev;
|
|
||||||
}, {});
|
|
||||||
},
|
|
||||||
log () {
|
|
||||||
console.log(this.exports());
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|
|
@ -1,46 +1,43 @@
|
||||||
<template>
|
<template>
|
||||||
<div>Hello world python</div>
|
<div>
|
||||||
|
Hello world python
|
||||||
|
<input v-model="test" type="text" />
|
||||||
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
import i18n from '../../i18n';
|
import i18n from '../../i18n';
|
||||||
|
import delegatedFromDefaults from '../../util/delegated_from_defaults';
|
||||||
|
import computedFromDefaults from '../../util/computed_from_defaults';
|
||||||
|
|
||||||
const defaults = {
|
const defaults = {
|
||||||
python: false,
|
python: {
|
||||||
djangoRules: false,
|
default: false,
|
||||||
|
enabled: true,
|
||||||
|
},
|
||||||
|
djangoRules: {
|
||||||
|
default: false,
|
||||||
|
enabled: true,
|
||||||
|
},
|
||||||
|
test: {
|
||||||
|
default: '',
|
||||||
|
enabled: true,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: 'DomainPython',
|
name: 'DomainPython', // Component name
|
||||||
|
display: 'Python', // Display name for tab
|
||||||
|
key: 'python', // Key for data in parent
|
||||||
|
delegated: delegatedFromDefaults(defaults), // Data the parent will present here
|
||||||
props: {
|
props: {
|
||||||
data: Object,
|
data: Object, // Data delegated back to us from parent
|
||||||
},
|
},
|
||||||
data () {
|
data () {
|
||||||
return {
|
return {
|
||||||
i18n,
|
i18n,
|
||||||
defaults,
|
|
||||||
...defaults,
|
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
created () {
|
computed: computedFromDefaults(defaults), // Getters & setters for the delegated data
|
||||||
if (this.$props.data) {
|
|
||||||
for (const key in this.$props.data) {
|
|
||||||
if (key in defaults) {
|
|
||||||
this.$data[key] = this.$props.data[key];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
methods: {
|
|
||||||
exports() {
|
|
||||||
return Object.keys(defaults).reduce((prev, key) => {
|
|
||||||
prev[key] = this.$data[key];
|
|
||||||
return prev;
|
|
||||||
}, {});
|
|
||||||
},
|
|
||||||
},
|
|
||||||
changes() {
|
|
||||||
return Object.keys(defaults).filter(key => defaults[key] !== this.$data[key]).length;
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|
|
@ -0,0 +1,14 @@
|
||||||
|
export default (defaults) => {
|
||||||
|
return Object.keys(defaults).reduce((prev, key) => {
|
||||||
|
prev[key] = {
|
||||||
|
get() {
|
||||||
|
return this.$props.data[key].value;
|
||||||
|
},
|
||||||
|
set (value) {
|
||||||
|
this.$props.data[key].value = value;
|
||||||
|
this.$props.data[key].computed = value;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
return prev;
|
||||||
|
}, {});
|
||||||
|
};
|
|
@ -0,0 +1,10 @@
|
||||||
|
export default (defaults) => {
|
||||||
|
return Object.keys(defaults).reduce((prev, key) => {
|
||||||
|
prev[key] = {
|
||||||
|
value: defaults[key].default,
|
||||||
|
computed: defaults[key].default,
|
||||||
|
...defaults[key],
|
||||||
|
};
|
||||||
|
return prev;
|
||||||
|
}, {});
|
||||||
|
};
|
Loading…
Reference in New Issue