暫無描述
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-dndstats.html 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <template id="card-dndstats">
  2. <style>
  3. :host {
  4. width: 100%;
  5. }
  6. table {
  7. width: 100%;
  8. border-spacing: 0;
  9. }
  10. th {
  11. text-align: center;
  12. padding: 0;
  13. /*width: 16.666%;*/
  14. }
  15. th.hidden, td.hidden {
  16. display: none;
  17. }
  18. td {
  19. text-align: center;
  20. padding: 0;
  21. color: black;
  22. }
  23. </style>
  24. <table>
  25. <tbody>
  26. <tr id="headers">
  27. </tr>
  28. <tr id="values">
  29. </tr>
  30. </tbody>
  31. </table>
  32. </template>
  33. <script>
  34. (function(window, document) {
  35. var elemName = 'card-dndstats';
  36. var mainDoc = document;
  37. var importDoc = document.currentScript.ownerDocument;
  38. // Definition of stats
  39. statNames = ["str", "dex", "con", "int", "wis", "cha"];
  40. function getStatMod (value) {
  41. var modValue = Math.floor((value - 10) / 2);
  42. if (modValue >= 0) {
  43. return "+" + modValue;
  44. } else {
  45. return "" + modValue;
  46. }
  47. }
  48. function getStatString (value, mode) {
  49. switch (mode) {
  50. case "both": return "" + value + "&nbsp;(" + getStatMod(value) + ")";
  51. case "mod": return getStatMod(value);
  52. case "stat": return "" + value;
  53. default: return "" + value;
  54. }
  55. }
  56. function updateStats (element) {
  57. for (var i = 0; i < statNames.length; ++i) {
  58. updateStat(element, statNames[i]);
  59. }
  60. }
  61. function updateStat(element, name) {
  62. var value = element.stats[name];
  63. var root = element.shadowRoot;
  64. var td = root.getElementById(name + "-value");
  65. var th = root.getElementById(name + "-header");
  66. if (value === null) {
  67. td.className = "hidden";
  68. th.className = "hidden";
  69. } else {
  70. td.className = "";
  71. th.className = "";
  72. if (value !== "" && value >= 0) {
  73. td.innerHTML = getStatString(value, element.mode);
  74. } else {
  75. td.innerHTML = value;
  76. }
  77. }
  78. }
  79. var proto = Object.create(HTMLTableElement.prototype);
  80. Object.defineProperty(proto, "stats", {
  81. value: {str:0, dex:0, con:0, int:0, wis:0, cha:0},
  82. writable: true,
  83. configurable: false,
  84. enumerable: false
  85. });
  86. Object.defineProperty(proto, "mode", {
  87. value: "both",
  88. writable: true,
  89. configurable: false,
  90. enumerable: false
  91. });
  92. proto.createdCallback = function () {
  93. var template = importDoc.getElementById(elemName);
  94. var clone = mainDoc.importNode(template.content, true);
  95. var root = this.createShadowRoot();
  96. root.appendChild(clone);
  97. // Create all stat columns
  98. var tr1 = root.getElementById("headers");
  99. var tr2 = root.getElementById("values");
  100. for (var i = 0; i < statNames.length; ++i) {
  101. var stat = statNames[i];
  102. var header = document.createElement("th");
  103. header.innerHTML = stat.toUpperCase();
  104. header.id = stat + "-header";
  105. tr1.appendChild(header);
  106. var value = document.createElement("td");
  107. value.innerHTML = stat.toUpperCase();
  108. value.id = stat + "-value";
  109. tr2.appendChild(value);
  110. }
  111. // Update the initial content of the stats
  112. this.mode = this.getAttribute("mode") || "both";
  113. for (var i = 0; i < statNames.length; ++i) {
  114. var stat = statNames[i];
  115. this.stats[stat] = this.getAttribute(stat);
  116. }
  117. updateStats(this);
  118. }
  119. proto.attributeChangedCallback = function (name, oldValue, newValue) {
  120. if (name == "mode") {
  121. this.mode = newValue;
  122. updateStats(this);
  123. }
  124. for (var i = 0; i < statNames.length; ++i) {
  125. var stat = statNames[i];
  126. if (stat == name) {
  127. this.stats[stat] = newValue;
  128. updateStat(this, stat);
  129. break;
  130. }
  131. }
  132. }
  133. mainDoc.registerElement(elemName, { prototype: proto });
  134. })(window, document);
  135. </script>