Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow to setup theme base on the user
fixes #511
- Loading branch information
1 parent
9b126d3
commit baa28f5
Showing
11 changed files
with
229 additions
and
84 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
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
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,59 @@ | ||
import merge from 'lodash/merge' | ||
import slash from 'slash' | ||
import path from 'path' | ||
import AdminBro from '../../admin-bro' | ||
import { CurrentAdmin } from '../../current-admin.interface' | ||
import { BrandingOptions, Assets } from '../../admin-bro-options.interface' | ||
|
||
|
||
const defaultBranding = { | ||
companyName: 'Company', | ||
softwareBrothers: true, | ||
} | ||
const defaultAssets = { | ||
styles: [], | ||
scripts: [], | ||
} | ||
|
||
export const getAssets = async ( | ||
admin: AdminBro, | ||
currentAdmin?: CurrentAdmin, | ||
): Promise<Assets> => { | ||
const { assets } = admin.options || {} | ||
const computed = typeof assets === 'function' | ||
? await assets(currentAdmin) | ||
: assets | ||
|
||
return merge({}, defaultAssets, computed) | ||
} | ||
|
||
export const getBranding = async ( | ||
admin: AdminBro, | ||
currentAdmin?: CurrentAdmin, | ||
): Promise<BrandingOptions> => { | ||
const { branding } = admin.options | ||
const defaultLogo = slash(path.join( | ||
admin.options.rootPath, | ||
'/frontend/assets/logo-mini.svg', | ||
)) | ||
|
||
const computed = typeof branding === 'function' | ||
? await branding(currentAdmin) | ||
: branding | ||
const merged = merge({}, defaultBranding, computed) | ||
|
||
// checking for undefined because logo can also be `false` or `null` | ||
merged.logo = merged.logo !== undefined ? merged.logo : defaultLogo | ||
|
||
return merged | ||
} | ||
|
||
export const getFaviconFromBranding = (branding: BrandingOptions): string => { | ||
if (branding.favicon) { | ||
const { favicon } = branding | ||
const type = favicon.match(/.*\.png$/) ? 'image/png' : 'image/x-icon' | ||
return `<link rel="shortcut icon" type="${type}" href="${favicon}" />` | ||
} | ||
|
||
return '' | ||
} |
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,66 +1,77 @@ | ||
import { expect } from 'chai' | ||
import layoutTemplate from './layout-template' | ||
import AdminBro from '../admin-bro' | ||
import AdminBroOptions from '../admin-bro-options.interface' | ||
import { BrandingOptions } from '../admin-bro-options.interface' | ||
|
||
describe('layoutTemplate', function () { | ||
context('AdminBro with default options and not logged in user', function () { | ||
beforeEach(function () { | ||
this.adminBro = new AdminBro({}) | ||
context('AdminBro with branding options set as a function', function () { | ||
const companyName = 'Dynamic Company' | ||
let html: string | ||
|
||
beforeEach(async function () { | ||
const adminBro = new AdminBro({ | ||
branding: async () => ({ companyName }), | ||
}) | ||
|
||
html = await layoutTemplate(adminBro, undefined, '/') | ||
}) | ||
|
||
it('renders default company name', function () { | ||
expect( | ||
layoutTemplate(this.adminBro, undefined, '/'), | ||
).to.contain(this.adminBro.options.branding.companyName) | ||
expect(html).to.contain(companyName) | ||
}) | ||
|
||
it('links to global bundle', function () { | ||
expect(layoutTemplate(this.adminBro, undefined, '/')).to.contain('global.bundle.js') | ||
it('links to global bundle', async function () { | ||
expect(html).to.contain('global.bundle.js') | ||
}) | ||
}) | ||
|
||
describe('AdminBro with branding options given', function () { | ||
beforeEach(function () { | ||
this.branding = { | ||
softwareBrothers: false, | ||
companyName: 'Other name', | ||
favicon: '/someImage.png', | ||
} as AdminBroOptions['branding'] | ||
const branding = { | ||
softwareBrothers: false, | ||
companyName: 'Other name', | ||
favicon: '/someImage.png', | ||
} as BrandingOptions | ||
let html: string | ||
|
||
this.adminBro = new AdminBro({ branding: this.branding }) | ||
this.renderedContent = layoutTemplate(this.adminBro, undefined, '/') | ||
beforeEach(async function () { | ||
const adminBro = new AdminBro({ branding }) | ||
|
||
html = await layoutTemplate(adminBro, undefined, '/') | ||
}) | ||
|
||
it('renders company name', function () { | ||
expect(this.renderedContent).to.contain(this.branding.companyName) | ||
expect(html).to.contain(branding.companyName) | ||
}) | ||
|
||
it('renders favicon', function () { | ||
expect(this.renderedContent).to.contain( | ||
`<link rel="shortcut icon" type="image/png" href="${this.branding.favicon}" />`, | ||
expect(html).to.contain( | ||
`<link rel="shortcut icon" type="image/png" href="${branding.favicon}" />`, | ||
) | ||
}) | ||
}) | ||
|
||
context('custom styles and scripts were defined in AdminBro options', function () { | ||
beforeEach(function () { | ||
this.scriptUrl = 'http://somescript.com' | ||
this.styleUrl = 'http://somestyle.com' | ||
this.adminBro = new AdminBro({ | ||
let html: string | ||
const scriptUrl = 'http://somescript.com' | ||
const styleUrl = 'http://somestyle.com' | ||
|
||
beforeEach(async function () { | ||
const adminBro = new AdminBro({ | ||
assets: { | ||
styles: [this.styleUrl], | ||
scripts: [this.scriptUrl], | ||
styles: [styleUrl], | ||
scripts: [scriptUrl], | ||
}, | ||
}) | ||
|
||
html = await layoutTemplate(adminBro, undefined, '/') | ||
}) | ||
|
||
it('adds styles to the head section', function () { | ||
expect(layoutTemplate(this.adminBro, undefined, '/')).to.contain(this.styleUrl) | ||
expect(html).to.contain(styleUrl) | ||
}) | ||
|
||
it('adds scripts to the body', function () { | ||
expect(layoutTemplate(this.adminBro, undefined, '/')).to.contain(this.scriptUrl) | ||
expect(html).to.contain(scriptUrl) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.