데이타 그룹처리

데이타를 그룹처리 해주는 함수입니다.

/**
* 데이타 그룹화
* @writer youngsu lee
* @email yengsu@hanmail.net
* 
* @param object opts 설정정보
*     
   opts = {
       'resource' : 리소스값
       'gkey' : [그룹키, 그룹키...]
       'gkey_branch' : 그룹키 구분자 (ex) '_' => abc_234 / '#' => abc#234
       'callback' : function(Res){
        // 데이타 : Res.data 
        // 그룹키 : Res.grp_key
        // 그룹화 시킨 데이타중의 하나의 record 정보 : Res.node
        
        return Res ;
       }
      }
*
* @return json object
*
*/
function Group_by(Opts)
{
  var dataMap = Opts.resource.reduce(function(maps, node) {

   // 그룹키 생성
   if( Opts.gkey.length > 1 )
   {
    var group_key = Opts.gkey.map(function( val, i ) {
     if(val){
      if(typeof node[val] !== 'undefined') return $.trim(node[val]);
     }
    }).join(Opts.gkey_branch || '') ;
   }else{
    var group_key = node[Opts.gkey[0]];
   }

   // 그룹화 object  생성
   if( typeof maps[group_key] == 'undefined' ) maps[group_key] = {} ;

   // 콜백실행
   if( $.isFunction(Opts.callback)){
    var callback_res = Opts.callback.call(this, {
      data : maps[group_key], 
      grp_key : group_key,
      node : node
     }).data || maps[group_key] ;
     //maps[group_key] = callback_res.data || maps[group_key] ;
   }

   /*카운트*/maps[group_key].Total = 0 ;
   return maps;
  }, {});

  var tree = [];
  Opts.resource.forEach(function(node) {
   
   // 그룹키 생성
   if( Opts.gkey.length > 1 )
   {
    var group_key = Opts.gkey.map(function( val, i ) {
     if(typeof node[val] !== 'undefined') return $.trim(node[val])
    }).join(Opts.gkey_branch || '') ;
   }else{
    var group_key = node[Opts.gkey[0]];
   }
   
      var parent = dataMap[group_key];
      if (parent) {
          (parent.children || (parent.children = []))
              .push(node);
    parent.Total++ ;
      } else {
          tree.push(node);
          tree.Total++ ;
      }
  });

  return dataMap ;
}