[Tree] Begin handling selection state

This commit is contained in:
Victor Woeltjen
2016-03-11 17:00:55 -08:00
parent 4a8222a152
commit 03ab3bddc4
3 changed files with 50 additions and 4 deletions

View File

@ -30,11 +30,11 @@ define([
this.callbacks = []; this.callbacks = [];
this.el = $(toggleTemplate); this.el = $(toggleTemplate);
this.el.on('click', function () { this.el.on('click', function () {
this.model(!this.expanded); this.value(!this.expanded);
}.bind(this)); }.bind(this));
} }
ToggleView.prototype.model = function (state) { ToggleView.prototype.value = function (state) {
this.expanded = state; this.expanded = state;
if (state) { if (state) {

View File

@ -76,6 +76,36 @@ define([
} }
}; };
TreeNodeView.prototype.value = function (domainObject) {
var activeIdPath = getIdPath(this.activeObject),
selectedIdPath = getIdPath(domainObject);
if (this.onSelectionPath) {
this.li.find('.tree-item').eq(0).removeClass('selected');
if (this.subtreeView) {
this.subtreeView.value(undefined);
}
}
this.onSelectionPath =
!!domainObject &&
!!this.activeObject &&
(activeIdPath.length <= selectedIdPath.length) &&
activeIdPath.every(function (id, index) {
return selectedIdPath[index] === id;
});
if (this.onSelectionPath) {
if (activeIdPath.length === selectedIdPath.length) {
this.li.find('.tree-item').eq(0).addClass('selected');
} else {
// Expand to reveal the selection
this.toggleView.value(true);
this.subtreeView.value(domainObject);
}
}
};
/** /**
* *
* @returns {HTMLElement[]} * @returns {HTMLElement[]}
@ -86,4 +116,4 @@ define([
return TreeNodeView; return TreeNodeView;
}); });

View File

@ -30,6 +30,7 @@ define([
function TreeView() { function TreeView() {
this.ul = $('<ul class="tree"></ul>'); this.ul = $('<ul class="tree"></ul>');
this.nodeViews = []; this.nodeViews = [];
this.callbacks = [];
} }
function newTreeView() { function newTreeView() {
@ -62,6 +63,7 @@ define([
if (domainObject === self.activeObject) { if (domainObject === self.activeObject) {
self.setSize(domainObjects.length); self.setSize(domainObjects.length);
domainObjects.forEach(addNode); domainObjects.forEach(addNode);
self.updateNodeViewSelection();
} }
} }
@ -87,6 +89,20 @@ define([
} }
}; };
TreeView.prototype.updateNodeViewSelection = function () {
this.nodeViews.forEach(function (nodeView) {
nodeView.value(this.selectedObject);
});
};
TreeView.prototype.value = function (domainObject) {
this.selectedObject = domainObject;
this.updateNodeViewSelection();
this.callbacks.forEach(function (callback) {
callback(domainObject);
});
};
/** /**
* *
* @returns {HTMLElement[]} * @returns {HTMLElement[]}
@ -97,4 +113,4 @@ define([
return TreeView; return TreeView;
}); });