Commit 6c1715b3 authored by techird's avatar techird

solve

parents 13e44a41 5fc1721f
var KityMinderId = 0; var KityMinderId = 0;
var KityMinder = km.KityMinder = kity.createClass( "KityMinder", { var KityMinder = km.KityMinder = kity.createClass("KityMinder", {
constructor: function ( id, option ) { constructor: function(id, option) {
// 初始化 // 初始化
this._initMinder( id, option || {} ); this._initMinder(id, option || {});
this._initEvents(); this._initEvents();
this._initModules(); this._initModules();
}, },
_initMinder: function ( id, option ) { _initMinder: function(id, option) {
this.id = id || KityMinderId++; this.id = id || KityMinderId++;
this._rc = new kity.Group(); this._rc = new kity.Group();
this._paper = new kity.Paper( option.renderTo || document.body ); this._paper = new kity.Paper(option.renderTo || document.body);
this._paper.addShape( this._rc ); this._paper.addShape(this._rc);
this._root = new MinderNode( this ); this._root = new MinderNode(this);
this._rc.addShape( this._root.getRenderContainer() ); this._rc.addShape(this._root.getRenderContainer());
} }
} ); });
//模块注册&暴露模块接口 //模块注册&暴露模块接口
( function () { (function() {
var _modules; var _modules;
KityMinder.registerModule = function ( name, module ) { KityMinder.registerModule = function(name, module) {
//初始化模块列表 //初始化模块列表
if ( !_modules ) { if (!_modules) {
_modules = {}; _modules = {};
} }
_modules[ name ] = module; _modules[name] = module;
}; };
KityMinder.getModules = function () { KityMinder.getModules = function() {
return _modules; return _modules;
}; };
} )(); })();
// 模块维护 // 模块维护
kity.extendClass( KityMinder, ( function () { kity.extendClass(KityMinder, (function() {
var _commands = {}; //command池子 var _commands = {}; //command池子
var _query = {}; //query池子 var _query = {}; //query池子
return { return {
_initModules: function () { _initModules: function() {
var _modules = KityMinder.getModules(); var _modules = KityMinder.getModules();
if ( _modules ) { if (_modules) {
var me = this; var me = this;
for ( var key in _modules ) { for (var key in _modules) {
//执行模块初始化,抛出后续处理对象 //执行模块初始化,抛出后续处理对象
var moduleDeals = _modules[ key ].call( me ); var moduleDeals = _modules[key].call(me);
if ( moduleDeals.ready ) { if (moduleDeals.ready) {
moduleDeals.ready.call( me ); moduleDeals.ready.call(me);
} }
//command加入命令池子 //command加入命令池子
var moduleDealsCommands = moduleDeals.commands; var moduleDealsCommands = moduleDeals.commands;
if ( moduleDealsCommands ) { if (moduleDealsCommands) {
for ( var _keyC in moduleDealsCommands ) { for (var _keyC in moduleDealsCommands) {
_commands[ _keyC ] = moduleDealsCommands[ _keyC ]; _commands[_keyC] = moduleDealsCommands[_keyC];
} }
} }
//绑定事件 //绑定事件
var moduleDealsEvents = moduleDeals.events; var moduleDealsEvents = moduleDeals.events;
if ( moduleDealsEvents ) { if (moduleDealsEvents) {
for ( var _keyE in moduleDealsEvents ) { for (var _keyE in moduleDealsEvents) {
me.on( _keyE, moduleDealsEvents[ _keyE ] ); me.on(_keyE, moduleDealsEvents[_keyE]);
} }
} }
} }
} }
}, },
execCommand: function ( name ) { execCommand: function(name) {
var me = this; var me = this;
var _action = new _commands[ name ](); var _action = new _commands[name]();
console.log( _action ); console.log(_action);
var args = arguments; var args = arguments;
args[ 0 ] = this; args[0] = this;
if ( _action.execute ) { var _sendingArgs = (function() {
var canceled = me.fire( 'beforecommand', { var _args = [];
commandName: "", for (var i = 1; i < args.length; i++) {
commandArgs: [] _args.push(args[i]);
}, true ); }
//me.fire("beforecommand",_action); return _args;
me.on( "precommand", function ( e ) { })();
if ( e.target === _action ) { console.log(args);
_action.execute.apply( null, args ); var eventParams = {
} command: _action,
me.fire( "command", _action ); commandName: name,
} ); commandArgs: _sendingArgs
};
var canceled = me._fire(new MinderEvent('beforecommand', eventParams, true));
if (!canceled) {
me._fire(new MinderEvent("precommand", eventParams, false));
_action.execute.apply(_action, args);
me._fire(new MinderEvent("command", eventParams, false));
if (_action.isContentChanged()) {
me._firePharse('contentchange');
}
if (_action.isSelectionChanged()) {
me._firePharse('selectionchange');
}
} }
}, },
queryCommandState: function ( name ) { queryCommandState: function(name) {
if ( !_commands[ name ] ) { if (!_commands[name]) {
return false; return false;
} }
if ( !_query[ name ] ) { if (!_query[name]) {
_query[ name ] = new _commands[ name ](); _query[name] = new _commands[name]();
} }
if ( _query[ name ].queryState ) { if (_query[name].queryState) {
return _query[ name ].queryState( this ); return _query[name].queryState(this);
} else { } else {
return 0; return 0;
} }
}, },
queryCommandValue: function ( name ) { queryCommandValue: function(name) {
if ( !_commands[ name ] ) { if (!_commands[name]) {
return false; return false;
} }
if ( !_query[ name ] ) { if (!_query[name]) {
_query[ name ] = new _commands[ name ](); _query[name] = new _commands[name]();
} }
if ( _query[ name ].queryValue ) { if (_query[name].queryValue) {
return _query[ name ].queryValue( this ); return _query[name].queryValue(this);
} else { } else {
return 0; return 0;
} }
} }
}; };
} )() ); })());
// 节点控制 // 节点控制
kity.extendClass( KityMinder, { kity.extendClass(KityMinder, {
getRoot: function () { getRoot: function() {
return this._root; return this._root;
}, },
traverse: function ( node, fn ) { traverse: function(node, fn) {
var children = node.getChildren(); var children = node.getChildren();
for ( var i = 0; i < children.length; i++ ) { for (var i = 0; i < children.length; i++) {
this.traverse( children[ i ], fn ); this.traverse(children[i], fn);
} }
fn.call( this, node ); fn.call(this, node);
}, },
handelNodeInsert: function ( node ) { handelNodeInsert: function(node) {
this.traverse( node, function ( current ) { this.traverse(node, function(current) {
this._rc.addShape( current.getRenderContainer() ); this._rc.addShape(current.getRenderContainer());
} ); });
}, },
handelNodeRemove: function ( node ) { handelNodeRemove: function(node) {
this.traverse( node, function ( current ) { this.traverse(node, function(current) {
this._rc.removeShape( current.getRenderContainer() ); this._rc.removeShape(current.getRenderContainer());
} ); });
}, },
update: function ( node ) { update: function(node) {
node = node || this._root; node = node || this._root;
this.traverse( node, function ( current ) { this.traverse(node, function(current) {
var rc = current.getRenderContainer(); var rc = current.getRenderContainer();
var x = current.getData( 'x' ) || 0, var x = current.getData('x') || 0,
y = current.getData( 'y' ) || 0; y = current.getData('y') || 0;
rc.setTransform( new kity.Matrix().translate( x, y ) ); rc.setTransform(new kity.Matrix().translate(x, y));
if ( !rc.rect ) { if (!rc.rect) {
rc.rect = new kity.Rect(); rc.rect = new kity.Rect();
rc.addShape( rc.rect ); rc.addShape(rc.rect);
rc.rect.fill( '#eee' ); rc.rect.fill('#eee');
rc.rect.setRadius( 5 ); rc.rect.setRadius(5);
} }
if ( !rc.text ) { if (!rc.text) {
rc.text = new kity.Text(); rc.text = new kity.Text();
rc.addShape( rc.text ); rc.addShape(rc.text);
} }
rc.text.setContent( current.getData( 'text' ) || '' ); rc.text.setContent(current.getData('text') || '');
var box = rc.text.getRenderBox(); var box = rc.text.getRenderBox();
rc.rect.setPosition( box.x - 5, box.y - 5 ); rc.rect.setPosition(box.x - 5, box.y - 5);
rc.rect.setSize( box.width + 10, box.height + 10 ); rc.rect.setSize(box.width + 10, box.height + 10);
} ); });
} }
} ); });
// 事件机制 // 事件机制
kity.extendClass( KityMinder, { kity.extendClass(KityMinder, {
_initEvents: function () { _initEvents: function() {
this._eventCallbacks = {}; this._eventCallbacks = {};
this._bindPaperEvents(); this._bindPaperEvents();
this._bindKeyboardEvents(); this._bindKeyboardEvents();
}, },
// TODO: mousemove lazy bind // TODO: mousemove lazy bind
_bindPaperEvents: function () { _bindPaperEvents: function() {
var minder = this; var minder = this;
this._paper.on( 'click mousedown mouseup mousemove', this._firePharse.bind( this ) ); this._paper.on('click mousedown mouseup mousemove', this._firePharse.bind(this));
}, },
_bindKeyboardEvents: function () { _bindKeyboardEvents: function() {
var minder = this; var minder = this;
var listen = function ( name, callback ) { var listen = function(name, callback) {
if ( window.addEventListener ) { if (window.addEventListener) {
window.addEventListener( name, callback ); window.addEventListener(name, callback);
} else if ( window.attachEvent ) { } else if (window.attachEvent) {
window.attachEvent( name, callback ); window.attachEvent(name, callback);
} }
}; };
var events = 'keydown keyup keypress'.split( ' ' ); var events = 'keydown keyup keypress'.split(' ');
for ( var i = 0; i < events.length; i++ ) { for (var i = 0; i < events.length; i++) {
listen( events[ i ], this._firePharse.bind( this ) ); listen(events[i], this._firePharse.bind(this));
} }
}, },
_firePharse: function ( e ) { _firePharse: function(e) {
var beforeEvent, preEvent, executeEvent; var beforeEvent, preEvent, executeEvent;
beforeEvent = new MinderEvent( 'before' + e.type, e, true ); beforeEvent = new MinderEvent('before' + e.type, e, true);
if ( this._fire( beforeEvent ) ) { if (this._fire(beforeEvent)) {
return; return;
} }
preEvent = new MinderEvent( 'pre' + e.type, e, false ); preEvent = new MinderEvent('pre' + e.type, e, false);
executeEvent = new MinderEvent( e.type, e, false ); executeEvent = new MinderEvent(e.type, e, false);
this._fire( preEvent ); this._fire(preEvent);
this._fire( executeEvent ); this._fire(executeEvent);
if ( ~'mousedown mouseup keydown keyup'.indexOf( e.type ) ) { if (~'mousedown mouseup keydown keyup'.indexOf(e.type)) {
this._interactChange( e ); this._interactChange(e);
} }
}, },
_interactChange: function ( e ) { _interactChange: function(e) {
var minder = this; var minder = this;
clearTimeout( this.interactTimeout ); clearTimeout(this.interactTimeout);
this.interactTimeout = setTimeout( function () { this.interactTimeout = setTimeout(function() {
var canceled = minder._fire( new MinderEvent( 'beforeinteractchange' ) ); var canceled = minder._fire(new MinderEvent('beforeinteractchange'));
if ( canceled ) { if (canceled) {
return; return;
} }
minder._fire( new MinderEvent( 'preinteractchange' ) ); minder._fire(new MinderEvent('preinteractchange'));
minder._fire( new MinderEvent( 'interactchange' ) ); minder._fire(new MinderEvent('interactchange'));
}, 300 ); }, 300);
}, },
_listen: function ( type, callback ) { _listen: function(type, callback) {
var callbacks = this._eventCallbacks[ type ] || ( this._eventCallbacks[ type ] = [] ); var callbacks = this._eventCallbacks[type] || (this._eventCallbacks[type] = []);
callbacks.push( callback ); callbacks.push(callback);
}, },
_fire: function ( e ) { _fire: function(e) {
var callbacks = this._eventCallbacks[ e.type ]; var callbacks = this._eventCallbacks[e.type];
if ( !callbacks ) { if (!callbacks) {
return false; return false;
} }
for ( var i = 0; i < callbacks.length; i++ ) { for (var i = 0; i < callbacks.length; i++) {
callbacks[ i ].call( this, e ); callbacks[i].call(this, e);
if ( e.shouldCancelImmediately() ) { if (e.shouldCancelImmediately()) {
break; break;
} }
} }
return e.shouldCancel(); return e.shouldCancel();
}, },
on: function ( name, callback ) { on: function(name, callback) {
var types = name.split( ' ' ); var types = name.split(' ');
for ( var i = 0; i < types.length; i++ ) { for (var i = 0; i < types.length; i++) {
this._listen( types[ i ], callback ); this._listen(types[i], callback);
} }
return this; return this;
}, },
off: function ( name, callback ) { off: function(name, callback) {
var types = name.split( ' ' ); var types = name.split(' ');
var i, j, callbacks, removeIndex; var i, j, callbacks, removeIndex;
for ( i = 0; i < types.length; i++ ) { for (i = 0; i < types.length; i++) {
callbacks = this._eventCallbacks[ types[ i ] ]; callbacks = this._eventCallbacks[types[i]];
if ( callbacks ) { if (callbacks) {
removeIndex = null; removeIndex = null;
for ( j = 0; j < callbacks.length; j++ ) { for (j = 0; j < callbacks.length; j++) {
if ( callbacks[ j ] == callback ) { if (callbacks[j] == callback) {
removeIndex = j; removeIndex = j;
} }
} }
if ( removeIndex !== null ) { if (removeIndex !== null) {
callbacks.splice( removeIndex, 1 ); callbacks.splice(removeIndex, 1);
} }
} }
} }
}, },
fire: function ( type, params, cancelable ) { fire: function(type, params) {
var e = new MinderEvent( type, params, cancelable ); var e = new MinderEvent(type, params);
this._fire( e ); this._fire(e);
return this; return this;
} }
} ); });
// 导入导出 // 导入导出
kity.extendClass( KityMinder, { kity.extendClass(KityMinder, {
exportData: function ( node ) { exportData: function(node) {
var exported = {}; var exported = {};
node = node || this.getRoot(); node = node || this.getRoot();
exported.data = node.getData(); exported.data = node.getData();
var childNodes = node.getChildren(); var childNodes = node.getChildren();
if ( childNodes.length ) { if (childNodes.length) {
exported.children = []; exported.children = [];
for ( var i = 0; i < childNodes.length; i++ ) { for (var i = 0; i < childNodes.length; i++) {
exported.children.push( this.exportData( childNodes[ i ] ) ); exported.children.push(this.exportData(childNodes[i]));
} }
} }
return exported; return exported;
}, },
importData: function ( treeData ) { importData: function(treeData) {
function importToNode( treeData, node ) { function importToNode(treeData, node) {
var data = treeData.data; var data = treeData.data;
for ( var field in data ) { for (var field in data) {
node.setData( field, data[ field ] ); node.setData(field, data[field]);
} }
var childrenTreeData = treeData.children; var childrenTreeData = treeData.children;
if ( !childrenTreeData ) return; if (!childrenTreeData) return;
for ( var i = 0; i < childrenTreeData.length; i++ ) { for (var i = 0; i < childrenTreeData.length; i++) {
var childNode = new MinderNode(); var childNode = new MinderNode();
importToNode( childrenTreeData[ i ], childNode ); importToNode(childrenTreeData[i], childNode);
node.appendChild( childNode ); node.appendChild(childNode);
} }
} }
...@@ -313,76 +325,76 @@ kity.extendClass( KityMinder, { ...@@ -313,76 +325,76 @@ kity.extendClass( KityMinder, {
importData: treeData importData: treeData
}; };
var canceled = this._fire( new MinderEvent( 'beforeimport', params, true ) ); var canceled = this._fire(new MinderEvent('beforeimport', params, true));
if ( canceled ) return this; if (canceled) return this;
this._fire( new MinderEvent( 'preimport', params, false ) ); this._fire(new MinderEvent('preimport', params, false));
while ( this._root.getChildren().length ) { while (this._root.getChildren().length) {
this._root.removeChild( 0 ); this._root.removeChild(0);
} }
importToNode( treeData, this._root ); importToNode(treeData, this._root);
this._fire( new MinderEvent( 'import', params, false ) ); this._fire(new MinderEvent('import', params, false));
this._firePharse( { this._firePharse({
type: 'contentchange' type: 'contentchange'
} ); });
return this; return this;
} }
} ); });
// 选区管理 // 选区管理
kity.extendClass( KityMinder, { kity.extendClass(KityMinder, {
getSelectedNodes: function () { getSelectedNodes: function() {
return this._selectedNodes || ( this._selectedNodes = [] ); return this._selectedNodes || (this._selectedNodes = []);
}, },
select: function ( nodes ) { select: function(nodes) {
var selection = this.getSelectedNodes(); var selection = this.getSelectedNodes();
if ( false === nodes instanceof Array ) nodes = [ nodes ]; if (false === nodes instanceof Array) nodes = [nodes];
for ( var i = 0; i < nodes.length; i++ ) { for (var i = 0; i < nodes.length; i++) {
if ( selection.indexOf( nodes[ i ] ) === -1 ) { if (selection.indexOf(nodes[i]) === -1) {
selection.push( nodes[ i ] ); selection.push(nodes[i]);
} }
} }
return this; return this;
}, },
selectSingle: function ( node ) { selectSingle: function(node) {
return this.clearSelect().select( node ); return this.clearSelect().select(node);
}, },
toggleSelect: function ( nodes ) { toggleSelect: function(nodes) {
var selection = this.getSelectedNodes(); var selection = this.getSelectedNodes();
var needAdd = [], var needAdd = [],
needRemove = []; needRemove = [];
if ( false === nodes instanceof Array ) nodes = [ nodes ]; if (false === nodes instanceof Array) nodes = [nodes];
for ( var i = 0; i < nodes.length; i++ ) { for (var i = 0; i < nodes.length; i++) {
if ( selection.indexOf( nodes[ i ] ) === -1 ) { if (selection.indexOf(nodes[i]) === -1) {
needAdd.push( nodes[ i ] ); needAdd.push(nodes[i]);
} else { } else {
needRemove.push( nodes[ i ] ); needRemove.push(nodes[i]);
} }
} }
this.clearSelect( needRemove ); this.clearSelect(needRemove);
this.select( needAdd ); this.select(needAdd);
}, },
clearSelect: function ( nodes ) { clearSelect: function(nodes) {
if ( !nodes ) { if (!nodes) {
this._selectedNodes = []; this._selectedNodes = [];
return this; return this;
} }
if ( false === nodes instanceof Array ) nodes = [ nodes ]; if (false === nodes instanceof Array) nodes = [nodes];
var originSelection = this.getSelectedNodes(); var originSelection = this.getSelectedNodes();
var newSelection = []; var newSelection = [];
for ( var i = 0; i < originSelection.length; i++ ) { for (var i = 0; i < originSelection.length; i++) {
if ( nodes.indexOf( originSelection[ i ] ) === -1 ) { if (nodes.indexOf(originSelection[i]) === -1) {
newSelection.push( originSelection[ i ] ); newSelection.push(originSelection[i]);
} }
} }
this._selectedNodes = newSelection; this._selectedNodes = newSelection;
return this; return this;
} }
} ); });
\ 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