component.js 6.8 KB

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