First version of complete Chakart rules (unfinished). Draft diagramer (missing marks...
[xogo.git] / base_rules.js
index 5eb4d77..ad30c0a 100644 (file)
@@ -415,6 +415,7 @@ export default class ChessRules {
 
     // Graphical (can use variables defined above)
     this.containerId = o.element;
+    this.isDiagram = o.diagram;
     this.graphicalInit();
   }
 
@@ -559,10 +560,22 @@ export default class ChessRules {
   graphicalInit() {
     // NOTE: not window.onresize = this.re_drawBoardElts because scope (this)
     window.onresize = () => this.re_drawBoardElements();
-    this.re_drawBoardElements();
-    this.initMouseEvents();
-    const chessboard =
-      document.getElementById(this.containerId).querySelector(".chessboard");
+    const g_init = () => {
+      this.re_drawBoardElements();
+      if (!this.isDiagram)
+        this.initMouseEvents();
+    };
+    let container = document.getElementById(this.containerId);
+    if (container.getBoundingClientRect().width == 0) {
+      // Element not ready yet
+      let ro = new ResizeObserver(() => {
+        ro.unobserve(container);
+        g_init();
+      });
+      ro.observe(container);
+    }
+    else
+      g_init();
   }
 
   re_drawBoardElements() {
@@ -644,6 +657,7 @@ export default class ChessRules {
   }
 
   setupPieces(r) {
+    // TODO: d_pieces : only markers (for diagrams) / also in rescale()
     if (this.g_pieces) {
       // Refreshing: delete old pieces first
       for (let i=0; i<this.size.x; i++) {
@@ -996,6 +1010,8 @@ export default class ChessRules {
   }
 
   removeListeners() {
+    if (this.isDiagram)
+      return; //no listeners in this case
     if ('onmousedown' in window) {
       this.mouseListeners.forEach(ml => {
         document.removeEventListener(ml.type, ml.listener);