calcite-graph_2.entry.js 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  1. /*!
  2. * All material copyright ESRI, All Rights Reserved, unless otherwise specified.
  3. * See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details.
  4. * v1.0.0-beta.82
  5. */
  6. import { r as registerInstance, f as forceUpdate, h, g as getElement, c as createEvent, H as Host } from './index-8ece2564.js';
  7. import { g as guid } from './guid-b4461004.js';
  8. import { c as createObserver } from './observers-b198f831.js';
  9. import { j as intersects } from './dom-da697a3f.js';
  10. import { d as decimalPlaces, c as clamp } from './math-2e4483eb.js';
  11. import { c as connectLabel, d as disconnectLabel } from './label-50132b90.js';
  12. import { c as connectForm, d as disconnectForm, a as afterConnectDefaultValueSet, H as HiddenFormInputSlot } from './form-bca481e1.js';
  13. import { u as updateHostInteraction } from './interactive-cb5bf285.js';
  14. /**
  15. * Calculate slope of the tangents
  16. * uses Steffen interpolation as it's monotonic
  17. * http://jrwalsh1.github.io/posts/interpolations/
  18. */
  19. function slope(p0, p1, p2) {
  20. const dx = p1[0] - p0[0];
  21. const dx1 = p2[0] - p1[0];
  22. const dy = p1[1] - p0[1];
  23. const dy1 = p2[1] - p1[1];
  24. const m = dy / (dx || (dx1 < 0 && 0));
  25. const m1 = dy1 / (dx1 || (dx < 0 && 0));
  26. const p = (m * dx1 + m1 * dx) / (dx + dx1);
  27. return (Math.sign(m) + Math.sign(m1)) * Math.min(Math.abs(m), Math.abs(m1), 0.5 * Math.abs(p)) || 0;
  28. }
  29. /**
  30. * Calculate slope for just one tangent (single-sided)
  31. */
  32. function slopeSingle(p0, p1, m) {
  33. const dx = p1[0] - p0[0];
  34. const dy = p1[1] - p0[1];
  35. return dx ? ((3 * dy) / dx - m) / 2 : m;
  36. }
  37. /**
  38. * Given two points and their tangent slopes,
  39. * calculate the bezier handle coordinates and return draw command.
  40. *
  41. * Translates Hermite Spline to Beziér curve:
  42. * stackoverflow.com/questions/42574940/
  43. */
  44. function bezier(p0, p1, m0, m1, t) {
  45. const [x0, y0] = p0;
  46. const [x1, y1] = p1;
  47. const dx = (x1 - x0) / 3;
  48. const h1 = t([x0 + dx, y0 + dx * m0]).join(",");
  49. const h2 = t([x1 - dx, y1 - dx * m1]).join(",");
  50. const p = t([x1, y1]).join(",");
  51. return `C ${h1} ${h2} ${p}`;
  52. }
  53. /**
  54. * Generate a function which will translate a point
  55. * from the data coordinate space to svg viewbox oriented pixels
  56. */
  57. function translate({ width, height, min, max }) {
  58. const rangeX = max[0] - min[0];
  59. const rangeY = max[1] - min[1];
  60. return (point) => {
  61. const x = ((point[0] - min[0]) / rangeX) * width;
  62. const y = height - (point[1] / rangeY) * height;
  63. return [x, y];
  64. };
  65. }
  66. /**
  67. * Get the min and max values from the dataset
  68. */
  69. function range(data) {
  70. const [startX, startY] = data[0];
  71. const min = [startX, startY];
  72. const max = [startX, startY];
  73. return data.reduce(({ min, max }, [x, y]) => ({
  74. min: [Math.min(min[0], x), Math.min(min[1], y)],
  75. max: [Math.max(max[0], x), Math.max(max[1], y)]
  76. }), { min, max });
  77. }
  78. /**
  79. * Generate drawing commands for an area graph
  80. * returns a string can can be passed directly to a path element's `d` attribute
  81. */
  82. function area({ data, min, max, t }) {
  83. if (data.length === 0) {
  84. return "";
  85. }
  86. // important points for beginning and ending the path
  87. const [startX, startY] = t(data[0]);
  88. const [minX, minY] = t(min);
  89. const [maxX] = t(max);
  90. // keep track of previous slope/points
  91. let m;
  92. let p0;
  93. let p1;
  94. // iterate over data points, calculating command for each
  95. const commands = data.reduce((acc, point, i) => {
  96. p0 = data[i - 2];
  97. p1 = data[i - 1];
  98. if (i > 1) {
  99. const m1 = slope(p0, p1, point);
  100. const m0 = m === undefined ? slopeSingle(p0, p1, m1) : m;
  101. const command = bezier(p0, p1, m0, m1, t);
  102. m = m1;
  103. return `${acc} ${command}`;
  104. }
  105. return acc;
  106. }, `M ${minX},${minY} L ${minX},${startY} L ${startX},${startY}`);
  107. // close the path
  108. const last = data[data.length - 1];
  109. const end = bezier(p1, last, m, slopeSingle(p1, last, m), t);
  110. return `${commands} ${end} L ${maxX},${minY} Z`;
  111. }
  112. const graphCss = "@-webkit-keyframes in{0%{opacity:0}100%{opacity:1}}@keyframes in{0%{opacity:0}100%{opacity:1}}@-webkit-keyframes in-down{0%{opacity:0;-webkit-transform:translate3D(0, -5px, 0);transform:translate3D(0, -5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@keyframes in-down{0%{opacity:0;-webkit-transform:translate3D(0, -5px, 0);transform:translate3D(0, -5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@-webkit-keyframes in-up{0%{opacity:0;-webkit-transform:translate3D(0, 5px, 0);transform:translate3D(0, 5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@keyframes in-up{0%{opacity:0;-webkit-transform:translate3D(0, 5px, 0);transform:translate3D(0, 5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@-webkit-keyframes in-scale{0%{opacity:0;-webkit-transform:scale3D(0.95, 0.95, 1);transform:scale3D(0.95, 0.95, 1)}100%{opacity:1;-webkit-transform:scale3D(1, 1, 1);transform:scale3D(1, 1, 1)}}@keyframes in-scale{0%{opacity:0;-webkit-transform:scale3D(0.95, 0.95, 1);transform:scale3D(0.95, 0.95, 1)}100%{opacity:1;-webkit-transform:scale3D(1, 1, 1);transform:scale3D(1, 1, 1)}}:root{--calcite-animation-timing:calc(150ms * var(--calcite-internal-duration-factor));--calcite-internal-duration-factor:var(--calcite-duration-factor, 1);--calcite-internal-animation-timing-fast:calc(100ms * var(--calcite-internal-duration-factor));--calcite-internal-animation-timing-medium:calc(200ms * var(--calcite-internal-duration-factor));--calcite-internal-animation-timing-slow:calc(300ms * var(--calcite-internal-duration-factor))}.calcite-animate{opacity:0;-webkit-animation-fill-mode:both;animation-fill-mode:both;-webkit-animation-duration:var(--calcite-animation-timing);animation-duration:var(--calcite-animation-timing)}.calcite-animate__in{-webkit-animation-name:in;animation-name:in}.calcite-animate__in-down{-webkit-animation-name:in-down;animation-name:in-down}.calcite-animate__in-up{-webkit-animation-name:in-up;animation-name:in-up}.calcite-animate__in-scale{-webkit-animation-name:in-scale;animation-name:in-scale}:root{--calcite-popper-transition:var(--calcite-animation-timing)}:host([hidden]){display:none}:host{display:block}.svg{fill:currentColor;stroke:transparent;margin:0px;display:block;height:100%;width:100%;padding:0px}.svg .graph-path--highlight{fill:var(--calcite-ui-brand);opacity:0.5}";
  113. const Graph = class {
  114. constructor(hostRef) {
  115. registerInstance(this, hostRef);
  116. //--------------------------------------------------------------------------
  117. //
  118. // Properties
  119. //
  120. //--------------------------------------------------------------------------
  121. /**
  122. * Array of tuples describing a single data point ([x, y])
  123. * These data points should be sorted by x-axis value
  124. */
  125. this.data = [];
  126. //--------------------------------------------------------------------------
  127. //
  128. // Private State/Props
  129. //
  130. //--------------------------------------------------------------------------
  131. this.graphId = `calcite-graph-${guid()}`;
  132. this.resizeObserver = createObserver("resize", () => forceUpdate(this));
  133. }
  134. //--------------------------------------------------------------------------
  135. //
  136. // Lifecycle
  137. //
  138. //--------------------------------------------------------------------------
  139. connectedCallback() {
  140. var _a;
  141. (_a = this.resizeObserver) === null || _a === void 0 ? void 0 : _a.observe(this.el);
  142. }
  143. disconnectedCallback() {
  144. var _a;
  145. (_a = this.resizeObserver) === null || _a === void 0 ? void 0 : _a.disconnect();
  146. }
  147. render() {
  148. const { data, colorStops, el, highlightMax, highlightMin, min, max } = this;
  149. const id = this.graphId;
  150. const { clientHeight: height, clientWidth: width } = el;
  151. // if we have no data, return empty svg
  152. if (!data || data.length === 0) {
  153. return (h("svg", { class: "svg", height: height, preserveAspectRatio: "none", viewBox: `0 0 ${width} ${height}`, width: width }));
  154. }
  155. const { min: rangeMin, max: rangeMax } = range(data);
  156. let currentMin = rangeMin;
  157. let currentMax = rangeMax;
  158. if (min < rangeMin[0] || min > rangeMin[0]) {
  159. currentMin = [min, 0];
  160. }
  161. if (max > rangeMax[0] || max < rangeMax[0]) {
  162. currentMax = [max, rangeMax[1]];
  163. }
  164. const t = translate({ min: currentMin, max: currentMax, width, height });
  165. const [hMinX] = t([highlightMin, currentMax[1]]);
  166. const [hMaxX] = t([highlightMax, currentMax[1]]);
  167. const areaPath = area({ data, min: rangeMin, max: rangeMax, t });
  168. const fill = colorStops ? `url(#linear-gradient-${id})` : undefined;
  169. return (h("svg", { class: "svg", height: height, preserveAspectRatio: "none", viewBox: `0 0 ${width} ${height}`, width: width }, colorStops ? (h("defs", null, h("linearGradient", { id: `linear-gradient-${id}`, x1: "0", x2: "1", y1: "0", y2: "0" }, colorStops.map(({ offset, color, opacity }) => (h("stop", { offset: `${offset * 100}%`, "stop-color": color, "stop-opacity": opacity })))))) : null, highlightMin !== undefined ? ([
  170. h("mask", { height: "100%", id: `${id}1`, width: "100%", x: "0%", y: "0%" }, h("path", { d: `
  171. M 0,0
  172. L ${hMinX - 1},0
  173. L ${hMinX - 1},${height}
  174. L 0,${height}
  175. Z
  176. `, fill: "white" })),
  177. h("mask", { height: "100%", id: `${id}2`, width: "100%", x: "0%", y: "0%" }, h("path", { d: `
  178. M ${hMinX + 1},0
  179. L ${hMaxX - 1},0
  180. L ${hMaxX - 1},${height}
  181. L ${hMinX + 1}, ${height}
  182. Z
  183. `, fill: "white" })),
  184. h("mask", { height: "100%", id: `${id}3`, width: "100%", x: "0%", y: "0%" }, h("path", { d: `
  185. M ${hMaxX + 1},0
  186. L ${width},0
  187. L ${width},${height}
  188. L ${hMaxX + 1}, ${height}
  189. Z
  190. `, fill: "white" })),
  191. h("path", { class: "graph-path", d: areaPath, fill: fill, mask: `url(#${id}1)` }),
  192. h("path", { class: "graph-path--highlight", d: areaPath, fill: fill, mask: `url(#${id}2)` }),
  193. h("path", { class: "graph-path", d: areaPath, fill: fill, mask: `url(#${id}3)` })
  194. ]) : (h("path", { class: "graph-path", d: areaPath, fill: fill }))));
  195. }
  196. get el() { return getElement(this); }
  197. };
  198. Graph.style = graphCss;
  199. const sliderCss = "@charset \"UTF-8\";@-webkit-keyframes in{0%{opacity:0}100%{opacity:1}}@keyframes in{0%{opacity:0}100%{opacity:1}}@-webkit-keyframes in-down{0%{opacity:0;-webkit-transform:translate3D(0, -5px, 0);transform:translate3D(0, -5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@keyframes in-down{0%{opacity:0;-webkit-transform:translate3D(0, -5px, 0);transform:translate3D(0, -5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@-webkit-keyframes in-up{0%{opacity:0;-webkit-transform:translate3D(0, 5px, 0);transform:translate3D(0, 5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@keyframes in-up{0%{opacity:0;-webkit-transform:translate3D(0, 5px, 0);transform:translate3D(0, 5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@-webkit-keyframes in-scale{0%{opacity:0;-webkit-transform:scale3D(0.95, 0.95, 1);transform:scale3D(0.95, 0.95, 1)}100%{opacity:1;-webkit-transform:scale3D(1, 1, 1);transform:scale3D(1, 1, 1)}}@keyframes in-scale{0%{opacity:0;-webkit-transform:scale3D(0.95, 0.95, 1);transform:scale3D(0.95, 0.95, 1)}100%{opacity:1;-webkit-transform:scale3D(1, 1, 1);transform:scale3D(1, 1, 1)}}:root{--calcite-animation-timing:calc(150ms * var(--calcite-internal-duration-factor));--calcite-internal-duration-factor:var(--calcite-duration-factor, 1);--calcite-internal-animation-timing-fast:calc(100ms * var(--calcite-internal-duration-factor));--calcite-internal-animation-timing-medium:calc(200ms * var(--calcite-internal-duration-factor));--calcite-internal-animation-timing-slow:calc(300ms * var(--calcite-internal-duration-factor))}.calcite-animate{opacity:0;-webkit-animation-fill-mode:both;animation-fill-mode:both;-webkit-animation-duration:var(--calcite-animation-timing);animation-duration:var(--calcite-animation-timing)}.calcite-animate__in{-webkit-animation-name:in;animation-name:in}.calcite-animate__in-down{-webkit-animation-name:in-down;animation-name:in-down}.calcite-animate__in-up{-webkit-animation-name:in-up;animation-name:in-up}.calcite-animate__in-scale{-webkit-animation-name:in-scale;animation-name:in-scale}:root{--calcite-popper-transition:var(--calcite-animation-timing)}:host([hidden]){display:none}.scale--s{--calcite-slider-handle-size:10px;--calcite-slider-handle-extension-height:6.5px;--calcite-slider-container-font-size:var(--calcite-font-size--3)}.scale--s .handle__label,.scale--s .tick__label{line-height:.75rem}.scale--m{--calcite-slider-handle-size:14px;--calcite-slider-handle-extension-height:8px;--calcite-slider-container-font-size:var(--calcite-font-size--2)}.scale--m .handle__label,.scale--m .tick__label{line-height:1rem}.scale--l{--calcite-slider-handle-size:16px;--calcite-slider-handle-extension-height:10.5px;--calcite-slider-container-font-size:var(--calcite-font-size--1)}.scale--l .handle__label,.scale--l .tick__label{line-height:1rem}.handle__label,.tick__label{font-weight:var(--calcite-font-weight-medium);color:var(--calcite-ui-text-2);font-size:var(--calcite-slider-container-font-size)}:host{display:block}.container{position:relative;display:block;overflow-wrap:normal;word-break:normal;padding:calc(var(--calcite-slider-handle-size) * 0.5);margin:calc(var(--calcite-slider-handle-size) * 0.5) 0;--calcite-slider-full-handle-height:calc(\n var(--calcite-slider-handle-size) + var(--calcite-slider-handle-extension-height)\n )}:host([disabled]){pointer-events:none;cursor:default;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;opacity:var(--calcite-ui-opacity-disabled)}:host([disabled]) .track__range,:host([disabled]) .tick--active{background-color:var(--calcite-ui-text-3)}:host([disabled]) ::slotted([calcite-hydrated][disabled]),:host([disabled]) [calcite-hydrated][disabled]{opacity:1}.scale--s .thumb:not(.thumb--precise){--calcite-slider-thumb-y-offset:-6px}.scale--m .thumb:not(.thumb--precise){--calcite-slider-thumb-y-offset:-8px}.scale--l .thumb:not(.thumb--precise){--calcite-slider-thumb-y-offset:-9px}:host([precise]:not([has-histogram])) .container .thumb--value{--calcite-slider-thumb-y-offset:calc(var(--calcite-slider-full-handle-height) * -1)}.thumb-container{position:relative;max-width:100%}.thumb{--calcite-slider-thumb-x-offset:calc(var(--calcite-slider-handle-size) * 0.5);position:absolute;margin:0px;display:-ms-flexbox;display:flex;cursor:pointer;-ms-flex-direction:column;flex-direction:column;-ms-flex-align:center;align-items:center;border-style:none;background-color:transparent;padding:0px;font-family:inherit;outline:2px solid transparent;outline-offset:2px;-webkit-transform:translate(var(--calcite-slider-thumb-x-offset), var(--calcite-slider-thumb-y-offset));transform:translate(var(--calcite-slider-thumb-x-offset), var(--calcite-slider-thumb-y-offset))}.thumb .handle__label.static,.thumb .handle__label.transformed{position:absolute;top:0px;bottom:0px;opacity:0}.thumb .handle__label.hyphen::after{content:\"—\";display:inline-block;width:1em}.thumb .handle__label.hyphen--wrap{display:-ms-flexbox;display:flex}.thumb .handle{-webkit-box-sizing:border-box;box-sizing:border-box;border-radius:9999px;background-color:var(--calcite-ui-foreground-1);outline-offset:0;outline-color:transparent;-webkit-transition:outline-offset 100ms ease-in-out, outline-color 100ms ease-in-out;transition:outline-offset 100ms ease-in-out, outline-color 100ms ease-in-out;height:var(--calcite-slider-handle-size);width:var(--calcite-slider-handle-size);-webkit-box-shadow:0 0 0 2px var(--calcite-ui-text-3) inset;box-shadow:0 0 0 2px var(--calcite-ui-text-3) inset;-webkit-transition:border var(--calcite-internal-animation-timing-medium) ease, background-color var(--calcite-internal-animation-timing-medium) ease, -webkit-box-shadow var(--calcite-animation-timing) ease;transition:border var(--calcite-internal-animation-timing-medium) ease, background-color var(--calcite-internal-animation-timing-medium) ease, -webkit-box-shadow var(--calcite-animation-timing) ease;transition:border var(--calcite-internal-animation-timing-medium) ease, background-color var(--calcite-internal-animation-timing-medium) ease, box-shadow var(--calcite-animation-timing) ease;transition:border var(--calcite-internal-animation-timing-medium) ease, background-color var(--calcite-internal-animation-timing-medium) ease, box-shadow var(--calcite-animation-timing) ease, -webkit-box-shadow var(--calcite-animation-timing) ease}.thumb .handle-extension{width:0.125rem;height:var(--calcite-slider-handle-extension-height);background-color:var(--calcite-ui-text-3)}.thumb:hover .handle{-webkit-box-shadow:0 0 0 3px var(--calcite-ui-brand) inset;box-shadow:0 0 0 3px var(--calcite-ui-brand) inset}.thumb:hover .handle-extension{background-color:var(--calcite-ui-brand)}.thumb:focus .handle{outline:2px solid var(--calcite-ui-brand);outline-offset:2px}.thumb:focus .handle-extension{background-color:var(--calcite-ui-brand)}.thumb.thumb--minValue{-webkit-transform:translate(calc(var(--calcite-slider-thumb-x-offset) * -1), var(--calcite-slider-thumb-y-offset));transform:translate(calc(var(--calcite-slider-thumb-x-offset) * -1), var(--calcite-slider-thumb-y-offset))}.thumb.thumb--precise{--calcite-slider-thumb-y-offset:-2px}:host([label-handles]) .thumb{--calcite-slider-thumb-x-offset:50%}:host([label-handles]):host(:not([has-histogram])) .scale--s .thumb:not(.thumb--precise){--calcite-slider-thumb-y-offset:-23px}:host([label-handles]):host(:not([has-histogram])) .scale--m .thumb:not(.thumb--precise){--calcite-slider-thumb-y-offset:-30px}:host([label-handles]):host(:not([has-histogram])) .scale--l .thumb:not(.thumb--precise){--calcite-slider-thumb-y-offset:-32px}:host([has-histogram][label-handles]) .handle__label,:host([label-handles]:not([has-histogram])) .thumb--minValue.thumb--precise .handle__label{margin-top:0.5em}:host(:not([has-histogram]):not([precise])) .handle__label,:host([label-handles]:not([has-histogram])) .thumb--value .handle__label{margin-bottom:0.5em}:host([label-handles][precise]):host(:not([has-histogram])) .scale--s .thumb--value{--calcite-slider-thumb-y-offset:-33px}:host([label-handles][precise]):host(:not([has-histogram])) .scale--m .thumb--value{--calcite-slider-thumb-y-offset:-44px}:host([label-handles][precise]):host(:not([has-histogram])) .scale--l .thumb--value{--calcite-slider-thumb-y-offset:-49px}.thumb:focus .handle,.thumb--active .handle{background-color:var(--calcite-ui-brand);-webkit-box-shadow:0 0 8px 0 rgba(0, 0, 0, 0.16);box-shadow:0 0 8px 0 rgba(0, 0, 0, 0.16)}.thumb:hover.thumb--precise:after,.thumb:focus.thumb--precise:after,.thumb--active.thumb--precise:after{background-color:var(--calcite-ui-brand)}.track{position:relative;height:0.125rem;border-radius:0px;background-color:var(--calcite-ui-border-2);-webkit-transition:all var(--calcite-internal-animation-timing-medium) ease-in;transition:all var(--calcite-internal-animation-timing-medium) ease-in}.track__range{position:absolute;top:0px;height:0.125rem;background-color:var(--calcite-ui-brand)}.container--range .track__range:hover{cursor:ew-resize}.container--range .track__range:after{position:absolute;width:100%;content:\"\";top:calc(var(--calcite-slider-full-handle-height) * 0.5 * -1);height:calc(var(--calcite-slider-handle-size) + var(--calcite-slider-handle-extension-height))}@media (forced-colors: active){.thumb{outline-width:0;outline-offset:0}.handle{outline:2px solid transparent;outline-offset:2px}.thumb:focus .handle,.thumb .handle-extension,.thumb:hover .handle-extension,.thumb:focus .handle-extension,.thumb:active .handle-extension{background-color:canvasText}.track{background-color:canvasText}.track__range{background-color:highlight}}.tick{position:absolute;height:0.25rem;width:0.125rem;border-width:1px;border-style:solid;background-color:var(--calcite-ui-border-input);border-color:var(--calcite-ui-foreground-1);top:-2px;pointer-events:none;-webkit-margin-start:calc(-1 * 0.125rem);margin-inline-start:calc(-1 * 0.125rem)}.tick--active{background-color:var(--calcite-ui-brand)}.tick__label{pointer-events:none;margin-top:0.875rem;display:-ms-flexbox;display:flex;-ms-flex-pack:center;justify-content:center}.tick__label--min{-webkit-transition:opacity var(--calcite-animation-timing);transition:opacity var(--calcite-animation-timing)}.tick__label--max{-webkit-transition:opacity var(--calcite-internal-animation-timing-fast);transition:opacity var(--calcite-internal-animation-timing-fast)}:host([has-histogram][label-handles]) .tick__label--min,:host([has-histogram][label-handles]) .tick__label--max,:host([has-histogram][precise]) .tick__label--min,:host([has-histogram][precise]) .tick__label--max{font-weight:var(--calcite-font-weight-normal);color:var(--calcite-ui-text-3)}.graph{color:var(--calcite-ui-foreground-3);height:48px}:host([label-ticks][ticks]) .container{padding-bottom:calc(0.875rem + var(--calcite-slider-container-font-size))}:host([has-histogram]):host([precise][label-handles]) .container{padding-bottom:calc(var(--calcite-slider-full-handle-height) + 1em)}:host([has-histogram]):host([label-handles]:not([precise])) .container{padding-bottom:calc(var(--calcite-slider-handle-size) * 0.5 + 1em)}:host([has-histogram]):host([precise]:not([label-handles])) .container{padding-bottom:var(--calcite-slider-full-handle-height)}:host(:not([has-histogram])):host([precise]:not([label-handles])) .container{padding-top:var(--calcite-slider-full-handle-height)}:host(:not([has-histogram])):host([precise]:not([label-handles])) .container--range{padding-bottom:var(--calcite-slider-full-handle-height)}:host(:not([has-histogram])):host([label-handles]:not([precise])) .container{padding-top:calc(var(--calcite-slider-full-handle-height) + 4px)}:host(:not([has-histogram])):host([label-handles][precise]) .container{padding-top:calc(var(--calcite-slider-full-handle-height) + var(--calcite-slider-container-font-size) + 4px)}:host(:not([has-histogram])):host([label-handles][precise]) .container--range{padding-bottom:calc(var(--calcite-slider-full-handle-height) + var(--calcite-slider-container-font-size) + 4px)}::slotted(input[slot=hidden-form-input]){bottom:0 !important;left:0 !important;margin:0 !important;opacity:0 !important;outline:none !important;padding:0 !important;position:absolute !important;right:0 !important;top:0 !important;-webkit-transform:none !important;transform:none !important;-webkit-appearance:none !important;z-index:-1 !important}";
  200. function isRange(value) {
  201. return Array.isArray(value);
  202. }
  203. const Slider = class {
  204. constructor(hostRef) {
  205. registerInstance(this, hostRef);
  206. this.calciteSliderInput = createEvent(this, "calciteSliderInput", 7);
  207. this.calciteSliderChange = createEvent(this, "calciteSliderChange", 7);
  208. this.calciteSliderUpdate = createEvent(this, "calciteSliderUpdate", 7);
  209. //--------------------------------------------------------------------------
  210. //
  211. // Properties
  212. //
  213. //--------------------------------------------------------------------------
  214. /** Disable and gray out the slider */
  215. this.disabled = false;
  216. /** Indicates if a histogram is present */
  217. this.hasHistogram = false;
  218. /** Label handles with their numeric value */
  219. this.labelHandles = false;
  220. /** Label tick marks with their numeric value. */
  221. this.labelTicks = false;
  222. /** Maximum selectable value */
  223. this.max = 100;
  224. /** Minimum selectable value */
  225. this.min = 0;
  226. /**
  227. * When true, the slider will display values from high to low.
  228. *
  229. * Note that this value will be ignored if the slider has an associated histogram.
  230. */
  231. this.mirrored = false;
  232. /** Use finer point for handles */
  233. this.precise = false;
  234. /**
  235. * When true, makes the component required for form-submission.
  236. */
  237. this.required = false;
  238. /** When true, enables snap selection along the step interval */
  239. this.snap = false;
  240. /** Interval to move on up/down keys */
  241. this.step = 1;
  242. /** Currently selected number (if single select) */
  243. this.value = 0;
  244. /**
  245. * Specify the scale of the slider, defaults to m
  246. */
  247. this.scale = "m";
  248. this.guid = `calcite-slider-${guid()}`;
  249. this.activeProp = "value";
  250. this.minMaxValueRange = null;
  251. this.minValueDragRange = null;
  252. this.maxValueDragRange = null;
  253. this.tickValues = [];
  254. this.dragUpdate = (event) => {
  255. event.preventDefault();
  256. if (this.dragProp) {
  257. const value = this.translate(event.clientX || event.pageX);
  258. if (isRange(this.value) && this.dragProp === "minMaxValue") {
  259. if (this.minValueDragRange && this.maxValueDragRange && this.minMaxValueRange) {
  260. const newMinValue = value - this.minValueDragRange;
  261. const newMaxValue = value + this.maxValueDragRange;
  262. if (newMaxValue <= this.max &&
  263. newMinValue >= this.min &&
  264. newMaxValue - newMinValue === this.minMaxValueRange) {
  265. this.minValue = this.clamp(newMinValue, "minValue");
  266. this.maxValue = this.clamp(newMaxValue, "maxValue");
  267. }
  268. }
  269. else {
  270. this.minValueDragRange = value - this.minValue;
  271. this.maxValueDragRange = this.maxValue - value;
  272. this.minMaxValueRange = this.maxValue - this.minValue;
  273. }
  274. }
  275. else {
  276. this.setValue(this.dragProp, this.clamp(value, this.dragProp));
  277. }
  278. }
  279. };
  280. this.dragEnd = (event) => {
  281. this.removeDragListeners();
  282. this.focusActiveHandle(event.clientX);
  283. if (this.lastDragPropValue != this[this.dragProp]) {
  284. this.emitChange();
  285. }
  286. this.dragProp = null;
  287. this.lastDragPropValue = null;
  288. this.minValueDragRange = null;
  289. this.maxValueDragRange = null;
  290. this.minMaxValueRange = null;
  291. };
  292. /**
  293. * Set the reference of the track Element
  294. * @internal
  295. * @param node
  296. */
  297. this.storeTrackRef = (node) => {
  298. this.trackEl = node;
  299. };
  300. }
  301. histogramWatcher(newHistogram) {
  302. this.hasHistogram = !!newHistogram;
  303. }
  304. valueHandler() {
  305. this.setMinMaxFromValue();
  306. }
  307. minMaxValueHandler() {
  308. this.setValueFromMinMax();
  309. }
  310. //--------------------------------------------------------------------------
  311. //
  312. // Lifecycle
  313. //
  314. //--------------------------------------------------------------------------
  315. connectedCallback() {
  316. this.setMinMaxFromValue();
  317. this.setValueFromMinMax();
  318. connectLabel(this);
  319. connectForm(this);
  320. }
  321. disconnectedCallback() {
  322. disconnectLabel(this);
  323. disconnectForm(this);
  324. this.removeDragListeners();
  325. }
  326. componentWillLoad() {
  327. this.tickValues = this.generateTickValues();
  328. if (!isRange(this.value)) {
  329. this.value = this.clamp(this.value);
  330. }
  331. afterConnectDefaultValueSet(this, this.value);
  332. if (this.snap && !isRange(this.value)) {
  333. this.value = this.getClosestStep(this.value);
  334. }
  335. if (this.histogram) {
  336. this.hasHistogram = true;
  337. }
  338. }
  339. componentDidRender() {
  340. if (this.labelHandles) {
  341. this.adjustHostObscuredHandleLabel("value");
  342. if (isRange(this.value)) {
  343. this.adjustHostObscuredHandleLabel("minValue");
  344. if (!(this.precise && !this.hasHistogram)) {
  345. this.hyphenateCollidingRangeHandleLabels();
  346. }
  347. }
  348. }
  349. this.hideObscuredBoundingTickLabels();
  350. updateHostInteraction(this);
  351. }
  352. render() {
  353. const id = this.el.id || this.guid;
  354. const maxProp = isRange(this.value) ? "maxValue" : "value";
  355. const value = isRange(this.value) ? this.maxValue : this.value;
  356. const min = this.minValue || this.min;
  357. const useMinValue = this.shouldUseMinValue();
  358. const minInterval = this.getUnitInterval(useMinValue ? this.minValue : min) * 100;
  359. const maxInterval = this.getUnitInterval(value) * 100;
  360. const mirror = this.shouldMirror();
  361. const leftThumbOffset = `${mirror ? 100 - minInterval : minInterval}%`;
  362. const rightThumbOffset = `${mirror ? maxInterval : 100 - maxInterval}%`;
  363. const valueIsRange = isRange(this.value);
  364. const handle = (h("div", { "aria-disabled": this.disabled, "aria-label": valueIsRange ? this.maxLabel : this.minLabel, "aria-orientation": "horizontal", "aria-valuemax": this.max, "aria-valuemin": this.min, "aria-valuenow": value, class: {
  365. thumb: true,
  366. "thumb--value": true,
  367. "thumb--active": this.lastDragProp !== "minMaxValue" && this.dragProp === maxProp
  368. }, onBlur: () => (this.activeProp = null), onFocus: () => (this.activeProp = maxProp), onPointerDown: () => this.dragStart(maxProp), ref: (el) => (this.maxHandle = el), role: "slider", style: { right: rightThumbOffset }, tabIndex: 0 }, h("div", { class: "handle" })));
  369. const labeledHandle = (h("div", { "aria-disabled": this.disabled, "aria-label": valueIsRange ? this.maxLabel : this.minLabel, "aria-orientation": "horizontal", "aria-valuemax": this.max, "aria-valuemin": this.min, "aria-valuenow": value, class: {
  370. thumb: true,
  371. "thumb--value": true,
  372. "thumb--active": this.lastDragProp !== "minMaxValue" && this.dragProp === maxProp
  373. }, onBlur: () => (this.activeProp = null), onFocus: () => (this.activeProp = maxProp), onPointerDown: () => this.dragStart(maxProp), ref: (el) => (this.maxHandle = el), role: "slider", style: { right: rightThumbOffset }, tabIndex: 0 }, h("span", { "aria-hidden": "true", class: "handle__label handle__label--value" }, value ? value.toLocaleString() : value), h("span", { "aria-hidden": "true", class: "handle__label handle__label--value static" }, value ? value.toLocaleString() : value), h("span", { "aria-hidden": "true", class: "handle__label handle__label--value transformed" }, value ? value.toLocaleString() : value), h("div", { class: "handle" })));
  374. const histogramLabeledHandle = (h("div", { "aria-disabled": this.disabled, "aria-label": valueIsRange ? this.maxLabel : this.minLabel, "aria-orientation": "horizontal", "aria-valuemax": this.max, "aria-valuemin": this.min, "aria-valuenow": value, class: {
  375. thumb: true,
  376. "thumb--value": true,
  377. "thumb--active": this.lastDragProp !== "minMaxValue" && this.dragProp === maxProp
  378. }, onBlur: () => (this.activeProp = null), onFocus: () => (this.activeProp = maxProp), onPointerDown: () => this.dragStart(maxProp), ref: (el) => (this.maxHandle = el), role: "slider", style: { right: rightThumbOffset }, tabIndex: 0 }, h("div", { class: "handle" }), h("span", { "aria-hidden": "true", class: "handle__label handle__label--value" }, value ? value.toLocaleString() : value), h("span", { "aria-hidden": "true", class: "handle__label handle__label--value static" }, value ? value.toLocaleString() : value), h("span", { "aria-hidden": "true", class: "handle__label handle__label--value transformed" }, value ? value.toLocaleString() : value)));
  379. const preciseHandle = (h("div", { "aria-disabled": this.disabled, "aria-label": valueIsRange ? this.maxLabel : this.minLabel, "aria-orientation": "horizontal", "aria-valuemax": this.max, "aria-valuemin": this.min, "aria-valuenow": value, class: {
  380. thumb: true,
  381. "thumb--value": true,
  382. "thumb--active": this.lastDragProp !== "minMaxValue" && this.dragProp === maxProp,
  383. "thumb--precise": true
  384. }, onBlur: () => (this.activeProp = null), onFocus: () => (this.activeProp = maxProp), onPointerDown: () => this.dragStart(maxProp), ref: (el) => (this.maxHandle = el), role: "slider", style: { right: rightThumbOffset }, tabIndex: 0 }, h("div", { class: "handle" }), h("div", { class: "handle-extension" })));
  385. const histogramPreciseHandle = (h("div", { "aria-disabled": this.disabled, "aria-label": valueIsRange ? this.maxLabel : this.minLabel, "aria-orientation": "horizontal", "aria-valuemax": this.max, "aria-valuemin": this.min, "aria-valuenow": value, class: {
  386. thumb: true,
  387. "thumb--value": true,
  388. "thumb--active": this.lastDragProp !== "minMaxValue" && this.dragProp === maxProp,
  389. "thumb--precise": true
  390. }, onBlur: () => (this.activeProp = null), onFocus: () => (this.activeProp = maxProp), onPointerDown: () => this.dragStart(maxProp), ref: (el) => (this.maxHandle = el), role: "slider", style: { right: rightThumbOffset }, tabIndex: 0 }, h("div", { class: "handle-extension" }), h("div", { class: "handle" })));
  391. const labeledPreciseHandle = (h("div", { "aria-disabled": this.disabled, "aria-label": valueIsRange ? this.maxLabel : this.minLabel, "aria-orientation": "horizontal", "aria-valuemax": this.max, "aria-valuemin": this.min, "aria-valuenow": value, class: {
  392. thumb: true,
  393. "thumb--value": true,
  394. "thumb--active": this.lastDragProp !== "minMaxValue" && this.dragProp === maxProp,
  395. "thumb--precise": true
  396. }, onBlur: () => (this.activeProp = null), onFocus: () => (this.activeProp = maxProp), onPointerDown: () => this.dragStart(maxProp), ref: (el) => (this.maxHandle = el), role: "slider", style: { right: rightThumbOffset }, tabIndex: 0 }, h("span", { "aria-hidden": "true", class: "handle__label handle__label--value" }, value ? value.toLocaleString() : value), h("span", { "aria-hidden": "true", class: "handle__label handle__label--value static" }, value ? value.toLocaleString() : value), h("span", { "aria-hidden": "true", class: "handle__label handle__label--value transformed" }, value ? value.toLocaleString() : value), h("div", { class: "handle" }), h("div", { class: "handle-extension" })));
  397. const histogramLabeledPreciseHandle = (h("div", { "aria-disabled": this.disabled, "aria-label": valueIsRange ? this.maxLabel : this.minLabel, "aria-orientation": "horizontal", "aria-valuemax": this.max, "aria-valuemin": this.min, "aria-valuenow": value, class: {
  398. thumb: true,
  399. "thumb--value": true,
  400. "thumb--active": this.lastDragProp !== "minMaxValue" && this.dragProp === maxProp,
  401. "thumb--precise": true
  402. }, onBlur: () => (this.activeProp = null), onFocus: () => (this.activeProp = maxProp), onPointerDown: () => this.dragStart(maxProp), ref: (el) => (this.maxHandle = el), role: "slider", style: { right: rightThumbOffset }, tabIndex: 0 }, h("div", { class: "handle-extension" }), h("div", { class: "handle" }), h("span", { "aria-hidden": "true", class: "handle__label handle__label--value" }, value ? value.toLocaleString() : value), h("span", { "aria-hidden": "true", class: "handle__label handle__label--value static" }, value ? value.toLocaleString() : value), h("span", { "aria-hidden": "true", class: "handle__label handle__label--value transformed" }, value ? value.toLocaleString() : value)));
  403. const minHandle = (h("div", { "aria-disabled": this.disabled, "aria-label": this.minLabel, "aria-orientation": "horizontal", "aria-valuemax": this.max, "aria-valuemin": this.min, "aria-valuenow": this.minValue, class: {
  404. thumb: true,
  405. "thumb--minValue": true,
  406. "thumb--active": this.dragProp === "minValue"
  407. }, onBlur: () => (this.activeProp = null), onFocus: () => (this.activeProp = "minValue"), onPointerDown: () => this.dragStart("minValue"), ref: (el) => (this.minHandle = el), role: "slider", style: { left: leftThumbOffset }, tabIndex: 0 }, h("div", { class: "handle" })));
  408. const minLabeledHandle = (h("div", { "aria-disabled": this.disabled, "aria-label": this.minLabel, "aria-orientation": "horizontal", "aria-valuemax": this.max, "aria-valuemin": this.min, "aria-valuenow": this.minValue, class: {
  409. thumb: true,
  410. "thumb--minValue": true,
  411. "thumb--active": this.dragProp === "minValue"
  412. }, onBlur: () => (this.activeProp = null), onFocus: () => (this.activeProp = "minValue"), onPointerDown: () => this.dragStart("minValue"), ref: (el) => (this.minHandle = el), role: "slider", style: { left: leftThumbOffset }, tabIndex: 0 }, h("span", { "aria-hidden": "true", class: "handle__label handle__label--minValue" }, this.minValue && this.minValue.toLocaleString()), h("span", { "aria-hidden": "true", class: "handle__label handle__label--minValue static" }, this.minValue && this.minValue.toLocaleString()), h("span", { "aria-hidden": "true", class: "handle__label handle__label--minValue transformed" }, this.minValue && this.minValue.toLocaleString()), h("div", { class: "handle" })));
  413. const minHistogramLabeledHandle = (h("div", { "aria-disabled": this.disabled, "aria-label": this.minLabel, "aria-orientation": "horizontal", "aria-valuemax": this.max, "aria-valuemin": this.min, "aria-valuenow": this.minValue, class: {
  414. thumb: true,
  415. "thumb--minValue": true,
  416. "thumb--active": this.dragProp === "minValue"
  417. }, onBlur: () => (this.activeProp = null), onFocus: () => (this.activeProp = "minValue"), onPointerDown: () => this.dragStart("minValue"), ref: (el) => (this.minHandle = el), role: "slider", style: { left: leftThumbOffset }, tabIndex: 0 }, h("div", { class: "handle" }), h("span", { "aria-hidden": "true", class: "handle__label handle__label--minValue" }, this.minValue && this.minValue.toLocaleString()), h("span", { "aria-hidden": "true", class: "handle__label handle__label--minValue static" }, this.minValue && this.minValue.toLocaleString()), h("span", { "aria-hidden": "true", class: "handle__label handle__label--minValue transformed" }, this.minValue && this.minValue.toLocaleString())));
  418. const minPreciseHandle = (h("div", { "aria-disabled": this.disabled, "aria-label": this.minLabel, "aria-orientation": "horizontal", "aria-valuemax": this.max, "aria-valuemin": this.min, "aria-valuenow": this.minValue, class: {
  419. thumb: true,
  420. "thumb--minValue": true,
  421. "thumb--active": this.dragProp === "minValue",
  422. "thumb--precise": true
  423. }, onBlur: () => (this.activeProp = null), onFocus: () => (this.activeProp = "minValue"), onPointerDown: () => this.dragStart("minValue"), ref: (el) => (this.minHandle = el), role: "slider", style: { left: leftThumbOffset }, tabIndex: 0 }, h("div", { class: "handle-extension" }), h("div", { class: "handle" })));
  424. const minLabeledPreciseHandle = (h("div", { "aria-disabled": this.disabled, "aria-label": this.minLabel, "aria-orientation": "horizontal", "aria-valuemax": this.max, "aria-valuemin": this.min, "aria-valuenow": this.minValue, class: {
  425. thumb: true,
  426. "thumb--minValue": true,
  427. "thumb--active": this.dragProp === "minValue",
  428. "thumb--precise": true
  429. }, onBlur: () => (this.activeProp = null), onFocus: () => (this.activeProp = "minValue"), onPointerDown: () => this.dragStart("minValue"), ref: (el) => (this.minHandle = el), role: "slider", style: { left: leftThumbOffset }, tabIndex: 0 }, h("div", { class: "handle-extension" }), h("div", { class: "handle" }), h("span", { "aria-hidden": "true", class: "handle__label handle__label--minValue" }, this.minValue && this.minValue.toLocaleString()), h("span", { "aria-hidden": "true", class: "handle__label handle__label--minValue static" }, this.minValue && this.minValue.toLocaleString()), h("span", { "aria-hidden": "true", class: "handle__label handle__label--minValue transformed" }, this.minValue && this.minValue.toLocaleString())));
  430. return (h(Host, { id: id, onTouchStart: this.handleTouchStart }, h("div", { class: {
  431. ["container"]: true,
  432. ["container--range"]: valueIsRange,
  433. [`scale--${this.scale}`]: true
  434. } }, this.renderGraph(), h("div", { class: "track", ref: this.storeTrackRef }, h("div", { class: "track__range", onPointerDown: () => this.dragStart("minMaxValue"), style: {
  435. left: `${mirror ? 100 - maxInterval : minInterval}%`,
  436. right: `${mirror ? minInterval : 100 - maxInterval}%`
  437. } }), h("div", { class: "ticks" }, this.tickValues.map((tick) => {
  438. const tickOffset = `${this.getUnitInterval(tick) * 100}%`;
  439. let activeTicks = tick >= min && tick <= value;
  440. if (useMinValue) {
  441. activeTicks = tick >= this.minValue && tick <= this.maxValue;
  442. }
  443. return (h("span", { class: {
  444. tick: true,
  445. "tick--active": activeTicks
  446. }, style: {
  447. left: mirror ? "" : tickOffset,
  448. right: mirror ? tickOffset : ""
  449. } }, this.renderTickLabel(tick)));
  450. }))), h("div", { class: "thumb-container" }, !this.precise && !this.labelHandles && valueIsRange && minHandle, !this.hasHistogram &&
  451. !this.precise &&
  452. this.labelHandles &&
  453. valueIsRange &&
  454. minLabeledHandle, this.precise && !this.labelHandles && valueIsRange && minPreciseHandle, this.precise && this.labelHandles && valueIsRange && minLabeledPreciseHandle, this.hasHistogram &&
  455. !this.precise &&
  456. this.labelHandles &&
  457. valueIsRange &&
  458. minHistogramLabeledHandle, !this.precise && !this.labelHandles && handle, !this.hasHistogram && !this.precise && this.labelHandles && labeledHandle, !this.hasHistogram && this.precise && !this.labelHandles && preciseHandle, this.hasHistogram && this.precise && !this.labelHandles && histogramPreciseHandle, !this.hasHistogram && this.precise && this.labelHandles && labeledPreciseHandle, this.hasHistogram && !this.precise && this.labelHandles && histogramLabeledHandle, this.hasHistogram &&
  459. this.precise &&
  460. this.labelHandles &&
  461. histogramLabeledPreciseHandle, h(HiddenFormInputSlot, { component: this })))));
  462. }
  463. renderGraph() {
  464. return this.histogram ? (h("calcite-graph", { class: "graph", colorStops: this.histogramStops, data: this.histogram, highlightMax: isRange(this.value) ? this.maxValue : this.value, highlightMin: isRange(this.value) ? this.minValue : this.min, max: this.max, min: this.min })) : null;
  465. }
  466. renderTickLabel(tick) {
  467. const valueIsRange = isRange(this.value);
  468. const isMinTickLabel = tick === this.min;
  469. const isMaxTickLabel = tick === this.max;
  470. const tickLabel = (h("span", { class: {
  471. tick__label: true,
  472. "tick__label--min": isMinTickLabel,
  473. "tick__label--max": isMaxTickLabel
  474. } }, tick.toLocaleString()));
  475. if (this.labelTicks && !this.hasHistogram && !valueIsRange) {
  476. return tickLabel;
  477. }
  478. if (this.labelTicks &&
  479. !this.hasHistogram &&
  480. valueIsRange &&
  481. !this.precise &&
  482. !this.labelHandles) {
  483. return tickLabel;
  484. }
  485. if (this.labelTicks &&
  486. !this.hasHistogram &&
  487. valueIsRange &&
  488. !this.precise &&
  489. this.labelHandles) {
  490. return tickLabel;
  491. }
  492. if (this.labelTicks &&
  493. !this.hasHistogram &&
  494. valueIsRange &&
  495. this.precise &&
  496. (isMinTickLabel || isMaxTickLabel)) {
  497. return tickLabel;
  498. }
  499. if (this.labelTicks && this.hasHistogram && !this.precise && !this.labelHandles) {
  500. return tickLabel;
  501. }
  502. if (this.labelTicks &&
  503. this.hasHistogram &&
  504. this.precise &&
  505. !this.labelHandles &&
  506. (isMinTickLabel || isMaxTickLabel)) {
  507. return tickLabel;
  508. }
  509. if (this.labelTicks &&
  510. this.hasHistogram &&
  511. !this.precise &&
  512. this.labelHandles &&
  513. (isMinTickLabel || isMaxTickLabel)) {
  514. return tickLabel;
  515. }
  516. if (this.labelTicks &&
  517. this.hasHistogram &&
  518. this.precise &&
  519. this.labelHandles &&
  520. (isMinTickLabel || isMaxTickLabel)) {
  521. return tickLabel;
  522. }
  523. return null;
  524. }
  525. //--------------------------------------------------------------------------
  526. //
  527. // Event Listeners
  528. //
  529. //--------------------------------------------------------------------------
  530. keyDownHandler(event) {
  531. const mirror = this.shouldMirror();
  532. const { activeProp, max, min, pageStep, step } = this;
  533. const value = this[activeProp];
  534. const key = event.key;
  535. if (key === "Enter" || key === " ") {
  536. event.preventDefault();
  537. return;
  538. }
  539. let adjustment;
  540. if (key === "ArrowUp" || key === "ArrowRight") {
  541. const directionFactor = mirror && key === "ArrowRight" ? -1 : 1;
  542. adjustment = value + step * directionFactor;
  543. }
  544. else if (key === "ArrowDown" || key === "ArrowLeft") {
  545. const directionFactor = mirror && key === "ArrowLeft" ? -1 : 1;
  546. adjustment = value - step * directionFactor;
  547. }
  548. else if (key === "PageUp") {
  549. if (pageStep) {
  550. adjustment = value + pageStep;
  551. }
  552. }
  553. else if (key === "PageDown") {
  554. if (pageStep) {
  555. adjustment = value - pageStep;
  556. }
  557. }
  558. else if (key === "Home") {
  559. adjustment = min;
  560. }
  561. else if (key === "End") {
  562. adjustment = max;
  563. }
  564. if (isNaN(adjustment)) {
  565. return;
  566. }
  567. event.preventDefault();
  568. const fixedDecimalAdjustment = Number(adjustment.toFixed(decimalPlaces(step)));
  569. this.setValue(activeProp, this.clamp(fixedDecimalAdjustment, activeProp));
  570. }
  571. clickHandler(event) {
  572. this.focusActiveHandle(event.clientX);
  573. }
  574. pointerDownHandler(event) {
  575. const x = event.clientX || event.pageX;
  576. const position = this.translate(x);
  577. let prop = "value";
  578. if (isRange(this.value)) {
  579. const inRange = position >= this.minValue && position <= this.maxValue;
  580. if (inRange && this.lastDragProp === "minMaxValue") {
  581. prop = "minMaxValue";
  582. }
  583. else {
  584. const closerToMax = Math.abs(this.maxValue - position) < Math.abs(this.minValue - position);
  585. prop = closerToMax || position > this.maxValue ? "maxValue" : "minValue";
  586. }
  587. }
  588. this.lastDragPropValue = this[prop];
  589. this.dragStart(prop);
  590. const isThumbActive = this.el.shadowRoot.querySelector(".thumb:active");
  591. if (!isThumbActive) {
  592. this.setValue(prop, this.clamp(position, prop));
  593. }
  594. }
  595. handleTouchStart(event) {
  596. // needed to prevent extra click at the end of a handle drag
  597. event.preventDefault();
  598. }
  599. //--------------------------------------------------------------------------
  600. //
  601. // Public Methods
  602. //
  603. //--------------------------------------------------------------------------
  604. /** Sets focus on the component. */
  605. async setFocus() {
  606. const handle = this.minHandle ? this.minHandle : this.maxHandle;
  607. handle.focus();
  608. }
  609. //--------------------------------------------------------------------------
  610. //
  611. // Private Methods
  612. //
  613. //--------------------------------------------------------------------------
  614. setValueFromMinMax() {
  615. const { minValue, maxValue } = this;
  616. if (typeof minValue === "number" && typeof maxValue === "number") {
  617. this.value = [minValue, maxValue];
  618. }
  619. }
  620. setMinMaxFromValue() {
  621. const { value } = this;
  622. if (isRange(value)) {
  623. this.minValue = value[0];
  624. this.maxValue = value[1];
  625. }
  626. }
  627. onLabelClick() {
  628. this.setFocus();
  629. }
  630. shouldMirror() {
  631. return this.mirrored && !this.hasHistogram;
  632. }
  633. shouldUseMinValue() {
  634. if (!isRange(this.value)) {
  635. return false;
  636. }
  637. return ((this.hasHistogram && this.maxValue === 0) || (!this.hasHistogram && this.minValue === 0));
  638. }
  639. generateTickValues() {
  640. const ticks = [];
  641. let current = this.min;
  642. while (this.ticks && current < this.max + this.ticks) {
  643. ticks.push(Math.min(current, this.max));
  644. current = current + this.ticks;
  645. }
  646. return ticks;
  647. }
  648. dragStart(prop) {
  649. this.dragProp = prop;
  650. this.lastDragProp = this.dragProp;
  651. this.activeProp = prop;
  652. document.addEventListener("pointermove", this.dragUpdate);
  653. document.addEventListener("pointerup", this.dragEnd);
  654. document.addEventListener("pointercancel", this.dragEnd);
  655. }
  656. focusActiveHandle(valueX) {
  657. switch (this.dragProp) {
  658. case "minValue":
  659. this.minHandle.focus();
  660. break;
  661. case "maxValue":
  662. this.maxHandle.focus();
  663. break;
  664. case "minMaxValue":
  665. this.getClosestHandle(valueX).focus();
  666. break;
  667. }
  668. }
  669. emitInput() {
  670. this.calciteSliderInput.emit();
  671. this.calciteSliderUpdate.emit();
  672. }
  673. emitChange() {
  674. this.calciteSliderChange.emit();
  675. }
  676. removeDragListeners() {
  677. document.removeEventListener("pointermove", this.dragUpdate);
  678. document.removeEventListener("pointerup", this.dragEnd);
  679. document.removeEventListener("pointercancel", this.dragEnd);
  680. }
  681. /**
  682. * Set the prop value if changed at the component level
  683. * @param valueProp
  684. * @param value
  685. */
  686. setValue(valueProp, value) {
  687. const oldValue = this[valueProp];
  688. const valueChanged = oldValue !== value;
  689. if (!valueChanged) {
  690. return;
  691. }
  692. this[valueProp] = value;
  693. const dragging = this.dragProp;
  694. if (!dragging) {
  695. this.emitChange();
  696. }
  697. this.emitInput();
  698. }
  699. /**
  700. * If number is outside range, constrain to min or max
  701. * @internal
  702. */
  703. clamp(value, prop) {
  704. value = clamp(value, this.min, this.max);
  705. // ensure that maxValue and minValue don't swap positions
  706. if (prop === "maxValue") {
  707. value = Math.max(value, this.minValue);
  708. }
  709. if (prop === "minValue") {
  710. value = Math.min(value, this.maxValue);
  711. }
  712. return value;
  713. }
  714. /**
  715. * Translate a pixel position to value along the range
  716. * @internal
  717. */
  718. translate(x) {
  719. const range = this.max - this.min;
  720. const { left, width } = this.trackEl.getBoundingClientRect();
  721. const percent = (x - left) / width;
  722. const mirror = this.shouldMirror();
  723. const clampedValue = this.clamp(this.min + range * (mirror ? 1 - percent : percent));
  724. let value = Number(clampedValue.toFixed(decimalPlaces(this.step)));
  725. if (this.snap && this.step) {
  726. value = this.getClosestStep(value);
  727. }
  728. return value;
  729. }
  730. /**
  731. * Get closest allowed value along stepped values
  732. * @internal
  733. */
  734. getClosestStep(num) {
  735. num = Number(this.clamp(num).toFixed(decimalPlaces(this.step)));
  736. if (this.step) {
  737. const step = Math.round(num / this.step) * this.step;
  738. num = Number(this.clamp(step).toFixed(decimalPlaces(this.step)));
  739. }
  740. return num;
  741. }
  742. getClosestHandle(valueX) {
  743. return this.getDistanceX(this.maxHandle, valueX) > this.getDistanceX(this.minHandle, valueX)
  744. ? this.minHandle
  745. : this.maxHandle;
  746. }
  747. getDistanceX(el, valueX) {
  748. return Math.abs(el.getBoundingClientRect().left - valueX);
  749. }
  750. getFontSizeForElement(element) {
  751. return Number(window.getComputedStyle(element).getPropertyValue("font-size").match(/\d+/)[0]);
  752. }
  753. /**
  754. * Get position of value along range as fractional value
  755. * @return {number} number in the unit interval [0,1]
  756. * @internal
  757. */
  758. getUnitInterval(num) {
  759. num = this.clamp(num);
  760. const range = this.max - this.min;
  761. return (num - this.min) / range;
  762. }
  763. adjustHostObscuredHandleLabel(name) {
  764. const label = this.el.shadowRoot.querySelector(`.handle__label--${name}`);
  765. const labelStatic = this.el.shadowRoot.querySelector(`.handle__label--${name}.static`);
  766. const labelTransformed = this.el.shadowRoot.querySelector(`.handle__label--${name}.transformed`);
  767. const labelStaticBounds = labelStatic.getBoundingClientRect();
  768. const labelStaticOffset = this.getHostOffset(labelStaticBounds.left, labelStaticBounds.right);
  769. label.style.transform = `translateX(${labelStaticOffset}px)`;
  770. labelTransformed.style.transform = `translateX(${labelStaticOffset}px)`;
  771. }
  772. hyphenateCollidingRangeHandleLabels() {
  773. const { shadowRoot } = this.el;
  774. const mirror = this.shouldMirror();
  775. const leftModifier = mirror ? "value" : "minValue";
  776. const rightModifier = mirror ? "minValue" : "value";
  777. const leftValueLabel = shadowRoot.querySelector(`.handle__label--${leftModifier}`);
  778. const leftValueLabelStatic = shadowRoot.querySelector(`.handle__label--${leftModifier}.static`);
  779. const leftValueLabelTransformed = shadowRoot.querySelector(`.handle__label--${leftModifier}.transformed`);
  780. const leftValueLabelStaticHostOffset = this.getHostOffset(leftValueLabelStatic.getBoundingClientRect().left, leftValueLabelStatic.getBoundingClientRect().right);
  781. const rightValueLabel = shadowRoot.querySelector(`.handle__label--${rightModifier}`);
  782. const rightValueLabelStatic = shadowRoot.querySelector(`.handle__label--${rightModifier}.static`);
  783. const rightValueLabelTransformed = shadowRoot.querySelector(`.handle__label--${rightModifier}.transformed`);
  784. const rightValueLabelStaticHostOffset = this.getHostOffset(rightValueLabelStatic.getBoundingClientRect().left, rightValueLabelStatic.getBoundingClientRect().right);
  785. const labelFontSize = this.getFontSizeForElement(leftValueLabel);
  786. const labelTransformedOverlap = this.getRangeLabelOverlap(leftValueLabelTransformed, rightValueLabelTransformed);
  787. const hyphenLabel = leftValueLabel;
  788. const labelOffset = labelFontSize / 2;
  789. if (labelTransformedOverlap > 0) {
  790. hyphenLabel.classList.add("hyphen", "hyphen--wrap");
  791. if (rightValueLabelStaticHostOffset === 0 && leftValueLabelStaticHostOffset === 0) {
  792. // Neither handle overlaps the host boundary
  793. let leftValueLabelTranslate = labelTransformedOverlap / 2 - labelOffset;
  794. leftValueLabelTranslate =
  795. Math.sign(leftValueLabelTranslate) === -1
  796. ? Math.abs(leftValueLabelTranslate)
  797. : -leftValueLabelTranslate;
  798. const leftValueLabelTransformedHostOffset = this.getHostOffset(leftValueLabelTransformed.getBoundingClientRect().left +
  799. leftValueLabelTranslate -
  800. labelOffset, leftValueLabelTransformed.getBoundingClientRect().right +
  801. leftValueLabelTranslate -
  802. labelOffset);
  803. let rightValueLabelTranslate = labelTransformedOverlap / 2;
  804. const rightValueLabelTransformedHostOffset = this.getHostOffset(rightValueLabelTransformed.getBoundingClientRect().left + rightValueLabelTranslate, rightValueLabelTransformed.getBoundingClientRect().right + rightValueLabelTranslate);
  805. if (leftValueLabelTransformedHostOffset !== 0) {
  806. leftValueLabelTranslate += leftValueLabelTransformedHostOffset;
  807. rightValueLabelTranslate += leftValueLabelTransformedHostOffset;
  808. }
  809. if (rightValueLabelTransformedHostOffset !== 0) {
  810. leftValueLabelTranslate += rightValueLabelTransformedHostOffset;
  811. rightValueLabelTranslate += rightValueLabelTransformedHostOffset;
  812. }
  813. leftValueLabel.style.transform = `translateX(${leftValueLabelTranslate}px)`;
  814. leftValueLabelTransformed.style.transform = `translateX(${leftValueLabelTranslate - labelOffset}px)`;
  815. rightValueLabel.style.transform = `translateX(${rightValueLabelTranslate}px)`;
  816. rightValueLabelTransformed.style.transform = `translateX(${rightValueLabelTranslate}px)`;
  817. }
  818. else if (leftValueLabelStaticHostOffset > 0 || rightValueLabelStaticHostOffset > 0) {
  819. // labels overlap host boundary on the left side
  820. leftValueLabel.style.transform = `translateX(${leftValueLabelStaticHostOffset + labelOffset}px)`;
  821. rightValueLabel.style.transform = `translateX(${labelTransformedOverlap + rightValueLabelStaticHostOffset}px)`;
  822. rightValueLabelTransformed.style.transform = `translateX(${labelTransformedOverlap + rightValueLabelStaticHostOffset}px)`;
  823. }
  824. else if (leftValueLabelStaticHostOffset < 0 || rightValueLabelStaticHostOffset < 0) {
  825. // labels overlap host boundary on the right side
  826. let leftValueLabelTranslate = Math.abs(leftValueLabelStaticHostOffset) + labelTransformedOverlap - labelOffset;
  827. leftValueLabelTranslate =
  828. Math.sign(leftValueLabelTranslate) === -1
  829. ? Math.abs(leftValueLabelTranslate)
  830. : -leftValueLabelTranslate;
  831. leftValueLabel.style.transform = `translateX(${leftValueLabelTranslate}px)`;
  832. leftValueLabelTransformed.style.transform = `translateX(${leftValueLabelTranslate - labelOffset}px)`;
  833. }
  834. }
  835. else {
  836. hyphenLabel.classList.remove("hyphen", "hyphen--wrap");
  837. leftValueLabel.style.transform = `translateX(${leftValueLabelStaticHostOffset}px)`;
  838. leftValueLabelTransformed.style.transform = `translateX(${leftValueLabelStaticHostOffset}px)`;
  839. rightValueLabel.style.transform = `translateX(${rightValueLabelStaticHostOffset}px)`;
  840. rightValueLabelTransformed.style.transform = `translateX(${rightValueLabelStaticHostOffset}px)`;
  841. }
  842. }
  843. /**
  844. * Hides bounding tick labels that are obscured by either handle.
  845. */
  846. hideObscuredBoundingTickLabels() {
  847. const valueIsRange = isRange(this.value);
  848. if (!this.hasHistogram && !valueIsRange && !this.labelHandles && !this.precise) {
  849. return;
  850. }
  851. if (!this.hasHistogram && !valueIsRange && this.labelHandles && !this.precise) {
  852. return;
  853. }
  854. if (!this.hasHistogram && !valueIsRange && !this.labelHandles && this.precise) {
  855. return;
  856. }
  857. if (!this.hasHistogram && !valueIsRange && this.labelHandles && this.precise) {
  858. return;
  859. }
  860. if (!this.hasHistogram && valueIsRange && !this.precise) {
  861. return;
  862. }
  863. if (this.hasHistogram && !this.precise && !this.labelHandles) {
  864. return;
  865. }
  866. const minHandle = this.el.shadowRoot.querySelector(".thumb--minValue");
  867. const maxHandle = this.el.shadowRoot.querySelector(".thumb--value");
  868. const minTickLabel = this.el.shadowRoot.querySelector(".tick__label--min");
  869. const maxTickLabel = this.el.shadowRoot.querySelector(".tick__label--max");
  870. if (!minHandle && maxHandle && minTickLabel && maxTickLabel) {
  871. minTickLabel.style.opacity = this.isMinTickLabelObscured(minTickLabel, maxHandle) ? "0" : "1";
  872. maxTickLabel.style.opacity = this.isMaxTickLabelObscured(maxTickLabel, maxHandle) ? "0" : "1";
  873. }
  874. if (minHandle && maxHandle && minTickLabel && maxTickLabel) {
  875. minTickLabel.style.opacity =
  876. this.isMinTickLabelObscured(minTickLabel, minHandle) ||
  877. this.isMinTickLabelObscured(minTickLabel, maxHandle)
  878. ? "0"
  879. : "1";
  880. maxTickLabel.style.opacity =
  881. this.isMaxTickLabelObscured(maxTickLabel, minHandle) ||
  882. (this.isMaxTickLabelObscured(maxTickLabel, maxHandle) && this.hasHistogram)
  883. ? "0"
  884. : "1";
  885. }
  886. }
  887. /**
  888. * Returns an integer representing the number of pixels to offset on the left or right side based on desired position behavior.
  889. * @internal
  890. */
  891. getHostOffset(leftBounds, rightBounds) {
  892. const hostBounds = this.el.getBoundingClientRect();
  893. const buffer = 7;
  894. if (leftBounds + buffer < hostBounds.left) {
  895. return hostBounds.left - leftBounds - buffer;
  896. }
  897. if (rightBounds - buffer > hostBounds.right) {
  898. return -(rightBounds - hostBounds.right) + buffer;
  899. }
  900. return 0;
  901. }
  902. /**
  903. * Returns an integer representing the number of pixels that the two given span elements are overlapping, taking into account
  904. * a space in between the two spans equal to the font-size set on them to account for the space needed to render a hyphen.
  905. * @param leftLabel
  906. * @param rightLabel
  907. */
  908. getRangeLabelOverlap(leftLabel, rightLabel) {
  909. const leftLabelBounds = leftLabel.getBoundingClientRect();
  910. const rightLabelBounds = rightLabel.getBoundingClientRect();
  911. const leftLabelFontSize = this.getFontSizeForElement(leftLabel);
  912. const rangeLabelOverlap = leftLabelBounds.right + leftLabelFontSize - rightLabelBounds.left;
  913. return Math.max(rangeLabelOverlap, 0);
  914. }
  915. /**
  916. * Returns a boolean value representing if the minLabel span element is obscured (being overlapped) by the given handle div element.
  917. * @param minLabel
  918. * @param handle
  919. */
  920. isMinTickLabelObscured(minLabel, handle) {
  921. const minLabelBounds = minLabel.getBoundingClientRect();
  922. const handleBounds = handle.getBoundingClientRect();
  923. return intersects(minLabelBounds, handleBounds);
  924. }
  925. /**
  926. * Returns a boolean value representing if the maxLabel span element is obscured (being overlapped) by the given handle div element.
  927. * @param maxLabel
  928. * @param handle
  929. */
  930. isMaxTickLabelObscured(maxLabel, handle) {
  931. const maxLabelBounds = maxLabel.getBoundingClientRect();
  932. const handleBounds = handle.getBoundingClientRect();
  933. return intersects(maxLabelBounds, handleBounds);
  934. }
  935. get el() { return getElement(this); }
  936. static get watchers() { return {
  937. "histogram": ["histogramWatcher"],
  938. "value": ["valueHandler"],
  939. "minValue": ["minMaxValueHandler"],
  940. "maxValue": ["minMaxValueHandler"]
  941. }; }
  942. };
  943. Slider.style = sliderCss;
  944. export { Graph as calcite_graph, Slider as calcite_slider };