Skip to content

fix(core): Adding back support of Bring Back Version feature in Container's portlet #32205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<dot-portlet-box>
<dot-iframe [src]="historyUrl" data-testId="historyIframe"></dot-iframe>
<dot-iframe [src]="historyUrl" data-testId="historyIframe" (custom)="onCustomEvent($event)" />
</dot-portlet-box>
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import { Component, DebugElement, ElementRef, Input, ViewChild } from '@angular/core';
import {
Component,
DebugElement,
ElementRef,
EventEmitter,
Input,
Output,
ViewChild
} from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';

import { DotPortletBoxModule } from '@components/dot-portlet-base/components/dot-portlet-box/dot-portlet-box.module';
import { DotRouterService } from '@dotcms/data-access';
import { MockDotRouterService } from '@dotcms/utils-testing';

import { DotContainerHistoryComponent } from './dot-container-history.component';

Expand All @@ -12,6 +22,7 @@ import { DotContainerHistoryComponent } from './dot-container-history.component'
})
export class IframeMockComponent {
@Input() src: string;
@Output() custom: EventEmitter<CustomEvent> = new EventEmitter();
@ViewChild('iframeElement') iframeElement: ElementRef;
}

Expand All @@ -29,18 +40,22 @@ describe('ContainerHistoryComponent', () => {
let hostComponent: DotTestHostComponent;
let fixture: ComponentFixture<DotTestHostComponent>;
let de: DebugElement;
let dotRouterService: DotRouterService;

beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [DotContainerHistoryComponent, IframeMockComponent, DotTestHostComponent],
imports: [DotPortletBoxModule]
imports: [DotPortletBoxModule],
providers: [{ provide: DotRouterService, useClass: MockDotRouterService }]
}).compileComponents();

fixture = TestBed.createComponent(DotTestHostComponent);
de = fixture.debugElement;
hostComponent = fixture.componentInstance;
hostComponent.containerId = '123';
fixture.detectChanges();

dotRouterService = TestBed.inject(DotRouterService);
});

it('should create', () => {
Expand All @@ -57,4 +72,23 @@ describe('ContainerHistoryComponent', () => {
);
});
});

describe('onCustomEvent', () => {
it('should call the service method when dotDialog emits custom output', () => {
const historyIframe: IframeMockComponent = de.query(
By.css('[data-testId="historyIframe"]')
).componentInstance;

const customEvent = new CustomEvent('custom', {
detail: {
data: { id: '456' },
name: 'bring-back-version'
}
});

historyIframe.custom.emit(customEvent);

expect(dotRouterService.goToEditContainer).toHaveBeenCalledWith('456');
});
});
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Component, Input, OnChanges, ViewChild } from '@angular/core';
import { Component, inject, Input, OnChanges, ViewChild } from '@angular/core';

import { IframeComponent } from '@components/_common/iframe/iframe-component';
import { DotRouterService } from '@dotcms/data-access';

