Skip to content

Commit 39a32ee

Browse files
committed
v2.0
1 parent fbf28b0 commit 39a32ee

3 files changed

Lines changed: 59 additions & 10 deletions

File tree

browser.js

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
class: { value: classToggleBind, writable: true, configurable: false, enumerable: false },
4444
attribute: { value: attributeBind, writable: true, configurable: false, enumerable: false },
4545
property: { value: propertyBind, writable: true, configurable: false, enumerable: false },
46+
language: { value: languageBind, writable: true, configurable: false, enumerable: false },
4647

4748
input: { value: valueBind, writable: true, configurable: false, enumerable: false },
4849
input_checkbox: { value: checkBind, writable: true, configurable: false, enumerable: false },
@@ -1353,6 +1354,41 @@
13531354
// I am alive!
13541355
return this.contextValue() === undefined ? false : true;
13551356
}
1357+
function languageBind(event) {
1358+
1359+
if (event?.eventName === "unbind") {
1360+
1361+
this.innerHTML = '';
1362+
// I am dead!
1363+
return false;
1364+
}
1365+
1366+
var val = this.contextValue();
1367+
1368+
if (typeof val === 'object' && val !== null) {
1369+
1370+
// If the value is an object, we will try to find the language key in it.
1371+
var lang = navigator.language || navigator.userLanguage || 'en';
1372+
lang = lang.toLowerCase().split('-')[0]; // Get the language code without region
1373+
1374+
if (val[lang] !== undefined) { val = val[lang]; }
1375+
1376+
else {
1377+
1378+
val = val['en'];
1379+
1380+
if (val[lang] !== undefined) {
1381+
1382+
val = Object.values(val).find(function (v) { return typeof v === 'string'; }) || '';
1383+
}
1384+
}
1385+
}
1386+
1387+
this.innerHTML = val;
1388+
1389+
// I am alive!
1390+
return val === undefined ? false : true;
1391+
}
13561392

13571393

13581394
function updateAttributeBinding(isEnabled, attributeName, attributeValue = '') {
@@ -1389,29 +1425,42 @@
13891425
}
13901426
function resolveTemplateLiterals(templateValue, isValueInverted) {
13911427

1392-
if (templateValue.indexOf('${')) {
1428+
if (templateValue.indexOf('${') > -1) {
1429+
1430+
var isEmpty = false;
13931431

13941432
templateValue = templateValue.replace(/\$\{([^\}]+)\}/g, function (match, propertyPath) {
13951433

1396-
var value = this.contextValue(),
1397-
propertyParts = propertyPath.split('.');
1434+
var value = this.contextValue();
13981435

1399-
if (value === undefined) { return ''; }
1436+
if (value === undefined) {
1437+
1438+
if (isValueInverted) { isEmpty = true; }
1439+
1440+
return '';
1441+
}
1442+
1443+
var propertyParts = propertyPath.split('.');
14001444

14011445
while (propertyParts.length) {
14021446

14031447
value = value[propertyParts.shift()];
14041448

1405-
if (value === undefined) { return ''; }
1406-
}
1449+
if (value === undefined) {
14071450

1408-
if (isValueInverted) { value = !value; }
1451+
if (isValueInverted) { isEmpty = true; }
1452+
1453+
return '';
1454+
}
1455+
}
14091456

14101457
return value;
14111458

14121459
}.bind(this));
14131460
}
14141461

1462+
if (isValueInverted && isEmpty) { return ''; }
1463+
14151464
return templateValue;
14161465
}
14171466

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "data-context-binding",
33
"description": "Simple and lightweight solution for binding data to DOM elements.",
4-
"version": "2.0.0-rc.2",
4+
"version": "2.0.0-rc.3",
55
"license": "MIT",
66
"keywords": [
77
"conextra",

0 commit comments

Comments
 (0)