-
-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(clipboard-if-supported): provide a directive for rendering eleme…
…nt if copy is supported (#182) * feat(clipboard-if-supported): provide a directive for rendering element if copy is supported provide a structural directive called ngxClipboardIfSupported that renders the host only if isCopySupported returns true
- Loading branch information
1 parent
c106a25
commit 19f17f9
Showing
5 changed files
with
87 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
projects/ngx-clipboard/src/lib/ngx-clipboard-if-supported.directive.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { Component } from '@angular/core'; | ||
import { async, ComponentFixture, TestBed } from '@angular/core/testing'; | ||
import { By } from '@angular/platform-browser'; | ||
|
||
import { ClipboardModule } from './ngx-clipboard.module'; | ||
import { ClipboardService } from './ngx-clipboard.service'; | ||
|
||
@Component({ | ||
// tslint:disable-next-line:component-selector | ||
selector: 'test-cmp', | ||
template: ` | ||
<span | ||
*ngxClipboardIfSupported | ||
ngxClipboard | ||
cbContent="Foo Bar"> | ||
Copy Foo Bar | ||
</span>` | ||
}) | ||
class TestComponent { | ||
} | ||
|
||
function createTestComponent(): ComponentFixture<TestComponent> { | ||
return TestBed.createComponent(TestComponent); | ||
} | ||
|
||
describe('ngxClipboardIfSupported directive', () => { | ||
let fixture: ComponentFixture<TestComponent>; | ||
let clipboardService: ClipboardService; | ||
let spy: jasmine.Spy; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
declarations: [TestComponent], | ||
imports: [ClipboardModule], | ||
}); | ||
|
||
clipboardService = TestBed.get(ClipboardService); | ||
fixture = createTestComponent(); | ||
spy = spyOnProperty(clipboardService, 'isSupported', 'get'); | ||
}); | ||
|
||
it('should not render host when copy is not supported', async(() => { | ||
spy.and.returnValue(false); | ||
fixture.detectChanges(); | ||
expect(fixture.debugElement.queryAll(By.css('span')).length).toEqual(0); | ||
})); | ||
|
||
it('should render host when copy is supported', async(() => { | ||
spy.and.returnValue(true); | ||
fixture.detectChanges(); | ||
expect(fixture.debugElement.queryAll(By.css('span')).length).toEqual(1); | ||
})); | ||
}); |
21 changes: 21 additions & 0 deletions
21
projects/ngx-clipboard/src/lib/ngx-clipboard-if-supported.directive.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Directive, OnChanges, TemplateRef, ViewContainerRef, OnInit } from '@angular/core'; | ||
|
||
import { ClipboardService } from './ngx-clipboard.service'; | ||
|
||
@Directive({ | ||
// tslint:disable-next-line:directive-selector | ||
selector: '[ngxClipboardIfSupported]', | ||
}) | ||
export class ClipboardIfSupportedDirective implements OnInit { | ||
constructor( | ||
private _clipboardService: ClipboardService, | ||
private _viewContainerRef: ViewContainerRef, | ||
private _templateRef: TemplateRef<any> | ||
) {} | ||
|
||
ngOnInit() { | ||
if (this._clipboardService.isSupported) { | ||
this._viewContainerRef.createEmbeddedView(this._templateRef); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters