From afbf2b6bf5ad2739b5a3243033760dc41deac78e Mon Sep 17 00:00:00 2001 From: DAB0mB Date: Mon, 27 Feb 2017 20:01:27 -0200 Subject: [PATCH] added HistoryLink directive --- src/current-tutorial.ts | 6 ++++++ src/tutorial-code-diff.directive.ts | 2 +- src/tutorial-history.directive.ts | 31 +++++++++++++++++++++++++++++ src/tutorial-page.component.ts | 3 +++ src/tutorials-module.ts | 2 ++ 5 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 src/tutorial-history.directive.ts diff --git a/src/current-tutorial.ts b/src/current-tutorial.ts index bcd6205..5f170e1 100644 --- a/src/current-tutorial.ts +++ b/src/current-tutorial.ts @@ -5,11 +5,13 @@ export class ActivatedTutorial { public tutorial: Subject; public step: Subject; public steps: Subject; + public branch: Subject; constructor() { this.step = new Subject(); this.tutorial = new Subject(); this.steps = new Subject(); + this.branch = new Subject(); } updateCurrentTutorial(tutorial: TutorialDefinition) { @@ -23,4 +25,8 @@ export class ActivatedTutorial { updateCurrentSteps(steps: TutorialStep[]) { this.steps.next(steps); } + + updateCurrentBranch(branch: string) { + this.branch.next(branch); + } } diff --git a/src/tutorial-code-diff.directive.ts b/src/tutorial-code-diff.directive.ts index ce54a14..2d680e7 100644 --- a/src/tutorial-code-diff.directive.ts +++ b/src/tutorial-code-diff.directive.ts @@ -36,7 +36,7 @@ export class CodeDiffLink { } let compareEnd = 'step' + (index + 1); - let url = 'https://github.com/' + tutorial.gitHub + '/compare/' + compareStart + '...' + compareEnd; + let url = `https://github.com/${tutorial.gitHub}/compare/${compareStart}...${compareEnd}`; renderer.setElementAttribute(el.nativeElement, 'href', url); } diff --git a/src/tutorial-history.directive.ts b/src/tutorial-history.directive.ts new file mode 100644 index 0000000..6b01a07 --- /dev/null +++ b/src/tutorial-history.directive.ts @@ -0,0 +1,31 @@ +import {Injectable, Directive, ElementRef, Renderer} from '@angular/core'; +import {ActivatedTutorial} from './current-tutorial'; +import {TutorialDefinition} from './tutorial-definition'; +import {Observable} from 'rxjs'; + +@Directive({ + selector: '[historyLink]' +}) +@Injectable() +export class HistoryLink { + constructor(private activated: ActivatedTutorial, el: ElementRef, renderer: Renderer) { + Observable.zip(activated.tutorial, activated.branch, (tutorial, branch) => { + return { + tutorial, + branch + }; + }).subscribe((data) => { + let tutorial: TutorialDefinition = data.tutorial; + let branch: string = data.branch; + + let url = `https://github.com/${tutorial.gitHub}/tree/${branch}-history`; + + renderer.setElementAttribute(el.nativeElement, 'href', url); + }); + } + + pad(n, width, z = '0') { + n = n + ''; + return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n; + } +} diff --git a/src/tutorial-page.component.ts b/src/tutorial-page.component.ts index 9cf38ef..2e70e4e 100644 --- a/src/tutorial-page.component.ts +++ b/src/tutorial-page.component.ts @@ -23,6 +23,7 @@ export class TutorialPage implements OnInit { private tutorial: TutorialDefinition; private step: TutorialStep; private steps: TutorialStep[]; + private branch: string; constructor(private route: ActivatedRoute, private compiler: Compiler, @@ -37,9 +38,11 @@ export class TutorialPage implements OnInit { this.tutorial = routeData.tutorialObject; this.step = routeData.stepObject; this.steps = routeData.steps; + this.branch = routeData.gitTagRevision; this.currentTutorial.updateCurrentTutorial(this.tutorial); this.currentTutorial.updateCurrentStep(this.step); this.currentTutorial.updateCurrentSteps(this.steps); + this.currentTutorial.updateCurrentBranch(this.branch); let htmlContent = data.resolveData.step; this.seo.setSeoDescription(htmlContent); diff --git a/src/tutorials-module.ts b/src/tutorials-module.ts index a77ae0f..a080621 100644 --- a/src/tutorials-module.ts +++ b/src/tutorials-module.ts @@ -20,6 +20,7 @@ import {ApiLoadResolve} from './api-load-resolve'; import {ActivatedApi} from './current-api'; import {StepListComponent} from './steps-list.component'; import {CodeDiffLink} from './tutorial-code-diff.directive'; +import {HistoryLink} from './tutorial-history.directive'; import {StepDownloadZipLink} from './tutorial-download-zip.directive'; import {PageTitleService} from './page-title.service'; @@ -34,6 +35,7 @@ const exportsAndDeclarations = [ ApiVersionsList, ApiListItems, CodeDiffLink, + HistoryLink, StepDownloadZipLink ];