Minder.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. //展现脑图,数据样式如下
  2. //var data = {
  3. // "root": {
  4. // "data": {"id": "9f92035021ac", "text": "软装修"},
  5. // "children": [{
  6. // "data": {"id": "b45yogtullsg", "created": 1463069010918, "text": "包阳台"},
  7. // "children": [{
  8. // "data": {"id": "3jl3i3j43", "created": 1463069010923, "text": "凤铝"}
  9. // }, {
  10. // "data": {"id": "3jl3i3j44", "created": 1463069010923, "text": "断桥"}
  11. // }]
  12. // },
  13. // {
  14. // "data": {"id": "b45yohdlynco", "created": 1463069012113, "text": "衣柜"}, "children": [
  15. // {"data": {"id": "b45yohdlynco", "created": 1463069012113, "text": "主卧"}},
  16. // {"data": {"id": "b45yohdlynco", "created": 1463069012113, "text": "次卧"}}
  17. // ]
  18. // },
  19. // {"data": {"id": "b45yohdlynco", "created": 1463069012113, "text": "床"}, "children": []},
  20. // {"data": {"id": "b45yohdlynco", "created": 1463069012113, "text": "餐桌"}, "children": []},
  21. // {"data": {"id": "b45yohdlynco", "created": 1463069012113, "text": "灯具"}, "children": []},
  22. // {"data": {"id": "b45yohdlynco", "created": 1463069012113, "text": "窗帘"}, "children": []}
  23. // ]
  24. // }
  25. //};
  26. MWF.xDesktop.requireApp("MinderEditor", "LeftToolbar", null, false);
  27. MWF.xApplication.Template = MWF.xApplication.Template || {};
  28. MWF.xApplication.Template.Minder = new Class({
  29. Extends: MWF.widget.Common,
  30. Implements: [Options, Events],
  31. options: {
  32. "style": "default",
  33. "template" : "default", //fish-bone
  34. "theme": "fresh-blue", //"fresh-blue-compat"
  35. "hasNavi" : true,
  36. "align" : "center"
  37. },
  38. initialize: function (container, app, data, options) {
  39. this.setOptions(options);
  40. this.content = app.content;
  41. this.container = this.contentNode = container;
  42. this.container.classList.add("km-editor");
  43. this.app = app;
  44. this.actions = this.app.restActions;
  45. this.data = data;
  46. },
  47. load: function () {
  48. this.loadResource( function(){
  49. this.loadKityMinder( this.data );
  50. }.bind(this) );
  51. //this.attachEvent();
  52. },
  53. reload : function( data ){
  54. this.container.empty();
  55. this.loadKityMinder( data || this.data )
  56. },
  57. refresh : function(){
  58. //this.alreadyBind = false;
  59. //this.km.execCommand('camera');
  60. this.moveToCenter();
  61. },
  62. destroy: function(){
  63. if(this.navi){
  64. this.navi.destroy();
  65. delete this.navi;
  66. }
  67. if(this.km)delete this.km;
  68. //delete this;
  69. },
  70. loadResource: function (callback) {
  71. var kityminderPath = "/x_desktop/res/framework/kityminder/";
  72. COMMON.AjaxModule.loadCss(kityminderPath + "core/src/kityminder.css", function () {
  73. COMMON.AjaxModule.load("kity", function () {
  74. COMMON.AjaxModule.load("kityminder", function () {
  75. if (callback)callback();
  76. }.bind(this));
  77. }.bind(this))
  78. }.bind(this))
  79. },
  80. loadKityMinder: function ( data ) {
  81. var _self = this;
  82. this.isMovingCenter = true;
  83. // 创建 km 实例
  84. /* global kityminder */
  85. var km = this.km = this.minder = new kityminder.Minder();
  86. //var target = document.querySelector('#minder-view');
  87. km.renderTo(this.container);
  88. data.theme = data.theme || this.options.theme;
  89. data.template = data.template || this.options.template;
  90. this.deepestLevel = 0;
  91. km.on("import", function (e) {
  92. if ( !_self.alreadyBind ) {
  93. var nodes = km.getAllNode();
  94. nodes.forEach(function (node) {
  95. var level = node.getLevel();
  96. _self.deepestLevel = level > _self.deepestLevel ? level : _self.deepestLevel;
  97. _self.fireEvent( "postLoadNode", node );
  98. });
  99. _self.alreadyBind = true;
  100. if( _self.options.hasNavi )_self.loadNavi();
  101. _self.fireEvent( "postLoad" , _self );
  102. }
  103. });
  104. //km.on("execCommand", function(e){
  105. // if (e.commandName === "template" ) {
  106. // _self.moveToCenter();
  107. // }
  108. //})
  109. km.on("layoutallfinish",function(){
  110. if( _self.templateChanged || _self.isMovingCenter ){
  111. _self.moveToCenter();
  112. _self.templateChanged = false;
  113. _self.isMovingCenter = false;
  114. }
  115. });
  116. km.importJson(data);
  117. km.execCommand('hand');
  118. },
  119. loadNavi : function( container ) {
  120. this.navi = new MWF.xApplication.MinderEditor.LeftToolbar(container || this.container, this, this.km, this.app );
  121. this.navi.load();
  122. },
  123. moveToCenter: function(){
  124. //setTimeout( this._moveToCenter.bind(this) , 100 );
  125. this._moveToCenter();
  126. },
  127. _moveToCenter: function(){
  128. if( this.options.align != "center")return;
  129. //图形居中
  130. var minderView = this.km.getRenderContainer().getRenderBox('screen'); //.getBoundaryBox();
  131. var containerView = this.container.getCoordinates();
  132. var root = this.km.getRoot();
  133. var rootView = root.getRenderContainer().getRenderBox('screen'); //getRenderBox('top');
  134. var rootClientTop = rootView.top - minderView.top;
  135. var rootClientLeft = rootView.left - minderView.left;
  136. var rootChildrenLength = root.getChildren().length;
  137. var template = this.km.queryCommandValue("template");
  138. var left, top, isCamera = false;
  139. if( minderView.width > containerView.width ){ //如果图形宽度大于容器宽度
  140. if( template == "fish-bone" || rootChildrenLength < 2 ){
  141. left = 50;
  142. }else{
  143. isCamera = true;
  144. }
  145. }else{
  146. left = parseInt( ( containerView.width - minderView.width ) / 2 + rootClientLeft + 50 );
  147. }
  148. if( minderView.height > containerView.height ){ //如果图形高度大于容器高度
  149. if( rootClientTop > containerView.height ){
  150. if( template == "fish-bone" ) {
  151. top = containerView.height - rootView.height
  152. }else if( rootChildrenLength < 2){
  153. top = parseInt( containerView.width / 2 );
  154. }else{
  155. isCamera = true;
  156. }
  157. }else{
  158. top = rootClientTop + 50;
  159. }
  160. }else{
  161. top = parseInt( ( containerView.height - minderView.height ) / 2 ) + rootClientTop;
  162. }
  163. if( isCamera ){
  164. this.km.execCommand('camera', this.km.getRoot(), 600);
  165. }else{
  166. var dragger = this.km.getViewDragger();
  167. dragger.moveTo(new kity.Point(left, top) , 300 );
  168. }
  169. }
  170. //attachEvent: function(){
  171. // this.container.addEvent("mousewheel", function(ev){ //鼠标滚轮事件
  172. // if( ev.wheel == 1){ //向上滚动,放大
  173. // this.km.execCommand('zoomIn');
  174. // if( this.navi && this.navi.navZoompanIndicator ){
  175. // var marginTop = parseInt( this.navi.navZoompanIndicator.getStyle("margin-top"));
  176. // if( marginTop > 0 ){
  177. // this.navi.navZoompanIndicator.setStyle( "margin-top", (marginTop - 10) + "px" );
  178. // }
  179. // }
  180. // }else{ //向下滚动,缩小
  181. // this.km.execCommand('zoomOut')
  182. // if( this.navi && this.navi.navZoompanIndicator ){
  183. // var totalHeight = parseInt( this.navi.navZoompan.getStyle("height") );
  184. // var marginTop = parseInt( this.navi.navZoompanIndicator.getStyle("margin-top"));
  185. // if( marginTop < totalHeight ){
  186. // this.navi.navZoompanIndicator.setStyle( "margin-top", (marginTop + 10 ) + "px" );
  187. // }
  188. // }
  189. // }
  190. // }.bind(this))
  191. //}
  192. });