-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a329c32
commit 29a9cbc
Showing
8 changed files
with
82 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./usePermissions" | ||
export * from "./useTranslations" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { ref } from 'vue' | ||
|
||
const _permissions = ref<string[]>([]); | ||
const _setted = false; | ||
|
||
export function usePermissions() { | ||
|
||
const some = (...permissionCodes: string[]) => { | ||
if (!_setted) { | ||
console.warn("Permissions not setted yet"); | ||
} | ||
return _permissions.value.some(p => permissionCodes.includes(p)); | ||
} | ||
|
||
const every = (...permissionCodes: string[]) => { | ||
if (!_setted) { | ||
console.warn("Permissions not setted yet"); | ||
} | ||
return permissionCodes.every(p => _permissions.value.includes(p)); | ||
} | ||
|
||
const has = (permissionCode: string) => { | ||
if (!_setted) { | ||
console.warn("Permissions not setted yet"); | ||
} | ||
return _permissions.value.includes(permissionCode); | ||
} | ||
|
||
const set = (permissions: string[]) => { | ||
_permissions.value = permissions; | ||
} | ||
|
||
return { | ||
some, | ||
every, | ||
has, | ||
set | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { ref } from 'vue' | ||
|
||
const _translations = ref<{ code: string, value: string }[]>([]); | ||
|
||
export function useTranslations() { | ||
|
||
const $tr = (code: string, defaultValue: string, ...parameters: (string | number)[]): string => { | ||
let translation = _translations.value.find(t => t.code === code)?.value ?? defaultValue; | ||
if (translation && parameters.length) { | ||
for (let p of parameters) { | ||
translation = translation.replace(`{${parameters.indexOf(p)}}`, p.toString()); | ||
} | ||
} | ||
return translation; | ||
}; | ||
|
||
const set = (translations: { code: string, value: string }[]) => { | ||
_translations.value = translations; | ||
} | ||
|
||
return { | ||
$tr, | ||
set | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,14 @@ | ||
import { Plugin } from "vue"; | ||
|
||
export const PermissionPlugin: Plugin<PermissionOptions> = { | ||
install: (app, options) => { | ||
if (!options || !options.permissionsProvider) { | ||
console.warn("Permission won't work without permissionsProvider") | ||
return; | ||
} | ||
import { usePermissions } from '../composables/usePermissions'; | ||
|
||
export const PermissionPlugin: Plugin = { | ||
install: (app) => { | ||
const { some, every } = usePermissions(); | ||
|
||
app.config.globalProperties.$pm = { | ||
some: options.permissionsProvider.some, | ||
every: options.permissionsProvider.every | ||
some, | ||
every | ||
} | ||
} | ||
} | ||
|
||
export interface PermissionOptions { | ||
permissionsProvider: { some: (...permissionCodes: string[]) => boolean, every: (...permissionCodes: string[]) => boolean }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,11 @@ | ||
import { Plugin } from "vue"; | ||
|
||
export const TranslationPlugin: Plugin<TranslationOptions> = { | ||
install: (app, options) => { | ||
if (!options || !options.translationsProvider) { | ||
console.warn("Translation won't work without translationsProvider") | ||
return; | ||
} | ||
import { useTranslations } from '../composables'; | ||
|
||
app.config.globalProperties.$tr = options.translationsProvider.$tr; | ||
} | ||
} | ||
export const TranslationPlugin: Plugin = { | ||
install: (app) => { | ||
const { $tr } = useTranslations(); | ||
|
||
export interface TranslationOptions { | ||
translationsProvider: { $tr: (code: string, defaultLabel: string, ...parameters: string[]) => string } | ||
app.config.globalProperties.$tr = $tr; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters