Skip to content

Commit

Permalink
Merge pull request #588 from mobi/feature_ability_to_copy_doc_permalinks
Browse files Browse the repository at this point in the history
Add directive for use in adding ability to copy permalinks to doc examples
  • Loading branch information
jaredami authored Jun 16, 2020
2 parents 07740ae + 69fd65c commit ab32b9b
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { Component, ElementRef } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { GoCopyDocLinkDirective } from './go-copy-doc-link.directive';
import { GoCopyComponent } from './go-copy.component';
import { GoCopyModule } from './go-copy.module';
import { GoIconModule } from '../go-icon/go-icon.module';

@Component({
template: `<go-copy cardId="testId" goCopyDocLink></go-copy>`
})
class TestParentComponent {}

describe('GoCopyDocLinkDirective', () => {
let fixture: ComponentFixture<TestParentComponent>;
let elementRef: ElementRef;
let goCopyComponent: GoCopyComponent;
let directive: GoCopyDocLinkDirective;

beforeEach(() => {
TestBed.configureTestingModule({
declarations: [
TestParentComponent
],
imports: [
GoCopyModule,
GoIconModule
]
});

fixture = TestBed.createComponent(TestParentComponent);
elementRef = fixture.elementRef;
goCopyComponent = TestBed.createComponent(GoCopyComponent).componentInstance;
directive = new GoCopyDocLinkDirective(elementRef, goCopyComponent);
});

it('creates an instance', () => {
expect(directive).toBeTruthy();
});

describe('ngOnInit', () => {
it('adds a class of go-copy--card-header and sets the title of the element', () => {
directive.ngOnInit();
expect(elementRef.nativeElement.classList).toContain('go-copy--card-header');
expect(elementRef.nativeElement.title).toBe('Copy the URL to this card');
});


it('sets the text of the element with the current url and an id selector', () => {
directive.cardId = 'testId';
directive.ngOnInit();
expect(goCopyComponent.text).toBe('http://localhost:9876/context.html/#testId');
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Directive, ElementRef, Host, Input, OnInit } from '@angular/core';
import { GoCopyComponent } from './go-copy.component';

@Directive({
selector: '[goCopyDocLink]'
})
export class GoCopyDocLinkDirective implements OnInit {

@Input() cardId: string;

constructor(
private elementRef: ElementRef,
@Host() private baseComponent: GoCopyComponent
) { }

ngOnInit(): void {
this.elementRef.nativeElement.classList.add('go-copy--card-header');
this.elementRef.nativeElement.title = 'Copy the URL to this card';
this.baseComponent.text = `${window.location.href}/#${this.cardId}`;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
position: relative;
}

:host.go-copy--card-header {
position: absolute;
margin-left: 0.75rem;
padding-top: 0.25rem;
}

.go-copy__textarea {
left: 99999px;
position: absolute;
Expand Down
7 changes: 5 additions & 2 deletions projects/go-lib/src/lib/components/go-copy/go-copy.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@ import { CommonModule } from '@angular/common';

import { GoCopyComponent } from './go-copy.component';
import { GoIconModule } from '../go-icon/go-icon.module';
import { GoCopyDocLinkDirective } from './go-copy-doc-link.directive';

@NgModule({
declarations: [
GoCopyComponent
GoCopyComponent,
GoCopyDocLinkDirective
],
imports: [
CommonModule,
GoIconModule
],
exports: [
GoCopyComponent
GoCopyComponent,
GoCopyDocLinkDirective
]
})

Expand Down
4 changes: 4 additions & 0 deletions projects/go-lib/src/styles/_typography.scss
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ strong {
@extend %element--no-margin;
}

.go-heading--inline {
display: inline;
}

.go-link {
@include transition(all);

Expand Down

0 comments on commit ab32b9b

Please sign in to comment.