aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/shared/angular
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2019-04-04 10:44:18 +0200
committerChocobozzz <me@florianbigard.com>2019-04-05 10:53:08 +0200
commit693263e936763a851e3c8c020e3739def8bd4eca (patch)
tree7fd333fcf76edbc24f3daf4a78e47ff55f048b04 /client/src/app/shared/angular
parent9ba1d64b1ac77304d9ffb1b3432a90ea00ff3281 (diff)
downloadPeerTube-693263e936763a851e3c8c020e3739def8bd4eca.tar.gz
PeerTube-693263e936763a851e3c8c020e3739def8bd4eca.tar.zst
PeerTube-693263e936763a851e3c8c020e3739def8bd4eca.zip
Refactor videos selection components
Diffstat (limited to 'client/src/app/shared/angular')
-rw-r--r--client/src/app/shared/angular/from-now.pipe.ts40
-rw-r--r--client/src/app/shared/angular/number-formatter.pipe.ts19
-rw-r--r--client/src/app/shared/angular/object-length.pipe.ts8
-rw-r--r--client/src/app/shared/angular/peertube-template.directive.ts12
4 files changed, 79 insertions, 0 deletions
diff --git a/client/src/app/shared/angular/from-now.pipe.ts b/client/src/app/shared/angular/from-now.pipe.ts
new file mode 100644
index 000000000..3a9a76411
--- /dev/null
+++ b/client/src/app/shared/angular/from-now.pipe.ts
@@ -0,0 +1,40 @@
1import { Pipe, PipeTransform } from '@angular/core'
2import { I18n } from '@ngx-translate/i18n-polyfill'
3
4// Thanks: https://stackoverflow.com/questions/3177836/how-to-format-time-since-xxx-e-g-4-minutes-ago-similar-to-stack-exchange-site
5@Pipe({ name: 'myFromNow' })
6export class FromNowPipe implements PipeTransform {
7
8 constructor (private i18n: I18n) { }
9
10 transform (arg: number | Date | string) {
11 const argDate = new Date(arg)
12 const seconds = Math.floor((Date.now() - argDate.getTime()) / 1000)
13
14 let interval = Math.floor(seconds / 31536000)
15 if (interval > 1) {
16 return this.i18n('{{interval}} years ago', { interval })
17 }
18
19 interval = Math.floor(seconds / 2592000)
20 if (interval > 1) return this.i18n('{{interval}} months ago', { interval })
21 if (interval === 1) return this.i18n('{{interval}} month ago', { interval })
22
23 interval = Math.floor(seconds / 604800)
24 if (interval > 1) return this.i18n('{{interval}} weeks ago', { interval })
25 if (interval === 1) return this.i18n('{{interval}} week ago', { interval })
26
27 interval = Math.floor(seconds / 86400)
28 if (interval > 1) return this.i18n('{{interval}} days ago', { interval })
29 if (interval === 1) return this.i18n('{{interval}} day ago', { interval })
30
31 interval = Math.floor(seconds / 3600)
32 if (interval > 1) return this.i18n('{{interval}} hours ago', { interval })
33 if (interval === 1) return this.i18n('{{interval}} hour ago', { interval })
34
35 interval = Math.floor(seconds / 60)
36 if (interval >= 1) return this.i18n('{{interval}} min ago', { interval })
37
38 return this.i18n('{{interval}} sec ago', { interval: Math.max(0, seconds) })
39 }
40}
diff --git a/client/src/app/shared/angular/number-formatter.pipe.ts b/client/src/app/shared/angular/number-formatter.pipe.ts
new file mode 100644
index 000000000..8a0756a36
--- /dev/null
+++ b/client/src/app/shared/angular/number-formatter.pipe.ts
@@ -0,0 +1,19 @@
1import { Pipe, PipeTransform } from '@angular/core'
2
3// Thanks: https://github.com/danrevah/ngx-pipes/blob/master/src/pipes/math/bytes.ts
4
5@Pipe({ name: 'myNumberFormatter' })
6export class NumberFormatterPipe implements PipeTransform {
7 private dictionary: Array<{max: number, type: string}> = [
8 { max: 1000, type: '' },
9 { max: 1000000, type: 'K' },
10 { max: 1000000000, type: 'M' }
11 ]
12
13 transform (value: number) {
14 const format = this.dictionary.find(d => value < d.max) || this.dictionary[this.dictionary.length - 1]
15 const calc = Math.floor(value / (format.max / 1000))
16
17 return `${calc}${format.type}`
18 }
19}
diff --git a/client/src/app/shared/angular/object-length.pipe.ts b/client/src/app/shared/angular/object-length.pipe.ts
new file mode 100644
index 000000000..84d182052
--- /dev/null
+++ b/client/src/app/shared/angular/object-length.pipe.ts
@@ -0,0 +1,8 @@
1import { Pipe, PipeTransform } from '@angular/core'
2
3@Pipe({ name: 'myObjectLength' })
4export class ObjectLengthPipe implements PipeTransform {
5 transform (value: Object) {
6 return Object.keys(value).length
7 }
8}
diff --git a/client/src/app/shared/angular/peertube-template.directive.ts b/client/src/app/shared/angular/peertube-template.directive.ts
new file mode 100644
index 000000000..a514b6057
--- /dev/null
+++ b/client/src/app/shared/angular/peertube-template.directive.ts
@@ -0,0 +1,12 @@
1import { Directive, Input, TemplateRef } from '@angular/core'
2
3@Directive({
4 selector: '[ptTemplate]'
5})
6export class PeerTubeTemplateDirective {
7 @Input('ptTemplate') name: string
8
9 constructor (public template: TemplateRef<any>) {
10 // empty
11 }
12}