jsroot
Version:
JavaScript ROOT
1,327 lines (1,189 loc) • 5.83 MB
JavaScript
// https://root.cern/js/ v7.10.1
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.JSROOT = global.JSROOT || {}));
})(this, (function (exports) { 'use strict';
var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
/* eslint-disable @stylistic/js/indent */
/** @summary version id
* @desc For the JSROOT release the string in format 'major.minor.patch' like '7.0.0' */
const version_id = '7.10.1',
/** @summary version date
* @desc Release date in format day/month/year like '14/04/2022' */
version_date = '26/11/2025',
/** @summary version id and date
* @desc Produced by concatenation of {@link version_id} and {@link version_date}
* Like '7.0.0 14/04/2022' */
version = version_id + ' ' + version_date,
/** @summary Is node.js flag
* @private */
nodejs = Boolean((typeof process === 'object') && process.versions?.node && process.versions.v8),
/** @summary internal data
* @private */
internals = {
/** @summary unique id counter, starts from 1 */
id_counter: 1
},
_src = (typeof document === 'undefined' && typeof location === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : typeof document === 'undefined' ? location.href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('jsroot.js', document.baseURI).href)),
_src_dir = '$jsrootsys';
/** @summary Check if argument is a not-null Object
* @private */
function isObject(arg) { return arg && typeof arg === 'object'; }
/** @summary Check if argument is a Function
* @private */
function isFunc(arg) { return typeof arg === 'function'; }
/** @summary Check if argument is a String
* @private */
function isStr(arg) { return typeof arg === 'string'; }
/** @summary Check if object is a Promise
* @private */
function isPromise(obj) { return isObject(obj) && isFunc(obj.then); }
/** @summary Postpone func execution and return result in promise
* @private */
function postponePromise(func, timeout) {
return new Promise(resolveFunc => {
setTimeout(() => {
const res = isFunc(func) ? func() : func;
resolveFunc(res);
}, timeout);
});
}
/** @summary Provide promise in any case
* @private */
function getPromise(obj) { return isPromise(obj) ? obj : Promise.resolve(obj); }
/** @summary Location of JSROOT modules
* @desc Automatically detected and used to dynamically load other modules
* @private */
exports.source_dir = '';
if (_src_dir[0] !== '$')
exports.source_dir = _src_dir;
else if (_src && isStr(_src)) {
let pos = _src.indexOf('modules/core.mjs');
if (pos < 0)
pos = _src.indexOf('build/jsroot.js');
if (pos < 0)
pos = _src.indexOf('build/jsroot.min.js');
if (pos >= 0)
exports.source_dir = _src.slice(0, pos);
else
internals.ignore_v6 = true;
}
if (!nodejs) {
if (exports.source_dir)
console.log(`Set jsroot source_dir to ${exports.source_dir}, ${version}`);
else
console.log(`jsroot bundle, ${version}`);
}
/** @summary Is batch mode flag
* @private */
let batch_mode = nodejs;
/** @summary Indicates if running in batch mode */
function isBatchMode() { return batch_mode; }
/** @summary Set batch mode
* @private */
function setBatchMode(on) { batch_mode = Boolean(on); }
/** @summary Indicates if running inside Node.js */
function isNodeJs() { return nodejs; }
/** @summary atob function in all environments
* @private */
const atob_func = isNodeJs() ? str => Buffer.from(str, 'base64').toString('latin1') : globalThis?.atob,
/** @summary btoa function in all environments
* @private */
btoa_func = isNodeJs() ? str => Buffer.from(str, 'latin1').toString('base64') : globalThis?.btoa,
/** @summary browser detection flags
* @private */
browser = { isFirefox: true, isSafari: false, isChrome: false, isWin: false, touches: false, screenWidth: 1200 };
if ((typeof document !== 'undefined') && (typeof window !== 'undefined') && (typeof navigator !== 'undefined')) {
navigator.userAgentData?.brands?.forEach(item => {
if (item.brand === 'HeadlessChrome') {
browser.isChromeHeadless = true;
browser.chromeVersion = parseInt(item.version);
} else if (item.brand === 'Chromium') {
browser.isChrome = true;
browser.chromeVersion = parseInt(item.version);
}
});
if (browser.chromeVersion) {
browser.isFirefox = false;
browser.isWin = navigator.userAgentData.platform === 'Windows';
} else {
browser.isFirefox = navigator.userAgent.indexOf('Firefox') >= 0;
browser.isSafari = Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0;
browser.isChrome = Boolean(window.chrome);
browser.isChromeHeadless = navigator.userAgent.indexOf('HeadlessChrome') >= 0;
browser.chromeVersion = (browser.isChrome || browser.isChromeHeadless) ? (navigator.userAgent.indexOf('Chrom') > 0 ? parseInt(navigator.userAgent.match(/Chrom(?:e|ium)\/([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)/)[1]) : 134) : 0;
browser.isWin = navigator.userAgent.indexOf('Windows') >= 0;
}
browser.android = /android/i.test(navigator.userAgent);
browser.touches = ('ontouchend' in document); // identify if touch events are supported
browser.screenWidth = window.screen?.width ?? 1200;
}
/** @summary Check if prototype string match to array (typed on untyped)
* @return {Number} 0 - not array, 1 - regular array, 2 - typed array
* @private */
function isArrayProto(proto) {
if ((proto.length < 14) || proto.indexOf('[object '))
return 0;
const p = proto.indexOf('Array]');
if ((p < 0) || (p !== proto.length - 6))
return 0;
// plain array has only '[object Array]', typed array type name inside
return proto.length === 14 ? 1 : 2;
}
/** @desc Specialized JSROOT constants, used in {@link settings}
* @namespace */
const constants$1 = {
/** @summary Kind of 3D rendering, used for {@link settings.Render3D}
* @namespace */
Render3D: {
/** @summary Default 3D rendering, normally WebGL, if not supported - SVG */
Default: 0,
/** @summary Use normal WebGL rendering and place as interactive Canvas element on HTML page */
WebGL: 1,
/** @summary Use WebGL rendering, but convert into svg image, not interactive */
WebGLImage: 2,
/** @summary Use SVG rendering, slow, imprecise and not interactive, not recommended */
SVG: 3,
/** @summary Disable renderer, used for three.js model creation, only for internal use recommended */
None: 4,
fromString(s) {
if ((s === 'webgl') || (s === 'gl'))
return this.WebGL;
if (s === 'img')
return this.WebGLImage;
if (s === 'svg')
return this.SVG;
if (s === 'none')
return this.None;
return this.Default;
}
},
/** @summary Way to embed 3D into SVG, used for {@link settings.Embed3D}
* @namespace */
Embed3D: {
/** @summary Do not embed 3D drawing, use complete space */
NoEmbed: -1,
/** @summary Default embedding mode - on Firefox and latest Chrome is real ```Embed```, on all other ```Overlay``` */
Default: 0,
/** @summary WebGL canvas not inserted into SVG, but just overlay. The only way how old Chrome browser can be used */
Overlay: 1,
/** @summary Really embed WebGL Canvas into SVG */
Embed: 2,
/** @summary Embedding, but when SVG rendering or SVG image conversion is used */
EmbedSVG: 3,
/** @summary Convert string values into number */
fromString(s) {
if (s === 'embed')
return this.Embed;
if (s === 'overlay')
return this.Overlay;
return this.Default;
}
},
/** @summary How to use latex in text drawing, used for {@link settings.Latex}
* @namespace */
Latex: {
/** @summary do not use Latex at all for text drawing */
Off: 0,
/** @summary convert only known latex symbols */
Symbols: 1,
/** @summary normal latex processing with svg */
Normal: 2,
/** @summary use MathJax for complex cases, otherwise simple SVG text */
MathJax: 3,
/** @summary always use MathJax for text rendering */
AlwaysMathJax: 4,
/** @summary Convert string values into number */
fromString(s) {
if (!s || !isStr(s))
return this.Normal;
switch (s) {
case 'off': return this.Off;
case 'symbols': return this.Symbols;
case 'normal':
case 'latex':
case 'exp':
case 'experimental': return this.Normal;
case 'MathJax':
case 'mathjax':
case 'math': return this.MathJax;
case 'AlwaysMathJax':
case 'alwaysmath':
case 'alwaysmathjax': return this.AlwaysMathJax;
}
const code = parseInt(s);
return (Number.isInteger(code) && (code >= this.Off) && (code <= this.AlwaysMathJax)) ? code : this.Normal;
}
}
},
/** @desc Global JSROOT settings
* @namespace */
settings = {
/** @summary Render of 3D drawing methods, see {@link constants.Render3D} for possible values */
Render3D: constants$1.Render3D.Default,
/** @summary 3D drawing methods in batch mode, see {@link constants.Render3D} for possible values */
Render3DBatch: constants$1.Render3D.Default,
/** @summary Way to embed 3D drawing in SVG, see {@link constants.Embed3D} for possible values */
Embed3D: constants$1.Embed3D.Default,
/** @summary Default canvas width */
CanvasWidth: 1200,
/** @summary Default canvas height */
CanvasHeight: 800,
/** @summary Canvas pixel ratio between viewport and display, default 1 */
CanvasScale: 1,
/** @summary Enable or disable tooltips, default on */
Tooltip: !nodejs,
/** @summary Time in msec for appearance of tooltips, 0 - no animation */
TooltipAnimation: 500,
/** @summary Enables context menu usage */
ContextMenu: !nodejs,
/** @summary Global zooming flag, enable/disable any kind of interactive zooming */
Zooming: !nodejs,
/** @summary Zooming with the mouse events */
ZoomMouse: !nodejs,
/** @summary Zooming with mouse wheel */
ZoomWheel: !nodejs,
/** @summary Zooming on touch devices */
ZoomTouch: !nodejs,
/** @summary Enables move and resize of elements like statistic box, title, pave, colz */
MoveResize: !browser.touches && !nodejs,
/** @summary Configures keyboard key press handling
* @desc Can be disabled to prevent keys handling in complex HTML layouts
* @default true */
HandleKeys: !nodejs,
/** @summary enables drag and drop functionality */
DragAndDrop: !nodejs,
/** @summary Interactive dragging of TGraph points */
DragGraphs: true,
/** @summary Value of user-select style in interactive drawings */
UserSelect: 'none',
/** @summary Show progress box, can be false, true or 'modal' */
ProgressBox: !nodejs,
/** @summary Show additional tool buttons on the canvas, false - disabled, true - enabled, 'popup' - only toggle button */
ToolBar: nodejs ? false : 'popup',
/** @summary Position of toolbar 'left' left-bottom corner on canvas, 'right' - right-bottom corner on canvas, opposite on sub-pads */
ToolBarSide: 'left',
/** @summary display tool bar vertical (default false) */
ToolBarVert: false,
/** @summary if drawing inside particular div can be enlarged on full window */
CanEnlarge: true,
/** @summary if frame position can be adjusted to let show axis or colz labels */
CanAdjustFrame: false,
/** @summary calculation of text size consumes time and can be skipped to improve performance (but with side effects on text adjustments) */
ApproxTextSize: false,
/** @summary Load symbol.ttf font to display greek labels. By default font file not loaded and unicode is used */
LoadSymbolTtf: false,
/** @summary Histogram drawing optimization: 0 - disabled, 1 - only for large (>5000 1d bins, >50 2d bins) histograms, 2 - always */
OptimizeDraw: 1,
/** @summary Automatically create stats box, default on */
AutoStat: true,
/** @summary Default frame position in NFC
* @deprecated Use gStyle.fPad[Left/Right/Top/Bottom]Margin values instead, to be removed in v8 */
FrameNDC: {},
/** @summary size of pad, where many features will be deactivated like text draw or zooming */
SmallPad: { width: 150, height: 100 },
/** @summary Default color palette id */
Palette: 57,
/** @summary Configures Latex usage, see {@link constants.Latex} for possible values */
Latex: constants$1.Latex.Normal,
/** @summary Grads per segment in TGeo spherical shapes like tube */
GeoGradPerSegm: 6,
/** @summary Enables faces compression after creation of composite shape */
GeoCompressComp: true,
/** @summary if true, ignore all kind of URL options in the browser URL */
IgnoreUrlOptions: false,
/** @summary how many items shown on one level of hierarchy */
HierarchyLimit: 250,
/** @summary default display kind for the hierarchy painter */
DislpayKind: 'simple',
/** @summary default left area width in browser layout */
BrowserWidth: 250,
/** @summary custom format for all X values, when not specified {@link gStyle.fStatFormat} is used */
XValuesFormat: undefined,
/** @summary custom format for all Y values, when not specified {@link gStyle.fStatFormat} is used */
YValuesFormat: undefined,
/** @summary custom format for all Z values, when not specified {@link gStyle.fStatFormat} is used */
ZValuesFormat: undefined,
/** @summary Let detect and solve problem when server returns wrong Content-Length header
* @desc See [jsroot#189]{@link https://github.com/root-project/jsroot/issues/189} for more info
* Can be enabled by adding 'wrong_http_response' parameter to URL when using JSROOT UI
* @default false */
HandleWrongHttpResponse: false,
/** @summary Tweak browser caching with stamp URL parameter
* @desc When specified, extra URL parameter like ```?stamp=unique_value``` append to each files loaded
* In such case browser will be forced to load file content disregards of browser or server cache settings
* Can be disabled by providing &usestamp=false in URL or via Settings/Files sub-menu
* Disabled by default on node.js, enabled in the web browsers */
UseStamp: !nodejs,
/** @summary Maximal number of bytes ranges in http 'Range' header
* @desc Some http server has limitations for number of bytes ranges therefore let change maximal number via setting
* @default 200 */
MaxRanges: 200,
/** @summary Number of bytes requested once by TTree::Draw processing
* @desc TTree can be very large and data from baskets read by portion specified by this variable
* @default 1e6 */
TreeReadBunchSize: 1e6,
/** @summary File read timeout in ms
* @desc Configures timeout for each http operation for reading ROOT files
* @default 0 */
FilesTimeout: 0,
/** @summary Default remap object for files loading
* @desc Allows to retry files reading if original URL fails
* @private */
FilesRemap: { 'https://root.cern/': 'https://root-eos.web.cern.ch/' },
/** @summary Configure xhr.withCredentials = true when submitting http requests from JSROOT */
WithCredentials: false,
/** @summary Skip streamer infos from the GUI */
SkipStreamerInfos: false,
/** @summary Show only last cycle for objects in TFile */
OnlyLastCycle: false,
/** @summary Configures dark mode for the GUI */
DarkMode: false,
/** @summary Prefer to use saved points in TF1/TF2, avoids eval() and Function() when possible */
PreferSavedPoints: false,
/** @summary Angle in degree for axis labels tilt when available space is not enough */
AxisTiltAngle: 25,
/** @summary Strip axis labels trailing 0 or replace 10^0 by 1 */
StripAxisLabels: true,
/** @summary If true exclude (cut off) axis labels which may exceed graphical range, also axis name can be specified */
CutAxisLabels: false,
/** @summary Draw TF1 by default as curve or line */
FuncAsCurve: false,
/** @summary Time zone used for date/time display, local by default, can be 'UTC' or 'Europe/Berlin' or any other valid value */
TimeZone: '',
/** @summary Page URL which will be used to show item in new tab, jsroot main dir used by default */
NewTabUrl: '',
/** @summary Extra parameters which will be append to the url when item shown in new tab */
NewTabUrlPars: '',
/** @summary Export different settings in output URL */
NewTabUrlExportSettings: false,
/** @summary Enable more debug output, also via 'WebGui.Debug: yes' on ROOT side */
Debug: false
},
/** @namespace
* @summary Instance of TStyle object like in ROOT
* @desc Includes default draw styles, can be changed after loading of core.mjs
* or can be load from the file providing style=itemname in the URL
* See [TStyle docu]{@link https://root.cern/doc/master/classTStyle.html} 'Private attributes' section for more detailed info about each value */
gStyle = {
fName: 'Modern',
/** @summary Default log x scale */
fOptLogx: 0,
/** @summary Default log y scale */
fOptLogy: 0,
/** @summary Default log z scale */
fOptLogz: 0,
/** @summary Show date on canvas */
fOptDate: 0,
/** @summary Show file name on canvas */
fOptFile: 0,
/** @summary X position of date on canvas */
fDateX: 0.01,
/** @summary Y position of date on canvas */
fDateY: 0.01,
/** @summary Draw histogram title */
fOptTitle: 1,
/** @summary Canvas fill color */
fCanvasColor: 0,
/** @summary Pad fill color */
fPadColor: 0,
/** @summary Pad bottom margin */
fPadBottomMargin: 0.1,
/** @summary Pad top margin */
fPadTopMargin: 0.1,
/** @summary Pad left margin */
fPadLeftMargin: 0.1,
/** @summary Pad right margin */
fPadRightMargin: 0.1,
/** @summary TPad.fGridx default value */
fPadGridX: false,
/** @summary TPad.fGridy default value */
fPadGridY: false,
/** @summary TPad.fTickx default value */
fPadTickX: 0,
/** @summary TPad.fTicky default value */
fPadTickY: 0,
/** @summary Pad border size */
fPadBorderSize: 2,
/** @summary Pad border mode */
fPadBorderMode: 0,
/** @summary Canvas border size */
fCanvasBorderSize: 2,
/** @summary Canvas border mode */
fCanvasBorderMode: 0,
/** @summary fill color for stat box */
fStatColor: 0,
/** @summary fill style for stat box */
fStatStyle: 1000,
/** @summary text color in stat box */
fStatTextColor: 1,
/** @summary text size in stat box */
fStatFontSize: 0,
/** @summary stat text font */
fStatFont: 42,
/** @summary Stat border size */
fStatBorderSize: 1,
/** @summary Printing format for stats */
fStatFormat: '6.4g',
/** @summary Stat box X position - top right corner */
fStatX: 0.98,
/** @summary Stat box Y position - top right corner */
fStatY: 0.935,
/** @summary Stat box width */
fStatW: 0.2,
/** @summary Stat box height */
fStatH: 0.16,
/** @summary Title text align */
fTitleAlign: 23,
/** @summary Title fill color */
fTitleColor: 0,
/** @summary Title text color */
fTitleTextColor: 1,
/** @summary Title border size */
fTitleBorderSize: 0,
/** @summary Title text font */
fTitleFont: 42,
/** @summary Title font size */
fTitleFontSize: 0.05,
/** @summary Title fill style */
fTitleStyle: 0,
/** @summary X position of top left corner of title box */
fTitleX: 0.5,
/** @summary Y position of top left corner of title box */
fTitleY: 0.995,
/** @summary Width of title box */
fTitleW: 0,
/** @summary Height of title box */
fTitleH: 0,
/** @summary Printing format for fit parameters */
fFitFormat: '5.4g',
/** @summary Default optstat value */
fOptStat: 1111,
/** @summary Default optfit value */
fOptFit: 0,
/** @summary Default number of colors in contour */
fNumberContours: 20,
/** @summary Grids color */
fGridColor: 0,
/** @summary Grids line style */
fGridStyle: 3,
/** @summary Grids line width */
fGridWidth: 1,
/** @summary Frame fill color */
fFrameFillColor: 0,
/** @summary Frame fill style */
fFrameFillStyle: 1001,
/** @summary Frame line color */
fFrameLineColor: 1,
/** @summary Frame line width */
fFrameLineWidth: 1,
/** @summary Frame line style */
fFrameLineStyle: 1,
/** @summary Frame border size */
fFrameBorderSize: 1,
/** @summary Frame border mode */
fFrameBorderMode: 0,
/** @summary size in pixels of end error for E1 draw options */
fEndErrorSize: 2,
/** @summary X size of the error marks for the histogram drawings */
fErrorX: 0.5,
/** @summary when true, BAR and LEGO drawing using base = 0 */
fHistMinimumZero: false,
/** @summary Margin between histogram's top and pad's top */
fHistTopMargin: 0.05,
/** @summary Histogram fill color */
fHistFillColor: 0,
/** @summary Histogram fill style */
fHistFillStyle: 1001,
/** @summary Histogram line color */
fHistLineColor: 602,
/** @summary Histogram line style */
fHistLineStyle: 1,
/** @summary Histogram line width */
fHistLineWidth: 1,
/** @summary format for bin content */
fPaintTextFormat: 'g',
/** @summary default time offset, UTC time at 01/01/95 */
fTimeOffset: 788918400,
/** @summary Legend border size */
fLegendBorderSize: 1,
/** @summary Legend text font */
fLegendFont: 42,
/** @summary Legend font size */
fLegendTextSize: 0,
/** @summary Legend fill color */
fLegendFillColor: 0,
/** @summary Legend fill style */
fLegendFillStyle: 1001,
/** @summary Hatches line width in fill styles */
fHatchesLineWidth: 1,
fHatchesSpacing: 1,
fCandleWhiskerRange: 1.0,
fCandleBoxRange: 0.5,
fCandleScaled: false,
fViolinScaled: true,
fCandleCircleLineWidth: 1,
fCandleCrossLineWidth: 1,
fOrthoCamera: false,
fXAxisExpXOffset: 0,
fXAxisExpYOffset: 0,
fYAxisExpXOffset: 0,
fYAxisExpYOffset: 0,
fAxisMaxDigits: 5,
fStripDecimals: true,
/** @summary Width of bar for graphs */
fBarWidth: 1,
/** @summary Offset of bar for graphs */
fBarOffset: 0
};
/** @summary Method returns current document in use
* @private */
function getDocument() {
if (nodejs)
return internals.nodejs_document;
if (typeof document !== 'undefined')
return document;
if (typeof window === 'object')
return window.document;
return undefined;
}
/** @summary Ensure global JSROOT and v6 support methods
* @private */
exports._ensureJSROOT = null;
/** @summary Generate mask for given bit
* @param {number} n bit number
* @return {Number} produced mask
* @private */
function BIT(n) { return 1 << n; }
/** @summary Make deep clone of the object, including all sub-objects
* @return {object} cloned object
* @private */
function clone(src, map, nofunc) {
if (!src)
return null;
if (!map)
map = { obj: [], clones: [], nofunc };
else {
const i = map.obj.indexOf(src);
if (i >= 0)
return map.clones[i];
}
const arr_kind = isArrayProto(Object.prototype.toString.apply(src));
// process normal array
if (arr_kind === 1) {
const tgt = [];
map.obj.push(src);
map.clones.push(tgt);
for (let i = 0; i < src.length; ++i)
tgt.push(isObject(src[i]) ? clone(src[i], map) : src[i]);
return tgt;
}
// process typed array
if (arr_kind === 2) {
const tgt = [];
map.obj.push(src);
map.clones.push(tgt);
for (let i = 0; i < src.length; ++i)
tgt.push(src[i]);
return tgt;
}
const tgt = {};
map.obj.push(src);
map.clones.push(tgt);
for (const k in src) {
if (isObject(src[k]))
tgt[k] = clone(src[k], map);
else if (!map.nofunc || !isFunc(src[k]))
tgt[k] = src[k];
}
return tgt;
}
// used very often - keep shortcut
const extend$1 = Object.assign;
/** @summary Adds specific methods to the object.
* @desc JSROOT implements some basic methods for different ROOT classes.
* @function
* @param {object} obj - object where methods are assigned
* @param {string} [typename] - optional typename, if not specified, obj._typename will be used
* @private */
exports.addMethods = null;
/** @summary Should be used to parse JSON string produced with TBufferJSON class
* @desc Replace all references inside object like { "$ref": "1" }
* @param {object|string} json object where references will be replaced
* @return {object} parsed object */
function parse$1(json) {
if (!json)
return null;
const obj = isStr(json) ? JSON.parse(json) : json, map = [];
let newfmt;
const unref_value = value => {
if ((value === null) || (value === undefined))
return;
if (isStr(value)) {
if (newfmt || (value.length < 6) || value.indexOf('$ref:'))
return;
const ref = parseInt(value.slice(5));
if (!Number.isInteger(ref) || (ref < 0) || (ref >= map.length))
return;
newfmt = false;
return map[ref];
}
if (typeof value !== 'object')
return;
const proto = Object.prototype.toString.apply(value);
// scan array - it can contain other objects
if (isArrayProto(proto) > 0) {
for (let i = 0; i < value.length; ++i) {
const res = unref_value(value[i]);
if (res !== undefined)
value[i] = res;
}
return;
}
const ks = Object.keys(value), len = ks.length;
if ((newfmt !== false) && (len === 1) && (ks[0] === '$ref')) {
const ref = parseInt(value.$ref);
if (!Number.isInteger(ref) || (ref < 0) || (ref >= map.length))
return;
newfmt = true;
return map[ref];
}
if ((newfmt !== false) && (len > 1) && (ks[0] === '$arr') && (ks[1] === 'len')) {
// this is ROOT-coded array
let arr;
switch (value.$arr) {
case 'Int8': arr = new Int8Array(value.len); break;
case 'Uint8': arr = new Uint8Array(value.len); break;
case 'Int16': arr = new Int16Array(value.len); break;
case 'Uint16': arr = new Uint16Array(value.len); break;
case 'Int32': arr = new Int32Array(value.len); break;
case 'Uint32': arr = new Uint32Array(value.len); break;
case 'Float32': arr = new Float32Array(value.len); break;
case 'Int64':
case 'Uint64':
case 'Float64': arr = new Float64Array(value.len); break;
default: arr = new Array(value.len);
}
arr.fill((value.$arr === 'Bool') ? false : 0);
if (value.b !== undefined) {
// base64 coding
const buf = atob_func(value.b);
if (arr.buffer) {
const dv = new DataView(arr.buffer, value.o || 0),
blen = Math.min(buf.length, dv.byteLength);
for (let k = 0; k < blen; ++k)
dv.setUint8(k, buf.charCodeAt(k));
} else
throw new Error('base64 coding supported only for native arrays with binary data');
} else {
// compressed coding
let nkey = 2, p = 0;
while (nkey < len) {
if (ks[nkey][0] === 'p')
p = value[ks[nkey++]]; // position
if (ks[nkey][0] !== 'v')
throw new Error(`Unexpected member ${ks[nkey]} in array decoding`);
const v = value[ks[nkey++]]; // value
if (typeof v === 'object') {
for (let k = 0; k < v.length; ++k)
arr[p++] = v[k];
} else {
arr[p++] = v;
if ((nkey < len) && (ks[nkey][0] === 'n')) {
let cnt = value[ks[nkey++]]; // counter
while (--cnt)
arr[p++] = v;
}
}
}
}
return arr;
}
if ((newfmt !== false) && (len === 3) && (ks[0] === '$pair') && (ks[1] === 'first') && (ks[2] === 'second')) {
newfmt = true;
const f1 = unref_value(value.first),
s1 = unref_value(value.second);
if (f1 !== undefined)
value.first = f1;
if (s1 !== undefined)
value.second = s1;
value._typename = value.$pair;
delete value.$pair;
return; // pair object is not counted in the objects map
}
// prevent endless loop
if (map.indexOf(value) >= 0)
return;
// add object to object map
map.push(value);
// add methods to all objects, where _typename is specified
if (value._typename)
exports.addMethods(value);
for (let k = 0; k < len; ++k) {
const i = ks[k], res = unref_value(value[i]);
if (res !== undefined)
value[i] = res;
}
};
unref_value(obj);
return obj;
}
/** @summary Parse response from multi.json request
* @desc Method should be used to parse JSON code, produced by multi.json request of THttpServer
* @param {string} json string to parse
* @return {Array} array of parsed elements */
function parseMulti(json) {
if (!json)
return null;
const arr = JSON.parse(json);
if (arr?.length) {
for (let i = 0; i < arr.length; ++i)
arr[i] = parse$1(arr[i]);
}
return arr;
}
/** @summary Method converts JavaScript object into ROOT-like JSON
* @desc When performed properly, JSON can be used in [TBufferJSON::fromJSON()]{@link https://root.cern/doc/master/classTBufferJSON.html}
* method to read data back with C++. Or one can again parse json with {@link parse} function
* @param {object} obj - JavaScript object to convert
* @param {number} [spacing] - optional line spacing in JSON
* @return {string} produced JSON code
* @example
* import { openFile, draw, toJSON } from 'https://root.cern/js/latest/modules/main.mjs';
* let file = await openFile('https://root.cern/js/files/hsimple.root');
* let obj = await file.readObject('hpxpy;1');
* obj.fTitle = 'New histogram title';
* let json = toJSON(obj); */
function toJSON(obj, spacing) {
if (!isObject(obj))
return '';
const map = [], // map of stored objects
copy_value = value => {
if (isFunc(value))
return undefined;
if ((value === undefined) || (value === null) || !isObject(value))
return value;
// typed array need to be converted into normal array, otherwise looks strange
if (isArrayProto(Object.prototype.toString.apply(value)) > 0) {
const arr = new Array(value.length);
for (let i = 0; i < value.length; ++i)
arr[i] = copy_value(value[i]);
return arr;
}
// this is how reference is code
const refid = map.indexOf(value);
if (refid >= 0)
return { $ref: refid };
const ks = Object.keys(value), len = ks.length, tgt = {};
if ((len === 3) && (ks[0] === '$pair') && (ks[1] === 'first') && (ks[2] === 'second')) {
// special handling of pair objects which does not included into objects map
tgt.$pair = value.$pair;
tgt.first = copy_value(value.first);
tgt.second = copy_value(value.second);
return tgt;
}
map.push(value);
for (let k = 0; k < len; ++k) {
const name = ks[k];
if (name && (name[0] !== '$'))
tgt[name] = copy_value(value[name]);
}
return tgt;
},
tgt = copy_value(obj);
return JSON.stringify(tgt, null, spacing);
}
/** @summary decodes URL options after '?' mark
* @desc Following options supported ?opt1&opt2=3
* @param {string} [url] URL string with options, document.URL will be used when not specified
* @return {Object} with ```.has(opt)``` and ```.get(opt,dflt)``` methods
* @example
* import { decodeUrl } from 'https://root.cern/js/latest/modules/core.mjs';
* let d = decodeUrl('any?opt1&op2=3');
* console.log(`Has opt1 ${d.has('opt1')}`); // true
* console.log(`Get opt1 ${d.get('opt1')}`); // ''
* console.log(`Get opt2 ${d.get('opt2')}`); // '3'
* console.log(`Get opt3 ${d.get('opt3','-')}`); // '-' */
function decodeUrl(url) {
const res = {
opts: {},
has(opt) { return this.opts[opt] !== undefined; },
get(opt, dflt) { return this.opts[opt] ?? dflt; }
};
if (!url || !isStr(url)) {
if (settings.IgnoreUrlOptions || (typeof document === 'undefined'))
return res;
url = document.URL;
}
res.url = url;
const p1 = url.indexOf('?');
if (p1 < 0)
return res;
url = decodeURI(url.slice(p1 + 1));
while (url) {
// try to correctly handle quotes in the URL
let pos = 0, nq = 0, eq = -1, firstq = -1;
while ((pos < url.length) && (nq || ((url[pos] !== '&') && (url[pos] !== '#')))) {
switch (url[pos]) {
case '\'':
if (nq >= 0)
nq = (nq + 1) % 2;
if (firstq < 0)
firstq = pos;
break;
case '"':
if (nq <= 0)
nq = (nq - 1) % 2;
if (firstq < 0)
firstq = pos;
break;
case '=':
if ((firstq < 0) && (eq < 0))
eq = pos;
break;
}
pos++;
}
if ((eq < 0) && (firstq < 0))
res.opts[url.slice(0, pos)] = '';
else if (eq > 0) {
let val = url.slice(eq + 1, pos);
if (((val[0] === '\'') || (val[0] === '"')) && (val.at(0) === val.at(-1)))
val = val.slice(1, val.length - 1);
res.opts[url.slice(0, eq)] = val;
}
if ((pos >= url.length) || (url[pos] === '#'))
break;
url = url.slice(pos + 1);
}
return res;
}
/** @summary Find function with given name
* @private */
function findFunction(name) {
if (isFunc(name))
return name;
if (!isStr(name))
return null;
const names = name.split('.');
let elem = globalThis;
for (let n = 0; elem && (n < names.length); ++n)
elem = elem[names[n]];
return isFunc(elem) ? elem : null;
}
/** @summary Method to create http request, without promise can be used only in browser environment
* @private */
function createHttpRequest(url, kind, user_accept_callback, user_reject_callback, use_promise) {
function configureXhr(xhr) {
xhr.http_callback = isFunc(user_accept_callback) ? user_accept_callback.bind(xhr) : () => {};
xhr.error_callback = isFunc(user_reject_callback) ? user_reject_callback.bind(xhr) : function(err) {
console.warn(err.message);
this.http_callback(null);
}.bind(xhr);
if (!kind)
kind = 'buf';
let method = 'GET', is_async = true;
const p = kind.indexOf(';sync');
if (p > 0) {
kind = kind.slice(0, p);
is_async = false;
}
switch (kind) {
case 'head':
method = 'HEAD';
break;
case 'posttext':
method = 'POST';
kind = 'text';
break;
case 'postbuf':
method = 'POST';
kind = 'buf';
break;
case 'post':
case 'multi':
method = 'POST';
break;
}
xhr.kind = kind;
if (settings.WithCredentials)
xhr.withCredentials = true;
if (settings.HandleWrongHttpResponse && (method === 'GET') && isFunc(xhr.addEventListener)) {
xhr.addEventListener('progress', function(oEvent) {
if (oEvent.lengthComputable && this.expected_size && (oEvent.loaded > this.expected_size)) {
this.did_abort = true;
this.abort();
this.error_callback(Error(`Server sends more bytes ${oEvent.loaded} than expected ${this.expected_size}. Abort I/O operation`), 598);
}
}.bind(xhr));
}
xhr.onreadystatechange = function() {
if (this.did_abort)
return;
if ((this.readyState === 2) && this.expected_size) {
const len = parseInt(this.getResponseHeader('Content-Length'));
if (Number.isInteger(len) && (len > this.expected_size) && !settings.HandleWrongHttpResponse) {
this.did_abort = 'large';
this.abort();
return this.error_callback(Error(`Server response size ${len} larger than expected ${this.expected_size}. Abort I/O operation`), 599);
}
}
if (this.readyState !== 4)
return;
if ((this.status !== 200) && (this.status !== 206) && !browser.qt6 &&
// in these special cases browsers not always set status
!((this.status === 0) && ((url.indexOf('file://') === 0) || (url.indexOf('blob:') === 0))))
return this.error_callback(Error(`Fail to load url ${url}`), this.status);
if (this.nodejs_checkzip && (this.getResponseHeader('content-encoding') === 'gzip')) {
// special handling of gzip JSON objects in Node.js
return Promise.resolve().then(function () { return _rollup_plugin_ignore_empty_module_placeholder$1; }).then(handle => {
const res = handle.unzipSync(Buffer.from(this.response)),
obj = JSON.parse(res); // zlib returns Buffer, use JSON to parse it
return this.http_callback(parse$1(obj));
});
}
switch (this.kind) {
case 'xml': return this.http_callback(this.responseXML);
case 'text': return this.http_callback(this.responseText);
case 'object': return this.http_callback(parse$1(this.responseText));
case 'multi': return this.http_callback(parseMulti(this.responseText));
case 'head': return this.http_callback(this);
}
// if no response type is supported, return as text (most probably, will fail)
if (this.responseType === undefined)
return this.http_callback(this.responseText);
if ((this.kind === 'bin') && ('byteLength' in this.response)) {
// if string representation in requested - provide it
const u8Arr = new Uint8Array(this.response);
let filecontent = '';
for (let i = 0; i < u8Arr.length; ++i)
filecontent += String.fromCharCode(u8Arr[i]);
return this.http_callback(filecontent);
}
this.http_callback(this.response);
};
xhr.open(method, url, is_async);
if ((kind === 'bin') || (kind === 'buf'))
xhr.responseType = 'arraybuffer';
if (nodejs && (method === 'GET') && (kind === 'object') && (url.indexOf('.json.gz') > 0)) {
xhr.nodejs_checkzip = true;
xhr.responseType = 'arraybuffer';
}
return xhr;
}
if (isNodeJs()) {
if (!use_promise)
throw Error('Not allowed to create http requests in node.js without promise');
return Promise.resolve().then(function () { return _rollup_plugin_ignore_empty_module_placeholder$1; }).then(h => configureXhr(new h.default()));
}
const xhr = configureXhr(new XMLHttpRequest());
return use_promise ? Promise.resolve(xhr) : xhr;
}
/** @summary Submit asynchronous http request
* @desc Following requests kind can be specified:
* - 'bin' - abstract binary data, result as string
* - 'buf' - abstract binary data, result as ArrayBuffer (default)
* - 'text' - returns req.responseText
* - 'object' - returns parse(req.responseText)
* - 'multi' - returns correctly parsed multi.json request
* - 'xml' - returns req.responseXML
* - 'head' - returns request itself, uses 'HEAD' request method
* - 'post' - creates post request, submits req.send(post_data)
* - 'postbuf' - creates post request, expects binary data as response
* @param {string} url - URL for the request
* @param {string} kind - kind of requested data
* @param {string} [post_data] - data submitted with post kind of request
* @return {Promise} Promise for requested data, result type depends from the kind
* @example
* import { httpRequest } from 'https://root.cern/js/latest/modules/core.mjs';
* httpRequest('https://root.cern/js/files/thstack.json.gz', 'object')
* .then(obj => console.log(`Get object of type ${obj._typename}`))
* .catch(err => console.error(err.message)); */
async function httpRequest(url, kind, post_data) {
return new Promise((resolve, reject) => {
createHttpRequest(url, kind, resolve, reject, true).then(xhr => xhr.send(post_data || null));
});
}
/** @summary Inject javascript code
* @desc Replacement for eval
* @return {Promise} when code is injected
* @private */
async function injectCode(code) {
if (nodejs) {
let name, fs;
return Promise.resolve().then(function () { return _rollup_plugin_ignore_empty_module_placeholder$1; }).then(tmp => {
name = tmp.tmpNameSync() + '.js';
return Promise.resolve().then(function () { return _rollup_plugin_ignore_empty_module_placeholder$1; });
}).then(_fs => {
fs = _fs;
fs.writeFileSync(name, code);
return import(/* webpackIgnore: true */ 'file://' + name);
}).finally(() => fs.unlinkSync(name));
}
if (typeof document !== 'undefined') {
// check if code already loaded - to avoid duplication
const scripts = document.getElementsByTagName('script');
for (let n = 0; n < scripts.length; ++n) {
if (scripts[n].innerText === code)
return true;
}
// try to detect if code includes import and must be treated as module
const is_v6 = code.indexOf('JSROOT.require') >= 0,
is_mjs = !is_v6 && (code.indexOf('import {') > 0) && (code.indexOf('} from \'') > 0),
is_batch = !is_v6 && !is_mjs && (code.indexOf('JSROOT.ObjectPainter') >= 0),
promise = (is_v6 ? exports._ensureJSROOT() : Promise.resolve(true));
if (is_batch && !globalThis.JSROOT)
globalThis.JSROOT = internals.jsroot;
return promise.then(() => {
const element = document.createElement('script');
element.setAttribute('type', is_mjs ? 'module' : 'text/javascript');
element.innerText = code;
document.head.appendChild(element);
// while onload event not fired, just postpone resolve
return isBatchMode() ? true : postponePromise(true, 10);
});
}
return false;
}
/** @summary Load ES6 modules
* @param {String} arg - single URL or array of URLs
* @return {Promise} */
async function loadModules(arg) {
if (isStr(arg))
arg = arg.split(';');
if (!arg.length)
return true;
return import(/* webpackIgnore: true */ arg.shift()).then(() => loadModules(arg));
}
/** @summary Load script or CSS file into the browser
* @param {String} url - script or css file URL (or array, in this case they all loaded sequentially)
* @return {Promise} */
async function loadScript(url) {
if (!url)
return true;
if (isStr(url) && (url.indexOf(';') >= 0))
url = url.split(';');
if (!isStr(url)) {
const scripts = url, loadNext = () => {
return !scripts.length ? true : loadScript(scripts.shift()).then(loadNext, loadNext);
};
return loadNext();
}
if (url.indexOf('$$$') === 0) {
url = url.slice(3);
if ((url.indexOf('style/') === 0) && (url.indexOf('.css') < 0))
url += '.css';
url = exports.source_dir + url;
}
const isstyle = url.indexOf('.css') > 0;
if (nodejs) {
if (isstyle)
return null;
if ((url.indexOf('http:') === 0) || (url.indexOf('https:') === 0))
return httpRequest(url, 'text').then(code => injectCode(code));
// local files, read and use it
if (url.indexOf('./') === 0)
return Promise.resolve().then(function () { return _rollup_plugin_ignore_empty_module_placeholder$1; }).then(fs => injectCode(fs.readFileSync(url)));
return import(/* webpackIgnore: true */ url);
}
const match_url = src => {
if (src === url)
return true;
const indx = src.indexOf(url);
return (indx > 0) && (indx + url.length === src.length) && (src[indx - 1] === '/');
};
if (isstyle) {
const styles = document.getElementsByTagName('link');
for (let n = 0; n < styles.length; ++n) {
if (!styles[n].href || (styles[n].type !== 'text/css') || (styles[n].rel !== 'stylesheet'))
continue;
if (match_url(styles[n].href))
return true;
}
} else {
const scripts = document.getElementsByTagName('script');
for (let n = 0; n < scripts.length; ++n) {
if (match_url(scripts[n].src))
return true;
}
}
let element;
if (isstyle) {
element = document.createElement('link');
element.setAttribute('rel', 'stylesheet');
element.setAttribute('type', 'text/css');
element.setAttribute('href', url);
} else {
element = document.createElement('script');
element.setAttribute('type', 'text/javascript');
element.setAttribute('src', url);
}
return new Promise((resolveFunc, rejectFunc) => {
element.onload = () => resolveFunc(true);
element.onerror = () => {
element.remove();
rejectFunc(Error(`Fail to load ${url}`));
};
document.head.appendChild(element);
});
}
exports._ensureJSROOT = async function() {
const pr = globalThis.JSROOT ? Promise.resolve(true) : loadScript(exports.source_dir + 'scripts/JSRoot.core.js');
return pr.then(() => {
if (globalThis.JSROOT?._complete_loading)
return globalThis.JSROOT._complete_loading();
}).then(() => globalThis.JSROOT);
};
const prROOT = 'ROOT.', clTObject = 'TObject', clTNamed = 'TNamed', clTString = 'TString', clTObjString = 'TObjString',
clTKey = 'TKey', clTFile = 'TFile', clTTree = 'TTree',
clTList = 'TList', clTHashList = 'THashList', clTMap = 'TMap', clTObjArray = 'TObjArray', clTClonesArray = 'TClonesArray',
clTAttLine = 'TAttLine', clTAttFill = 'TAttFill', clTAttMarker = 'TAttMarker', clTAttText = 'TAttText',
clTHStack = 'THStack', clTGraph = 'TGraph', clTMultiGraph = 'TMultiGraph', clTCutG = 'TCutG',
clTGraph2DErrors = 'TGraph2DErrors', clTGraph2DAsymmErrors = 'TGraph2DAsymmErrors',
clTGraphPolar = 'TGraphPolar', clTGraphPolargram = 'TGraphPolargram', clTGraphTime = 'TGraphTime',
clTPave = 'TPave', clTPaveText = 'TPaveText', clTPaveStats = 'TPaveStats', clTPavesText = 'TPavesText',
clTPaveLabel = 'TPaveLabel', clTPaveClass = 'TPaveClass', clTDiamond = 'TDiamond',
clTLegend = 'TLegend', clTLegendEntry = 'TLegendEntry',
clTPaletteAxis = 'TPaletteAxis', clTImagePalette = 'TImagePalette',
clTText = 'TText', clTLink = 'TLink', clTLatex = 'TLatex', clTMathText = 'TMathText', clTAnnotation = 'TAnnotation',
clTColor = 'TColor', clTLine = 'TLine', clTMarker = 'TMarker', clTBox = 'TBox', clTPolyLine = 'TPolyLine',
clTPolyLine3D = 'TPolyLine3D', clTPolyMarker3D = 'TPolyMarker3D',
clTAttPad = 'TAttPad', clTPad = 'TPad', clTCanvas = 'TCanvas', clTFrame = 'TFrame', clTAttCanvas = 'TAttCanvas',
clTGaxis = 'TGaxis', clTAttAxis = 'TAttAxis', clTAxis = 'TAxis', clTStyle = 'TStyle',
clTH1 = 'TH1', clTH1I = 'TH1I', clTH1F = 'TH1F', clTH1D = 'TH1D', clTH2 = 'TH2', clTH2I = 'TH2I', clTH2F = 'TH2F', clTH2D = 'TH2D', clTH3 = 'TH3',
clTF1 = 'TF1', clTF12 = 'TF12', clTF2 = 'TF2', clTF3 = 'TF3', clTProfile = 'TProfile', clTProfile2D = 'TProfile2D', clTProfile3D = 'TProfile3D',
clTGeoVolume = 'TGeoVolume', clTGeoNode = 'TGeoNode', clTGeoNodeMatrix = 'TGeoNodeMatrix',
nsROOT = 'ROOT::', nsREX = nsROOT + 'Experimental::', nsSVG = 'http://www.w3.org/2000/svg',
kNoZoom = -1111, kNoStats = BIT(9), kInspect = 'inspect', kTitle = 'title',
urlClassPrefix = 'https://root.cern/doc/master/class';
/** @summary Create some ROOT classes
* @desc Supported classes: `TObject`, `TNamed`, `TList`, `TAxis`, `TLine`, `TText`, `TLatex`, `TPad`, `TCanvas`
* @param {string} typename - ROOT class name
* @example
* import { create } from 'https://root.cern/js/latest/modules/core.mjs';
* let obj = create('TNamed');
* obj.fName = 'name';
* obj.fTitle = 'title'; */
function create$1(typename, target) {
const obj = target || {};
switch (typename) {
case clTObject:
extend$1(obj, { fUniqueID: 0, fBits: 0 });
break;
case clTNamed:
extend$1(obj, { fUniqueID: 0, fBits: 0, fName: '', fTitle: '' });
break;
case clTList:
case clTHashList:
extend$1(obj, { name: typename, arr: [], opt: [] });
break;
case clTObjArray:
extend$1(obj, { name: typename, arr: [] });
break;
case clTAttAxis:
extend$1(obj, { fNdivisions: 510, fAxisColor: 1,
fLabelColor: 1, fLabelFont: 42, fLabelOffset: 0.005, fLabelSize: 0.035, fTickLength: 0.03,
fTitleOffset: 1, fTitleSize: 0.035, fTitleColor: 1, fTitleFont: 42 });
break;
case c