slightly improved treelist

This commit is contained in:
rolux 2012-04-09 10:41:06 +02:00
parent ffa8f1738a
commit baf0f72912

View file

@ -31,10 +31,7 @@ Ox.TreeList = function(options, self) {
selected: [],
width: 256
})
.options(options || {})
.bindEvent({
select: selectItem
});
.options(options || {});
if (self.options.data) {
self.options.items = [];
@ -62,10 +59,13 @@ Ox.TreeList = function(options, self) {
})
.bindEvent({
anyclick: clickItem,
select: selectItem,
toggle: toggleItems
})
);
self.options.selected.length && selectItem({ids: self.options.selected});
function clickItem(e) {
var $target = $(e.target),
$item, id, item;
@ -107,7 +107,7 @@ Ox.TreeList = function(options, self) {
.appendTo($item);
}
$('<div>')
.addClass('OxCell OxTarget'+(!data.items?' OxSelectable':''))
.addClass('OxCell OxTarget' + (!data.items ? ' OxSelectable' : ''))
.css({
width: (self.options.width - padding - 32 + !data.items * 16) + 'px'
})
@ -140,13 +140,13 @@ Ox.TreeList = function(options, self) {
function getParent(id, items) {
var ret;
Ox.forEach(items, function(item) {
if(item.items) {
if(Ox.getObjectById(item.items, id)) {
if (item.items) {
if (Ox.getObjectById(item.items, id)) {
ret = item.id;
} else {
ret = getParent(id, item.items);
}
if(ret) {
if (ret) {
return false;
}
}
@ -155,7 +155,6 @@ Ox.TreeList = function(options, self) {
}
function parseData(key, value) {
//Ox.Log('List', 'parseData', key, value)
var ret = {
expanded: false,
id: Ox.uid().toString(),
@ -199,8 +198,8 @@ Ox.TreeList = function(options, self) {
}
function selectItem(data) {
var id = data.ids[0], parent = id, parents=[];
while(parent = getParent(parent, self.options.items)) {
var id = data.ids[0], parent = id, parents = [];
while (parent = getParent(parent, self.options.items)) {
parents.push(parent);
}
parents = parents.reverse();
@ -208,7 +207,9 @@ Ox.TreeList = function(options, self) {
expanded: true,
ids: parents
});
self.$list.options({selected: data.ids})
}
function toggleItem(item, expanded) {
var $img, pos;
item.expanded = expanded;
@ -248,7 +249,9 @@ Ox.TreeList = function(options, self) {
if (key == 'data') {
// ...
} else if (key == 'selected') {
self.$list.options({selected: value});
//self.$list.options({selected: value});
selectItem({ids: value});
self.$list.scrollToSelection();
} else if (key == 'width') {
// ...
}