index.js 6.6 KB

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