index.vwf.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { h, text,patch } from "$host/lib/ui/superfine.js"
  2. class UserView {
  3. constructor(view) {
  4. this.view = view;
  5. this.init();
  6. }
  7. init() {
  8. let self = this;
  9. vwf_view.initializedNode = function (nodeID, childID, childExtendsID, childImplementsIDs,
  10. childSource, childType, childIndex, childName) {
  11. if (childID == vwf_view.kernel.application()) {
  12. ["time", "clicks", "random"].forEach(name => {
  13. let el = document.createElement(name);
  14. el.setAttribute("id", name);
  15. document.querySelector("body").appendChild(el);
  16. })
  17. self.satTime(0);
  18. self.satClicks(0);
  19. self.satRandom(0);
  20. }
  21. }
  22. vwf_view.satProperty = function (nodeID, propertyName, propertyValue) {
  23. if (propertyValue === undefined || propertyValue == null) {
  24. return;
  25. }
  26. //let el = document.querySelector("[id='" + nodeID + "']");
  27. if (!document.getElementById("time"))
  28. return
  29. if (propertyName == 'timeCount') {
  30. self.satTime(propertyValue);
  31. }
  32. if(propertyName == 'clicks'){
  33. self.satClicks(propertyValue)
  34. }
  35. if(propertyName == 'randomNumber'){
  36. self.satRandom(propertyValue);
  37. //update body color
  38. let randomColor = Math.floor(parseFloat(propertyValue)*16777215).toString(16);
  39. document.body.style.backgroundColor = "#" + randomColor;
  40. }
  41. }
  42. }
  43. satTime(state) {
  44. patch(
  45. document.getElementById("time"),
  46. h("time", {style: "position: absolute; top: 100px; margin-left: 20px;" }, [
  47. h("h2", {}, text('Time: ')),
  48. h("h1", {}, text(Math.floor(state)))
  49. ]))
  50. }
  51. satClicks(state) {
  52. patch(
  53. document.getElementById("clicks"),
  54. h("clicks", {style: "position: absolute; top: 240px; margin-left: 20px;"}, [
  55. h("h2", {}, text('Clicks: ')),
  56. h("h1", {}, text(state)),
  57. h("button", { onclick: () => vwf_view.kernel.callMethod(vwf.application(), "incClicks") }, text("+")),
  58. h("button", { onclick: () => vwf_view.kernel.callMethod(vwf.application(), "decClicks") }, text("-"))
  59. ])
  60. )
  61. }
  62. satRandom(state) {
  63. patch(
  64. document.getElementById("random"),
  65. h("random", {style: "position: absolute; top: 400px; margin-left: 20px;"}, [
  66. h("h2", {}, text('Random number: ')),
  67. h("h1", {}, text(state)),
  68. h("button", { onclick: () => vwf_view.kernel.callMethod(vwf.application(), "getRandom") }, text("Generate"))
  69. ])
  70. )
  71. }
  72. }
  73. export {UserView as default}