Skip to content

Commit

Permalink
feat: Observer
Browse files Browse the repository at this point in the history
  • Loading branch information
herrero-tranquilo committed Aug 22, 2021
1 parent e1debfa commit 42c73d9
Show file tree
Hide file tree
Showing 3 changed files with 221 additions and 113 deletions.
1 change: 1 addition & 0 deletions JS/src/objectOrientationProgramming/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<section id="target" data-viewmodel="wrapper">
<h2 data-viewmodel="title"></h2>
<section data-viewmodel="contents"></section>
<input type="text" data-viewmodel="input">
</section>
</body>
</html>
102 changes: 59 additions & 43 deletions JS/src/objectOrientationProgramming/index.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,61 @@
const { type, ViewModel, BinderItem, Binder, Processor, Scanner } = require("./util.js");
const {
ViewModel,
Processor,
Scanner,
} = require("./util.js");

(() => {
const scanner = new Scanner();
const binder = scanner.scan(document.querySelector("#target"));
binder.addProcessor(
new (class extends Processor {
_process(vm, el, k, v) {
el.style[k] = v;
}
})("styles"),
);
binder.addProcessor(
new (class extends Processor {
_process(vm, el, k, v) {
el.setAttribute(k, v);
}
})("attributes"),
);
binder.addProcessor(
new (class extends Processor {
_process(vm, el, k, v) {
el[k] = v;
}
})("properties"),
);
binder.addProcessor(
new (class extends Processor {
_process(vm, el, k, v) {
el["on" + k] = (e) => v.call(el, e, vm);
}
})("events"),
);
const viewmodel = ViewModel.get({
isStop: false,
changeContents() {
this.wrapper.styles.background = `rgb(
${parseInt(Math.random() * 150) + 100},
${parseInt(Math.random() * 150) + 100},
${parseInt(Math.random() * 150) + 100}
)`;
this.wrapper.styles.background = `rgb(${
parseInt(Math.random() * 150) + 100
},${parseInt(Math.random() * 150) + 100},${
parseInt(Math.random() * 150) + 100
})`;
this.contents.properties.innerHTML = Math.random()
.toString(16)
.replace(".", "");
},
wrapper: ViewModel.get({
isStop: false,
styles: {
width: "50%",
background: "#ffa",
cursor: "pointer",
},
events: {
click(e, vm) {
vm.isStop = true;
vm.parent.isStop = true;
},
},
}),
Expand All @@ -32,46 +66,28 @@ const { type, ViewModel, BinderItem, Binder, Processor, Scanner } = require("./u
}),
contents: ViewModel.get({
properties: {
innerHTML: "contents",
innerHTML: "Contents",
},
}),
input: ViewModel.get({
properties: {
value: "Title",
},
events: {
input(e, vm) {
if (e.isComposing) return;
vm.properties.value = e.target.value;
},
blur(e, vm) {
vm.properties.value = e.target.value.trim();
},
},
}),
});
const scanner = new Scanner();
const binder = scanner.scan(document.querySelector("#target"));

binder.addProcessor(
new (class extends Processor {
_process(vm, el, k, v) {
el.style[k] = v;
}
})("styles"),
);
binder.addProcessor(
new (class extends Processor {
_process(vm, el, k, v) {
el[k] = v;
}
})("properties"),
);
binder.addProcessor(
new (class extends Processor {
_process(vm, el, k, v) {
el.setAttribute(k, v);
}
})("attributes"),
);
binder.addProcessor(
new (class extends Processor {
_process(vm, el, k, v) {
el["on" + k] = (e) => v.call(el, e, vm);
}
})("events"),
);

binder.watch(viewmodel);
const f = (_) => {
viewmodel.changeContents();
binder.render(viewmodel);
if (!viewmodel.wrapper.isStop) requestAnimationFrame(f);
if (!viewmodel.isStop) requestAnimationFrame(f);
};
requestAnimationFrame(f);
})();
Loading

0 comments on commit 42c73d9

Please sign in to comment.