clazz.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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 =
  94. /** @class */
  95. function (_super) {
  96. __extends(class_1, _super);
  97. function class_1() {
  98. return _super.apply(this, arguments) || this;
  99. }
  100. return class_1;
  101. }(superClass);
  102. } else {
  103. // For backward compat, we both support ts class inheritance and this
  104. // "extend" approach.
  105. // The constructor should keep the same behavior as ts class inheritance:
  106. // If this constructor/$constructor is not declared, auto invoke the super
  107. // constructor.
  108. // If this constructor/$constructor is declared, it is responsible for
  109. // calling the super constructor.
  110. ExtendedClass = function () {
  111. (proto.$constructor || superClass).apply(this, arguments);
  112. };
  113. zrUtil.inherits(ExtendedClass, this);
  114. }
  115. zrUtil.extend(ExtendedClass.prototype, proto);
  116. ExtendedClass[IS_EXTENDED_CLASS] = true;
  117. ExtendedClass.extend = this.extend;
  118. ExtendedClass.superCall = superCall;
  119. ExtendedClass.superApply = superApply;
  120. ExtendedClass.superClass = superClass;
  121. return ExtendedClass;
  122. };
  123. }
  124. function isESClass(fn) {
  125. return zrUtil.isFunction(fn) && /^class\s/.test(Function.prototype.toString.call(fn));
  126. }
  127. /**
  128. * A work around to both support ts extend and this extend mechanism.
  129. * on sub-class.
  130. * @usage
  131. * ```ts
  132. * class Component { ... }
  133. * classUtil.enableClassExtend(Component);
  134. * classUtil.enableClassManagement(Component, {registerWhenExtend: true});
  135. *
  136. * class Series extends Component { ... }
  137. * // Without calling `markExtend`, `registerWhenExtend` will not work.
  138. * Component.markExtend(Series);
  139. * ```
  140. */
  141. export function mountExtend(SubClz, SupperClz) {
  142. SubClz.extend = SupperClz.extend;
  143. } // A random offset.
  144. var classBase = Math.round(Math.random() * 10);
  145. /**
  146. * Implements `CheckableConstructor` for `target`.
  147. * Can not use instanceof, consider different scope by
  148. * cross domain or es module import in ec extensions.
  149. * Mount a method "isInstance()" to Clz.
  150. *
  151. * @usage
  152. * ```ts
  153. * class Xxx {}
  154. * type XxxConstructor = typeof Xxx & CheckableConstructor;
  155. * enableClassCheck(Xxx as XxxConstructor)
  156. * ```
  157. */
  158. export function enableClassCheck(target) {
  159. var classAttr = ['__\0is_clz', classBase++].join('_');
  160. target.prototype[classAttr] = true;
  161. if (process.env.NODE_ENV !== 'production') {
  162. zrUtil.assert(!target.isInstance, 'The method "is" can not be defined.');
  163. }
  164. target.isInstance = function (obj) {
  165. return !!(obj && obj[classAttr]);
  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); // If only static type declared, we assign it to prototype mandatorily.
  211. clz.prototype.type = componentFullType;
  212. var componentTypeInfo = parseClassType(componentFullType);
  213. if (!componentTypeInfo.sub) {
  214. if (process.env.NODE_ENV !== 'production') {
  215. if (storage[componentTypeInfo.main]) {
  216. console.warn(componentTypeInfo.main + ' exists.');
  217. }
  218. }
  219. storage[componentTypeInfo.main] = clz;
  220. } else if (componentTypeInfo.sub !== IS_CONTAINER) {
  221. var container = makeContainer(componentTypeInfo);
  222. container[componentTypeInfo.sub] = clz;
  223. }
  224. }
  225. return clz;
  226. };
  227. target.getClass = function (mainType, subType, throwWhenNotFound) {
  228. var clz = storage[mainType];
  229. if (clz && clz[IS_CONTAINER]) {
  230. clz = subType ? clz[subType] : null;
  231. }
  232. if (throwWhenNotFound && !clz) {
  233. throw new Error(!subType ? mainType + '.' + 'type should be specified.' : 'Component ' + mainType + '.' + (subType || '') + ' is used but not imported.');
  234. }
  235. return clz;
  236. };
  237. target.getClassesByMainType = function (componentType) {
  238. var componentTypeInfo = parseClassType(componentType);
  239. var result = [];
  240. var obj = storage[componentTypeInfo.main];
  241. if (obj && obj[IS_CONTAINER]) {
  242. zrUtil.each(obj, function (o, type) {
  243. type !== IS_CONTAINER && result.push(o);
  244. });
  245. } else {
  246. result.push(obj);
  247. }
  248. return result;
  249. };
  250. target.hasClass = function (componentType) {
  251. // Just consider componentType.main.
  252. var componentTypeInfo = parseClassType(componentType);
  253. return !!storage[componentTypeInfo.main];
  254. };
  255. /**
  256. * @return Like ['aa', 'bb'], but can not be ['aa.xx']
  257. */
  258. target.getAllClassMainTypes = function () {
  259. var types = [];
  260. zrUtil.each(storage, function (obj, type) {
  261. types.push(type);
  262. });
  263. return types;
  264. };
  265. /**
  266. * If a main type is container and has sub types
  267. */
  268. target.hasSubTypes = function (componentType) {
  269. var componentTypeInfo = parseClassType(componentType);
  270. var obj = storage[componentTypeInfo.main];
  271. return obj && obj[IS_CONTAINER];
  272. };
  273. function makeContainer(componentTypeInfo) {
  274. var container = storage[componentTypeInfo.main];
  275. if (!container || !container[IS_CONTAINER]) {
  276. container = storage[componentTypeInfo.main] = {};
  277. container[IS_CONTAINER] = true;
  278. }
  279. return container;
  280. }
  281. } // /**
  282. // * @param {string|Array.<string>} properties
  283. // */
  284. // export function setReadOnly(obj, properties) {
  285. // FIXME It seems broken in IE8 simulation of IE11
  286. // if (!zrUtil.isArray(properties)) {
  287. // properties = properties != null ? [properties] : [];
  288. // }
  289. // zrUtil.each(properties, function (prop) {
  290. // let value = obj[prop];
  291. // Object.defineProperty
  292. // && Object.defineProperty(obj, prop, {
  293. // value: value, writable: false
  294. // });
  295. // zrUtil.isArray(obj[prop])
  296. // && Object.freeze
  297. // && Object.freeze(obj[prop]);
  298. // });
  299. // }