Class: Wee::Brush::SelectListTag

Inherits:
GenericTagBrush show all
Includes:
CallbackMixin
Defined in:
lib/wee/html_brushes.rb

Overview


Form - Select


Constant Summary collapse

HTML_TAG =
'select'.freeze

Constants inherited from GenericTagBrush

GenericTagBrush::EVENTS

Instance Attribute Summary

Attributes inherited from Wee::Brush

#canvas, #document

Instance Method Summary collapse

Methods included from CallbackMixin

#call, #callback, #callback_method

Methods inherited from GenericTagBrush

#callback_on, #get_oid, html_attr, #javascript_on, #oid, #onclick_callback, #onclick_javascript, #ondblclick_callback, #update_component_on, #update_on

Methods inherited from Wee::Brush

#close, nesting?, #setup

Constructor Details

#initialize(items) ⇒ SelectListTag

Returns a new instance of SelectListTag.



571
572
573
574
# File 'lib/wee/html_brushes.rb', line 571

def initialize(items)
  super(HTML_TAG)
  @items = items
end

Instance Method Details

#__callbackObject



599
600
601
602
603
604
605
# File 'lib/wee/html_brushes.rb', line 599

def __callback
  #
  # A callback was specified. We have to wrap it inside another
  # callback, as we want to perform some additional actions.
  #
  name(@canvas.register_callback(:input, method(:handler)) + "[]") 
end

#items(items) ⇒ Object



576
577
578
579
# File 'lib/wee/html_brushes.rb', line 576

def items(items)
  @items = items
  self
end

#labels(arg = nil, &block) ⇒ Object

Raises:

  • (ArgumentError)


587
588
589
590
591
592
593
594
595
# File 'lib/wee/html_brushes.rb', line 587

def labels(arg=nil, &block)
  raise ArgumentError if arg and block
  if block
    @labels = proc {|i| block.call(@items[i])}
  else
    @labels = arg
  end
  self
end

#selected(arg = nil, &block) ⇒ Object

Raises:

  • (ArgumentError)


581
582
583
584
585
# File 'lib/wee/html_brushes.rb', line 581

def selected(arg=nil, &block)
  raise ArgumentError if arg and block
  @selected = block || arg
  self
end

#withObject



625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
# File 'lib/wee/html_brushes.rb', line 625

def with
  @labels ||= @items.collect {|i| i.to_s}

  if @attributes.has_key?(:multiple)
    @selected ||= Array.new
    meth = @selected.kind_of?(Proc) ? (:call) : (:include?)
  else
    meth = @selected.kind_of?(Proc) ? (:call) : (:==)
  end

  super {
    @items.each_index do |i|
      @canvas.option.value(i).selected(@selected.send(meth, @items[i])).with(@labels[i])
    end 
  }
end