Add ability to update icons content
authorChocobozzz <me@florianbigard.com>
Thu, 5 Dec 2019 16:26:58 +0000 (17:26 +0100)
committerChocobozzz <me@florianbigard.com>
Thu, 5 Dec 2019 16:26:58 +0000 (17:26 +0100)
client/src/app/core/plugins/hooks.service.ts
client/src/app/shared/images/global-icon.component.ts
client/src/app/videos/+video-watch/video-watch.component.ts
shared/models/plugins/client-hook.model.ts

index 93dac1167cdecb8d6ee009089a8b5ea99c0652a8..24274183129ec55c5b5f26ab09ec624edc6a6bb3 100644 (file)
@@ -16,13 +16,10 @@ export class HooksService {
     private pluginService: PluginService
   ) { }
 
-  wrapObject<T, U extends ClientFilterHookName> (result: T, hookName: U) {
-    return this.pluginService.runHook(hookName, result)
-  }
-
   wrapObsFun
     <P, R, H1 extends ClientFilterHookName, H2 extends ClientFilterHookName>
-    (fun: ObservableFunction<P, R>, params: P, scope: PluginClientScope, hookParamName: H1, hookResultName: H2) {
+    (fun: ObservableFunction<P, R>, params: P, scope: PluginClientScope, hookParamName: H1, hookResultName: H2)
+  {
     return from(this.pluginService.ensurePluginsAreLoaded(scope))
       .pipe(
         mergeMap(() => this.wrapObject(params, hookParamName)),
@@ -31,10 +28,16 @@ export class HooksService {
       )
   }
 
-  async wrapFun<U, T, V extends ClientFilterHookName> (fun: RawFunction<U, T>, params: U, hookName: V) {
-    const result = fun(params)
+  async wrapFun
+    <P, R, H1 extends ClientFilterHookName, H2 extends ClientFilterHookName>
+    (fun: RawFunction<P, R>, params: P, scope: PluginClientScope, hookParamName: H1, hookResultName: H2)
+  {
+    await this.pluginService.ensurePluginsAreLoaded(scope)
+
+    const newParams = await this.wrapObject(params, hookParamName)
+    const result = fun(newParams)
 
-    return this.pluginService.runHook(hookName, result, params)
+    return this.pluginService.runHook(hookResultName, result, params)
   }
 
   runAction<T, U extends ClientActionHookName> (hookName: U, scope: PluginClientScope, params?: T) {
@@ -42,4 +45,8 @@ export class HooksService {
         .then(() => this.pluginService.runHook(hookName, undefined, params))
         .catch((err: any) => console.error('Fatal hook error.', { err }))
   }
+
+  private wrapObject<T, U extends ClientFilterHookName> (result: T, hookName: U) {
+    return this.pluginService.runHook(hookName, result)
+  }
 }
index a13b7d8e05bb00bf3259e63e5d4b7cbbebba754b..eb723db94a4da24dea7e40789a7d67513a30a26f 100644 (file)
@@ -1,4 +1,5 @@
 import { ChangeDetectionStrategy, Component, ElementRef, Input, OnInit } from '@angular/core'
+import { HooksService } from '@app/core/plugins/hooks.service'
 
 const icons = {
   'add': require('!!raw-loader?!../../../assets/images/global/add.svg'),
@@ -60,11 +61,24 @@ export type GlobalIconName = keyof typeof icons
 export class GlobalIconComponent implements OnInit {
   @Input() iconName: GlobalIconName
 
-  constructor (private el: ElementRef) {}
+  constructor (
+    private el: ElementRef,
+    private hooks: HooksService
+  ) { }
 
-  ngOnInit () {
+  async ngOnInit () {
     const nativeElement = this.el.nativeElement
 
-    nativeElement.innerHTML = icons[this.iconName]
+    nativeElement.innerHTML = await this.hooks.wrapFun(
+      this.getSVGContent.bind(this),
+      { name: this.iconName },
+      'common',
+      'filter:internal.common.svg-icons.get-content.params',
+      'filter:internal.common.svg-icons.get-content.result'
+    )
+  }
+
+  private getSVGContent (options: { name: string }) {
+    return icons[options.name]
   }
 }
index 523865feffc3299ea637dfa9e06f6d706c1bd85e..adf6dc12f14d1bba4c2cf4ce0babb164375b6ac0 100644 (file)
@@ -418,6 +418,8 @@ export class VideoWatchComponent implements OnInit, OnDestroy {
     const { playerMode, playerOptions } = await this.hooks.wrapFun(
       this.buildPlayerManagerOptions.bind(this),
       params,
+      'video-watch',
+      'filter:internal.video-watch.player.build-options.params',
       'filter:internal.video-watch.player.build-options.result'
     )
 
index f0cdb8b1930392ae916333d18a2d7fa5dbfa3fb1..ecbe8bd3c8e1e92e35d7efca5e50021e3dd0fc72 100644 (file)
@@ -45,7 +45,12 @@ export const clientFilterHookObject = {
   'filter:api.signup.registration.create.params': true,
 
   // Filter the options to create our player
-  'filter:internal.video-watch.player.build-options.result': true
+  'filter:internal.video-watch.player.build-options.params': true,
+  'filter:internal.video-watch.player.build-options.result': true,
+
+  // Filter our SVG icons content
+  'filter:internal.common.svg-icons.get-content.params': true,
+  'filter:internal.common.svg-icons.get-content.result': true
 }
 
 export type ClientFilterHookName = keyof typeof clientFilterHookObject