index.html 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <title>Boulier</title>
  7. <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@picocss/pico@1/css/pico.min.css">
  8. <link rel="stylesheet" href="boulier.css">
  9. </head>
  10. <body>
  11. <style>
  12. #reset {
  13. text-align: center;
  14. margin-top: 1em;
  15. }
  16. #color-picker {
  17. text-align: center;
  18. margin-top: 2em;
  19. }
  20. .hide {
  21. display: none;
  22. }
  23. </style>
  24. <main class="container">
  25. <div id="setup" class="hide">
  26. <article>
  27. <form>
  28. <label for="nb-line">
  29. Nombre de ligne
  30. <input type="number" id="nb-line" name="nb-line" required>
  31. </label>
  32. <label for="nb-ball">
  33. Nombre de boules
  34. <input type="number" id="nb-ball" name="nb-ball" required>
  35. </label>
  36. </form>
  37. <button onclick="setup()">Valider</button>
  38. </article>
  39. </div>
  40. <div id="boulier"></div>
  41. <div id="color-picker"></div>
  42. <div id="reset" class="hide"><a href="#" onclick="boulier.reset();boulier.render()">Reset</a>&nbsp;|&nbsp;<a href="#" onclick="Boulier.clear();location.reload()">Clear</a></div>
  43. </main>
  44. <script src="boulier.js"></script>
  45. <script>
  46. let boulier;
  47. if (Boulier.isSaved()) {
  48. boulier = Boulier.load(document.getElementById('boulier'));
  49. renderAll();
  50. } else {
  51. document.getElementById('setup').classList.remove('hide');
  52. }
  53. /* --- Setup --- */
  54. function setup() {
  55. document.getElementById('setup').classList.add('hide');
  56. const nbLine = parseInt(document.getElementById('nb-line').value);
  57. const nbBall = parseInt(document.getElementById('nb-ball').value);
  58. boulier = Boulier.setup(document.getElementById('boulier'), nbLine, nbBall);
  59. boulier.save();
  60. renderAll();
  61. }
  62. function renderAll() {
  63. document.getElementById('reset').classList.remove('hide');
  64. boulier.render();
  65. boulier.onChange(boulier.save);
  66. renderColor();
  67. }
  68. /* --- Color picker --- */
  69. function renderColor() {
  70. let html = '';
  71. for (let i = 0; i < boulier.color.length; i++) {
  72. const line = boulier.color[i];
  73. html += '<div>Ligne ' + (i+1) + ' :</div><div class="grid"><div><div role="button" data-line="' + i + '" onclick="changeNbColor(this, -1)"' + ((line.length < 2) ? ' disabled' : '') + '>-</div></div>';
  74. for(let j = 0; j < line.length; j++) {
  75. html += '<div><select id="color-' + i + '-' + j + '" data-line="' + i + '" data-pos="' + j + '" onchange="changeColor(this)">';
  76. for (const color in Boulier.colors) {
  77. html += '<option value="' + color + '"' + ((color == line[j]) ? ' selected' : '') + '>' + Boulier.colors[color] + '</option>';
  78. }
  79. html += '</select></div>';
  80. }
  81. html += '<div><div role="button" data-line="' + i + '" onclick="changeNbColor(this, 1)"' + ((line.length >= boulier._ball) ? ' disabled' : '') + '>+</div></div></div>';
  82. }
  83. document.getElementById('color-picker').innerHTML = html;
  84. }
  85. function changeNbColor(elt, nb) {
  86. const line = parseInt(elt.getAttribute('data-line'));
  87. if (nb > 0) {
  88. for (let i = 0; i < nb; i++) {
  89. boulier.color[line].push('red');
  90. }
  91. } else if (nb < 0) {
  92. for (let i = 0; i < -nb; i++) {
  93. boulier.color[line].pop();
  94. }
  95. }
  96. boulier.save();
  97. boulier.render();
  98. renderColor();
  99. }
  100. function changeColor(elt) {
  101. const line = parseInt(elt.getAttribute('data-line'));
  102. const pos = parseInt(elt.getAttribute('data-pos'));
  103. boulier.color[line][pos] = elt.value;
  104. boulier.save();
  105. boulier.render();
  106. }
  107. </script>
  108. </body>
  109. </html>