helper.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. var zrUtil = require("zrender/lib/core/util");
  20. var formatUtil = require("../../util/format");
  21. /*
  22. * Licensed to the Apache Software Foundation (ASF) under one
  23. * or more contributor license agreements. See the NOTICE file
  24. * distributed with this work for additional information
  25. * regarding copyright ownership. The ASF licenses this file
  26. * to you under the Apache License, Version 2.0 (the
  27. * "License"); you may not use this file except in compliance
  28. * with the License. You may obtain a copy of the License at
  29. *
  30. * http://www.apache.org/licenses/LICENSE-2.0
  31. *
  32. * Unless required by applicable law or agreed to in writing,
  33. * software distributed under the License is distributed on an
  34. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  35. * KIND, either express or implied. See the License for the
  36. * specific language governing permissions and limitations
  37. * under the License.
  38. */
  39. var AXIS_DIMS = ['x', 'y', 'z', 'radius', 'angle', 'single']; // Supported coords.
  40. var COORDS = ['cartesian2d', 'polar', 'singleAxis'];
  41. /**
  42. * @param {string} coordType
  43. * @return {boolean}
  44. */
  45. function isCoordSupported(coordType) {
  46. return zrUtil.indexOf(COORDS, coordType) >= 0;
  47. }
  48. /**
  49. * Create "each" method to iterate names.
  50. *
  51. * @pubilc
  52. * @param {Array.<string>} names
  53. * @param {Array.<string>=} attrs
  54. * @return {Function}
  55. */
  56. function createNameEach(names, attrs) {
  57. names = names.slice();
  58. var capitalNames = zrUtil.map(names, formatUtil.capitalFirst);
  59. attrs = (attrs || []).slice();
  60. var capitalAttrs = zrUtil.map(attrs, formatUtil.capitalFirst);
  61. return function (callback, context) {
  62. zrUtil.each(names, function (name, index) {
  63. var nameObj = {
  64. name: name,
  65. capital: capitalNames[index]
  66. };
  67. for (var j = 0; j < attrs.length; j++) {
  68. nameObj[attrs[j]] = name + capitalAttrs[j];
  69. }
  70. callback.call(context, nameObj);
  71. });
  72. };
  73. }
  74. /**
  75. * Iterate each dimension name.
  76. *
  77. * @public
  78. * @param {Function} callback The parameter is like:
  79. * {
  80. * name: 'angle',
  81. * capital: 'Angle',
  82. * axis: 'angleAxis',
  83. * axisIndex: 'angleAixs',
  84. * index: 'angleIndex'
  85. * }
  86. * @param {Object} context
  87. */
  88. var eachAxisDim = createNameEach(AXIS_DIMS, ['axisIndex', 'axis', 'index', 'id']);
  89. /**
  90. * If tow dataZoomModels has the same axis controlled, we say that they are 'linked'.
  91. * dataZoomModels and 'links' make up one or more graphics.
  92. * This function finds the graphic where the source dataZoomModel is in.
  93. *
  94. * @public
  95. * @param {Function} forEachNode Node iterator.
  96. * @param {Function} forEachEdgeType edgeType iterator
  97. * @param {Function} edgeIdGetter Giving node and edgeType, return an array of edge id.
  98. * @return {Function} Input: sourceNode, Output: Like {nodes: [], dims: {}}
  99. */
  100. function createLinkedNodesFinder(forEachNode, forEachEdgeType, edgeIdGetter) {
  101. return function (sourceNode) {
  102. var result = {
  103. nodes: [],
  104. records: {} // key: edgeType.name, value: Object (key: edge id, value: boolean).
  105. };
  106. forEachEdgeType(function (edgeType) {
  107. result.records[edgeType.name] = {};
  108. });
  109. if (!sourceNode) {
  110. return result;
  111. }
  112. absorb(sourceNode, result);
  113. var existsLink;
  114. do {
  115. existsLink = false;
  116. forEachNode(processSingleNode);
  117. } while (existsLink);
  118. function processSingleNode(node) {
  119. if (!isNodeAbsorded(node, result) && isLinked(node, result)) {
  120. absorb(node, result);
  121. existsLink = true;
  122. }
  123. }
  124. return result;
  125. };
  126. function isNodeAbsorded(node, result) {
  127. return zrUtil.indexOf(result.nodes, node) >= 0;
  128. }
  129. function isLinked(node, result) {
  130. var hasLink = false;
  131. forEachEdgeType(function (edgeType) {
  132. zrUtil.each(edgeIdGetter(node, edgeType) || [], function (edgeId) {
  133. result.records[edgeType.name][edgeId] && (hasLink = true);
  134. });
  135. });
  136. return hasLink;
  137. }
  138. function absorb(node, result) {
  139. result.nodes.push(node);
  140. forEachEdgeType(function (edgeType) {
  141. zrUtil.each(edgeIdGetter(node, edgeType) || [], function (edgeId) {
  142. result.records[edgeType.name][edgeId] = true;
  143. });
  144. });
  145. }
  146. }
  147. exports.isCoordSupported = isCoordSupported;
  148. exports.createNameEach = createNameEach;
  149. exports.eachAxisDim = eachAxisDim;
  150. exports.createLinkedNodesFinder = createLinkedNodesFinder;