From 7443f662a3701cba37819192a37a67a8a1eaafcc Mon Sep 17 00:00:00 2001
From: Sergey Linev <S.Linev@gsi.de>
Date: Thu, 8 Nov 2018 17:23:12 +0100
Subject: [PATCH] jsroot: several fixes from dev

Provide methods to check if sending via websocket is possible
Fix - correctly handle negative parameter values in TF1/TF2
Fix - clear range flag when drawings are cleared
Fix - do not forget reset pads cache when changing TWebCanvas
---
 etc/http/scripts/JSRootCore.js       |  7 ++++---
 etc/http/scripts/JSRootPainter.js    |  7 ++++++-
 etc/http/scripts/JSRootPainter.v6.js | 25 +++++++++++++++++--------
 etc/http/scripts/JSRootPainter.v7.js |  7 ++++++-
 4 files changed, 33 insertions(+), 13 deletions(-)

diff --git a/etc/http/scripts/JSRootCore.js b/etc/http/scripts/JSRootCore.js
index 7324963d2cf..c079a658b7d 100644
--- a/etc/http/scripts/JSRootCore.js
+++ b/etc/http/scripts/JSRootCore.js
@@ -105,7 +105,7 @@
    JSROOT.nocache = false;
    JSROOT.sources = ['core']; // indicates which major sources were loaded
 
-   JSROOT.openui5src = 'jsroot'; // use in ROOT distribution for local copy of OpenUI5
+   // JSROOT.openui5src = 'jsroot'; // use in ROOT distribution for local copy of OpenUI5
 
    JSROOT.id_counter = 0;
    if (JSROOT.BatchMode === undefined)
@@ -1840,8 +1840,9 @@
                  }
                  if (this.fFormula.fClingParameters && this.fFormula.fParams) {
                     for (var i=0;i<this.fFormula.fParams.length;++i) {
-                       var regex = new RegExp('(\\[' + this.fFormula.fParams[i].first + '\\])', 'g');
-                       _func = _func.replace(regex, this.fFormula.fClingParameters[this.fFormula.fParams[i].second]);
+                       var regex = new RegExp('(\\[' + this.fFormula.fParams[i].first + '\\])', 'g'),
+                           parvalue = this.fFormula.fClingParameters[this.fFormula.fParams[i].second];
+                       _func = _func.replace(regex, (parvalue < 0) ? "(" + parvalue + ")" : parvalue);
                     }
                  }
               }
diff --git a/etc/http/scripts/JSRootPainter.js b/etc/http/scripts/JSRootPainter.js
index 1d41e281c5b..8ab02e88c4e 100644
--- a/etc/http/scripts/JSRootPainter.js
+++ b/etc/http/scripts/JSRootPainter.js
@@ -1799,7 +1799,7 @@
       this.msgqueue.push({ ready: true, msg: _msg, len: _len});
    }
 
-   /** Reserver entry in queue for data, which is not yet decoded.
+   /** Reserve entry in queue for data, which is not yet decoded.
     * @private */
    WebWindowHandle.prototype.ReserveQueueItem = function() {
       if (!this.msgqueue) this.msgqueue = [];
@@ -1841,6 +1841,11 @@
       }
    }
 
+   /** Returns if one can send text message to server. Checks number of send credits */
+   WebWindowHandle.prototype.CanSend = function(numsend) {
+      return (this.cansend >= (numsend || 1));
+   }
+
    /** Send text message via the connection. */
    WebWindowHandle.prototype.Send = function(msg, chid) {
       if (!this._websocket || (this.state<=0)) return false;
diff --git a/etc/http/scripts/JSRootPainter.v6.js b/etc/http/scripts/JSRootPainter.v6.js
index 41309f01edd..d200235adaa 100644
--- a/etc/http/scripts/JSRootPainter.v6.js
+++ b/etc/http/scripts/JSRootPainter.v6.js
@@ -1531,6 +1531,8 @@
       this.CleanupAxes();
       this.CleanXY();
 
+      this.ranges_set = false;
+
       this.xmin = this.xmax = 0;
       this.ymin = this.ymax = 0;
       this.zmin = this.zmax = 0;
@@ -3709,7 +3711,7 @@
          this.CreatePadSvg(true);
       }
 
-      var isanyfound = false;
+      var isanyfound = false, isanyremove = false;
 
       // find and remove painters which no longer exists in the list
       for (var k=0;k<this.painters.length;++k) {
@@ -3724,10 +3726,13 @@
             // remove painter which does not found in the list of snaps
             this.painters.splice(k--,1);
             sub.Cleanup(); // cleanup such painter
+            isanyremove = true;
          }
       }
 
-      // return JSROOT.CallBack(call_back, padpainter);
+      if (isanyremove) {
+         delete this.pads_cache;
+      }
 
       if (!isanyfound) {
          var svg_p = this.svg_pad(this.this_pad_name),
@@ -4500,9 +4505,12 @@
       this.CloseWebsocket(true);
    }
 
-   TCanvasPainter.prototype.SendWebsocket = function(msg, chid) {
-      if (this._websocket)
-         this._websocket.Send(msg, chid);
+   TCanvasPainter.prototype.SendWebsocket = function(msg) {
+      if (!this._websocket) return;
+      if (this._websocket.CanSend())
+         this._websocket.Send(msg);
+      else
+         console.warn("DROP SEND: " + msg);
    }
 
    TCanvasPainter.prototype.CloseWebsocket = function(force) {
@@ -4720,7 +4728,8 @@
    /// method informs that something was changed in the canvas
    /// used to update information on the server (when used with web6gui)
    TCanvasPainter.prototype.ProcessChanges = function(kind, source_pad) {
-      if (!this._websocket) return;
+      // check if we could send at least one message more - for some meaningful actions
+      if (!this._websocket || !this._websocket.CanSend(2)) return;
 
       var msg = (kind == "sbits") ? "STATUSBITS:" + this.GetStatusBits() : "RANGES6:" + this.GetAllRanges();
 
@@ -4754,8 +4763,8 @@
          if (click_pos.dbl) arg.dbl = true;
       }
 
-      if (this._websocket && arg && ischanged)
-         this._websocket.Send("PADCLICKED:" + JSROOT.toJSON(arg));
+      if (arg && ischanged)
+         this.SendWebsocket("PADCLICKED:" + JSROOT.toJSON(arg));
    }
 
    TCanvasPainter.prototype.GetStatusBits = function() {
diff --git a/etc/http/scripts/JSRootPainter.v7.js b/etc/http/scripts/JSRootPainter.v7.js
index 55fb9c6ef2c..175c8c51806 100644
--- a/etc/http/scripts/JSRootPainter.v7.js
+++ b/etc/http/scripts/JSRootPainter.v7.js
@@ -3334,7 +3334,7 @@
          this.CreatePadSvg(true);
       }
 
-      var isanyfound = false;
+      var isanyfound = false, isanyremove = false;
 
       // find and remove painters which no longer exists in the list
       for (var k=0;k<this.painters.length;++k) {
@@ -3348,9 +3348,14 @@
             // remove painter which does not found in the list of snaps
             this.painters.splice(k--,1);
             sub.Cleanup(); // cleanup such painter
+            isanyremove = true;
          }
       }
 
+      if (isanyremove) {
+         delete this.pads_cache;
+      }
+
       if (!isanyfound) {
          var svg_p = this.svg_pad(this.this_pad_name),
              fp = this.frame_painter();
-- 
GitLab