[ADF-3530, ADF-3598] Sharing files (#3828)

* share node

* lint fix

* createSharedLinks parameter

* remove es6 async

* use fakeAsync

* removed trailing comma

* e2e update dialog title locator

* Expires label localization

* docs update
This commit is contained in:
Cilibiu Bogdan 2018-10-01 16:10:40 +03:00 committed by Eugenio Romano
parent b34ca87657
commit 42f4bee2b4
39 changed files with 1241 additions and 435 deletions

View File

@ -135,6 +135,7 @@
"DELETE": "Delete",
"FAVORITES": "Add to favorites",
"SHARE": "Share",
"SHARE_EDIT": "Edit settings",
"THEME": "Select a theme",
"SHOW_VERSION": "Show version",
"HIDE_VERSION": "Hide version",

View File

@ -2,6 +2,7 @@
"$schema": "../../lib/core/app-config/schema.json",
"ecmHost": "http://{hostname}:{port}",
"bpmHost": "http://{hostname}:{port}",
"baseShareUrl": null,
"loginRoute": "login",
"providers": "ALL",
"contextRootBpm": "activiti-app",

View File

@ -119,10 +119,13 @@
</mat-icon>
</button>
<button mat-icon-button
disabled
[disabled]="!shareRef.isFile"
[baseShareUrl]="baseShareUrl"
#shareRef="adfShare"
[adf-share]="documentList.selection[0]"
matTooltip="{{ 'DOCUMENT_LIST.TOOLBAR.SHARE' | translate }}">
[matTooltip]="(shareRef.isShared
? 'DOCUMENT_LIST.TOOLBAR.SHARE_EDIT'
: 'DOCUMENT_LIST.TOOLBAR.SHARE') | translate">
<mat-icon>
share
</mat-icon>

View File

