Commit 1da2f6eb authored by Akikonata's avatar Akikonata

dev

parent 7d2f62b9
KityMinder.registerModule( "image", function () { KityMinder.registerModule("image", function () {
function loadImageSize( url, callback ) { function loadImageSize(url, callback) {
var img = document.createElement( 'img' ); var img = document.createElement('img');
img.onload = function () { img.onload = function () {
callback( img.width, img.height ); callback(img.width, img.height);
}; };
img.onerror = function () { img.onerror = function () {
callback( null ); callback(null);
}; };
img.src = url; img.src = url;
} }
function fitImageSize( width, height, maxWidth, maxHeight ) { function fitImageSize(width, height, maxWidth, maxHeight) {
var ratio = width / height, var ratio = width / height,
fitRatio = maxWidth / maxHeight; fitRatio = maxWidth / maxHeight;
// 宽高比大于最大尺寸的宽高比,以宽度为标准适应 // 宽高比大于最大尺寸的宽高比,以宽度为标准适应
if ( ratio > fitRatio && width > maxWidth ) { if (ratio > fitRatio && width > maxWidth) {
width = maxWidth; width = maxWidth;
height = maxWidth / ratio; height = maxWidth / ratio;
} else if(height > maxHeight) { } else if (height > maxHeight) {
height = maxHeight; height = maxHeight;
width = maxHeight / ratio; width = maxHeight / ratio;
} }
...@@ -34,98 +34,98 @@ KityMinder.registerModule( "image", function () { ...@@ -34,98 +34,98 @@ KityMinder.registerModule( "image", function () {
"maxImageHeight": 200 "maxImageHeight": 200
}, },
"commands": { "commands": {
"image": kity.createClass( "ImageCommand", { "image": kity.createClass("ImageCommand", {
base: Command, base: Command,
execute: function ( km, url ) { execute: function (km, url) {
var nodes = km.getSelectedNodes(); var nodes = km.getSelectedNodes();
loadImageSize( url, function ( width, height ) { loadImageSize(url, function (width, height) {
if ( !width ) return; if (!width) return;
utils.each( nodes, function ( i, n ) { utils.each(nodes, function (i, n) {
n.setData( 'image', url ); n.setData('image', url);
n.setData( 'imageWidth', width ); n.setData('imageWidth', width);
n.setData( 'imageHeight', height ); n.setData('imageHeight', height);
km.updateLayout( n ); km.updateLayout(n);
} ); });
} ); });
}, },
queryState: function ( km ) { queryState: function (km) {
var nodes = km.getSelectedNodes(), var nodes = km.getSelectedNodes(),
result = 0; result = 0;
if ( nodes.length === 0 ) { if (nodes.length === 0) {
return -1; return -1;
} }
utils.each( nodes, function ( i, n ) { utils.each(nodes, function (i, n) {
if ( n && n.getData( 'image' ) ) { if (n && n.getData('image')) {
result = 0; result = 0;
return false; return false;
} }
} ); });
return result; return result;
}, },
queryValue: function ( km ) { queryValue: function (km) {
var node = km.getSelectedNode(); var node = km.getSelectedNode();
return node.getData( 'image' ); return node.getData('image');
} }
} ), }),
"removeimage": kity.createClass( "RemoveImageCommand", { "removeimage": kity.createClass("RemoveImageCommand", {
base: Command, base: Command,
execute: function ( km ) { execute: function (km) {
var nodes = km.getSelectedNodes(); var nodes = km.getSelectedNodes();
utils.each( nodes, function ( i, n ) { utils.each(nodes, function (i, n) {
n.setData( 'image' ); n.setData('image');
km.updateLayout( n ); km.updateLayout(n);
} ); });
}, },
queryState: function ( km ) { queryState: function (km) {
var nodes = km.getSelectedNodes(); var nodes = km.getSelectedNodes();
if ( nodes.length == 0 ) { if (nodes.length == 0) {
return -1; return -1;
} }
var image = false; var image = false;
utils.each( nodes, function ( i, n ) { utils.each(nodes, function (i, n) {
if ( n.getData( 'image' ) ) { if (n.getData('image')) {
image = true; image = true;
return false; return false;
} }
} ); });
if ( image ) { if (image) {
return 0; return 0;
} }
return -1; return -1;
} }
} ) })
}, },
"events": { "events": {
"RenderNodeTop": function ( e ) { "RenderNodeTop": function (e) {
var node = e.node, var node = e.node,
url = node.getData( 'image' ); url = node.getData('image');
var link, img, size, currentBox; var link, img, size, currentBox;
if ( url ) { if (url) {
size = fitImageSize( size = fitImageSize(
node.getData( 'imageWidth' ), node.getData('imageWidth'),
node.getData( 'imageHeight' ), node.getData('imageHeight'),
this.getOptions( 'maxImageWidth' ), this.getOptions('maxImageWidth'),
this.getOptions( 'maxImageHeight' ) ); this.getOptions('maxImageHeight'));
img = new kity.Image( url, size.width, size.height ); img = new kity.Image(url, size.width, size.height);
link = new kity.HyperLink( url ); link = new kity.HyperLink(url);
link.addShape( img ); link.addShape(img);
link.setTarget( '_blank' ); link.setTarget('_blank');
link.setStyle( 'cursor', 'pointer' ); link.setStyle('cursor', 'pointer');
currentBox = node.getContRc().getBoundaryBox(); currentBox = node.getContRc().getBoundaryBox();
node.getContRc().addShape( link.setTranslate( 0, currentBox.y - size.height ) ); node.getContRc().addShape(link.setTranslate(0, currentBox.y - size.height));
} }
} }
} }
}; };
} ); });
\ No newline at end of file \ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment