mirror of
https://github.com/Alfresco/alfresco-content-app.git
synced 2025-05-12 17:04:46 +00:00
deprecate lock-node directive (#1774)
* deprecate lock-node directive * delete old file after rebase * fix code and tests
This commit is contained in:
parent
94eb4c90ff
commit
882cc1c68b
@ -24,9 +24,8 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { ToggleEditOfflineComponent } from './toggle-edit-offline.component';
|
import { ToggleEditOfflineComponent } from './toggle-edit-offline.component';
|
||||||
import { LockNodeDirective } from '../../../directives/lock-node.directive';
|
import { CoreModule } from '@alfresco/adf-core';
|
||||||
import { setupTestBed, CoreModule } from '@alfresco/adf-core';
|
import { ComponentFixture, TestBed } from '@angular/core/testing';
|
||||||
import { TestBed } from '@angular/core/testing';
|
|
||||||
import { of } from 'rxjs';
|
import { of } from 'rxjs';
|
||||||
import { Store } from '@ngrx/store';
|
import { Store } from '@ngrx/store';
|
||||||
import { NodeEntry } from '@alfresco/js-api';
|
import { NodeEntry } from '@alfresco/js-api';
|
||||||
@ -34,16 +33,17 @@ import { DownloadNodesAction, EditOfflineAction, SnackbarErrorAction } from '@al
|
|||||||
import { TranslateModule } from '@ngx-translate/core';
|
import { TranslateModule } from '@ngx-translate/core';
|
||||||
|
|
||||||
describe('ToggleEditOfflineComponent', () => {
|
describe('ToggleEditOfflineComponent', () => {
|
||||||
let fixture;
|
let fixture: ComponentFixture<ToggleEditOfflineComponent>;
|
||||||
let component;
|
let component: ToggleEditOfflineComponent;
|
||||||
let store;
|
let store: Store;
|
||||||
let dispatchSpy;
|
let dispatchSpy: jasmine.Spy;
|
||||||
let selectSpy;
|
let selectSpy: jasmine.Spy;
|
||||||
const selection = { file: { entry: { name: 'test', properties: {} } } };
|
let selection: any;
|
||||||
|
|
||||||
setupTestBed({
|
beforeEach(() => {
|
||||||
|
TestBed.configureTestingModule({
|
||||||
imports: [TranslateModule.forRoot(), CoreModule.forRoot()],
|
imports: [TranslateModule.forRoot(), CoreModule.forRoot()],
|
||||||
declarations: [ToggleEditOfflineComponent, LockNodeDirective],
|
declarations: [ToggleEditOfflineComponent],
|
||||||
providers: [
|
providers: [
|
||||||
{
|
{
|
||||||
provide: Store,
|
provide: Store,
|
||||||
@ -55,17 +55,16 @@ describe('ToggleEditOfflineComponent', () => {
|
|||||||
]
|
]
|
||||||
});
|
});
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
fixture = TestBed.createComponent(ToggleEditOfflineComponent);
|
fixture = TestBed.createComponent(ToggleEditOfflineComponent);
|
||||||
component = fixture.componentInstance;
|
component = fixture.componentInstance;
|
||||||
|
spyOn(component, 'unlockNode').and.returnValue(Promise.resolve(null));
|
||||||
|
spyOn(component, 'lockNode').and.returnValue(Promise.resolve(null));
|
||||||
store = TestBed.inject(Store);
|
store = TestBed.inject(Store);
|
||||||
|
|
||||||
dispatchSpy = spyOn(store, 'dispatch');
|
dispatchSpy = spyOn(store, 'dispatch');
|
||||||
selectSpy = spyOn(store, 'select');
|
selectSpy = spyOn(store, 'select');
|
||||||
});
|
|
||||||
|
|
||||||
afterEach(() => {
|
selection = { file: { entry: { name: 'test', properties: {}, isLocked: false } } };
|
||||||
dispatchSpy.calls.reset();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should initialized with data from store', () => {
|
it('should initialized with data from store', () => {
|
||||||
@ -73,15 +72,15 @@ describe('ToggleEditOfflineComponent', () => {
|
|||||||
|
|
||||||
fixture.detectChanges();
|
fixture.detectChanges();
|
||||||
|
|
||||||
expect(component.selection).toEqual(selection.file);
|
expect(component.selection).toEqual(selection.file as any);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should download content if node is locked', () => {
|
it('should download content when node is locked', async () => {
|
||||||
selectSpy.and.returnValue(of(selection));
|
selectSpy.and.returnValue(of(selection));
|
||||||
fixture.detectChanges();
|
fixture.detectChanges();
|
||||||
|
|
||||||
const isLocked = true;
|
selection.file.entry.isLocked = false;
|
||||||
component.onToggleEvent(isLocked);
|
await component.onClick();
|
||||||
|
|
||||||
fixture.detectChanges();
|
fixture.detectChanges();
|
||||||
|
|
||||||
@ -90,23 +89,21 @@ describe('ToggleEditOfflineComponent', () => {
|
|||||||
|
|
||||||
it('should not download content if node is not locked', () => {
|
it('should not download content if node is not locked', () => {
|
||||||
selectSpy.and.returnValue(of(selection));
|
selectSpy.and.returnValue(of(selection));
|
||||||
|
|
||||||
fixture.detectChanges();
|
fixture.detectChanges();
|
||||||
|
component.onClick();
|
||||||
const isLocked = false;
|
|
||||||
component.onToggleEvent(isLocked);
|
|
||||||
|
|
||||||
fixture.detectChanges();
|
fixture.detectChanges();
|
||||||
|
|
||||||
expect(dispatchSpy.calls.argsFor(0)).not.toEqual([new DownloadNodesAction([selection.file as NodeEntry])]);
|
expect(dispatchSpy.calls.argsFor(0)).not.toEqual([new DownloadNodesAction([selection.file as NodeEntry])]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should dispatch EditOfflineAction action', () => {
|
it('should dispatch EditOfflineAction action', async () => {
|
||||||
selectSpy.and.returnValue(of(selection));
|
selectSpy.and.returnValue(of(selection));
|
||||||
|
|
||||||
|
selection.file.entry.isLocked = true;
|
||||||
|
|
||||||
fixture.detectChanges();
|
fixture.detectChanges();
|
||||||
|
await component.onClick();
|
||||||
const isLocked = false;
|
|
||||||
component.onToggleEvent(isLocked);
|
|
||||||
|
|
||||||
fixture.detectChanges();
|
fixture.detectChanges();
|
||||||
|
|
||||||
expect(dispatchSpy.calls.argsFor(0)).toEqual([new EditOfflineAction(selection.file as NodeEntry)]);
|
expect(dispatchSpy.calls.argsFor(0)).toEqual([new EditOfflineAction(selection.file as NodeEntry)]);
|
||||||
@ -114,8 +111,8 @@ describe('ToggleEditOfflineComponent', () => {
|
|||||||
|
|
||||||
it('should raise notification on lock error', () => {
|
it('should raise notification on lock error', () => {
|
||||||
selectSpy.and.returnValue(of(selection));
|
selectSpy.and.returnValue(of(selection));
|
||||||
fixture.detectChanges();
|
|
||||||
|
|
||||||
|
fixture.detectChanges();
|
||||||
component.onLockError();
|
component.onLockError();
|
||||||
fixture.detectChanges();
|
fixture.detectChanges();
|
||||||
|
|
||||||
@ -128,9 +125,9 @@ describe('ToggleEditOfflineComponent', () => {
|
|||||||
|
|
||||||
it('should raise notification on unlock error', () => {
|
it('should raise notification on unlock error', () => {
|
||||||
selectSpy.and.returnValue(of(selection));
|
selectSpy.and.returnValue(of(selection));
|
||||||
fixture.detectChanges();
|
|
||||||
|
|
||||||
component.onUnlockLockError();
|
fixture.detectChanges();
|
||||||
|
component.onUnlockError();
|
||||||
fixture.detectChanges();
|
fixture.detectChanges();
|
||||||
|
|
||||||
expect(dispatchSpy.calls.argsFor(0)).toEqual([
|
expect(dispatchSpy.calls.argsFor(0)).toEqual([
|
||||||
|
@ -24,28 +24,26 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { AppStore, DownloadNodesAction, EditOfflineAction, SnackbarErrorAction, getAppSelection } from '@alfresco/aca-shared/store';
|
import { AppStore, DownloadNodesAction, EditOfflineAction, SnackbarErrorAction, getAppSelection } from '@alfresco/aca-shared/store';
|
||||||
import { MinimalNodeEntity } from '@alfresco/js-api';
|
import { MinimalNodeEntity, NodeEntry, SharedLinkEntry, Node } from '@alfresco/js-api';
|
||||||
import { Component, OnInit, ViewEncapsulation } from '@angular/core';
|
import { Component, OnInit, ViewEncapsulation } from '@angular/core';
|
||||||
import { Store } from '@ngrx/store';
|
import { Store } from '@ngrx/store';
|
||||||
|
import { isLocked } from '@alfresco/aca-shared';
|
||||||
|
import { AlfrescoApiService } from '@alfresco/adf-core';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-toggle-edit-offline',
|
selector: 'app-toggle-edit-offline',
|
||||||
template: `
|
template: `
|
||||||
<button
|
<button
|
||||||
#lock="lockNode"
|
|
||||||
mat-menu-item
|
mat-menu-item
|
||||||
(toggle)="onToggleEvent($event)"
|
[attr.title]="(isNodeLocked ? 'APP.ACTIONS.EDIT_OFFLINE_CANCEL' : 'APP.ACTIONS.EDIT_OFFLINE') | translate"
|
||||||
(lockError)="onLockError()"
|
(click)="onClick()"
|
||||||
(unlockError)="onUnlockLockError()"
|
|
||||||
[acaLockNode]="selection"
|
|
||||||
[attr.title]="lock.isNodeLocked() ? ('APP.ACTIONS.EDIT_OFFLINE_CANCEL' | translate) : ('APP.ACTIONS.EDIT_OFFLINE' | translate)"
|
|
||||||
>
|
>
|
||||||
<ng-container *ngIf="lock.isNodeLocked()">
|
<ng-container *ngIf="isNodeLocked">
|
||||||
<mat-icon>cancel</mat-icon>
|
<mat-icon>cancel</mat-icon>
|
||||||
<span>{{ 'APP.ACTIONS.EDIT_OFFLINE_CANCEL' | translate }}</span>
|
<span>{{ 'APP.ACTIONS.EDIT_OFFLINE_CANCEL' | translate }}</span>
|
||||||
</ng-container>
|
</ng-container>
|
||||||
|
|
||||||
<ng-container *ngIf="!lock.isNodeLocked()">
|
<ng-container *ngIf="!isNodeLocked">
|
||||||
<mat-icon>edit</mat-icon>
|
<mat-icon>edit</mat-icon>
|
||||||
<span>{{ 'APP.ACTIONS.EDIT_OFFLINE' | translate }}</span>
|
<span>{{ 'APP.ACTIONS.EDIT_OFFLINE' | translate }}</span>
|
||||||
</ng-container>
|
</ng-container>
|
||||||
@ -57,7 +55,7 @@ import { Store } from '@ngrx/store';
|
|||||||
export class ToggleEditOfflineComponent implements OnInit {
|
export class ToggleEditOfflineComponent implements OnInit {
|
||||||
selection: MinimalNodeEntity;
|
selection: MinimalNodeEntity;
|
||||||
|
|
||||||
constructor(private store: Store<AppStore>) {}
|
constructor(private store: Store<AppStore>, private alfrescoApiService: AlfrescoApiService) {}
|
||||||
|
|
||||||
ngOnInit() {
|
ngOnInit() {
|
||||||
this.store.select(getAppSelection).subscribe(({ file }) => {
|
this.store.select(getAppSelection).subscribe(({ file }) => {
|
||||||
@ -65,11 +63,37 @@ export class ToggleEditOfflineComponent implements OnInit {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
onToggleEvent(isNodeLocked: boolean) {
|
get isNodeLocked(): boolean {
|
||||||
if (isNodeLocked) {
|
return !!(this.selection && isLocked(this.selection));
|
||||||
this.store.dispatch(new DownloadNodesAction([this.selection]));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async onClick() {
|
||||||
|
await this.toggleLock(this.selection);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async toggleLock(node: NodeEntry | SharedLinkEntry) {
|
||||||
|
const id = (node as SharedLinkEntry).entry.nodeId || node.entry.id;
|
||||||
|
|
||||||
|
if (isLocked(this.selection)) {
|
||||||
|
try {
|
||||||
|
const response = await this.unlockNode(id);
|
||||||
|
|
||||||
|
this.update(response?.entry);
|
||||||
this.store.dispatch(new EditOfflineAction(this.selection));
|
this.store.dispatch(new EditOfflineAction(this.selection));
|
||||||
|
} catch {
|
||||||
|
this.onUnlockError();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
const response = await this.lockNode(id);
|
||||||
|
|
||||||
|
this.update(response?.entry);
|
||||||
|
this.store.dispatch(new DownloadNodesAction([this.selection]));
|
||||||
|
this.store.dispatch(new EditOfflineAction(this.selection));
|
||||||
|
} catch {
|
||||||
|
this.onLockError();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
onLockError() {
|
onLockError() {
|
||||||
@ -80,11 +104,34 @@ export class ToggleEditOfflineComponent implements OnInit {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
onUnlockLockError() {
|
onUnlockError() {
|
||||||
this.store.dispatch(
|
this.store.dispatch(
|
||||||
new SnackbarErrorAction('APP.MESSAGES.ERRORS.UNLOCK_NODE', {
|
new SnackbarErrorAction('APP.MESSAGES.ERRORS.UNLOCK_NODE', {
|
||||||
fileName: this.selection.entry.name
|
fileName: this.selection.entry.name
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lockNode(nodeId: string) {
|
||||||
|
return this.alfrescoApiService.nodesApi.lockNode(nodeId, {
|
||||||
|
type: 'ALLOW_OWNER_CHANGES',
|
||||||
|
lifetime: 'PERSISTENT'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
unlockNode(nodeId: string) {
|
||||||
|
return this.alfrescoApiService.nodesApi.unlockNode(nodeId);
|
||||||
|
}
|
||||||
|
|
||||||
|
private update(data: Node) {
|
||||||
|
if (data && data.properties) {
|
||||||
|
const properties = this.selection.entry.properties || {};
|
||||||
|
|
||||||
|
properties['cm:lockLifetime'] = data.properties['cm:lockLifetime'];
|
||||||
|
properties['cm:lockOwner'] = data.properties['cm:lockOwner'];
|
||||||
|
properties['cm:lockType'] = data.properties['cm:lockType'];
|
||||||
|
|
||||||
|
this.selection.entry.properties = properties;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -27,11 +27,10 @@ import { NgModule } from '@angular/core';
|
|||||||
import { DocumentListDirective } from './document-list.directive';
|
import { DocumentListDirective } from './document-list.directive';
|
||||||
import { LibraryMembershipDirective } from './library-membership.directive';
|
import { LibraryMembershipDirective } from './library-membership.directive';
|
||||||
import { LibraryFavoriteDirective } from './library-favorite.directive';
|
import { LibraryFavoriteDirective } from './library-favorite.directive';
|
||||||
import { LockNodeDirective } from './lock-node.directive';
|
|
||||||
import { SharedDirectivesModule } from '@alfresco/aca-shared';
|
import { SharedDirectivesModule } from '@alfresco/aca-shared';
|
||||||
|
|
||||||
export function directives() {
|
export function directives() {
|
||||||
return [DocumentListDirective, LibraryMembershipDirective, LibraryFavoriteDirective, LockNodeDirective];
|
return [DocumentListDirective, LibraryMembershipDirective, LibraryFavoriteDirective];
|
||||||
}
|
}
|
||||||
|
|
||||||
@NgModule({
|
@NgModule({
|
||||||
|
@ -1,134 +0,0 @@
|
|||||||
/*!
|
|
||||||
* @license
|
|
||||||
* Alfresco Example Content Application
|
|
||||||
*
|
|
||||||
* Copyright (C) 2005 - 2020 Alfresco Software Limited
|
|
||||||
*
|
|
||||||
* This file is part of the Alfresco Example Content Application.
|
|
||||||
* If the software was purchased under a paid Alfresco license, the terms of
|
|
||||||
* the paid license agreement will prevail. Otherwise, the software is
|
|
||||||
* provided under the following open source license terms:
|
|
||||||
*
|
|
||||||
* The Alfresco Example Content Application is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU Lesser General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* The Alfresco Example Content Application is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU Lesser General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU Lesser General Public License
|
|
||||||
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
import { Component, ViewChild } from '@angular/core';
|
|
||||||
import { LockNodeDirective } from './lock-node.directive';
|
|
||||||
import { AlfrescoApiService, setupTestBed, CoreModule } from '@alfresco/adf-core';
|
|
||||||
import { TestBed, fakeAsync, tick } from '@angular/core/testing';
|
|
||||||
import { TranslateModule } from '@ngx-translate/core';
|
|
||||||
import { AppTestingModule } from '../testing/app-testing.module';
|
|
||||||
|
|
||||||
@Component({
|
|
||||||
selector: 'app-test-component',
|
|
||||||
template: ` <button #lock="lockNode" [acaLockNode]="selection">Lock</button> `
|
|
||||||
})
|
|
||||||
class TestComponent {
|
|
||||||
@ViewChild('lock')
|
|
||||||
directive: LockNodeDirective;
|
|
||||||
|
|
||||||
selection = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
describe('LockNodeDirective', () => {
|
|
||||||
let fixture;
|
|
||||||
let api;
|
|
||||||
let component;
|
|
||||||
|
|
||||||
setupTestBed({
|
|
||||||
imports: [TranslateModule.forRoot(), CoreModule.forRoot(), AppTestingModule],
|
|
||||||
declarations: [TestComponent, LockNodeDirective]
|
|
||||||
});
|
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
fixture = TestBed.createComponent(TestComponent);
|
|
||||||
component = fixture.componentInstance;
|
|
||||||
component.selection = null;
|
|
||||||
api = TestBed.inject(AlfrescoApiService);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should return false if selection is null', () => {
|
|
||||||
component.selection = null;
|
|
||||||
fixture.detectChanges();
|
|
||||||
expect(component.directive.isNodeLocked()).toBe(false);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should return false if selection is not locked', () => {
|
|
||||||
component.selection = { entry: { name: 'test-name', properties: {} } };
|
|
||||||
fixture.detectChanges();
|
|
||||||
expect(component.directive.isNodeLocked()).toBe(false);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should return true if selection is locked', () => {
|
|
||||||
component.selection = {
|
|
||||||
entry: {
|
|
||||||
name: 'test-name',
|
|
||||||
properties: { 'cm:lockType': 'WRITE_LOCK' }
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
fixture.detectChanges();
|
|
||||||
expect(component.directive.isNodeLocked()).toBe(true);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should lock selection', fakeAsync(() => {
|
|
||||||
component.selection = {
|
|
||||||
entry: {
|
|
||||||
id: 'id',
|
|
||||||
name: 'test-name',
|
|
||||||
properties: {}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
spyOn(api.nodesApi, 'lockNode').and.returnValue(
|
|
||||||
Promise.resolve({
|
|
||||||
entry: { properties: { 'cm:lockType': 'WRITE_LOCK' } }
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
fixture.detectChanges();
|
|
||||||
|
|
||||||
component.directive.onClick();
|
|
||||||
tick();
|
|
||||||
fixture.detectChanges();
|
|
||||||
|
|
||||||
expect(component.selection.entry.properties['cm:lockType']).toBe('WRITE_LOCK');
|
|
||||||
}));
|
|
||||||
|
|
||||||
it('should unlock selection', fakeAsync(() => {
|
|
||||||
component.selection = {
|
|
||||||
entry: {
|
|
||||||
id: 'id',
|
|
||||||
name: 'test-name',
|
|
||||||
properties: {
|
|
||||||
'cm:lockType': 'WRITE_LOCK'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
spyOn(api.nodesApi, 'unlockNode').and.returnValue(
|
|
||||||
Promise.resolve({
|
|
||||||
entry: { properties: {} }
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
fixture.detectChanges();
|
|
||||||
component.directive.onClick();
|
|
||||||
|
|
||||||
tick();
|
|
||||||
fixture.detectChanges();
|
|
||||||
|
|
||||||
expect(component.selection.entry.properties['cm:lockType']).toBe(undefined);
|
|
||||||
}));
|
|
||||||
});
|
|
@ -1,96 +0,0 @@
|
|||||||
/*!
|
|
||||||
* @license
|
|
||||||
* Alfresco Example Content Application
|
|
||||||
*
|
|
||||||
* Copyright (C) 2005 - 2020 Alfresco Software Limited
|
|
||||||
*
|
|
||||||
* This file is part of the Alfresco Example Content Application.
|
|
||||||
* If the software was purchased under a paid Alfresco license, the terms of
|
|
||||||
* the paid license agreement will prevail. Otherwise, the software is
|
|
||||||
* provided under the following open source license terms:
|
|
||||||
*
|
|
||||||
* The Alfresco Example Content Application is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU Lesser General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* The Alfresco Example Content Application is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU Lesser General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU Lesser General Public License
|
|
||||||
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
import { Directive, EventEmitter, HostListener, Input, Output } from '@angular/core';
|
|
||||||
import { NodeEntry, SharedLinkEntry } from '@alfresco/js-api';
|
|
||||||
import { AlfrescoApiService } from '@alfresco/adf-core';
|
|
||||||
import { isLocked } from '@alfresco/aca-shared';
|
|
||||||
|
|
||||||
@Directive({
|
|
||||||
selector: '[acaLockNode]',
|
|
||||||
exportAs: 'lockNode'
|
|
||||||
})
|
|
||||||
export class LockNodeDirective {
|
|
||||||
@Input('acaLockNode')
|
|
||||||
node: NodeEntry = null;
|
|
||||||
|
|
||||||
@Output() toggle: EventEmitter<any> = new EventEmitter();
|
|
||||||
@Output() lockError: EventEmitter<any> = new EventEmitter();
|
|
||||||
@Output() unlockError: EventEmitter<any> = new EventEmitter();
|
|
||||||
|
|
||||||
@HostListener('click')
|
|
||||||
onClick() {
|
|
||||||
this.toggleLock(this.node);
|
|
||||||
}
|
|
||||||
|
|
||||||
constructor(private alfrescoApiService: AlfrescoApiService) {}
|
|
||||||
|
|
||||||
isNodeLocked(): boolean {
|
|
||||||
return !!(this.node && isLocked(this.node));
|
|
||||||
}
|
|
||||||
|
|
||||||
private async toggleLock(node: NodeEntry | SharedLinkEntry) {
|
|
||||||
const id = (node as SharedLinkEntry).entry.nodeId || node.entry.id;
|
|
||||||
|
|
||||||
if (isLocked(this.node)) {
|
|
||||||
try {
|
|
||||||
const response = await this.unlockNode(id);
|
|
||||||
|
|
||||||
this.update(response.entry);
|
|
||||||
this.toggle.emit(false);
|
|
||||||
} catch (error) {
|
|
||||||
this.unlockError.emit(error);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
try {
|
|
||||||
const response = await this.lockNode(id);
|
|
||||||
|
|
||||||
this.update(response.entry);
|
|
||||||
this.toggle.emit(true);
|
|
||||||
} catch (error) {
|
|
||||||
this.lockError.emit(error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private lockNode(nodeId: string) {
|
|
||||||
return this.alfrescoApiService.nodesApi.lockNode(nodeId, {
|
|
||||||
type: 'ALLOW_OWNER_CHANGES',
|
|
||||||
lifetime: 'PERSISTENT'
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private unlockNode(nodeId: string) {
|
|
||||||
return this.alfrescoApiService.nodesApi.unlockNode(nodeId);
|
|
||||||
}
|
|
||||||
|
|
||||||
private update(data) {
|
|
||||||
const properties = this.node.entry.properties || {};
|
|
||||||
|
|
||||||
properties['cm:lockLifetime'] = data.properties['cm:lockLifetime'];
|
|
||||||
properties['cm:lockOwner'] = data.properties['cm:lockOwner'];
|
|
||||||
properties['cm:lockType'] = data.properties['cm:lockType'];
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user