class TaskJuggler::ReportTableLegend

The ReportTableLegend models an output format independent legend for the ReportTable. It lists the graphical symbols used in the table together with a short textual description.

Attributes

showGanttItems[RW]

Public Class Methods

new() click to toggle source

Create a new ReportTableLegend object.

# File lib/taskjuggler/reports/ReportTableLegend.rb, line 25
def initialize
  @showGanttItems = false
  @ganttItems = []
  @calendarItems = []
end

Public Instance Methods

addCalendarItem(text, color) click to toggle source

Add another chart item to the legend. Make sure we don’t have any duplicates.

# File lib/taskjuggler/reports/ReportTableLegend.rb, line 39
def addCalendarItem(text, color)
  unless @calendarItems.include?([ text, color ])
    @calendarItems << [ text, color ]
  end
end
addGanttItem(text, color) click to toggle source

Add another Gantt item to the legend. Make sure we don’t have any duplicates.

# File lib/taskjuggler/reports/ReportTableLegend.rb, line 33
def addGanttItem(text, color)
  @ganttItems << [ text, color ] unless @ganttItems.include?([ text, color ])
end
to_html() click to toggle source

Convert the abstract description into HTML elements.

# File lib/taskjuggler/reports/ReportTableLegend.rb, line 46
def to_html
  return nil if !@showGanttItems && @ganttItems.empty? &&
                @calendarItems.empty?

  frame = XMLElement.new('div', 'class' => 'tj_table_legend_frame')
  frame << (legend = XMLElement.new('table', 'class' => 'tj_table_legend',
                                             'cellspacing' => '1'))

  legend << headlineToHTML('Gantt Chart Symbols:')
  # Generate the Gantt chart symbols
  if @showGanttItems
    legend << (row = XMLElement.new('tr', 'class' => 'tj_legend_row'))

    row << ganttItemToHTML(GanttContainer.new(15, 10, 35, 0),
                           'Container Task', 40)
    row << ganttItemToHTML(GanttTaskBar.new(nil, 15, 5, 35, 0),
                           'Normal Task', 40)
    row << ganttItemToHTML(GanttMilestone.new(15, 10, 0), 'Milestone', 20)
    row << XMLElement.new('td', 'class' => 'tj_legend_spacer')
  end

  legend << itemsToHTML(@ganttItems)

  legend << headlineToHTML('Calendar Symbols:')
  legend << itemsToHTML(@calendarItems)

  frame
end

Private Instance Methods

ganttItemToHTML(itemRef, name, width) click to toggle source

Turn the Gantt symbold descriptions into HTML elements.

# File lib/taskjuggler/reports/ReportTableLegend.rb, line 91
def ganttItemToHTML(itemRef, name, width)
  cells = []
  # Empty cell for margin first.
  cells << (item = XMLElement.new('td', 'class' => 'tj_legend_spacer'))
  # The symbol cell
  cells << (item = XMLElement.new('td', 'class' => 'tj_legend_item'))
  item << (symbol = XMLElement.new('div', 'class' => 'tj_legend_symbol',
                                   'style' => 'top:3px'))
  symbol << itemRef.to_html
  # The label cell
  cells << (item = XMLElement.new('td', 'class' => 'tj_legend_item'))
  item << (label = XMLElement.new('div', 'class' => 'tj_legend_label'))
  label << XMLText.new(name)

  cells
end
headlineToHTML(text) click to toggle source

In case we have both the calendar and the Gantt chart in the report element, we have to add description lines before the symbols. The two charts use the same colors for different meanings. This function generates the HTML version of the headlines.

# File lib/taskjuggler/reports/ReportTableLegend.rb, line 81
def headlineToHTML(text)
  unless @calendarItems.empty? || @ganttItems.empty?
    div = XMLElement.new('tr', 'class' => 'tj_legend_headline')
    div << XMLNamedText.new(text, 'td', 'colspan' => '10')
    return div
  end
  nil
end
itemToHTML(itemRef) click to toggle source

Turn a single color item into HTML elements.

# File lib/taskjuggler/reports/ReportTableLegend.rb, line 109
def itemToHTML(itemRef)
  cells = []
  # Empty cell for margin first.
  cells << XMLElement.new('td', 'class' => 'tj_legend_spacer')
  # The symbol cell
  cells << (item = XMLElement.new('td', 'class' => 'tj_legend_item'))
  item << (symbol = XMLElement.new('div', 'class' => 'tj_legend_symbol'))
  symbol << (box = XMLElement.new('div',
                                  'style' => 'position:relative; ' +
                                             'top:2px;' +
                                             'width:20px; height:15px'))
  box << (div = XMLElement.new('div', 'class' => 'loadstackframe',
                               'style' => 'position:absolute; ' +
                               'left:5px; width:16px; height:15px;'))
  div << XMLElement.new('div', 'class' => "#{itemRef[1]}",
                        'style' => 'position:absolute; ' +
                                   'left:1px; top:1px; ' +
                                   'width:14px; height:13px;')
  # The label cell
  cells << (item = XMLElement.new('td', 'class' => 'tj_legend_item'))
  item << (label = XMLElement.new('div', 'class' => 'tj_legend_label'))
  label << XMLText.new(itemRef[0])

  cells
end
itemsToHTML(items) click to toggle source

Turn the color items into HTML elements.

# File lib/taskjuggler/reports/ReportTableLegend.rb, line 136
def itemsToHTML(items)
  rows = []
  row = nil
  gridCells = ((items.length / 3) + (items.length % 3 != 0 ? 1 : 0)) * 3
  gridCells.times do |i|
    # We show no more than 3 items in a row.
    if i % 3 == 0
      rows << (row = XMLElement.new('tr', 'class' => 'tj_legend_row'))
    end

    # If we run out of items before the line is filled, we just insert
    # empty cells to fill the line.
    if i < items.length
      row << itemToHTML(items[i])
    else
      row << XMLElement.new('td', 'class' => 'tj_legend_item',
                                  'colspan' => '3')
    end
    if (i + 1) % 3 == 0
      # Append an empty cell at the end of each row.
      row << XMLElement.new('td', 'class' => 'tj_legend_spacer')
    end
  end
  rows
end