]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/shared-main/misc/help.component.ts
Use onpush strategy for dropdown
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-main / misc / help.component.ts
1 import { AfterContentInit, Component, ContentChildren, Input, OnChanges, OnInit, QueryList, TemplateRef } from '@angular/core'
2 import { GlobalIconName } from '@app/shared/shared-icons'
3 import { ENHANCED_RULES, TEXT_RULES } from '@shared/core-utils/renderer/markdown'
4 import { PeerTubeTemplateDirective } from '../angular'
5
6 @Component({
7 selector: 'my-help',
8 styleUrls: [ './help.component.scss' ],
9 templateUrl: './help.component.html'
10 })
11
12 export class HelpComponent implements OnInit, OnChanges, AfterContentInit {
13 @Input() helpType: 'custom' | 'markdownText' | 'markdownEnhanced' = 'custom'
14 @Input() tooltipPlacement = 'right auto'
15 @Input() iconName: GlobalIconName = 'help'
16 @Input() title = $localize`Get help`
17 @Input() autoClose = 'outside'
18
19 @ContentChildren(PeerTubeTemplateDirective) templates: QueryList<PeerTubeTemplateDirective<'preHtml' | 'customHtml' | 'postHtml'>>
20
21 isPopoverOpened = false
22 mainHtml = ''
23
24 preHtmlTemplate: TemplateRef<any>
25 customHtmlTemplate: TemplateRef<any>
26 postHtmlTemplate: TemplateRef<any>
27
28 ngOnInit () {
29 this.init()
30 }
31
32 ngAfterContentInit () {
33 {
34 const t = this.templates.find(t => t.name === 'preHtml')
35 if (t) this.preHtmlTemplate = t.template
36 }
37
38 {
39 const t = this.templates.find(t => t.name === 'customHtml')
40 if (t) this.customHtmlTemplate = t.template
41 }
42
43 {
44 const t = this.templates.find(t => t.name === 'postHtml')
45 if (t) this.postHtmlTemplate = t.template
46 }
47 }
48
49 ngOnChanges () {
50 this.init()
51 }
52
53 onPopoverHidden () {
54 this.isPopoverOpened = false
55 }
56
57 onPopoverShown () {
58 this.isPopoverOpened = true
59 }
60
61 private init () {
62 if (this.helpType === 'markdownText') {
63 this.mainHtml = this.formatMarkdownSupport(TEXT_RULES)
64 return
65 }
66
67 if (this.helpType === 'markdownEnhanced') {
68 this.mainHtml = this.formatMarkdownSupport(ENHANCED_RULES)
69 return
70 }
71 }
72
73 private formatMarkdownSupport (rules: string[]) {
74 /* eslint-disable max-len */
75 return $localize`<a href="https://en.wikipedia.org/wiki/Markdown#Example" target="_blank" rel="noopener noreferrer">Markdown</a> compatible that supports:` +
76 this.createMarkdownList(rules)
77 }
78
79 private createMarkdownList (rules: string[]) {
80 const rulesToText = {
81 emphasis: $localize`Emphasis`,
82 link: $localize`Links`,
83 newline: $localize`New lines`,
84 list: $localize`Lists`,
85 image: $localize`Images`
86 }
87
88 const bullets = rules.map(r => rulesToText[r])
89 .filter(text => text)
90 .map(text => '<li>' + text + '</li>')
91 .join('')
92
93 return '<ul>' + bullets + '</ul>'
94 }
95 }