File: panel.rb

package info (click to toggle)
ruby-rubyvis 0.6.1%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 1,808 kB
  • ctags: 679
  • sloc: ruby: 11,114; makefile: 2
file content (138 lines) | stat: -rw-r--r-- 3,281 bytes parent folder | download | duplicates (3)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
module Rubyvis
  # Alias for Rubyvis::Panel
  def self.Panel
    Rubyvis::Panel
  end
  class Panel < Bar
    def type
      "panel"
    end

    @properties=Bar.properties.dup
    attr_accessor_dsl :transform, :overflow, :canvas
    attr_accessor :children, :root
    def initialize
      @children=[]
      @root=self
      super
      
    end
    def children_inspect(level=0)
      out=[]
      @children.each do |c|
        out << ("  "*level)+"- #{c.type} (#{c.object_id}) proto:#{c.proto.object_id} target:#{c.target.object_id}"
        if c.respond_to? :children and c.children
          out << c.children_inspect(level+1)
        end
      end
      out

    end

    def self.defaults
      Panel.new.mark_extend(Bar.defaults).fill_style(nil).overflow('visible')
    end
    def add(type)
      child=type.new
      child.parent=self
      child.root=root
      child.child_index=children.size
      children.push(child)
      child
    end

    attr_reader :_canvas
    def bind
      super
      children.each {|c|
        c.bind()
      }
    end
    def anchor(name)
      anchor=mark_anchor(name)
      anchor.parent=self
      anchor
    end
    def build_instance(s)
      super(s)
      return if !s.visible
      s.children=[] if !s.children
      scale=self.scale * s.transform.k
      n=self.children.size
      Mark.index=-1
      n.times {|i|
        child=children[i]
        child.scene=s.children[i]
        child.scale=scale
        child.build
      }
      n.times {|i|
        child=children[i]
        s.children[i]=child.scene
        child.scene=nil
        child.scale=nil
      }
      s.children=s.children[0,n]

    end

    def to_svg
      if Rubyvis.xml_engine==:nokogiri
        @_canvas.sort.map {|v|
          v[1].get_element(1).to_xml(:indent => 5, :encoding => 'UTF-8')
        }.join
      else
        @_canvas.sort.map {|v|
          bar = REXML::Formatters::Default.new
          out = String.new
          bar.write(v[1].elements[1], out)
        }.join
      end
    end
    def build_implied(s)
      panel_build_implied(s)
    end
    def panel_build_implied(s)
      if(!self.parent)
        c=s.canvas
        if(c)
          if(c._panel!=self)
            c._panel=self
            c.delete_if? {true}
          end
          if s.width.nil?
            w=Rubyvis.css(c,'width')
            s.width=w - s.left - s.right
          end
          if s.height.nil?
            w=Rubyvis.css(c,'height')
            s.height=h - s.top - s.bottom
          end

        else
          @_canvas||={}
          cache=@_canvas
          if(!(c=cache[self.index]))
          
          if Rubyvis.xml_engine==:nokogiri
            document=Nokogiri::XML::Document.new
            document.root=document.create_element('document')
            Rubyvis.nokogiri_document(document)
          else
            document=REXML::Document.new
            document.add_element("document")            
          end
            cache[self.index]=document.root
            c=cache[self.index]
            
            #c=cache[self.index]=Rubyvis.document.add_element('span')
          end
        end
        s.canvas=c
      end
      s.transform=Rubyvis.Transform.identity if (s.transform.nil?)
      mark_build_implied(s)
    end

  end
end