|
|
import { AdminJSOptions, Assets } from '../../../adminjs-options.interface.js' |
|
|
import type { PathsInState } from '../../../index.js' |
|
|
|
|
|
type Paths = PathsInState |
|
|
|
|
|
let globalAny: any = {} |
|
|
|
|
|
try { |
|
|
globalAny = window |
|
|
} catch (error) { |
|
|
if (!(error instanceof ReferenceError)) { |
|
|
throw error |
|
|
} |
|
|
} finally { |
|
|
if (!globalAny) { |
|
|
globalAny = {} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export type ActionParams = { |
|
|
|
|
|
|
|
|
|
|
|
resourceId: string |
|
|
|
|
|
|
|
|
|
|
|
actionName: string |
|
|
|
|
|
|
|
|
|
|
|
search?: string |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export type RecordActionParams = ActionParams & { |
|
|
|
|
|
|
|
|
|
|
|
recordId: string |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export type BulkActionParams = ActionParams & { |
|
|
|
|
|
|
|
|
|
|
|
recordIds?: Array<string> |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export type ResourceActionParams = ActionParams |
|
|
|
|
|
const runDate = new Date() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export class ViewHelpers { |
|
|
public options: Paths |
|
|
|
|
|
constructor({ options }: { options?: AdminJSOptions } = {}) { |
|
|
let opts: Paths = ViewHelpers.getPaths(options) |
|
|
|
|
|
opts = opts || { |
|
|
rootPath: '/admin', |
|
|
} |
|
|
|
|
|
|
|
|
this.options = opts |
|
|
} |
|
|
|
|
|
static getPaths(options?: AdminJSOptions): Paths { |
|
|
return options || globalAny.REDUX_STATE?.paths |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
urlBuilder(paths: Array<string> = [], search = ''): string { |
|
|
const separator = '/' |
|
|
const replace = new RegExp(`${separator}{1,}`, 'g') |
|
|
|
|
|
let { rootPath } = this.options |
|
|
if (!rootPath.startsWith(separator)) { |
|
|
rootPath = `${separator}${rootPath}` |
|
|
} |
|
|
|
|
|
const parts = [rootPath, ...paths] |
|
|
return `${parts.join(separator).replace(replace, separator)}${search}` |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
loginUrl(): string { |
|
|
return this.options.loginPath |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
logoutUrl(): string { |
|
|
return this.options.logoutPath |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
dashboardUrl(): string { |
|
|
return this.options.rootPath |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pageUrl(pageName: string): string { |
|
|
return this.urlBuilder(['pages', pageName]) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
editUrl(resourceId: string, recordId: string, search?: string): string { |
|
|
return this.recordActionUrl({ resourceId, recordId, actionName: 'edit', search }) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
showUrl(resourceId: string, recordId: string, search?: string): string { |
|
|
return this.recordActionUrl({ resourceId, recordId, actionName: 'show', search }) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
deleteUrl(resourceId: string, recordId: string, search?: string): string { |
|
|
return this.recordActionUrl({ resourceId, recordId, actionName: 'delete', search }) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
newUrl(resourceId: string, search?: string): string { |
|
|
return this.resourceActionUrl({ resourceId, actionName: 'new', search }) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
listUrl(resourceId: string, search?: string): string { |
|
|
return this.resourceActionUrl({ resourceId, actionName: 'list', search }) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bulkDeleteUrl(resourceId: string, recordIds: Array<string>, search?: string): string { |
|
|
return this.bulkActionUrl({ resourceId, recordIds, actionName: 'bulkDelete', search }) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
resourceActionUrl({ resourceId, actionName, search }: ResourceActionParams): string { |
|
|
return this.urlBuilder(['resources', resourceId, 'actions', actionName], search) |
|
|
} |
|
|
|
|
|
resourceUrl({ resourceId, search }: Omit<ResourceActionParams, 'actionName'>): string { |
|
|
return this.urlBuilder(['resources', resourceId], search) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
recordActionUrl({ resourceId, recordId, actionName, search }: RecordActionParams): string { |
|
|
return this.urlBuilder(['resources', resourceId, 'records', recordId, actionName], search) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bulkActionUrl({ resourceId, recordIds, actionName, search }: BulkActionParams): string { |
|
|
const url = this.urlBuilder(['resources', resourceId, 'bulk', actionName]) |
|
|
if (recordIds && recordIds.length) { |
|
|
const query = new URLSearchParams(search) |
|
|
query.set('recordIds', recordIds.join(',')) |
|
|
return `${url}?${query.toString()}` |
|
|
} |
|
|
return `${url}${search || ''}` |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assetPath(asset: string, assetsConfig?: Assets): string { |
|
|
if (this.options.assetsCDN) { |
|
|
const pathname = assetsConfig?.coreScripts?.[asset] ?? asset |
|
|
const url = new URL(pathname, this.options.assetsCDN).href |
|
|
|
|
|
|
|
|
return `${url}?date=${runDate.getTime()}` |
|
|
} |
|
|
return this.urlBuilder(['frontend', 'assets', asset]) |
|
|
} |
|
|
} |
|
|
|
|
|
export default ViewHelpers |
|
|
|