Class: Nitro::Form::ArrayControl

Inherits:
Control
  • Object
show all
Defined in:
lib/nitro/helper/form/controls.rb

Overview

ArrayControl

Instance Attribute Summary

Attributes inherited from Control

#obj, #prop, #value

Instance Method Summary collapse

Methods inherited from Control

fetch, #initialize, #label, #on_populate

Methods included from XhtmlHelper

#date_select, #datetime_select, #hidden, #href_of, #js_popup, #link_to, #onclick_popup, #options, #popup, #submit, #time_select

Constructor Details

This class inherits a constructor from Nitro::Form::Control

Instance Method Details

#emit_array_element(options = {}) ⇒ Object



187
188
189
190
191
192
193
194
195
196
# File 'lib/nitro/helper/form/controls.rb', line 187

def emit_array_element(options={})
  removable = options.fetch(:removable, true)
  %{
    <div>
      <input type="text" id="#{prop.symbol}_ctl" name="#{prop.symbol}[]" value="#{value}"#{emit_style}#{emit_disabled} />
      <input type="button" class="#{prop.symbol}_remove_btn" value=" - " onclick="rm_#{prop.symbol}_rel(this);" #{'disabled="disabled"' unless removable} />
      <input type="button" class="#{prop.symbol}_add_btn" value=" + " onclick="add_#{prop.symbol}_rel(this);"#{emit_disabled} />
    </div>
  }
end

#emit_container_endObject



202
203
204
# File 'lib/nitro/helper/form/controls.rb', line 202

def emit_container_end
  %{</div>}
end

#emit_container_startObject



198
199
200
# File 'lib/nitro/helper/form/controls.rb', line 198

def emit_container_start
  %{<div class="array_container">}
end

#emit_jsObject



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/nitro/helper/form/controls.rb', line 206

def emit_js
  %{
    <script type="text/javascript">
      rm_#{prop.symbol}_rel = function(el){
        ctl=el.parentNode; 
        container=ctl.parentNode; 
        container.removeChild(ctl); 
        inputTags = container.getElementsByTagName('input');
        if(inputTags.length==2) 
          inputTags[0].disabled='disabled';
      }
      add_#{prop.symbol}_rel = function(el){
        ctl=el.parentNode; 
        container=ctl.parentNode; 
        node=ctl.cloneNode(true); 
        node.getElementsByTagName('input')[0].removeAttribute('disabled'); 
        if(container.lastChild==ctl) container.appendChild(node);
        else container.insertBefore(node, ctl.nextSibling);
        if(container.childNodes.length>1) container.getElementsByTagName('input')[1].disabled='';
      }
    </script>
  }
end

#renderObject



173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/nitro/helper/form/controls.rb', line 173

def render
  str = emit_container_start
  str << emit_js
  if values.empty?
    str << emit_array_element(:removable => false)
  else
    removable = values.size != 1 ? true : false
    values.each do |item|
      str << emit_array_element()
    end
  end
  str << emit_container_end
end