@ -62,7 +62,9 @@ export class FilesComponent implements OnInit, OnChanges, OnDestroy {
displayMode = DisplayMode.List;
includeFields = ['isFavorite', 'isLocked', 'aspectNames'];
baseShareUrl = this.appConfig.get<string>(AppConfigValues.ECMHOST) + '/preview/s/';
baseShareUrl = (
this.appConfig.get<string>(AppConfigValues.BASESHAREURL) ||
this.appConfig.get<string>(AppConfigValues.ECMHOST)) + '/preview/s/';
toolbarColor = 'default';

View File

@ -15,6 +15,8 @@ Creates and manages public shared links for files.
```html
<adf-toolbar>
<button mat-icon-button
#shared="adfShare"
[disabled]="!shared.isFile"
[baseShareUrl]="http://localhost:8080/myrouteForShareFile/"
[adf-share]="documentList.selection[0]">
<mat-icon>share</mat-icon>
@ -33,7 +35,9 @@ Creates and manages public shared links for files.
| Name | Type | Default value | Description |
| ---- | ---- | ------------- | ----------- |
| baseShareUrl | `string` | | Prefix to add to the generated link. |
| node | [`MinimalNodeEntity`](../content-services/document-library.model.md) | | Node to share. |
| adf-share | [`MinimalNodeEntity`](../content-services/document-library.model.md) | | Node to share. |
| isFile | `boolean` | false | bound to 'adfShare' exportAs |
| isShared | `boolean` | false | bound to 'adfShare' exportAs |
## Details
@ -42,6 +46,14 @@ For example, if you set the input parameter as follows:
[baseShareUrl]="http://localhost:8080/myrouteForShareFile/"
...or through `app.config.json`:
{
...
"baseShareUrl": 'http://external/url',
...
}
...then the directive will ask the [Content service](../core/content.service.md) to generate
a `sharedId` for the file. This will create a URL like the following:

Binary file not shown.

Before

Width:  |  Height:  |  Size: 64 KiB

After

Width:  |  Height:  |  Size: 71 KiB

View File

@ -20,7 +20,7 @@ import Util = require('../../../util/util');
export class ShareDialog {
dialogTitle = element(by.id('adf-share-title'));
dialogTitle = element(by.id('adf-share-dialog-title'));
shareToggle = element(by.id('adf-share-toggle'));
shareLink = element(by.id('adf-share-link'));
closeButton = element(by.css('button[class="mat-button mat-primary"]'));

View File

@ -1,5 +1,6 @@
{
"ecmHost": "http://{hostname}:{port}/ecm",
"bpmHost": "http://{hostname}:{port}/bpm",
"baseShareUrl": null,
"logLevel" : "silent"
}

View File

@ -0,0 +1,50 @@
<div class="adf-share-link__dialog-content">
<h1 data-automation-id="adf-share-dialog-title"
class="adf-share-link__title">
{{ 'SHARE.DIALOG-TITLE' | translate }} {{ fileName }}
</h1>
<mat-dialog-content>
<p class="adf-share-link__info">{{ 'SHARE.DESCRIPTION' | translate }}</p>
<div class="adf-share-link--row">
<h1 class="adf-share-link__label">{{ 'SHARE.TITLE' | translate }}</h1>
<mat-slide-toggle
color="primary"
id="adf-share-toggle"
[checked]="isFileShared"
[disabled]="!canUpdate || isDisabled"
(change)="onSlideShareChange($event)">
</mat-slide-toggle>
</div>
<form [formGroup]="form">
<mat-form-field class="full-width">
<input #sharedLinkInput
data-automation-id="adf-share-link"
class="adf-share-link__input"
matInput
cdkFocusInitial
placeholder="{{ 'SHARE.PUBLIC-LINK' | translate }}"
formControlName="sharedUrl">
<mat-icon class="input-action" matSuffix
[clipboard-notification]="'SHARE.CLIPBOARD-MESSAGE' | translate"
[adf-clipboard]="sharedLinkInput">
link
</mat-icon>
</mat-form-field>
<h1 class="adf-share-link__label">{{ 'SHARE.EXPIRES' | translate }}</h1>
<mat-form-field class="full-width">
<mat-datetimepicker-toggle [for]="datetimePicker" matSuffix></mat-datetimepicker-toggle>
<mat-datetimepicker #datetimePicker type="datetime" openOnFocus="true" timeInterval="1"></mat-datetimepicker>
<input class="adf-share-link__input"
matInput
[min]="minDate"
formControlName="time"
[matDatetimepicker]="datetimePicker">
</mat-form-field>
</form>
</mat-dialog-content>
</div>

View File

@ -0,0 +1,65 @@
@mixin adf-share-link-typography {
letter-spacing: -0.4px;
line-height: 2;
font-weight: normal;
font-style: normal;
font-stretch: normal;
font-size: 16px;
opacity: 0.87;
}
.adf-share-link-dialog {
.adf-share-link {
&__dialog-content {
display: flex;
flex-direction: column;
}
&__label {
@include adf-share-link-typography;
flex: 1 1 auto;
}
&__title {
@include adf-share-link-typography;
}
&__info {
@include adf-share-link-typography;
opacity: 0.54;
font-size: 13px;
}
&--row {
display: flex;
flex-direction: row;
flex-wrap: wrap;
align-items: center;
}
&__input {
opacity: 0.54;
}
}
.input-action {
cursor: pointer;
}
.full-width {
width: 100%;
}
.mat-form-field-infix {
border-top: unset;
}
.mat-dialog-actions {
justify-content: flex-end;
}
.mat-form-field-flex {
align-items: center;
}
}

View File

@ -0,0 +1,242 @@
/*!
* @license
* Copyright 2016 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { TestBed, fakeAsync } from '@angular/core/testing';
import { MatDialogRef, MAT_DIALOG_DATA, MatDialog } from '@angular/material';
import { of } from 'rxjs';
import { setupTestBed, CoreModule, SharedLinksApiService, NodesApiService } from '@alfresco/adf-core';
import { ContentNodeShareModule } from './content-node-share.module';
import { ShareDialogComponent } from './content-node-share.dialog';
import moment from 'moment-es6';
describe('ShareDialogComponent', () => {
let node;
let matDialog: MatDialog;
let sharedLinksApiService: SharedLinksApiService;
let nodesApiService: NodesApiService;
let fixture;
let component;
setupTestBed({
imports: [
NoopAnimationsModule,
CoreModule.forRoot(),
ContentNodeShareModule
],
providers: [
NodesApiService,
SharedLinksApiService,
{ provide: MatDialogRef, useValue: {} },
{ provide: MAT_DIALOG_DATA, useValue: {} }
]
});
beforeEach(() => {
fixture = TestBed.createComponent(ShareDialogComponent);
matDialog = TestBed.get(MatDialog);
sharedLinksApiService = TestBed.get(SharedLinksApiService);
nodesApiService = TestBed.get(NodesApiService);
component = fixture.componentInstance;
});
beforeEach(() => {
node = {
entry: {
id: 'nodeId',
allowableOperations: ['update'],
isFile: true,
properties: {}
}
};
});
afterEach(() => {
fixture.destroy();
});
it(`should toggle share action when property 'sharedId' does not exists`, () => {
spyOn(sharedLinksApiService, 'createSharedLinks').and.returnValue(of({
entry: { id: 'sharedId', sharedId: 'sharedId' }
}));
component.data = {
node,
baseShareUrl: 'some-url/'
};
fixture.detectChanges();
expect(sharedLinksApiService.createSharedLinks).toHaveBeenCalled();
expect(fixture.nativeElement.querySelector('input[formcontrolname="sharedUrl"]').value).toBe('some-url/sharedId');
expect(fixture.nativeElement.querySelector('.mat-slide-toggle').classList).toContain('mat-checked');
});
it(`should not toggle share action when file has 'sharedId' property`, () => {
spyOn(sharedLinksApiService, 'createSharedLinks');
node.entry.properties['qshare:sharedId'] = 'sharedId';
component.data = {
node,
baseShareUrl: 'some-url/'
};
fixture.detectChanges();
expect(sharedLinksApiService.createSharedLinks).not.toHaveBeenCalled();
expect(fixture.nativeElement.querySelector('input[formcontrolname="sharedUrl"]').value).toBe('some-url/sharedId');
expect(fixture.nativeElement.querySelector('.mat-slide-toggle').classList).toContain('mat-checked');
});
it(`should copy shared link automatically and notify`, () => {
jasmine.clock().uninstall();
jasmine.clock().install();
spyOn(document, 'execCommand');
node.entry.properties['qshare:sharedId'] = 'sharedId';
component.data = {
node,
baseShareUrl: 'some-url/'
};
fixture.detectChanges();
jasmine.clock().tick(100);
expect(document.execCommand).toHaveBeenCalledWith('copy');
expect(document.body.querySelector('snack-bar-container span').innerHTML)
.toBe('SHARE.CLIPBOARD-MESSAGE');
jasmine.clock().uninstall();
});
it(`should copy shared link and notify on button event`, () => {
node.entry.properties['qshare:sharedId'] = 'sharedId';
spyOn(document, 'execCommand').and.callThrough();
component.data = {
node,
baseShareUrl: 'some-url/'
};
fixture.detectChanges();
fixture.nativeElement.querySelector('.input-action')
.dispatchEvent(new MouseEvent('click'));
fixture.detectChanges();
expect(document.execCommand).toHaveBeenCalledWith('copy');
expect(document.body.querySelector('snack-bar-container span').innerHTML)
.toBe('SHARE.CLIPBOARD-MESSAGE');
});
it('should open a confirmation dialog when unshare button is triggered', () => {
spyOn(matDialog, 'open').and.returnValue({ beforeClose: () => of(false) });
spyOn(sharedLinksApiService, 'deleteSharedLink');
node.entry.properties['qshare:sharedId'] = 'sharedId';
component.data = {
node,
baseShareUrl: 'some-url/'
};
fixture.detectChanges();
fixture.nativeElement.querySelector('.mat-slide-toggle label')
.dispatchEvent(new MouseEvent('click'));
fixture.detectChanges();
expect(matDialog.open).toHaveBeenCalled();
});
it('should unshare file when confirmation dialog returns true', fakeAsync(() => {
spyOn(matDialog, 'open').and.returnValue({ beforeClose: () => of(true) });
spyOn(sharedLinksApiService, 'deleteSharedLink');
node.entry.properties['qshare:sharedId'] = 'sharedId';
component.data = {
node,
baseShareUrl: 'some-url/'
};
fixture.detectChanges();
fixture.nativeElement.querySelector('.mat-slide-toggle label')
.dispatchEvent(new MouseEvent('click'));
fixture.detectChanges();
expect(sharedLinksApiService.deleteSharedLink).toHaveBeenCalled();
}));
it('should not unshare file when confirmation dialog returns false', fakeAsync(() => {
spyOn(matDialog, 'open').and.returnValue({ beforeClose: () => of(false) });
spyOn(sharedLinksApiService, 'deleteSharedLink');
node.entry.properties['qshare:sharedId'] = 'sharedId';
component.data = {
node,
baseShareUrl: 'some-url/'
};
fixture.detectChanges();
fixture.nativeElement.querySelector('.mat-slide-toggle label')
.dispatchEvent(new MouseEvent('click'));
fixture.detectChanges();
expect(sharedLinksApiService.deleteSharedLink).not.toHaveBeenCalled();
}));
it('should not allow unshare when node has no update permission', () => {
node.entry.properties['qshare:sharedId'] = 'sharedId';
node.entry.allowableOperations = [];
component.data = {
node,
baseShareUrl: 'some-url/'
};
fixture.detectChanges();
expect(fixture.nativeElement.querySelector('.mat-slide-toggle').classList).toContain('mat-disabled');
expect(fixture.nativeElement.querySelector('input[formcontrolname="time"]').disabled).toBe(true);
expect(fixture.nativeElement.querySelector('mat-datetimepicker-toggle button').disabled).toBe(true);
});
it('should not update shared node expiryDate property when value changes', () => {
const date = moment();
node.entry.properties['qshare:sharedId'] = 'sharedId';
spyOn(nodesApiService, 'updateNode');
fixture.componentInstance.form.controls['time'].setValue(null);
component.data = {
node,
baseShareUrl: 'some-url/'
};
fixture.detectChanges();
fixture.componentInstance.form.controls['time'].setValue(date);
fixture.detectChanges();
expect(nodesApiService.updateNode).toHaveBeenCalled();
});
});

View File

@ -0,0 +1,226 @@
/*!
* @license
* Copyright 2016 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {
Component,
Inject,
OnInit,
ViewEncapsulation,
ViewChild,
ElementRef,
OnDestroy
} from '@angular/core';
import { MAT_DIALOG_DATA, MatDialogRef, MatDialog } from '@angular/material';
import { FormGroup, FormControl } from '@angular/forms';
import { Subscription, Observable } from 'rxjs';
import { tap, skip } from 'rxjs/operators';
import {
TranslationService,
SharedLinksApiService,
ClipboardService,
NodesApiService,
ContentService
} from '@alfresco/adf-core';
import { SharedLinkEntry, MinimalNodeEntryEntity } from 'alfresco-js-api';
import { ConfirmDialogComponent } from '../dialogs/confirm.dialog';
import moment from 'moment-es6';
@Component({
selector: 'adf-share-dialog',
templateUrl: './content-node-share.dialog.html',
styleUrls: ['./content-node-share.dialog.scss'],
host: { 'class': 'adf-share-dialog' },
encapsulation: ViewEncapsulation.None
})
export class ShareDialogComponent implements OnInit, OnDestroy {
private subscriptions: Subscription[] = [];
minDate = moment().toDate();
sharedId: string;
fileName: string;
baseShareUrl: string;
isFileShared: boolean = false;
isDisabled: boolean = false;
form: FormGroup = new FormGroup({
'sharedUrl': new FormControl(''),
'time': new FormControl({value: '', disabled: false})
});
@ViewChild('sharedLinkInput') sharedLinkInput: ElementRef;
constructor(
private sharedLinksApiService: SharedLinksApiService,
private dialogRef: MatDialogRef<ShareDialogComponent>,
private dialog: MatDialog,
private nodesApiService: NodesApiService,
private clipboardService: ClipboardService,
private contentService: ContentService,
private translation: TranslationService,
@Inject(MAT_DIALOG_DATA) public data: any) {
}
ngOnInit() {
if (!this.canUpdate) {
this.form.controls['time'].disable();
}
this.subscriptions.push(
this.form.valueChanges
.pipe(
skip(1),
tap((updates) => {
this.updateNode(updates);
})
)
.subscribe((updates) => this.updateEntryExpiryDate(updates))
);
if (this.data.node && this.data.node.entry) {
this.fileName = this.data.node.entry.name;
this.baseShareUrl = this.data.baseShareUrl;
const properties = this.data.node.entry.properties;
if (properties && !properties['qshare:sharedId']) {
this.createSharedLinks(this.data.node.entry.id);
} else {
this.sharedId = properties['qshare:sharedId'];
this.isFileShared = true;
this.updateForm();
this.copyToClipboard();
}
}
}
ngOnDestroy() {
this.subscriptions.forEach((subscription) => subscription.unsubscribe);
}
removeShare() {
this.deleteSharedLink(this.sharedId);
}
onSlideShareChange(event: any) {
if (event.checked) {
this.createSharedLinks(this.data.node.entry.id);
} else {
this.openConfirmationDialog();
}
}
get canUpdate() {
return this.contentService.hasPermission(this.data.node.entry, 'update');
}
private openConfirmationDialog() {
this.isFileShared = false;
this.dialog
.open(ConfirmDialogComponent, {
data: {
title: 'SHARE.CONFIRMATION.DIALOG-TITLE',
message: 'SHARE.CONFIRMATION.MESSAGE',
yesLabel: 'SHARE.CONFIRMATION.REMOVE',
noLabel: 'SHARE.CONFIRMATION.CANCEL'
},
minWidth: '250px',
closeOnNavigation: true
})
.beforeClose().subscribe(deleteSharedLink => {
if (deleteSharedLink) {
this.deleteSharedLink(this.sharedId);
} else {
this.isFileShared = true;
}
});
}
private createSharedLinks(nodeId: string) {
this.isDisabled = true;
this.sharedLinksApiService.createSharedLinks(nodeId)
.subscribe((sharedLink: SharedLinkEntry) => {
if (sharedLink.entry) {
this.sharedId = sharedLink.entry.id;
this.data.node.entry.properties['qshare:sharedId'] = this.sharedId;
this.isDisabled = false;
this.isFileShared = true;
this.updateForm();
this.copyToClipboard();
}
},
() => {
this.isDisabled = false;
this.isFileShared = false;
});
}
private deleteSharedLink(sharedId: string) {
this.isDisabled = true;
this.sharedLinksApiService.deleteSharedLink(sharedId).subscribe(() => {
this.data.node.entry.properties['qshare:sharedId'] = null;
this.data.node.entry.properties['qshare:expiryDate'] = null;
this.dialogRef.close(false);
},
() => {
this.isDisabled = false;
this.isFileShared = false;
});
}
private copyToClipboard() {
setTimeout(() => this.clipboardService.copyToClipboard(
this.sharedLinkInput.nativeElement,
this.translation.instant('SHARE.CLIPBOARD-MESSAGE')
), 0);
}
private updateForm() {
const { entry } = this.data.node;
const expiryDate = entry.properties['qshare:expiryDate'];
this.form.setValue({
'sharedUrl': `${this.baseShareUrl}${this.sharedId}`,
'time': expiryDate ? moment(expiryDate) : null
});
}
private updateNode(updates): Observable<MinimalNodeEntryEntity> {
return this.nodesApiService.updateNode(
this.data.node.entry.id,
{
properties: {
'qshare:expiryDate': updates.time ? updates.time.toDate() : null
}
}
);
}
private updateEntryExpiryDate(updates) {
const { properties } = this.data.node.entry;
properties['qshare:expiryDate'] = updates.time
? updates.time.toDate()
: null;
}
}

View File

@ -0,0 +1,154 @@
/*!
* @license
* Copyright 2016 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { ComponentFixture } from '@angular/core/testing';
import { Component } from '@angular/core';
import { DOCUMENT } from '@angular/platform-browser';
import { ContentTestingModule } from '../testing/content.testing.module';
import { setupTestBed, CoreModule, SharedLinksApiService } from '@alfresco/adf-core';
import { ContentNodeShareModule } from './content-node-share.module';
@Component({
selector: 'adf-node-share-test-component',
template: `
<button
[disabled]="!shareRef.isFile"
#shareRef="adfShare"
[baseShareUrl]="baseShareUrl"
[adf-share]="documentList.selection[0]"
[title]="shareRef.isShared ? 'Shared' : 'Not Shared'">
</button>
`
})
class NodeShareTestComponent {
baseShareUrl = 'some-url/';
documentList = {
selection: []
};
}
describe('NodeSharedDirective', () => {
let fixture: ComponentFixture<NodeShareTestComponent>;
let component: NodeShareTestComponent;
let document: any;
let shareButtonElement;
let selection;
setupTestBed({
imports: [
NoopAnimationsModule,
CoreModule.forRoot(),
ContentTestingModule,
ContentNodeShareModule
],
declarations: [
NodeShareTestComponent
],
providers: [
SharedLinksApiService
]
});
beforeEach(() => {
fixture = TestBed.createComponent(NodeShareTestComponent);
document = TestBed.get(DOCUMENT);
component = fixture.componentInstance;
fixture.detectChanges();
});
afterEach(() => {
fixture.destroy();
});
beforeEach(() => {
shareButtonElement = fixture.debugElement.query(By.css('button')).nativeElement;
selection = {
entry: {
isFile: true,
properties: {}
}
};
});
it('should have share button disabled when selection is empty', async() => {
fixture.whenStable().then(() => {
fixture.detectChanges();
expect(shareButtonElement.disabled).toBe(true);
});
});
it('should have button enabled when selection is not empty', () => {
component.documentList.selection = [selection];
fixture.detectChanges();
fixture.whenStable().then(() => {
fixture.detectChanges();
expect(shareButtonElement.disabled).toBe(false);
});
});
it('should have button disabled when selection is not a file', () => {
selection.entry.isFile = false;
component.documentList.selection = [selection];
fixture.detectChanges();
fixture.whenStable().then(() => {
fixture.detectChanges();
expect(shareButtonElement.disabled).toBe(true);
});
});
it('should indicate if file is not shared', () => {
component.documentList.selection = [selection];
fixture.detectChanges();
fixture.whenStable().then(() => {
fixture.detectChanges();
expect(shareButtonElement.title).toBe('Not Shared');
});
});
it('should indicate if file is already shared', () => {
selection.entry.properties['qshare:sharedId'] = 'someId';
component.documentList.selection = [selection];
fixture.detectChanges();
fixture.whenStable().then(() => {
fixture.detectChanges();
expect(shareButtonElement.title).toBe('Shared');
});
});
it('should open share dialog on click event', () => {
selection.entry.properties['qshare:sharedId'] = 'someId';
component.documentList.selection = [selection];
fixture.detectChanges();
fixture.whenStable().then(() => {
fixture.detectChanges();
shareButtonElement.click();
fixture.detectChanges();
expect(document.querySelector('.adf-share-link-dialog')).not.toBe(null);
});
});
});

View File

@ -15,60 +15,57 @@
* limitations under the License.
*/
import { Directive, Input, HostListener, ElementRef, OnChanges } from '@angular/core';
import { Directive, Input, HostListener, OnChanges, NgZone } from '@angular/core';
import { MatDialog } from '@angular/material';
import { MinimalNodeEntity } from 'alfresco-js-api';
import { ShareDialogComponent } from '../dialogs/share.dialog';
import { ShareDialogComponent } from './content-node-share.dialog';
@Directive({
selector: '[adf-share]'
selector: '[adf-share]',
exportAs: 'adfShare'
})
export class NodeSharedDirective implements OnChanges {
isFile: boolean = false;
isShared: boolean = false;
/** Node to share. */
// tslint:disable-next-line:no-input-rename
@Input('adf-share')
node: MinimalNodeEntity;
/** Prefix to add to the generated link. */
@Input()
baseShareUrl: string;
@HostListener('click')
onClick() {
if (this.node) {
this.shareNode(this.node);
}
constructor(private dialog: MatDialog,
private elementRef: ElementRef) {
}
constructor(private dialog: MatDialog, private zone: NgZone) {}
shareNode(node: MinimalNodeEntity) {
if (node && node.entry && node.entry.isFile) {
this.dialog.open(ShareDialogComponent, {
width: '600px',
disableClose: true,
panelClass: 'adf-share-link-dialog',
data: {
node: node,
baseShareUrl: this.baseShareUrl
}
});
} else {
this.setDisableAttribute(true);
}
}
ngOnChanges() {
if (!this.node || this.node.entry.isFolder) {
this.setDisableAttribute(true);
} else {
this.setDisableAttribute(false);
this.zone.onStable.subscribe(() => {
if (this.node) {
this.isFile = this.node.entry.isFile;
this.isShared = this.node.entry.properties['qshare:sharedId'];
}
});
}
private setDisableAttribute(disable: boolean) {
this.elementRef.nativeElement.disabled = disable;
}
}

View File

@ -0,0 +1,55 @@
/*!
* @license
* Copyright 2016 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { NgModule, ModuleWithProviders } from '@angular/core';
import { CommonModule } from '@angular/common';
import { CoreModule } from '@alfresco/adf-core';
import { MaterialModule } from '../material.module';
import { ShareDialogComponent } from './content-node-share.dialog';
import { NodeSharedDirective } from './content-node-share.directive';
@NgModule({
imports: [
CoreModule.forChild(),
CommonModule,
MaterialModule
],
declarations: [
ShareDialogComponent,
NodeSharedDirective
],
exports: [
ShareDialogComponent,
NodeSharedDirective
],
entryComponents: [
ShareDialogComponent
]
})
export class ContentNodeShareModule {
static forRoot(): ModuleWithProviders {
return {
ngModule: ContentNodeShareModule
};
}
static forChild(): ModuleWithProviders {
return {
ngModule: ContentNodeShareModule
};
}
}

View File

@ -0,0 +1,18 @@
/*!
* @license
* Copyright 2016 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export * from './public-api';

View File

@ -0,0 +1,21 @@
/*!
* @license
* Copyright 2016 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export * from './content-node-share.dialog';
export * from './content-node-share.directive';
export * from './content-node-share.module';

View File

@ -32,6 +32,7 @@ import { SitesDropdownModule } from './site-dropdown/sites-dropdown.module';
import { BreadcrumbModule } from './breadcrumb/breadcrumb.module';
import { VersionManagerModule } from './version-manager/version-manager.module';
import { ContentNodeSelectorModule } from './content-node-selector/content-node-selector.module';
import { ContentNodeShareModule } from './content-node-share/content-node-share.module';
import { ContentDirectiveModule } from './directives/content-directive.module';
import { DialogModule } from './dialogs/dialog.module';
import { FolderDirectiveModule } from './folder-directive/folder-directive.module';
@ -96,6 +97,7 @@ export function providers() {
SitesDropdownModule,
BreadcrumbModule,
ContentNodeSelectorModule,
ContentNodeShareModule,
ContentMetadataModule,
FolderDirectiveModule,
ContentDirectiveModule,
@ -112,6 +114,7 @@ export function providers() {
SitesDropdownModule,
BreadcrumbModule,
ContentNodeSelectorModule,
ContentNodeShareModule,
ContentMetadataModule,
DialogModule,
FolderDirectiveModule,
@ -139,6 +142,7 @@ export class ContentModuleLazy {}
SitesDropdownModule,
BreadcrumbModule,
ContentNodeSelectorModule,
ContentNodeShareModule,
ContentMetadataModule,
FolderDirectiveModule,
ContentDirectiveModule,
@ -166,6 +170,7 @@ export class ContentModuleLazy {}
SitesDropdownModule,
BreadcrumbModule,
ContentNodeSelectorModule,
ContentNodeShareModule,
ContentMetadataModule,
DialogModule,
FolderDirectiveModule,

View File

@ -24,7 +24,6 @@ import { MaterialModule } from '../material.module';
import { DownloadZipDialogComponent } from './download-zip.dialog';
import { FolderDialogComponent } from './folder.dialog';
import { NodeLockDialogComponent } from './node-lock.dialog';
import { ShareDialogComponent } from './share.dialog';
import { ConfirmDialogComponent } from './confirm.dialog';
import { MatDatetimepickerModule } from '@mat-datetimepicker/core';
import { MatMomentDatetimeModule } from '@mat-datetimepicker/moment';
@ -43,21 +42,18 @@ import { MatMomentDatetimeModule } from '@mat-datetimepicker/moment';
DownloadZipDialogComponent,
FolderDialogComponent,
NodeLockDialogComponent,
ShareDialogComponent,
ConfirmDialogComponent
],
exports: [
DownloadZipDialogComponent,
FolderDialogComponent,
NodeLockDialogComponent,
ShareDialogComponent,
ConfirmDialogComponent
],
entryComponents: [
DownloadZipDialogComponent,
FolderDialogComponent,
NodeLockDialogComponent,
ShareDialogComponent,
ConfirmDialogComponent
]
})

View File

@ -18,7 +18,6 @@
export * from './download-zip.dialog';
export * from './folder.dialog';
export * from './node-lock.dialog';
export * from './share.dialog';
export * from './confirm.dialog';
export * from './dialog.module';

View File

@ -1,32 +0,0 @@
<h1 mat-dialog-title id="adf-share-title">{{ 'SHARE.TITLE' | translate }} {{fileName}}</h1>
<mat-dialog-content>
<span class="spacer"></span>
<mat-slide-toggle id="adf-share-toggle" [checked]="isFileShared" [disabled]="isDisabled"
(change)="onSlideShareChange($event)">
{{ 'SHARE.ACTIONS.SHARE' | translate }}
</mat-slide-toggle>
<span class="spacer"></span>
<h2 *ngIf="isFileShared">{{ 'SHARE.DESCRIPTION' | translate }}</h2>
<div *ngIf="isFileShared">{{ 'SHARE.ALERT' | translate }}</div>
<form *ngIf="isFileShared">
<mat-form-field class="full-width">
<input id="adf-share-link" lass="adf-share-link" matInput placeholder="{{ 'SHARE.PUBLIC-LINK' | translate }}"
[value]="baseShareUrl + sharedId">
<mat-icon matPrefix>link</mat-icon>
</mat-form-field>
</form>
<h2 *ngIf="!isFileShared">{{ 'SHARE.UNSHARED' | translate }}</h2>
</mat-dialog-content>
<mat-dialog-actions>
<button mat-button color="primary" (click)="cancelShare()">
{{ 'SHARE.ACTIONS.CLOSE' | translate }}
</button>
</mat-dialog-actions>

View File

@ -1,14 +0,0 @@
.spacer { flex: 1 1 auto; }
.adf-share-dialog .mat-dialog-actions .mat-button-wrapper {
text-transform: uppercase;
}
.full-width {
width: 100%;
}
.adf-share-link {
margin-left: 10px;
font-size: 18px !important;
}

View File

@ -1,128 +0,0 @@
/*!
* @license
* Copyright 2016 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { async, TestBed } from '@angular/core/testing';
import { ComponentFixture } from '@angular/core/testing';
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material';
import { of } from 'rxjs';
import { ShareDialogComponent } from './share.dialog';
import { ContentTestingModule } from '../testing/content.testing.module';
import { SharedLinksApiService, setupTestBed } from '@alfresco/adf-core';
describe('ShareDialogComponent', () => {
let fixture: ComponentFixture<ShareDialogComponent>;
let spyCreate: any;
let spyDelete: any;
let component: ShareDialogComponent;
let sharedLinksApiService: SharedLinksApiService;
const dialogRef = {
close: jasmine.createSpy('close')
};
let data: any = {
node: { entry: { properties: { 'qshare:sharedId': 'example-link' }, name: 'example-name' } },
baseShareUrl: 'baseShareUrl-example'
};
setupTestBed({
imports: [ContentTestingModule],
providers: [
{ provide: MatDialogRef, useValue: dialogRef },
{ provide: MAT_DIALOG_DATA, useValue: data }
]
});
beforeEach(() => {
fixture = TestBed.createComponent(ShareDialogComponent);
sharedLinksApiService = TestBed.get(SharedLinksApiService);
component = fixture.componentInstance;
fixture.detectChanges();
spyCreate = spyOn(sharedLinksApiService, 'createSharedLinks').and.returnValue(of({ entry: { id: 'test-sharedId' } }));
spyDelete = spyOn(sharedLinksApiService, 'deleteSharedLink').and.returnValue(of({}));
});
it('should init the dialog with the file name and baseShareUrl', async(() => {
component.data = {
baseShareUrl: 'base-url/',
node: { entry: { properties: { 'qshare:sharedId': 'example-link' }, name: 'example-name' } }
};
component.ngOnInit();
fixture.detectChanges();
fixture.whenStable().then(() => {
expect(fixture.nativeElement.querySelector('#adf-share-link').value).toBe('base-url/example-link');
});
}));
describe('public link creation', () => {
it('should not create the public link if it already present in the node', () => {
component.data = {
node: { entry: { properties: { 'qshare:sharedId': 'example-link' }, name: 'example-name' } }
};
component.ngOnInit();
expect(spyCreate).not.toHaveBeenCalled();
});
it('should not create the public link if is not present in the node', () => {
component.data = {
node: { entry: { properties: {}, name: 'example-name' } },
baseShareUrl: 'baseShareUrl-example'
};
component.ngOnInit();
expect(spyCreate).not.toHaveBeenCalled();
});
it('should be able to delete the shared link', async(() => {
component.data = {
node: { entry: { name: 'example-name', properties: { 'qshare:sharedId': 'example-link' } } },
baseShareUrl: 'baseShareUrl-example'
};
component.ngOnInit();
fixture.detectChanges();
fixture.nativeElement.querySelector('#adf-share-toggle-input').click();
fixture.detectChanges();
fixture.whenStable().then(() => {
expect(spyDelete).toHaveBeenCalled();
expect(component.data.node.entry.properties['qshare:sharedId']).toBeNull();
});
}));
it('should show the toggle disabled when the shareid property is null', async(() => {
component.data = {
node: { entry: { name: 'example-name', properties: { 'qshare:sharedId': null } } },
baseShareUrl: 'baseShareUrl-example'
};
component.ngOnInit();
fixture.detectChanges();
fixture.whenStable().then(() => {
expect(fixture.nativeElement.querySelector('#adf-share-toggle')).not.toBeNull();
expect(fixture.nativeElement.querySelector('#adf-share-toggle.mat-checked')).toBeNull();
});
}));
});
});

View File

@ -1,100 +0,0 @@
/*!
* @license
* Copyright 2016 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Component, Inject, OnInit, ViewEncapsulation } from '@angular/core';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material';
import { SharedLinksApiService } from '@alfresco/adf-core';
import { SharedLinkEntry } from 'alfresco-js-api';
@Component({
selector: 'adf-share-dialog',
templateUrl: './share.dialog.html',
styleUrls: ['./share.dialog.scss'],
host: { 'class': 'adf-share-dialog' },
encapsulation: ViewEncapsulation.None
})
export class ShareDialogComponent implements OnInit {
sharedId: string;
fileName: string;
baseShareUrl: string;
isFileShared: boolean = false;
isDisabled: boolean = false;
constructor(private sharedLinksApiService: SharedLinksApiService,
private dialogRef: MatDialogRef<ShareDialogComponent>,
@Inject(MAT_DIALOG_DATA)
public data: any) {
}
ngOnInit() {
if (this.data.node && this.data.node.entry) {
this.fileName = this.data.node.entry.name;
this.baseShareUrl = this.data.baseShareUrl;
if (this.data.node.entry.properties && this.data.node.entry.properties['qshare:sharedId']) {
this.sharedId = this.data.node.entry.properties['qshare:sharedId'];
this.isFileShared = true;
} else {
this.isFileShared = false;
this.isDisabled = false;
}
}
}
cancelShare() {
this.dialogRef.close(false);
}
onSlideShareChange(event: any) {
this.isDisabled = true;
if (event.checked) {
this.createSharedLinks(this.data.node.entry.id);
} else {
this.deleteSharedLink(this.sharedId);
}
}
createSharedLinks(nodeId: string) {
this.sharedLinksApiService.createSharedLinks(nodeId).subscribe((sharedLink: SharedLinkEntry) => {
if (sharedLink.entry) {
this.sharedId = sharedLink.entry.id;
this.data.node.entry.properties['qshare:sharedId'] = this.sharedId;
this.isFileShared = true;
this.isDisabled = false;
}
},
() => {
this.isFileShared = false;
this.isDisabled = false;
});
}
deleteSharedLink(sharedId: string) {
this.sharedLinksApiService.deleteSharedLink(sharedId).subscribe(() => {
this.data.node.entry.properties['qshare:sharedId'] = null;
this.isFileShared = false;
this.isDisabled = false;
},
() => {
this.isFileShared = true;
this.isDisabled = false;
});
}
}

View File

@ -20,7 +20,6 @@ import { NgModule } from '@angular/core';
import { MaterialModule } from '../material.module';
import { NodeDownloadDirective } from './node-download.directive';
import { NodeSharedDirective } from './node-share.directive';
import { NodeLockDirective } from './node-lock.directive';
@NgModule({
@ -30,12 +29,10 @@ import { NodeLockDirective } from './node-lock.directive';
],
declarations: [
NodeDownloadDirective,
NodeSharedDirective,
NodeLockDirective
],
exports: [
NodeDownloadDirective,
NodeSharedDirective,
NodeLockDirective
]
})

View File

@ -1,117 +0,0 @@
/*!
* @license
* Copyright 2016 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Component, DebugElement } from '@angular/core';
import { fakeAsync, async, ComponentFixture, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { NodeSharedDirective } from './node-share.directive';
import { DialogModule } from '../dialogs/dialog.module';
import { MatDialog } from '@angular/material';
@Component({
template: `
<div [adf-share]="node">
</div>`
})
class TestComponent {
node = null;
done = jasmine.createSpy('done');
}
describe('NodeSharedDirective', () => {
let fixture: ComponentFixture<TestComponent>;
let element: DebugElement;
let component: TestComponent;
let dialog: MatDialog;
let dialogSpy;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
DialogModule
],
declarations: [
TestComponent,
NodeSharedDirective
]
}).compileComponents()
.then(() => {
fixture = TestBed.createComponent(TestComponent);
component = fixture.componentInstance;
dialog = TestBed.get(MatDialog);
element = fixture.debugElement.query(By.directive(NodeSharedDirective));
dialogSpy = spyOn(dialog, 'open');
});
}));
describe('Share', () => {
it('should not share when selection has no nodes', () => {
component.node = null;
fixture.detectChanges();
element.triggerEventHandler('click', null);
expect(dialogSpy).not.toHaveBeenCalled();
});
it('should not share when the selection node is a folder', () => {
component.node = {
entry: {
isFolder: true
}
};
fixture.detectChanges();
element.triggerEventHandler('click', null);
expect(dialogSpy).not.toHaveBeenCalled();
});
it('should share when the selection node is a file', () => {
component.node = {
entry: {
isFolder: false,
isFile: true
}
};
fixture.detectChanges();
element.triggerEventHandler('click', null);
expect(dialogSpy).toHaveBeenCalled();
});
it('should disable the button if no node is selected', fakeAsync(() => {
component.node = null;
fixture.detectChanges();
expect(element.nativeElement.disabled).toEqual(true);
}));
it('should enable the button if nodes is selected and is a file', fakeAsync(() => {
component.node = { entry: { id: '1', name: 'name1', isFolder: false, isFile: true } };
fixture.detectChanges();
expect(element.nativeElement.disabled).toEqual(false);
}));
});
});

View File

@ -16,6 +16,5 @@
*/
export * from './node-download.directive';
export * from './node-share.directive';
export * from './content-directive.module';

View File

@ -254,15 +254,16 @@
}
},
"SHARE": {
"TITLE": "Share",
"PUBLIC-LINK": "Public link",
"UNSHARED": "File not shared",
"DESCRIPTION": "This is a link to your file",
"ALERT": "Anyone can access this link",
"ACTIONS": {
"SHARE": "SHARE",
"CLOSE": "CLOSE",
"COPY-LINK": "COPY LINK"
"DIALOG-TITLE": "Share",
"DESCRIPTION": "Clicking the link bellow, will copy it to the clipboard.",
"TITLE": "Link to share",
"EXPIRES": "Expires on",
"CLIPBOARD-MESSAGE": "Link copied to the clipboard",
"CONFIRMATION": {
"DIALOG-TITLE": "Remove this shared link",
"MESSAGE": "This link will be deleted and a new link will be created next time this file is shared",
"CANCEL": "Cancel",
"REMOVE": "Remove"
}
},
"PERMISSION_MANAGER": {

View File

@ -30,5 +30,6 @@ export * from './dialogs/index';
export * from './folder-directive/index';
export * from './content-metadata/index';
export * from './permission-manager/index';
export * from './content-node-share/index';
export * from './content.module';

View File

@ -26,6 +26,7 @@ export enum AppConfigValues {
PROVIDERS = 'providers',
OAUTHCONFIG = 'oauth2',
ECMHOST = 'ecmHost',
BASESHAREURL = 'baseShareUrl',
BPMHOST = 'bpmHost',
AUTHTYPE = 'authType',
CONTEXTROOTECM = 'contextRootEcm',

View File

@ -0,0 +1,67 @@
/*!
* @license
* Copyright 2016 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Component } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { setupTestBed } from '../testing/setupTestBed';
import { CoreModule } from '../core.module';
import { ClipboardService } from './clipboard.service';
@Component({
selector: 'adf-test-component',
template: `
<button
clipboard-notification="copy success"
[adf-clipboard]="ref">
copy
</button>
<input #ref />
`
})
class TestComponent {}
describe('ClipboardDirective', () => {
let fixture: ComponentFixture<TestComponent>;
let clipboardService: ClipboardService;
setupTestBed({
imports: [
CoreModule.forRoot()
],
declarations: [
TestComponent
],
providers: [
ClipboardService
]
});
beforeEach(() => {
fixture = TestBed.createComponent(TestComponent);
clipboardService = TestBed.get(ClipboardService);
fixture.detectChanges();
});
it('should notify copy target value on button click event', () => {
spyOn(clipboardService, 'copyToClipboard');
fixture.nativeElement.querySelector('input').value = 'some value';
fixture.nativeElement.querySelector('button').dispatchEvent(new MouseEvent('click'));
expect(clipboardService.copyToClipboard).toHaveBeenCalled();
});
});

View File

@ -0,0 +1,47 @@
/*!
* @license
* Copyright 2016 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Directive, Input, HostListener } from '@angular/core';
import { ClipboardService } from './clipboard.service';
@Directive({
selector: '[adf-clipboard]',
exportAs: 'adfClipboard'
})
export class ClipboardDirective {
// tslint:disable-next-line:no-input-rename
@Input('adf-clipboard') target: HTMLInputElement | HTMLTextAreaElement;
// tslint:disable-next-line:no-input-rename
@Input('clipboard-notification') message: string;
@HostListener('click', ['$event'])
handleClickEvent(event: MouseEvent) {
event.preventDefault();
this.copyToClipboard();
}
constructor(private clipboardService: ClipboardService) {}
private copyToClipboard() {
const isValidTarget = this.clipboardService.isTargetValid(this.target);
if (isValidTarget) {
this.clipboardService.copyToClipboard(this.target, this.message);
}
}
}

View File

@ -0,0 +1,38 @@
/*!
* @license
* Copyright 2016 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core';
import { ClipboardDirective } from './clipboard.directive';
import { ClipboardService } from './clipboard.service';
@NgModule({
imports: [
CommonModule
],
providers: [
ClipboardService
],
declarations: [
ClipboardDirective
],
exports: [
ClipboardDirective
]
})
export class ClipboardModule {}

View File

@ -0,0 +1,91 @@
/*!
* @license
* Copyright 2016 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { LogService } from '../services/log.service';
import { NotificationService } from '../services/notification.service';
import { AppConfigService } from '../app-config/app-config.service';
import { TestBed } from '@angular/core/testing';
import { ClipboardModule } from './clipboard.module';
import { ClipboardService } from './clipboard.service';
describe('ClipboardService', () => {
let clipboardService: ClipboardService;
let notificationService: NotificationService;
let inputElement;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
ClipboardModule
],
providers: [
LogService,
NotificationService,
{
provide: AppConfigService,
useValue: new AppConfigService(null)
},
{
provide: NotificationService,
useValue: new NotificationService(null, null)
}
]
});
});
beforeEach(() => {
clipboardService = TestBed.get(ClipboardService);
notificationService = TestBed.get(NotificationService);
inputElement = document.createElement('input');
});
it('should validate target when element is input', () => {
const isValid = clipboardService.isTargetValid(inputElement);
expect(isValid).toBe(true);
});
it('should invalidate target when element is input and disabled', () => {
inputElement.disabled = true;
const isValid = clipboardService.isTargetValid(inputElement);
expect(isValid).toBe(false);
});
it('should copy text to clipboard', () => {
spyOn(document, 'execCommand');
spyOn(inputElement, 'select');
spyOn(inputElement, 'setSelectionRange');
inputElement.value = 'some text';
clipboardService.copyToClipboard(inputElement);
expect(inputElement.select).toHaveBeenCalledWith();
expect(inputElement.setSelectionRange)
.toHaveBeenCalledWith(0, inputElement.value.length);
expect(document.execCommand).toHaveBeenCalledWith('copy');
});
it('should notify copy to clipboard with message', () => {
spyOn(notificationService, 'openSnackMessage');
inputElement.value = 'some text';
clipboardService.copyToClipboard(inputElement, 'success');
expect(notificationService.openSnackMessage).toHaveBeenCalledWith('success');
});
});

View File

@ -0,0 +1,62 @@
/*!
* @license
* Copyright 2016 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Injectable, Inject } from '@angular/core';
import { DOCUMENT } from '@angular/platform-browser';
import { LogService } from '../services/log.service';
import { NotificationService } from '../services/notification.service';
@Injectable()
export class ClipboardService {
constructor(
@Inject(DOCUMENT) private document: any,
private logService: LogService,
private notificationService: NotificationService) {}
isTargetValid(target: HTMLInputElement | HTMLTextAreaElement) {
if (target instanceof HTMLInputElement || target instanceof HTMLTextAreaElement) {
if (target.hasAttribute('disabled')) {
return false;
}
return true;
}
this.logService.error(`${target} should be input or textarea`);
return false;
}
copyToClipboard(target: HTMLInputElement | HTMLTextAreaElement, message?: string) {
if (this.isTargetValid(target)) {
try {
target.select();
target.setSelectionRange(0, target.value.length);
this.document.execCommand('copy');
this.notify(message);
} catch (error) {
this.logService.error(error);
}
}
}
private notify(message) {
if (message) {
this.notificationService.openSnackMessage(message);
}
}
}

View File

@ -0,0 +1,18 @@
/*!
* @license
* Copyright 2016 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export * from './public-api';

View File

@ -0,0 +1,22 @@
/*!
* @license
* Copyright 2016 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// export * from './accordion-group.component';
export * from './clipboard.directive';
export * from './clipboard.service';
export * from './clipboard.module';

View File

@ -43,6 +43,7 @@ import { SidenavLayoutModule } from './layout/layout.module';
import { CommentsModule } from './comments/comments.module';
import { ButtonsMenuModule } from './buttons-menu/buttons-menu.module';
import { TemplateModule } from './templates/template.module';
import { ClipboardModule } from './clipboard/clipboard.module';
import { DirectiveModule } from './directives/directive.module';
import { PipeModule } from './pipes/pipe.module';
@ -166,6 +167,7 @@ export function providers() {
PipeModule,
CommonModule,
DirectiveModule,
ClipboardModule,
FormsModule,
ReactiveFormsModule,
HttpClientModule,
@ -203,6 +205,7 @@ export function providers() {
PipeModule,
CommonModule,
DirectiveModule,
ClipboardModule,
FormsModule,
ReactiveFormsModule,
HttpClientModule,
@ -276,6 +279,7 @@ export class CoreModuleLazy {
PipeModule,
CommonModule,
DirectiveModule,
ClipboardModule,
FormsModule,
ReactiveFormsModule,
HttpClientModule,

View File

@ -39,6 +39,7 @@ export * from './templates/index';
export * from './pipes/index';
export * from './services/index';
export * from './directives/index';
export * from './clipboard/index';
export * from './utils/index';
export * from './interface/index';