Skip to content

Commit

Permalink
feat: Save data in page template - MEED-8114 - Meeds-io/meeds#2753
Browse files Browse the repository at this point in the history
This change will allow to save the data of a page when creating a page
template from it.
  • Loading branch information
boubaker committed Jan 17, 2025
1 parent 6afd347 commit de5636d
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,18 @@ public LayoutModel getPageLayout(
@Parameter(description = "Application Storage Id", required = false)
@RequestParam(name = "applicationId", required = false, defaultValue = "0")
long applicationId,
@Parameter(description = "Generate Page Data And Preferences to allow cloning the page", required = false)
@RequestParam(name = "impersonate", required = false, defaultValue = "false")
boolean impersonate,
@Parameter(description = "expand options", required = true)
@RequestParam("expand")
@RequestParam(name = "expand", required = false)
String expand) {
try {
ModelObject modelObject = applicationId > 0 ? pageLayoutService.getPageApplicationLayout(PageKey.parse(pageRef),
applicationId,
request.getRemoteUser()) :
pageLayoutService.getPageLayout(PageKey.parse(pageRef),
impersonate,
request.getRemoteUser());
return RestEntityBuilder.toLayoutModel(modelObject, layoutService, expand);
} catch (ObjectNotFoundException e) {
Expand Down Expand Up @@ -194,7 +198,7 @@ public LayoutModel updatePageLayout(
RestEntityBuilder.fromLayoutModel(layoutModel),
publish.orElse(false).booleanValue(),
request.getRemoteUser());
return getPageLayout(request, pageRef, 0, expand);
return getPageLayout(request, pageRef, 0, false, expand);
} catch (IllegalArgumentException | IllegalStateException e) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage());
} catch (ObjectNotFoundException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,20 @@ public void impersonatePage(PageKey pageKey) {
}

public Page getPageLayout(PageKey pageKey, String username) throws ObjectNotFoundException, IllegalAccessException {
return getPageLayout(pageKey, false, username);
}

public Page getPageLayout(PageKey pageKey, boolean impersonate, String username) throws ObjectNotFoundException,
IllegalAccessException {
Page page = getPageLayout(pageKey);
if (page == null) {
throw new ObjectNotFoundException(String.format(PAGE_NOT_ACCESSIBLE_MESSAGE, pageKey, username));
} else if (!aclService.canViewPage(pageKey, username)) {
throw new IllegalAccessException(String.format(PAGE_NOT_ACCESSIBLE_MESSAGE, pageKey, username));
}
if (impersonate) {
impersonateModel(page);
}
return page;
}

Expand Down Expand Up @@ -506,6 +514,18 @@ private void impersonateModel(ModelObject object, Page page) {
}
}

private void impersonateModel(ModelObject object) {
if (object instanceof Container container) {
ArrayList<ModelObject> children = container.getChildren();
if (CollectionUtils.isNotEmpty(children)) {
children.forEach(this::impersonateModel);
}
} else if (object instanceof Application application) {
Portlet preferences = portletInstanceService.getApplicationPortletPreferences(application);
application.setState(new TransientApplicationState(layoutService.getId(application.getState()), preferences));
}
}

private Portlet getApplicationPreferences(long applicationId, String username) throws IllegalAccessException,
ObjectNotFoundException {
List<PortletInstancePreference> preferences = portletInstanceService.getApplicationPreferences(applicationId, username);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,19 @@ export function getPage(pageRef) {
});
}

