import {i18n} from './i18n.js'; import {css, html} from 'lit-element'; import {ScopedElementsMixin} from '@open-wc/scoped-elements'; import {AuthKeycloak, LoginButton} from '@dbp-toolkit/auth'; import * as commonUtils from '@dbp-toolkit/common/utils'; import * as commonStyles from '@dbp-toolkit/common/styles'; import {Provider} from '@dbp-toolkit/provider'; import {Adapter} from '@dbp-toolkit/provider'; import {LanguageSelect} from '@dbp-toolkit/language-select'; import DBPLitElement from "@dbp-toolkit/common/dbp-lit-element"; class ProviderDemo extends ScopedElementsMixin(DBPLitElement) { constructor() { super(); this.lang = 'de'; } static get scopedElements() { return { 'dbp-auth-keycloak': AuthKeycloak, 'dbp-login-button': LoginButton, 'dbp-language-select': LanguageSelect, 'dbp-provider': Provider, 'dbp-provider-adapter': Adapter, 'dbp-consumer': DemoConsumer, }; } static get properties() { return { ...super.properties, lang: { type: String }, }; } connectedCallback() { super.connectedCallback(); i18n.changeLanguage(this.lang); } attributeChangedCallback(name, oldValue, newValue) { console.log('ProviderDemo (' + this.id + ') attributeChangesCallback( ' + name + ', ' + oldValue + ', ' + newValue + ')'); switch(name) { case 'lang': this.lang = newValue; i18n.changeLanguage(this.lang); break; default: super.attributeChangedCallback(name, oldValue, newValue); } this.render(); } static get styles() { // language=css return [ commonStyles.getThemeCSS(), commonStyles.getGeneralCSS(), css` h1.title {margin-bottom: 1em;} div.container {margin-bottom: 1.5em; padding-left:20px;} ` ]; } get id() { return this.getAttribute('id'); } render() { return html` <section class="section"> <p>${i18n.t('demo.provider_description', {id: "root", description: "is the top most in hierarchy"})}</p> <pre><dbp-provider id="root" root="1" availability="global" ></dbp-provider></pre> <div class="container"> <h1 class="title">${i18n.t('demo.provider')}-Demo</h1> </div> <div class="container"> <dbp-auth-keycloak subscribe="requested-login-status" lang="${this.lang}" entry-point-url="${this.entryPointUrl}" url="https://auth-dev.tugraz.at/auth" realm="tugraz" client-id="auth-dev-mw-frontend-local" load-person try-login></dbp-auth-keycloak> <dbp-login-button subscribe="auth" lang="${this.lang}" show-image></dbp-login-button> <dbp-language-select></dbp-language-select> </div> <dbp-provider id="demo" bc="blue"> <dbp-provider id="foo-bar" foo="9" bar="20"> <div class="container"> <h2>${i18n.t('demo.provider')}</h2> <p>${i18n.t('demo.provider_description', {id: "demo", description: "has only \"border-color\" to offer"})}</p> <pre><dbp-provider id="demo" bc="blue" ></dbp-provider></pre> <p>${i18n.t('demo.provider_description', {id: "foo-bar", description: "has some values in its store"})}</p> <pre><dbp-provider id="foo-bar" foo="9" bar="20" ></dbp-provider></pre> <h2>${i18n.t('demo.consumer')}</h2> <p>${i18n.t('demo.consumer_description', {id: "c1", subscriptions: "border-color"})}</p> <pre><dbp-consumer id="c1" subscribe="border-color:bc" ></dbp-consumer></pre> <dbp-consumer id="c1" subscribe="border-color:bc,lang:lang"></dbp-consumer> <p>${i18n.t('demo.consumer_description', {id: "c2", subscriptions: "foo"})}</p> <pre><dbp-consumer id="c2" subscribe="foo:foo" ></dbp-consumer></pre> <dbp-consumer id="c2" subscribe="foo:foo,lang:lang"></dbp-consumer> <p>${i18n.t('demo.consumer_description', {id: "c3", subscriptions: "availability:status"})}</p> <p>Local <em>status</em> is provided as <em>availability</em></p> <pre><dbp-consumer id="c3" subscribe="status:availability" border-color="orange" ></dbp-consumer></pre> <dbp-consumer id="c3" subscribe="status:availability,lang:lang" border-color="orange"></dbp-consumer> <p>${i18n.t('demo.consumer_description', {id: "c4", subscriptions: "unknown-name:status"})}</p> <p>Remote <em>unknown-name</em> does not exist, the default value is overwritten by <em>undefined</em></i></p> <pre><dbp-consumer id="c4" subscribe="status:unknown-name" border-color="darkgray" ></dbp-consumer></pre> <dbp-consumer id="c4" subscribe="status:unknown-name" border-color="darkgray"></dbp-consumer> </div> </dbp-provider> </dbp-provider> <h2> DBP Provider Adapter</h2> <p> The dbp-provider is for third party webcomponents, which we want to configure with a provider.</p> <pre><dbp-provider id="demoadapter" dbp-style-red="color:red;" dbp-style-green="color:green;" ></dbp-provider></pre> <dbp-provider id="demoadapter" dbp-style-red="color:red;" dbp-style-green="color:green;"> <pre><dbp-provider-adapter id="a1" subscribe="style:dbp-style-red" ></dbp-provider-adapter></pre> <dbp-provider-adapter id="a1" subscribe="style:dbp-style-red"> <p> I'm a normal p tag without attributes and without style. </p> <p> I'm a normal p tag without attributes and without style. </p> <p> I'm a normal p tag without attributes and without style. </p> </dbp-provider-adapter> <pre><dbp-provider-adapter id="a2" subscribe="style:dbp-style-green" ></dbp-provider-adapter></pre> <dbp-provider-adapter id="a2" subscribe="style:dbp-style-green"> <p style="background-color:green;"> I'm a normal p tag without attributes and without style. <span style="color:blue;"> I'm blue dabedidabedei...</span> </p> </dbp-provider-adapter> </dbp-provider> </section> `; } } commonUtils.defineCustomElement('dbp-provider-demo', ProviderDemo); // ======================================================= class DemoConsumer extends DBPLitElement { constructor() { super(); this.lang = 'de'; this.entryPointUrl = ''; // default values this.foo = 100; this.bar = 900; this.ping = 0; this.borderColor = 'green'; this.status = 'local'; console.log('DemoConsumer constructor()'); } connectedCallback() { super.connectedCallback(); i18n.changeLanguage(this.lang); console.log('DemoConsumer(' + this.id + ') connectedCallback()'); this.render(); } static get properties() { return { ...super.properties, lang: { type: String }, entryPointUrl: { type: String, attribute: 'entry-point-url' }, foo: { type: String }, bar: { type: String }, gong: { type: String }, borderColor: { type: String, attribute: 'border-color' }, ping: { type: String } }; } attributeChangedCallback(name, oldValue, newValue) { if (oldValue === newValue) { return; } console.log('DemoConsumer(' + this.id + ') attributeChangesCallback( ' + name + ', ' + oldValue + ', ' + newValue + ')'); switch(name) { case 'lang': this.lang = newValue; i18n.changeLanguage(this.lang); break; case 'foo': this.foo = parseInt(newValue); break; case 'bar': this.bar = parseInt(newValue); break; case 'status': this.status = newValue; break; case 'border-color': this['border-color'] = newValue; break; default: super.attributeChangedCallback(name, oldValue, newValue); } this.render(); } get id() { return this.getAttribute('id'); } render() { if (! this.connected) { return `not connected!`; } console.log('DemoConsumer(' + this.id + ') render()'); const sum = this.foo + this.bar; return html` <div style="border: ${this['border-color']} dotted; padding: 10px;"> <table style="width:200px;"> <tr style="background-color: #aaa;"> <th style="text-align: left;">${i18n.t('consumer.item')}</th> <th style="text-align: right;">${i18n.t('consumer.price')}</th> </tr> <tr><td>foo</td><td style="text-align: right;">${this.foo}</td></tr> <tr><td>bar</td><td style="text-align: right;">${this.bar}</td></tr> <tr><td>${i18n.t('consumer.sum')}</td><td style="text-align: right;">${sum}</td></tr> </table> <p>Status: <b>${this.status}</b></p> </div> `; } } customElements.define('dbp-consumer', DemoConsumer);