@digital-blueprint/nexus-app
Version:
[GitHub Repository](https://github.com/digital-blueprint/nexus-app) | [npmjs package](https://www.npmjs.com/package/@digital-blueprint/nexus-app) | [Unpkg CDN](https://unpkg.com/browse/@digital-blueprint/nexus-app/) |
108 lines (94 loc) • 3.28 kB
JavaScript
import DBPLitElement from '@dbp-toolkit/common/dbp-lit-element';
import {IconButton} from "@dbp-toolkit/common";
import {Translated} from "@dbp-toolkit/common";
import {createInstance} from './i18n';
export default class DBPNexusLitElement extends DBPLitElement {
constructor() {
super();
this.auth = {};
this._i18n = createInstance();
this.lang = this._i18n.language;
this.entryPointUrl = '';
}
static get scopedElements() {
return {
'dbp-icon-button': IconButton,
'dbp-translated': Translated,
};
}
static get properties() {
return {
...super.properties,
auth: { type: Object },
lang: {type: String},
entryPointUrl: { type: String, attribute: 'entry-point-url' },
fileHandlingEnabledTargets: {type: String, attribute: 'file-handling-enabled-targets'},
nextcloudWebAppPasswordURL: {type: String, attribute: 'nextcloud-web-app-password-url'},
nextcloudWebDavURL: {type: String, attribute: 'nextcloud-webdav-url'},
nextcloudName: {type: String, attribute: 'nextcloud-name'},
nextcloudFileURL: {type: String, attribute: 'nextcloud-file-url'},
nextcloudAuthInfo: {type: String, attribute: 'nextcloud-auth-info'},
basePath: {type: String, attribute: 'base-path'},
};
}
connectedCallback() {
super.connectedCallback();
this._loginStatus = '';
this._loginState = [];
}
/**
* Request a re-rendering every time isLoggedIn()/isLoading() changes
*/
_updateAuth() {
this._loginStatus = this.auth['login-status'];
let newLoginState = [this.isLoggedIn(), this.isLoading()];
if (this._loginState.toString() !== newLoginState.toString()) {
this.requestUpdate();
}
this._loginState = newLoginState;
}
update(changedProperties) {
changedProperties.forEach((oldValue, propName) => {
switch (propName) {
case 'lang':
this._i18n.changeLanguage(this.lang);
break;
case "auth":
this._updateAuth();
break;
}
});
super.update(changedProperties);
}
/**
* Returns if a person is set in or not
* @returns {boolean} true or false
*/
isLoggedIn() {
return (this.auth.person !== undefined && this.auth.person !== null);
}
/**
* Returns true if a person has successfully logged in
* @returns {boolean} true or false
*/
isLoading() {
if (this._loginStatus === "logged-out")
return false;
return (!this.isLoggedIn() && this.auth.token !== undefined);
}
/**
* Send a fetch to given url with given options
* @param url
* @param options
* @returns {object} response (error or result)
*/
async httpGetAsync(url, options) {
let response = await fetch(url, options).then(result => {
if (!result.ok) throw result;
return result;
}).catch(error => {
return error;
});
return response;
}
}