index.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { __assign, __extends } from "tslib";
  2. import { get, isEqual } from '@antv/util';
  3. import { GUI } from '../../core';
  4. import { select } from '../../util';
  5. import { Tag } from '../tag';
  6. import { SIZE_STYLE } from './constant';
  7. // 开启颜色 默认
  8. var OPTION_COLOR = '#1890FF';
  9. // 关闭颜色 默认
  10. var CLOSE_COLOR = '#00000040';
  11. function getHandleShapeStyle(shape, spacing, checked) {
  12. if (spacing === void 0) { spacing = 0; }
  13. if (checked === void 0) { checked = true; }
  14. var size = Number(shape.style.height) - spacing * 2;
  15. return {
  16. x: checked ? Number(shape.style.width) + Number(shape.style.x) - spacing - size : Number(shape.style.x) + spacing,
  17. y: Number(shape.style.y) + spacing,
  18. width: size,
  19. height: size,
  20. radius: size / 2,
  21. };
  22. }
  23. function getTagShapeStyle(backgroundStyle, _a, spacing, checked) {
  24. var width = _a.width, height = _a.height;
  25. if (spacing === void 0) { spacing = 0; }
  26. if (checked === void 0) { checked = true; }
  27. return {
  28. x: checked
  29. ? Number(backgroundStyle.x) + spacing
  30. : Number(backgroundStyle.width) + Number(backgroundStyle.x) - width,
  31. y: Number(backgroundStyle.y) + (Number(backgroundStyle.height) - height) / 2,
  32. };
  33. }
  34. var Switch = /** @class */ (function (_super) {
  35. __extends(Switch, _super);
  36. function Switch(options) {
  37. return _super.call(this, options, {
  38. x: 0,
  39. y: 0,
  40. size: 'default',
  41. spacing: 2,
  42. checked: true,
  43. disabled: false,
  44. }) || this;
  45. }
  46. Switch.prototype.render = function (attributes, container) {
  47. var _this = this;
  48. var size = attributes.size, spacing = attributes.spacing, disabled = attributes.disabled, checked = attributes.checked, unCheckedChildren = attributes.unCheckedChildren, checkedChildren = attributes.checkedChildren;
  49. this.attributes;
  50. var group = select(container).maybeAppendByClassName('switch-content', 'g').node();
  51. var bounds = group.getLocalBounds();
  52. var _a = get(SIZE_STYLE, size, SIZE_STYLE.default), sizeStyle = _a.sizeStyle, tagStyle = _a.tagStyle;
  53. // 其他统一属性
  54. var cursor = disabled ? 'no-drop' : 'pointer';
  55. var color = checked ? OPTION_COLOR : CLOSE_COLOR;
  56. // 背景位置大小配置
  57. var backgroundStyle = sizeStyle;
  58. // Tag 配置, 创建
  59. var tagCfg = checked ? checkedChildren : unCheckedChildren;
  60. if (checkedChildren || unCheckedChildren) {
  61. select(group)
  62. .maybeAppendByClassName('switch-tag', function () { return new Tag({}); })
  63. .call(function (selection) {
  64. var tagShape = selection.node();
  65. tagShape.update(__assign(__assign({ cursor: cursor, backgroundStyle: null, text: false, marker: false }, tagStyle), tagCfg));
  66. // 增加 整体宽度 需要对 tag 提前渲染获得 width 然后通过 width 计算 x 的位置
  67. var _a = tagShape === null || tagShape === void 0 ? void 0 : tagShape.getLocalBounds(), max = _a.max, min = _a.min;
  68. var width = max[0] - min[0] + sizeStyle.radius;
  69. var height = max[1] - min[1];
  70. // 计算获得背景宽度
  71. var backgroundWidth = Math.max(width + sizeStyle.height + 2, sizeStyle.width);
  72. backgroundStyle = __assign(__assign({}, sizeStyle), { width: backgroundWidth });
  73. tagShape.update(getTagShapeStyle({
  74. x: bounds.min[0],
  75. y: bounds.min[1],
  76. width: backgroundWidth,
  77. height: backgroundStyle.height,
  78. }, { width: width, height: height }, backgroundStyle.radius, checked));
  79. });
  80. }
  81. // 背景 组件
  82. var backgroundShape = select(group)
  83. .maybeAppendByClassName('switch-background', 'rect')
  84. .styles(__assign({ zIndex: (group.style.zIndex || 0) - 1, x: bounds.min[0], y: bounds.min[1], fill: color, cursor: cursor, fillOpacity: disabled ? 0.4 : 1 }, backgroundStyle))
  85. .node();
  86. // 背景阴影动效 组件
  87. var backgroundStrokeShape = select(group)
  88. .maybeAppendByClassName('switch-background-stroke', 'rect')
  89. .styles(__assign({ zIndex: (group.style.zIndex || 0) - 2, x: bounds.min[0], y: bounds.min[1], stroke: color, lineWidth: 0 }, backgroundStyle))
  90. .node();
  91. // 控件 组件
  92. select(group)
  93. .maybeAppendByClassName('switch-handle', 'rect')
  94. .styles({
  95. fill: '#fff',
  96. cursor: cursor,
  97. })
  98. .call(function (selection) {
  99. var _a, _b;
  100. var handleShape = selection.node();
  101. // 动画添加 通过获取 开启 和 关闭的 x 来实现动画效果
  102. var newHandleShapeStyle = getHandleShapeStyle(backgroundShape, spacing, checked);
  103. var oldHandleShapeStyle = getHandleShapeStyle(backgroundShape, spacing, !checked);
  104. if (handleShape.attr('x') && !isEqual(newHandleShapeStyle, oldHandleShapeStyle) && _this.checked !== checked) {
  105. // 调整控件 和 tag 位置
  106. handleShape.attr(oldHandleShapeStyle);
  107. // 清理之前的动画
  108. (_a = handleShape.getAnimations()[0]) === null || _a === void 0 ? void 0 : _a.cancel();
  109. (_b = backgroundStrokeShape.getAnimations()[0]) === null || _b === void 0 ? void 0 : _b.cancel();
  110. // 控件组建变化添加动画 动画为 原 x -> 新 x
  111. handleShape.animate([{ x: oldHandleShapeStyle.x }, { x: newHandleShapeStyle.x }], {
  112. duration: 120,
  113. fill: 'both',
  114. });
  115. // 动效组件变化添加动画 动画为 由内向外的 渐淡扩散
  116. backgroundStrokeShape.animate([
  117. { lineWidth: 0, strokeOpacity: 0.5 },
  118. { lineWidth: 14, strokeOpacity: 0 },
  119. ], {
  120. duration: 400,
  121. easing: 'ease-on',
  122. });
  123. }
  124. else {
  125. handleShape.attr(newHandleShapeStyle);
  126. }
  127. });
  128. this.checked = !!checked;
  129. };
  130. /**
  131. * 组件 switch
  132. */
  133. Switch.tag = 'switch';
  134. return Switch;
  135. }(GUI));
  136. export { Switch };
  137. //# sourceMappingURL=index.js.map