Class: IRuby::Input::Checkbox

Inherits:
Label
  • Object
show all
Defined in:
lib/iruby/input/checkbox.rb

Instance Method Summary collapse

Methods inherited from Label

#widget_label

Methods inherited from Widget

builder, #content, #widget_display, #widget_join

Instance Method Details

#widget_cssObject



25
26
27
28
29
30
# File 'lib/iruby/input/checkbox.rb', line 25

def widget_css
  <<-CSS
    .iruby-checkbox.form-control { display: inline-table; }
    .iruby-checkbox .checkbox-inline { margin: 0 15px 0 0; }
  CSS
end

#widget_htmlObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/iruby/input/checkbox.rb', line 51

def widget_html
  params = {
    :'data-iruby-key' => @key,
    class: 'iruby-checkbox form-control'
  }
  widget_label do
    div **params do
      @options.each do |option|
        label class: 'checkbox-inline' do
          input(
            name: @key,
            value: option,
            type: 'checkbox',
            checked: @default.include?(option)
          )
          text option
        end
      end
    end
  end
end

#widget_jsObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/iruby/input/checkbox.rb', line 32

def widget_js
  <<-JS
    $('.iruby-checkbox input').change(function(){
      var parent = $(this).closest('.iruby-checkbox');
      $(parent).data('iruby-value', []);

      $(parent).find(':checked').each(function(){
        $(parent).data('iruby-value').push($(this).val());
      });

      if ($(parent).data('iruby-value').length == 0) {
        $(parent).data('iruby-value', null);
      }
    });

    $('.iruby-checkbox input').trigger('change');
  JS
end