clazz.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 { __extends } from "tslib";
  41. import * as zrUtil from 'zrender/lib/core/util.js';
  42. var TYPE_DELIMITER = '.';
  43. var IS_CONTAINER = '___EC__COMPONENT__CONTAINER___';
  44. var IS_EXTENDED_CLASS = '___EC__EXTENDED_CLASS___';
  45. /**
  46. * Notice, parseClassType('') should returns {main: '', sub: ''}
  47. * @public
  48. */
  49. export function parseClassType(componentType) {
  50. var ret = {
  51. main: '',
  52. sub: ''
  53. };
  54. if (componentType) {
  55. var typeArr = componentType.split(TYPE_DELIMITER);
  56. ret.main = typeArr[0] || '';
  57. ret.sub = typeArr[1] || '';
  58. }
  59. return ret;
  60. }
  61. /**
  62. * @public
  63. */
  64. function checkClassType(componentType) {
  65. zrUtil.assert(/^[a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)?$/.test(componentType), 'componentType "' + componentType + '" illegal');
  66. }
  67. export function isExtendedClass(clz) {
  68. return !!(clz && clz[IS_EXTENDED_CLASS]);
  69. }
  70. /**
  71. * Implements `ExtendableConstructor` for `rootClz`.
  72. *
  73. * @usage
  74. * ```ts
  75. * class Xxx {}
  76. * type XxxConstructor = typeof Xxx & ExtendableConstructor
  77. * enableClassExtend(Xxx as XxxConstructor);
  78. * ```
  79. */
  80. export function enableClassExtend(rootClz, mandatoryMethods) {
  81. rootClz.$constructor = rootClz; // FIXME: not necessary?
  82. rootClz.extend = function (proto) {
  83. if (process.env.NODE_ENV !== 'production') {
  84. zrUtil.each(mandatoryMethods, function (method) {
  85. if (!proto[method]) {
  86. console.warn('Method `' + method + '` should be implemented' + (proto.type ? ' in ' + proto.type : '') + '.');
  87. }
  88. });
  89. }
  90. var superClass = this;
  91. var ExtendedClass;
  92. if (isESClass(superClass)) {
  93. ExtendedClass = /** @class */function (_super) {
  94. __extends(class_1, _super);
  95. function class_1() {
  96. return _super.apply(this, arguments) || this;
  97. }
  98. return class_1;
  99. }(superClass);
  100. } else {
  101. // For backward compat, we both support ts class inheritance and this
  102. // "extend" approach.
  103. // The constructor should keep the same behavior as ts class inheritance:
  104. // If this constructor/$constructor is not declared, auto invoke the super
  105. // constructor.
  106. // If this constructor/$constructor is declared, it is responsible for
  107. // calling the super constructor.
  108. ExtendedClass = function () {
  109. (proto.$constructor || superClass).apply(this, arguments);
  110. };
  111. zrUtil.inherits(ExtendedClass, this);
  112. }
  113. zrUtil.extend(ExtendedClass.prototype, proto);
  114. ExtendedClass[IS_EXTENDED_CLASS] = true;
  115. ExtendedClass.extend = this.extend;
  116. ExtendedClass.superCall = superCall;
  117. ExtendedClass.superApply = superApply;
  118. ExtendedClass.superClass = superClass;
  119. return ExtendedClass;
  120. };
  121. }
  122. function isESClass(fn) {
  123. return zrUtil.isFunction(fn) && /^class\s/.test(Function.prototype.toString.call(fn));
  124. }
  125. /**
  126. * A work around to both support ts extend and this extend mechanism.
  127. * on sub-class.
  128. * @usage
  129. * ```ts
  130. * class Component { ... }
  131. * classUtil.enableClassExtend(Component);
  132. * classUtil.enableClassManagement(Component, {registerWhenExtend: true});
  133. *
  134. * class Series extends Component { ... }
  135. * // Without calling `markExtend`, `registerWhenExtend` will not work.
  136. * Component.markExtend(Series);
  137. * ```
  138. */
  139. export function mountExtend(SubClz, SupperClz) {
  140. SubClz.extend = SupperClz.extend;
  141. }
  142. // A random offset.
  143. var classBase = Math.round(Math.random() * 10);
  144. /**
  145. * Implements `CheckableConstructor` for `target`.
  146. * Can not use instanceof, consider different scope by
  147. * cross domain or es module import in ec extensions.
  148. * Mount a method "isInstance()" to Clz.
  149. *
  150. * @usage
  151. * ```ts
  152. * class Xxx {}
  153. * type XxxConstructor = typeof Xxx & CheckableConstructor;
  154. * enableClassCheck(Xxx as XxxConstructor)
  155. * ```
  156. */
  157. export function enableClassCheck(target) {
  158. var classAttr = ['__\0is_clz', classBase++].join('_');
  159. target.prototype[classAttr] = true;
  160. if (process.env.NODE_ENV !== 'production') {
  161. zrUtil.assert(!target.isInstance, 'The method "is" can not be defined.');
  162. }
  163. target.isInstance = function (obj) {
  164. return !!(obj && obj[classAttr]);
  165. };
  166. }
  167. // superCall should have class info, which can not be fetched from 'this'.
  168. // Consider this case:
  169. // class A has method f,
  170. // class B inherits class A, overrides method f, f call superApply('f'),
  171. // class C inherits class B, does not override method f,
  172. // then when method of class C is called, dead loop occurred.
  173. function superCall(context, methodName) {
  174. var args = [];
  175. for (var _i = 2; _i < arguments.length; _i++) {
  176. args[_i - 2] = arguments[_i];
  177. }
  178. return this.superClass.prototype[methodName].apply(context, args);
  179. }
  180. function superApply(context, methodName, args) {
  181. return this.superClass.prototype[methodName].apply(context, args);
  182. }
  183. /**
  184. * Implements `ClassManager` for `target`
  185. *
  186. * @usage
  187. * ```ts
  188. * class Xxx {}
  189. * type XxxConstructor = typeof Xxx & ClassManager
  190. * enableClassManagement(Xxx as XxxConstructor);
  191. * ```
  192. */
  193. export function enableClassManagement(target) {
  194. /**
  195. * Component model classes
  196. * key: componentType,
  197. * value:
  198. * componentClass, when componentType is 'a'
  199. * or Object.<subKey, componentClass>, when componentType is 'a.b'
  200. */
  201. var storage = {};
  202. target.registerClass = function (clz) {
  203. // `type` should not be a "instance member".
  204. // If using TS class, should better declared as `static type = 'series.pie'`.
  205. // otherwise users have to mount `type` on prototype manually.
  206. // For backward compat and enable instance visit type via `this.type`,
  207. // we still support fetch `type` from prototype.
  208. var componentFullType = clz.type || clz.prototype.type;
  209. if (componentFullType) {
  210. checkClassType(componentFullType);
  211. // If only static type declared, we assign it to prototype mandatorily.
  212. clz.prototype.type = componentFullType;
  213. var componentTypeInfo = parseClassType(componentFullType);
  214. if (!componentTypeInfo.sub) {
  215. if (process.env.NODE_ENV !== 'production') {
  216. if (storage[componentTypeInfo.main]) {
  217. console.warn(componentTypeInfo.main + ' exists.');
  218. }
  219. }
  220. storage[componentTypeInfo.main] = clz;
  221. } else if (componentTypeInfo.sub !== IS_CONTAINER) {
  222. var container = makeContainer(componentTypeInfo);
  223. container[componentTypeInfo.sub] = clz;
  224. }
  225. }
  226. return clz;
  227. };
  228. target.getClass = function (mainType, subType, throwWhenNotFound) {
  229. var clz = storage[mainType];
  230. if (clz && clz[IS_CONTAINER]) {
  231. clz = subType ? clz[subType] : null;
  232. }
  233. if (throwWhenNotFound && !clz) {
  234. throw new Error(!subType ? mainType + '.' + 'type should be specified.' : 'Component ' + mainType + '.' + (subType || '') + ' is used but not imported.');
  235. }
  236. return clz;
  237. };
  238. target.getClassesByMainType = function (componentType) {
  239. var componentTypeInfo = parseClassType(componentType);
  240. var result = [];
  241. var obj = storage[componentTypeInfo.main];
  242. if (obj && obj[IS_CONTAINER]) {
  243. zrUtil.each(obj, function (o, type) {
  244. type !== IS_CONTAINER && result.push(o);
  245. });
  246. } else {
  247. result.push(obj);
  248. }
  249. return result;
  250. };
  251. target.hasClass = function (componentType) {
  252. // Just consider componentType.main.
  253. var componentTypeInfo = parseClassType(componentType);
  254. return !!storage[componentTypeInfo.main];
  255. };
  256. /**
  257. * @return Like ['aa', 'bb'], but can not be ['aa.xx']
  258. */
  259. target.getAllClassMainTypes = function () {
  260. var types = [];
  261. zrUtil.each(storage, function (obj, type) {
  262. types.push(type);
  263. });
  264. return types;
  265. };
  266. /**
  267. * If a main type is container and has sub types
  268. */
  269. target.hasSubTypes = function (componentType) {
  270. var componentTypeInfo = parseClassType(componentType);
  271. var obj = storage[componentTypeInfo.main];
  272. return obj && obj[IS_CONTAINER];
  273. };
  274. function makeContainer(componentTypeInfo) {
  275. var container = storage[componentTypeInfo.main];
  276. if (!container || !container[IS_CONTAINER]) {
  277. container = storage[componentTypeInfo.main] = {};
  278. container[IS_CONTAINER] = true;
  279. }
  280. return container;
  281. }
  282. }
  283. // /**
  284. // * @param {string|Array.<string>} properties
  285. // */
  286. // export function setReadOnly(obj, properties) {
  287. // FIXME It seems broken in IE8 simulation of IE11
  288. // if (!zrUtil.isArray(properties)) {
  289. // properties = properties != null ? [properties] : [];
  290. // }
  291. // zrUtil.each(properties, function (prop) {
  292. // let value = obj[prop];
  293. // Object.defineProperty
  294. // && Object.defineProperty(obj, prop, {
  295. // value: value, writable: false
  296. // });
  297. // zrUtil.isArray(obj[prop])
  298. // && Object.freeze
  299. // && Object.freeze(obj[prop]);
  300. // });
  301. // }