helper.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. var zrUtil = require("zrender/lib/core/util");
  2. var formatUtil = require("../../util/format");
  3. var AXIS_DIMS = ['x', 'y', 'z', 'radius', 'angle', 'single']; // Supported coords.
  4. var COORDS = ['cartesian2d', 'polar', 'singleAxis'];
  5. /**
  6. * @param {string} coordType
  7. * @return {boolean}
  8. */
  9. function isCoordSupported(coordType) {
  10. return zrUtil.indexOf(COORDS, coordType) >= 0;
  11. }
  12. /**
  13. * Create "each" method to iterate names.
  14. *
  15. * @pubilc
  16. * @param {Array.<string>} names
  17. * @param {Array.<string>=} attrs
  18. * @return {Function}
  19. */
  20. function createNameEach(names, attrs) {
  21. names = names.slice();
  22. var capitalNames = zrUtil.map(names, formatUtil.capitalFirst);
  23. attrs = (attrs || []).slice();
  24. var capitalAttrs = zrUtil.map(attrs, formatUtil.capitalFirst);
  25. return function (callback, context) {
  26. zrUtil.each(names, function (name, index) {
  27. var nameObj = {
  28. name: name,
  29. capital: capitalNames[index]
  30. };
  31. for (var j = 0; j < attrs.length; j++) {
  32. nameObj[attrs[j]] = name + capitalAttrs[j];
  33. }
  34. callback.call(context, nameObj);
  35. });
  36. };
  37. }
  38. /**
  39. * Iterate each dimension name.
  40. *
  41. * @public
  42. * @param {Function} callback The parameter is like:
  43. * {
  44. * name: 'angle',
  45. * capital: 'Angle',
  46. * axis: 'angleAxis',
  47. * axisIndex: 'angleAixs',
  48. * index: 'angleIndex'
  49. * }
  50. * @param {Object} context
  51. */
  52. var eachAxisDim = createNameEach(AXIS_DIMS, ['axisIndex', 'axis', 'index', 'id']);
  53. /**
  54. * If tow dataZoomModels has the same axis controlled, we say that they are 'linked'.
  55. * dataZoomModels and 'links' make up one or more graphics.
  56. * This function finds the graphic where the source dataZoomModel is in.
  57. *
  58. * @public
  59. * @param {Function} forEachNode Node iterator.
  60. * @param {Function} forEachEdgeType edgeType iterator
  61. * @param {Function} edgeIdGetter Giving node and edgeType, return an array of edge id.
  62. * @return {Function} Input: sourceNode, Output: Like {nodes: [], dims: {}}
  63. */
  64. function createLinkedNodesFinder(forEachNode, forEachEdgeType, edgeIdGetter) {
  65. return function (sourceNode) {
  66. var result = {
  67. nodes: [],
  68. records: {} // key: edgeType.name, value: Object (key: edge id, value: boolean).
  69. };
  70. forEachEdgeType(function (edgeType) {
  71. result.records[edgeType.name] = {};
  72. });
  73. if (!sourceNode) {
  74. return result;
  75. }
  76. absorb(sourceNode, result);
  77. var existsLink;
  78. do {
  79. existsLink = false;
  80. forEachNode(processSingleNode);
  81. } while (existsLink);
  82. function processSingleNode(node) {
  83. if (!isNodeAbsorded(node, result) && isLinked(node, result)) {
  84. absorb(node, result);
  85. existsLink = true;
  86. }
  87. }
  88. return result;
  89. };
  90. function isNodeAbsorded(node, result) {
  91. return zrUtil.indexOf(result.nodes, node) >= 0;
  92. }
  93. function isLinked(node, result) {
  94. var hasLink = false;
  95. forEachEdgeType(function (edgeType) {
  96. zrUtil.each(edgeIdGetter(node, edgeType) || [], function (edgeId) {
  97. result.records[edgeType.name][edgeId] && (hasLink = true);
  98. });
  99. });
  100. return hasLink;
  101. }
  102. function absorb(node, result) {
  103. result.nodes.push(node);
  104. forEachEdgeType(function (edgeType) {
  105. zrUtil.each(edgeIdGetter(node, edgeType) || [], function (edgeId) {
  106. result.records[edgeType.name][edgeId] = true;
  107. });
  108. });
  109. }
  110. }
  111. exports.isCoordSupported = isCoordSupported;
  112. exports.createNameEach = createNameEach;
  113. exports.eachAxisDim = eachAxisDim;
  114. exports.createLinkedNodesFinder = createLinkedNodesFinder;