-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip: Refactor activity management #859
- Loading branch information
Showing
4 changed files
with
149 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<template> | ||
<KPage> | ||
<template v-slot:page-content> | ||
<slot /> | ||
</template> | ||
</KPage> | ||
</template> | ||
|
||
<script> | ||
import _ from 'lodash' | ||
import logger from 'loglevel' | ||
import config from 'config' | ||
import { useActivity, useLayout } from '../composables' | ||
import KPage from './layout/KPage.vue' | ||
export default { | ||
components: { | ||
KPage, | ||
}, | ||
props: { | ||
name: { | ||
type: String, | ||
required: true | ||
}, | ||
layout: { | ||
type: [Object, Function], | ||
default: () => null | ||
} | ||
}, | ||
setup (props) { | ||
const keyName = `${_.camelCase(props.name)}Activity` | ||
logger.debug(`[KDK] Reading '${props.name}' activity options with key ${keyName}`) | ||
const options = _.get(config, keyName, {}) | ||
const { setCurrentActivity } = useActivity(keyName, options) | ||
const { configureLayout, clearLayout } = useLayout() | ||
return { | ||
options, | ||
setCurrentActivity, | ||
configureLayout, | ||
clearLayout | ||
} | ||
}, | ||
methods: { | ||
async configure () { | ||
logger.debug(`[KDK] Configuring '${this.name}' activity`) | ||
// because this component is wrapped within an AsyncComponent it returns the grand parent | ||
const concreteActivity = this.$parent.$parent | ||
// configure the layout | ||
let customLayout = {} | ||
if (this.layout) { | ||
if (typeof this.layout === 'function') customLayout = await this.layout() | ||
else customLayout = this.layout | ||
} | ||
this.configureLayout(_.merge({}, this.options, customLayout), concreteActivity) | ||
// set the current activity | ||
this.setCurrentActivity(concreteActivity) | ||
} | ||
}, | ||
async mounted () { | ||
await this.configure() | ||
// whenever the user abilities are updated, update activity as well | ||
this.$events.on('user-abilities-changed', this.configure) | ||
}, | ||
beforeUnmount () { | ||
logger.debug(`[KDK] Clearing '${this.name}' activity`) | ||
this.$events.off('user-abilities-changed', this.configure) | ||
// clear the current activity | ||
this.setCurrentActivity(null) | ||
// Clear the layout | ||
this.clearLayout() | ||
} | ||
} | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { Layout } from '../layout.js' | ||
|
||
export function useLayout () { | ||
|
||
// functions | ||
function configureLayout (configuration, context) { | ||
if (configuration.header) Layout.setHeader(configuration.header, context) | ||
if (configuration.footer) Layout.setFooter(configuration.footer, context) | ||
if (configuration.page) Layout.setPage(configuration.page, context) | ||
if (configuration.fab) Layout.setFab(configuration.fab, context) | ||
Layout.placements.forEach(placement => { | ||
if (_.has(configuration, `panes.${placement}`)) Layout.setPane(placement, _.get(configuration, `panes.${placement}`), context) | ||
if (_.has(configuration, `windows.${placement}`)) Layout.setWindow(placement, _.get(configuration, `windows.${placement}`), context) | ||
}) | ||
// for backward compatibility | ||
if (configuration.leftPane) Layout.setPane('left', configuration.leftPane, context) | ||
if (configuration.rightPane) Layout.setPane('right', configuration.rightPane, context) | ||
if (configuration.topPane) Layout.setPane('top', configuration.topPane, context) | ||
if (configuration.bottomPane) Layout.setPane('bottom', configuration.bottomPane, context) | ||
} | ||
function clearLayout () { | ||
Layout.clearHeader() | ||
Layout.clearFooter() | ||
Layout.clearPage() | ||
Layout.clearFab() | ||
Layout.placements.forEach(placement => { | ||
Layout.clearPane(placement) | ||
Layout.clearWindow(placement) | ||
}) | ||
} | ||
|
||
// immediate | ||
const additionalFunctions = {} | ||
Layout.placements.forEach(placement => { | ||
additionalFunctions[`set${_.upperFirst(placement)}Pane`] = (options, context) => { Layout.setPane(placement, options, context) } | ||
additionalFunctions[`set${_.upperFirst(placement)}PaneMode`] = (mode) => { Layout.setPaneMode(placement, mode) } | ||
additionalFunctions[`set${_.upperFirst(placement)}PaneFilter`] = (filter) => { Layout.setPaneFilter(placement, filter) } | ||
additionalFunctions[`set${_.upperFirst(placement)}PaneVisible`] = (visible) => { Layout.setPaneVisible(placement, visible) } | ||
additionalFunctions[`set${_.upperFirst(placement)}PaneOpener`] = (opener) => { Layout.setPaneOpener(placement, opener) } | ||
additionalFunctions[`clear${_.upperFirst(placement)}Pane`] = () => { Layout.clearPane(placement) } | ||
}) | ||
|
||
// expose | ||
return { | ||
layout: Layout, | ||
configureLayout, | ||
clearLayout, | ||
...additionalFunctions | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters