cssEmphasis.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import Displayable from '../graphic/Displayable';
  2. import { liftColor } from '../tool/color';
  3. import { BrushScope, SVGVNodeAttrs } from './core';
  4. import { getClassId } from './cssClassId';
  5. export function createCSSEmphasis(
  6. el: Displayable,
  7. attrs: SVGVNodeAttrs,
  8. scope: BrushScope
  9. ) {
  10. if (!el.ignore) {
  11. if (el.isSilent()) {
  12. // If el is silent, it can not be hovered nor selected.
  13. // So set pointer-events to pass through.
  14. const style = {
  15. 'pointer-events': 'none'
  16. };
  17. setClassAttribute(style, attrs, scope, true);
  18. }
  19. else {
  20. const emphasisStyle = el.states.emphasis && el.states.emphasis.style
  21. ? el.states.emphasis.style
  22. : {};
  23. let fill = emphasisStyle.fill;
  24. if (!fill) {
  25. // No empahsis fill, lift color
  26. const normalFill = el.style && el.style.fill;
  27. const selectFill = el.states.select
  28. && el.states.select.style
  29. && el.states.select.style.fill;
  30. const fromFill = el.currentStates.indexOf('select') >= 0
  31. ? (selectFill || normalFill)
  32. : normalFill;
  33. if (fromFill) {
  34. fill = liftColor(fromFill);
  35. }
  36. }
  37. let lineWidth = emphasisStyle.lineWidth;
  38. if (lineWidth) {
  39. // Symbols use transform to set size, so lineWidth
  40. // should be divided by scaleX
  41. const scaleX = (!emphasisStyle.strokeNoScale && el.transform)
  42. ? el.transform[0]
  43. : 1;
  44. lineWidth = lineWidth / scaleX;
  45. }
  46. const style = {
  47. cursor: 'pointer', // TODO: Should this be customized?
  48. } as any;
  49. if (fill) {
  50. style.fill = fill;
  51. }
  52. if (emphasisStyle.stroke) {
  53. style.stroke = emphasisStyle.stroke;
  54. }
  55. if (lineWidth) {
  56. style['stroke-width'] = lineWidth;
  57. }
  58. setClassAttribute(style, attrs, scope, true);
  59. }
  60. }
  61. }
  62. function setClassAttribute(style: object, attrs: SVGVNodeAttrs, scope: BrushScope, withHover: boolean) {
  63. const styleKey = JSON.stringify(style);
  64. let className = scope.cssStyleCache[styleKey];
  65. if (!className) {
  66. className = scope.zrId + '-cls-' + getClassId();
  67. scope.cssStyleCache[styleKey] = className;
  68. scope.cssNodes['.' + className + (withHover ? ':hover' : '')] = style as any;
  69. }
  70. attrs.class = attrs.class ? (attrs.class + ' ' + className) : className;
  71. }