]>
Commit | Line | Data |
---|---|---|
1 | import { Subject } from 'rxjs' | |
2 | import { Component, OnInit } from '@angular/core' | |
3 | import { ActivatedRoute, Router } from '@angular/router' | |
4 | import { PluginApiService } from '@app/+admin/plugins/shared/plugin-api.service' | |
5 | import { ComponentPagination, ConfirmService, hasMoreItems, Notifier } from '@app/core' | |
6 | import { PluginService } from '@app/core/plugins/plugin.service' | |
7 | import { compareSemVer } from '@shared/core-utils/miscs/miscs' | |
8 | import { PeerTubePlugin } from '@shared/models/plugins/peertube-plugin.model' | |
9 | import { PluginType } from '@shared/models/plugins/plugin.type' | |
10 | ||
11 | @Component({ | |
12 | selector: 'my-plugin-list-installed', | |
13 | templateUrl: './plugin-list-installed.component.html', | |
14 | styleUrls: [ | |
15 | '../shared/toggle-plugin-type.scss', | |
16 | '../shared/plugin-list.component.scss', | |
17 | './plugin-list-installed.component.scss' | |
18 | ] | |
19 | }) | |
20 | export class PluginListInstalledComponent implements OnInit { | |
21 | pluginTypeOptions: { label: string, value: PluginType }[] = [] | |
22 | pluginType: PluginType = PluginType.PLUGIN | |
23 | ||
24 | pagination: ComponentPagination = { | |
25 | currentPage: 1, | |
26 | itemsPerPage: 10, | |
27 | totalItems: null | |
28 | } | |
29 | sort = 'name' | |
30 | ||
31 | plugins: PeerTubePlugin[] = [] | |
32 | updating: { [name: string]: boolean } = {} | |
33 | ||
34 | PluginType = PluginType | |
35 | ||
36 | onDataSubject = new Subject<any[]>() | |
37 | ||
38 | constructor ( | |
39 | private pluginService: PluginService, | |
40 | private pluginApiService: PluginApiService, | |
41 | private notifier: Notifier, | |
42 | private confirmService: ConfirmService, | |
43 | private router: Router, | |
44 | private route: ActivatedRoute | |
45 | ) { | |
46 | this.pluginTypeOptions = this.pluginApiService.getPluginTypeOptions() | |
47 | } | |
48 | ||
49 | ngOnInit () { | |
50 | const query = this.route.snapshot.queryParams | |
51 | if (query['pluginType']) this.pluginType = parseInt(query['pluginType'], 10) | |
52 | ||
53 | this.reloadPlugins() | |
54 | } | |
55 | ||
56 | reloadPlugins () { | |
57 | this.pagination.currentPage = 1 | |
58 | this.plugins = [] | |
59 | ||
60 | this.router.navigate([], { queryParams: { pluginType: this.pluginType } }) | |
61 | ||
62 | this.loadMorePlugins() | |
63 | } | |
64 | ||
65 | loadMorePlugins () { | |
66 | this.pluginApiService.getPlugins(this.pluginType, this.pagination, this.sort) | |
67 | .subscribe( | |
68 | res => { | |
69 | this.plugins = this.plugins.concat(res.data) | |
70 | this.pagination.totalItems = res.total | |
71 | ||
72 | this.onDataSubject.next(res.data) | |
73 | }, | |
74 | ||
75 | err => this.notifier.error(err.message) | |
76 | ) | |
77 | } | |
78 | ||
79 | onNearOfBottom () { | |
80 | if (!hasMoreItems(this.pagination)) return | |
81 | ||
82 | this.pagination.currentPage += 1 | |
83 | ||
84 | this.loadMorePlugins() | |
85 | } | |
86 | ||
87 | getNoResultMessage () { | |
88 | if (this.pluginType === PluginType.PLUGIN) { | |
89 | return $localize`You don't have plugins installed yet.` | |
90 | } | |
91 | ||
92 | return $localize`You don't have themes installed yet.` | |
93 | } | |
94 | ||
95 | isUpdateAvailable (plugin: PeerTubePlugin) { | |
96 | return plugin.latestVersion && compareSemVer(plugin.latestVersion, plugin.version) > 0 | |
97 | } | |
98 | ||
99 | getUpdateLabel (plugin: PeerTubePlugin) { | |
100 | return $localize`Update to ${plugin.latestVersion}` | |
101 | } | |
102 | ||
103 | isUpdating (plugin: PeerTubePlugin) { | |
104 | return !!this.updating[this.getUpdatingKey(plugin)] | |
105 | } | |
106 | ||
107 | async uninstall (plugin: PeerTubePlugin) { | |
108 | const res = await this.confirmService.confirm( | |
109 | $localize`Do you really want to uninstall ${plugin.name}?`, | |
110 | $localize`Uninstall` | |
111 | ) | |
112 | if (res === false) return | |
113 | ||
114 | this.pluginApiService.uninstall(plugin.name, plugin.type) | |
115 | .subscribe( | |
116 | () => { | |
117 | this.notifier.success($localize`${plugin.name} uninstalled.`) | |
118 | ||
119 | this.plugins = this.plugins.filter(p => p.name !== plugin.name) | |
120 | this.pagination.totalItems-- | |
121 | }, | |
122 | ||
123 | err => this.notifier.error(err.message) | |
124 | ) | |
125 | } | |
126 | ||
127 | async update (plugin: PeerTubePlugin) { | |
128 | const updatingKey = this.getUpdatingKey(plugin) | |
129 | if (this.updating[updatingKey]) return | |
130 | ||
131 | this.updating[updatingKey] = true | |
132 | ||
133 | this.pluginApiService.update(plugin.name, plugin.type) | |
134 | .pipe() | |
135 | .subscribe( | |
136 | res => { | |
137 | this.updating[updatingKey] = false | |
138 | ||
139 | this.notifier.success($localize`${plugin.name} updated.`) | |
140 | ||
141 | Object.assign(plugin, res) | |
142 | }, | |
143 | ||
144 | err => this.notifier.error(err.message) | |
145 | ) | |
146 | } | |
147 | ||
148 | getShowRouterLink (plugin: PeerTubePlugin) { | |
149 | return [ '/admin', 'plugins', 'show', this.pluginService.nameToNpmName(plugin.name, plugin.type) ] | |
150 | } | |
151 | ||
152 | getPluginOrThemeHref (name: string) { | |
153 | return this.pluginApiService.getPluginOrThemeHref(this.pluginType, name) | |
154 | } | |
155 | ||
156 | private getUpdatingKey (plugin: PeerTubePlugin) { | |
157 | return plugin.name + plugin.type | |
158 | } | |
159 | } |