class Weaver::FormElements

Attributes

options[RW]
scripts[RW]

Public Class Methods

new(page, anchors, formName, options = {}) click to toggle source
Calls superclass method Weaver::Elements::new
# File lib/weaver/element_types/form_elements.rb, line 9
def initialize(page, anchors, formName, options = {})
  super(page, anchors)
  @formName = formName
  @options = options
  @scripts = []
end

Public Instance Methods

boolean_element(checkbox_label, options = {}) click to toggle source
# File lib/weaver/element_types/form_elements.rb, line 266
    def boolean_element(checkbox_label, options = {})
      @page.request_css 'css/plugins/iCheck/custom.css'
      @page.request_js 'js/plugins/iCheck/icheck.min.js'

      @page.write_script_once <<~SCRIPT
        $(document).ready(function () {
            $('.i-checks').iCheck({
                checkboxClass: 'icheckbox_square-green',
                radioClass: 'iradio_square-green',
            });
        });
      SCRIPT

      label_options = {}
      elem = Elements.new(@page, @anchors)
      elem.instance_eval do
        if options[:form] == :button
          options.delete(:form)
          label class: 'btn btn-primary btn-block btn-outline' do
            input options
            text checkbox_label.to_s
          end
        else
          div class: 'i-checks' do
            label label_options do
              input options do
                text " #{checkbox_label}"
              end
            end
          end
        end
      end

      elem.generate
    end
checkbox(name, checkbox_label, options = {}) click to toggle source
# File lib/weaver/element_types/form_elements.rb, line 245
    def checkbox(name, checkbox_label, options = {})
      checkbox_name = options[:id] || @page.create_anchor('checkbox')
      options[:type] = 'checkbox'
      options[:name] = name
      options[:id] = checkbox_name
      text boolean_element(checkbox_label, options)
      @scripts << <<-SCRIPT
        object["#{name}"] = $('##{checkbox_name}').is(":checked");
      SCRIPT
    end
credit_card(options = {}, &block) click to toggle source
# File lib/weaver/element_types/form_elements.rb, line 302
def credit_card(options = {}, &block)
  elem = CreditCardForm.new(@page, @anchors, options, &block)
  elem.apply_script(@scripts)
  text elem.generate
end
dropdown(name, dropdown_label, choice_array, options = {}) click to toggle source
hiddenfield(name, value, options = {}) click to toggle source
# File lib/weaver/element_types/form_elements.rb, line 87
    def hiddenfield(name, value, options = {})
      hiddenfield_name = options[:id] || @page.create_anchor('hiddenfield')

      input_options = {}
      input_options[:type] = 'hidden'
      input_options[:value] = value
      input_options[:id] = hiddenfield_name
      input_options[:name] = name

      input input_options

      @scripts << <<-SCRIPT
        object["#{name}"] = $('##{hiddenfield_name}').val();
      SCRIPT
    end
knob(name, options = {}) click to toggle source
# File lib/weaver/element_types/form_elements.rb, line 167
    def knob(name, options = {})
      knob_name = @page.create_anchor 'knob'

      @page.request_js 'js/plugins/jsKnob/jquery.knob.js'
      @page.write_script_once <<-SCRIPT
        $(".dial").knob();
      SCRIPT

      knob_options = {}

      knob_options[:id] = knob_name
      knob_options[:type] = 'text'
      knob_options[:value] = options[:value] || '0'
      knob_options[:class] = 'dial'

      options.each do |key, value|
        knob_options["data-#{key}".to_sym] = value
      end

      knob_options[:"data-fgColor"] = '#1AB394'
      knob_options[:"data-width"] = '85'
      knob_options[:"data-height"] = '85'

      input knob_options

      @scripts << <<-SCRIPT
        object["#{name}"] = $('##{knob_name}').val();
      SCRIPT
    end
passwordfield(name, textfield_label = nil, options = {}, &block) click to toggle source
# File lib/weaver/element_types/form_elements.rb, line 16
def passwordfield(name, textfield_label = nil, options = {}, &block)
  if textfield_label.is_a? Hash
    options = textfield_label
    textfield_label = nil
  end

  options[:type] = 'password'
  textfield(name, textfield_label, options, &block)
