Class: Sol::Bootstrap

Inherits:
Object
  • Object
show all
Defined in:
lib/webview/bootstrap.rb

Overview

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Bootstrap) initialize





44
45
46
47
# File 'lib/webview/bootstrap.rb', line 44

def initialize
  @specified = false  # scene has not yet been defined
  @root_grid = MDArray.string([1])  # base grid with one row
end

Instance Attribute Details

- (Object) root_grid (readonly)

Returns the value of attribute root_grid



37
38
39
# File 'lib/webview/bootstrap.rb', line 37

def root_grid
  @root_grid
end

- (Object) specified (readonly)

Returns the value of attribute specified



38
39
40
# File 'lib/webview/bootstrap.rb', line 38

def specified
  @specified
end

Instance Method Details

- (Object) []=(val)





61
62
63
64
65
# File 'lib/webview/bootstrap.rb', line 61

def []=(val)
  # when a grid is assigned to the root_grid, then the scene has been specified
  @specified = true
  @root_grid[0] = val
end

- (Object) add_grid(grid)





223
224
225
226
# File 'lib/webview/bootstrap.rb', line 223

def add_grid(grid)
  @specified = true
  @root_grid[0] = grid
end

- (Object) bootstrap





125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/webview/bootstrap.rb', line 125

def bootstrap
  
  container = "// Add new Bootstrap Container"
  # container = "container = d3.select(\"body\").append(\"div\")"
  container = "container = d3.select(\"last\").insert(\"div\")"
  container << ".attr(\"class\", \"container\")"
  container << ".attr(\"style\", \"font: 12px sans-serif;\");\n\n"

  # Add a title row
  container << "// Add a row for the dashboard title\n"
  container << "var title = container.append(\"div\").attr(\"class\", \"row\");\n"
  container << "title.attr(\"class\", \"col-sm-12\")"
  container << ".attr(\"id\", \"title\")"
  container << ".attr(\"align\", \"center\");\n"
  container << @title if @title

  container << "var main = container.append(\"div\").attr(\"class\", \"row\");\n"
  container << "main.attr(\"class\", \"col-sm-12\")"

  grid = @root_grid[0]
  if (grid.is_a? StringMDArray)
    raise "Grid should have rank of at most 2" if grid.get_rank > 2
    container << "\n " << traverse(grid, 12, "main")
  elsif
    p grid
    raise "Something wrong happened! Sorry."
  end

  container

end

- (Object) create_grid(charts_num, names, cols = 2)


Creates a grid of the appropriate size for storing the specified number of charts and name them. The grid will have cols columns and as many rows as necessary to store all charts. The columns width will be as large as possible.




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
# File 'lib/webview/bootstrap.rb', line 87

def create_grid(charts_num, names, cols = 2)

  raise "Then number of columns has to be an integer and not #{cols}" if 
    !cols.is_a? Integer

  # Not used yet... 
  # columns_width = (@max_width / cols).floor
  rows = 0

  if (charts_num <= cols)
    rows = 1
    grid = new_grid([charts_num, rows])
  else
    rows = (charts_num.to_f / cols).ceil
    grid = new_grid([rows, cols])
  end
  
  grid.each_with_counter do |cel, count|
    i = rows * count[0] + count[1]
    grid[*count] = (i < names.size)? names[i] : "__bootstrap_empty__"
  end
  
  add_grid(grid)

end

- (Object) new_grid(shape)


String MDArray is actually an Object MDArray, so, any object can be added to it




71
72
73
74
75
76
77
78
79
# File 'lib/webview/bootstrap.rb', line 71

def new_grid(shape)

  if (shape.size != 2)
    raise "Grid specification invalid - rank must be 2 and not: #{shape.size}"
  end

  MDArray.string(shape)

end

- (Boolean) specified?



Returns:

  • (Boolean)


53
54
55
# File 'lib/webview/bootstrap.rb', line 53

def specified?
  return @specified
end

- (Object) title=(title)





117
118
119
# File 'lib/webview/bootstrap.rb', line 117

def title=(title)
  @title = "title.append(\"h4\").text(\"#{title}\");"
end

- (Object) traverse(grid, size, parent = "container", g = 1)





161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/webview/bootstrap.rb', line 161

def traverse(grid, size, parent = "container", g = 1)

  rank = grid.get_rank
  shape = grid.get_shape
  span = size/shape[1]

  if (span < 1 || rank != 2)
    raise "Grid specification invalid with rank: #{rank} and span #{span}"
  end
  
  row = -1
  col = -1
  # cel = "g#{g}_#{row}"
  # container = "var #{cel} = #{parent}.append(\"div\").attr(\"class\", \"row\");\n"
  # parent = cel
  container = String.new
  push = String.new
  row_spec = String.new
  cel = String.new

  grid.each_with_counter do |val, counter|
    # new row
    if (counter[0] > row)
      col = -1    # reset the column counter
      row = counter[0]
      row_spec = "g#{g}_#{row}"
      container << "var #{row_spec} = #{parent}.append(\"div\").attr(\"class\", \"row\");\n"
      # cel = row_spec
      # parent = cel
    end
    # new column?
    # if (shape[1] > 1 && counter[1] > col)
    if (counter[1] > col)
      col = counter[1]
      cel = "#{row_spec}_#{col}"
      container << "var #{cel} = #{row_spec}.append(\"div\").attr(\"class\", \"col-sm-#{span}\");\n"
    end

    if (val.is_a? String)
      # leaf cel
      container << "#{cel}.attr(\"id\", \"#{val}Chart\");\n "
    elsif (val.is_a? StringMDArray)
      # new grid
      # p "calling traverse with #{g + 1}"
      push << traverse(val, span, cel, g + 1)
    end
  end

  container + push

end