@Component({
selector: 'dot-container-history',
Expand All @@ -10,12 +11,22 @@ import { IframeComponent } from '@components/_common/iframe/iframe-component';
export class DotContainerHistoryComponent implements OnChanges {
@Input() containerId: string;
@ViewChild('historyIframe') historyIframe: IframeComponent;
historyUrl = '/html/containers/push_history.jsp';

protected historyUrl = '/html/containers/push_history.jsp';
private readonly dotRouterService = inject(DotRouterService);

ngOnChanges(): void {
this.historyUrl = `/html/containers/push_history.jsp?containerId=${this.containerId}&popup=true`;
if (this.historyIframe) {
this.historyIframe.iframeElement.nativeElement.contentWindow.location.reload();
}
}

onCustomEvent($event: CustomEvent): void {
const { data, name } = $event.detail;

if (name === 'bring-back-version') {
this.dotRouterService.goToEditContainer(data.id);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ const routes: Routes = [
),
resolve: {
container: DotContainerEditResolver
}
},
data: {
reuseRoute: false
},
runGuardsAndResolvers: 'always'
}
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export class MockDotRouterService {
goToEditTemplate = jasmine.createSpy('goToEditTemplate');
allowRouteDeactivation = jasmine.createSpy('allowRouteDeactivation');
forbidRouteDeactivation = jasmine.createSpy('forbidRouteDeactivation');
goToEditContainer = jasmine.createSpy('goToEditContainer');
isEditPage() {
/* */
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -740,36 +740,39 @@ public final Response update(@Context final HttpServletRequest request,
() -> "Updating container. Request payload is : " + JsonUtil.getJsonStringFromObject(
containerForm));

container.setInode(StringPool.BLANK);
container.setCode(containerForm.getCode());
container.setMaxContentlets(containerForm.getMaxContentlets());
container.setNotes(containerForm.getNotes());
container.setPreLoop(containerForm.getPreLoop());
container.setPostLoop(containerForm.getPostLoop());
container.setSortContentletsBy(containerForm.getSortContentletsBy());
container.setStaticify(containerForm.isStaticify());
container.setUseDiv(containerForm.isUseDiv());
container.setFriendlyName(containerForm.getFriendlyName());
container.setModDate(new Date());
container.setModUser(user.getUserId());
container.setOwner(user.getUserId());
container.setShowOnMenu(containerForm.isShowOnMenu());
container.setTitle(containerForm.getTitle());

if (containerForm.getMaxContentlets() == 0) {
container.setCode(containerForm.getCode());
}
Container newContainerVersion = new Container();
newContainerVersion.setIdentifier(containerForm.getIdentifier());
newContainerVersion.setCode(containerForm.getCode());
newContainerVersion.setMaxContentlets(containerForm.getMaxContentlets());
newContainerVersion.setNotes(containerForm.getNotes());
newContainerVersion.setPreLoop(containerForm.getPreLoop());
newContainerVersion.setPostLoop(containerForm.getPostLoop());
newContainerVersion.setSortContentletsBy(containerForm.getSortContentletsBy());
newContainerVersion.setStaticify(containerForm.isStaticify());
newContainerVersion.setUseDiv(containerForm.isUseDiv());
newContainerVersion.setFriendlyName(containerForm.getFriendlyName());
newContainerVersion.setModDate(new Date());
newContainerVersion.setModUser(user.getUserId());
newContainerVersion.setOwner(user.getUserId());
newContainerVersion.setShowOnMenu(containerForm.isShowOnMenu());
newContainerVersion.setTitle(containerForm.getTitle());

this.containerAPI.save(container, containerForm.getContainerStructures(), host, user,
pageMode.respectAnonPerms);
if (containerForm.getMaxContentlets() == 0) {
newContainerVersion.setCode(containerForm.getCode());
}

newContainerVersion.setLuceneQuery(container.getLuceneQuery());

this.containerAPI.save(newContainerVersion, containerForm.getContainerStructures(), host, user,
pageMode.respectAnonPerms);

ActivityLogger.logInfo(this.getClass(),
"Update Container: " + containerForm.getIdentifier(),
getInfoMessage(user,
MessageConstants.SAVED + container.getTitle()),
MessageConstants.SAVED + newContainerVersion.getTitle()),
host.getHostname());

return Response.ok(new ResponseEntityView(new ContainerView(container))).build();
return Response.ok(new ResponseEntityView(new ContainerView(newContainerVersion))).build();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@ public Container add(String key, Container container) {
return container;
}

key = primaryGroup + key;

// Add the key to the cache
cache.put(key, container, primaryGroup);
cache.put(primaryGroup + key, container, primaryGroup);

return container;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public Container find(final String inode) throws DotStateException, DotDataExcep
container = TransformerLocator.createContainerTransformer(containerResults).findFirst();

if(container != null && container.getInode() != null) {
containerCache.add(inode, container);
containerCache.add(container.getInode(), container);
}
}

Expand Down
39 changes: 38 additions & 1 deletion dotCMS/src/main/webapp/html/containers/push_history.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
final String containerId = httpServletRequest.getParameter("containerId");
Container container = APILocator.getContainerAPI().getWorkingContainerById(containerId, user, true);
request.setAttribute(com.dotmarketing.util.WebKeys.VERSIONS_INODE_EDIT, container);
request.setAttribute("hideBringBack", true);
request.setAttribute("hideBringBack", false);
%>
<%@ include file="/html/portlet/ext/common/edit_versions_inc.jsp" %>

Expand All @@ -43,4 +43,41 @@
});
document.dispatchEvent(customEvent);
}

let isBringBack = false;
const message = '<%=UtilMethods.escapeSingleQuotes(LanguageUtil.get(pageContext, "folder.replace.container.working.version"))%>'
function bringBackVersion(inode){
if(!isBringBack && confirm(message)){

setIsBringBack(true);

fetch(`/api/v1/versionables/${inode}/_bringback`, {
method: 'PUT'
})
.then(response => response.json())
.then(({ entity }) => {
const customEvent = document.createEvent('CustomEvent');
customEvent.initCustomEvent('ng-event', false, false, {
name: 'bring-back-version',
data: {
id: entity.versionId,
inode: entity.inode,
type: 'container'
}
})

document.dispatchEvent(customEvent);
})
.catch((error) => {
console.error('Error bringing back version: ', error);
})
.finally(() => setIsBringBack(false));
}
}

function setIsBringBack(isBringBack){
const element = document.querySelector("[data-messageId='bring-back-message']");
element.style.display = isBringBack ? 'flex' : 'none';
isBringBack = isBringBack
}
</script>
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
<a href="javascript: deleteVersion('<%= vinode%>');"><%= LanguageUtil.get(pageContext, "Delete") %></a>
<% } %>
<% if(!hideBringBack) { %>
- <a href="javascript: bringBackTemplateVersion('<%= vinode %>');"><%= LanguageUtil.get(pageContext, "Bring-Back") %></a>
- <a href="javascript: bringBackVersion('<%= vinode %>');"><%= LanguageUtil.get(pageContext, "Bring-Back") %></a>
<%}%>
<% } %>
<% } else { %>
Expand Down
2 changes: 1 addition & 1 deletion dotCMS/src/main/webapp/html/templates/push_history.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
}

let isBringBack = false;
function bringBackTemplateVersion(inode){
function bringBackVersion(inode){
if(!isBringBack && confirm('<%=UtilMethods.escapeSingleQuotes(LanguageUtil.get(pageContext, "folder.replace.template.working.version"))%>')){
setIsBringBack(true);
fetch(`/api/v1/versionables/${inode}/_bringback`, {
Expand Down
Loading