Class: Tiler::Widget

Inherits:
Object
  • Object
show all
Defined in:
lib/tiler/widget.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(panel) ⇒ Widget

Returns a new instance of Widget.



19
20
21
22
# File 'lib/tiler/widget.rb', line 19

def initialize(panel)
  @panel  = panel
  @config = panel.parsed_config
end

Class Attribute Details

.col_spansObject

Returns the value of attribute col_spans.



4
5
6
# File 'lib/tiler/widget.rb', line 4

def col_spans
  @col_spans
end

.default_configObject

Returns the value of attribute default_config.



4
5
6
# File 'lib/tiler/widget.rb', line 4

def default_config
  @default_config
end

.default_sizeObject

Returns the value of attribute default_size.



4
5
6
# File 'lib/tiler/widget.rb', line 4

def default_size
  @default_size
end

.labelObject

Returns the value of attribute label.



4
5
6
# File 'lib/tiler/widget.rb', line 4

def label
  @label
end

.max_sizeObject

Returns the value of attribute max_size.



4
5
6
# File 'lib/tiler/widget.rb', line 4

def max_size
  @max_size
end

.min_sizeObject

Returns the value of attribute min_size.



4
5
6
# File 'lib/tiler/widget.rb', line 4

def min_size
  @min_size
end

.partialObject

Returns the value of attribute partial.



4
5
6
# File 'lib/tiler/widget.rb', line 4

def partial
  @partial
end

.query_classObject

Returns the value of attribute query_class.



4
5
6
# File 'lib/tiler/widget.rb', line 4

def query_class
  @query_class
end

.typeObject

Returns the value of attribute type.



4
5
6
# File 'lib/tiler/widget.rb', line 4

def type
  @type
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



17
18
19
# File 'lib/tiler/widget.rb', line 17

def config
  @config
end

#panelObject (readonly)

Returns the value of attribute panel.



17
18
19
# File 'lib/tiler/widget.rb', line 17

def panel
  @panel
end

Class Method Details

.example_configObject

An example config object for this widget. Shown verbatim on the panel edit form so users can copy-paste a working starting point. Default falls back to the class-level default_config; override per widget for richer examples (e.g. specifying value_column / group_by for charts).



66
67
68
# File 'lib/tiler/widget.rb', line 66

def self.example_config
  default_config
end

.example_payloadObject

An example payload — one record’s JSON shape — that this widget can render against. Used to scaffold sample curl commands on the edit page. Default returns a minimal “ok” which most widgets can render with default config.



74
75
76
# File 'lib/tiler/widget.rb', line 74

def self.example_payload
  { "status" => "ok", "value" => 42, "duration" => 142.3 }
end

.example_previewObject

An example data hash matching the shape this widget’s partial expects. Wrapped under ‘…` and shown on the edit form so users can paste it into Config to test the widget without a data source. Return nil for config-only widgets (clock/text/iframe/image) — they render their own placeholders and don’t benefit from preview JSON.



83
84
85
# File 'lib/tiler/widget.rb', line 83

def self.example_preview
  nil
end

.supports_color_config?Boolean

Whether this widget’s edit form should expose the per-panel color picker. Charts/meters override this to true; default false hides the picker for widgets that don’t render a colored visualization (clock/text/iframe/image/list/table/comments/status_grid).

Returns:

  • (Boolean)


91
92
93
# File 'lib/tiler/widget.rb', line 91

def self.supports_color_config?
  false
end

.supports_palette_config?Boolean

Whether this widget’s edit form should expose the multi-color palette picker (array of hex). Multi-color charts (bar, pie, line) override to true; default false renders only the single-color picker (or none).

Returns:

  • (Boolean)


98
99
100
# File 'lib/tiler/widget.rb', line 98

def self.supports_palette_config?
  false
end

Instance Method Details

#dataObject



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/tiler/widget.rb', line 24

def data
  # Static-preview escape hatch: if config carries a `_preview` key, use
  # that as the widget's data and skip the query entirely. Lets users
  # paste arbitrary JSON into the Config field to preview a widget
  # without wiring up a data source.
  preview = config["_preview"]
  if preview.present?
    return preview.is_a?(Hash) ? preview.deep_symbolize_keys : preview
  end
  return nil if query_class.nil?
  query_class.new(panel, config).call
end

#empty?(data) ⇒ Boolean

Default rule for the global “configure your panel” empty state:

- Data-backed widget (query_class set) with no data source and no
  preview data → empty.
- Config-only widgets (clock/text/iframe/image: query_class nil)
  never trigger this — they render their own placeholders.

Subclasses override for stricter checks (e.g. chart widgets look for empty datasets even when a data source IS attached).

Returns:

  • (Boolean)


56
57
58
59
60
# File 'lib/tiler/widget.rb', line 56

def empty?(data)
  return false unless self.class.query_class
  return false if config["_preview"].present?
  panel.data_source.nil?
end

#labelObject



41
42
43
# File 'lib/tiler/widget.rb', line 41

def label
  self.class.label
end

#partialObject



37
38
39
# File 'lib/tiler/widget.rb', line 37

def partial
  self.class.partial || "tiler/widgets/#{self.class.type}"
end

#query_classObject



45
46
47
# File 'lib/tiler/widget.rb', line 45

def query_class
  self.class.query_class
end