@@ -30,9 +30,10 @@ function treeDirective($mdTheming, $mdUtil) {
3030  } 
3131
3232  /*@ngInject */ 
33-   function  controller ( $scope )  { 
33+   function  controller ( $scope ,   $attrs )  { 
3434    /*jshint validthis:true*/ 
3535    var  vm  =  this ; 
36+     var  selectionRestictions ; 
3637    var  branches  =  { } ; 
3738
3839    vm . selected  =  { } ; 
@@ -107,17 +108,20 @@ function treeDirective($mdTheming, $mdUtil) {
107108      }  else  { 
108109        deselect ( hashKey ) ; 
109110      } 
110-       refreshViewValue ( ) ; 
111111    } 
112112
113113    function  deselectAll ( )  { 
114114      Object . keys ( branches ) . forEach ( deselect ) ; 
115115    } 
116116
117117    function  select ( hashKey ,  hashedValue )  { 
118+       var  depth ; 
118119      var  branch  =  branches [ hashKey ] ; 
119-       if  ( branch  !==  undefined )  {  branch . setSelected ( true ) ;  } 
120-       vm . selected [ hashKey ]  =  hashedValue ; 
120+       if  ( branch  !==  undefined )  { 
121+         handleSelectionConflicts ( branch ) 
122+         branch . setSelected ( true ) ; 
123+         vm . selected [ hashKey ]  =  hashedValue ; 
124+       } 
121125    } 
122126
123127    function  deselect ( hashKey )  { 
@@ -127,6 +131,31 @@ function treeDirective($mdTheming, $mdUtil) {
127131      delete  vm . selected [ hashKey ] ; 
128132    } 
129133
134+     // handle selection restrictions set by `[restrict-selection]` attr 
135+     function  handleSelectionConflicts ( branch )  { 
136+       var  restictions  =  getSelectionRestrictions ( ) ; 
137+       if  ( restictions . single )  {  deselectAll ( ) ;  } 
138+       var  depth  =  branch . getDepth ( ) ; 
139+       var  conflictingDepths  =  Object . keys ( vm . selected ) . filter ( function  ( hashKey )  { 
140+         return  branches [ hashKey ] . getDepth ( )  !==  depth ; 
141+       } ) ; 
142+       if  ( restictions . depth  &&  conflictingDepths . length )  { 
143+         conflictingDepths . forEach ( deselect ) ; 
144+       } 
145+     } 
146+ 
147+     // gets selection restrictions from the `[restrict-selection]` attr and puts it into an object 
148+     function  getSelectionRestrictions ( )  { 
149+       if  ( ! selectionRestictions )  { 
150+         selectionRestictions  =  { } ; 
151+         var  attrArr  =  $attrs . restrictSelection  ? $attrs . restrictSelection . split ( ',' ) . map ( function  ( item )  {  return  item . trim ( ) ;  } )  : [ ] ; 
152+         attrArr . forEach ( function  ( key )  { 
153+           selectionRestictions [ key ]  =  true ; 
154+         } ) ; 
155+       } 
156+       return  selectionRestictions ; 
157+     } 
158+ 
130159    function  refreshViewValue ( )  { 
131160      var  branchValue ; 
132161      var  newValue ; 
0 commit comments