Skip to content

Commit 10534f8

Browse files
Ben RubinBen Rubin
authored andcommitted
add selection restrictions for depth and single select
1 parent 02f1cd1 commit 10534f8

File tree

3 files changed

+48
-9
lines changed

3 files changed

+48
-9
lines changed

docsApp/pages/home/home.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<div layout="row">
2-
<md-tree ng-model="vm.selectedItems" flex>
2+
<md-tree ng-model="vm.selectedItems" restrict-selection="depth,single" flex>
33
<md-branch branch-repeat="location in vm.locationData" class="md-2-line" select>
44
<div class="md-branch-text">
55
<h3>{{location.name}}</h3>

src/js/branch.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,13 +243,11 @@ function branchDirective($parse, $document, $compile) {
243243

244244
// --- Controller ---
245245

246-
var cid = 0;
247246
/*@ngInject*/
248247
function branchController($scope, $mdUtil, $animateCss) {
249248
/*jshint validthis: true*/
250249
var vm = this;
251250
var isOpen = false;
252-
var _id = cid++;
253251

254252
// injected $element is holds refernce to the comment. heres how to get arround this
255253
var $element = $scope.$element;
@@ -333,7 +331,6 @@ function branchController($scope, $mdUtil, $animateCss) {
333331
function open(noAnimation) {
334332
if (isOpen) { return; }
335333
isOpen = true;
336-
// findTree();
337334
reconnectScope();
338335
vm.startWatching();
339336
$element.toggleClass('md-no-animation', noAnimation || false);
@@ -402,11 +399,24 @@ function branchController($scope, $mdUtil, $animateCss) {
402399
// register branch if tree controller is accesable
403400
function registerBranch() {
404401
vm.treeCtrl.registerBranch(vm.treeCtrl.hashGetter($scope[$scope.repeatName]), {
405-
setSelected: setSelected
402+
setSelected: setSelected,
403+
getDepth: getDepth
406404
});
407405
setSelected(vm.treeCtrl.selected[vm.treeCtrl.hashGetter($scope[$scope.repeatName])] !== undefined);
408406
}
409407

408+
function getDepth() {
409+
if ($scope.$depth) { return $scope.$depth; }
410+
var depth = 0;
411+
var parent = $element[0].parentNode;
412+
while (parent && parent !== document.body) {
413+
if (parent.nodeName === 'MD-TREE') { break; }
414+
if (parent.nodeName === 'MD-BRANCH') { depth += 1; }
415+
parent = parent.parentNode;
416+
}
417+
return $scope.$depth = depth;
418+
}
419+
410420

411421
// remove scope from parents reference so it is not used in digest
412422
function disconnectScope() {

src/js/tree.js

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)