class Weaver::Elements

Add accordion to elements

Add tables to elements

add modal dialog to elements

Add row and column elements

Base element class for all HTML elements

Public Class Methods

new(page, anchors) click to toggle source
# File lib/weaver/elements.rb, line 6
def initialize(page, anchors)
  @inner_content = []
  @anchors = anchors
  @page = page
end

Public Instance Methods

_button(options = {}, &block) click to toggle source
# File lib/weaver/elements.rb, line 280
def _button(options = {}, &block)
  anIcon = options[:icon]
  title = options[:title]

  if title.is_a? Hash
    options.merge! title
    title = anIcon
    anIcon = nil
  end

  style = options[:style] || :primary
  size = "btn-#{options[:size]}" if options[:size]
  blockstyle = 'btn-block' if options[:block]
  outline = 'btn-outline' if options[:outline]
  dim = 'dim' if options[:threedee]
  dim = 'dim btn-large-dim' if options[:bigthreedee]
  dim = 'btn-rounded' if options[:rounded]
  dim = 'btn-circle' if options[:circle]

  buttonOptions = {
    type: options[:type] || 'button',
    class: "btn btn-#{style} #{size} #{blockstyle} #{outline} #{dim}",
    id: options[:id]
  }

  if block
    closer = ''

    closer = '; return false;' if options[:nosubmit]

    action = Action.new(@page, @anchors, &block)
    buttonOptions[:onclick] = "#{action.name}(this)"
    buttonOptions[:onclick] = "#{action.name}(this, #{options[:data]})#{closer}" if options[:data]
    @page.scripts << action.generate
  end

  type = :button

  buttonOptions[:"data-toggle"] = 'button' if options[:toggle]
  type = :a if options[:toggle]

  method_missing type, buttonOptions do
    if title.is_a? Symbol
      icon title
    else
      icon anIcon if anIcon
      text ' ' if anIcon
      text title
    end
  end
end
accordion(&block) click to toggle source
# File lib/weaver/element_types/accordion.rb, line 117
def accordion(&block)
  acc = Accordion.new(@page, @anchors)
  acc.instance_eval(&block)

  @inner_content << acc.generate
end
background(&block) click to toggle source
# File lib/weaver/elements.rb, line 60
def background(&block)
  @page.background(block)
end
badge(label, options = {}) click to toggle source
# File lib/weaver/elements.rb, line 200
def badge(label, options = {})
  options[:type] ||= 'plain'

  kind = 'label'
  kind = 'badge' if options[:rounded]
  tag_options = options.clone
  tag_options[:class] = "#{kind} #{kind}-#{options[:type]}"

  span tag_options do
    text label
  end
end
big_button(anIcon, title = {}, options = {}, &block) click to toggle source
# File lib/weaver/elements.rb, line 352
def big_button(anIcon, title = {}, options = {}, &block)
  options[:size] = :lg
  options[:icon] = anIcon
  options[:title] = title
  _button(options, &block)
end
big_embossed_button(anIcon, title = {}, options = {}, &block) click to toggle source
# File lib/weaver/elements.rb, line 380
def big_embossed_button(anIcon, title = {}, options = {}, &block)
  options[:bigthreedee] = true
  options[:icon] = anIcon
  options[:title] = title
  _button(options, &block)
end
block_button(anIcon, title = {}, options = {}, &block) click to toggle source
# File lib/weaver/elements.rb, line 338
def block_button(anIcon, title = {}, options = {}, &block)
  options[:block] = true
  options[:icon] = anIcon
  options[:title] = title
  _button(options, &block)
end
breadcrumb(patharray) click to toggle source
center(content = nil, options = {}, &block) click to toggle source
# File lib/weaver/elements.rb, line 92
def center(content = nil, options = {}, &block)
  options[:style] = '' unless options[:style]

  options[:style] += '; text-align: center;'
  if !content
    div options, &block
  else
    div content, options, &block
  end
end
circle_button(anIcon, title = {}, options = {}, &block) click to toggle source
# File lib/weaver/elements.rb, line 394
def circle_button(anIcon, title = {}, options = {}, &block)
  options[:circle] = true
  options[:icon] = anIcon
  options[:title] = title
  _button(options, &block)
