prometheus
1/*
2SPDX-License-Identifier: MIT
3Source: https://github.com/grafana/grafana/blob/main/public/vendor/flot/jquery.flot.selection.js
4*/
5
6/* eslint-disable prefer-spread */
7/* eslint-disable no-loop-func */
8/* eslint-disable @typescript-eslint/no-this-alias */
9/* eslint-disable no-redeclare */
10/* eslint-disable no-useless-escape */
11/* eslint-disable prefer-const */
12/* eslint-disable @typescript-eslint/explicit-function-return-type */
13/* eslint-disable @typescript-eslint/no-use-before-define */
14/* eslint-disable eqeqeq */
15/* eslint-disable no-var */
16/* Flot plugin for selecting regions of a plot.
17
18Copyright (c) 2007-2013 IOLA and Ole Laursen.
19Licensed under the MIT license.
20
21The plugin supports these options:
22
23selection: {
24mode: null or "x" or "y" or "xy",
25color: color,
26shape: "round" or "miter" or "bevel",
27minSize: number of pixels
28}
29
30Selection support is enabled by setting the mode to one of "x", "y" or "xy".
31In "x" mode, the user will only be able to specify the x range, similarly for
32"y" mode. For "xy", the selection becomes a rectangle where both ranges can be
33specified. "color" is color of the selection (if you need to change the color
34later on, you can get to it with plot.getOptions().selection.color). "shape"
35is the shape of the corners of the selection.
36
37"minSize" is the minimum size a selection can be in pixels. This value can
38be customized to determine the smallest size a selection can be and still
39have the selection rectangle be displayed. When customizing this value, the
40fact that it refers to pixels, not axis units must be taken into account.
41Thus, for example, if there is a bar graph in time mode with BarWidth set to 1
42minute, setting "minSize" to 1 will not make the minimum selection size 1
43minute, but rather 1 pixel. Note also that setting "minSize" to 0 will prevent
44"plotunselected" events from being fired when the user clicks the mouse without
45dragging.
46
47When selection support is enabled, a "plotselected" event will be emitted on
48the DOM element you passed into the plot function. The event handler gets a
49parameter with the ranges selected on the axes, like this:
50
51placeholder.bind( "plotselected", function( event, ranges ) {
52alert("You selected " + ranges.xaxis.from + " to " + ranges.xaxis.to)
53// similar for yaxis - with multiple axes, the extra ones are in
54// x2axis, x3axis, ...
55});
56
57The "plotselected" event is only fired when the user has finished making the
58selection. A "plotselecting" event is fired during the process with the same
59parameters as the "plotselected" event, in case you want to know what's
60happening while it's happening,
61
62A "plotunselected" event with no arguments is emitted when the user clicks the
63mouse to remove the selection. As stated above, setting "minSize" to 0 will
64destroy this behavior.
65
66The plugin allso adds the following methods to the plot object:
67
68- setSelection( ranges, preventEvent )
69
70Set the selection rectangle. The passed in ranges is on the same form as
71returned in the "plotselected" event. If the selection mode is "x", you
72should put in either an xaxis range, if the mode is "y" you need to put in
73an yaxis range and both xaxis and yaxis if the selection mode is "xy", like
74this:
75
76setSelection({ xaxis: { from: 0, to: 10 }, yaxis: { from: 40, to: 60 } });
77
78setSelection will trigger the "plotselected" event when called. If you don't
79want that to happen, e.g. if you're inside a "plotselected" handler, pass
80true as the second parameter. If you are using multiple axes, you can
81specify the ranges on any of those, e.g. as x2axis/x3axis/... instead of
82xaxis, the plugin picks the first one it sees.
83
84- clearSelection( preventEvent )
85
86Clear the selection rectangle. Pass in true to avoid getting a
87"plotunselected" event.
88
89- getSelection()
90
91Returns the current selection in the same format as the "plotselected"
92event. If there's currently no selection, the function returns null.
93
94*/
95
96(function($) {97function init(plot) {98var selection = {99first: { x: -1, y: -1 },100second: { x: -1, y: -1 },101show: false,102active: false,103};104
105// FIXME: The drag handling implemented here should be106// abstracted out, there's some similar code from a library in107// the navigation plugin, this should be massaged a bit to fit108// the Flot cases here better and reused. Doing this would109// make this plugin much slimmer.110var savedhandlers = {};111
112var mouseUpHandler = null;113
114function onMouseMove(e) {115if (selection.active) {116updateSelection(e);117
118plot.getPlaceholder().trigger('plotselecting', [getSelection()]);119}120}121
122function onMouseDown(e) {123if (e.which != 1)124// only accept left-click125return;126
127// cancel out any text selections128document.body.focus();129
130// prevent text selection and drag in old-school browsers131if (document.onselectstart !== undefined && savedhandlers.onselectstart == null) {132savedhandlers.onselectstart = document.onselectstart;133document.onselectstart = function() {134return false;135};136}137if (document.ondrag !== undefined && savedhandlers.ondrag == null) {138savedhandlers.ondrag = document.ondrag;139document.ondrag = function() {140return false;141};142}143
144setSelectionPos(selection.first, e);145
146selection.active = true;147
148// this is a bit silly, but we have to use a closure to be149// able to whack the same handler again150mouseUpHandler = function(e) {151onMouseUp(e);152};153
154$(document).one('mouseup', mouseUpHandler);155}156
157function onMouseUp(e) {158mouseUpHandler = null;159
160// revert drag stuff for old-school browsers161if (document.onselectstart !== undefined) document.onselectstart = savedhandlers.onselectstart;162if (document.ondrag !== undefined) document.ondrag = savedhandlers.ondrag;163
164// no more dragging165selection.active = false;166updateSelection(e);167
168if (selectionIsSane()) triggerSelectedEvent(e);169else {170// this counts as a clear171plot.getPlaceholder().trigger('plotunselected', []);172plot.getPlaceholder().trigger('plotselecting', [null]);173}174
175setTimeout(function() {176plot.isSelecting = false;177}, 10);178
179return false;180}181
182function getSelection() {183if (!selectionIsSane()) return null;184
185if (!selection.show) return null;186
187var r = {},188c1 = selection.first,189c2 = selection.second;190var axes = plot.getAxes();191// look if no axis is used192var noAxisInUse = true;193$.each(axes, function(name, axis) {194if (axis.used) {195//anyUsed = false;196}197});198
199$.each(axes, function(name, axis) {200if (axis.used || noAxisInUse) {201var p1 = axis.c2p(c1[axis.direction]),202p2 = axis.c2p(c2[axis.direction]);203r[name] = { from: Math.min(p1, p2), to: Math.max(p1, p2) };204}205});206return r;207}208
209function triggerSelectedEvent(event) {210var r = getSelection();211
212// Add ctrlKey and metaKey to event213r.ctrlKey = event.ctrlKey;214r.metaKey = event.metaKey;215
216plot.getPlaceholder().trigger('plotselected', [r]);217
218// backwards-compat stuff, to be removed in future219if (r.xaxis && r.yaxis)220plot.getPlaceholder().trigger('selected', [{ x1: r.xaxis.from, y1: r.yaxis.from, x2: r.xaxis.to, y2: r.yaxis.to }]);221}222
223function clamp(min, value, max) {224return value < min ? min : value > max ? max : value;225}226
227function setSelectionPos(pos, e) {228var o = plot.getOptions();229var offset = plot.getPlaceholder().offset();230var plotOffset = plot.getPlotOffset();231pos.x = clamp(0, e.pageX - offset.left - plotOffset.left, plot.width());232pos.y = clamp(0, e.pageY - offset.top - plotOffset.top, plot.height());233
234if (o.selection.mode == 'y') pos.x = pos == selection.first ? 0 : plot.width();235
236if (o.selection.mode == 'x') pos.y = pos == selection.first ? 0 : plot.height();237}238
239function updateSelection(pos) {240if (pos.pageX == null) return;241
242setSelectionPos(selection.second, pos);243if (selectionIsSane()) {244plot.isSelecting = true;245selection.show = true;246plot.triggerRedrawOverlay();247} else clearSelection(true);248}249
250function clearSelection(preventEvent) {251if (selection.show) {252selection.show = false;253plot.triggerRedrawOverlay();254if (!preventEvent) plot.getPlaceholder().trigger('plotunselected', []);255}256}257
258// function taken from markings support in Flot259function extractRange(ranges, coord) {260var axis,261from,262to,263key,264axes = plot.getAxes();265
266for (var k in axes) {267axis = axes[k];268if (axis.direction == coord) {269key = coord + axis.n + 'axis';270if (!ranges[key] && axis.n == 1) key = coord + 'axis'; // support x1axis as xaxis271if (ranges[key]) {272from = ranges[key].from;273to = ranges[key].to;274break;275}276}277}278
279// backwards-compat stuff - to be removed in future280if (!ranges[key]) {281axis = coord == 'x' ? plot.getXAxes()[0] : plot.getYAxes()[0];282from = ranges[coord + '1'];283to = ranges[coord + '2'];284}285
286// auto-reverse as an added bonus287if (from != null && to != null && from > to) {288var tmp = from;289from = to;290to = tmp;291}292
293return { from: from, to: to, axis: axis };294}295
296function setSelection(ranges, preventEvent) {297var range,298o = plot.getOptions();299
300if (o.selection.mode == 'y') {301selection.first.x = 0;302selection.second.x = plot.width();303} else {304range = extractRange(ranges, 'x');305
306selection.first.x = range.axis.p2c(range.from);307selection.second.x = range.axis.p2c(range.to);308}309
310if (o.selection.mode == 'x') {311selection.first.y = 0;312selection.second.y = plot.height();313} else {314range = extractRange(ranges, 'y');315
316selection.first.y = range.axis.p2c(range.from);317selection.second.y = range.axis.p2c(range.to);318}319
320selection.show = true;321plot.triggerRedrawOverlay();322if (!preventEvent && selectionIsSane()) triggerSelectedEvent();323}324
325function selectionIsSane() {326var minSize = plot.getOptions().selection.minSize;327return (328Math.abs(selection.second.x - selection.first.x) >= minSize &&329Math.abs(selection.second.y - selection.first.y) >= minSize330);331}332
333plot.clearSelection = clearSelection;334plot.setSelection = setSelection;335plot.getSelection = getSelection;336
337plot.hooks.bindEvents.push(function(plot, eventHolder) {338var o = plot.getOptions();339if (o.selection.mode != null) {340eventHolder.mousemove(onMouseMove);341eventHolder.mousedown(onMouseDown);342}343});344
345plot.hooks.drawOverlay.push(function(plot, ctx) {346// draw selection347if (selection.show && selectionIsSane()) {348var plotOffset = plot.getPlotOffset();349var o = plot.getOptions();350
351ctx.save();352ctx.translate(plotOffset.left, plotOffset.top);353
354var c = $.color.parse(o.selection.color);355
356ctx.strokeStyle = c.scale('a', 0.8).toString();357ctx.lineWidth = 1;358ctx.lineJoin = o.selection.shape;359ctx.fillStyle = c.scale('a', 0.4).toString();360
361var x = Math.min(selection.first.x, selection.second.x) + 0.5,362y = Math.min(selection.first.y, selection.second.y) + 0.5,363w = Math.abs(selection.second.x - selection.first.x) - 1,364h = Math.abs(selection.second.y - selection.first.y) - 1;365
366ctx.fillRect(x, y, w, h);367ctx.strokeRect(x, y, w, h);368
369ctx.restore();370}371});372
373plot.hooks.shutdown.push(function(plot, eventHolder) {374eventHolder.unbind('mousemove', onMouseMove);375eventHolder.unbind('mousedown', onMouseDown);376
377if (mouseUpHandler) {378$(document).unbind('mouseup', mouseUpHandler);379// grafana addition380// In L114 this plugin is overrinding document.onselectstart handler to prevent default or custom behaviour381// Then this patch is being restored during mouseup event. But, mouseup handler is unbound when this plugin is destroyed382// and the overridden onselectstart handler is not restored. The problematic behaviour surfaces when flot is re-rendered383// as a consequence of panel's model update. When i.e. options are applied via onBlur384// event on some input which results in flot re-render. The mouseup handler should be called to resture the original handlers385// but by the time the document mouseup event occurs, the event handler is no longer there, so onselectstart is permanently overridden.386// To fix that we are making sure that the overrides are reverted when this plugin is destroyed, the same way as they would387// via mouseup event handler (L138)388
389if (document.onselectstart !== undefined) document.onselectstart = savedhandlers.onselectstart;390if (document.ondrag !== undefined) document.ondrag = savedhandlers.ondrag;391}392});393}394
395$.plot.plugins.push({396init: init,397options: {398selection: {399mode: null, // one of null, "x", "y" or "xy"400color: '#e8cfac',401shape: 'round', // one of "round", "miter", or "bevel"402minSize: 5, // minimum number of pixels403},404},405name: 'selection',406version: '1.1',407});408})(window.jQuery);409