UNPKG

cione-comp-lib

Version:

`$ yarn add cione-comp-lib` 或者 `$ npm i cione-comp-lib -S`

142 lines (141 loc) 4.77 kB
import { __extends } from "../../../tslib/tslib.es6.mjs"; import { inherits, extend, isFunction, each, assert } from "../../../zrender/lib/core/util.mjs"; var TYPE_DELIMITER = "."; var IS_CONTAINER = "___EC__COMPONENT__CONTAINER___"; var IS_EXTENDED_CLASS = "___EC__EXTENDED_CLASS___"; function parseClassType(componentType) { var ret = { main: "", sub: "" }; if (componentType) { var typeArr = componentType.split(TYPE_DELIMITER); ret.main = typeArr[0] || ""; ret.sub = typeArr[1] || ""; } return ret; } function checkClassType(componentType) { assert(/^[a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)?$/.test(componentType), 'componentType "' + componentType + '" illegal'); } function isExtendedClass(clz) { return !!(clz && clz[IS_EXTENDED_CLASS]); } function enableClassExtend(rootClz, mandatoryMethods) { rootClz.$constructor = rootClz; rootClz.extend = function(proto) { var superClass = this; var ExtendedClass; if (isESClass(superClass)) { ExtendedClass = function(_super) { __extends(class_1, _super); function class_1() { return _super.apply(this, arguments) || this; } return class_1; }(superClass); } else { ExtendedClass = function() { (proto.$constructor || superClass).apply(this, arguments); }; inherits(ExtendedClass, this); } extend(ExtendedClass.prototype, proto); ExtendedClass[IS_EXTENDED_CLASS] = true; ExtendedClass.extend = this.extend; ExtendedClass.superCall = superCall; ExtendedClass.superApply = superApply; ExtendedClass.superClass = superClass; return ExtendedClass; }; } function isESClass(fn) { return isFunction(fn) && /^class\s/.test(Function.prototype.toString.call(fn)); } function mountExtend(SubClz, SupperClz) { SubClz.extend = SupperClz.extend; } var classBase = Math.round(Math.random() * 10); function enableClassCheck(target) { var classAttr = ["__\0is_clz", classBase++].join("_"); target.prototype[classAttr] = true; target.isInstance = function(obj) { return !!(obj && obj[classAttr]); }; } function superCall(context, methodName) { var args = []; for (var _i = 2; _i < arguments.length; _i++) { args[_i - 2] = arguments[_i]; } return this.superClass.prototype[methodName].apply(context, args); } function superApply(context, methodName, args) { return this.superClass.prototype[methodName].apply(context, args); } function enableClassManagement(target) { var storage = {}; target.registerClass = function(clz) { var componentFullType = clz.type || clz.prototype.type; if (componentFullType) { checkClassType(componentFullType); clz.prototype.type = componentFullType; var componentTypeInfo = parseClassType(componentFullType); if (!componentTypeInfo.sub) { storage[componentTypeInfo.main] = clz; } else if (componentTypeInfo.sub !== IS_CONTAINER) { var container = makeContainer(componentTypeInfo); container[componentTypeInfo.sub] = clz; } } return clz; }; target.getClass = function(mainType, subType, throwWhenNotFound) { var clz = storage[mainType]; if (clz && clz[IS_CONTAINER]) { clz = subType ? clz[subType] : null; } if (throwWhenNotFound && !clz) { throw new Error(!subType ? mainType + ".type should be specified." : "Component " + mainType + "." + (subType || "") + " is used but not imported."); } return clz; }; target.getClassesByMainType = function(componentType) { var componentTypeInfo = parseClassType(componentType); var result = []; var obj = storage[componentTypeInfo.main]; if (obj && obj[IS_CONTAINER]) { each(obj, function(o, type) { type !== IS_CONTAINER && result.push(o); }); } else { result.push(obj); } return result; }; target.hasClass = function(componentType) { var componentTypeInfo = parseClassType(componentType); return !!storage[componentTypeInfo.main]; }; target.getAllClassMainTypes = function() { var types = []; each(storage, function(obj, type) { types.push(type); }); return types; }; target.hasSubTypes = function(componentType) { var componentTypeInfo = parseClassType(componentType); var obj = storage[componentTypeInfo.main]; return obj && obj[IS_CONTAINER]; }; function makeContainer(componentTypeInfo) { var container = storage[componentTypeInfo.main]; if (!container || !container[IS_CONTAINER]) { container = storage[componentTypeInfo.main] = {}; container[IS_CONTAINER] = true; } return container; } } export { enableClassCheck, enableClassExtend, enableClassManagement, isExtendedClass, mountExtend, parseClassType };