end
col(occupies, options = {}, &block) click to toggle source
# File lib/weaver/element_types/row.rb, line 114
def col(occupies, options = {}, &block)
  column = Column.new(occupies, @page, @anchors, options, &block)
  text column.generate
end
crossfade_image(image_normal, image_hover) click to toggle source
# File lib/weaver/elements.rb, line 149
def crossfade_image(image_normal, image_hover)
  div class: 'crossfade' do
    image image_hover, class: 'bottom'
    image image_normal, class: 'top'
  end
  image image_hover
  @page.request_css 'css/crossfade_style.css'
end
embossed_button(anIcon, title = {}, options = {}, &block) click to toggle source
# File lib/weaver/elements.rb, line 373
def embossed_button(anIcon, title = {}, options = {}, &block)
  options[:threedee] = true
  options[:icon] = anIcon
  options[:title] = title
  _button(options, &block)
end
generate() click to toggle source
# File lib/weaver/elements.rb, line 405
def generate
  @inner_content.join
end
half(&block) click to toggle source
# File lib/weaver/element_types/row.rb, line 81
def half(&block)
  opts =
    {
      xs: 12,
      sm: 12,
      md: 12,
      lg: 6
    }
  col(4, opts, &block)
end
ibox(options = {}, &block) click to toggle source
# File lib/weaver/elements.rb, line 86
def ibox(options = {}, &block)
  panel = Panel.new(@page, @anchors, options)
  panel.instance_eval(&block)
  @inner_content << panel.generate
end
icon(type) click to toggle source
# File lib/weaver/elements.rb, line 68
def icon(type)
  iconname = type.to_s.tr('_', '-')
  if type.is_a? Symbol
    i class: "fa fa-#{iconname}" do
    end
  else
    i class: 'fa' do
      text type
    end
  end
end
image(name, options = {}) click to toggle source
# File lib/weaver/elements.rb, line 128
def image(name, options = {})
  style = (options[:style]).to_s
  if options[:rounded_corners] == true
    style += ' border-radius: 8px'
  elsif options[:rounded_corners] == :top
    style += ' border-radius: 8px 8px 0px 0px'
  else
    style += " border-radius: #{options[:rounded_corners]}px" if options[:rounded_corners]

  end

  img_options = {
    class: "img-responsive #{options[:class]}",
    src: "#{@page.root}images/#{name}",
    style: style
  }
  img_options[:id] = options[:id] if options[:id]

  img img_options
end
jumbotron(options = {}, &block) click to toggle source
# File lib/weaver/elements.rb, line 260
def jumbotron(options = {}, &block)
  additional_style = ''

  if options[:background]
    additional_style += " background-image: url('#{@page.root}images/#{options[:background]}'); background-position: center center; background-size: cover;"
  end

  additional_style += " height: #{options[:height]}px;" if options[:height]

  if options[:min_height]
    additional_style += " min-height: #{options[:min_height]}px;"
  end

  if options[:max_height]
    additional_style += " max-height: #{options[:max_height]}px;"
  end

  div class: 'jumbotron', style: additional_style, &block
end
math(string) click to toggle source
# File lib/weaver/elements.rb, line 401
def math(string)
  text "$$$MATH$$$#{string}$$$ENDMATH$$$"
end
method_missing(name, *args, &block) click to toggle source
# File lib/weaver/elements.rb, line 12
def method_missing(name, *args, &block)
  tag = "<#{name} />"

  inner = args.shift if args[0].is_a? String
  if block
    elem = Elements.new(@page, @anchors)
    elem.instance_eval(&block)
    inner = elem.generate
  end

  if !inner

    options = args[0] || []
    opts = options.map { |key, value| "#{key}=\"#{value}\"" }.join ' '

    tag = "<#{name} #{opts} />"
  elsif args.empty?
    tag = "<#{name}>#{inner}</#{name}>"
  elsif (args.length == 1) && args[0].is_a?(Hash)
    options = args[0]
    opts = options.map { |key, value| "#{key}=\"#{value}\"" }.join ' '
    tag = "<#{name} #{opts}>#{inner}</#{name}>"
  end

  @inner_content << tag
  tag
