Skip to content
Snippets Groups Projects
Verified Commit 9b04e9c1 authored by Volker Schukai's avatar Volker Schukai :alien:
Browse files

refactor: monitor all attribute changes

parent 86675435
No related branches found
No related tags found
No related merge requests found
...@@ -203,9 +203,9 @@ class CustomElement extends HTMLElement { ...@@ -203,9 +203,9 @@ class CustomElement extends HTMLElement {
this[internalSymbol] = new ProxyObserver({ this[internalSymbol] = new ProxyObserver({
options: initOptionsFromAttributes(this, extend({}, this.defaults)), options: initOptionsFromAttributes(this, extend({}, this.defaults)),
}); });
initAttributeChangeMutationObserver.call(this);
initOptionObserver.call(this);
this[initMethodSymbol](); this[initMethodSymbol]();
initOptionObserver.call(this);
initAttributeChangeMutationObserver.call(this);
} }
/** /**
...@@ -229,6 +229,28 @@ class CustomElement extends HTMLElement { ...@@ -229,6 +229,28 @@ class CustomElement extends HTMLElement {
return [ATTRIBUTE_OPTIONS, ATTRIBUTE_DISABLED]; return [ATTRIBUTE_OPTIONS, ATTRIBUTE_DISABLED];
} }
/**
*
* @param attribute
* @param callback
* @returns {Monster.DOM.CustomElement}
*/
addAttributeObserver(attribute, callback) {
validateFunction(callback);
this[attributeObserverSymbol][attribute] = callback;
return this;
}
/**
*
* @param attribute
* @returns {Monster.DOM.CustomElement}
*/
removeAttributeObserver(attribute) {
delete this[attributeObserverSymbol][attribute];
return this;
}
/** /**
* Derived classes can override and extend this method as follows. * Derived classes can override and extend this method as follows.
* *
...@@ -611,14 +633,6 @@ class CustomElement extends HTMLElement { ...@@ -611,14 +633,6 @@ class CustomElement extends HTMLElement {
function initAttributeChangeMutationObserver() { function initAttributeChangeMutationObserver() {
const self = this; const self = this;
if (self[attributeObserverSymbol] === undefined) {
self[attributeObserverSymbol] = {};
}
if(Object.keys(self[attributeObserverSymbol]).length === 0) {
return;
}
new MutationObserver(function (mutations) { new MutationObserver(function (mutations) {
for (const mutation of mutations) { for (const mutation of mutations) {
if (mutation.type === "attributes") { if (mutation.type === "attributes") {
...@@ -628,7 +642,6 @@ function initAttributeChangeMutationObserver() { ...@@ -628,7 +642,6 @@ function initAttributeChangeMutationObserver() {
}).observe(self, { }).observe(self, {
attributes: true, attributes: true,
attributeOldValue: true, attributeOldValue: true,
attributeFilter: Object.keys(self[attributeObserverSymbol]),
}); });
} }
......
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Popper Button</title>
<script src="main.mjs" type="module"></script>
</head>
<body>
<a href="../">Back</a>
<main>
<h1>Updater</h1>
<script type="application/json" data-monster-role="translations">
{
"theListContainsNoEntries": "translation1",
"key2": {
"other": "translation2"
},
"multi": {
"two": "translation3"
}
}
</script>
<monster-1>Monster1</monster-1>
</main>
</body>
</html>
import {Fetch} from '../../../application/source/i18n/providers/fetch.mjs';
import {Updater} from '../../../application/source/dom/updater.mjs';
import {
attributeObserverSymbol,
CustomElement,
registerCustomElement
} from '../../../application/source/dom/customelement.mjs';
import {domReady} from '../../../application/source/dom/ready.mjs';
import {Embed} from '../../../application/source/i18n/providers/embed.mjs';
import {ATTRIBUTE_OPTIONS_SELECTOR} from "../../../application/source/monster.mjs";
const initMethodSymbol = Symbol.for("@schukai/monster/dom/@@initMethodSymbol");
class Monster1 extends CustomElement {
constructor() {
super();
}
get defaults() {
return Object.assign(
{},
super.defaults,
{
a: {
b: {
c: "d"
}
}
});
};
[initMethodSymbol]() {
self = this;
console.log("initMethodSymbol");
this[attributeObserverSymbol]["data-monster-option-a-b-c"] = (value) => {
console.log("data-monster-option-a-b-c", value);
self.setOption("a.b.c", value);
};
}
/**
*
* @return {string}
*/
static getTag() {
return "monster-1";
}
}
domReady.then(() => {
const m1 = document.querySelector("monster-1");
console.log(m1);
window.m1 = m1;
// read from scritp tag with id i18n
});
registerCustomElement(Monster1);
{
"key1": "value1",
"key2": "value2"
}
\ No newline at end of file
<!DOCTYPE html> <!doctype html>
<html lang="en"> <html lang="en">
<head> <head>
<script type="module" src="main.mjs"></script> <meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Popper Button</title>
<script src="main.mjs" type="module"></script>
</head> </head>
<body> <body>
<h1>Updater</h1> <a href="../">Back</a>
<main>
<template id="current"> <template id="current">
...@@ -14,6 +18,6 @@ ...@@ -14,6 +18,6 @@
<ul data-monster-insert="current path:a.b" > <ul data-monster-insert="current path:a.b" >
</ul> </ul>
</div> </div>
</main>
</body> </body>
</html> </html>
<!DOCTYPE html> <!doctype html>
<html lang="en"> <html lang="en">
<head> <head>
<script type="module" src="main.mjs"></script> <meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Popper Button</title>
<script src="main.mjs" type="module"></script>
</head> </head>
<body> <body>
<a href="../">Back</a>
<main>
<h1>Updater</h1> <h1>Updater</h1>
<script type="application/json" data-monster-role="translations"> <script type="application/json" data-monster-role="translations">
...@@ -20,5 +25,6 @@ ...@@ -20,5 +25,6 @@
<monster-1>Monster1</monster-1> <monster-1>Monster1</monster-1>
</main>
</body> </body>
</html> </html>
<!DOCTYPE html> <!doctype html>
<html lang="en"> <html lang="en">
<head> <head>
<script type="module" src="updater.js"></script> <meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Popper Button</title>
<script src="main.mjs" type="module"></script>
</head> </head>
<body> <body>
<a href="../">Back</a>
<main>
<h1>Updater</h1> <h1>Updater</h1>
<div data-monster-replace="i18n:key1"></div> <div data-monster-replace="i18n:key1"></div>
<div data-monster-replace="i18n:key2"></div> <div data-monster-replace="i18n:key2"></div>
</main>
</body> </body>
</html> </html>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment