number.js
3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/**
* Sample plugin.
*/
Draw.loadPlugin(function(ui) {
// Adds numbered toggle property
Editor.commonVertexProperties.push({name: 'numbered', dispName: 'Numbered', type: 'bool', defVal: true, isVisible: function(state, format)
{
var graph = format.editorUi.editor.graph;
return graph.view.redrawNumberShape != null;
}, onChange: function(graph, newValue)
{
graph.refresh();
}});
var graph = ui.editor.graph;
var enabled = true;
var counter = 0;
var graphViewResetValidationState = graph.view.resetValidationState;
graph.view.resetValidationState = function()
{
graphViewResetValidationState.apply(this, arguments);
this.numberCounter = 0;
};
var graphViewValidateCellState = graph.view.validateCellState;
graph.view.validateCellState = function(cell, recurse)
{
var state = graphViewValidateCellState.apply(this, arguments);
recurse = (recurse != null) ? recurse : true;
if (recurse && state != null && graph.model.isVertex(state.cell) &&
mxUtils.getValue(state.style, 'numbered', 1) == 1)
{
this.numberCounter++;
this.redrawNumberShape(state);
}
return state;
};
graph.view.redrawNumberShape = function(state)
{
var numbered = mxUtils.getValue(state.style, 'numbered', 1) == 1;
var value = '<div style="padding:2px;border:1px solid gray;background:yellow;border-radius:2px;">' +
(this.numberCounter) + '</div>';
if (enabled && numbered && graph.model.isVertex(state.cell) &&
state.shape != null && state.secondLabel == null)
{
state.secondLabel = new mxText(value, new mxRectangle(),
mxConstants.ALIGN_LEFT, mxConstants.ALIGN_BOTTOM);
// Styles the label
state.secondLabel.size = 12;
state.secondLabel.dialect = mxConstants.DIALECT_STRICTHTML;
graph.cellRenderer.initializeLabel(state, state.secondLabel);
}
if (state.secondLabel != null)
{
if (!numbered)
{
state.secondLabel.destroy();
state.secondLabel = null;
}
else
{
var scale = graph.getView().getScale();
var bounds = new mxRectangle(state.x + state.width - 4 * scale, state.y + 4 * scale, 0, 0);
state.secondLabel.value = value;
state.secondLabel.state = state;
state.secondLabel.scale = scale;
state.secondLabel.bounds = bounds;
state.secondLabel.redraw();
}
}
};
// Destroys the shape number
var destroy = graph.cellRenderer.destroy;
graph.cellRenderer.destroy = function(state)
{
destroy.apply(this, arguments);
if (state.secondLabel != null)
{
state.secondLabel.destroy();
state.secondLabel = null;
}
};
graph.cellRenderer.getShapesForState = function(state)
{
return [state.shape, state.text, state.secondLabel, state.control];
};
// Extends View menu
mxResources.parse('number=Number');
// Adds action
var action = ui.actions.addAction('number...', function()
{
enabled = !enabled;
graph.refresh();
});
action.setToggleAction(true);
action.setSelectedCallback(function() { return enabled; });
var menu = ui.menus.get((urlParams['sketch'] == '1') ? 'extras' : 'view');
var oldFunct = menu.funct;
menu.funct = function(menu, parent)
{
oldFunct.apply(this, arguments);
ui.menus.addMenuItems(menu, ['-', 'number'], parent);
};
// Forces refresh if file was loaded before plugin
if (ui.getCurrentFile() != null)
{
graph.refresh();
}
});