end
modal(id = nil, &block) click to toggle source
normal_button(anIcon, title = {}, options = {}, &block) click to toggle source
# File lib/weaver/elements.rb, line 332
def normal_button(anIcon, title = {}, options = {}, &block)
  options[:icon] = anIcon
  options[:title] = title
  _button(options, &block)
end
on_page_load(script) click to toggle source
# File lib/weaver/elements.rb, line 52
def on_page_load(script)
  @page.on_page_load(script)
end
outline_button(anIcon, title = {}, options = {}, &block) click to toggle source
# File lib/weaver/elements.rb, line 345
def outline_button(anIcon, title = {}, options = {}, &block)
  options[:outline] = true
  options[:icon] = anIcon
  options[:title] = title
  _button(options, &block)
end
p(*args, &block) click to toggle source
# File lib/weaver/elements.rb, line 192
def p(*args, &block)
  method_missing(:p, *args, &block)
end
panel(title, &block) click to toggle source
# File lib/weaver/elements.rb, line 103
def panel(title, &block)
  div class: 'panel panel-default' do
    div class: 'panel-heading' do
      h5 title
    end
    div class: 'panel-body', &block
  end
end
quarter(&block) click to toggle source
# File lib/weaver/element_types/row.rb, line 103
def quarter(&block)
  opts =
    {
      xs: 12,
      sm: 12,
      md: 6,
      lg: 3
    }
  col(3, opts, &block)
end
request_css(path) click to toggle source
# File lib/weaver/elements.rb, line 48
def request_css(path)
  @page.request_css(path)
end
request_js(path) click to toggle source
# File lib/weaver/elements.rb, line 44
def request_js(path)
  @page.request_js(path)
end
root() click to toggle source
# File lib/weaver/elements.rb, line 40
def root
  @page.root
end
rounded_button(anIcon, title = {}, options = {}, &block) click to toggle source
# File lib/weaver/elements.rb, line 387
def rounded_button(anIcon, title = {}, options = {}, &block)
  options[:rounded] = true
  options[:icon] = anIcon
  options[:title] = title
  _button(options, &block)
end
row(options = {}, &block) click to toggle source
# File lib/weaver/element_types/row.rb, line 63
def row(options = {}, &block)
  options[:class] = 'row'
  div options do
    instance_eval(&block)
  end
end
small_button(anIcon, title = {}, options = {}, &block) click to toggle source
# File lib/weaver/elements.rb, line 359
def small_button(anIcon, title = {}, options = {}, &block)
  options[:size] = :sm
  options[:icon] = anIcon
  options[:title] = title
  _button(options, &block)
end
syntax(lang = :javascript, &block) click to toggle source
# File lib/weaver/elements.rb, line 119
def syntax(lang = :javascript, &block)
  code = Code.new(@page, @anchors, lang)
  code.instance_eval(&block)

  @inner_content << code.generate

  @page.scripts << code.generate_script
