Skip to content
Snippets Groups Projects
Commit d929cf61 authored by Bekerle, Patrizio's avatar Bekerle, Patrizio :fire: Committed by Reiter, Christoph
Browse files

Implement dynamic language switching

parent 6ffbd638
No related branches found
No related tags found
No related merge requests found
...@@ -15,6 +15,7 @@ class PersonSelect extends VPULitElementJQuery { ...@@ -15,6 +15,7 @@ class PersonSelect extends VPULitElementJQuery {
constructor() { constructor() {
super(); super();
this.lang = 'de'; this.lang = 'de';
this.jsonld = null;
} }
static get properties() { static get properties() {
...@@ -29,13 +30,33 @@ class PersonSelect extends VPULitElementJQuery { ...@@ -29,13 +30,33 @@ class PersonSelect extends VPULitElementJQuery {
this.updateComplete.then(()=>{ this.updateComplete.then(()=>{
const that = this; const that = this;
const $that = $(this);
let lastResult = {}; let lastResult = {};
JSONLD.initialize(utils.getAPiUrl(), function (jsonld) { JSONLD.initialize(utils.getAPiUrl(), function (jsonld) {
that.jsonld = jsonld;
const $select = that.initSelect2();
// close the selector on blur of the web component
$(that).blur(() => {
// the 500ms delay is a workaround to actually get an item selected when clicking on it,
// because the blur gets also fired when clicking in the selector
setTimeout(() => {$select.select2('close')}, 500);
});
});
})
}
/**
* Initializes the Select2 selector
*/
initSelect2() {
const that = this;
const $this = $(this);
let lastResult = {};
// find the correct api url for a person // find the correct api url for a person
const apiUrl = jsonld.getApiUrlForIdentifier("http://schema.org/Person"); const apiUrl = this.jsonld.getApiUrlForIdentifier("http://schema.org/Person");
// const apiUrl = jsonld.getApiUrlForEntityName("Event"); // const apiUrl = this.jsonld.getApiUrlForEntityName("Event");
// the mapping we need for Select2 // the mapping we need for Select2
const localContext = { const localContext = {
...@@ -43,14 +64,14 @@ class PersonSelect extends VPULitElementJQuery { ...@@ -43,14 +64,14 @@ class PersonSelect extends VPULitElementJQuery {
"text": "http://schema.org/name" "text": "http://schema.org/name"
}; };
const $select = that.$('#person-select'); const $select = this.$('#person-select');
$select.select2({ $select.select2({
width: '100%', width: '100%',
language: that.lang === "de" ? select2LangDe() : select2LangEn(), language: this.lang === "de" ? select2LangDe() : select2LangEn(),
minimumInputLength: 2, minimumInputLength: 2,
placeholder: i18n.t('person-select.placeholder'), placeholder: i18n.t('person-select.placeholder'),
dropdownParent: that.$('#person-select-dropdown'), dropdownParent: this.$('#person-select-dropdown'),
ajax: { ajax: {
delay: 250, delay: 250,
url: apiUrl, url: apiUrl,
...@@ -68,7 +89,7 @@ class PersonSelect extends VPULitElementJQuery { ...@@ -68,7 +89,7 @@ class PersonSelect extends VPULitElementJQuery {
console.log(data); console.log(data);
lastResult = data; lastResult = data;
const results = jsonld.transformMembers(data, localContext); const results = that.jsonld.transformMembers(data, localContext);
console.log("results"); console.log("results");
console.log(results); console.log(results);
...@@ -81,14 +102,14 @@ class PersonSelect extends VPULitElementJQuery { ...@@ -81,14 +102,14 @@ class PersonSelect extends VPULitElementJQuery {
}).on("select2:select", function (e) { }).on("select2:select", function (e) {
// set value custom element // set value custom element
const identifier = e.params.data.id; const identifier = e.params.data.id;
$that.attr("value", identifier); $this.attr("value", identifier);
$that.val(identifier); $this.val(identifier);
const object = utils.findObjectInApiResults(identifier, lastResult); const object = utils.findObjectInApiResults(identifier, lastResult);
$that.attr("data-object", JSON.stringify(object)); $this.attr("data-object", JSON.stringify(object));
// fire a change event // fire a change event
that.dispatchEvent(new CustomEvent('change', { this.dispatchEvent(new CustomEvent('change', {
detail: { detail: {
value: identifier, value: identifier,
}, },
...@@ -96,14 +117,23 @@ class PersonSelect extends VPULitElementJQuery { ...@@ -96,14 +117,23 @@ class PersonSelect extends VPULitElementJQuery {
})); }));
}); });
// close the selector on blur of the web component return $select;
$(that).blur(() => { }
// the 500ms delay is a workaround to actually get an item selected when clicking on it,
// because the blur gets also fired when clicking in the selector updated(changedProperties) {
setTimeout(() => {$select.select2('close')}, 500); changedProperties.forEach((oldValue, propName) => {
}); if (propName === "lang") {
i18n.changeLanguage(this.lang);
const $select = this.$('#person-select.select2-hidden-accessible');
if ($select.length > 0) {
// no other way to set an other language at runtime did work
$select.select2('destroy');
this.initSelect2();
}
}
}); });
})
} }
render() { render() {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment