Commit 1da2f6eb authored by Akikonata's avatar Akikonata

dev

parent 7d2f62b9
KityMinder.registerModule( "image", function () {
function loadImageSize( url, callback ) {
var img = document.createElement( 'img' );
KityMinder.registerModule("image", function () {
function loadImageSize(url, callback) {
var img = document.createElement('img');
img.onload = function () {
callback( img.width, img.height );
callback(img.width, img.height);
};
img.onerror = function () {
callback( null );
callback(null);
};
img.src = url;
}
function fitImageSize( width, height, maxWidth, maxHeight ) {
function fitImageSize(width, height, maxWidth, maxHeight) {
var ratio = width / height,
fitRatio = maxWidth / maxHeight;
// 宽高比大于最大尺寸的宽高比,以宽度为标准适应
if ( ratio > fitRatio && width > maxWidth ) {
if (ratio > fitRatio && width > maxWidth) {
width = maxWidth;
height = maxWidth / ratio;
} else if(height > maxHeight) {
} else if (height > maxHeight) {
height = maxHeight;
width = maxHeight / ratio;
}
......@@ -34,98 +34,98 @@ KityMinder.registerModule( "image", function () {
"maxImageHeight": 200
},
"commands": {
"image": kity.createClass( "ImageCommand", {
"image": kity.createClass("ImageCommand", {
base: Command,
execute: function ( km, url ) {
execute: function (km, url) {
var nodes = km.getSelectedNodes();
loadImageSize( url, function ( width, height ) {
if ( !width ) return;
utils.each( nodes, function ( i, n ) {
n.setData( 'image', url );
n.setData( 'imageWidth', width );
n.setData( 'imageHeight', height );
km.updateLayout( n );
} );
} );
loadImageSize(url, function (width, height) {
if (!width) return;
utils.each(nodes, function (i, n) {
n.setData('image', url);
n.setData('imageWidth', width);
n.setData('imageHeight', height);
km.updateLayout(n);
});
});
},
queryState: function ( km ) {
queryState: function (km) {
var nodes = km.getSelectedNodes(),
result = 0;
if ( nodes.length === 0 ) {
if (nodes.length === 0) {
return -1;
}
utils.each( nodes, function ( i, n ) {
if ( n && n.getData( 'image' ) ) {
utils.each(nodes, function (i, n) {
if (n && n.getData('image')) {
result = 0;
return false;
}
} );
});
return result;
},
queryValue: function ( km ) {
queryValue: function (km) {
var node = km.getSelectedNode();
return node.getData( 'image' );
return node.getData('image');
}
} ),
"removeimage": kity.createClass( "RemoveImageCommand", {
}),
"removeimage": kity.createClass("RemoveImageCommand", {
base: Command,
execute: function ( km ) {
execute: function (km) {
var nodes = km.getSelectedNodes();
utils.each( nodes, function ( i, n ) {
n.setData( 'image' );
km.updateLayout( n );
} );
utils.each(nodes, function (i, n) {
n.setData('image');
km.updateLayout(n);
});
},
queryState: function ( km ) {
queryState: function (km) {
var nodes = km.getSelectedNodes();
if ( nodes.length == 0 ) {
if (nodes.length == 0) {
return -1;
}
var image = false;
utils.each( nodes, function ( i, n ) {
if ( n.getData( 'image' ) ) {
utils.each(nodes, function (i, n) {
if (n.getData('image')) {
image = true;
return false;
}
} );
if ( image ) {
});
if (image) {
return 0;
}
return -1;
}
} )
})
},
"events": {
"RenderNodeTop": function ( e ) {
"RenderNodeTop": function (e) {
var node = e.node,
url = node.getData( 'image' );
url = node.getData('image');
var link, img, size, currentBox;
if ( url ) {
if (url) {
size = fitImageSize(
node.getData( 'imageWidth' ),
node.getData( 'imageHeight' ),
this.getOptions( 'maxImageWidth' ),
this.getOptions( 'maxImageHeight' ) );
node.getData('imageWidth'),
node.getData('imageHeight'),
this.getOptions('maxImageWidth'),
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.addShape( img );
link.setTarget( '_blank' );
link.setStyle( 'cursor', 'pointer' );
link = new kity.HyperLink(url);
link.addShape(img);
link.setTarget('_blank');
link.setStyle('cursor', 'pointer');
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