export function getPageLayout(pageRef, expand) {
return fetch(`/layout/rest/pages/layout?pageRef=${pageRef}&expand=${expand || ''}`, {
export function getPageLayout(pageRef, expand, impersonate) {
const formData = new FormData();
if (pageRef) {
formData.append('pageRef', pageRef);
}
if (impersonate) {
formData.append('impersonate', !!impersonate);
}
if (expand) {
formData.append('expand', expand);
}
const params = new URLSearchParams(formData).toString();
return fetch(`/layout/rest/pages/layout?${params}`, {
method: 'GET',
credentials: 'include',
}).then(resp => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,34 @@ export default {
window.setTimeout(() => this.openPageTemplateDrawer(), 10);
},
openPageTemplateDrawer() {
document.addEventListener('drawerOpened', this.endLoading);
const pageLayout = this.$layoutUtils.cleanAttributes(this.$root.layout, true, true);
this.$root.$emit('layout-page-template-drawer-open', {
content: JSON.stringify(pageLayout),
}, false, true);
this.$root.$on('layout-draft-saved', this.handleDraftSavedSuccess);
this.$root.$on('layout-draft-save-error', this.handleDraftSavedError);
this.$root.$emit('layout-save-draft');
},
handleDraftSavedSuccess() {
this.handleDraftSaved(true);
},
handleDraftSavedError() {
this.handleDraftSaved();
},
async handleDraftSaved(success) {
this.$root.$off('layout-draft-saved', this.handleDraftSavedSuccess);
this.$root.$off('layout-draft-save-error', this.handleDraftSavedError);
if (success) {
try {
document.addEventListener('drawerOpened', this.endLoading);
const draftPageLayout = await this.$pageLayoutService.getPageLayout(this.$root.draftPageRef, null, true);
this.$layoutUtils.parseSections(draftPageLayout);
const pageLayout = this.$layoutUtils.cleanAttributes(draftPageLayout, true, true);
this.$root.$emit('layout-page-template-drawer-open', {
content: JSON.stringify(pageLayout),
}, false, true);
} finally {
window.setTimeout(() => this.loading = false, 2000);
}
} else {
this.loading = false;
}
},
endLoading() {
window.setTimeout(() => this.loading = false, 200);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,35 @@ export default {
this.loading = true;
window.setTimeout(() => this.openPageTemplateDrawer(), 10);
},
async openPageTemplateDrawer() {
document.addEventListener('drawerOpened', this.endLoading);
let pageLayout = JSON.parse(JSON.stringify(this.$root.layout));
await this.attachPortletsPreferences(pageLayout);
pageLayout = this.$layoutUtils.cleanAttributes(pageLayout, true, true);
this.$root.$emit('layout-page-template-drawer-open', {
content: JSON.stringify(pageLayout),
}, false, true);
openPageTemplateDrawer() {
this.$root.$on('layout-draft-saved', this.handleDraftSavedSuccess);
this.$root.$on('layout-draft-save-error', this.handleDraftSavedError);
this.$root.$emit('layout-save-draft');
},
async attachPortletsPreferences(layoutModel) {
if (layoutModel.children?.length) {
await Promise.all(layoutModel.children.map(c => this.attachPortletsPreferences(c)));
} else if (layoutModel.contentId) {
const application = await this.$pageLayoutService.getPageApplicationLayout(this.$root.draftPageRef, layoutModel.storageId);
layoutModel.preferences = application?.preferences;
handleDraftSavedSuccess() {
this.handleDraftSaved(true);
},
handleDraftSavedError() {
this.handleDraftSaved();
},
async handleDraftSaved(success) {
this.$root.$off('layout-draft-saved', this.handleDraftSavedSuccess);
this.$root.$off('layout-draft-save-error', this.handleDraftSavedError);
if (success) {
try {
document.addEventListener('drawerOpened', this.endLoading);
const draftPageLayout = await this.$pageLayoutService.getPageLayout(this.$root.draftPageRef, null, true);
this.$layoutUtils.parseSections(draftPageLayout);
const pageLayout = this.$layoutUtils.cleanAttributes(draftPageLayout, true, true);
this.$root.$emit('layout-page-template-drawer-open', {
...this.$root.pageTemplate,
content: JSON.stringify(pageLayout),
}, false, true);
} finally {
window.setTimeout(() => this.loading = false, 2000);
}
} else {
this.loading = false;
}
},
endLoading() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,18 @@
target="_blank"
rel="opener"
dense>
<v-icon
:class="pageTemplate.system && 'disabled--text'"
size="13">
fa-columns
</v-icon>
<v-list-item-title
:class="pageTemplate.system && 'disabled--text'"
class="ps-2">
<v-card
color="transparent"
min-width="15"
class="me-2"
flat>
<v-icon
:class="pageTemplate.system && 'disabled--text'"
size="13">
fa-columns
</v-icon>
</v-card>
<v-list-item-title :class="pageTemplate.system && 'disabled--text'">
{{ $t('pageTemplate.label.editLayout') }}
</v-list-item-title>
</v-list-item>
Expand All @@ -71,20 +75,32 @@
<v-list-item
dense
@click="$root.$emit('layout-page-template-drawer-open', pageTemplate)">
<v-icon size="13">
fa-edit
</v-icon>
<v-list-item-title class="ps-2">
<v-card
color="transparent"
min-width="15"
class="me-2"
flat>
<v-icon size="13">
fa-edit
</v-icon>
</v-card>
<v-list-item-title>
{{ $t('pageTemplate.label.editProperties') }}
</v-list-item-title>
</v-list-item>
<v-list-item
dense
@click="$root.$emit('layout-page-template-drawer-open', pageTemplate, true)">
<v-icon size="13">
fa-copy
</v-icon>
<v-list-item-title class="ps-2">
<v-card
color="transparent"
min-width="15"
class="me-2"
flat>
<v-icon size="13">
fa-copy
</v-icon>
</v-card>
<v-list-item-title>
{{ $t('pageTemplate.label.duplicate') }}
</v-list-item-title>
</v-list-item>
Expand All @@ -97,15 +113,19 @@
:disabled="pageTemplate.system"
dense
@click="$root.$emit('page-templates-delete', pageTemplate)">
<v-icon
:class="!pageTemplate.system && 'error--text' || 'disabled--text'"
size="13">
fa-trash
</v-icon>
<v-list-item-title
:class="!pageTemplate.system && 'error--text' || 'disabled--text'"
class="ps-2">
{{ $t('pageTemplate.label.delete') }}
<v-card
color="transparent"
min-width="15"
class="me-2"
flat>
<v-icon
:class="!pageTemplate.system && 'error--text' || 'disabled--text'"
size="13">
fa-trash
</v-icon>
</v-card>
<v-list-item-title>
<span :class="!pageTemplate.system && 'error--text' || 'disabled--text'">{{ $t('pageTemplate.label.delete') }}</span>
</v-list-item-title>
</v-list-item>
</div>
Expand Down

0 comments on commit de5636d

Please sign in to comment.