]> git.openstreetmap.org Git - rails.git/blob - app/helpers/svg_helper.rb
Use Bootstrap toggle buttons for layers menu
[rails.git] / app / helpers / svg_helper.rb
1 module SvgHelper
2   def previous_page_svg_tag(**options)
3     adjacent_page_svg_tag(dir == "rtl" ? 1 : -1, **options)
4   end
5
6   def next_page_svg_tag(**options)
7     adjacent_page_svg_tag(dir == "rtl" ? -1 : 1, **options)
8   end
9
10   def key_svg_tag(**options)
11     border_width = options["border"] ? (options["border-width"] || 1) : 0
12     rect_attrs = {
13       :width => "100%",
14       :height => "100%",
15       :fill => options["fill"] || "none"
16     }
17     if border_width.positive?
18       rect_attrs[:x] = rect_attrs[:y] = format("%g", 0.5 * border_width)
19       rect_attrs[:width] = options["width"] - border_width
20       rect_attrs[:height] = options["height"] - border_width
21     end
22     svg_attrs = options.slice("width", "height", "opacity", :class)
23
24     tag.svg(**svg_attrs) do
25       horizontal = "H#{options['width']}"
26       concat tag.rect(**rect_attrs, **stroke_attrs(options, "border")) if options["fill"] || options["border"]
27       if options["line"]
28         y_middle = format("%g", 0.5 * options["height"])
29         concat tag.path(:d => "M0,#{y_middle} #{horizontal}", **stroke_attrs(options, "line"))
30       end
31       if options["casing"]
32         casing_width = options["casing-width"] || 1
33         y_top = format("%g", 0.5 * casing_width)
34         y_bottom = format("%g", options["height"] - (0.5 * casing_width))
35         concat tag.path(:d => "M0,#{y_top} #{horizontal} M0,#{y_bottom} #{horizontal}", **stroke_attrs(options, "casing"))
36       end
37     end
38   end
39
40   private
41
42   # returns "<" shape if side == -1; ">" if side == 1
43   def adjacent_page_svg_tag(side, **options)
44     count = options[:count] || 1
45     height = options[:height] || 15
46     pad = 2
47     segment = (0.5 * height) - pad
48     width = (segment + (2 * count * pad)).ceil
49     angled_line_data = "l#{side * segment},#{segment} l#{-side * segment},#{segment}"
50     path_data = Array.new(count) { |i| "M#{side * ((2 * i) + 1) * pad},#{pad} #{angled_line_data}" }.join(" ")
51     path_tag = tag.path :d => path_data, :fill => "none", :stroke => "currentColor", :"stroke-width" => 1.5
52     view_box = "#{-width} 0 #{width} #{height}" if side.negative?
53     tag.svg path_tag, :width => width, :height => height, :viewBox => view_box, :class => options[:class]
54   end
55
56   def stroke_attrs(attrs, prefix)
57     attrs.select { |key| key.start_with?(prefix) }.transform_keys { |key| key.delete_prefix(prefix).prepend("stroke") }
58   end
59 end