forked from fex-team/kityminder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage.js
158 lines (138 loc) · 4.46 KB
/
image.js
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
KityMinder.registerModule('image', function() {
function loadImageSize(url, callback) {
var img = document.createElement('img');
img.onload = function() {
callback(img.width, img.height);
};
img.onerror = function() {
callback(null);
};
img.src = url;
}
function fitImageSize(width, height, maxWidth, maxHeight) {
var ratio = width / height,
fitRatio = maxWidth / maxHeight;
// 宽高比大于最大尺寸的宽高比,以宽度为标准适应
if (width > maxWidth && ratio > fitRatio) {
width = maxWidth;
height = width / ratio;
} else if (height > maxHeight) {
height = maxHeight;
width = height * ratio;
}
return {
width: width | 0,
height: height | 0
};
}
var ImageCommand = kity.createClass('ImageCommand', {
base: Command,
execute: function(km, url, title) {
var nodes = km.getSelectedNodes();
loadImageSize(url, function(width, height) {
if (!width) return;
utils.each(nodes, function(i, n) {
var size = fitImageSize(
width, height,
km.getOptions('maxImageWidth'),
km.getOptions('maxImageHeight'));
n.setData('image', url);
n.setData('imageTitle', title);
n.setData('imageSize', size);
n.render();
});
km.fire("saveScene");
km.layout(300);
});
},
queryState: function(km) {
var nodes = km.getSelectedNodes(),
result = 0;
if (nodes.length === 0) {
return -1;
}
utils.each(nodes, function(i, n) {
if (n && n.getData('image')) {
result = 0;
return false;
}
});
return result;
},
queryValue: function(km) {
var node = km.getSelectedNode();
return {
url: node.getData('image'),
title: node.getData('imageTitle')
};
}
});
var RemoveImageCommand = kity.createClass('RemoveImageCommand', {
base: Command,
execute: function(km) {
var nodes = km.getSelectedNodes();
utils.each(nodes, function(i, n) {
n.setData('image').render();
});
km.layout(300);
},
queryState: function(km) {
var nodes = km.getSelectedNodes();
if (nodes.length === 0) {
return -1;
}
var image = false;
utils.each(nodes, function(i, n) {
if (n.getData('image')) {
image = true;
return false;
}
});
if (image) {
return 0;
}
return -1;
}
});
var ImageRenderer = kity.createClass('ImageRenderer', {
base: KityMinder.Renderer,
create: function(node) {
return new kity.Image(node.getData('image'));
},
shouldRender: function(node) {
return node.getData('image');
},
update: function(image, node, box) {
var url = node.getData('image');
var title = node.getData('imageTitle');
var size = node.getData('imageSize');
var spaceTop = node.getStyle('space-top');
if (!size) return;
if (title) {
image.node.setAttributeNS('http://www.w3.org/1999/xlink', 'title', title);
}
var x = box.cx - size.width / 2;
var y = box.y - size.height - spaceTop;
image
.setUrl(url)
.setX(x | 0)
.setY(y | 0)
.setWidth(size.width | 0)
.setHeight(size.height | 0);
return new kity.Box(x | 0, y | 0, size.width | 0, size.height | 0);
}
});
return {
'defaultOptions': {
'maxImageWidth': 200,
'maxImageHeight': 200
},
'commands': {
'image': ImageCommand,
'removeimage': RemoveImageCommand
},
'renderers': {
'top': ImageRenderer
}
};
});