No Description
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.5KB

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