end
table(options = {}, &block) click to toggle source
# File lib/weaver/element_types/dynamic_table.rb, line 224
    def table(options = {}, &block)
      table_name = options[:id] || @page.create_anchor('table')
      table_style = ''

      table_style = options[:style] unless options[:style].nil?

      classname = 'table'

      classname += ' table-bordered' if options[:bordered]
      classname += ' table-hover' if options[:hover]
      classname += ' table-striped' if options[:striped]

      table_options = {
        class: classname,
        id: table_name,
        style: table_style
      }

      if options[:system] == :data_table
        @page.request_js 'js/plugins/dataTables/jquery.dataTables.js'
        @page.request_js 'js/plugins/dataTables/dataTables.bootstrap.js'
        @page.request_js 'js/plugins/dataTables/dataTables.responsive.js'
        @page.request_js 'js/plugins/dataTables/dataTables.tableTools.min.js'

        @page.request_css 'css/plugins/dataTables/dataTables.bootstrap.css'
        @page.request_css 'css/plugins/dataTables/dataTables.responsive.css'
        @page.request_css 'css/plugins/dataTables/dataTables.tableTools.min.css'

        @page.scripts << <<-DATATABLE_SCRIPT
                $('##{table_name}').DataTable();
        DATATABLE_SCRIPT
      end

      if options[:system] == :foo_table
        table_options[:"data-filtering"] = true
        table_options[:"data-sorting"] = true
        table_options[:"data-paging"] = true
        table_options[:"data-show-toggle"] = true
        table_options[:"data-toggle-column"] = 'last'

        table_options[:"data-paging-size"] = (options[:max_items_per_page] || 8).to_i.to_s
        table_options[:class] = table_options[:class] + ' toggle-arrow-tiny'

        @page.request_js 'js/plugins/footable/footable.all.min.js'

        @page.request_css 'css/plugins/footable/footable.core.css'

        @page.scripts << <<-DATATABLE_SCRIPT
                $('##{table_name}').footable({
                        paging: {
                                size: #{(options[:max_items_per_page] || 8).to_i}
                        }
                });
                $('##{table_name}').append(this.html).trigger('footable_redraw');



        DATATABLE_SCRIPT

        @page.onload_scripts << <<-SCRIPT
                                        SCRIPT
      end

      method_missing(:table, table_options, &block)
      ul class: 'pagination'
    end
table_from_hashes(hashes, options = {}) click to toggle source
# File lib/weaver/element_types/dynamic_table.rb, line 299
def table_from_hashes(hashes, options = {})
  keys = {}
  hashes.each do |hash|
    hash.each do |key, _value|
      keys[key] = ''
    end
  end

  table options do
    thead do
      keys.each do |key, _|
        th key.to_s
      end
    end

    tbody do
      hashes.each do |hash|
        tr do
          keys.each do |key, _|
            td (hash[key]).to_s || '&nbsp;'
          end
        end
      end
    end
  end
end
table_from_source(url, options = {}, &block) click to toggle source
# File lib/weaver/element_types/dynamic_table.rb, line 291
def table_from_source(url, options = {}, &block)
  dyn_table = DynamicTable.new(@page, @anchors, url, options, &block)

  text dyn_table.generate_table

  @page.scripts << dyn_table.generate_script
end
tabs(&block) click to toggle source
# File lib/weaver/elements.rb, line 112
def tabs(&block)
  tabs = Tabs.new(@page, @anchors)
  tabs.instance_eval(&block)

  @inner_content << tabs.generate
end
text(theText) click to toggle source
# File lib/weaver/elements.rb, line 196
def text(theText)
  @inner_content << theText
end
third(&block) click to toggle source
# File lib/weaver/element_types/row.rb, line 92
def third(&block)
  opts =
    {
      xs: 12,
      sm: 12,
      md: 4,
      lg: 4
    }
  col(4, opts, &block)
end
tiny_button(anIcon, title = {}, options = {}, &block) click to toggle source
# File lib/weaver/elements.rb, line 366
def tiny_button(anIcon, title = {}, options = {}, &block)
  options[:size] = :xs
  options[:icon] = anIcon
  options[:title] = title
  _button(options, &block)
end
twothirds(&block) click to toggle source
# File lib/weaver/element_types/row.rb, line 70
def twothirds(&block)
  opts =
    {
      xs: 12,
      sm: 12,
      md: 8,
      lg: 8
    }
  col(4, opts, &block)
end
wform(options = {}, &block) click to toggle source
# File lib/weaver/elements.rb, line 80
def wform(options = {}, &block)
  theform = Form.new(@page, @anchors, options, &block)
  @inner_content << theform.generate
  @page.scripts << theform.generate_script
end
widget(options = {}, &block) click to toggle source
# File lib/weaver/elements.rb, line 245
def widget(options = {}, &block)
  # gray-bg
  # white-bg
  # navy-bg
  # blue-bg
  # lazur-bg
  # yellow-bg
  # red-bg
  # black-bg

  color = "#{options[:color]}-bg" || 'navy-bg'

  div class: "widget style1 #{color}", &block
end
write_script_once(script) click to toggle source
# File lib/weaver/elements.rb, line 56
def write_script_once(script)
  @page.write_script_once(script)
end