Class: Aurita::GUI::Selection_List_Field

Inherits:
Options_Field show all
Defined in:
lib/aurita-gui/form/selection_list.rb

Overview

A selection list maintains a list of available options and a select field of further available options.

Example:

Selection_List_Field.new(:name => :the_list, 
                         :value => ['10','30' ]          # Active selection from options
                         :options => { '10' => :blue,    # List of all options
                                       '20' => :red, 
                                       '30' => :green }

In the example, any combination of ‘blue’, ‘red’ and ‘green’ could be selected, here it is ‘blue’ and ‘green’. The select field contains 20 => ‘red’, the only additionally available option (as it is not already set in value).

Use case: Assign user to categories. Options is all available categories, value is an array of category ids already assigned to this user.

Customization

You can override the Form_Field class rendering the option list elements, as well as the Form_Field class rendering the select field.

Use #option_field_decorator to override rendering of option fields. Default is Selection_List_Option_Field.

Example for custom option field decorator:

class Delete_Option_Decorator < Form_Field
  def element
    HTML.div { 
      HTML.img(:src => '/images/delete.png') + @label
    }
  end
end

selection_list.option_field_decorator = Delete_Option_Decorator

Example for custom select field class:

Note that Selection_List_Field will initialize key / label map of available options.

class Ajax_Selection_Field < Form_Field
  def initialize(params={})
    super(params)
    params[:onchange] = js.do_something.ajaxian.with(:this, @options)
  end
  def element
    HTML.div { 
      Input_Field.new(@attrib) 
    }
  end
end

selection_list.select_field_class = Ajax_Selection_Field

Instance Attribute Summary collapse

Attributes inherited from Options_Field

#option_labels, #option_values, #value

Attributes inherited from Form_Field

#data_type, #form, #hidden, #hint, #invalid, #label, #required, #type, #value

Attributes inherited from Element

#attrib, #force_closing_tag, #gui_element_id, #parent, #tag

Instance Method Summary collapse

Methods inherited from Options_Field

#[], #[]=, #add_option, #content, #option_hash, #options

Methods inherited from Form_Field

#disable!, #disabled=, #editable!, #enable!, #hidden?, #hide!, #invalid!, #invalid?, #optional!, #readonly!, #readonly=, #readonly?, #required!, #required?, #show!, #to_hidden_field, #to_s

Methods inherited from Element

#+, #<<, #[], #[]=, #add_class, #aurita_gui_element, #clear_floating, #css_classes, #find_by_dom_id, #get_content, #has_content?, #id, #id=, #inspect, #js_init_code, #length, #method_missing, #recurse, #remove_class, #set_content, #string, #swap, #to_ary, #touch, #touched?, #type=, #untouch

Methods included from Marshal_Helper_Class_Methods

#marshal_load

Methods included from Marshal_Helper

#marshal_dump

Constructor Details

#initialize(params = {}) ⇒ Selection_List_Field

Returns a new instance of Selection_List_Field.



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/aurita-gui/form/selection_list.rb', line 93

def initialize(params={})
  @option_field_decorator ||= params[:option_field]
  @select_field_class     ||= params[:select_field]
  @option_field_decorator ||= Selection_List_Option_Field
  @select_field_class     ||= Select_Field
  @selectable_options     ||= params[:selectable_options]
  @selectable_options     ||= []

  params.delete(:option_field)
  params.delete(:select_field)
  params.delete(:selectable_options)
  super(params)
  set_value(@value) 
  add_css_class(:selection_list_field)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Aurita::GUI::Element

Instance Attribute Details

#option_field_decoratorObject

Returns the value of attribute option_field_decorator.



91
92
93
# File 'lib/aurita-gui/form/selection_list.rb', line 91

def option_field_decorator
  @option_field_decorator
end

#select_field_classObject

Returns the value of attribute select_field_class.



91
92
93
# File 'lib/aurita-gui/form/selection_list.rb', line 91

def select_field_class
  @select_field_class
end

#selectable_optionsObject

Returns the value of attribute selectable_options.



91
92
93
# File 'lib/aurita-gui/form/selection_list.rb', line 91

def selectable_options
  @selectable_options
end

Instance Method Details

#elementObject

Renders list of active options, as well as select field element containing additionally available options.



154
155
156
157
158
159
160
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
# File 'lib/aurita-gui/form/selection_list.rb', line 154

def element
  select_options = []
  select_option_ids = []
  @selectable_options.each { |v|
    select_option_ids << v 
    select_options << @option_labels[v]
  }
  select_options.fields = select_option_ids 

  base_id   = @attrib[:id]
  base_id ||= @attrib[:name]
  
  if @value && @value.length > 0 then
    HTML.div(@attrib) { 
      HTML.ul(:id => "#{base_id}_selected_options") { 
        option_elements()
      } + 
      @select_field_class.new(:id      => "#{base_id}_select", 
                              :options => select_options, 
                              :parent  => self, 
                              :name    => "#{@attrib[:name]}" ) 
    }
  else
    HTML.div(@attrib) { 
      HTML.ul(:id => "#{base_id}_selected_options", :force_closing_tag => true)  + 
      @select_field_class.new(:id      => "#{base_id}_select", 
                              :options => select_options, 
                              :parent  => self, 
                              :name    => "#{@attrib[:name]}" ) 
    }
  end
end

#option_elementsObject

Returns array of available options, decorated by @option_field_decorator (see comments on Selection_List_Field).



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/aurita-gui/form/selection_list.rb', line 133

def option_elements
  base_id   = @attrib[:id]
  base_id ||= @attrib[:name]
  elements = []
  options().each_pair { |opt_value, opt_label|
    selected = @value.map { |v| v.to_s }.include?(opt_value.to_s)
    if selected then
      elements << HTML.li(:id => "#{base_id}_#{opt_value}") { 
        @option_field_decorator.new(:name   => @attrib[:name], 
                                    :value  => opt_value, 
                                    :label  => opt_label, 
                                    :parent => self)
      }
    end
  }
  elements
end

#options=(options) ⇒ Object

Set list of all options as value / label hash.



110
111
112
113
114
115
116
117
# File 'lib/aurita-gui/form/selection_list.rb', line 110

def options=(options)
  super(options)
  if !@selectable_options || @selectable_options.length == 0 then
    options().each_pair { |value, name|
      @selectable_options << value unless (@value.is_a?(Array) && @value.map { |v| v.to_s }.include?(value.to_s))
    }
  end
end

#readonly_elementObject



187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/aurita-gui/form/selection_list.rb', line 187

def readonly_element
  base_id   = @attrib[:id]
  base_id ||= @attrib[:name]
  elements = HTML.ul.readonly_selection_list { } 
  options().each_pair { |opt_value, opt_label|
    selected = @value.map { |v| v.to_s }.include?(opt_value.to_s)
    if selected then
      elements << HTML.li(:id => "#{base_id}_#{opt_value}") { opt_label }
    end
  }
  elements
end

#value=(value) ⇒ Object Also known as: set_value

Set list of active selection options as array.



120
121
122
123
124
125
126
127
# File 'lib/aurita-gui/form/selection_list.rb', line 120

def value=(value)
  return unless value
  super(value)
  @selectable_options = []
  options().each_pair { |value, name|
    @selectable_options << value unless @value.map { |v| v.to_s }.include? value.to_s
  }
end