Skip to content

Commit d76eced

Browse files
Ben RubinBen Rubin
authored andcommitted
start transferring branch controller functionality to tree(to many issues managing it via a branch controller)
1 parent 14de247 commit d76eced

File tree

1 file changed

+154
-36
lines changed

1 file changed

+154
-36
lines changed

src/js/tree.js

Lines changed: 154 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ function treeDirective($mdTheming, $mdUtil) {
3737
var branches = {};
3838

3939
vm.selected = {};
40+
vm.opened = {};
4041
vm.registerBranch = registerBranch;
4142
vm.unregisterBranch = unregisterBranch;
4243
vm.toggleSelect = toggleSelect;
@@ -58,6 +59,9 @@ function treeDirective($mdTheming, $mdUtil) {
5859
return !value || value.length === 0;
5960
};
6061

62+
63+
element.on('click', handleClicks);
64+
6165
function validateArray(modelValue, viewValue) {
6266
return angular.isArray(modelValue || viewValue || []);
6367
}
@@ -87,59 +91,94 @@ function treeDirective($mdTheming, $mdUtil) {
8791
return value;
8892
}
8993

90-
91-
function registerBranch(hashKey, ctrl) {
92-
if (branches[hashKey] !== undefined) {
93-
console.warn('This branch was already registered, ignoring.');
94-
return;
95-
}
96-
branches[hashKey] = ctrl;
97-
}
98-
99-
function unregisterBranch(hashKey) {
100-
if (branches[branch] === undefined) { return; }
101-
branches[branch] = undefined;
102-
delete branches[branch];
103-
}
104-
105-
function toggleSelect(selected, hashKey, value) {
106-
if (selected) {
107-
select(hashKey, value);
94+
function toggleSelect(isSelected, hashKey, hashValue) {
95+
if (isSelected) {
96+
select(hashKey, hashValue);
10897
} else {
10998
deselect(hashKey);
11099
}
100+
111101
// TODO update model without calling select on branches
112102
refreshViewValue();
103+
// TODO render tree
104+
}
105+
function select(hashKey, hashValue) {
106+
// handleSelectionConflicts(hashKey);
107+
vm.selected[hashKey] = hashedValue;
108+
// TODO render tree
109+
}
110+
function deselect(hashKey) {
111+
delete vm.selected[hashKey];
112+
// TODO render tree
113113
}
114-
115114
function deselectAll() {
116115
Object.keys(branches).forEach(deselect);
117116
}
118117

119-
function select(hashKey, hashedValue) {
120-
var depth;
121-
var branch = branches[hashKey];
122-
if (branch !== undefined) {
123-
handleSelectionConflicts(branch);
124-
branch.setSelected(true);
125-
vm.selected[hashKey] = hashedValue;
118+
function toggleOpen(hashKey, hashedValue) {
119+
if (!vm.opened[hashKey]) {
120+
vm.opened[hashKey] = hashedValue;
121+
} else {
122+
delete vm.opened[hashKey];
126123
}
124+
// TODO render tree
125+
// dosconnect/reconnect scope
127126
}
128127

129-
function deselect(hashKey) {
130-
var branch = branches[hashKey];
131-
if (branch !== undefined) { branch.setSelected(false); }
132-
vm.selected[hashKey] = undefined;
133-
delete vm.selected[hashKey];
134-
}
128+
// function registerBranch(hashKey, ctrl) {
129+
// if (branches[hashKey] !== undefined) {
130+
// console.warn('This branch was already registered, ignoring.');
131+
// return;
132+
// }
133+
// branches[hashKey] = ctrl;
134+
// }
135+
136+
// function unregisterBranch(hashKey) {
137+
// if (branches[branch] === undefined) { return; }
138+
// branches[branch] = undefined;
139+
// delete branches[branch];
140+
// }
141+
142+
// function toggleSelect(selected, hashKey, value) {
143+
// if (selected) {
144+
// select(hashKey, value);
145+
// } else {
146+
// deselect(hashKey);
147+
// }
148+
// // TODO update model without calling select on branches
149+
// refreshViewValue();
150+
// }
151+
152+
// function deselectAll() {
153+
// Object.keys(branches).forEach(deselect);
154+
// }
155+
156+
// function select(hashKey, hashedValue) {
157+
// var branch = branches[hashKey];
158+
// if (branch !== undefined) {
159+
// handleSelectionConflicts(branch);
160+
// // branch.setSelected(true);
161+
// vm.selected[hashKey] = hashedValue;
162+
// }
163+
// }
164+
165+
// function deselect(hashKey) {
166+
// var branch = branches[hashKey];
167+
// // if (branch !== undefined) { branch.setSelected(false); }
168+
// vm.selected[hashKey] = undefined;
169+
// delete vm.selected[hashKey];
170+
// }
171+
135172

136173
// handle selection restrictions set by `[restrict-selection]` attr
137-
function handleSelectionConflicts(branch) {
174+
// TODO how do i invoke this if there is no controller to call
175+
// could add $$depth to data
176+
function handleSelectionConflicts() {
138177
var restictions = getSelectionRestrictions();
139178
if (restictions.single) { deselectAll(); }
140-
var depth = branch.getDepth();
141-
var conflictingDepths = Object.keys(vm.selected).filter(function (hashKey) {
142-
return branches[hashKey].getDepth() !== depth;
179+
// var depth = getDepth(hashKey);
180+
var conflictingDepths = Object.keys(vm.selected).filter(function (_hashKey) {
181+
return branches[_hashKey].getDepth() !== depth;
143182
});
144183
if (restictions.depth && conflictingDepths.length) {
145184
conflictingDepths.forEach(deselect);
@@ -158,6 +197,7 @@ function treeDirective($mdTheming, $mdUtil) {
158197
return selectionRestictions;
159198
}
160199

200+
161201
function refreshViewValue() {
162202
var branchValue;
163203
var newValue;
@@ -182,5 +222,83 @@ function treeDirective($mdTheming, $mdUtil) {
182222
vm.ngModel.$render();
183223
}
184224
}
225+
226+
227+
228+
// -- Clicks --
229+
230+
function handleClicks(e) {
231+
var closest = getClosest(e.target); // closest clickable element (arrow, checkbox, branch)
232+
var branch = getBranch(closest);
233+
if (!branch) { return; };
234+
var branchScope = angular.element(branch).scope();
235+
var item = branchScope[branchScope.repeatName];
236+
237+
// toggle branch
238+
if (isArrow(closest)) {
239+
toggleBranchClick(e, item);
240+
return;
241+
}
242+
243+
if (isSelectOn(branch)) {
244+
var selected = isSelected(branch);
245+
var item = branchScope[branchScope.repeatName];
246+
247+
if (isCheckbox(closest)) {
248+
if (Object.keys(selected).length > 1) { selected = false; }
249+
deselectAll();
250+
}
251+
252+
branch.setAttribute('selected', selected);
253+
branchItem.$$selected = !branchItem.$$selected;
254+
toggleSelect(selected, hashGetter(item), item);
255+
e.stopPropagation();
256+
} else {
257+
toggleBranchClick(e, item);
258+
}
259+
}
260+
261+
// set open state
262+
function toggleBranchClick(e, branchItem) {
263+
toggleOpen(hashGetter(branchItem), branchItem);
264+
e.stopPropagation();
265+
}
266+
267+
function getClosest(el) {
268+
if (valid(el)) { return el; }
269+
var parent = el.parentNode;
270+
while (parent && parent !== document.body) {
271+
if (valid(parent)) { return parent; }
272+
parent = parent.parentNode;
273+
}
274+
return null;
275+
276+
function valid(el) {
277+
return el.nodeName === 'MD-BRANCH' || el.classList.contains('md-branch-icon-container') || el.classList.contains('checkbox-container');
278+
}
279+
}
280+
281+
function getBranch(el) {
282+
if (!el) { return null; }
283+
if (el.nodeName === 'MD-BRANCH') { return el; }
284+
var parent = el.parentNode;
285+
while (parent && parent !== document.body) {
286+
if (parent.nodeName === 'MD-BRANCH') { return parent; }
287+
parent = parent.parentNode;
288+
}
289+
return null;
290+
}
291+
292+
function isArrow(el) {
293+
return el.classList.contains('md-branch-icon-container');
294+
}
295+
296+
function isSelectOn(el) {
297+
return el.hasAttribute('select');
298+
}
299+
300+
function isSelected() {
301+
return el.hasAttribute('selected');
302+
}
185303
}
186304
}

0 commit comments

Comments
 (0)