@webtides/element-js 0.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Wrapper for defining custom elements so that registering an element multiple times won't crash
3
+ * @param name for the tag
4
+ * @param constructor for the custom element
5
+ */
6
+ export function defineElement(name, constructor) {
7
+ try {
8
+ customElements.define(name, constructor);
9
+ } catch (e) {
10
+ // console.log('error defining custom element', e);
11
+ }
12
+ }
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Retrieves a translated key from a dictionary on the window object
3
+ * Example: ${i18n('CustomElement.buttonLabel', 'Label')}
4
+ * @param key to be translated
5
+ * @param fallback to be used if key is not defined
6
+ * @return String of the translated key or fallback or original key
7
+ */
8
+ export function i18n(key, fallback) {
9
+ try {
10
+ const translations = window.elementjs.i18n();
11
+ if (translations[key] === undefined) {
12
+ throw 'Translation Missing';
13
+ }
14
+
15
+ return translations[key];
16
+ } catch (err) {
17
+ if (fallback) return fallback;
18
+ else return key;
19
+ }
20
+ }
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Little wrapper function for JSON.stringify() to easily convert objects and arrays to strings
3
+ * to be able to set them as attributes on custom elements
4
+ * @param value to be stringified
5
+ * @return String of the stringified value
6
+ */
7
+ export function toString(value) {
8
+ try {
9
+ return JSON.stringify(value);
10
+ } catch (e) {
11
+ return '';
12
+ }
13
+ }