Commit d90f302b authored by techird's avatar techird

导入导出功能

parent d91df167
......@@ -22,6 +22,6 @@
</div>
</body>
<script>
KM.getKityMinder('kityminder');
window.m = KM.getKityMinder('kityminder');
</script>
</html>
\ No newline at end of file
......@@ -51,7 +51,8 @@ $dependency = Array(
,'src/adapter/adapter.js'
,'src/adapter/button.js'
,'src/adapter/combobox.js'
,'src/protocal/plain.js'
,'src/protocal/json.js'
);
$content = "";
......
utils.extend(KityMinder,function(){
utils.extend( KityMinder, function () {
var _kityminderUI = {},
_activeWidget = null,
_widgetData = {},
_widgetCallBack = {};
return {
registerUI:function(uiname,fn){
utils.each(uiname.split(/\s+/), function (i,name) {
_kityminderUI[name] = fn;
})
registerUI: function ( uiname, fn ) {
utils.each( uiname.split( /\s+/ ), function ( i, name ) {
_kityminderUI[ name ] = fn;
} )
},
_createUI:function(id){
var $cont = $('<div class="kmui-container"></div>'),
_createUI: function ( id ) {
var $cont = $( '<div class="kmui-container"></div>' ),
$toolbar = $.kmuitoolbar(),
$kmbody = $('<div class="kmui-editor-body"></div>'),
$statusbar = $('<div class="kmui-statusbar"></div>');
$kmbody = $( '<div class="kmui-editor-body"></div>' ),
$statusbar = $( '<div class="kmui-statusbar"></div>' );
$cont.append($toolbar).append($kmbody).append($statusbar);
$(utils.isString(id) ? '#' + id : id).append($cont);
$cont.append( $toolbar ).append( $kmbody ).append( $statusbar );
$( utils.isString( id ) ? '#' + id : id ).append( $cont );
return {
'$container':$cont,
'$toolbar':$toolbar,
'$body':$kmbody,
'$statusbar':$statusbar
'$container': $cont,
'$toolbar': $toolbar,
'$body': $kmbody,
'$statusbar': $statusbar
};
},
_createToolbar:function($toolbar,km){
var toolbars = km.getOptions('toolbars');
if (toolbars && toolbars.length) {
_createToolbar: function ( $toolbar, km ) {
var toolbars = km.getOptions( 'toolbars' );
if ( toolbars && toolbars.length ) {
var btns = [];
$.each(toolbars,function(i,uiNames){
$.each(uiNames.split(/\s+/),function(index,name){
if(name == '|'){
$.kmuiseparator && btns.push($.kmuiseparator());
}else{
if(_kityminderUI[name]){
var ui = _kityminderUI[name].call(km,name);
ui && btns.push(ui);
$.each( toolbars, function ( i, uiNames ) {
$.each( uiNames.split( /\s+/ ), function ( index, name ) {
if ( name == '|' ) {
$.kmuiseparator && btns.push( $.kmuiseparator() );
} else {
if ( _kityminderUI[ name ] ) {
var ui = _kityminderUI[ name ].call( km, name );
ui && btns.push( ui );
}
}
});
btns.length && $toolbar.kmui().appendToBtnmenu(btns);
});
} );
btns.length && $toolbar.kmui().appendToBtnmenu( btns );
} );
}
$toolbar.append($('<div class="kmui-dialog-container"></div>'));
$toolbar.append( $( '<div class="kmui-dialog-container"></div>' ) );
},
_createStatusbar:function($statusbar,km){
_createStatusbar: function ( $statusbar, km ) {
},
getKityMinder:function(id,options){
var containers = this._createUI(id);
var km = this.getMinder(containers.$body.get(0),options);
this._createToolbar(containers.$toolbar,km);
this._createStatusbar(containers.$statusbar,km);
getKityMinder: function ( id, options ) {
var containers = this._createUI( id );
var km = this.getMinder( containers.$body.get( 0 ), options );
this._createToolbar( containers.$toolbar, km );
this._createStatusbar( containers.$statusbar, km );
km.$container = containers.$container;
return km;
},
registerWidget : function(name,pro,cb){
_widgetData[name] = $.extend2(pro,{
$root : '',
_preventDefault:false,
root:function($el){
return this.$root || (this.$root = $el);
registerWidget: function ( name, pro, cb ) {
_widgetData[ name ] = $.extend2( pro, {
$root: '',
_preventDefault: false,
root: function ( $el ) {
return this.$root || ( this.$root = $el );
},
preventDefault:function(){
preventDefault: function () {
this._preventDefault = true;
},
clear:false
});
if(cb){
_widgetCallBack[name] = cb;
clear: false
} );
if ( cb ) {
_widgetCallBack[ name ] = cb;
}
},
getWidgetData : function(name){
return _widgetData[name]
getWidgetData: function ( name ) {
return _widgetData[ name ]
},
setWidgetBody : function(name,$widget,km){
if(!km._widgetData){
setWidgetBody: function ( name, $widget, km ) {
if ( !km._widgetData ) {
utils.extend(km,{
_widgetData : {},
getWidgetData : function(name){
return this._widgetData[name];
utils.extend( km, {
_widgetData: {},
getWidgetData: function ( name ) {
return this._widgetData[ name ];
},
getWidgetCallback : function(widgetName){
getWidgetCallback: function ( widgetName ) {
var me = this;
return function(){
return _widgetCallBack[widgetName].apply(me,[me,$widget].concat(utils.argsToArray(arguments,0)))
return function () {
return _widgetCallBack[ widgetName ].apply( me, [ me, $widget ].concat( utils.argsToArray( arguments, 0 ) ) )
}
}
})
} )
}
var pro = _widgetData[name];
if(!pro){
var pro = _widgetData[ name ];
if ( !pro ) {
return null;
}
pro = km._widgetData[name];
if(!pro){
pro = _widgetData[name];
pro = km._widgetData[name] = $.type(pro) == 'function' ? pro : utils.clone(pro);
pro = km._widgetData[ name ];
if ( !pro ) {
pro = _widgetData[ name ];
pro = km._widgetData[ name ] = $.type( pro ) == 'function' ? pro : utils.clone( pro );
}
pro.root($widget.kmui().getBodyContainer());
pro.root( $widget.kmui().getBodyContainer() );
pro.initContent(km,$widget);
if(!pro._preventDefault){
pro.initEvent(km,$widget);
pro.initContent( km, $widget );
if ( !pro._preventDefault ) {
pro.initEvent( km, $widget );
}
pro.width && $widget.width(pro.width);
pro.width && $widget.width( pro.width );
},
setActiveWidget : function($widget){
setActiveWidget: function ( $widget ) {
_activeWidget = $widget;
}
}
}());
}() );
\ No newline at end of file
Utils.extend( KityMinder, {
_protocals: {},
registerProtocal: function ( name, protocalDeal ) {
KityMinder._protocals[ name ] = protocalDeal();
},
findProtocal: function ( name ) {
return KityMinder._protocals[ name ] || null;
},
getSupportedProtocals: function () {
return Utils.keys( KityMinder._protocals );
}
} );
// 这里的 Json 是一个对象
function exportNode( node ) {
var exported = {};
exported.data = node.getData();
var childNodes = node.getChildren();
if ( childNodes.length ) {
exported.children = [];
for ( var i = 0; i < childNodes.length; i++ ) {
exported.children.push( exportNode( childNodes[ i ] ) );
}
}
return exported;
}
function importNode( node, json ) {
var data = json.data;
for ( var field in data ) {
node.setData( field, data[ field ] );
}
var childrenTreeData = json.children;
if ( !childrenTreeData ) return;
for ( var i = 0; i < childrenTreeData.length; i++ ) {
var childNode = new MinderNode();
importNode( childNode, childrenTreeData[ i ] );
node.appendChild( childNode );
}
return node;
}
// 导入导出
kity.extendClass( Minder, {
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 ] ) );
}
exportData: function ( protocalName ) {
var json, protocal;
json = exportNode( this.getRoot() );
protocal = KityMinder.findProtocal( protocalName );
if ( protocal ) {
return protocal.encode( json );
} else {
return json;
}
return exported;
},
importData: function ( treeData ) {
function importToNode( treeData, node ) {
var data = treeData.data;
for ( var field in data ) {
node.setData( field, data[ field ] );
}
var childrenTreeData = treeData.children;
if ( !childrenTreeData ) return;
for ( var i = 0; i < childrenTreeData.length; i++ ) {
var childNode = new MinderNode();
importToNode( childrenTreeData[ i ], childNode );
node.appendChild( childNode );
}
importData: function ( local, protocalName ) {
var json, protocal;
if ( protocalName ) {
protocal = KityMinder.findProtocal( protocalName );
} else {
KityMinder.getSupportedProtocals().every( function ( name ) {
var test = KityMinder.findProtocal( name );
if ( test.recognize && test.recognize( local ) ) {
protocal = test;
}
return !test;
} );
}
if ( !protocal ) {
throw new Error( "Unsupported protocal: " + protocalName );
}
var params = {
importData: treeData
local: local,
protocalName: protocalName,
protocal: protocal
};
// 是否需要阻止导入
var stoped = this._fire( new MinderEvent( 'beforeimport', params, true ) );
if ( stoped ) return this;
json = params.json || ( params.json = protocal.decode( local ) );
this._fire( new MinderEvent( 'preimport', params, false ) );
// 删除当前所有节点
while ( this._root.getChildren().length ) {
this._root.removeChild( 0 );
}
importToNode( treeData, this._root );
importNode( this._root, json );
this._fire( new MinderEvent( 'import', params, false ) );
this._firePharse( {
......@@ -52,6 +103,7 @@ kity.extendClass( Minder, {
this._firePharse( {
type: 'interactchange'
} );
return this;
}
} );
\ No newline at end of file
KityMinder.registerProtocal( 'json', function () {
function filter( key, value ) {
if ( key == 'layout' || key == 'shicon' ) {
return undefined;
}
return value;
}
return {
encode: function ( json ) {
return JSON.stringify( json, filter );
},
decode: function ( local ) {
return JSON.parse( local );
},
recognize: function ( local ) {
return Utils.isString( local ) && local.charAt( 0 ) == '{' && local.charAt( local.length - 1 ) == '}';
}
};
} );
\ No newline at end of file
KityMinder.registerProtocal( "plain", function () {
var LINE_ENDING = '\n',
TAB_CHAR = '\t';
function repeat( s, n ) {
var result = "";
while ( n-- ) result += s;
return result;
}
function encode( json, level ) {
var local = "";
level = level || 0;
local += repeat( TAB_CHAR, level );
local += json.data.text + LINE_ENDING;
if ( json.children ) {
json.children.forEach( function ( child ) {
local += encode( child, level + 1 );
} );
}
return local;
}
function isEmpty( line ) {
return !/\S/.test( line );
}
function getLevel( line ) {
var level = 0;
while ( line.charAt( level ) === TAB_CHAR ) level++;
return level;
}
function getNode( line ) {
return {
data: {
text: line.replace( new RegExp( '^' + TAB_CHAR + '*' ), '' )
}
};
}
function decode( local ) {
var json,
parentMap = {},
lines = local.split( LINE_ENDING ),
line, level, node;
function addChild( parent, child ) {
var children = parent.children || ( parent.children = [] );
children.push( child );
}
for ( var i = 0; i < lines.length; i++ ) {
line = lines[ i ];
if ( isEmpty( line ) ) continue;
level = getLevel( line );
node = getNode( line );
if ( level === 0 ) {
if ( json ) {
throw new Error( 'Invalid local format' );
}
json = node;
} else {
if ( !parentMap[ level - 1 ] ) {
throw new Error( 'Invalid local format' );
}
addChild( parentMap[ level - 1 ], node );
}
parentMap[ level ] = node;
}
return json;
}
var lastTry, lastResult;
function recognize( local ) {
if ( !Utils.isString( local ) ) return false;
lastTry = local;
try {
lastResult = decode( local );
} catch ( e ) {
lastResult = null;
}
return !!lastResult;
}
return {
encode: function ( json ) {
return encode( json, 0 );
},
decode: function ( local ) {
if ( lastTry == local && lastResult ) {
return lastResult;
}
return decode( local );
},
recognize: recognize
};
} );
\ 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