diff --git a/client/web/compose/src/components/Admin/Page/Builder/Selector.vue b/client/web/compose/src/components/Admin/Page/Builder/Selector.vue index f2e032f872..8e60f5b436 100644 --- a/client/web/compose/src/components/Admin/Page/Builder/Selector.vue +++ b/client/web/compose/src/components/Admin/Page/Builder/Selector.vue @@ -33,31 +33,32 @@
[], }, - selectableNamespaceGlobalBlocks: { + selectableGlobalBlocks: { type: Array, default: () => [], }, @@ -160,7 +160,7 @@ export default { return { current: undefined, - selectedLayoutExistingBlock: undefined, + selectedLayoutBlock: undefined, selectedGlobalBlock: undefined, types: [ @@ -275,22 +275,26 @@ export default { setDefaultValues () { this.current = undefined - this.selectedLayoutExistingBlock = undefined + this.selectedLayoutBlock = undefined this.selectedGlobalBlock = undefined this.types = [] }, - getOptionKey ({ blockID }) { - return blockID - }, - fetchBlockData (blockID) { if (blockID.includes('-')) { - return this.selectableNamespaceGlobalBlocks.find((b) => b.blockID === blockID) + return this.selectableGlobalBlocks.find((b) => b.blockID === blockID) } return this.existingLayoutBlocks.find((b) => b.blockID === blockID) }, + + selectBlock (block, clone = false) { + if (clone) { + this.$emit('select', this.fetchBlockData(block).clone()) + } else { + this.$emit('select', this.fetchBlockData(block)) + } + }, }, } diff --git a/client/web/compose/src/components/PageBlocks/Configurator.vue b/client/web/compose/src/components/PageBlocks/Configurator.vue index 3b91272264..536124025a 100644 --- a/client/web/compose/src/components/PageBlocks/Configurator.vue +++ b/client/web/compose/src/components/PageBlocks/Configurator.vue @@ -130,11 +130,11 @@ {{ $t('general.globalBlock.label') }} @@ -238,12 +238,6 @@ export default { }, }, - data () { - return { - alreadyGlobalBlock: false, - } - }, - computed: { textVariants () { return [ @@ -275,22 +269,16 @@ export default { }, }, - watch: { - block: { - immediate: true, - handler (block) { - if (block.meta.namespaceID && !this.alreadyGlobalBlock) { - this.alreadyGlobalBlock = !!block.meta.namespaceID - } - }, - }, - }, - methods: { updateRefresh (e) { // If value is less than 5 but greater than 0 make it 5. Otherwise value stays the same. this.block.options.refreshRate = e.target.value < 5 && e.target.value > 0 ? 5 : e.target.value }, + updateGlobalState (value) { + if (value) { + this.block.meta.namespaceID = value + } + }, }, } @@ -298,4 +286,4 @@ export default { .mh-tab { max-height: calc(100vh - 16rem); } -import { watch } from 'fs' + diff --git a/client/web/compose/src/store/namespace.js b/client/web/compose/src/store/namespace.js index d95f44d9f0..18a30ba5c2 100644 --- a/client/web/compose/src/store/namespace.js +++ b/client/web/compose/src/store/namespace.js @@ -37,6 +37,16 @@ export default function (ComposeAPI) { set (state) { return state.set }, + + getNamespaceBlocksByID (state, { getByID }) { + return (ID) => ((getByID(ID) || {}).blocks || []).map((b) => { + const ns = getByID(ID) + const block = compose.PageBlockMaker(b) + block.blockID = `${ns.namespaceID}-${b.blockID}` + + return block + }) + }, }, actions: { diff --git a/client/web/compose/src/views/Admin/Pages/Builder.vue b/client/web/compose/src/views/Admin/Pages/Builder.vue index 80245f970c..a103843c91 100644 --- a/client/web/compose/src/views/Admin/Pages/Builder.vue +++ b/client/web/compose/src/views/Admin/Pages/Builder.vue @@ -174,7 +174,7 @@ @@ -443,6 +443,7 @@ export default { getModuleByID: 'module/getByID', previousPage: 'ui/previousPage', namespaces: 'namespace/set', + getNamespaceBlocks: 'namespace/getNamespaceBlocksByID', }), trPage: { @@ -508,19 +509,9 @@ export default { selectableNamespaceGlobalBlocks () { const { namespaceID } = this.namespace - const namespace = this.namespaces.find((n) => n.namespaceID === namespaceID) - if (!namespace) { - return [] - } - - return cloneDeep(namespace.blocks).map((b, i) => { - const block = compose.PageBlockMaker(b) - block.blockID = `${namespaceID}-${b.blockID}` - - return block - }).filter(b => { - return !this.blocks.find(({ blockID }) => blockID === b.blockID) + return this.getNamespaceBlocks(namespaceID).filter(({ blockID }) => { + return !this.usedBlocks.some(b => b.blockID === blockID) }) }, @@ -906,7 +897,7 @@ export default { this.findPageByID({ ...this.page, force: true }), this.findLayoutByID({ ...this.layout }), ]).then(async ([page, layout]) => { - let blocks = [ + const blocks = [ ...page.blocks.filter(({ blockID }) => { // Check if block exists in any other layout, if not delete it permanently return !this.blocks.some(b => b.blockID === blockID) && this.layouts.some(({ pageLayoutID, blocks }) => pageLayoutID !== layout.pageLayoutID && blocks.some(b => b.blockID === blockID)) @@ -921,6 +912,8 @@ export default { b.meta.namespaceID ? namespaceBlocks.push(b) : pageBlocks.push(b) }) + console.log('namespaceBlocks', namespaceBlocks) + const updatedGlobalBlocks = await this.processGlobalBlocks(namespaceBlocks) return this.updatePage({ namespaceID, ...page, blocks: pageBlocks }) @@ -928,7 +921,7 @@ export default { .then(async page => { const blocks = this.blocks.map(({ blockID, meta, xywh }) => { // If the global blocks is a newly created global block - if (meta.namespaceID && !blockID.includes('-')) { + if (meta.namespaceID) { blockID = (updatedGlobalBlocks.find(block => block.meta.tempID === meta.tempID) || {}).blockID } else if (blockID === NoID) { blockID = (page.blocks.find(block => block.meta.tempID === meta.tempID) || {}).blockID @@ -970,24 +963,21 @@ export default { let { namespaceID, name, slug, enabled, meta } = this.namespace - // If new blocks are added to global blocks update the ID's + // Block ID is already reset const newGlobalBlocks = globalBlocks - // We can't detect new global block through NOID because there can be an existing local block that - // was now made global previously and make a global block which means it does have the NoID added to it - .filter(({ blockID }) => !blockID.includes('-')) + .filter(({ blockID }) => !blockID.includes(namespaceID)) .map((block) => { block.meta.namespaceID = namespaceID return block }) - // Update existing global blocks - const existingGlobalBlocks = this.namespace.blocks + const namespace = this.namespaces.find((n) => n.namespaceID === this.namespace.namespaceID) + + const existingGlobalBlocks = namespace.blocks - // Update the state of the existing global blocks so the include the updated state changes globalBlocks.forEach((block) => { - block.blockID = String(block.blockID).replace(`${namespaceID}-`, '') - const matchingBlockIndex = existingGlobalBlocks.findIndex(({ blockID }) => blockID === block.blockID) + const matchingBlockIndex = existingGlobalBlocks.findIndex(({ meta }) => meta.tempID === block.meta.tempID) if (matchingBlockIndex > -1) { const normalBlockID = existingGlobalBlocks[matchingBlockIndex].blockID @@ -1187,9 +1177,9 @@ export default { meta, })) - block.blockID = meta.namespaceID ? `${namespaceID}-${block.blockID}` : block.blockID - if (block) { + block.blockID = meta.namespaceID ? `${namespaceID}-${block.blockID}` : block.blockID + block.xywh = xywh block.meta.hidden = !!meta.hidden tempBlocks.push(block) @@ -1255,11 +1245,9 @@ export default { blockID = fetchID({ blockID, meta }) if (meta.namespaceID) { - const namespace = this.namespaces.find((n) => n.namespaceID === this.namespace.namespaceID) + const { blocks = [] } = this.namespaces.find((n) => n.namespaceID === this.namespace.namespaceID) || {} - if (namespace) { - return namespace.blocks.find((b) => fetchID(b) === blockID) - } + return blocks.find((b) => fetchID(b) === blockID) } return this.page.blocks.find((b) => fetchID(b) === blockID) @@ -1293,4 +1281,4 @@ div.toolbox { right: auto; } } - +, includes diff --git a/client/web/compose/src/views/Public/Pages/View.vue b/client/web/compose/src/views/Public/Pages/View.vue index 4b0cc89765..e230bfd649 100644 --- a/client/web/compose/src/views/Public/Pages/View.vue +++ b/client/web/compose/src/views/Public/Pages/View.vue @@ -156,6 +156,7 @@ export default { ...mapGetters({ recordPaginationUsable: 'ui/recordPaginationUsable', getPageLayouts: 'pageLayout/getByPageID', + namespaces: 'namespace/set', }), isRecordPage () { @@ -299,14 +300,17 @@ export default { document.title = [title, this.namespace.name, this.$t('general:label.app-name.public')].filter(v => v).join(' | ') this.blocks = (this.layout || {}).blocks.map(({ blockID, meta, xywh }) => { + console.log('{ blockID, meta, xywh }', { blockID, meta, xywh }) const block = this.fetchBlockData({ blockID, meta, }) - block.xywh = xywh - return block - }) + if (block) { + block.xywh = xywh + return block + } + }).filter(b => b) }, refetchRecords () { @@ -318,7 +322,11 @@ export default { blockID = fetchID({ blockID, meta }) if (meta.namespaceID) { - return this.namespace.blocks.find((b) => fetchID(b) === blockID) + const namespace = this.namespaces.find((n) => n.namespaceID === this.namespace.namespaceID) + + if (namespace) { + return namespace.blocks.find((b) => fetchID(b) === blockID) + } } return this.page.blocks.find((b) => fetchID(b) === blockID) diff --git a/lib/js/src/compose/types/page-block/base.ts b/lib/js/src/compose/types/page-block/base.ts index c3b4a4591e..d78f0915ea 100644 --- a/lib/js/src/compose/types/page-block/base.ts +++ b/lib/js/src/compose/types/page-block/base.ts @@ -24,7 +24,7 @@ interface PageBlockStyle { interface PageBlockMeta { hidden?: boolean; tempID?: string; - isGlobal?: boolean; + // `namespaceID` is used to identify what namespace the block belongs too and also if the block is a global block on the namespace namespaceID?: boolean; } @@ -34,11 +34,11 @@ const defaultXYWH = [0, 0, 20, 15] export class PageBlock { // blockID is auto generated by the server in order to support resource translations - public blockID = NoID; - public kind = ''; + public blockID = NoID + public kind = '' - public title = ''; - public description = ''; + public title = '' + public description = '' public xywh: number[] = defaultXYWH; @@ -47,7 +47,6 @@ export class PageBlock { public meta: PageBlockMeta = { hidden: false, tempID: undefined, - isGlobal: false, namespaceID: undefined, }; diff --git a/lib/js/src/compose/types/page-block/chart.ts b/lib/js/src/compose/types/page-block/chart.ts index c0d6716b9d..764c858e12 100644 --- a/lib/js/src/compose/types/page-block/chart.ts +++ b/lib/js/src/compose/types/page-block/chart.ts @@ -1,5 +1,5 @@ import { PageBlock, PageBlockInput, Registry } from './base' -import { Apply,NoID } from '../../../cast' +import { Apply, NoID } from '../../../cast' import { Options as PageBlockRecordListOptions } from './record-list' import { cloneDeep, merge } from 'lodash' @@ -30,7 +30,7 @@ const defaults: Readonly = Object.freeze({ recordListOptions: { fields: [], }, - } + }, }) export class PageBlockChart extends PageBlock { @@ -57,7 +57,7 @@ export class PageBlockChart extends PageBlock { } } - resetDrillDown(): void { + resetDrillDown (): void { this.options.drillDown = cloneDeep(defaults.drillDown) } } diff --git a/locale/en/corteza-webapp-compose/block.yaml b/locale/en/corteza-webapp-compose/block.yaml index 445fd5e2c3..9bbbdddae6 100644 --- a/locale/en/corteza-webapp-compose/block.yaml +++ b/locale/en/corteza-webapp-compose/block.yaml @@ -5,6 +5,8 @@ selector: clone: ref: Clone with references noRef: Clone without references + selectableGlobalBlocks: + placeholder: Select global block from other pages automation: addPlaceholderLabel: Add placeholder (dummy button) availableScriptsAndWorkflow: Available scripts and workflows ({{count}}) @@ -181,7 +183,7 @@ general: dragAndDrop: Drag and drop to change order translations: Field translations globalBlock: - label: Make a global block + label: Global block iframe: label: IFrame pickURLField: Pick an URL field