diff --git a/addons/website/static/src/client_actions/website_preview/website_preview.js b/addons/website/static/src/client_actions/website_preview/website_preview.js index 1c8f8762a85..4081a9be3f2 100644 --- a/addons/website/static/src/client_actions/website_preview/website_preview.js +++ b/addons/website/static/src/client_actions/website_preview/website_preview.js @@ -2,6 +2,7 @@ import { registry } from '@web/core/registry'; import { useService } from '@web/core/utils/hooks'; +import { unslugHtmlDataObject } from '../../services/website_service'; const { Component, onWillStart, useRef, useEffect } = owl; @@ -84,8 +85,12 @@ export class WebsitePreview extends Component { this.currentUrl = this.iframe.el.contentDocument.location.href; history.replaceState({}, this.props.action.display_name, this.currentUrl); + const { mainObject, isPublished, canPublish } = this.iframe.el.contentDocument.documentElement.dataset; this.websiteService.currentMetadata = { path: this.currentUrl, + mainObject: unslugHtmlDataObject(mainObject), + isPublished: isPublished === 'True', + canPublish: canPublish === 'True', }; // Before leaving the iframe, its content is replicated on an diff --git a/addons/website/static/src/components/navbar/navbar.js b/addons/website/static/src/components/navbar/navbar.js index 05c9e6baf3b..9f63fc28ff3 100644 --- a/addons/website/static/src/components/navbar/navbar.js +++ b/addons/website/static/src/components/navbar/navbar.js @@ -20,6 +20,8 @@ patch(NavBar.prototype, 'website_navbar', { if (this.env.debug && !websiteSystrayRegistry.contains('web.debug_mode_menu')) { websiteSystrayRegistry.add('web.debug_mode_menu', registry.category('systray').get('web.debug_mode_menu'), {sequence: 100}); } + + useBus(websiteSystrayRegistry, 'CONTENT-UPDATED', () => this.render(true)); }, /** diff --git a/addons/website/static/src/services/website_service.js b/addons/website/static/src/services/website_service.js index 0b6a1d95fcf..be467016f5a 100644 --- a/addons/website/static/src/services/website_service.js +++ b/addons/website/static/src/services/website_service.js @@ -6,7 +6,7 @@ const { reactive } = owl; const websiteSystrayRegistry = registry.category('website_systray'); -const unslugHtmlDataObject = (repr) => { +export const unslugHtmlDataObject = (repr) => { const match = repr && repr.match(/(.+)\((\d+),(.*)\)/); if (!match) { return null; @@ -37,6 +37,7 @@ export const websiteService = { }, set currentMetadata(metadata) { currentMetadata = metadata; + websiteSystrayRegistry.trigger('CONTENT-UPDATED'); }, get currentWebsite() { const currentWebsite = websites.find(w => w.id === currentWebsiteId); diff --git a/addons/website/static/src/systray_items/publish.js b/addons/website/static/src/systray_items/publish.js new file mode 100644 index 00000000000..ea860af4111 --- /dev/null +++ b/addons/website/static/src/systray_items/publish.js @@ -0,0 +1,52 @@ +/** @odoo-module **/ + +import { registry } from "@web/core/registry"; +import { Switch } from '@website/components/switch/switch'; +import { useService, useBus } from '@web/core/utils/hooks'; + +const { Component, xml, useState } = owl; + +const websiteSystrayRegistry = registry.category('website_systray'); + +class PublishSystray extends Component { + setup() { + this.website = useService('website'); + this.rpc = useService('rpc'); + + this.state = useState({ + published: this.website.currentWebsite.metadata.isPublished, + }); + + useBus(websiteSystrayRegistry, 'CONTENT-UPDATED', () => this.state.published = this.website.currentWebsite.metadata.isPublished); + } + + get label() { + return this.state.published ? this.env._t("Published") : this.env._t("Unpublished"); + } + + publishContent() { + this.state.published = !this.state.published; + const { metadata: { mainObject } } = this.website.currentWebsite; + return this.rpc('/website/publish', { + id: mainObject.id, + object: mainObject.model, + }); + } +} +PublishSystray.template = xml` +
`; +PublishSystray.components = { + Switch +}; + +export const systrayItem = { + Component: PublishSystray, + isDisplayed: env => env.services.website.currentWebsite && env.services.website.currentWebsite.metadata.canPublish, +}; + +websiteSystrayRegistry.add("Publish", systrayItem, { sequence: 12 }); diff --git a/addons/website/views/website_navbar_templates.xml b/addons/website/views/website_navbar_templates.xml index 821087fc54d..d06fab82b78 100644 --- a/addons/website/views/website_navbar_templates.xml +++ b/addons/website/views/website_navbar_templates.xml @@ -75,11 +75,7 @@ -