33  . directive ( 'mdBranch' ,  branchDirective ) ; 
44
55
6+ // checkbox html 
67var  CHECKBOX_SELECTION_INDICATOR  =  angular . element ( '<div class="checkbox-container"><div class="checkbox-icon"></div></div>' ) ; 
8+ // branch arrow icon svg 
79var  BRANCH_ARROW_TEMPLATE  =  angular . element ( '<div class="md-branch-icon-container">' + 
810  '<div class="md-branch-icon">' + 
911    '<svg fill="#000000" height="24" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg">' + 
@@ -45,14 +47,18 @@ function branchDirective($parse, $document, $mdUtil, $filter, $$mdTree) {
4547      var  isFilterOpen  =  false ; 
4648      if  ( isOpen )  {  startWatching ( ) ;  } 
4749
48- 
50+        // standard angular filter wrapped so we can determian if the parent should be opened for closed 
4951      scope . $mdBranchFilter  =  function  ( value )  { 
5052        var  filtered  =  $filter ( 'filter' ) ( value ) ; 
53+ 
54+         // open branches if filter string is greater then 2 and items have been found 
5155        if  ( filtered  &&  filtered . length  >  2 )  { 
5256          isFilterOpen  =  true ; 
5357          blocks . forEach ( function  ( block )  { 
5458            $$mdTree . filterOpen ( block ) ; 
5559          } ) ; 
60+ 
61+         // close branches if filter is less than 3 characters or no items have been found 
5662        }  else  if  ( ( ! filtered  ||  filtered . length  <  3 )  &&  isFilterOpen )  { 
5763          isFilterOpen  =  false ; 
5864          blocks . forEach ( function  ( block )  { 
@@ -63,16 +69,20 @@ function branchDirective($parse, $document, $mdUtil, $filter, $$mdTree) {
6369      } 
6470
6571
72+       // watch model data 
6673      function  startWatching ( )  { 
6774        if  ( dataWatcher )  {  return ;  } 
6875        dataWatcher  =  scope . $watchCollection ( repeatListExpression ,  updateBranch ) ; 
6976      } 
77+       // kill watcher 
7078      function  killWatching ( )  { 
7179        if  ( typeof  dataWatcher  ===  'function' )  { 
7280          dataWatcher ( ) ; 
7381          dataWatcher  =  undefined ; 
7482        } 
7583      } 
84+ 
85+       // expose methods to scope 
7686      scope . startWatching  =  startWatching ; 
7787      scope . killWatching  =  killWatching ; 
7888
@@ -87,12 +97,10 @@ function branchDirective($parse, $document, $mdUtil, $filter, $$mdTree) {
8797        var  index ; 
8898        var  length ; 
8999        var  maxIndex ; 
90-         var  lengthChanged  =  false ; 
91100        var  newBlocks  =  [ ] ; 
92101        var  _itemsLength  =  newItems  &&  newItems . length  ||  0 ; 
93102
94103        if  ( _itemsLength  !==  itemsLength )  { 
95-           lengthChanged  =  true ; 
96104          itemsLength  =  _itemsLength ; 
97105        } 
98106        items  =  newItems ; 
@@ -137,43 +145,41 @@ function branchDirective($parse, $document, $mdUtil, $filter, $$mdTree) {
137145      } 
138146
139147
148+       // store block in memory and remove it from the dom. 
140149      function  poolBlock ( index )  { 
141150        pooledBlocks . unshift ( blocks [ index ] ) ; 
142151        blocks [ index ] . element [ 0 ] . parentNode . removeChild ( blocks [ index ] . element [ 0 ] ) ; 
143152        delete  blocks [ index ] ; 
144153      } 
145154
155+       // update block scope and state 
146156      function  updateBlock ( block ,  index )  { 
147157        blocks [ index ]  =  block ; 
148158
149-         if  ( block . new )  {  updateNewBlock ( block ) ;  } 
159+         if  ( block . new )  {  updateNewBlock ( block ) ;  }   // configure template for new blocks 
150160        if  ( ! block . new  &&  ( block . scope . $index  ===  index  &&  block . scope [ repeatName ]  ===  items [ index ] ) )  { 
151-           updateState ( block . scope ,   index ) ; 
161+           updateState ( block . scope ,   index ) ;   // update state if a block is nore or changes 
152162          return ; 
153163        } 
154164        block . new  =  false ; 
165+ 
155166        // Update and digest the block's scope. 
156167        updateScope ( block . scope ,  index ) ; 
157168        updateState ( block . scope ,   index ) ; 
158169
159-         // Perform digest before re-attaching the block. 
160-         // Any resulting synchronous dom mutations should be much faster as a result. 
161-         // This might break some directives, but I'm going to try it for now. 
162170        if  ( ! scope . $root . $$phase )  { 
163171          block . scope . $digest ( ) ; 
164172        } 
165173      } 
166174
167175
168-       // NOTE this might  cause problems when applying a new scope 
176+       // NOTE Might  cause problems when applying a new scope 
169177      // place contents into containers to display items correctly 
170178      // this is only done once 
171179      function  updateNewBlock ( block )  { 
172180        var  isSelectable  =  block . element . attr ( 'select' )  !==  undefined ; 
173-         // branch contents 
174-         var  innerContainer  =  angular . element ( '<div class="md-branch-inner">' ) ; 
175-         // nested branched 
176-         var  branchContainer  =  angular . element ( '<div class="md-branch-container">' ) ; 
181+         var  innerContainer  =  angular . element ( '<div class="md-branch-inner">' ) ;  // branch contents 
182+         var  branchContainer  =  angular . element ( '<div class="md-branch-container">' ) ;  // nested branched 
177183        innerContainer . append ( BRANCH_ARROW_TEMPLATE . clone ( ) ) ; 
178184        if  ( isSelectable )  { 
179185          block . element . addClass ( 'md-checkbox-enabled' ) ; 
@@ -188,7 +194,6 @@ function branchDirective($parse, $document, $mdUtil, $filter, $$mdTree) {
188194        } ) ; 
189195        block . element . append ( innerContainer ) ; 
190196
191- 
192197        // add branches 
193198        if  ( branchContainer [ 0 ] . childNodes . length )  { 
194199          block . element . append ( branchContainer ) ; 
@@ -199,10 +204,11 @@ function branchDirective($parse, $document, $mdUtil, $filter, $$mdTree) {
199204        } 
200205      } 
201206
207+       // Change the model value attached to the scope 
202208      function  updateScope ( $scope ,  index )  { 
203-         $scope . $index  =  index ; 
204-         $scope . repeatName  =  repeatName ; 
205-         $scope [ repeatName ]  =  items  &&  items [ index ] ; 
209+         $scope . $index  =  index ;   // data index 
210+         $scope . repeatName  =  repeatName ;   // data property 
211+         $scope [ repeatName ]  =  items  &&  items [ index ] ;   // data 
206212        $scope . $odd  =  ! ( $scope . $even  =  ( index  &  1 )  ===  0 ) ; 
207213        $scope . $depth  =  ( $scope . $parent . $depth  +  1 )  ||  0 ; 
208214        items [ index ] . $$depth  =  $scope . $depth ; 
@@ -233,6 +239,7 @@ function branchDirective($parse, $document, $mdUtil, $filter, $$mdTree) {
233239        } ) ; 
234240      } 
235241
242+       // walk dome to find tree 
236243      function  getTreeCtrl ( scope )  { 
237244        if  ( scope . treeCtrl )  {  return  scope . treeCtrl ;  } 
238245        var  parent  =  scope . $element [ 0 ] . parentNode ; 
@@ -246,6 +253,7 @@ function branchDirective($parse, $document, $mdUtil, $filter, $$mdTree) {
246253        console . error ( '`<md-branch>` element is not nested in a `<md-tree>` element. Selection will not work' ) ; 
247254      } 
248255
256+       // set initial state on data 
249257      function  initState ( item )  { 
250258        if  ( item . $$isOpen  ===  undefined )  { 
251259          Object . defineProperty ( item ,  '$$isOpen' ,  { 
@@ -257,11 +265,14 @@ function branchDirective($parse, $document, $mdUtil, $filter, $$mdTree) {
257265        } 
258266      } 
259267
268+       // check pool for block 
269+       // otherwise create a new block 
260270      function  getBlock ( index )  { 
261271        if  ( pooledBlocks . length )  { 
262272          return  pooledBlocks . pop ( ) ; 
263273        } 
264274
275+         // create new bloc 
265276        var  block ; 
266277        transclude ( function ( clone ,  scope )  { 
267278          block  =  { 
@@ -273,12 +284,12 @@ function branchDirective($parse, $document, $mdUtil, $filter, $$mdTree) {
273284          updateScope ( scope ,  index ) ; 
274285          initState ( items [ index ] ) ; 
275286          scope . $element  =  clone ;  // attach element to scope so it can be accessed in controller 
276- 
277287          parentNode . appendChild ( clone [ 0 ] ) ; 
278288        } ) ; 
279289        return  block ; 
280290      } 
281291
292+       // add blocks to one fragment for better performance 
282293      function  domFragmentFromBlocks ( blocks )  { 
283294        var  fragment  =  $document [ 0 ] . createDocumentFragment ( ) ; 
284295        blocks . forEach ( function ( block )  { 
0 commit comments