RangeSelector.js 83 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198
  1. /* *
  2. *
  3. * (c) 2010-2021 Torstein Honsi
  4. *
  5. * License: www.highcharts.com/license
  6. *
  7. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  8. *
  9. * */
  10. 'use strict';
  11. import Axis from '../Core/Axis/Axis.js';
  12. import Chart from '../Core/Chart/Chart.js';
  13. import H from '../Core/Globals.js';
  14. import O from '../Core/Options.js';
  15. var defaultOptions = O.defaultOptions;
  16. import palette from '../Core/Color/Palette.js';
  17. import SVGElement from '../Core/Renderer/SVG/SVGElement.js';
  18. import U from '../Core/Utilities.js';
  19. var addEvent = U.addEvent, createElement = U.createElement, css = U.css, defined = U.defined, destroyObjectProperties = U.destroyObjectProperties, discardElement = U.discardElement, extend = U.extend, find = U.find, fireEvent = U.fireEvent, isNumber = U.isNumber, merge = U.merge, objectEach = U.objectEach, pad = U.pad, pick = U.pick, pInt = U.pInt, splat = U.splat;
  20. /**
  21. * Define the time span for the button
  22. *
  23. * @typedef {"all"|"day"|"hour"|"millisecond"|"minute"|"month"|"second"|"week"|"year"|"ytd"} Highcharts.RangeSelectorButtonTypeValue
  24. */
  25. /**
  26. * Callback function to react on button clicks.
  27. *
  28. * @callback Highcharts.RangeSelectorClickCallbackFunction
  29. *
  30. * @param {global.Event} e
  31. * Event arguments.
  32. *
  33. * @param {boolean|undefined}
  34. * Return false to cancel the default button event.
  35. */
  36. /**
  37. * Callback function to parse values entered in the input boxes and return a
  38. * valid JavaScript time as milliseconds since 1970.
  39. *
  40. * @callback Highcharts.RangeSelectorParseCallbackFunction
  41. *
  42. * @param {string} value
  43. * Input value to parse.
  44. *
  45. * @return {number}
  46. * Parsed JavaScript time value.
  47. */
  48. /* ************************************************************************** *
  49. * Start Range Selector code *
  50. * ************************************************************************** */
  51. extend(defaultOptions, {
  52. /**
  53. * The range selector is a tool for selecting ranges to display within
  54. * the chart. It provides buttons to select preconfigured ranges in
  55. * the chart, like 1 day, 1 week, 1 month etc. It also provides input
  56. * boxes where min and max dates can be manually input.
  57. *
  58. * @product highstock gantt
  59. * @optionparent rangeSelector
  60. */
  61. rangeSelector: {
  62. /**
  63. * Whether to enable all buttons from the start. By default buttons are
  64. * only enabled if the corresponding time range exists on the X axis,
  65. * but enabling all buttons allows for dynamically loading different
  66. * time ranges.
  67. *
  68. * @sample {highstock} stock/rangeselector/allbuttonsenabled-true/
  69. * All buttons enabled
  70. *
  71. * @since 2.0.3
  72. */
  73. allButtonsEnabled: false,
  74. /**
  75. * An array of configuration objects for the buttons.
  76. *
  77. * Defaults to:
  78. * ```js
  79. * buttons: [{
  80. * type: 'month',
  81. * count: 1,
  82. * text: '1m',
  83. * title: 'View 1 month'
  84. * }, {
  85. * type: 'month',
  86. * count: 3,
  87. * text: '3m',
  88. * title: 'View 3 months'
  89. * }, {
  90. * type: 'month',
  91. * count: 6,
  92. * text: '6m',
  93. * title: 'View 6 months'
  94. * }, {
  95. * type: 'ytd',
  96. * text: 'YTD',
  97. * title: 'View year to date'
  98. * }, {
  99. * type: 'year',
  100. * count: 1,
  101. * text: '1y',
  102. * title: 'View 1 year'
  103. * }, {
  104. * type: 'all',
  105. * text: 'All',
  106. * title: 'View all'
  107. * }]
  108. * ```
  109. *
  110. * @sample {highstock} stock/rangeselector/datagrouping/
  111. * Data grouping by buttons
  112. *
  113. * @type {Array<*>}
  114. */
  115. buttons: void 0,
  116. /**
  117. * How many units of the defined type the button should span. If `type`
  118. * is "month" and `count` is 3, the button spans three months.
  119. *
  120. * @type {number}
  121. * @default 1
  122. * @apioption rangeSelector.buttons.count
  123. */
  124. /**
  125. * Fires when clicking on the rangeSelector button. One parameter,
  126. * event, is passed to the function, containing common event
  127. * information.
  128. *
  129. * ```js
  130. * click: function(e) {
  131. * console.log(this);
  132. * }
  133. * ```
  134. *
  135. * Return false to stop default button's click action.
  136. *
  137. * @sample {highstock} stock/rangeselector/button-click/
  138. * Click event on the button
  139. *
  140. * @type {Highcharts.RangeSelectorClickCallbackFunction}
  141. * @apioption rangeSelector.buttons.events.click
  142. */
  143. /**
  144. * Additional range (in milliseconds) added to the end of the calculated
  145. * time span.
  146. *
  147. * @sample {highstock} stock/rangeselector/min-max-offsets/
  148. * Button offsets
  149. *
  150. * @type {number}
  151. * @default 0
  152. * @since 6.0.0
  153. * @apioption rangeSelector.buttons.offsetMax
  154. */
  155. /**
  156. * Additional range (in milliseconds) added to the start of the
  157. * calculated time span.
  158. *
  159. * @sample {highstock} stock/rangeselector/min-max-offsets/
  160. * Button offsets
  161. *
  162. * @type {number}
  163. * @default 0
  164. * @since 6.0.0
  165. * @apioption rangeSelector.buttons.offsetMin
  166. */
  167. /**
  168. * When buttons apply dataGrouping on a series, by default zooming
  169. * in/out will deselect buttons and unset dataGrouping. Enable this
  170. * option to keep buttons selected when extremes change.
  171. *
  172. * @sample {highstock} stock/rangeselector/preserve-datagrouping/
  173. * Different preserveDataGrouping settings
  174. *
  175. * @type {boolean}
  176. * @default false
  177. * @since 6.1.2
  178. * @apioption rangeSelector.buttons.preserveDataGrouping
  179. */
  180. /**
  181. * A custom data grouping object for each button.
  182. *
  183. * @see [series.dataGrouping](#plotOptions.series.dataGrouping)
  184. *
  185. * @sample {highstock} stock/rangeselector/datagrouping/
  186. * Data grouping by range selector buttons
  187. *
  188. * @type {*}
  189. * @extends plotOptions.series.dataGrouping
  190. * @apioption rangeSelector.buttons.dataGrouping
  191. */
  192. /**
  193. * The text for the button itself.
  194. *
  195. * @type {string}
  196. * @apioption rangeSelector.buttons.text
  197. */
  198. /**
  199. * Explanation for the button, shown as a tooltip on hover, and used by
  200. * assistive technology.
  201. *
  202. * @type {string}
  203. * @apioption rangeSelector.buttons.title
  204. */
  205. /**
  206. * Defined the time span for the button. Can be one of `millisecond`,
  207. * `second`, `minute`, `hour`, `day`, `week`, `month`, `year`, `ytd`,
  208. * and `all`.
  209. *
  210. * @type {Highcharts.RangeSelectorButtonTypeValue}
  211. * @apioption rangeSelector.buttons.type
  212. */
  213. /**
  214. * The space in pixels between the buttons in the range selector.
  215. */
  216. buttonSpacing: 5,
  217. /**
  218. * Whether to collapse the range selector buttons into a dropdown when
  219. * there is not enough room to show everything in a single row, instead
  220. * of dividing the range selector into multiple rows.
  221. * Can be one of the following:
  222. * - `always`: Always collapse
  223. * - `responsive`: Only collapse when there is not enough room
  224. * - `never`: Never collapse
  225. *
  226. * @sample {highstock} stock/rangeselector/dropdown/
  227. * Dropdown option
  228. *
  229. * @validvalue ["always", "responsive", "never"]
  230. * @since 9.0.0
  231. */
  232. dropdown: 'responsive',
  233. /**
  234. * Enable or disable the range selector. Default to `true` for stock
  235. * charts, using the `stockChart` factory.
  236. *
  237. * @sample {highstock} stock/rangeselector/enabled/
  238. * Disable the range selector
  239. *
  240. * @type {boolean|undefined}
  241. * @default {highstock} true
  242. */
  243. enabled: void 0,
  244. /**
  245. * The vertical alignment of the rangeselector box. Allowed properties
  246. * are `top`, `middle`, `bottom`.
  247. *
  248. * @sample {highstock} stock/rangeselector/vertical-align-middle/
  249. * Middle
  250. * @sample {highstock} stock/rangeselector/vertical-align-bottom/
  251. * Bottom
  252. *
  253. * @type {Highcharts.VerticalAlignValue}
  254. * @since 6.0.0
  255. */
  256. verticalAlign: 'top',
  257. /**
  258. * A collection of attributes for the buttons. The object takes SVG
  259. * attributes like `fill`, `stroke`, `stroke-width`, as well as `style`,
  260. * a collection of CSS properties for the text.
  261. *
  262. * The object can also be extended with states, so you can set
  263. * presentational options for `hover`, `select` or `disabled` button
  264. * states.
  265. *
  266. * CSS styles for the text label.
  267. *
  268. * In styled mode, the buttons are styled by the
  269. * `.highcharts-range-selector-buttons .highcharts-button` rule with its
  270. * different states.
  271. *
  272. * @sample {highstock} stock/rangeselector/styling/
  273. * Styling the buttons and inputs
  274. *
  275. * @type {Highcharts.SVGAttributes}
  276. */
  277. buttonTheme: {
  278. /** @ignore */
  279. width: 28,
  280. /** @ignore */
  281. height: 18,
  282. /** @ignore */
  283. padding: 2,
  284. /** @ignore */
  285. zIndex: 7 // #484, #852
  286. },
  287. /**
  288. * When the rangeselector is floating, the plot area does not reserve
  289. * space for it. This opens for positioning anywhere on the chart.
  290. *
  291. * @sample {highstock} stock/rangeselector/floating/
  292. * Placing the range selector between the plot area and the
  293. * navigator
  294. *
  295. * @since 6.0.0
  296. */
  297. floating: false,
  298. /**
  299. * The x offset of the range selector relative to its horizontal
  300. * alignment within `chart.spacingLeft` and `chart.spacingRight`.
  301. *
  302. * @since 6.0.0
  303. */
  304. x: 0,
  305. /**
  306. * The y offset of the range selector relative to its horizontal
  307. * alignment within `chart.spacingLeft` and `chart.spacingRight`.
  308. *
  309. * @since 6.0.0
  310. */
  311. y: 0,
  312. /**
  313. * Deprecated. The height of the range selector. Currently it is
  314. * calculated dynamically.
  315. *
  316. * @deprecated
  317. * @type {number|undefined}
  318. * @since 2.1.9
  319. */
  320. height: void 0,
  321. /**
  322. * The border color of the date input boxes.
  323. *
  324. * @sample {highstock} stock/rangeselector/styling/
  325. * Styling the buttons and inputs
  326. *
  327. * @type {Highcharts.ColorString}
  328. * @since 1.3.7
  329. */
  330. inputBoxBorderColor: 'none',
  331. /**
  332. * The pixel height of the date input boxes.
  333. *
  334. * @sample {highstock} stock/rangeselector/styling/
  335. * Styling the buttons and inputs
  336. *
  337. * @since 1.3.7
  338. */
  339. inputBoxHeight: 17,
  340. /**
  341. * The pixel width of the date input boxes. When `undefined`, the width
  342. * is fitted to the rendered content.
  343. *
  344. * @sample {highstock} stock/rangeselector/styling/
  345. * Styling the buttons and inputs
  346. *
  347. * @type {number|undefined}
  348. * @since 1.3.7
  349. */
  350. inputBoxWidth: void 0,
  351. /**
  352. * The date format in the input boxes when not selected for editing.
  353. * Defaults to `%b %e, %Y`.
  354. *
  355. * This is used to determine which type of input to show,
  356. * `datetime-local`, `date` or `time` and falling back to `text` when
  357. * the browser does not support the input type or the format contains
  358. * milliseconds.
  359. *
  360. * @sample {highstock} stock/rangeselector/input-type/
  361. * Input types
  362. * @sample {highstock} stock/rangeselector/input-format/
  363. * Milliseconds in the range selector
  364. *
  365. */
  366. inputDateFormat: '%b %e, %Y',
  367. /**
  368. * A custom callback function to parse values entered in the input boxes
  369. * and return a valid JavaScript time as milliseconds since 1970.
  370. * The first argument passed is a value to parse,
  371. * second is a boolean indicating use of the UTC time.
  372. *
  373. * This will only get called for inputs of type `text`. Since v8.2.3,
  374. * the input type is dynamically determined based on the granularity
  375. * of the `inputDateFormat` and the browser support.
  376. *
  377. * @sample {highstock} stock/rangeselector/input-format/
  378. * Milliseconds in the range selector
  379. *
  380. * @type {Highcharts.RangeSelectorParseCallbackFunction}
  381. * @since 1.3.3
  382. */
  383. inputDateParser: void 0,
  384. /**
  385. * The date format in the input boxes when they are selected for
  386. * editing. This must be a format that is recognized by JavaScript
  387. * Date.parse.
  388. *
  389. * This will only be used for inputs of type `text`. Since v8.2.3,
  390. * the input type is dynamically determined based on the granularity
  391. * of the `inputDateFormat` and the browser support.
  392. *
  393. * @sample {highstock} stock/rangeselector/input-format/
  394. * Milliseconds in the range selector
  395. *
  396. */
  397. inputEditDateFormat: '%Y-%m-%d',
  398. /**
  399. * Enable or disable the date input boxes.
  400. */
  401. inputEnabled: true,
  402. /**
  403. * Positioning for the input boxes. Allowed properties are `align`,
  404. * `x` and `y`.
  405. *
  406. * @since 1.2.4
  407. */
  408. inputPosition: {
  409. /**
  410. * The alignment of the input box. Allowed properties are `left`,
  411. * `center`, `right`.
  412. *
  413. * @sample {highstock} stock/rangeselector/input-button-position/
  414. * Alignment
  415. *
  416. * @type {Highcharts.AlignValue}
  417. * @since 6.0.0
  418. */
  419. align: 'right',
  420. /**
  421. * X offset of the input row.
  422. */
  423. x: 0,
  424. /**
  425. * Y offset of the input row.
  426. */
  427. y: 0
  428. },
  429. /**
  430. * The space in pixels between the labels and the date input boxes in
  431. * the range selector.
  432. *
  433. * @since 9.0.0
  434. */
  435. inputSpacing: 5,
  436. /**
  437. * The index of the button to appear pre-selected.
  438. *
  439. * @type {number}
  440. */
  441. selected: void 0,
  442. /**
  443. * Positioning for the button row.
  444. *
  445. * @since 1.2.4
  446. */
  447. buttonPosition: {
  448. /**
  449. * The alignment of the input box. Allowed properties are `left`,
  450. * `center`, `right`.
  451. *
  452. * @sample {highstock} stock/rangeselector/input-button-position/
  453. * Alignment
  454. *
  455. * @type {Highcharts.AlignValue}
  456. * @since 6.0.0
  457. */
  458. align: 'left',
  459. /**
  460. * X offset of the button row.
  461. */
  462. x: 0,
  463. /**
  464. * Y offset of the button row.
  465. */
  466. y: 0
  467. },
  468. /**
  469. * CSS for the HTML inputs in the range selector.
  470. *
  471. * In styled mode, the inputs are styled by the
  472. * `.highcharts-range-input text` rule in SVG mode, and
  473. * `input.highcharts-range-selector` when active.
  474. *
  475. * @sample {highstock} stock/rangeselector/styling/
  476. * Styling the buttons and inputs
  477. *
  478. * @type {Highcharts.CSSObject}
  479. * @apioption rangeSelector.inputStyle
  480. */
  481. inputStyle: {
  482. /** @ignore */
  483. color: palette.highlightColor80,
  484. /** @ignore */
  485. cursor: 'pointer'
  486. },
  487. /**
  488. * CSS styles for the labels - the Zoom, From and To texts.
  489. *
  490. * In styled mode, the labels are styled by the
  491. * `.highcharts-range-label` class.
  492. *
  493. * @sample {highstock} stock/rangeselector/styling/
  494. * Styling the buttons and inputs
  495. *
  496. * @type {Highcharts.CSSObject}
  497. */
  498. labelStyle: {
  499. /** @ignore */
  500. color: palette.neutralColor60
  501. }
  502. }
  503. });
  504. extend(defaultOptions.lang,
  505. /**
  506. * Language object. The language object is global and it can't be set
  507. * on each chart initialization. Instead, use `Highcharts.setOptions` to
  508. * set it before any chart is initialized.
  509. *
  510. * ```js
  511. * Highcharts.setOptions({
  512. * lang: {
  513. * months: [
  514. * 'Janvier', 'Février', 'Mars', 'Avril',
  515. * 'Mai', 'Juin', 'Juillet', 'Août',
  516. * 'Septembre', 'Octobre', 'Novembre', 'Décembre'
  517. * ],
  518. * weekdays: [
  519. * 'Dimanche', 'Lundi', 'Mardi', 'Mercredi',
  520. * 'Jeudi', 'Vendredi', 'Samedi'
  521. * ]
  522. * }
  523. * });
  524. * ```
  525. *
  526. * @optionparent lang
  527. */
  528. {
  529. /**
  530. * The text for the label for the range selector buttons.
  531. *
  532. * @product highstock gantt
  533. */
  534. rangeSelectorZoom: 'Zoom',
  535. /**
  536. * The text for the label for the "from" input box in the range
  537. * selector. Since v9.0, this string is empty as the label is not
  538. * rendered by default.
  539. *
  540. * @product highstock gantt
  541. */
  542. rangeSelectorFrom: '',
  543. /**
  544. * The text for the label for the "to" input box in the range selector.
  545. *
  546. * @product highstock gantt
  547. */
  548. rangeSelectorTo: '→'
  549. });
  550. /* eslint-disable no-invalid-this, valid-jsdoc */
  551. /**
  552. * The range selector.
  553. *
  554. * @private
  555. * @class
  556. * @name Highcharts.RangeSelector
  557. * @param {Highcharts.Chart} chart
  558. */
  559. var RangeSelector = /** @class */ (function () {
  560. function RangeSelector(chart) {
  561. /* *
  562. *
  563. * Properties
  564. *
  565. * */
  566. this.buttons = void 0;
  567. this.buttonOptions = RangeSelector.prototype.defaultButtons;
  568. this.initialButtonGroupWidth = 0;
  569. this.options = void 0;
  570. this.chart = chart;
  571. // Run RangeSelector
  572. this.init(chart);
  573. }
  574. /**
  575. * The method to run when one of the buttons in the range selectors is
  576. * clicked
  577. *
  578. * @private
  579. * @function Highcharts.RangeSelector#clickButton
  580. * @param {number} i
  581. * The index of the button
  582. * @param {boolean} [redraw]
  583. * @return {void}
  584. */
  585. RangeSelector.prototype.clickButton = function (i, redraw) {
  586. var rangeSelector = this, chart = rangeSelector.chart, rangeOptions = rangeSelector.buttonOptions[i], baseAxis = chart.xAxis[0], unionExtremes = (chart.scroller && chart.scroller.getUnionExtremes()) || baseAxis || {}, dataMin = unionExtremes.dataMin, dataMax = unionExtremes.dataMax, newMin, newMax = baseAxis && Math.round(Math.min(baseAxis.max, pick(dataMax, baseAxis.max))), // #1568
  587. type = rangeOptions.type, baseXAxisOptions, range = rangeOptions._range, rangeMin, minSetting, rangeSetting, ctx, ytdExtremes, dataGrouping = rangeOptions.dataGrouping;
  588. // chart has no data, base series is removed
  589. if (dataMin === null || dataMax === null) {
  590. return;
  591. }
  592. // Set the fixed range before range is altered
  593. chart.fixedRange = range;
  594. rangeSelector.setSelected(i);
  595. // Apply dataGrouping associated to button
  596. if (dataGrouping) {
  597. this.forcedDataGrouping = true;
  598. Axis.prototype.setDataGrouping.call(baseAxis || { chart: this.chart }, dataGrouping, false);
  599. this.frozenStates = rangeOptions.preserveDataGrouping;
  600. }
  601. // Apply range
  602. if (type === 'month' || type === 'year') {
  603. if (!baseAxis) {
  604. // This is set to the user options and picked up later when the
  605. // axis is instantiated so that we know the min and max.
  606. range = rangeOptions;
  607. }
  608. else {
  609. ctx = {
  610. range: rangeOptions,
  611. max: newMax,
  612. chart: chart,
  613. dataMin: dataMin,
  614. dataMax: dataMax
  615. };
  616. newMin = baseAxis.minFromRange.call(ctx);
  617. if (isNumber(ctx.newMax)) {
  618. newMax = ctx.newMax;
  619. }
  620. }
  621. // Fixed times like minutes, hours, days
  622. }
  623. else if (range) {
  624. newMin = Math.max(newMax - range, dataMin);
  625. newMax = Math.min(newMin + range, dataMax);
  626. }
  627. else if (type === 'ytd') {
  628. // On user clicks on the buttons, or a delayed action running from
  629. // the beforeRender event (below), the baseAxis is defined.
  630. if (baseAxis) {
  631. // When "ytd" is the pre-selected button for the initial view,
  632. // its calculation is delayed and rerun in the beforeRender
  633. // event (below). When the series are initialized, but before
  634. // the chart is rendered, we have access to the xData array
  635. // (#942).
  636. if (typeof dataMax === 'undefined') {
  637. dataMin = Number.MAX_VALUE;
  638. dataMax = Number.MIN_VALUE;
  639. chart.series.forEach(function (series) {
  640. // reassign it to the last item
  641. var xData = series.xData;
  642. dataMin = Math.min(xData[0], dataMin);
  643. dataMax = Math.max(xData[xData.length - 1], dataMax);
  644. });
  645. redraw = false;
  646. }
  647. ytdExtremes = rangeSelector.getYTDExtremes(dataMax, dataMin, chart.time.useUTC);
  648. newMin = rangeMin = ytdExtremes.min;
  649. newMax = ytdExtremes.max;
  650. // "ytd" is pre-selected. We don't yet have access to processed
  651. // point and extremes data (things like pointStart and pointInterval
  652. // are missing), so we delay the process (#942)
  653. }
  654. else {
  655. rangeSelector.deferredYTDClick = i;
  656. return;
  657. }
  658. }
  659. else if (type === 'all' && baseAxis) {
  660. newMin = dataMin;
  661. newMax = dataMax;
  662. }
  663. if (defined(newMin)) {
  664. newMin += rangeOptions._offsetMin;
  665. }
  666. if (defined(newMax)) {
  667. newMax += rangeOptions._offsetMax;
  668. }
  669. if (this.dropdown) {
  670. this.dropdown.selectedIndex = i + 1;
  671. }
  672. // Update the chart
  673. if (!baseAxis) {
  674. // Axis not yet instanciated. Temporarily set min and range
  675. // options and remove them on chart load (#4317).
  676. baseXAxisOptions = splat(chart.options.xAxis)[0];
  677. rangeSetting = baseXAxisOptions.range;
  678. baseXAxisOptions.range = range;
  679. minSetting = baseXAxisOptions.min;
  680. baseXAxisOptions.min = rangeMin;
  681. addEvent(chart, 'load', function resetMinAndRange() {
  682. baseXAxisOptions.range = rangeSetting;
  683. baseXAxisOptions.min = minSetting;
  684. });
  685. }
  686. else {
  687. // Existing axis object. Set extremes after render time.
  688. baseAxis.setExtremes(newMin, newMax, pick(redraw, true), void 0, // auto animation
  689. {
  690. trigger: 'rangeSelectorButton',
  691. rangeSelectorButton: rangeOptions
  692. });
  693. }
  694. fireEvent(this, 'afterBtnClick');
  695. };
  696. /**
  697. * Set the selected option. This method only sets the internal flag, it
  698. * doesn't update the buttons or the actual zoomed range.
  699. *
  700. * @private
  701. * @function Highcharts.RangeSelector#setSelected
  702. * @param {number} [selected]
  703. * @return {void}
  704. */
  705. RangeSelector.prototype.setSelected = function (selected) {
  706. this.selected = this.options.selected = selected;
  707. };
  708. /**
  709. * Initialize the range selector
  710. *
  711. * @private
  712. * @function Highcharts.RangeSelector#init
  713. * @param {Highcharts.Chart} chart
  714. * @return {void}
  715. */
  716. RangeSelector.prototype.init = function (chart) {
  717. var rangeSelector = this, options = chart.options.rangeSelector, buttonOptions = options.buttons || rangeSelector.defaultButtons.slice(), selectedOption = options.selected, blurInputs = function () {
  718. var minInput = rangeSelector.minInput, maxInput = rangeSelector.maxInput;
  719. // #3274 in some case blur is not defined
  720. if (minInput && minInput.blur) {
  721. fireEvent(minInput, 'blur');
  722. }
  723. if (maxInput && maxInput.blur) {
  724. fireEvent(maxInput, 'blur');
  725. }
  726. };
  727. rangeSelector.chart = chart;
  728. rangeSelector.options = options;
  729. rangeSelector.buttons = [];
  730. rangeSelector.buttonOptions = buttonOptions;
  731. this.eventsToUnbind = [];
  732. this.eventsToUnbind.push(addEvent(chart.container, 'mousedown', blurInputs));
  733. this.eventsToUnbind.push(addEvent(chart, 'resize', blurInputs));
  734. // Extend the buttonOptions with actual range
  735. buttonOptions.forEach(rangeSelector.computeButtonRange);
  736. // zoomed range based on a pre-selected button index
  737. if (typeof selectedOption !== 'undefined' &&
  738. buttonOptions[selectedOption]) {
  739. this.clickButton(selectedOption, false);
  740. }
  741. this.eventsToUnbind.push(addEvent(chart, 'load', function () {
  742. // If a data grouping is applied to the current button, release it
  743. // when extremes change
  744. if (chart.xAxis && chart.xAxis[0]) {
  745. addEvent(chart.xAxis[0], 'setExtremes', function (e) {
  746. if (this.max - this.min !==
  747. chart.fixedRange &&
  748. e.trigger !== 'rangeSelectorButton' &&
  749. e.trigger !== 'updatedData' &&
  750. rangeSelector.forcedDataGrouping &&
  751. !rangeSelector.frozenStates) {
  752. this.setDataGrouping(false, false);
  753. }
  754. });
  755. }
  756. }));
  757. };
  758. /**
  759. * Dynamically update the range selector buttons after a new range has been
  760. * set
  761. *
  762. * @private
  763. * @function Highcharts.RangeSelector#updateButtonStates
  764. * @return {void}
  765. */
  766. RangeSelector.prototype.updateButtonStates = function () {
  767. var rangeSelector = this, chart = this.chart, dropdown = this.dropdown, baseAxis = chart.xAxis[0], actualRange = Math.round(baseAxis.max - baseAxis.min), hasNoData = !baseAxis.hasVisibleSeries, day = 24 * 36e5, // A single day in milliseconds
  768. unionExtremes = (chart.scroller &&
  769. chart.scroller.getUnionExtremes()) || baseAxis, dataMin = unionExtremes.dataMin, dataMax = unionExtremes.dataMax, ytdExtremes = rangeSelector.getYTDExtremes(dataMax, dataMin, chart.time.useUTC), ytdMin = ytdExtremes.min, ytdMax = ytdExtremes.max, selected = rangeSelector.selected, selectedExists = isNumber(selected), allButtonsEnabled = rangeSelector.options.allButtonsEnabled, buttons = rangeSelector.buttons;
  770. rangeSelector.buttonOptions.forEach(function (rangeOptions, i) {
  771. var range = rangeOptions._range, type = rangeOptions.type, count = rangeOptions.count || 1, button = buttons[i], state = 0, disable, select, offsetRange = rangeOptions._offsetMax -
  772. rangeOptions._offsetMin, isSelected = i === selected,
  773. // Disable buttons where the range exceeds what is allowed in
  774. // the current view
  775. isTooGreatRange = range >
  776. dataMax - dataMin,
  777. // Disable buttons where the range is smaller than the minimum
  778. // range
  779. isTooSmallRange = range < baseAxis.minRange,
  780. // Do not select the YTD button if not explicitly told so
  781. isYTDButNotSelected = false,
  782. // Disable the All button if we're already showing all
  783. isAllButAlreadyShowingAll = false, isSameRange = range === actualRange;
  784. // Months and years have a variable range so we check the extremes
  785. if ((type === 'month' || type === 'year') &&
  786. (actualRange + 36e5 >=
  787. { month: 28, year: 365 }[type] * day * count - offsetRange) &&
  788. (actualRange - 36e5 <=
  789. { month: 31, year: 366 }[type] * day * count + offsetRange)) {
  790. isSameRange = true;
  791. }
  792. else if (type === 'ytd') {
  793. isSameRange = (ytdMax - ytdMin + offsetRange) === actualRange;
  794. isYTDButNotSelected = !isSelected;
  795. }
  796. else if (type === 'all') {
  797. isSameRange = (baseAxis.max - baseAxis.min >=
  798. dataMax - dataMin);
  799. isAllButAlreadyShowingAll = (!isSelected &&
  800. selectedExists &&
  801. isSameRange);
  802. }
  803. // The new zoom area happens to match the range for a button - mark
  804. // it selected. This happens when scrolling across an ordinal gap.
  805. // It can be seen in the intraday demos when selecting 1h and scroll
  806. // across the night gap.
  807. disable = (!allButtonsEnabled &&
  808. (isTooGreatRange ||
  809. isTooSmallRange ||
  810. isAllButAlreadyShowingAll ||
  811. hasNoData));
  812. select = ((isSelected && isSameRange) ||
  813. (isSameRange && !selectedExists && !isYTDButNotSelected) ||
  814. (isSelected && rangeSelector.frozenStates));
  815. if (disable) {
  816. state = 3;
  817. }
  818. else if (select) {
  819. selectedExists = true; // Only one button can be selected
  820. state = 2;
  821. }
  822. // If state has changed, update the button
  823. if (button.state !== state) {
  824. button.setState(state);
  825. if (dropdown) {
  826. dropdown.options[i + 1].disabled = disable;
  827. if (state === 2) {
  828. dropdown.selectedIndex = i + 1;
  829. }
  830. }
  831. // Reset (#9209)
  832. if (state === 0 && selected === i) {
  833. rangeSelector.setSelected();
  834. }
  835. }
  836. });
  837. };
  838. /**
  839. * Compute and cache the range for an individual button
  840. *
  841. * @private
  842. * @function Highcharts.RangeSelector#computeButtonRange
  843. * @param {Highcharts.RangeSelectorButtonsOptions} rangeOptions
  844. * @return {void}
  845. */
  846. RangeSelector.prototype.computeButtonRange = function (rangeOptions) {
  847. var type = rangeOptions.type, count = rangeOptions.count || 1,
  848. // these time intervals have a fixed number of milliseconds, as
  849. // opposed to month, ytd and year
  850. fixedTimes = {
  851. millisecond: 1,
  852. second: 1000,
  853. minute: 60 * 1000,
  854. hour: 3600 * 1000,
  855. day: 24 * 3600 * 1000,
  856. week: 7 * 24 * 3600 * 1000
  857. };
  858. // Store the range on the button object
  859. if (fixedTimes[type]) {
  860. rangeOptions._range = fixedTimes[type] * count;
  861. }
  862. else if (type === 'month' || type === 'year') {
  863. rangeOptions._range = {
  864. month: 30,
  865. year: 365
  866. }[type] * 24 * 36e5 * count;
  867. }
  868. rangeOptions._offsetMin = pick(rangeOptions.offsetMin, 0);
  869. rangeOptions._offsetMax = pick(rangeOptions.offsetMax, 0);
  870. rangeOptions._range +=
  871. rangeOptions._offsetMax - rangeOptions._offsetMin;
  872. };
  873. /**
  874. * Get the unix timestamp of a HTML input for the dates
  875. *
  876. * @private
  877. * @function Highcharts.RangeSelector#getInputValue
  878. * @param {string} name
  879. * @return {number}
  880. */
  881. RangeSelector.prototype.getInputValue = function (name) {
  882. var input = name === 'min' ? this.minInput : this.maxInput;
  883. var options = this.chart.options.rangeSelector;
  884. var time = this.chart.time;
  885. if (input) {
  886. return ((input.type === 'text' && options.inputDateParser) ||
  887. this.defaultInputDateParser)(input.value, time.useUTC, time);
  888. }
  889. return 0;
  890. };
  891. /**
  892. * Set the internal and displayed value of a HTML input for the dates
  893. *
  894. * @private
  895. * @function Highcharts.RangeSelector#setInputValue
  896. * @param {string} name
  897. * @param {number} [inputTime]
  898. * @return {void}
  899. */
  900. RangeSelector.prototype.setInputValue = function (name, inputTime) {
  901. var options = this.options, time = this.chart.time, input = name === 'min' ? this.minInput : this.maxInput, dateBox = name === 'min' ? this.minDateBox : this.maxDateBox;
  902. if (input) {
  903. var hcTimeAttr = input.getAttribute('data-hc-time');
  904. var updatedTime = defined(hcTimeAttr) ? Number(hcTimeAttr) : void 0;
  905. if (defined(inputTime)) {
  906. var previousTime = updatedTime;
  907. if (defined(previousTime)) {
  908. input.setAttribute('data-hc-time-previous', previousTime);
  909. }
  910. input.setAttribute('data-hc-time', inputTime);
  911. updatedTime = inputTime;
  912. }
  913. input.value = time.dateFormat(this.inputTypeFormats[input.type] || options.inputEditDateFormat, updatedTime);
  914. if (dateBox) {
  915. dateBox.attr({
  916. text: time.dateFormat(options.inputDateFormat, updatedTime)
  917. });
  918. }
  919. }
  920. };
  921. /**
  922. * Set the min and max value of a HTML input for the dates
  923. *
  924. * @private
  925. * @function Highcharts.RangeSelector#setInputExtremes
  926. * @param {string} name
  927. * @param {number} min
  928. * @param {number} max
  929. * @return {void}
  930. */
  931. RangeSelector.prototype.setInputExtremes = function (name, min, max) {
  932. var input = name === 'min' ? this.minInput : this.maxInput;
  933. if (input) {
  934. var format = this.inputTypeFormats[input.type];
  935. var time = this.chart.time;
  936. if (format) {
  937. var newMin = time.dateFormat(format, min);
  938. if (input.min !== newMin) {
  939. input.min = newMin;
  940. }
  941. var newMax = time.dateFormat(format, max);
  942. if (input.max !== newMax) {
  943. input.max = newMax;
  944. }
  945. }
  946. }
  947. };
  948. /**
  949. * @private
  950. * @function Highcharts.RangeSelector#showInput
  951. * @param {string} name
  952. * @return {void}
  953. */
  954. RangeSelector.prototype.showInput = function (name) {
  955. var dateBox = name === 'min' ? this.minDateBox : this.maxDateBox;
  956. var input = name === 'min' ? this.minInput : this.maxInput;
  957. if (input && dateBox && this.inputGroup) {
  958. var isTextInput = input.type === 'text';
  959. var _a = this.inputGroup, translateX = _a.translateX, translateY = _a.translateY;
  960. var inputBoxWidth = this.options.inputBoxWidth;
  961. css(input, {
  962. width: isTextInput ? ((dateBox.width + (inputBoxWidth ? -2 : 20)) + 'px') : 'auto',
  963. height: isTextInput ? ((dateBox.height - 2) + 'px') : 'auto',
  964. border: '2px solid silver'
  965. });
  966. if (isTextInput && inputBoxWidth) {
  967. css(input, {
  968. left: (translateX + dateBox.x) + 'px',
  969. top: translateY + 'px'
  970. });
  971. // Inputs of types date, time or datetime-local should be centered
  972. // on top of the dateBox
  973. }
  974. else {
  975. css(input, {
  976. left: Math.min(Math.round(dateBox.x +
  977. translateX -
  978. (input.offsetWidth - dateBox.width) / 2), this.chart.chartWidth - input.offsetWidth) + 'px',
  979. top: (translateY - 1 - (input.offsetHeight - dateBox.height) / 2) + 'px'
  980. });
  981. }
  982. }
  983. };
  984. /**
  985. * @private
  986. * @function Highcharts.RangeSelector#hideInput
  987. * @param {string} name
  988. * @return {void}
  989. */
  990. RangeSelector.prototype.hideInput = function (name) {
  991. var input = name === 'min' ? this.minInput : this.maxInput;
  992. if (input) {
  993. css(input, {
  994. top: '-9999em',
  995. border: 0,
  996. width: '1px',
  997. height: '1px'
  998. });
  999. }
  1000. };
  1001. /**
  1002. * @private
  1003. * @function Highcharts.RangeSelector#defaultInputDateParser
  1004. */
  1005. RangeSelector.prototype.defaultInputDateParser = function (inputDate, useUTC, time) {
  1006. var hasTimezone = function (str) {
  1007. return str.length > 6 &&
  1008. (str.lastIndexOf('-') === str.length - 6 ||
  1009. str.lastIndexOf('+') === str.length - 6);
  1010. };
  1011. var input = inputDate.split('/').join('-').split(' ').join('T');
  1012. if (input.indexOf('T') === -1) {
  1013. input += 'T00:00';
  1014. }
  1015. if (useUTC) {
  1016. input += 'Z';
  1017. }
  1018. else if (H.isSafari && !hasTimezone(input)) {
  1019. var offset = new Date(input).getTimezoneOffset() / 60;
  1020. input += offset <= 0 ? "+" + pad(-offset) + ":00" : "-" + pad(offset) + ":00";
  1021. }
  1022. var date = Date.parse(input);
  1023. // If the value isn't parsed directly to a value by the
  1024. // browser's Date.parse method, like YYYY-MM-DD in IE8, try
  1025. // parsing it a different way
  1026. if (!isNumber(date)) {
  1027. var parts = inputDate.split('-');
  1028. date = Date.UTC(pInt(parts[0]), pInt(parts[1]) - 1, pInt(parts[2]));
  1029. }
  1030. if (time && useUTC && isNumber(date)) {
  1031. date += time.getTimezoneOffset(date);
  1032. }
  1033. return date;
  1034. };
  1035. /**
  1036. * Draw either the 'from' or the 'to' HTML input box of the range selector
  1037. *
  1038. * @private
  1039. * @function Highcharts.RangeSelector#drawInput
  1040. * @param {string} name
  1041. * @return {RangeSelectorInputElements}
  1042. */
  1043. RangeSelector.prototype.drawInput = function (name) {
  1044. var _a = this, chart = _a.chart, div = _a.div, inputGroup = _a.inputGroup;
  1045. var rangeSelector = this, chartStyle = chart.renderer.style || {}, renderer = chart.renderer, options = chart.options.rangeSelector, lang = defaultOptions.lang, isMin = name === 'min';
  1046. /**
  1047. * @private
  1048. */
  1049. function updateExtremes() {
  1050. var value = rangeSelector.getInputValue(name), chartAxis = chart.xAxis[0], dataAxis = chart.scroller && chart.scroller.xAxis ?
  1051. chart.scroller.xAxis :
  1052. chartAxis, dataMin = dataAxis.dataMin, dataMax = dataAxis.dataMax;
  1053. var maxInput = rangeSelector.maxInput, minInput = rangeSelector.minInput;
  1054. if (value !== Number(input.getAttribute('data-hc-time-previous')) &&
  1055. isNumber(value)) {
  1056. input.setAttribute('data-hc-time-previous', value);
  1057. // Validate the extremes. If it goes beyound the data min or
  1058. // max, use the actual data extreme (#2438).
  1059. if (isMin && maxInput && isNumber(dataMin)) {
  1060. if (value > Number(maxInput.getAttribute('data-hc-time'))) {
  1061. value = void 0;
  1062. }
  1063. else if (value < dataMin) {
  1064. value = dataMin;
  1065. }
  1066. }
  1067. else if (minInput && isNumber(dataMax)) {
  1068. if (value < Number(minInput.getAttribute('data-hc-time'))) {
  1069. value = void 0;
  1070. }
  1071. else if (value > dataMax) {
  1072. value = dataMax;
  1073. }
  1074. }
  1075. // Set the extremes
  1076. if (typeof value !== 'undefined') { // @todo typof undefined
  1077. chartAxis.setExtremes(isMin ? value : chartAxis.min, isMin ? chartAxis.max : value, void 0, void 0, { trigger: 'rangeSelectorInput' });
  1078. }
  1079. }
  1080. }
  1081. // Create the text label
  1082. var text = lang[isMin ? 'rangeSelectorFrom' : 'rangeSelectorTo'];
  1083. var label = renderer
  1084. .label(text, 0)
  1085. .addClass('highcharts-range-label')
  1086. .attr({
  1087. padding: text ? 2 : 0
  1088. })
  1089. .add(inputGroup);
  1090. // Create an SVG label that shows updated date ranges and and records
  1091. // click events that bring in the HTML input.
  1092. var dateBox = renderer
  1093. .label('', 0)
  1094. .addClass('highcharts-range-input')
  1095. .attr({
  1096. padding: 2,
  1097. width: options.inputBoxWidth,
  1098. height: options.inputBoxHeight,
  1099. 'text-align': 'center'
  1100. })
  1101. .on('click', function () {
  1102. // If it is already focused, the onfocus event doesn't fire
  1103. // (#3713)
  1104. rangeSelector.showInput(name);
  1105. rangeSelector[name + 'Input'].focus();
  1106. });
  1107. if (!chart.styledMode) {
  1108. dateBox.attr({
  1109. stroke: options.inputBoxBorderColor,
  1110. 'stroke-width': 1
  1111. });
  1112. }
  1113. dateBox.add(inputGroup);
  1114. // Create the HTML input element. This is rendered as 1x1 pixel then set
  1115. // to the right size when focused.
  1116. var input = createElement('input', {
  1117. name: name,
  1118. className: 'highcharts-range-selector'
  1119. }, void 0, div);
  1120. // #14788: Setting input.type to an unsupported type throws in IE, so
  1121. // we need to use setAttribute instead
  1122. input.setAttribute('type', preferredInputType(options.inputDateFormat || '%b %e, %Y'));
  1123. if (!chart.styledMode) {
  1124. // Styles
  1125. label.css(merge(chartStyle, options.labelStyle));
  1126. dateBox.css(merge({
  1127. color: palette.neutralColor80
  1128. }, chartStyle, options.inputStyle));
  1129. css(input, extend({
  1130. position: 'absolute',
  1131. border: 0,
  1132. boxShadow: '0 0 15px rgba(0,0,0,0.3)',
  1133. width: '1px',
  1134. height: '1px',
  1135. padding: 0,
  1136. textAlign: 'center',
  1137. fontSize: chartStyle.fontSize,
  1138. fontFamily: chartStyle.fontFamily,
  1139. top: '-9999em' // #4798
  1140. }, options.inputStyle));
  1141. }
  1142. // Blow up the input box
  1143. input.onfocus = function () {
  1144. rangeSelector.showInput(name);
  1145. };
  1146. // Hide away the input box
  1147. input.onblur = function () {
  1148. // update extermes only when inputs are active
  1149. if (input === H.doc.activeElement) { // Only when focused
  1150. // Update also when no `change` event is triggered, like when
  1151. // clicking inside the SVG (#4710)
  1152. updateExtremes();
  1153. }
  1154. // #10404 - move hide and blur outside focus
  1155. rangeSelector.hideInput(name);
  1156. rangeSelector.setInputValue(name);
  1157. input.blur(); // #4606
  1158. };
  1159. var keyDown = false;
  1160. // handle changes in the input boxes
  1161. input.onchange = function () {
  1162. // Update extremes and blur input when clicking date input calendar
  1163. if (!keyDown) {
  1164. updateExtremes();
  1165. rangeSelector.hideInput(name);
  1166. input.blur();
  1167. }
  1168. };
  1169. input.onkeypress = function (event) {
  1170. // IE does not fire onchange on enter
  1171. if (event.keyCode === 13) {
  1172. updateExtremes();
  1173. }
  1174. };
  1175. input.onkeydown = function (event) {
  1176. keyDown = true;
  1177. // Arrow keys
  1178. if (event.keyCode === 38 || event.keyCode === 40) {
  1179. updateExtremes();
  1180. }
  1181. };
  1182. input.onkeyup = function () {
  1183. keyDown = false;
  1184. };
  1185. return { dateBox: dateBox, input: input, label: label };
  1186. };
  1187. /**
  1188. * Get the position of the range selector buttons and inputs. This can be
  1189. * overridden from outside for custom positioning.
  1190. *
  1191. * @private
  1192. * @function Highcharts.RangeSelector#getPosition
  1193. *
  1194. * @return {Highcharts.Dictionary<number>}
  1195. */
  1196. RangeSelector.prototype.getPosition = function () {
  1197. var chart = this.chart, options = chart.options.rangeSelector, top = options.verticalAlign === 'top' ?
  1198. chart.plotTop - chart.axisOffset[0] :
  1199. 0; // set offset only for varticalAlign top
  1200. return {
  1201. buttonTop: top + options.buttonPosition.y,
  1202. inputTop: top + options.inputPosition.y - 10
  1203. };
  1204. };
  1205. /**
  1206. * Get the extremes of YTD. Will choose dataMax if its value is lower than
  1207. * the current timestamp. Will choose dataMin if its value is higher than
  1208. * the timestamp for the start of current year.
  1209. *
  1210. * @private
  1211. * @function Highcharts.RangeSelector#getYTDExtremes
  1212. *
  1213. * @param {number} dataMax
  1214. *
  1215. * @param {number} dataMin
  1216. *
  1217. * @return {*}
  1218. * Returns min and max for the YTD
  1219. */
  1220. RangeSelector.prototype.getYTDExtremes = function (dataMax, dataMin, useUTC) {
  1221. var time = this.chart.time, min, now = new time.Date(dataMax), year = time.get('FullYear', now), startOfYear = useUTC ?
  1222. time.Date.UTC(year, 0, 1) : // eslint-disable-line new-cap
  1223. +new time.Date(year, 0, 1);
  1224. min = Math.max(dataMin, startOfYear);
  1225. var ts = now.getTime();
  1226. return {
  1227. max: Math.min(dataMax || ts, ts),
  1228. min: min
  1229. };
  1230. };
  1231. /**
  1232. * Render the range selector including the buttons and the inputs. The first
  1233. * time render is called, the elements are created and positioned. On
  1234. * subsequent calls, they are moved and updated.
  1235. *
  1236. * @private
  1237. * @function Highcharts.RangeSelector#render
  1238. * @param {number} [min]
  1239. * X axis minimum
  1240. * @param {number} [max]
  1241. * X axis maximum
  1242. * @return {void}
  1243. */
  1244. RangeSelector.prototype.render = function (min, max) {
  1245. var chart = this.chart, renderer = chart.renderer, container = chart.container, chartOptions = chart.options, options = chartOptions.rangeSelector,
  1246. // Place inputs above the container
  1247. inputsZIndex = pick(chartOptions.chart.style &&
  1248. chartOptions.chart.style.zIndex, 0) + 1, inputEnabled = options.inputEnabled, rendered = this.rendered;
  1249. if (options.enabled === false) {
  1250. return;
  1251. }
  1252. // create the elements
  1253. if (!rendered) {
  1254. this.group = renderer.g('range-selector-group')
  1255. .attr({
  1256. zIndex: 7
  1257. })
  1258. .add();
  1259. this.div = createElement('div', void 0, {
  1260. position: 'relative',
  1261. height: 0,
  1262. zIndex: inputsZIndex
  1263. });
  1264. if (this.buttonOptions.length) {
  1265. this.renderButtons();
  1266. }
  1267. // First create a wrapper outside the container in order to make
  1268. // the inputs work and make export correct
  1269. if (container.parentNode) {
  1270. container.parentNode.insertBefore(this.div, container);
  1271. }
  1272. if (inputEnabled) {
  1273. // Create the group to keep the inputs
  1274. this.inputGroup = renderer.g('input-group').add(this.group);
  1275. var minElems = this.drawInput('min');
  1276. this.minDateBox = minElems.dateBox;
  1277. this.minLabel = minElems.label;
  1278. this.minInput = minElems.input;
  1279. var maxElems = this.drawInput('max');
  1280. this.maxDateBox = maxElems.dateBox;
  1281. this.maxLabel = maxElems.label;
  1282. this.maxInput = maxElems.input;
  1283. }
  1284. }
  1285. if (inputEnabled) {
  1286. // Set or reset the input values
  1287. this.setInputValue('min', min);
  1288. this.setInputValue('max', max);
  1289. var unionExtremes = (chart.scroller && chart.scroller.getUnionExtremes()) || chart.xAxis[0] || {};
  1290. if (defined(unionExtremes.dataMin) && defined(unionExtremes.dataMax)) {
  1291. var minRange = chart.xAxis[0].minRange || 0;
  1292. this.setInputExtremes('min', unionExtremes.dataMin, Math.min(unionExtremes.dataMax, this.getInputValue('max')) - minRange);
  1293. this.setInputExtremes('max', Math.max(unionExtremes.dataMin, this.getInputValue('min')) + minRange, unionExtremes.dataMax);
  1294. }
  1295. // Reflow
  1296. if (this.inputGroup) {
  1297. var x_1 = 0;
  1298. [
  1299. this.minLabel,
  1300. this.minDateBox,
  1301. this.maxLabel,
  1302. this.maxDateBox
  1303. ].forEach(function (label) {
  1304. if (label) {
  1305. var width = label.getBBox().width;
  1306. if (width) {
  1307. label.attr({ x: x_1 });
  1308. x_1 += width + options.inputSpacing;
  1309. }
  1310. }
  1311. });
  1312. }
  1313. }
  1314. this.alignElements();
  1315. this.rendered = true;
  1316. };
  1317. /**
  1318. * Render the range buttons. This only runs the first time, later the
  1319. * positioning is laid out in alignElements.
  1320. *
  1321. * @private
  1322. * @function Highcharts.RangeSelector#renderButtons
  1323. * @return {void}
  1324. */
  1325. RangeSelector.prototype.renderButtons = function () {
  1326. var _this = this;
  1327. var _a = this, buttons = _a.buttons, chart = _a.chart, options = _a.options;
  1328. var lang = defaultOptions.lang;
  1329. var renderer = chart.renderer;
  1330. var buttonTheme = merge(options.buttonTheme);
  1331. var states = buttonTheme && buttonTheme.states;
  1332. // Prevent the button from resetting the width when the button state
  1333. // changes since we need more control over the width when collapsing
  1334. // the buttons
  1335. var width = buttonTheme.width || 28;
  1336. delete buttonTheme.width;
  1337. delete buttonTheme.states;
  1338. this.buttonGroup = renderer.g('range-selector-buttons').add(this.group);
  1339. var dropdown = this.dropdown = createElement('select', void 0, {
  1340. position: 'absolute',
  1341. width: '1px',
  1342. height: '1px',
  1343. padding: 0,
  1344. border: 0,
  1345. top: '-9999em',
  1346. cursor: 'pointer',
  1347. opacity: 0.0001
  1348. }, this.div);
  1349. // Prevent page zoom on iPhone
  1350. addEvent(dropdown, 'touchstart', function () {
  1351. dropdown.style.fontSize = '16px';
  1352. });
  1353. // Forward events from select to button
  1354. [
  1355. [H.isMS ? 'mouseover' : 'mouseenter'],
  1356. [H.isMS ? 'mouseout' : 'mouseleave'],
  1357. ['change', 'click']
  1358. ].forEach(function (_a) {
  1359. var from = _a[0], to = _a[1];
  1360. addEvent(dropdown, from, function () {
  1361. var button = buttons[_this.currentButtonIndex()];
  1362. if (button) {
  1363. fireEvent(button.element, to || from);
  1364. }
  1365. });
  1366. });
  1367. this.zoomText = renderer
  1368. .text(lang.rangeSelectorZoom, 0, 15)
  1369. .add(this.buttonGroup);
  1370. if (!this.chart.styledMode) {
  1371. this.zoomText.css(options.labelStyle);
  1372. buttonTheme['stroke-width'] = pick(buttonTheme['stroke-width'], 0);
  1373. }
  1374. createElement('option', {
  1375. textContent: this.zoomText.textStr,
  1376. disabled: true
  1377. }, void 0, dropdown);
  1378. this.buttonOptions.forEach(function (rangeOptions, i) {
  1379. createElement('option', {
  1380. textContent: rangeOptions.title || rangeOptions.text
  1381. }, void 0, dropdown);
  1382. buttons[i] = renderer
  1383. .button(rangeOptions.text, 0, 0, function (e) {
  1384. // extract events from button object and call
  1385. var buttonEvents = (rangeOptions.events &&
  1386. rangeOptions.events.click), callDefaultEvent;
  1387. if (buttonEvents) {
  1388. callDefaultEvent =
  1389. buttonEvents.call(rangeOptions, e);
  1390. }
  1391. if (callDefaultEvent !== false) {
  1392. _this.clickButton(i);
  1393. }
  1394. _this.isActive = true;
  1395. }, buttonTheme, states && states.hover, states && states.select, states && states.disabled)
  1396. .attr({
  1397. 'text-align': 'center',
  1398. width: width
  1399. })
  1400. .add(_this.buttonGroup);
  1401. if (rangeOptions.title) {
  1402. buttons[i].attr('title', rangeOptions.title);
  1403. }
  1404. });
  1405. };
  1406. /**
  1407. * Align the elements horizontally and vertically.
  1408. *
  1409. * @private
  1410. * @function Highcharts.RangeSelector#alignElements
  1411. * @return {void}
  1412. */
  1413. RangeSelector.prototype.alignElements = function () {
  1414. var _this = this;
  1415. var _a = this, buttonGroup = _a.buttonGroup, buttons = _a.buttons, chart = _a.chart, group = _a.group, inputGroup = _a.inputGroup, options = _a.options, zoomText = _a.zoomText;
  1416. var chartOptions = chart.options;
  1417. var navButtonOptions = (chartOptions.exporting &&
  1418. chartOptions.exporting.enabled !== false &&
  1419. chartOptions.navigation &&
  1420. chartOptions.navigation.buttonOptions);
  1421. var buttonPosition = options.buttonPosition, inputPosition = options.inputPosition, verticalAlign = options.verticalAlign;
  1422. // Get the X offset required to avoid overlapping with the exporting
  1423. // button. This is is used both by the buttonGroup and the inputGroup.
  1424. var getXOffsetForExportButton = function (group, position) {
  1425. if (navButtonOptions &&
  1426. _this.titleCollision(chart) &&
  1427. verticalAlign === 'top' &&
  1428. position.align === 'right' && ((position.y -
  1429. group.getBBox().height - 12) <
  1430. ((navButtonOptions.y || 0) +
  1431. (navButtonOptions.height || 0) +
  1432. chart.spacing[0]))) {
  1433. return -40;
  1434. }
  1435. return 0;
  1436. };
  1437. var plotLeft = chart.plotLeft;
  1438. if (group && buttonPosition && inputPosition) {
  1439. var translateX = buttonPosition.x - chart.spacing[3];
  1440. if (buttonGroup) {
  1441. this.positionButtons();
  1442. if (!this.initialButtonGroupWidth) {
  1443. var width_1 = 0;
  1444. if (zoomText) {
  1445. width_1 += zoomText.getBBox().width + 5;
  1446. }
  1447. buttons.forEach(function (button, i) {
  1448. width_1 += button.width;
  1449. if (i !== buttons.length - 1) {
  1450. width_1 += options.buttonSpacing;
  1451. }
  1452. });
  1453. this.initialButtonGroupWidth = width_1;
  1454. }
  1455. plotLeft -= chart.spacing[3];
  1456. this.updateButtonStates();
  1457. // Detect collision between button group and exporting
  1458. var xOffsetForExportButton_1 = getXOffsetForExportButton(buttonGroup, buttonPosition);
  1459. this.alignButtonGroup(xOffsetForExportButton_1);
  1460. // Skip animation
  1461. group.placed = buttonGroup.placed = chart.hasLoaded;
  1462. }
  1463. var xOffsetForExportButton = 0;
  1464. if (inputGroup) {
  1465. // Detect collision between the input group and exporting button
  1466. xOffsetForExportButton = getXOffsetForExportButton(inputGroup, inputPosition);
  1467. if (inputPosition.align === 'left') {
  1468. translateX = plotLeft;
  1469. }
  1470. else if (inputPosition.align === 'right') {
  1471. translateX = -Math.max(chart.axisOffset[1], -xOffsetForExportButton);
  1472. }
  1473. // Update the alignment to the updated spacing box
  1474. inputGroup.align({
  1475. y: inputPosition.y,
  1476. width: inputGroup.getBBox().width,
  1477. align: inputPosition.align,
  1478. // fix wrong getBBox() value on right align
  1479. x: inputPosition.x + translateX - 2
  1480. }, true, chart.spacingBox);
  1481. // Skip animation
  1482. inputGroup.placed = chart.hasLoaded;
  1483. }
  1484. this.handleCollision(xOffsetForExportButton);
  1485. // Vertical align
  1486. group.align({
  1487. verticalAlign: verticalAlign
  1488. }, true, chart.spacingBox);
  1489. var alignTranslateY = group.alignAttr.translateY;
  1490. // Set position
  1491. var groupHeight = group.getBBox().height + 20; // # 20 padding
  1492. var translateY = 0;
  1493. // Calculate bottom position
  1494. if (verticalAlign === 'bottom') {
  1495. var legendOptions = chart.legend && chart.legend.options;
  1496. var legendHeight = (legendOptions &&
  1497. legendOptions.verticalAlign === 'bottom' &&
  1498. legendOptions.enabled &&
  1499. !legendOptions.floating ?
  1500. (chart.legend.legendHeight +
  1501. pick(legendOptions.margin, 10)) :
  1502. 0);
  1503. groupHeight = groupHeight + legendHeight - 20;
  1504. translateY = (alignTranslateY -
  1505. groupHeight -
  1506. (options.floating ? 0 : options.y) -
  1507. (chart.titleOffset ? chart.titleOffset[2] : 0) -
  1508. 10 // 10 spacing
  1509. );
  1510. }
  1511. if (verticalAlign === 'top') {
  1512. if (options.floating) {
  1513. translateY = 0;
  1514. }
  1515. if (chart.titleOffset && chart.titleOffset[0]) {
  1516. translateY = chart.titleOffset[0];
  1517. }
  1518. translateY += ((chart.margin[0] - chart.spacing[0]) || 0);
  1519. }
  1520. else if (verticalAlign === 'middle') {
  1521. if (inputPosition.y === buttonPosition.y) {
  1522. translateY = alignTranslateY;
  1523. }
  1524. else if (inputPosition.y || buttonPosition.y) {
  1525. if (inputPosition.y < 0 ||
  1526. buttonPosition.y < 0) {
  1527. translateY -= Math.min(inputPosition.y, buttonPosition.y);
  1528. }
  1529. else {
  1530. translateY = alignTranslateY - groupHeight;
  1531. }
  1532. }
  1533. }
  1534. group.translate(options.x, options.y + Math.floor(translateY));
  1535. // Translate HTML inputs
  1536. var _b = this, minInput = _b.minInput, maxInput = _b.maxInput, dropdown = _b.dropdown;
  1537. if (options.inputEnabled && minInput && maxInput) {
  1538. minInput.style.marginTop = group.translateY + 'px';
  1539. maxInput.style.marginTop = group.translateY + 'px';
  1540. }
  1541. if (dropdown) {
  1542. dropdown.style.marginTop = group.translateY + 'px';
  1543. }
  1544. }
  1545. };
  1546. /**
  1547. * Align the button group horizontally and vertically.
  1548. *
  1549. * @private
  1550. * @function Highcharts.RangeSelector#alignButtonGroup
  1551. * @param {number} xOffsetForExportButton
  1552. * @param {number} [width]
  1553. * @return {void}
  1554. */
  1555. RangeSelector.prototype.alignButtonGroup = function (xOffsetForExportButton, width) {
  1556. var _a = this, chart = _a.chart, options = _a.options, buttonGroup = _a.buttonGroup, buttons = _a.buttons;
  1557. var buttonPosition = options.buttonPosition;
  1558. var plotLeft = chart.plotLeft - chart.spacing[3];
  1559. var translateX = buttonPosition.x - chart.spacing[3];
  1560. if (buttonPosition.align === 'right') {
  1561. translateX += xOffsetForExportButton - plotLeft; // #13014
  1562. }
  1563. else if (buttonPosition.align === 'center') {
  1564. translateX -= plotLeft / 2;
  1565. }
  1566. if (buttonGroup) {
  1567. // Align button group
  1568. buttonGroup.align({
  1569. y: buttonPosition.y,
  1570. width: pick(width, this.initialButtonGroupWidth),
  1571. align: buttonPosition.align,
  1572. x: translateX
  1573. }, true, chart.spacingBox);
  1574. }
  1575. };
  1576. /**
  1577. * @private
  1578. * @function Highcharts.RangeSelector#positionButtons
  1579. * @return {void}
  1580. */
  1581. RangeSelector.prototype.positionButtons = function () {
  1582. var _a = this, buttons = _a.buttons, chart = _a.chart, options = _a.options, zoomText = _a.zoomText;
  1583. var verb = chart.hasLoaded ? 'animate' : 'attr';
  1584. var buttonPosition = options.buttonPosition;
  1585. var plotLeft = chart.plotLeft;
  1586. var buttonLeft = plotLeft;
  1587. if (zoomText && zoomText.visibility !== 'hidden') {
  1588. // #8769, allow dynamically updating margins
  1589. zoomText[verb]({
  1590. x: pick(plotLeft + buttonPosition.x, plotLeft)
  1591. });
  1592. // Button start position
  1593. buttonLeft += buttonPosition.x +
  1594. zoomText.getBBox().width + 5;
  1595. }
  1596. this.buttonOptions.forEach(function (rangeOptions, i) {
  1597. if (buttons[i].visibility !== 'hidden') {
  1598. buttons[i][verb]({ x: buttonLeft });
  1599. // increase button position for the next button
  1600. buttonLeft += buttons[i].width + options.buttonSpacing;
  1601. }
  1602. else {
  1603. buttons[i][verb]({ x: plotLeft });
  1604. }
  1605. });
  1606. };
  1607. /**
  1608. * Handle collision between the button group and the input group
  1609. *
  1610. * @private
  1611. * @function Highcharts.RangeSelector#handleCollision
  1612. *
  1613. * @param {number} xOffsetForExportButton
  1614. * The X offset of the group required to make room for the
  1615. * exporting button
  1616. * @return {void}
  1617. */
  1618. RangeSelector.prototype.handleCollision = function (xOffsetForExportButton) {
  1619. var _this = this;
  1620. var _a = this, chart = _a.chart, buttonGroup = _a.buttonGroup, inputGroup = _a.inputGroup;
  1621. var _b = this.options, buttonPosition = _b.buttonPosition, dropdown = _b.dropdown, inputPosition = _b.inputPosition;
  1622. var maxButtonWidth = function () {
  1623. var buttonWidth = 0;
  1624. _this.buttons.forEach(function (button) {
  1625. var bBox = button.getBBox();
  1626. if (bBox.width > buttonWidth) {
  1627. buttonWidth = bBox.width;
  1628. }
  1629. });
  1630. return buttonWidth;
  1631. };
  1632. var groupsOverlap = function (buttonGroupWidth) {
  1633. if (inputGroup && buttonGroup) {
  1634. var inputGroupX = (inputGroup.alignAttr.translateX +
  1635. inputGroup.alignOptions.x -
  1636. xOffsetForExportButton +
  1637. // getBBox for detecing left margin
  1638. inputGroup.getBBox().x +
  1639. // 2px padding to not overlap input and label
  1640. 2);
  1641. var inputGroupWidth = inputGroup.alignOptions.width;
  1642. var buttonGroupX = buttonGroup.alignAttr.translateX +
  1643. buttonGroup.getBBox().x;
  1644. return (buttonGroupX + buttonGroupWidth > inputGroupX) &&
  1645. (inputGroupX + inputGroupWidth > buttonGroupX) &&
  1646. (buttonPosition.y <
  1647. (inputPosition.y +
  1648. inputGroup.getBBox().height));
  1649. }
  1650. return false;
  1651. };
  1652. var moveInputsDown = function () {
  1653. if (inputGroup && buttonGroup) {
  1654. inputGroup.attr({
  1655. translateX: inputGroup.alignAttr.translateX + (chart.axisOffset[1] >= -xOffsetForExportButton ?
  1656. 0 :
  1657. -xOffsetForExportButton),
  1658. translateY: inputGroup.alignAttr.translateY +
  1659. buttonGroup.getBBox().height + 10
  1660. });
  1661. }
  1662. };
  1663. if (buttonGroup) {
  1664. if (dropdown === 'always') {
  1665. this.collapseButtons(xOffsetForExportButton);
  1666. if (groupsOverlap(maxButtonWidth())) {
  1667. // Move the inputs down if there is still a collision
  1668. // after collapsing the buttons
  1669. moveInputsDown();
  1670. }
  1671. return;
  1672. }
  1673. if (dropdown === 'never') {
  1674. this.expandButtons();
  1675. }
  1676. }
  1677. // Detect collision
  1678. if (inputGroup && buttonGroup) {
  1679. if ((inputPosition.align === buttonPosition.align) ||
  1680. // 20 is minimal spacing between elements
  1681. groupsOverlap(this.initialButtonGroupWidth + 20)) {
  1682. if (dropdown === 'responsive') {
  1683. this.collapseButtons(xOffsetForExportButton);
  1684. if (groupsOverlap(maxButtonWidth())) {
  1685. moveInputsDown();
  1686. }
  1687. }
  1688. else {
  1689. moveInputsDown();
  1690. }
  1691. }
  1692. else if (dropdown === 'responsive') {
  1693. this.expandButtons();
  1694. }
  1695. }
  1696. else if (buttonGroup && dropdown === 'responsive') {
  1697. if (this.initialButtonGroupWidth > chart.plotWidth) {
  1698. this.collapseButtons(xOffsetForExportButton);
  1699. }
  1700. else {
  1701. this.expandButtons();
  1702. }
  1703. }
  1704. };
  1705. /**
  1706. * Collapse the buttons and put the select element on top.
  1707. *
  1708. * @private
  1709. * @function Highcharts.RangeSelector#collapseButtons
  1710. * @param {number} xOffsetForExportButton
  1711. * @return {void}
  1712. */
  1713. RangeSelector.prototype.collapseButtons = function (xOffsetForExportButton) {
  1714. var _a = this, buttons = _a.buttons, buttonOptions = _a.buttonOptions, dropdown = _a.dropdown, options = _a.options, zoomText = _a.zoomText;
  1715. var getAttribs = function (text) { return ({
  1716. text: text ? text + " \u25BE" : '▾',
  1717. width: 'auto',
  1718. paddingLeft: 8,
  1719. paddingRight: 8
  1720. }); };
  1721. if (zoomText) {
  1722. zoomText.hide();
  1723. }
  1724. var hasActiveButton = false;
  1725. buttonOptions.forEach(function (rangeOptions, i) {
  1726. var button = buttons[i];
  1727. if (button.state !== 2) {
  1728. button.hide();
  1729. }
  1730. else {
  1731. button.show();
  1732. button.attr(getAttribs(rangeOptions.text));
  1733. hasActiveButton = true;
  1734. }
  1735. });
  1736. if (!hasActiveButton) {
  1737. if (dropdown) {
  1738. dropdown.selectedIndex = 0;
  1739. }
  1740. buttons[0].show();
  1741. buttons[0].attr(getAttribs(this.zoomText && this.zoomText.textStr));
  1742. }
  1743. var align = options.buttonPosition.align;
  1744. this.positionButtons();
  1745. if (align === 'right' || align === 'center') {
  1746. this.alignButtonGroup(xOffsetForExportButton, buttons[this.currentButtonIndex()].getBBox().width);
  1747. }
  1748. this.showDropdown();
  1749. };
  1750. /**
  1751. * Show all the buttons and hide the select element.
  1752. *
  1753. * @private
  1754. * @function Highcharts.RangeSelector#expandButtons
  1755. * @return {void}
  1756. */
  1757. RangeSelector.prototype.expandButtons = function () {
  1758. var _a = this, buttons = _a.buttons, buttonOptions = _a.buttonOptions, options = _a.options, zoomText = _a.zoomText;
  1759. this.hideDropdown();
  1760. if (zoomText) {
  1761. zoomText.show();
  1762. }
  1763. buttonOptions.forEach(function (rangeOptions, i) {
  1764. var button = buttons[i];
  1765. button.show();
  1766. button.attr({
  1767. text: rangeOptions.text,
  1768. width: options.buttonTheme.width || 28,
  1769. paddingLeft: 'unset',
  1770. paddingRight: 'unset'
  1771. });
  1772. if (button.state < 2) {
  1773. button.setState(0);
  1774. }
  1775. });
  1776. this.positionButtons();
  1777. };
  1778. /**
  1779. * Get the index of the visible button when the buttons are collapsed.
  1780. *
  1781. * @private
  1782. * @function Highcharts.RangeSelector#currentButtonIndex
  1783. * @return {number}
  1784. */
  1785. RangeSelector.prototype.currentButtonIndex = function () {
  1786. var dropdown = this.dropdown;
  1787. if (dropdown && dropdown.selectedIndex > 0) {
  1788. return dropdown.selectedIndex - 1;
  1789. }
  1790. return 0;
  1791. };
  1792. /**
  1793. * Position the select element on top of the button.
  1794. *
  1795. * @private
  1796. * @function Highcharts.RangeSelector#showDropdown
  1797. * @return {void}
  1798. */
  1799. RangeSelector.prototype.showDropdown = function () {
  1800. var _a = this, buttonGroup = _a.buttonGroup, buttons = _a.buttons, chart = _a.chart, dropdown = _a.dropdown;
  1801. if (buttonGroup && dropdown) {
  1802. var translateX = buttonGroup.translateX, translateY = buttonGroup.translateY;
  1803. var bBox = buttons[this.currentButtonIndex()].getBBox();
  1804. css(dropdown, {
  1805. left: (chart.plotLeft + translateX) + 'px',
  1806. top: (translateY + 0.5) + 'px',
  1807. width: bBox.width + 'px',
  1808. height: bBox.height + 'px'
  1809. });
  1810. this.hasVisibleDropdown = true;
  1811. }
  1812. };
  1813. /**
  1814. * @private
  1815. * @function Highcharts.RangeSelector#hideDropdown
  1816. * @return {void}
  1817. */
  1818. RangeSelector.prototype.hideDropdown = function () {
  1819. var dropdown = this.dropdown;
  1820. if (dropdown) {
  1821. css(dropdown, {
  1822. top: '-9999em',
  1823. width: '1px',
  1824. height: '1px'
  1825. });
  1826. this.hasVisibleDropdown = false;
  1827. }
  1828. };
  1829. /**
  1830. * Extracts height of range selector
  1831. *
  1832. * @private
  1833. * @function Highcharts.RangeSelector#getHeight
  1834. * @return {number}
  1835. * Returns rangeSelector height
  1836. */
  1837. RangeSelector.prototype.getHeight = function () {
  1838. var rangeSelector = this, options = rangeSelector.options, rangeSelectorGroup = rangeSelector.group, inputPosition = options.inputPosition, buttonPosition = options.buttonPosition, yPosition = options.y, buttonPositionY = buttonPosition.y, inputPositionY = inputPosition.y, rangeSelectorHeight = 0, minPosition;
  1839. if (options.height) {
  1840. return options.height;
  1841. }
  1842. // Align the elements before we read the height in case we're switching
  1843. // between wrapped and non-wrapped layout
  1844. this.alignElements();
  1845. rangeSelectorHeight = rangeSelectorGroup ?
  1846. // 13px to keep back compatibility
  1847. (rangeSelectorGroup.getBBox(true).height) + 13 +
  1848. yPosition :
  1849. 0;
  1850. minPosition = Math.min(inputPositionY, buttonPositionY);
  1851. if ((inputPositionY < 0 && buttonPositionY < 0) ||
  1852. (inputPositionY > 0 && buttonPositionY > 0)) {
  1853. rangeSelectorHeight += Math.abs(minPosition);
  1854. }
  1855. return rangeSelectorHeight;
  1856. };
  1857. /**
  1858. * Detect collision with title or subtitle
  1859. *
  1860. * @private
  1861. * @function Highcharts.RangeSelector#titleCollision
  1862. *
  1863. * @param {Highcharts.Chart} chart
  1864. *
  1865. * @return {boolean}
  1866. * Returns collision status
  1867. */
  1868. RangeSelector.prototype.titleCollision = function (chart) {
  1869. return !(chart.options.title.text ||
  1870. chart.options.subtitle.text);
  1871. };
  1872. /**
  1873. * Update the range selector with new options
  1874. *
  1875. * @private
  1876. * @function Highcharts.RangeSelector#update
  1877. * @param {Highcharts.RangeSelectorOptions} options
  1878. * @return {void}
  1879. */
  1880. RangeSelector.prototype.update = function (options) {
  1881. var chart = this.chart;
  1882. merge(true, chart.options.rangeSelector, options);
  1883. this.destroy();
  1884. this.init(chart);
  1885. this.render();
  1886. };
  1887. /**
  1888. * Destroys allocated elements.
  1889. *
  1890. * @private
  1891. * @function Highcharts.RangeSelector#destroy
  1892. */
  1893. RangeSelector.prototype.destroy = function () {
  1894. var rSelector = this, minInput = rSelector.minInput, maxInput = rSelector.maxInput;
  1895. if (rSelector.eventsToUnbind) {
  1896. rSelector.eventsToUnbind.forEach(function (unbind) { return unbind(); });
  1897. rSelector.eventsToUnbind = void 0;
  1898. }
  1899. // Destroy elements in collections
  1900. destroyObjectProperties(rSelector.buttons);
  1901. // Clear input element events
  1902. if (minInput) {
  1903. minInput.onfocus = minInput.onblur = minInput.onchange = null;
  1904. }
  1905. if (maxInput) {
  1906. maxInput.onfocus = maxInput.onblur = maxInput.onchange = null;
  1907. }
  1908. // Destroy HTML and SVG elements
  1909. objectEach(rSelector, function (val, key) {
  1910. if (val && key !== 'chart') {
  1911. if (val instanceof SVGElement) {
  1912. // SVGElement
  1913. val.destroy();
  1914. }
  1915. else if (val instanceof window.HTMLElement) {
  1916. // HTML element
  1917. discardElement(val);
  1918. }
  1919. }
  1920. if (val !== RangeSelector.prototype[key]) {
  1921. rSelector[key] = null;
  1922. }
  1923. }, this);
  1924. };
  1925. return RangeSelector;
  1926. }());
  1927. /**
  1928. * The default buttons for pre-selecting time frames
  1929. */
  1930. RangeSelector.prototype.defaultButtons = [{
  1931. type: 'month',
  1932. count: 1,
  1933. text: '1m',
  1934. title: 'View 1 month'
  1935. }, {
  1936. type: 'month',
  1937. count: 3,
  1938. text: '3m',
  1939. title: 'View 3 months'
  1940. }, {
  1941. type: 'month',
  1942. count: 6,
  1943. text: '6m',
  1944. title: 'View 6 months'
  1945. }, {
  1946. type: 'ytd',
  1947. text: 'YTD',
  1948. title: 'View year to date'
  1949. }, {
  1950. type: 'year',
  1951. count: 1,
  1952. text: '1y',
  1953. title: 'View 1 year'
  1954. }, {
  1955. type: 'all',
  1956. text: 'All',
  1957. title: 'View all'
  1958. }];
  1959. /**
  1960. * The date formats to use when setting min, max and value on date inputs
  1961. */
  1962. RangeSelector.prototype.inputTypeFormats = {
  1963. 'datetime-local': '%Y-%m-%dT%H:%M:%S',
  1964. 'date': '%Y-%m-%d',
  1965. 'time': '%H:%M:%S'
  1966. };
  1967. /**
  1968. * Get the preferred input type based on a date format string.
  1969. *
  1970. * @private
  1971. * @function preferredInputType
  1972. * @param {string} format
  1973. * @return {string}
  1974. */
  1975. function preferredInputType(format) {
  1976. var ms = format.indexOf('%L') !== -1;
  1977. if (ms) {
  1978. return 'text';
  1979. }
  1980. var date = ['a', 'A', 'd', 'e', 'w', 'b', 'B', 'm', 'o', 'y', 'Y'].some(function (char) {
  1981. return format.indexOf('%' + char) !== -1;
  1982. });
  1983. var time = ['H', 'k', 'I', 'l', 'M', 'S'].some(function (char) {
  1984. return format.indexOf('%' + char) !== -1;
  1985. });
  1986. if (date && time) {
  1987. return 'datetime-local';
  1988. }
  1989. if (date) {
  1990. return 'date';
  1991. }
  1992. if (time) {
  1993. return 'time';
  1994. }
  1995. return 'text';
  1996. }
  1997. /**
  1998. * Get the axis min value based on the range option and the current max. For
  1999. * stock charts this is extended via the {@link RangeSelector} so that if the
  2000. * selected range is a multiple of months or years, it is compensated for
  2001. * various month lengths.
  2002. *
  2003. * @private
  2004. * @function Highcharts.Axis#minFromRange
  2005. * @return {number|undefined}
  2006. * The new minimum value.
  2007. */
  2008. Axis.prototype.minFromRange = function () {
  2009. var rangeOptions = this.range, type = rangeOptions.type, min, max = this.max, dataMin, range, time = this.chart.time,
  2010. // Get the true range from a start date
  2011. getTrueRange = function (base, count) {
  2012. var timeName = type === 'year' ? 'FullYear' : 'Month';
  2013. var date = new time.Date(base);
  2014. var basePeriod = time.get(timeName, date);
  2015. time.set(timeName, date, basePeriod + count);
  2016. if (basePeriod === time.get(timeName, date)) {
  2017. time.set('Date', date, 0); // #6537
  2018. }
  2019. return date.getTime() - base;
  2020. };
  2021. if (isNumber(rangeOptions)) {
  2022. min = max - rangeOptions;
  2023. range = rangeOptions;
  2024. }
  2025. else {
  2026. min = max + getTrueRange(max, -rangeOptions.count);
  2027. // Let the fixedRange reflect initial settings (#5930)
  2028. if (this.chart) {
  2029. this.chart.fixedRange = max - min;
  2030. }
  2031. }
  2032. dataMin = pick(this.dataMin, Number.MIN_VALUE);
  2033. if (!isNumber(min)) {
  2034. min = dataMin;
  2035. }
  2036. if (min <= dataMin) {
  2037. min = dataMin;
  2038. if (typeof range === 'undefined') { // #4501
  2039. range = getTrueRange(min, rangeOptions.count);
  2040. }
  2041. this.newMax = Math.min(min + range, this.dataMax);
  2042. }
  2043. if (!isNumber(max)) {
  2044. min = void 0;
  2045. }
  2046. return min;
  2047. };
  2048. if (!H.RangeSelector) {
  2049. var chartDestroyEvents_1 = [];
  2050. var initRangeSelector_1 = function (chart) {
  2051. var extremes, rangeSelector = chart.rangeSelector, legend, alignTo, verticalAlign;
  2052. /**
  2053. * @private
  2054. */
  2055. function render() {
  2056. if (rangeSelector) {
  2057. extremes = chart.xAxis[0].getExtremes();
  2058. legend = chart.legend;
  2059. verticalAlign = (rangeSelector &&
  2060. rangeSelector.options.verticalAlign);
  2061. if (isNumber(extremes.min)) {
  2062. rangeSelector.render(extremes.min, extremes.max);
  2063. }
  2064. // Re-align the legend so that it's below the rangeselector
  2065. if (legend.display &&
  2066. verticalAlign === 'top' &&
  2067. verticalAlign === legend.options.verticalAlign) {
  2068. // Create a new alignment box for the legend.
  2069. alignTo = merge(chart.spacingBox);
  2070. if (legend.options.layout === 'vertical') {
  2071. alignTo.y = chart.plotTop;
  2072. }
  2073. else {
  2074. alignTo.y += rangeSelector.getHeight();
  2075. }
  2076. legend.group.placed = false; // Don't animate the alignment.
  2077. legend.align(alignTo);
  2078. }
  2079. }
  2080. }
  2081. if (rangeSelector) {
  2082. var events = find(chartDestroyEvents_1, function (e) { return e[0] === chart; });
  2083. if (!events) {
  2084. chartDestroyEvents_1.push([chart, [
  2085. // redraw the scroller on setExtremes
  2086. addEvent(chart.xAxis[0], 'afterSetExtremes', function (e) {
  2087. if (rangeSelector) {
  2088. rangeSelector.render(e.min, e.max);
  2089. }
  2090. }),
  2091. // redraw the scroller chart resize
  2092. addEvent(chart, 'redraw', render)
  2093. ]]);
  2094. }
  2095. // do it now
  2096. render();
  2097. }
  2098. };
  2099. // Initialize rangeselector for stock charts
  2100. addEvent(Chart, 'afterGetContainer', function () {
  2101. if (this.options.rangeSelector &&
  2102. this.options.rangeSelector.enabled) {
  2103. this.rangeSelector = new RangeSelector(this);
  2104. }
  2105. });
  2106. addEvent(Chart, 'beforeRender', function () {
  2107. var chart = this, axes = chart.axes, rangeSelector = chart.rangeSelector, verticalAlign;
  2108. if (rangeSelector) {
  2109. if (isNumber(rangeSelector.deferredYTDClick)) {
  2110. rangeSelector.clickButton(rangeSelector.deferredYTDClick);
  2111. delete rangeSelector.deferredYTDClick;
  2112. }
  2113. axes.forEach(function (axis) {
  2114. axis.updateNames();
  2115. axis.setScale();
  2116. });
  2117. chart.getAxisMargins();
  2118. rangeSelector.render();
  2119. verticalAlign = rangeSelector.options.verticalAlign;
  2120. if (!rangeSelector.options.floating) {
  2121. if (verticalAlign === 'bottom') {
  2122. this.extraBottomMargin = true;
  2123. }
  2124. else if (verticalAlign !== 'middle') {
  2125. this.extraTopMargin = true;
  2126. }
  2127. }
  2128. }
  2129. });
  2130. addEvent(Chart, 'update', function (e) {
  2131. var chart = this, options = e.options, optionsRangeSelector = options.rangeSelector, rangeSelector = chart.rangeSelector, verticalAlign, extraBottomMarginWas = this.extraBottomMargin, extraTopMarginWas = this.extraTopMargin;
  2132. if (optionsRangeSelector &&
  2133. optionsRangeSelector.enabled &&
  2134. !defined(rangeSelector) &&
  2135. this.options.rangeSelector) {
  2136. this.options.rangeSelector.enabled = true;
  2137. this.rangeSelector = rangeSelector = new RangeSelector(this);
  2138. }
  2139. this.extraBottomMargin = false;
  2140. this.extraTopMargin = false;
  2141. if (rangeSelector) {
  2142. initRangeSelector_1(this);
  2143. verticalAlign = (optionsRangeSelector &&
  2144. optionsRangeSelector.verticalAlign) || (rangeSelector.options && rangeSelector.options.verticalAlign);
  2145. if (!rangeSelector.options.floating) {
  2146. if (verticalAlign === 'bottom') {
  2147. this.extraBottomMargin = true;
  2148. }
  2149. else if (verticalAlign !== 'middle') {
  2150. this.extraTopMargin = true;
  2151. }
  2152. }
  2153. if (this.extraBottomMargin !== extraBottomMarginWas ||
  2154. this.extraTopMargin !== extraTopMarginWas) {
  2155. this.isDirtyBox = true;
  2156. }
  2157. }
  2158. });
  2159. addEvent(Chart, 'render', function () {
  2160. var chart = this, rangeSelector = chart.rangeSelector, verticalAlign;
  2161. if (rangeSelector && !rangeSelector.options.floating) {
  2162. rangeSelector.render();
  2163. verticalAlign = rangeSelector.options.verticalAlign;
  2164. if (verticalAlign === 'bottom') {
  2165. this.extraBottomMargin = true;
  2166. }
  2167. else if (verticalAlign !== 'middle') {
  2168. this.extraTopMargin = true;
  2169. }
  2170. }
  2171. });
  2172. addEvent(Chart, 'getMargins', function () {
  2173. var rangeSelector = this.rangeSelector, rangeSelectorHeight;
  2174. if (rangeSelector) {
  2175. rangeSelectorHeight = rangeSelector.getHeight();
  2176. if (this.extraTopMargin) {
  2177. this.plotTop += rangeSelectorHeight;
  2178. }
  2179. if (this.extraBottomMargin) {
  2180. this.marginBottom += rangeSelectorHeight;
  2181. }
  2182. }
  2183. });
  2184. Chart.prototype.callbacks.push(initRangeSelector_1);
  2185. // Remove resize/afterSetExtremes at chart destroy
  2186. addEvent(Chart, 'destroy', function destroyEvents() {
  2187. for (var i = 0; i < chartDestroyEvents_1.length; i++) {
  2188. var events = chartDestroyEvents_1[i];
  2189. if (events[0] === this) {
  2190. events[1].forEach(function (unbind) { return unbind(); });
  2191. chartDestroyEvents_1.splice(i, 1);
  2192. return;
  2193. }
  2194. }
  2195. });
  2196. H.RangeSelector = RangeSelector;
  2197. }
  2198. export default H.RangeSelector;