budibase/packages/client/src/index.js

84 lines
2.2 KiB
JavaScript
Raw Normal View History

import { createApp } from "./createApp"
import { trimSlash } from "./common/trimSlash"
import { builtins, builtinLibName } from "./render/builtinComponents"
import * as standardComponents from "../../standard-components/dist";
import * as materialDesignComponents from "../../materialdesign-components/dist";
2019-09-07 07:50:35 +02:00
2020-02-20 21:19:24 +01:00
/**
* create a web application from static budibase definition files.
* @param {object} opts - configuration options for budibase client libary
*/
2020-02-25 16:21:23 +01:00
export const loadBudibase = async opts => {
let componentLibraries = opts && opts.componentLibraries
const _window = (opts && opts.window) || window
2020-02-25 16:21:23 +01:00
const _localStorage = (opts && opts.localStorage) || localStorage
const frontendDefinition = _window["##BUDIBASE_FRONTEND_DEFINITION##"]
const uiFunctions = _window["##BUDIBASE_FRONTEND_FUNCTIONS##"]
2020-02-25 16:21:23 +01:00
const userFromStorage = _localStorage.getItem("budibase:user")
const user = userFromStorage
? JSON.parse(userFromStorage)
: {
2019-09-23 07:08:06 +02:00
name: "annonymous",
permissions: [],
isUser: false,
temp: false,
}
let { appRootPath } = frontendDefinition;
appRootPath = appRootPath === "" ? "" : "/" + trimSlash(appRootPath)
// if (!componentLibraries) componentLibraries = {};
componentLibraries = {
"@budibase/standard-components": standardComponents,
"@budibase/materialdesign-components": materialDesignComponents
};
// if (!componentLibraries) {
// componentLibraries = {}
// for (let lib of frontendDefinition.componentLibraries) {
// componentLibraries[lib.libName] = await import(
// `${frontendDefinition.appRootPath}/${trimSlash(lib.importPath)}`
// )
// }
// }
componentLibraries[builtinLibName] = builtins(_window)
2020-02-25 17:23:45 +01:00
const {
initialisePage,
screenStore,
pageStore,
routeTo,
rootNode,
} = createApp(
componentLibraries,
frontendDefinition,
user,
uiFunctions || {},
2020-02-25 16:21:23 +01:00
_window,
rootNode
)
2020-02-25 16:21:23 +01:00
const route = _window.location
? _window.location.pathname.replace(frontendDefinition.appRootPath, "")
: ""
2020-02-25 17:23:45 +01:00
initialisePage(frontendDefinition.page, _window.document.body, route)
return {
screenStore,
pageStore,
routeTo,
2020-02-25 17:23:45 +01:00
rootNode,
}
}
if (window) {
window.loadBudibase = loadBudibase
}