image.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. define(function(require, exports, module) {
  2. var kity = require('../core/kity');
  3. var utils = require('../core/utils');
  4. var Minder = require('../core/minder');
  5. var MinderNode = require('../core/node');
  6. var Command = require('../core/command');
  7. var Module = require('../core/module');
  8. var Renderer = require('../core/render');
  9. Module.register('image', function() {
  10. function loadImageSize(url, callback) {
  11. var img = document.createElement('img');
  12. img.onload = function() {
  13. callback(img.width, img.height);
  14. };
  15. img.onerror = function() {
  16. callback(null);
  17. };
  18. img.src = url;
  19. }
  20. function fitImageSize(width, height, maxWidth, maxHeight) {
  21. var ratio = width / height,
  22. fitRatio = maxWidth / maxHeight;
  23. // 宽高比大于最大尺寸的宽高比,以宽度为标准适应
  24. if (width > maxWidth && ratio > fitRatio) {
  25. width = maxWidth;
  26. height = width / ratio;
  27. } else if (height > maxHeight) {
  28. height = maxHeight;
  29. width = height * ratio;
  30. }
  31. return {
  32. width: width | 0,
  33. height: height | 0
  34. };
  35. }
  36. /**
  37. * @command Image
  38. * @description 为选中的节点添加图片
  39. * @param {string} url 图片的 URL,设置为 null 移除
  40. * @param {string} title 图片的说明
  41. * @state
  42. * 0: 当前有选中的节点
  43. * -1: 当前没有选中的节点
  44. * @return 返回首个选中节点的图片信息,JSON 对象: `{url: url, title: title}`
  45. */
  46. var ImageCommand = kity.createClass('ImageCommand', {
  47. base: Command,
  48. execute: function(km, url, title) {
  49. var nodes = km.getSelectedNodes();
  50. loadImageSize(url, function(width, height) {
  51. nodes.forEach(function(n) {
  52. var size = fitImageSize(
  53. width, height,
  54. km.getOption('maxImageWidth'),
  55. km.getOption('maxImageHeight'));
  56. n.setData('image', url);
  57. n.setData('imageTitle', url && title);
  58. n.setData('imageSize', url && size);
  59. n.render();
  60. });
  61. km.fire('saveScene');
  62. km.layout(300);
  63. });
  64. },
  65. queryState: function(km) {
  66. var nodes = km.getSelectedNodes(),
  67. result = 0;
  68. if (nodes.length === 0) {
  69. return -1;
  70. }
  71. nodes.forEach(function(n) {
  72. if (n && n.getData('image')) {
  73. result = 0;
  74. return false;
  75. }
  76. });
  77. return result;
  78. },
  79. queryValue: function(km) {
  80. var node = km.getSelectedNode();
  81. return {
  82. url: node.getData('image'),
  83. title: node.getData('imageTitle')
  84. };
  85. }
  86. });
  87. var ImageRenderer = kity.createClass('ImageRenderer', {
  88. base: Renderer,
  89. create: function(node) {
  90. return new kity.Image(node.getData('image'));
  91. },
  92. shouldRender: function(node) {
  93. return node.getData('image');
  94. },
  95. update: function(image, node, box) {
  96. var url = node.getData('image');
  97. var title = node.getData('imageTitle');
  98. var size = node.getData('imageSize');
  99. var spaceTop = node.getStyle('space-top');
  100. if (!size) return;
  101. if (title) {
  102. image.node.setAttributeNS('http://www.w3.org/1999/xlink', 'title', title);
  103. }
  104. var x = box.cx - size.width / 2;
  105. var y = box.y - size.height - spaceTop;
  106. image
  107. .setUrl(url)
  108. .setX(x | 0)
  109. .setY(y | 0)
  110. .setWidth(size.width | 0)
  111. .setHeight(size.height | 0);
  112. return new kity.Box(x | 0, y | 0, size.width | 0, size.height | 0);
  113. }
  114. });
  115. return {
  116. 'defaultOptions': {
  117. 'maxImageWidth': 200,
  118. 'maxImageHeight': 200
  119. },
  120. 'commands': {
  121. 'image': ImageCommand
  122. },
  123. 'renderers': {
  124. 'top': ImageRenderer
  125. }
  126. };
  127. });
  128. });