utils.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. "use strict";
  2. var __rest = (this && this.__rest) || function (s, e) {
  3. var t = {};
  4. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
  5. t[p] = s[p];
  6. if (s != null && typeof Object.getOwnPropertySymbols === "function")
  7. for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
  8. if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
  9. t[p[i]] = s[p[i]];
  10. }
  11. return t;
  12. };
  13. Object.defineProperty(exports, "__esModule", { value: true });
  14. exports.adaptor = exports.domainOf = exports.inferComponentShape = exports.isVertical = exports.isHorizontal = exports.scaleOf = exports.LegendCategoryLayout = exports.G2Layout = exports.inferComponentLayout = exports.titleContent = exports.maybeAppend = exports.createComponent = void 0;
  15. const g_1 = require("@antv/g");
  16. const gui_1 = require("@antv/gui");
  17. const util_1 = require("@antv/util");
  18. const selection_1 = require("../utils/selection");
  19. function createComponent(descriptor) {
  20. return class extends g_1.CustomElement {
  21. constructor(config) {
  22. super(config);
  23. this.descriptor = descriptor;
  24. }
  25. connectedCallback() {
  26. var _a, _b;
  27. (_b = (_a = this.descriptor).render) === null || _b === void 0 ? void 0 : _b.call(_a, this.attributes, this);
  28. }
  29. update(cfg = {}) {
  30. var _a, _b;
  31. this.attr((0, util_1.deepMix)({}, this.attributes, cfg));
  32. (_b = (_a = this.descriptor).render) === null || _b === void 0 ? void 0 : _b.call(_a, this.attributes, this);
  33. }
  34. };
  35. }
  36. exports.createComponent = createComponent;
  37. function maybeAppend(parent, selector, node) {
  38. if (!parent.querySelector(selector)) {
  39. return (0, selection_1.select)(parent).append(node);
  40. }
  41. return (0, selection_1.select)(parent).select(selector);
  42. }
  43. exports.maybeAppend = maybeAppend;
  44. function titleContent(field) {
  45. return Array.isArray(field) ? field.join(', ') : `${field || ''}`;
  46. }
  47. exports.titleContent = titleContent;
  48. function inferComponentLayout(position, userDefinitions) {
  49. const preset = {
  50. display: 'flex',
  51. flexDirection: 'row',
  52. justifyContent: 'flex-start',
  53. alignItems: 'center',
  54. };
  55. if (userDefinitions) {
  56. return Object.assign(Object.assign({}, preset), userDefinitions);
  57. }
  58. let { flexDirection, justifyContent, alignItems } = preset;
  59. const layout = {
  60. top: ['row', 'flex-start', 'center'],
  61. bottom: ['row', 'flex-start', 'center'],
  62. left: ['colunm', 'center', 'center'],
  63. right: ['colunm', 'center', 'center'],
  64. center: ['column', 'center', 'center'],
  65. };
  66. if (position in layout) {
  67. [flexDirection, justifyContent, alignItems] = layout[position];
  68. }
  69. return { display: 'flex', flexDirection, justifyContent, alignItems };
  70. }
  71. exports.inferComponentLayout = inferComponentLayout;
  72. class G2Layout extends gui_1.Layout {
  73. get child() {
  74. var _a;
  75. return (_a = this.children) === null || _a === void 0 ? void 0 : _a[0];
  76. }
  77. update(options) {
  78. var _a;
  79. this.attr(options);
  80. const { subOptions } = options;
  81. (_a = this.child) === null || _a === void 0 ? void 0 : _a.update(subOptions);
  82. }
  83. }
  84. exports.G2Layout = G2Layout;
  85. class LegendCategoryLayout extends G2Layout {
  86. update(options) {
  87. var _a;
  88. const { subOptions } = options;
  89. (_a = this.child) === null || _a === void 0 ? void 0 : _a.update(subOptions);
  90. }
  91. }
  92. exports.LegendCategoryLayout = LegendCategoryLayout;
  93. function scaleOf(scales, type) {
  94. var _a;
  95. return (_a = scales.filter((s) => s.getOptions().name === type)) === null || _a === void 0 ? void 0 : _a[0];
  96. }
  97. exports.scaleOf = scaleOf;
  98. function isHorizontal(orientation) {
  99. return orientation === 'horizontal' || orientation === 0;
  100. }
  101. exports.isHorizontal = isHorizontal;
  102. function isVertical(orientation) {
  103. return orientation === 'vertical' || orientation === -Math.PI / 2;
  104. }
  105. exports.isVertical = isVertical;
  106. function inferComponentShape(value, options, component) {
  107. const { bbox } = value;
  108. const { position = 'top', size: userDefinedSize, length: userDefinedLength, } = options;
  109. const isHorizontal = ['top', 'bottom', 'center'].includes(position);
  110. const [bboxSize, bboxLength] = isHorizontal
  111. ? [bbox.height, bbox.width]
  112. : [bbox.width, bbox.height];
  113. const { defaultSize, defaultLength } = component.props;
  114. const size = userDefinedSize || defaultSize || bboxSize;
  115. const length = userDefinedLength || defaultLength || bboxLength;
  116. const orientation = isHorizontal ? 'horizontal' : 'vertical';
  117. const [width, height] = isHorizontal ? [length, size] : [size, length];
  118. return { orientation, width, height, size, length };
  119. }
  120. exports.inferComponentShape = inferComponentShape;
  121. function domainOf(scales) {
  122. // to get a available scale's domain
  123. return scales
  124. .find((scale) => scale.getOptions().domain.length > 0)
  125. .getOptions().domain;
  126. }
  127. exports.domainOf = domainOf;
  128. function adaptor(style) {
  129. const reservedKeys = [
  130. 'arrow',
  131. 'crosshairs',
  132. 'grid',
  133. 'handle',
  134. 'handleLabel',
  135. 'indicator',
  136. 'label',
  137. 'line',
  138. 'tick',
  139. 'tip',
  140. 'title',
  141. 'trunc',
  142. ];
  143. // @ts-ignore
  144. const { style: styles } = style, rest = __rest(style, ["style"]);
  145. const finalStyle = {};
  146. Object.entries(rest).forEach(([key, value]) => {
  147. if (reservedKeys.includes(key)) {
  148. finalStyle[`show${(0, util_1.upperFirst)(key)}`] = value;
  149. }
  150. else
  151. finalStyle[key] = value;
  152. });
  153. return Object.assign(Object.assign({}, finalStyle), styles);
  154. }
  155. exports.adaptor = adaptor;
  156. //# sourceMappingURL=utils.js.map