Nessuna descrizione
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

card-boxes.html 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <template id="card-box">
  2. <style>
  3. :host {
  4. display: inline-block;
  5. vertical-align: top;
  6. }
  7. .box {
  8. width: 100%;
  9. height: 100%;
  10. border: 2px solid;
  11. box-sizing: border-box;
  12. vertical-align: middle;
  13. text-align: center;
  14. display: flex;
  15. justify-content: center;
  16. align-content: center;
  17. flex-direction: column;
  18. }
  19. #box2 {
  20. display: flex;
  21. justify-content: center;
  22. align-content: center;
  23. flex-direction: row;
  24. }
  25. </style>
  26. <div class="box">
  27. <div id="box2">
  28. <content></content>
  29. </div>
  30. </div>
  31. </template>
  32. <template id="card-boxes">
  33. <style>
  34. :host {
  35. width: 100%;
  36. vertical-align: top;
  37. }
  38. ::content card-box + card-box {
  39. padding-left: 2px;
  40. }
  41. </style>
  42. <style id="box-size">
  43. ::content card-box {
  44. width:2.5em;
  45. height:2.5em;
  46. }
  47. </style>
  48. <content></content>
  49. </template>
  50. <script>
  51. (function (window, document) {
  52. var elemName = 'card-box';
  53. var mainDoc = document;
  54. var importDoc = document.currentScript.ownerDocument;
  55. var proto = Object.create(HTMLElement.prototype);
  56. proto.createdCallback = function () {
  57. var template = importDoc.getElementById(elemName);
  58. var clone = mainDoc.importNode(template.content, true);
  59. var root = this.createShadowRoot();
  60. root.appendChild(clone);
  61. }
  62. mainDoc.registerElement(elemName, { prototype: proto });
  63. })(window, document);
  64. (function(window, document) {
  65. var elemName = 'card-boxes';
  66. var mainDoc = document;
  67. var importDoc = document.currentScript.ownerDocument;
  68. function createBoxes(element, count) {
  69. while (element.firstChild) {
  70. element.removeChild(element.firstChild);
  71. }
  72. for (var i = 0; i < count; ++i) {
  73. element.appendChild(document.createElement("card-box"));
  74. }
  75. }
  76. var proto = Object.create(HTMLElement.prototype);
  77. Object.defineProperty(proto, "size", {
  78. get: function () {
  79. return "";
  80. },
  81. set: function (value) {
  82. var root = this.shadowRoot;
  83. var style = root.getElementById("box-size");
  84. style.innerHTML = "\
  85. ::content card-box {\
  86. width:" + value + "em;\
  87. height:" + value + "em;\
  88. }\
  89. ";
  90. }
  91. });
  92. Object.defineProperty(proto, "count", {
  93. get: function () {
  94. return "";
  95. },
  96. set: function (value) {
  97. createBoxes(this, value);
  98. }
  99. });
  100. proto.createdCallback = function () {
  101. var template = importDoc.getElementById(elemName);
  102. var clone = mainDoc.importNode(template.content, true);
  103. var root = this.createShadowRoot();
  104. root.appendChild(clone);
  105. this.size = this.getAttribute('size') || "1";
  106. this.count = this.getAttribute('count') || "1";
  107. }
  108. proto.attributeChangedCallback = function (name, oldValue, newValue) {
  109. switch (name) {
  110. case "size": this.size = newValue; break;
  111. case "count": this.count = newValue; break;
  112. }
  113. }
  114. mainDoc.registerElement(elemName, { prototype: proto });
  115. })(window, document);
  116. </script>