Writing in the front
In this paper, an online network topology design and editing based on Jtopo is implemented, which can create complex networks and carry out various operations on networks and devices, providing serialization and anti-sequence operations of topology.
For demonstration purposes, I have deployed a static DEMO to Github, portal
With regard to project access, there are security limitations in reading remote JSON data and opening it directly in a browser. Please put the project into Tomcat for startup access or directly open the project with IDEA /webstorm by right clicking, as shown in the picture below:
The full code is posted separately on Github: github.com/gongxufan/w…
Function introduction
The basic operation can be seen in the GIF below, where a node is dragged from the left icon bar to the edit area. You can drag a line by clicking on the node icon and then releasing the mouse button. Click on the destination node to complete a wire operation. The left column allows you to select a variety of connections.
The layout of the topology editor UI is done by easyUI, and the node editing and wiring is developed based on jTopo. JTopo provides support for stages, scenes, Nodes, containers, and animations. The API is relatively simple to use. The disadvantage is the lack of documentation, but we can get familiar with the DEMO provided by the author. Once familiar with its source code, it is very convenient for secondary development. It’s also easy to use for some animations. Since it’s a Canvas based painting, every change and operation actually redraws the entire canvas. There are still a lot of areas that can be optimized. In fact, d3.js can also go to such an effect, but the recent work is not to explore the back end. Since it is only the front-end part of the personal development project, only this part can be developed due to the company’s permission. If you are interested, you can clone it on my Github for reference. The core part of the code is in editor.js, which provides support for node drag, node wiring, layout and other methods. This is an initial version. Code rough, for reference only. Here are a few important points to explain:
Editor initializes the code
|
|
We can call jtopo.stage (Canvas) to initialize a drawing area, and then we can use the API to manipulate nodes and animations. The JSON hierarchy of the entire drawing object is as follows:
|
|
Its structure is:
Usually we only need one Scene object to manage all objects, but if we want to achieve more complex grouped object management, we can create multiple Scenes objects to manage separately. At the same time, we can call JTopo. CreateStageFromJson (stageJson, canvas) method to render a save good topology.
Node drag and drop
Node drag-and-drop is implemented using the native H5 drag&drop
|
|
The new Node is constructed using jtopo.node (), set its properties, and is added to the Stage via scene.add(Node). Why is the new node visible on the screen when you perform the add operation?
The reason is that the Stage has a frames attribute, which defines the canvas redraw frequency of 1000/frames
Frames properties
Sets the number of frames per second that the current stage plays
The default value is: 24
Frames can be 0, which means that the frames are not drawn automatically, but are triggered manually by the user by calling the paint() method of the Stage object.
If the value is less than 0, the frames will be redrawn only when the keyboard and mouse are active, for example, stage.frames = -24.
The default screen number is 24 frames, which means the screen will be redrawn every 1000/24ms. The background refresh code is as follows:
|
|
SetTimeout calls the following redraw function,
|
|
Paint Calls the repaint method in turn to iterate over all visible objects.
The node connection
The connection method used here is to press the left button of the mouse on the node, and then release the mouse to create a connection. The starting point is the node to be clicked, and the end point will be dynamically updated as the mouse moves. Therefore, if a single node releases the mouse, a line moving with the mouse can be seen. Then click the left button on a node to release how to complete the connection between the two nodes. The effect is as follows:
Part of the code is as follows:
JTopo supports common branches, polylines, curves, and so on, but the corner lengths of polylines can now only be specified at creation time. Second development is required for dynamic second point creation. The code is as follows:
|
|
Save and load topologies
Because this article is only the front end of the topology editor, the back end part is not open source for commercial reasons. And because the overall project is so large, it will not be easy to separate it. Serialization of topologies is easy as long as we know the load and save logic of topologies. The following takes up a space to focus on the discussion:
Load an existing topology view
We now have a topology, the topology shown by the text portal address, with the following serialization structure:
|
|
We just call on the page
Editor. LoadTopology (” images/backimg. PNG “, “${templateId} ‘, ‘${topologyId}’,” “);
TemplateIdtemplateId and topologyId are the primary key ids involved in the back-end table, which you can ignore here. The loadTopology method code is as follows:
|
|
The logic for topology loading is really implemented in the editor.init method by first sending the request for the JSON data and then processing the response structure. If the requested topology does not exist, create a blank topology. Here is the implementation logic for init:
|
|
The seemingly complex code here does two things: 1) build the basic stage and scene 2) call JTopo. CreateStageFromJson (stageJson, Canvas) can create the structure of the entire topology 3) initialize and edit its menu 4) finally bind the processing logic of various events 5) Finally create a blank topology if errors occur when obtaining topology data
|
|
This completes the task of loading the topology and creating a blank topology
Save the topology view
Saving topologies means serializing the edited topologies into JSON and uploading them to the server to save the database. In the simplest way, we need to follow Node,Scene,Stage. So we define the following entity objects:
Node
|
|
Scene
|
|
Stage
|
|
Having defined these three structures, we have a contrast at the front and back ends. And then how does the front end serialize? Let’s see how the front end serializes the topology:
|
|
- To delete the ruler line (edit the horizontal and vertical lines displayed in the area), this object needs to be removed from our structure by calling the clearRuleLines method.
- The node objects of the containers area need to be extracted, because these nodes are already included when the call is serialized, so only their node IDS need to be associated with the container when it is loaded into the library
- If hierarchical editing is supported, the number of layers in the current topology is also saved
- A call to editor.stage.tojson () gets the structure of the entire topology
- Json substitution, just to reduce data transfer
- Finally, the parsed JSON is sent to the background processing
Other details will not be described here. This project requires readers to have basic knowledge of H5, easyUI, jTopo, Canvas, JSON and so on. As for jTopo, just look at the DEMO and few apis provided by the author and you can get started very quickly. The best way to learn is to break the debug trace at different points and see how the whole thing works. The topology editing shown here is also a very simple and incomplete example, but there are many things that can be customized, such as the wiring and the way the wiring is done, which can be further customized.
end
Resources: www.jtopo.com/
www.jeasyui.com/documentati…
www.w3school.com.cn/html5/