component.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 _clazz = require("./clazz");
  21. var parseClassType = _clazz.parseClassType;
  22. /*
  23. * Licensed to the Apache Software Foundation (ASF) under one
  24. * or more contributor license agreements. See the NOTICE file
  25. * distributed with this work for additional information
  26. * regarding copyright ownership. The ASF licenses this file
  27. * to you under the Apache License, Version 2.0 (the
  28. * "License"); you may not use this file except in compliance
  29. * with the License. You may obtain a copy of the License at
  30. *
  31. * http://www.apache.org/licenses/LICENSE-2.0
  32. *
  33. * Unless required by applicable law or agreed to in writing,
  34. * software distributed under the License is distributed on an
  35. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  36. * KIND, either express or implied. See the License for the
  37. * specific language governing permissions and limitations
  38. * under the License.
  39. */
  40. var base = 0;
  41. /**
  42. * @public
  43. * @param {string} type
  44. * @return {string}
  45. */
  46. function getUID(type) {
  47. // Considering the case of crossing js context,
  48. // use Math.random to make id as unique as possible.
  49. return [type || '', base++, Math.random().toFixed(5)].join('_');
  50. }
  51. /**
  52. * @inner
  53. */
  54. function enableSubTypeDefaulter(entity) {
  55. var subTypeDefaulters = {};
  56. entity.registerSubTypeDefaulter = function (componentType, defaulter) {
  57. componentType = parseClassType(componentType);
  58. subTypeDefaulters[componentType.main] = defaulter;
  59. };
  60. entity.determineSubType = function (componentType, option) {
  61. var type = option.type;
  62. if (!type) {
  63. var componentTypeMain = parseClassType(componentType).main;
  64. if (entity.hasSubTypes(componentType) && subTypeDefaulters[componentTypeMain]) {
  65. type = subTypeDefaulters[componentTypeMain](option);
  66. }
  67. }
  68. return type;
  69. };
  70. return entity;
  71. }
  72. /**
  73. * Topological travel on Activity Network (Activity On Vertices).
  74. * Dependencies is defined in Model.prototype.dependencies, like ['xAxis', 'yAxis'].
  75. *
  76. * If 'xAxis' or 'yAxis' is absent in componentTypeList, just ignore it in topology.
  77. *
  78. * If there is circle dependencey, Error will be thrown.
  79. *
  80. */
  81. function enableTopologicalTravel(entity, dependencyGetter) {
  82. /**
  83. * @public
  84. * @param {Array.<string>} targetNameList Target Component type list.
  85. * Can be ['aa', 'bb', 'aa.xx']
  86. * @param {Array.<string>} fullNameList By which we can build dependency graph.
  87. * @param {Function} callback Params: componentType, dependencies.
  88. * @param {Object} context Scope of callback.
  89. */
  90. entity.topologicalTravel = function (targetNameList, fullNameList, callback, context) {
  91. if (!targetNameList.length) {
  92. return;
  93. }
  94. var result = makeDepndencyGraph(fullNameList);
  95. var graph = result.graph;
  96. var stack = result.noEntryList;
  97. var targetNameSet = {};
  98. zrUtil.each(targetNameList, function (name) {
  99. targetNameSet[name] = true;
  100. });
  101. while (stack.length) {
  102. var currComponentType = stack.pop();
  103. var currVertex = graph[currComponentType];
  104. var isInTargetNameSet = !!targetNameSet[currComponentType];
  105. if (isInTargetNameSet) {
  106. callback.call(context, currComponentType, currVertex.originalDeps.slice());
  107. delete targetNameSet[currComponentType];
  108. }
  109. zrUtil.each(currVertex.successor, isInTargetNameSet ? removeEdgeAndAdd : removeEdge);
  110. }
  111. zrUtil.each(targetNameSet, function () {
  112. throw new Error('Circle dependency may exists');
  113. });
  114. function removeEdge(succComponentType) {
  115. graph[succComponentType].entryCount--;
  116. if (graph[succComponentType].entryCount === 0) {
  117. stack.push(succComponentType);
  118. }
  119. } // Consider this case: legend depends on series, and we call
  120. // chart.setOption({series: [...]}), where only series is in option.
  121. // If we do not have 'removeEdgeAndAdd', legendModel.mergeOption will
  122. // not be called, but only sereis.mergeOption is called. Thus legend
  123. // have no chance to update its local record about series (like which
  124. // name of series is available in legend).
  125. function removeEdgeAndAdd(succComponentType) {
  126. targetNameSet[succComponentType] = true;
  127. removeEdge(succComponentType);
  128. }
  129. };
  130. /**
  131. * DepndencyGraph: {Object}
  132. * key: conponentType,
  133. * value: {
  134. * successor: [conponentTypes...],
  135. * originalDeps: [conponentTypes...],
  136. * entryCount: {number}
  137. * }
  138. */
  139. function makeDepndencyGraph(fullNameList) {
  140. var graph = {};
  141. var noEntryList = [];
  142. zrUtil.each(fullNameList, function (name) {
  143. var thisItem = createDependencyGraphItem(graph, name);
  144. var originalDeps = thisItem.originalDeps = dependencyGetter(name);
  145. var availableDeps = getAvailableDependencies(originalDeps, fullNameList);
  146. thisItem.entryCount = availableDeps.length;
  147. if (thisItem.entryCount === 0) {
  148. noEntryList.push(name);
  149. }
  150. zrUtil.each(availableDeps, function (dependentName) {
  151. if (zrUtil.indexOf(thisItem.predecessor, dependentName) < 0) {
  152. thisItem.predecessor.push(dependentName);
  153. }
  154. var thatItem = createDependencyGraphItem(graph, dependentName);
  155. if (zrUtil.indexOf(thatItem.successor, dependentName) < 0) {
  156. thatItem.successor.push(name);
  157. }
  158. });
  159. });
  160. return {
  161. graph: graph,
  162. noEntryList: noEntryList
  163. };
  164. }
  165. function createDependencyGraphItem(graph, name) {
  166. if (!graph[name]) {
  167. graph[name] = {
  168. predecessor: [],
  169. successor: []
  170. };
  171. }
  172. return graph[name];
  173. }
  174. function getAvailableDependencies(originalDeps, fullNameList) {
  175. var availableDeps = [];
  176. zrUtil.each(originalDeps, function (dep) {
  177. zrUtil.indexOf(fullNameList, dep) >= 0 && availableDeps.push(dep);
  178. });
  179. return availableDeps;
  180. }
  181. }
  182. exports.getUID = getUID;
  183. exports.enableSubTypeDefaulter = enableSubTypeDefaulter;
  184. exports.enableTopologicalTravel = enableTopologicalTravel;