From 15264c188f72d0a5b99d99fa3fff2990a0e6be14 Mon Sep 17 00:00:00 2001 From: rolux Date: Tue, 12 Jun 2012 13:13:23 +0200 Subject: [PATCH] add custom icons to Ox.TreeList --- source/Ox.UI/js/List/TreeList.js | 59 +++++++++++++++++--------------- 1 file changed, 32 insertions(+), 27 deletions(-) diff --git a/source/Ox.UI/js/List/TreeList.js b/source/Ox.UI/js/List/TreeList.js index daf54051..8246d37b 100644 --- a/source/Ox.UI/js/List/TreeList.js +++ b/source/Ox.UI/js/List/TreeList.js @@ -5,6 +5,7 @@ Ox.TreeList TreeList Object ([options[, self]]) -> TreeList Object options Options object data data to be parsed to items, needs documentation + icon Image URL, or function that returns an image object items array of items max maximum number of items that can be selected, -1 unlimited min minimum number of items that have to be selected @@ -21,6 +22,7 @@ Ox.TreeList = function(options, self) { var that = Ox.Element({}, self) .defaults({ data: null, + icon: null, items: [], max: 1, min: 0, @@ -87,44 +89,49 @@ Ox.TreeList = function(options, self) { } function constructItem(data) { - var $item = $('
').css({width: self.options.width + 'px'}), - padding = (data.level + !data.items) * 16 - 8; + var $item = $('
').css({ + width: self.options.width - Ox.UI.SCROLLBAR_SIZE + 'px' + }), + $cell = $('
').addClass('OxCell').css({width: '8px'}), + $icon = data.id ? getIcon(data.id, data.expanded || ( + data.items ? false : null + )) : null, + padding = data.level * 16 - 8; if (data.level || !data.items) { $('
') .addClass('OxCell OxTarget') - .css({ - width: padding + 'px' - }) - .appendTo($item); - } - if (data.items) { - $('
') - .addClass('OxCell') - .css({ - width: '8px' - }) - .append( - // fixme: need Ox.Icon() - $('') - .addClass('OxToggle') - .attr({ - src: Ox.UI.getImageURL( - 'symbol' + (data.expanded ? 'Down' : 'Right') - ) - }) - ) + .css({width: padding + 'px'}) .appendTo($item); } + $cell.appendTo($item); + $icon && $icon.addClass(data.items ? 'OxToggle' : 'OxTarget').appendTo($cell); $('
') .addClass('OxCell OxTarget' + (!data.items ? ' OxSelectable' : '')) .css({ - width: (self.options.width - padding - 32 + !data.items * 16) + 'px' + width: self.options.width - padding - 32 - Ox.UI.SCROLLBAR_SIZE + 'px' }) .html(data.title || '') .appendTo($item); return $item; } + function getIcon(id, expanded) { + var isFunction = Ox.isFunction(self.options.icon), + $icon = isFunction ? self.options.icon(id, expanded) : null; + if (!$icon) { + if (expanded === null) { + if (!$icon && self.options.icon && !isFunction) { + $icon = $('').attr({src: self.options.icon}); + } + } else { + $icon = $('').attr({src: Ox.UI.getImageURL( + 'symbol' + (expanded ? 'Down' : 'Right') + )}); + } + } + return $icon; + } + function getItemById(id, items, level) { var ret = null; items = items || self.options.items; @@ -238,9 +245,7 @@ Ox.TreeList = function(options, self) { }); //that.$element.value(item.id, 'expanded', expanded); $img.attr({ - src: Ox.UI.getImageURL( - 'symbol' + (expanded ? 'Down' : 'Right') - ) + src: getIcon(item.id, expanded).attr('src') }); expanded ? that.$element.addItems(