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

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

#get_oid, html_attr, #oid, #onclick_callback, #onclick_javascript, #onclick_update_callback, #onclick_update_self_callback, #ondblclick_callback

Methods inherited from Wee::Brush

#close, nesting?, #setup

Constructor Details

#initialize(items) ⇒ SelectListTag

Returns a new instance of SelectListTag.



524
525
526
527
# File 'lib/wee/html_brushes.rb', line 524

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

Instance Method Details

#__callbackObject



552
553
554
555
556
557
558
# File 'lib/wee/html_brushes.rb', line 552

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



529
530
531
532
# File 'lib/wee/html_brushes.rb', line 529

def items(items)
  @items = items
  self
end

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

Raises:

  • (ArgumentError)


540
541
542
543
544
545
546
547
548
# File 'lib/wee/html_brushes.rb', line 540

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)


534
535
536
537
538
# File 'lib/wee/html_brushes.rb', line 534

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

#withObject



578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
# File 'lib/wee/html_brushes.rb', line 578

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