Skip to content

Commit 6a3c648

Browse files
author
Petr Kachanovsky
committed
feat: add option to inject custom component on every page
1 parent 872a38d commit 6a3c648

File tree

6 files changed

+58
-4
lines changed

6 files changed

+58
-4
lines changed

adminforth/documentation/docs/tutorial/03-Customization/08-pageInjections.md

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,4 +430,48 @@ function openCustomPage() {
430430
Also there are:
431431
432432
* `config.customization.globalInjections.header`
433-
* `config.customization.globalInjections.sidebar`
433+
* `config.customization.globalInjections.sidebar`
434+
* `config.customization.globalInjections.everyPageBottom`
435+
436+
Unlike `userMenu`, `header` and `sidebar` injections, `everyPageBottom` will be added to the bottom of every page even when user is not logged in.
437+
You can use it to execute some piece of code when any page is loaded. For example, you can add welcoming pop up when user visits a page.
438+
439+
```ts title="/index.ts"
440+
{
441+
...
442+
customization: {
443+
globalInjections: {
444+
userMenu: [
445+
'@@/CustomUserMenuItem.vue',
446+
//diff-remove
447+
]
448+
//diff-add
449+
],
450+
//diff-add
451+
everyPageBottom: [
452+
//diff-add
453+
'@@/AnyPageWelcome.vue',
454+
//diff-add
455+
]
456+
}
457+
}
458+
...
459+
}
460+
```
461+
462+
Now create file `AnyPageWelcome.vue` in the `custom` folder of your project:
463+
464+
```html title="./custom/AnyPageWelcome.vue"
465+
<template></template>
466+
467+
<script setup>
468+
import { onMounted } from 'vue';
469+
import adminforth from '@/adminforth';
470+
onMounted(() => {
471+
adminforth.alert({
472+
message: 'Welcome!',
473+
variant: 'success',
474+
});
475+
});
476+
</script>
477+
```

adminforth/modules/configValidator.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,11 @@ export default class ConfigValidator implements IConfigValidator {
131131
userMenu: [],
132132
header: [],
133133
sidebar: [],
134+
everyPageBottom: [],
134135
};
135136

136137
if (this.inputConfig.customization?.globalInjections) {
137-
const ALLOWED_GLOBAL_INJECTIONS = ['userMenu', 'header', 'sidebar']
138+
const ALLOWED_GLOBAL_INJECTIONS = ['userMenu', 'header', 'sidebar', 'everyPageBottom'];
138139
Object.keys(this.inputConfig.customization.globalInjections).forEach((injection) => {
139140
if (ALLOWED_GLOBAL_INJECTIONS.includes(injection)) {
140141
globalInjections[injection] = this.validateAndListifyInjectionNew(this.inputConfig.customization.globalInjections, injection, errors);

adminforth/modules/restApi.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,9 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
220220
demoCredentials: this.adminforth.config.auth.demoCredentials,
221221
loginPromptHTML: await tr(this.adminforth.config.auth.loginPromptHTML, 'system.loginPromptHTML'),
222222
loginPageInjections: this.adminforth.config.customization.loginPageInjections,
223+
globalInjections: {
224+
everyPageBottom: this.adminforth.config.customization.globalInjections.everyPageBottom,
225+
},
223226
rememberMeDays: this.adminforth.config.auth.rememberMeDays,
224227
};
225228
},

adminforth/spa/src/App.vue

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,12 @@
209209
drawer-backdrop="" class="bg-gray-900/50 dark:bg-gray-900/80 fixed inset-0 z-20">
210210
</div>
211211

212-
212+
<component
213+
v-for="c in coreStore?.config?.globalInjections?.everyPageBottom || []"
214+
:is="getCustomComponent(c)"
215+
:meta="c.meta"
216+
/>
213217
</div>
214-
215218
</template>
216219

217220
<style lang="scss" scoped>

adminforth/types/Back.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,7 @@ interface AdminForthInputConfigCustomization {
708708
userMenu?: AdminForthComponentDeclaration | Array<AdminForthComponentDeclaration>,
709709
header?: AdminForthComponentDeclaration | Array<AdminForthComponentDeclaration>,
710710
sidebar?: AdminForthComponentDeclaration | Array<AdminForthComponentDeclaration>,
711+
everyPageBottom?: AdminForthComponentDeclaration | Array<AdminForthComponentDeclaration>,
711712
}
712713
}
713714

@@ -984,6 +985,7 @@ export interface AdminForthConfigCustomization extends Omit<AdminForthInputConfi
984985
userMenu: Array<AdminForthComponentDeclarationFull>,
985986
header: Array<AdminForthComponentDeclarationFull>,
986987
sidebar: Array<AdminForthComponentDeclarationFull>,
988+
everyPageBottom: Array<AdminForthComponentDeclarationFull>,
987989
},
988990
}
989991

adminforth/types/Common.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -966,6 +966,7 @@ export interface AdminForthConfigForFrontend {
966966
userMenu: Array<AdminForthComponentDeclarationFull>,
967967
header: Array<AdminForthComponentDeclarationFull>,
968968
sidebar: Array<AdminForthComponentDeclarationFull>,
969+
everyPageBottom: Array<AdminForthComponentDeclarationFull>,
969970
}
970971
}
971972

0 commit comments

Comments
 (0)