2020-02-03 10:24:25 +01:00
|
|
|
import { createApp } from "./createApp"
|
2020-02-10 16:51:09 +01:00
|
|
|
import { builtins, builtinLibName } from "./render/builtinComponents"
|
2020-07-03 00:22:20 +02:00
|
|
|
import { parseAppIdFromCookie } from "./render/getAppId"
|
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 => {
|
2020-02-12 13:45:24 +01:00
|
|
|
const _window = (opts && opts.window) || window
|
2020-05-07 11:53:34 +02:00
|
|
|
// const _localStorage = (opts && opts.localStorage) || localStorage
|
2020-07-03 00:22:20 +02:00
|
|
|
const appId = parseAppIdFromCookie(_window.document.cookie)
|
2020-02-12 13:45:24 +01:00
|
|
|
const frontendDefinition = _window["##BUDIBASE_FRONTEND_DEFINITION##"]
|
2020-02-25 16:21:23 +01:00
|
|
|
|
2020-05-07 11:53:34 +02:00
|
|
|
const user = {}
|
2020-02-03 10:24:25 +01:00
|
|
|
|
2020-05-18 12:01:09 +02:00
|
|
|
const componentLibraryModules = (opts && opts.componentLibraries) || {}
|
2020-02-03 10:24:25 +01:00
|
|
|
|
2020-05-07 23:25:27 +02:00
|
|
|
const libraries = frontendDefinition.libraries || []
|
2020-05-07 11:53:34 +02:00
|
|
|
|
2020-05-06 13:17:15 +02:00
|
|
|
for (let library of libraries) {
|
2020-05-07 11:53:34 +02:00
|
|
|
// fetch the JavaScript for the component libraries from the server
|
2020-07-03 00:22:20 +02:00
|
|
|
// componentLibraryModules[library] = await import(
|
|
|
|
// `/componentlibrary?library=${encodeURI(library)}`
|
|
|
|
// )
|
|
|
|
componentLibraryModules[library] = await import(`/assets/componentlibrary/${library}/dist/index.js`)
|
2020-05-07 11:53:34 +02:00
|
|
|
}
|
2020-04-29 21:29:42 +02:00
|
|
|
|
2020-05-06 13:17:15 +02:00
|
|
|
componentLibraryModules[builtinLibName] = builtins(_window)
|
2020-02-03 10:24:25 +01:00
|
|
|
|
2020-02-25 17:23:45 +01:00
|
|
|
const {
|
|
|
|
initialisePage,
|
|
|
|
screenStore,
|
|
|
|
pageStore,
|
|
|
|
routeTo,
|
|
|
|
rootNode,
|
2020-05-29 15:06:10 +02:00
|
|
|
} = createApp({
|
|
|
|
componentLibraries: componentLibraryModules,
|
2020-02-10 22:35:51 +01:00
|
|
|
frontendDefinition,
|
2020-02-03 10:24:25 +01:00
|
|
|
user,
|
2020-06-14 21:30:23 +02:00
|
|
|
window: _window,
|
2020-05-29 15:06:10 +02:00
|
|
|
})
|
2020-02-03 10:24:25 +01:00
|
|
|
|
2020-02-25 16:21:23 +01:00
|
|
|
const route = _window.location
|
2020-06-12 21:42:55 +02:00
|
|
|
? _window.location.pathname.replace(`${appId}/`, "").replace(appId, "")
|
2020-02-25 16:21:23 +01:00
|
|
|
: ""
|
2020-02-10 16:51:09 +01:00
|
|
|
|
2020-02-25 17:23:45 +01:00
|
|
|
initialisePage(frontendDefinition.page, _window.document.body, route)
|
|
|
|
|
2020-02-10 16:51:09 +01:00
|
|
|
return {
|
|
|
|
screenStore,
|
|
|
|
pageStore,
|
|
|
|
routeTo,
|
2020-02-25 17:23:45 +01:00
|
|
|
rootNode,
|
2020-02-10 16:51:09 +01:00
|
|
|
}
|
2020-02-03 10:24:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (window) {
|
|
|
|
window.loadBudibase = loadBudibase
|
|
|
|
}
|