bytev-charts
Version:
基于echarts和JavaScript及ES6封装的一个可以直接调用的图表组件库,内置主题设计,简单快捷,且支持用户自定义配置; npm 安装方式: npm install bytev-charts 若启动提示还需额外install插件,则运行 npm install @babel/runtime-corejs2 即可;
197 lines (168 loc) • 6.42 kB
JavaScript
import _typeof from "@babel/runtime-corejs2/helpers/typeof";
import _Array$isArray from "@babel/runtime-corejs2/core-js/array/is-array";
import "core-js/modules/es.function.name.js";
import "core-js/modules/es.date.to-string.js";
import "core-js/modules/es.object.to-string.js";
import "core-js/modules/es.regexp.to-string.js";
/**
* Development repository: https://github.com/kaisalmen/WWOBJLoader
*/
var CodeSerializer = {
/**
* Serialize an object with specific prototype definition.
*
* @param {Object} targetPrototype The object that should be serialized
* @param {Object} targetPrototypeInstance An instance of the oriobject that should be serialized
* @param {String} [basePrototypeName] Name of the prototype
* @param {Object} [overrideFunctions} Array of {@Link CodeSerializationInstruction} allows to replace or remove function with provided content
*
* @returns {String}
*/
serializeClass: function serializeClass(targetPrototype, targetPrototypeInstance, basePrototypeName, overrideFunctions) {
var objectPart, constructorString, i, funcInstructions, funcTemp;
var fullObjectName = targetPrototypeInstance.constructor.name;
var prototypeFunctions = [];
var objectProperties = [];
var objectFunctions = [];
var isExtended = basePrototypeName !== null && basePrototypeName !== undefined;
if (!_Array$isArray(overrideFunctions)) overrideFunctions = [];
for (var name in targetPrototype.prototype) {
objectPart = targetPrototype.prototype[name];
funcInstructions = new CodeSerializationInstruction(name, fullObjectName + '.prototype.' + name);
funcInstructions.setCode(objectPart.toString());
if (name === 'constructor') {
if (!funcInstructions.isRemoveCode()) {
constructorString = fullObjectName + ' = ' + funcInstructions.getCode() + ';\n\n';
}
} else if (typeof objectPart === 'function') {
funcTemp = overrideFunctions[name];
if (funcTemp instanceof CodeSerializationInstruction && funcTemp.getName() === funcInstructions.getName()) {
funcInstructions = funcTemp;
}
if (!funcInstructions.isRemoveCode()) {
if (isExtended) {
prototypeFunctions.push(funcInstructions.getFullName() + ' = ' + funcInstructions.getCode() + ';\n\n');
} else {
prototypeFunctions.push('\t' + funcInstructions.getName() + ': ' + funcInstructions.getCode() + ',\n\n');
}
}
}
}
for (var _name in targetPrototype) {
objectPart = targetPrototype[_name];
funcInstructions = new CodeSerializationInstruction(_name, fullObjectName + '.' + _name);
if (typeof objectPart === 'function') {
funcTemp = overrideFunctions[_name];
if (funcTemp instanceof CodeSerializationInstruction && funcTemp.getName() === funcInstructions.getName()) {
funcInstructions = funcTemp;
} else {
funcInstructions.setCode(objectPart.toString());
}
if (!funcInstructions.isRemoveCode()) {
objectFunctions.push(funcInstructions.getFullName() + ' = ' + funcInstructions.getCode() + ';\n\n');
}
} else {
if (typeof objectPart === 'string' || objectPart instanceof String) {
funcInstructions.setCode('\"' + objectPart.toString() + '\"');
} else if (_typeof(objectPart) === 'object') {
console.log('Omitting object "' + funcInstructions.getName() + '" and replace it with empty object.');
funcInstructions.setCode("{}");
} else {
funcInstructions.setCode(objectPart);
}
if (!funcInstructions.isRemoveCode()) {
objectProperties.push(funcInstructions.getFullName() + ' = ' + funcInstructions.getCode() + ';\n');
}
}
}
var objectString = constructorString + '\n\n';
if (isExtended) {
objectString += fullObjectName + '.prototype = Object.create( ' + basePrototypeName + '.prototype );\n';
}
objectString += fullObjectName + '.prototype.constructor = ' + fullObjectName + ';\n';
objectString += '\n\n';
for (i = 0; i < objectProperties.length; i++) {
objectString += objectProperties[i];
}
objectString += '\n\n';
for (i = 0; i < objectFunctions.length; i++) {
objectString += objectFunctions[i];
}
objectString += '\n\n';
if (isExtended) {
for (i = 0; i < prototypeFunctions.length; i++) {
objectString += prototypeFunctions[i];
}
} else {
objectString += fullObjectName + '.prototype = {\n\n';
for (i = 0; i < prototypeFunctions.length; i++) {
objectString += prototypeFunctions[i];
}
objectString += '\n};';
}
objectString += '\n\n';
return objectString;
}
};
/**
* Allows to define instructions to override or remove
* @param {String} name Usually the name of a function
* @param {String} fullName The name plus full object description
* @constructor
*/
var CodeSerializationInstruction = function CodeSerializationInstruction(name, fullName) {
this.name = name;
this.fullName = fullName;
this.code = null;
this.removeCode = false;
};
CodeSerializationInstruction.prototype = {
constructor: CodeSerializationInstruction,
/**
* Returns the name of the function
* @return {String}
*/
getName: function getName() {
return this.name;
},
/**
* Returns the full name of the function
* @return {String}
*/
getFullName: function getFullName() {
return this.fullName;
},
/**
* Set the string containing the serialized function
* @param {String} code
* @return {CodeSerializationInstruction}
*/
setCode: function setCode(code) {
this.code = code;
return this;
},
/**
* Returns the serialized function code
* @return {String}
*/
getCode: function getCode() {
return this.code;
},
/**
* Set if function should be removed
* @param {boolean} removeCode
* @return {CodeSerializationInstruction}
*/
setRemoveCode: function setRemoveCode(removeCode) {
this.removeCode = removeCode;
return this;
},
/**
* If function should be completely removed
* @return {boolean}
*/
isRemoveCode: function isRemoveCode() {
return this.removeCode;
}
};
export { CodeSerializer, CodeSerializationInstruction };