summaryrefslogtreecommitdiff
path: root/lib/postrunner/ActivityLink.rb
blob: 77898e679105ffd0e67786c76d2537a2cdfc8687 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env ruby -w
# encoding: UTF-8
#
# = ActivityLink.rb -- PostRunner - Manage the data from your Garmin sport devices.
#
# Copyright (c) 2014, 2015 by Chris Schlaeger <cs@taskjuggler.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#

require 'postrunner/HTMLBuilder'

module PostRunner

  # Generates the name of an Activity with a link to the ActivityReport.
  # Optionally, an icon can be shown for Activities that contain a current
  # personal record.
  class ActivityLink

    def initialize(activity, show_record_icon = false)
      @activity = activity
      @show_record_icon = show_record_icon
    end

    # Add the ActivityLink as HTML Elements to the document.
    # @param doc [HTMLBuilder] XML Document
    def to_html(doc)
      doc.unique(:activitylink_style) { doc.style(style) }

      doc.a(@activity.name, { :class => 'activity_link',
                              :href => @activity.html_file_name(false) })
      if @show_record_icon && @activity.has_records?
        doc.img(nil, { :src => 'icons/record-small.png',
                       :style => 'vertical-align:middle' })
      end
    end

    # Convert the ActivityLink into a plain text form. Return the first 20
    # characters of the Activity name.
    def to_s
      @activity.name[0..19]
    end

    private

    def style
      <<EOT
.activity_link {
  padding: 0px 3px 0px 3px;
}
EOT
    end

  end

end