component.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. /**
  20. * AUTO-GENERATED FILE. DO NOT MODIFY.
  21. */
  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. import * as zrUtil from 'zrender/lib/core/util.js';
  41. import { parseClassType } from './clazz.js';
  42. import { makePrintable } from './log.js';
  43. // A random offset
  44. var base = Math.round(Math.random() * 10);
  45. /**
  46. * @public
  47. * @param {string} type
  48. * @return {string}
  49. */
  50. export function getUID(type) {
  51. // Considering the case of crossing js context,
  52. // use Math.random to make id as unique as possible.
  53. return [type || '', base++].join('_');
  54. }
  55. /**
  56. * Implements `SubTypeDefaulterManager` for `target`.
  57. */
  58. export function enableSubTypeDefaulter(target) {
  59. var subTypeDefaulters = {};
  60. target.registerSubTypeDefaulter = function (componentType, defaulter) {
  61. var componentTypeInfo = parseClassType(componentType);
  62. subTypeDefaulters[componentTypeInfo.main] = defaulter;
  63. };
  64. target.determineSubType = function (componentType, option) {
  65. var type = option.type;
  66. if (!type) {
  67. var componentTypeMain = parseClassType(componentType).main;
  68. if (target.hasSubTypes(componentType) && subTypeDefaulters[componentTypeMain]) {
  69. type = subTypeDefaulters[componentTypeMain](option);
  70. }
  71. }
  72. return type;
  73. };
  74. }
  75. /**
  76. * Implements `TopologicalTravelable<any>` for `entity`.
  77. *
  78. * Topological travel on Activity Network (Activity On Vertices).
  79. * Dependencies is defined in Model.prototype.dependencies, like ['xAxis', 'yAxis'].
  80. * If 'xAxis' or 'yAxis' is absent in componentTypeList, just ignore it in topology.
  81. * If there is circular dependencey, Error will be thrown.
  82. */
  83. export function enableTopologicalTravel(entity, dependencyGetter) {
  84. /**
  85. * @param targetNameList Target Component type list.
  86. * Can be ['aa', 'bb', 'aa.xx']
  87. * @param fullNameList By which we can build dependency graph.
  88. * @param callback Params: componentType, dependencies.
  89. * @param context Scope of callback.
  90. */
  91. entity.topologicalTravel = function (targetNameList, fullNameList, callback, context) {
  92. if (!targetNameList.length) {
  93. return;
  94. }
  95. var result = makeDepndencyGraph(fullNameList);
  96. var graph = result.graph;
  97. var noEntryList = result.noEntryList;
  98. var targetNameSet = {};
  99. zrUtil.each(targetNameList, function (name) {
  100. targetNameSet[name] = true;
  101. });
  102. while (noEntryList.length) {
  103. var currComponentType = noEntryList.pop();
  104. var currVertex = graph[currComponentType];
  105. var isInTargetNameSet = !!targetNameSet[currComponentType];
  106. if (isInTargetNameSet) {
  107. callback.call(context, currComponentType, currVertex.originalDeps.slice());
  108. delete targetNameSet[currComponentType];
  109. }
  110. zrUtil.each(currVertex.successor, isInTargetNameSet ? removeEdgeAndAdd : removeEdge);
  111. }
  112. zrUtil.each(targetNameSet, function () {
  113. var errMsg = '';
  114. if (process.env.NODE_ENV !== 'production') {
  115. errMsg = makePrintable('Circular dependency may exists: ', targetNameSet, targetNameList, fullNameList);
  116. }
  117. throw new Error(errMsg);
  118. });
  119. function removeEdge(succComponentType) {
  120. graph[succComponentType].entryCount--;
  121. if (graph[succComponentType].entryCount === 0) {
  122. noEntryList.push(succComponentType);
  123. }
  124. }
  125. // Consider this case: legend depends on series, and we call
  126. // chart.setOption({series: [...]}), where only series is in option.
  127. // If we do not have 'removeEdgeAndAdd', legendModel.mergeOption will
  128. // not be called, but only sereis.mergeOption is called. Thus legend
  129. // have no chance to update its local record about series (like which
  130. // name of series is available in legend).
  131. function removeEdgeAndAdd(succComponentType) {
  132. targetNameSet[succComponentType] = true;
  133. removeEdge(succComponentType);
  134. }
  135. };
  136. function makeDepndencyGraph(fullNameList) {
  137. var graph = {};
  138. var noEntryList = [];
  139. zrUtil.each(fullNameList, function (name) {
  140. var thisItem = createDependencyGraphItem(graph, name);
  141. var originalDeps = thisItem.originalDeps = dependencyGetter(name);
  142. var availableDeps = getAvailableDependencies(originalDeps, fullNameList);
  143. thisItem.entryCount = availableDeps.length;
  144. if (thisItem.entryCount === 0) {
  145. noEntryList.push(name);
  146. }
  147. zrUtil.each(availableDeps, function (dependentName) {
  148. if (zrUtil.indexOf(thisItem.predecessor, dependentName) < 0) {
  149. thisItem.predecessor.push(dependentName);
  150. }
  151. var thatItem = createDependencyGraphItem(graph, dependentName);
  152. if (zrUtil.indexOf(thatItem.successor, dependentName) < 0) {
  153. thatItem.successor.push(name);
  154. }
  155. });
  156. });
  157. return {
  158. graph: graph,
  159. noEntryList: noEntryList
  160. };
  161. }
  162. function createDependencyGraphItem(graph, name) {
  163. if (!graph[name]) {
  164. graph[name] = {
  165. predecessor: [],
  166. successor: []
  167. };
  168. }
  169. return graph[name];
  170. }
  171. function getAvailableDependencies(originalDeps, fullNameList) {
  172. var availableDeps = [];
  173. zrUtil.each(originalDeps, function (dep) {
  174. zrUtil.indexOf(fullNameList, dep) >= 0 && availableDeps.push(dep);
  175. });
  176. return availableDeps;
  177. }
  178. }
  179. export function inheritDefaultOption(superOption, subOption) {
  180. // See also `model/Component.ts#getDefaultOption`
  181. return zrUtil.merge(zrUtil.merge({}, superOption, true), subOption, true);
  182. }