HighContrastMode.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /* *
  2. *
  3. * (c) 2009-2021 Øystein Moseng
  4. *
  5. * Handling for Windows High Contrast Mode.
  6. *
  7. * License: www.highcharts.com/license
  8. *
  9. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  10. *
  11. * */
  12. 'use strict';
  13. import H from '../Core/Globals.js';
  14. var doc = H.doc, isMS = H.isMS, win = H.win;
  15. var whcm = {
  16. /**
  17. * Detect WHCM in the browser.
  18. *
  19. * @function Highcharts#isHighContrastModeActive
  20. * @private
  21. * @return {boolean} Returns true if the browser is in High Contrast mode.
  22. */
  23. isHighContrastModeActive: function () {
  24. // Use media query on Edge, but not on IE
  25. var isEdge = /(Edg)/.test(win.navigator.userAgent);
  26. if (win.matchMedia && isEdge) {
  27. return win.matchMedia('(-ms-high-contrast: active)').matches;
  28. }
  29. // Test BG image for IE
  30. if (isMS && win.getComputedStyle) {
  31. var testDiv = doc.createElement('div');
  32. var imageSrc = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==';
  33. testDiv.style.backgroundImage = "url(" + imageSrc + ")"; // #13071
  34. doc.body.appendChild(testDiv);
  35. var bi = (testDiv.currentStyle ||
  36. win.getComputedStyle(testDiv)).backgroundImage;
  37. doc.body.removeChild(testDiv);
  38. return bi === 'none';
  39. }
  40. // Not used for other browsers
  41. return false;
  42. },
  43. /**
  44. * Force high contrast theme for the chart. The default theme is defined in
  45. * a separate file.
  46. *
  47. * @function Highcharts#setHighContrastTheme
  48. * @private
  49. * @param {Highcharts.AccessibilityChart} chart The chart to set the theme of.
  50. * @return {void}
  51. */
  52. setHighContrastTheme: function (chart) {
  53. // We might want to add additional functionality here in the future for
  54. // storing the old state so that we can reset the theme if HC mode is
  55. // disabled. For now, the user will have to reload the page.
  56. chart.highContrastModeActive = true;
  57. // Apply theme to chart
  58. var theme = (chart.options.accessibility.highContrastTheme);
  59. chart.update(theme, false);
  60. // Force series colors (plotOptions is not enough)
  61. chart.series.forEach(function (s) {
  62. var plotOpts = theme.plotOptions[s.type] || {};
  63. s.update({
  64. color: plotOpts.color || 'windowText',
  65. colors: [plotOpts.color || 'windowText'],
  66. borderColor: plotOpts.borderColor || 'window'
  67. });
  68. // Force point colors if existing
  69. s.points.forEach(function (p) {
  70. if (p.options && p.options.color) {
  71. p.update({
  72. color: plotOpts.color || 'windowText',
  73. borderColor: plotOpts.borderColor || 'window'
  74. }, false);
  75. }
  76. });
  77. });
  78. // The redraw for each series and after is required for 3D pie
  79. // (workaround)
  80. chart.redraw();
  81. }
  82. };
  83. export default whcm;