-
Notifications
You must be signed in to change notification settings - Fork 93
[refactor] Improve code structure #347 #348
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
fe99e6e
5df12de
1003bee
9556a4e
892fe05
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,147 +7,159 @@ const colorTool = require("zrender/lib/tool/color"); | |
const {each} = require("zrender/lib/core/util"); | ||
const env = require("zrender/lib/core/env"); | ||
|
||
/** | ||
* @class | ||
* Class NetJSONGraph is entry point for NetJSONGraph library. | ||
* Used as a global object in all examples located in `/public/examples_templates/`. | ||
*/ | ||
class NetJSONGraph { | ||
/** | ||
* @constructor | ||
* Initializes a new NetJSONGraph instance. | ||
* | ||
* @param {string} JSONParam The NetJSON file param | ||
* @param {Object} config | ||
* @param {string} JSONParam - The NetJSON file parameter. | ||
* @param {Object} [config={}] - Configuration options for the graph. | ||
*/ | ||
constructor(JSONParam, config = {}) { | ||
this.graph = new NetJSONGraphCore(JSONParam); | ||
this.config = this.initializeConfig(config); | ||
this.graph.setConfig(this.config); | ||
this.setupGraph(); | ||
this.config.onInit.call(this.graph); | ||
this.initializeECharts(); | ||
// eslint-disable-next-line no-constructor-return | ||
return this.graph; | ||
} | ||
|
||
constructor(JSONParam, config) { | ||
if (config && config.render === "map") { | ||
config.render = NetJSONGraphRender.prototype.mapRender; | ||
} else if (!config || !config.render || config.render === "graph") { | ||
config = config || {}; | ||
config.render = NetJSONGraphRender.prototype.graphRender; | ||
} | ||
/** | ||
* Initializes the configuration with values and set render to map or graph. | ||
* | ||
* @param {Object} config - The user-defined configuration. | ||
* @returns {Object} - The final configuration object. | ||
*/ | ||
initializeConfig(config) { | ||
return { | ||
...config, | ||
render: | ||
config.render === "map" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dee077 & @nemesifier
https://github.com/openwisp/netjsongraph.js?tab=readme-ov-file#configuration-instructions I think, config can be undefined and user can pass custom render function so this need to be handled. |
||
? NetJSONGraphRender.prototype.mapRender | ||
: NetJSONGraphRender.prototype.graphRender, | ||
onInit: this.onInit, | ||
onRender: this.onRender, | ||
onUpdate: this.onUpdate, | ||
afterUpdate: this.afterUpdate, | ||
onLoad: this.onLoad, | ||
setupModeSwitching: this.setupModeSwitching, | ||
}; | ||
} | ||
|
||
const graph = new NetJSONGraphCore(JSONParam); | ||
/** | ||
* Sets up rendering utilities, GUI, and event handling. Used in constructor | ||
*/ | ||
setupGraph() { | ||
Object.setPrototypeOf(NetJSONGraphRender.prototype, this.graph.utils); | ||
this.graph.gui = new NetJSONGraphGUI(this.graph); | ||
this.graph.utils = new NetJSONGraphRender(); | ||
this.graph.setUtils(); | ||
this.graph.event = this.graph.utils.createEvent(); | ||
} | ||
|
||
Object.setPrototypeOf(NetJSONGraphRender.prototype, graph.utils); | ||
graph.gui = new NetJSONGraphGUI(graph); | ||
graph.utils = new NetJSONGraphRender(); | ||
graph.setUtils(); | ||
/** | ||
* Initializes the ECharts rendering engine. Used in constructor | ||
*/ | ||
initializeECharts() { | ||
this.graph.echarts = echarts.init(this.graph.el, null, { | ||
renderer: this.graph.config.svgRender ? "svg" : "canvas", | ||
}); | ||
} | ||
|
||
graph.event = graph.utils.createEvent(); | ||
// ──────────── Lifecycle Methods ──────────── | ||
|
||
graph.setConfig({ | ||
/** | ||
* @function | ||
* @name onInit | ||
* Callback function executed on initialization | ||
* | ||
* @this {object} The instantiated object of NetJSONGraph | ||
* | ||
* @return {object} this.config | ||
*/ | ||
onInit() { | ||
return this.config; | ||
}, | ||
/** | ||
* Callback function executed during initialization. | ||
* | ||
* @this {NetJSONGraph} | ||
* @returns {Object} - The graph configuration. | ||
*/ | ||
onInit() { | ||
return this.config; | ||
} | ||
|
||
/** | ||
* @function | ||
* @name onRender | ||
* Callback function executed on render start | ||
* | ||
* @this {object} The instantiated object of NetJSONGraph | ||
* | ||
* @return {object} this.config | ||
*/ | ||
onRender() { | ||
this.utils.showLoading.call(this); | ||
this.gui.init(); | ||
return this.config; | ||
}, | ||
/** | ||
* Callback function executed when rendering starts. | ||
* | ||
* @this {NetJSONGraph} | ||
* @returns {Object} - The graph configuration. | ||
*/ | ||
onRender() { | ||
this.utils.showLoading.call(this); | ||
this.gui.init(); | ||
return this.config; | ||
} | ||
|
||
/** | ||
* @function | ||
* @name onUpdate | ||
* Callback function executed when data update. | ||
* | ||
* @this {object} The instantiated object of NetJSONGraph | ||
* | ||
* @return {object} this.config | ||
*/ | ||
onUpdate() { | ||
return this.config; | ||
}, | ||
/** | ||
* Callback function executed when data updates. | ||
* | ||
* @this {NetJSONGraph} | ||
* @returns {Object} - The graph configuration. | ||
*/ | ||
onUpdate() { | ||
return this.config; | ||
} | ||
|
||
/** | ||
* @function | ||
* @name afterUpdate | ||
* Callback function executed after data update. | ||
* | ||
* @this {object} The instantiated object of NetJSONGraph | ||
* | ||
* @return {object} this.config | ||
*/ | ||
afterUpdate() { | ||
return this.config; | ||
}, | ||
/** | ||
* Callback function executed after data updates. | ||
* | ||
* @this {NetJSONGraph} | ||
* @returns {Object} - The graph configuration. | ||
*/ | ||
afterUpdate() { | ||
return this.config; | ||
} | ||
|
||
/** | ||
* @function | ||
* @name onLoad | ||
* Callback function executed when first rendered. | ||
* | ||
* @this {object} The instantiated object of NetJSONGraph | ||
* | ||
* @return {object} this.config | ||
*/ | ||
onLoad() { | ||
if (this.config.metadata && this.type === "netjson") { | ||
this.gui.createMetaInfoContainer(graph); | ||
this.utils.updateMetadata.call(this); | ||
} else { | ||
this.gui.nodeLinkInfoContainer = | ||
this.gui.createNodeLinkInfoContainer(); | ||
} | ||
/** | ||
* Callback function executed when the graph is first rendered. | ||
* | ||
* @this {NetJSONGraph} | ||
* @returns {Object} - The graph configuration. | ||
*/ | ||
onLoad() { | ||
if (this.config.metadata && this.type === "netjson") { | ||
this.gui.createMetaInfoContainer(this.graph); | ||
this.utils.updateMetadata.call(this); | ||
} else { | ||
this.gui.nodeLinkInfoContainer = this.gui.createNodeLinkInfoContainer(); | ||
} | ||
|
||
if (this.config.switchMode && this.type === "netjson") { | ||
this.gui.renderModeSelector.onclick = () => { | ||
if (this.config.render === this.utils.mapRender) { | ||
this.config.render = this.utils.graphRender; | ||
const canvasContainer = this.echarts | ||
.getZr() | ||
.painter.getViewportRoot().parentNode; | ||
this.echarts.clear(); | ||
this.utils.graphRender(this.data, this); | ||
canvasContainer.style.background = | ||
// eslint-disable-next-line no-underscore-dangle | ||
this.echarts.getZr()._backgroundColor; | ||
document.querySelector( | ||
".leaflet-control-attribution", | ||
).style.display = "none"; | ||
document.querySelector(".leaflet-control-zoom").style.display = | ||
"none"; | ||
} else { | ||
this.echarts.clear(); | ||
this.config.render = this.utils.mapRender; | ||
this.utils.mapRender(this.data, this); | ||
document.querySelector( | ||
".leaflet-control-attribution", | ||
).style.display = "block"; | ||
document.querySelector(".leaflet-control-zoom").style.display = | ||
"block"; | ||
} | ||
}; | ||
if (this.config.switchMode && this.type === "netjson") { | ||
nemesifier marked this conversation as resolved.
Show resolved
Hide resolved
|
||
this.gui.renderModeSelector.onclick = () => { | ||
if (this.config.render === this.utils.mapRender) { | ||
this.config.render = this.utils.graphRender; | ||
const canvasContainer = this.echarts | ||
.getZr() | ||
.painter.getViewportRoot().parentNode; | ||
this.echarts.clear(); | ||
this.utils.graphRender(this.data, this); | ||
canvasContainer.style.background = | ||
// eslint-disable-next-line no-underscore-dangle | ||
this.echarts.getZr()._backgroundColor; | ||
document.querySelector(".leaflet-control-attribution").style.display = | ||
"none"; | ||
document.querySelector(".leaflet-control-zoom").style.display = | ||
"none"; | ||
} else { | ||
this.echarts.clear(); | ||
this.config.render = this.utils.mapRender; | ||
this.utils.mapRender(this.data, this); | ||
document.querySelector(".leaflet-control-attribution").style.display = | ||
"block"; | ||
document.querySelector(".leaflet-control-zoom").style.display = | ||
"block"; | ||
} | ||
|
||
this.utils.hideLoading.call(this); | ||
return this.config; | ||
}, | ||
...config, | ||
}); | ||
graph.echarts = echarts.init(graph.el, null, { | ||
renderer: graph.config.svgRender ? "svg" : "canvas", | ||
}); | ||
|
||
graph.config.onInit.call(graph); | ||
|
||
// eslint-disable-next-line no-constructor-return | ||
return graph; | ||
}; | ||
} | ||
this.utils.hideLoading.call(this); | ||
return this.config; | ||
} | ||
} | ||
|
||
|
@@ -157,6 +169,8 @@ registerLeafletSystem(echarts, L, { | |
env, | ||
}); | ||
|
||
// Expose objects globally | ||
|
||
window.NetJSONGraph = NetJSONGraph; | ||
window.echarts = echarts; | ||
window.L = L; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any programmer should know that a constructor initializes a new instance of the class, I think this comment can be removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove this. Not useful.