/* tslint:disable component-selector */ import { Component, AfterViewInit, ViewChild, Input, ViewChildren, QueryList, OnInit } from '@angular/core'; import { CodeComponent } from './code.component'; export interface TabInfo { class: string|null; code: string; language: string|null; linenums: any; path: string; region: string; title: string|null; } /** * Renders a set of tab group of code snippets. * * The innerHTML of the `` component should contain `` elements. * Each `` has the same interface as the embedded `` component. * The optional `linenums` attribute is the default `linenums` for each code pane. */ @Component({ selector: 'code-tabs', template: `
{{ tab.title }} `, }) export class CodeTabsComponent implements OnInit, AfterViewInit { tabs: TabInfo[]; @Input('linenums') linenums: string; @ViewChild('content') content; @ViewChildren(CodeComponent) codeComponents: QueryList; ngOnInit() { this.tabs = []; const codeExamples = this.content.nativeElement.querySelectorAll('code-pane'); for (let i = 0; i < codeExamples.length; i++) { const tabContent = codeExamples[i]; this.tabs.push(this.getTabInfo(tabContent)); } } ngAfterViewInit() { this.codeComponents.toArray().forEach((codeComponent, i) => { codeComponent.code = this.tabs[i].code; }); } /** Gets the extracted TabInfo data from the provided code-pane element. */ private getTabInfo(tabContent: HTMLElement): TabInfo { return { class: tabContent.getAttribute('class'), code: tabContent.innerHTML, language: tabContent.getAttribute('language'), linenums: tabContent.getAttribute('linenums') || this.linenums, path: tabContent.getAttribute('path') || '', region: tabContent.getAttribute('region') || '', title: tabContent.getAttribute('title') }; } }