end
radio(name, choice_array, options = {}) click to toggle source
# File lib/weaver/element_types/form_elements.rb, line 197
    def radio(name, choice_array, options = {})
      radio_name = @page.create_anchor 'radio'

      choice_array = choice_array.map do |choice|
        if choice.is_a? Hash
          { value: choice[:value], label: choice[:label] }
        else
          { value: choice, label: choice }
        end
      end

      active = choice_array[0][:value]
      if options[:value] && (choice_array.index { |x| x[:value] == options[:value] } != nil)
        active = options[:value]
      end

      div_options = {}
      curobject = self
      div_options[:"data-toggle"] = 'buttons' if options[:form] == :button
      div div_options do
        choice_array.each do |choice|
          value = choice[:value]
          label = choice[:label]

          the_options = Hash.new(options)

          the_options[:checked] = '' if active == value

          if options[:form] == :button
            the_options[:type] = 'radio'
            the_options[:value] = value
            the_options[:name] = name
            the_options[:form] = :button
            text curobject.boolean_element(label, the_options)
          else
            the_options[:type] = 'radio'
            the_options[:value] = value
            the_options[:name] = name
            text curobject.boolean_element(label, the_options)
          end
        end
      end

      @scripts << <<-SCRIPT
        object["#{name}"] = $('input[name=#{name}]:checked', '##{@formName}').val()
      SCRIPT
    end
submit(anIcon, title = {}, options = {}, &block) click to toggle source
# File lib/weaver/element_types/form_elements.rb, line 256
def submit(anIcon, title = {}, options = {}, &block)
  options[:id] = @page.create_anchor('submit_button')
  options[:icon] = anIcon
  options[:title] = title
  options[:type] = 'submit'
  options[:data] = "get_#{@formName}_object()"
  options[:nosubmit] = true if block
  _button(options, &block)
end
textfield(name, textfield_label = nil, options = {}, &block) click to toggle source
# File lib/weaver/element_types/form_elements.rb, line 26
    def textfield(name, textfield_label = nil, options = {}, &block)
      if textfield_label.is_a? Hash
        options = textfield_label
        textfield_label = nil
      end

      textfield_name = options[:id] || @page.create_anchor('textfield')
      options[:type] ||= 'text'
      options[:placeholder] ||= ''
      options[:name] = name

      input_options = {}
      input_options[:type] = options[:type]
      input_options[:placeholder] = options[:placeholder]
      input_options[:id] = textfield_name
      input_options[:name] = options[:name]
      input_options[:rows] = options[:rows]
      input_options[:class] = 'form-control'
      input_options[:value] = options[:value]
      input_options[:style] = options[:style]

      input_options[:autocomplete] = options[:autocomplete] || 'on'
      input_options[:autocorrect] = options[:autocorrect] || 'on'
      input_options[:autocapitalize] = options[:autocapitalize] || 'off'

      if options[:mask]
        @page.request_css 'css/plugins/jasny/jasny-bootstrap.min.css'
        @page.request_js 'js/plugins/jasny/jasny-bootstrap.min.js'

        input_options[:"data-mask"] = options[:mask]
      end

      div class: "form-group #{options[:extra_class]}", id: "#{input_options[:id]}-group" do
        label textfield_label if textfield_label

        div_class = ' '
        if options[:front_text] || options[:back_text]
          div_class = 'input-group m-b'
         end

        div "class": div_class do
          span (options[:front_text]).to_s, class: 'input-group-addon' if options[:front_text]
          if input_options[:rows] && (input_options[:rows] > 1)
            textarea input_options do
            end
          else
            input input_options
          end
          span (options[:back_text]).to_s, class: 'input-group-addon' if options[:back_text]
        end
      end

      textjs = TextfieldJavascript.new(input_options[:id])

      @page.on_page_load textjs.generate(&block) if block

      @scripts << <<-SCRIPT
        object["#{name}"] = $('##{textfield_name}').val();
      SCRIPT
    end