Skip to content
Snippets Groups Projects
  • Reiter, Christoph's avatar
    433d5c85
    Add a new resource-select package/web-component · 433d5c85
    Reiter, Christoph authored
    The goal of this new component is to be a generic selector for API resources,
    unlike the organization/person select, which hardcode various things for their
    used resource.
    
    Instead this provides some events which can be used to customize the creation of the
    URL for fetching as well as for formatting the list of results.
    
    Atm this only handles the organization and no search-as-you-type like the person select,
    but maybe this can be extended in the future.
    433d5c85
    History
    Add a new resource-select package/web-component
    Reiter, Christoph authored
    The goal of this new component is to be a generic selector for API resources,
    unlike the organization/person select, which hardcode various things for their
    used resource.
    
    Instead this provides some events which can be used to customize the creation of the
    URL for fetching as well as for formatting the list of results.
    
    Atm this only handles the organization and no search-as-you-type like the person select,
    but maybe this can be extended in the future.
dbp-resource-select-demo.js 4.50 KiB
import {createInstance} from './i18n.js';
import {css, html} from 'lit';
import {ScopedElementsMixin} from '@open-wc/scoped-elements';
import {ResourceSelect} from './resource-select.js';
import {AuthKeycloak, LoginButton} from '@dbp-toolkit/auth';
import * as commonUtils from '@dbp-toolkit/common/utils';
import * as commonStyles from '@dbp-toolkit/common/styles';
import DBPLitElement from '@dbp-toolkit/common/dbp-lit-element';

export class ResourceSelectDemo extends ScopedElementsMixin(DBPLitElement) {
    constructor() {
        super();
        this._i18n = createInstance();
        this.lang = this._i18n.language;
        this.entryPointUrl = '';
        this.noAuth = false;
    }

    static get scopedElements() {
        return {
            'dbp-auth-keycloak': AuthKeycloak,
            'dbp-login-button': LoginButton,
            'dbp-resource-select': ResourceSelect,
        };
    }

    static get properties() {
        return {
            ...super.properties,
            lang: {type: String},
            entryPointUrl: {type: String, attribute: 'entry-point-url'},
            noAuth: {type: Boolean, attribute: 'no-auth'},
        };
    }

    update(changedProperties) {
        if (changedProperties.has('lang')) {
            this._i18n.changeLanguage(this.lang);
        }
        super.update(changedProperties);
    }

    static get styles() {
        // language=css
        return [
            commonStyles.getThemeCSS(),
            commonStyles.getGeneralCSS(),
            css`
                h1.title {
                    margin-bottom: 1em;
                }
                div.container {
                    margin-bottom: 1.5em;
                }
            `,
        ];
    }

    getAuthComponentHtml() {
        return this.noAuth
            ? html`
                  <dbp-login-button subscribe="auth" lang="${this.lang}"></dbp-login-button>
              `
            : html`
                  <div class="container">
                      <dbp-auth-keycloak
                          subscribe="requested-login-status"
                          lang="${this.lang}"
                          entry-point-url="${this.entryPointUrl}"
                          silent-check-sso-redirect-uri="/silent-check-sso.html"
                          url="https://auth-dev.tugraz.at/auth"
                          realm="tugraz-vpu"
                          client-id="auth-dev-mw-frontend-local"
                          try-login></dbp-auth-keycloak>
                      <dbp-login-button subscribe="auth" lang="${this.lang}"></dbp-login-button>
                  </div>
              `;
    }

    render() {
        let buildUrl = (event) => {
            let select = event.target;
            let url = new URL(select.resourcePath, select.entryPointUrl).href;
            url += '/' + encodeURIComponent(select.auth['person-id']);
            url += '/organizations';
            url += '?' + new URLSearchParams({lang: select.lang}).toString();
            event.detail.url = url;
        };

        let formatResource = (event) => {
            event.detail.text = event.detail.object.name;
        };

        let change = (event) => {
            console.log('change:', event.detail.value, event.detail.object);
        };

        return html`
            <section class="section">
                <div class="container">
                    <h1 class="title">resource-select-Demo</h1>
                </div>
                ${this.getAuthComponentHtml()}
                <div class="container">
                    <form>
                        <div class="field">
                            <label class="label">Organization of the current user</label>
                            <div class="control">
                                <dbp-resource-select
                                    id="resource-select-library-manager"
                                    subscribe="auth"
                                    lang="${this.lang}"
                                    entry-point-url="${this.entryPointUrl}"
                                    resource-path="base/people"
                                    @change="${change}"
                                    @build-url="${buildUrl}"
                                    @format-resource="${formatResource}"></dbp-resource-select>
                            </div>
                        </div>
                    </form>
                </div>
            </section>
        `;
    }
}

commonUtils.defineCustomElement('dbp-resource-select-demo', ResourceSelectDemo);