2017-03-26 13:32:29 -07:00
|
|
|
/* tslint:disable component-selector */
|
|
|
|
|
import { Component, ElementRef, OnInit } from '@angular/core';
|
2017-05-02 19:17:05 -07:00
|
|
|
import { getBoolFromAttribute } from 'app/shared/attribute-utils';
|
2017-03-26 13:32:29 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* An embeddable code block that displays nicely formatted code.
|
|
|
|
|
* Example usage:
|
|
|
|
|
*
|
|
|
|
|
* ```
|
|
|
|
|
* <code-example language="ts" linenums="2" class="special" title="Do Stuff">
|
|
|
|
|
* // a code block
|
|
|
|
|
* console.log('do stuff');
|
|
|
|
|
* </code-example>
|
|
|
|
|
* ```
|
|
|
|
|
*/
|
|
|
|
|
@Component({
|
|
|
|
|
selector: 'code-example',
|
|
|
|
|
template: `
|
|
|
|
|
<header *ngIf="title">{{title}}</header>
|
2017-04-27 22:57:34 -07:00
|
|
|
<aio-code [ngClass]="{'headed-code':title, 'simple-code':!title}" [code]="code"
|
2017-05-02 11:57:26 +01:00
|
|
|
[language]="language" [linenums]="linenums" [path]="path" [region]="region" [hideCopy]="hideCopy"></aio-code>
|
2017-03-26 13:32:29 -07:00
|
|
|
`
|
|
|
|
|
})
|
2017-04-27 15:32:46 -07:00
|
|
|
export class CodeExampleComponent implements OnInit {
|
2017-03-26 13:32:29 -07:00
|
|
|
|
|
|
|
|
code: string;
|
|
|
|
|
language: string;
|
|
|
|
|
linenums: boolean | number;
|
2017-04-27 22:57:34 -07:00
|
|
|
path: string;
|
|
|
|
|
region: string;
|
2017-03-26 13:32:29 -07:00
|
|
|
title: string;
|
2017-05-02 11:57:26 +01:00
|
|
|
hideCopy: boolean;
|
2017-03-26 13:32:29 -07:00
|
|
|
|
|
|
|
|
constructor(private elementRef: ElementRef) {
|
|
|
|
|
const element = this.elementRef.nativeElement;
|
2017-04-27 22:57:34 -07:00
|
|
|
|
2017-03-26 13:32:29 -07:00
|
|
|
this.language = element.getAttribute('language') || '';
|
|
|
|
|
this.linenums = element.getAttribute('linenums');
|
2017-04-27 22:57:34 -07:00
|
|
|
this.path = element.getAttribute('path') || '';
|
|
|
|
|
this.region = element.getAttribute('region') || '';
|
2017-03-26 13:32:29 -07:00
|
|
|
this.title = element.getAttribute('title') || '';
|
2017-05-02 19:17:05 -07:00
|
|
|
this.hideCopy = getBoolFromAttribute(element, ['hidecopy', 'hide-copy']);
|
2017-03-26 13:32:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
|
// The `codeExampleContent` property is set by the DocViewer when it builds this component.
|
|
|
|
|
// It is the original innerHTML of the host element.
|
|
|
|
|
this.code = this.elementRef.nativeElement.codeExampleContent;
|
|
|
|
|
}
|
|
|
|
|
}
|