Skip to content

Commit 002c6a6

Browse files
Ben RubinBen Rubin
authored andcommitted
add more code comments
1 parent 526ad97 commit 002c6a6

File tree

6 files changed

+102
-50
lines changed

6 files changed

+102
-50
lines changed

docsApp/pages/home/home.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ <h3>{{menu.name}}</h3>
1414
<p>Menu</p>
1515
</div>
1616

17-
<md-branch branch-repeat="item in menu.items | filter: searchTerm | orderBy: item.name" class="md-2-line" select>
17+
<md-branch branch-repeat="item in menu.items | filter: $mdBranchFilter(searchTerm) | orderBy: item.name" class="md-2-line" select>
1818
<div class="md-branch-text">
1919
<h3>{{item.name}}</h3>
2020
<p>Item</p>

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ angular
1111

1212
/*@ngInject*/
1313
function mdTreeTheme($mdThemingProvider, TREE_THEME) {
14+
// register theme styles
1415
$mdThemingProvider.registerStyles(TREE_THEME);
1516
}

src/js/branch.js

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ angular
33
.directive('mdBranch', branchDirective);
44

55

6+
// checkbox html
67
var CHECKBOX_SELECTION_INDICATOR = angular.element('<div class="checkbox-container"><div class="checkbox-icon"></div></div>');
8+
// branch arrow icon svg
79
var 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) {

src/js/branchTemplates.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// TODO Implament branch templates
2+
// May need to manually transclude element
3+
14
angular
25
.module('angular-material-tree')
36
.directive('mdBranchTemplates', branchTemplateDirective);

src/js/service.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,17 @@ function treeService($mdUtil, $animateCss) {
1212
filterClose: filterClose
1313
};
1414

15+
// Connect scope and set it's state
16+
// animate branch open
1517
function open(branchElement, noAnimation) {
1618
if (!branchElement) { return; }
1719

1820
var element = angular.element(branchElement);
1921
var scope = element.scope();
2022
$mdUtil.reconnectScope(scope);
2123
scope.isOpen = true;
22-
scope.startWatching();
23-
if (noAnimation === true) { element.addClass('md-no-animation'); }
24+
scope.startWatching(); // watch model data
25+
if (noAnimation === true) { element.addClass('md-no-animation'); } // remove css transitions
2426

2527
$mdUtil.nextTick(function () {
2628
var container = angular.element(element[0].querySelector('.md-branch-container'));
@@ -41,14 +43,16 @@ function treeService($mdUtil, $animateCss) {
4143
});
4244
}
4345

46+
// disconnect scope and set it's state
47+
// animate branch closed
4448
function close(branchElement, noAnimation) {
4549
if (!branchElement) { return; }
4650

4751
var element = angular.element(branchElement);
4852
var scope = element.scope();
4953
scope.isOpen = false;
50-
scope.killWatching();
51-
if (noAnimation === true) { element.addClass('md-no-animation'); }
54+
scope.killWatching(); // stop watching model data
55+
if (noAnimation === true) { element.addClass('md-no-animation'); } // remove css transitions
5256

5357
$mdUtil.nextTick(function () {
5458
var container = angular.element(element[0].querySelector('.md-branch-container'));
@@ -68,11 +72,14 @@ function treeService($mdUtil, $animateCss) {
6872
});
6973
}
7074

75+
// used to animate branch open
7176
function getHeight(element) {
7277
return element[0].scrollHeight + 'px';
7378
}
7479

7580

81+
// open branch for filtering
82+
// No animations
7683
function filterOpen(block) {
7784
$mdUtil.reconnectScope(block.scope);
7885
block.scope.isOpen = true;
@@ -82,6 +89,8 @@ function treeService($mdUtil, $animateCss) {
8289
container.css('max-height', 'none');
8390
}
8491

92+
// close branch for filtering
93+
// No animations
8594
function filterClose(block) {
8695
$mdUtil.disconnectScope(block.scope);
8796
block.scope.isOpen = false;

0 commit comments

Comments
 (0)