This tutorial builds upon the OrgChartNode type from Tutorial 3. It shows how to fully integrate the custom type with other MindFusion.Diagramming for JavaScript features, such as clipboard support, undo support, creating nodes by drag-and-drop or drawing with the mouse on the canvas. The code is also available in Samples folder of JsDiagram distribution.
1. Create copies of Tutorial 3 files - Tutorail3.html and Tutorial3.js and name them Tuturial4.html and Tutorial4.js respectively.
2. Add a DIV and CANVAS elements to Tutorial4.html that will represent a NodeListView control:
HTML Copy Code |
---|
<!-- The NodeList component is bound to the canvas element below --> |
3. Add five buttons to the page - copy, cut, paste, undo and redo - and define click handlers that will be used to call the corresponding diagram methods.
HTML Copy Code |
---|
<button onclick="diagramView.copyToClipboard()">copy</button> |
4. Add shortcuts to the NodeListView and Size classes in Tutorial4.js and define a nodeListView variable.
JavaScript Copy Code |
---|
var NodeListView = MindFusion.Diagramming.NodeListView; |
5. Classes generated by calling classFromTemplate implement undo/redo, clipboard and serialization support for their auto-properties out of the box. When creating more complex types with their own data properties, you'd need to implement serialization and cloning for them yourself. Lets show how to implement the various operations by creating a new class that adds a custom property. Start by renaming the class from tutorial 3 to TemplatedBase, and creating a new OrgChartNode class that derives from it.
JavaScript Copy Code |
---|
var TemplatedBase = CompositeNode.classFromTemplate("TemplatedBase", |
6. Add a clone method override to the OrgChartNode prototype.
JavaScript Copy Code |
---|
// support for the NodeListView drag and drop |
7. To implement clipboard support of any new properties added by a custom item class, add fromJson and toJson method overrides to the prototype definition:
JavaScript Copy Code |
---|
// clipboard and serialization support OrgChartNode.prototype.fromJson = function (json) { |
8. In order to enable undo / redo of changes done on OrgChartNode, add saveState and restoreState method overrides to the prototype definition:
JavaScript Copy Code |
---|
// undo/redo OrgChartNode.prototype.restoreState = function (state) { |
9. In order to enable drawing OrgChartNodes with the mouse, add the following code to the DOMContentLoaded handler:
JavaScript Copy Code |
---|
// enable drawing of custom nodes interactively |
10. To enable undo / redo support on the Diagram object, set its undoEnabled property to true in the DOMContentLoaded handler.
JavaScript Copy Code |
---|
// enable undo/redo support diagram.undoEnabled = true; |
11. Finally, add initialization code for the NodeListView to the load hadler.
JavaScript Copy Code |
---|
// create a NodeListView component that wraps the "nodeListView" canvas |
12. Publish MindFusion.*.js and tutorial files using web server of your choice. Open Tutorial4.html in a web browser and you should be able to create nodes by drag and drop from NodeListView, copy and paste them, and redo/undo operations done on them.