2020-02-03 10:24:25 +01:00
|
|
|
import { createApp } from "./createApp"
|
|
|
|
import { trimSlash } from "./common/trimSlash"
|
2020-02-10 16:51:09 +01:00
|
|
|
import { builtins, builtinLibName } from "./render/builtinComponents"
|
2019-09-07 07:50:35 +02:00
|
|
|
|
2020-02-12 13:45:24 +01:00
|
|
|
export const loadBudibase = async (opts) => {
|
2020-02-03 10:24:25 +01:00
|
|
|
|
2020-02-12 13:45:24 +01:00
|
|
|
let componentLibraries = opts && opts.componentLibraries
|
|
|
|
const _window = (opts && opts.window) || window
|
|
|
|
const _localStorage = (opts && opts.localStorage) || localStorage
|
|
|
|
|
|
|
|
const backendDefinition = _window["##BUDIBASE_BACKEND_DEFINITION##"]
|
|
|
|
const frontendDefinition = _window["##BUDIBASE_FRONTEND_DEFINITION##"]
|
|
|
|
const uiFunctions = _window["##BUDIBASE_FRONTEND_FUNCTIONS##"]
|
|
|
|
|
|
|
|
const userFromStorage = _localStorage.getItem("budibase:user")
|
2020-02-03 10:24:25 +01:00
|
|
|
|
|
|
|
const user = userFromStorage
|
|
|
|
? JSON.parse(userFromStorage)
|
|
|
|
: {
|
2019-09-23 07:08:06 +02:00
|
|
|
name: "annonymous",
|
2020-02-03 10:24:25 +01:00
|
|
|
permissions: [],
|
|
|
|
isUser: false,
|
|
|
|
temp: false,
|
|
|
|
}
|
|
|
|
|
2020-02-10 16:51:09 +01:00
|
|
|
const rootPath =
|
2020-02-10 22:35:51 +01:00
|
|
|
frontendDefinition.appRootPath === ""
|
2020-02-10 16:51:09 +01:00
|
|
|
? ""
|
2020-02-10 22:35:51 +01:00
|
|
|
: "/" + trimSlash(frontendDefinition.appRootPath)
|
2020-02-10 16:51:09 +01:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
if (!componentLibraries) {
|
2020-02-10 16:51:09 +01:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const componentLibraryUrl = lib => rootPath + "/" + trimSlash(lib)
|
|
|
|
componentLibraries = {}
|
|
|
|
|
2020-02-10 22:35:51 +01:00
|
|
|
for (let lib of frontendDefinition.componentLibraries) {
|
2020-02-03 10:24:25 +01:00
|
|
|
componentLibraries[lib.libName] = await import(
|
|
|
|
componentLibraryUrl(lib.importPath)
|
|
|
|
)
|
2019-09-19 05:35:40 +02:00
|
|
|
}
|
2020-02-03 10:24:25 +01:00
|
|
|
}
|
|
|
|
|
2020-02-12 13:45:24 +01:00
|
|
|
componentLibraries[builtinLibName] = builtins(_window)
|
2020-02-03 10:24:25 +01:00
|
|
|
|
2020-02-10 16:51:09 +01:00
|
|
|
const { initialisePage, screenStore, pageStore, routeTo, rootNode } = createApp(
|
2020-02-12 13:45:24 +01:00
|
|
|
_window.document,
|
2020-02-03 10:24:25 +01:00
|
|
|
componentLibraries,
|
2020-02-10 22:35:51 +01:00
|
|
|
frontendDefinition,
|
|
|
|
backendDefinition,
|
2020-02-03 10:24:25 +01:00
|
|
|
user,
|
2020-02-12 13:45:24 +01:00
|
|
|
uiFunctions || {}
|
2020-02-03 10:24:25 +01:00
|
|
|
)
|
|
|
|
|
2020-02-12 13:45:24 +01:00
|
|
|
const route = _window.location
|
|
|
|
? _window.location.pathname.replace(rootPath, "")
|
2020-02-10 16:51:09 +01:00
|
|
|
: "";
|
|
|
|
|
|
|
|
return {
|
2020-02-12 13:45:24 +01:00
|
|
|
rootNode: initialisePage(frontendDefinition.page, _window.document.body, route),
|
2020-02-10 16:51:09 +01:00
|
|
|
screenStore,
|
|
|
|
pageStore,
|
|
|
|
routeTo,
|
|
|
|
rootNode
|
|
|
|
}
|
2020-02-03 10:24:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (window) {
|
|
|
|
window.loadBudibase = loadBudibase
|
|
|
|
}
|