Commit f2d230d1 authored by techird's avatar techird

event & import & export

parent d38e61b3
......@@ -10,6 +10,7 @@
"predef" : [
"kity",
"MinderNode",
"MinderEvent",
"require",
"km"
]
......
......@@ -123,19 +123,97 @@ kity.extendClass(KityMinder, {
// 事件机制
kity.extendClass(KityMinder, {
_initEvents: function() {
this._eventCallbacks = {};
this._bindPaperEvents();
this._bindKeyboardEvents();
},
on: function( name, callback ) {
_bindPaperEvents: function() {
var minder = this;
this.paper.on('click mousedown mouseup mousemove', this._firePharse.bind(this));
},
once: function( name, callback ) {
_bindKeyboardEvents: function() {
var minder = this;
var listen = function(name, callback) {
if(window.addEventListener) {
window.addEventListener(name, callback);
} else if(window.attachEvent) {
window.attachEvent(name, callback);
}
};
var events = 'keydown keyup keypress'.split(' ');
for(var i = 0; i < events.length; i++) {
listen(events[i], this._firePharse.bind(this));
}
},
_firePharse: function(e) {
var beforeEvent, preEvent, executeEvent;
beforeEvent = new MinderEvent('before' + e.type, e, true);
if( !this._fire(beforeEvent) ) {
return;
}
preEvent = new MinderEvent('pre' + e.type, e, false);
executeEvent = new MinderEvent(e.type, e, false);
this._fire(preEvent);
this._fire(beforeEvent);
if(~'mousedown mouseup keydown keyup'.indexOf(e.type)) {
this._interactChange(e);
}
},
_interactChange: function(e) {
var minder = this;
this.interactTimeout = setTimeout(function() {
var canceled = minder._fire(new MinderEvent('beforeinteractchange'));
if(canceled) {
return;
}
minder._fire(new MinderEvent('preinteractchange'));
minder._fire(new MinderEvent('interactchange'));
}, 500);
},
_listen: function( type, callback ) {
var callbacks = this._eventCallbacks[type] || (this._eventCallbacks[type] = []);
callbacks.push( callback );
},
_fire: function( e ) {
var callbacks = this._eventCallbacks[e.type];
for(var i = 0; i < callbacks.length; i++) {
callbacks[i].call(this, e);
if(e.shouldCancelImmediately()) {
break;
}
}
return e.shouldCancel();
},
on: function( name, callback ) {
var types = name.split(' ');
for(var i = 0; i < types.length; i++) {
this._listen( types[i], callback );
}
return this;
},
off: function( name, callback ) {
var types = name.split(' ');
var i, j, callbacks, removeIndex;
for(i = 0; i < types.length; i++) {
callbacks = this._eventCallbacks[ types[i] ];
if(callbacks) {
removeIndex = null;
for(j = 0; j < callbacks.length; j++) {
if(callbacks[j] == callback) {
removeIndex = j;
}
}
if(removeIndex !== null) {
callbacks.splice(removeIndex, 1);
}
}
}
},
fire: function( name, params ) {
fire: function( type, params ) {
var e = params || {};
e.type = type;
this._firePharse(e);
return this;
}
});
......@@ -161,12 +239,38 @@ kity.extendClass(KityMinder, {
// 导入导出
kity.extendClass(KityMinder, {
export: function() {
exportData: function(node) {
var exported = {};
node = node || this.getRoot();
exported.data = node.getData();
var childNodes = node.getChildren();
if(childNodes.length) {
exported.children = [];
for(var i = 0; i < childNodes.length; i++) {
exported.children.push(this.exportData(childNodes[i]));
}
}
return exported;
},
import: function() {
importData: function(data) {
function importToNode(data, node) {
var children = data.children;
delete data.children;
for(var field in data) {
node.setData(field, data[field]);
}
for(var i = 0; i < children.length; i++) {
var child = new MinderNode();
importToNode(children[i], child);
node.appendChild(child);
}
}
while(this.root.getChildren().length) {
this.root.removeChild(0);
}
importToNode(data, this.root);
return this;
}
});
......
var MinderEvent = kity.createClass('MindEvent', {
constructor: function (type, params, cancelable) {
kity.Utils.extend(this, params || {});
this.type = type;
this.cancelable = cancelable || false;
},
cancel: function() {
this.canceled = true;
},
cancelImmediately: function() {
this.immediatelyCanceld = true;
this.canceled = true;
},
shouldCancel: function() {
return this.cancelable && this.canceled;
},
shouldCancelImmediately: function() {
return this.cancelable && this.immediatelyCanceld;
}
});
\ No newline at end of file
......@@ -5,6 +5,7 @@ var MinderNode = km.MinderNode = kity.createClass("MinderNode", {
this.data = {};
this.tnh = treeNotifyHandler;
this.rc = new kity.Group();
this.rc.minderNode = this;
},
getParent: function() {
......
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