UNPKG

8.1 kBJavaScriptView Raw
1import { createVNode as _createVNode } from "vue";
2
3function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
4
5function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
6
7function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
8
9function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
10
11var __rest = this && this.__rest || function (s, e) {
12 var t = {};
13
14 for (var p in s) {
15 if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
16 }
17
18 if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
19 if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
20 }
21 return t;
22};
23
24import PropTypes, { withUndefined } from '../../_util/vue-types';
25import { initDefaultProps } from '../../_util/props-util';
26import enhancer from './enhancer';
27import { propTypes, defaultProps } from './types';
28import { defineComponent } from 'vue';
29
30var circlePropTypes = _extends(_extends({}, propTypes), {
31 gapPosition: PropTypes.oneOf(['top', 'bottom', 'left', 'right']),
32 gapDegree: withUndefined(PropTypes.oneOfType([PropTypes.number, PropTypes.string, PropTypes.looseBool]))
33});
34
35var circleDefaultProps = _extends(_extends({}, defaultProps), {
36 gapPosition: 'top'
37});
38
39var gradientSeed = 0;
40
41function stripPercentToNumber(percent) {
42 return +percent.replace('%', '');
43}
44
45function toArray(symArray) {
46 return Array.isArray(symArray) ? symArray : [symArray];
47}
48
49function getPathStyles(offset, percent, strokeColor, strokeWidth) {
50 var gapDegree = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 0;
51 var gapPosition = arguments.length > 5 ? arguments[5] : undefined;
52 var radius = 50 - strokeWidth / 2;
53 var beginPositionX = 0;
54 var beginPositionY = -radius;
55 var endPositionX = 0;
56 var endPositionY = -2 * radius;
57
58 switch (gapPosition) {
59 case 'left':
60 beginPositionX = -radius;
61 beginPositionY = 0;
62 endPositionX = 2 * radius;
63 endPositionY = 0;
64 break;
65
66 case 'right':
67 beginPositionX = radius;
68 beginPositionY = 0;
69 endPositionX = -2 * radius;
70 endPositionY = 0;
71 break;
72
73 case 'bottom':
74 beginPositionY = radius;
75 endPositionY = 2 * radius;
76 break;
77
78 default:
79 }
80
81 var pathString = "M 50,50 m ".concat(beginPositionX, ",").concat(beginPositionY, "\n a ").concat(radius, ",").concat(radius, " 0 1 1 ").concat(endPositionX, ",").concat(-endPositionY, "\n a ").concat(radius, ",").concat(radius, " 0 1 1 ").concat(-endPositionX, ",").concat(endPositionY);
82 var len = Math.PI * 2 * radius;
83 var pathStyle = {
84 stroke: strokeColor,
85 strokeDasharray: "".concat(percent / 100 * (len - gapDegree), "px ").concat(len, "px"),
86 strokeDashoffset: "-".concat(gapDegree / 2 + offset / 100 * (len - gapDegree), "px"),
87 transition: 'stroke-dashoffset .3s ease 0s, stroke-dasharray .3s ease 0s, stroke .3s, stroke-width .06s ease .3s, opacity .3s ease 0s'
88 };
89 return {
90 pathString: pathString,
91 pathStyle: pathStyle
92 };
93}
94
95var Circle = defineComponent({
96 name: 'Circle',
97 props: initDefaultProps(circlePropTypes, circleDefaultProps),
98 created: function created() {
99 this.paths = {};
100 this.gradientId = gradientSeed;
101 gradientSeed += 1;
102 },
103 methods: {
104 getStokeList: function getStokeList() {
105 var _this = this;
106
107 var _this$$props = this.$props,
108 prefixCls = _this$$props.prefixCls,
109 percent = _this$$props.percent,
110 strokeColor = _this$$props.strokeColor,
111 strokeWidth = _this$$props.strokeWidth,
112 strokeLinecap = _this$$props.strokeLinecap,
113 gapDegree = _this$$props.gapDegree,
114 gapPosition = _this$$props.gapPosition;
115 var percentList = toArray(percent);
116 var strokeColorList = toArray(strokeColor);
117 var stackPtg = 0;
118 return percentList.map(function (ptg, index) {
119 var color = strokeColorList[index] || strokeColorList[strokeColorList.length - 1];
120 var stroke = Object.prototype.toString.call(color) === '[object Object]' ? "url(#".concat(prefixCls, "-gradient-").concat(_this.gradientId, ")") : '';
121
122 var _getPathStyles = getPathStyles(stackPtg, ptg, color, strokeWidth, gapDegree, gapPosition),
123 pathString = _getPathStyles.pathString,
124 pathStyle = _getPathStyles.pathStyle;
125
126 stackPtg += ptg;
127 var pathProps = {
128 key: index,
129 d: pathString,
130 stroke: stroke,
131 'stroke-linecap': strokeLinecap,
132 'stroke-width': strokeWidth,
133 opacity: ptg === 0 ? 0 : 1,
134 'fill-opacity': '0',
135 class: "".concat(prefixCls, "-circle-path"),
136 style: pathStyle
137 };
138 return _createVNode("path", _objectSpread({
139 "ref": function ref(c) {
140 return _this.paths[index] = c;
141 }
142 }, pathProps), null);
143 });
144 }
145 },
146 render: function render() {
147 var _a = this.$props,
148 prefixCls = _a.prefixCls,
149 strokeWidth = _a.strokeWidth,
150 trailWidth = _a.trailWidth,
151 gapDegree = _a.gapDegree,
152 gapPosition = _a.gapPosition,
153 trailColor = _a.trailColor,
154 strokeLinecap = _a.strokeLinecap,
155 strokeColor = _a.strokeColor,
156 restProps = __rest(_a, ["prefixCls", "strokeWidth", "trailWidth", "gapDegree", "gapPosition", "trailColor", "strokeLinecap", "strokeColor"]);
157
158 var _getPathStyles2 = getPathStyles(0, 100, trailColor, strokeWidth, gapDegree, gapPosition),
159 pathString = _getPathStyles2.pathString,
160 pathStyle = _getPathStyles2.pathStyle;
161
162 delete restProps.percent;
163 var strokeColorList = toArray(strokeColor);
164 var gradient = strokeColorList.find(function (color) {
165 return Object.prototype.toString.call(color) === '[object Object]';
166 });
167 var pathFirst = {
168 d: pathString,
169 stroke: trailColor,
170 'stroke-linecap': strokeLinecap,
171 'stroke-width': trailWidth || strokeWidth,
172 'fill-opacity': '0',
173 class: "".concat(prefixCls, "-circle-trail"),
174 style: pathStyle
175 };
176 return _createVNode("svg", _objectSpread({
177 "class": "".concat(prefixCls, "-circle"),
178 "viewBox": "0 0 100 100"
179 }, restProps), [gradient && _createVNode("defs", null, [_createVNode("linearGradient", {
180 "id": "".concat(prefixCls, "-gradient-").concat(this.gradientId),
181 "x1": "100%",
182 "y1": "0%",
183 "x2": "0%",
184 "y2": "0%"
185 }, [Object.keys(gradient).sort(function (a, b) {
186 return stripPercentToNumber(a) - stripPercentToNumber(b);
187 }).map(function (key, index) {
188 return _createVNode("stop", {
189 "key": index,
190 "offset": key,
191 "stop-color": gradient[key]
192 }, null);
193 })])]), _createVNode("path", pathFirst, null), this.getStokeList().reverse()]);
194 }
195});
196export default enhancer(Circle);
\No newline at end of file