Commit ce1fca81 authored by techird's avatar techird

添加策略函数

parent a5fe311c
...@@ -3,6 +3,38 @@ KityMinder.registerModule( "Expand", function () { ...@@ -3,6 +3,38 @@ KityMinder.registerModule( "Expand", function () {
STATE_EXPAND = 'expand', STATE_EXPAND = 'expand',
STATE_COLLAPSE = 'collapse'; STATE_COLLAPSE = 'collapse';
/**
* 该函数返回一个策略,表示递归到节点指定的层数
*
* 返回的策略表示把操作(展开/收起)进行到指定的层数
* 也可以给出一个策略指定超过层数的节点如何操作,默认不进行任何操作
*
* @param {int} deep_level 指定的层数
* @param {Function} policy_after_level 超过的层数执行的策略
*/
function generateDeepPolicy( deep_level, policy_after_level ) {
return function ( node, state, policy, level ) {
var children, child, i;
node.setData( EXPAND_STATE_DATA, state );
level = level || 1;
children = node.getChildren();
for ( i = 0; i < children.length; i++ ) {
child = children[ i ];
if ( level <= deep_level ) {
policy( child, state, policy, level + 1 );
} else if ( policy_after_level ) {
policy_after_level( child, state, policy, level + 1 );
}
}
};
}
/** /**
* 节点展开和收缩的策略常量 * 节点展开和收缩的策略常量
* *
...@@ -13,45 +45,58 @@ KityMinder.registerModule( "Expand", function () { ...@@ -13,45 +45,58 @@ KityMinder.registerModule( "Expand", function () {
* @param {Function} policy 提供当前策略的函数,方便递归调用 * @param {Function} policy 提供当前策略的函数,方便递归调用
*/ */
MinderNode.EXAPND_POLICY = { MinderNode.EXAPND_POLICY = {
/** /**
* 策略 1:只修改当前节点的状态,不递归子节点的状态 * 策略 1:只修改当前节点的状态,不递归子节点的状态
*/ */
KEEP_STATE: function ( node, state, policy ) { KEEP_STATE: function ( node, state, policy ) {
node.setData( expandStateData, state === STATE_EXPAND ); node.setData( EXPAND_STATE_DATA, state );
}, },
generateDeepPolicy: generateDeepPolicy,
/** /**
* 策略 2:该函数返回一个策略,表示递归到节点指定的层数 * 策略 2:把操作进行到儿子
* 返回的策略表示把操作(展开/收起)进行到指定的层数
*
* @param {int} deep_level 指定的层数
*/ */
DEEP_TO_LEVEL: function ( deep_level ) { DEEP_TO_CHILD: generateDeepPolicy( 1 ),
return function ( node, state, policy ) {
};
},
DEEP_TO_CHILD: function () {
}, /**
DEEP_TO_LEAVE: function () { * 策略 3:把操作进行到叶子
*/
} DEEP_TO_LEAF: generateDeepPolicy( Number.MAX_VALUE )
}; };
// 将展开的操作和状态读取接口拓展到 MinderNode 上
kity.extendClass( MinderNode, { kity.extendClass( MinderNode, {
/**
* 使用指定的策略展开节点
* @param {Policy} policy 展开的策略,默认为 KEEP_STATE
*/
expand: function ( policy ) { expand: function ( policy ) {
node.setData( EXPAND_STATE_DATA, STATE_EXPAND ); policy = policy || EXAPND_POLICY.KEEP_STATE;
policy( this, STATE_EXPAND, policy );
return this;
}, },
collapse: function () {
node.setData( EXPAND_STATE_DATA, STATE_COLLAPSE ); /**
* 使用指定的策略收起节点
* @param {Policy} policy 展开的策略,默认为 KEEP_STATE
*/
collapse: function ( policy ) {
policy = policy || EXAPND_POLICY.KEEP_STATE;
policy( this, STATE_EXPAND, policy );
return this;
}, },
/**
* 判断节点当前的状态是否为展开
*/
isExpanded: function () { isExpanded: function () {
return node.getData( EXPAND_STATE_DATA ) === STATE_EXPAND; return !!node.getData( EXPAND_STATE_DATA );
} }
} ); } );
return { return {
'commands': { 'commands': {}
}
}; };
} ); } );
\ 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