2020-02-03 22:24:25 +13:00
|
|
|
import { createApp } from "./createApp"
|
|
|
|
import { trimSlash } from "./common/trimSlash"
|
2020-02-11 04:51:09 +13:00
|
|
|
import { builtins, builtinLibName } from "./render/builtinComponents"
|
2020-05-06 23:17:15 +12:00
|
|
|
// import * as standardComponents from "../../standard-components/dist";
|
|
|
|
// import * as materialDesignComponents from "../../materialdesign-components/dist";
|
2019-09-07 17:50:35 +12:00
|
|
|
|
2020-02-21 09:19:24 +13:00
|
|
|
/**
|
|
|
|
* create a web application from static budibase definition files.
|
|
|
|
* @param {object} opts - configuration options for budibase client libary
|
|
|
|
*/
|
2020-02-26 04:21:23 +13:00
|
|
|
export const loadBudibase = async opts => {
|
2020-02-13 01:45:24 +13:00
|
|
|
const _window = (opts && opts.window) || window
|
2020-02-26 04:21:23 +13:00
|
|
|
const _localStorage = (opts && opts.localStorage) || localStorage
|
2020-02-13 01:45:24 +13:00
|
|
|
|
|
|
|
const frontendDefinition = _window["##BUDIBASE_FRONTEND_DEFINITION##"]
|
|
|
|
const uiFunctions = _window["##BUDIBASE_FRONTEND_FUNCTIONS##"]
|
2020-02-26 04:21:23 +13:00
|
|
|
|
2020-02-13 01:45:24 +13:00
|
|
|
const userFromStorage = _localStorage.getItem("budibase:user")
|
2020-02-03 22:24:25 +13:00
|
|
|
|
|
|
|
const user = userFromStorage
|
|
|
|
? JSON.parse(userFromStorage)
|
|
|
|
: {
|
2019-09-23 17:08:06 +12:00
|
|
|
name: "annonymous",
|
2020-02-03 22:24:25 +13:00
|
|
|
permissions: [],
|
|
|
|
isUser: false,
|
|
|
|
temp: false,
|
|
|
|
}
|
|
|
|
|
2020-05-03 02:29:10 +12:00
|
|
|
let { appRootPath } = frontendDefinition;
|
2020-04-30 07:29:42 +12:00
|
|
|
appRootPath = appRootPath === "" ? "" : "/" + trimSlash(appRootPath)
|
2020-02-11 04:51:09 +13:00
|
|
|
|
2020-02-03 22:24:25 +13:00
|
|
|
|
2020-05-06 23:17:15 +12:00
|
|
|
const componentLibraryModules = {};
|
|
|
|
const libraries = frontendDefinition.libraries || [
|
|
|
|
"@budibase/standard-components",
|
|
|
|
"@budibase/materialdesign-components",
|
|
|
|
];
|
|
|
|
for (let library of libraries) {
|
|
|
|
// fetch the JavaScript for the component libraries from the server
|
|
|
|
componentLibraryModules[library] = await import(
|
|
|
|
`/${frontendDefinition.appId}/componentlibrary?library=${encodeURI(library)}`
|
|
|
|
);
|
2020-04-30 07:29:42 +12:00
|
|
|
};
|
|
|
|
|
2020-05-06 23:17:15 +12:00
|
|
|
componentLibraryModules[builtinLibName] = builtins(_window)
|
2020-02-03 22:24:25 +13:00
|
|
|
|
2020-02-26 05:23:45 +13:00
|
|
|
const {
|
|
|
|
initialisePage,
|
|
|
|
screenStore,
|
|
|
|
pageStore,
|
|
|
|
routeTo,
|
|
|
|
rootNode,
|
|
|
|
} = createApp(
|
2020-05-06 23:17:15 +12:00
|
|
|
componentLibraryModules,
|
2020-02-11 10:35:51 +13:00
|
|
|
frontendDefinition,
|
2020-02-03 22:24:25 +13:00
|
|
|
user,
|
2020-02-19 01:29:38 +13:00
|
|
|
uiFunctions || {},
|
2020-02-26 04:21:23 +13:00
|
|
|
_window,
|
|
|
|
rootNode
|
2020-02-03 22:24:25 +13:00
|
|
|
)
|
|
|
|
|
2020-02-26 04:21:23 +13:00
|
|
|
const route = _window.location
|
|
|
|
? _window.location.pathname.replace(frontendDefinition.appRootPath, "")
|
|
|
|
: ""
|
2020-02-11 04:51:09 +13:00
|
|
|
|
2020-02-26 05:23:45 +13:00
|
|
|
initialisePage(frontendDefinition.page, _window.document.body, route)
|
|
|
|
|
2020-02-11 04:51:09 +13:00
|
|
|
return {
|
|
|
|
screenStore,
|
|
|
|
pageStore,
|
|
|
|
routeTo,
|
2020-02-26 05:23:45 +13:00
|
|
|
rootNode,
|
2020-02-11 04:51:09 +13:00
|
|
|
}
|
2020-02-03 22:24:25 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
if (window) {
|
|
|
|
window.loadBudibase = loadBudibase
|
|
|
|
}
|