Module: ExtForm::Layouts::BaseLayout

Extended by:
ActiveSupport::Concern
Included in:
BootstrapLayout, DefaultLayout
Defined in:
lib/ext_form/layouts/base_layout.rb

Instance Method Summary collapse

Instance Method Details

#calc_actual_widthObject

Raises:

  • (NotImplementedError)


65
66
67
# File 'lib/ext_form/layouts/base_layout.rb', line 65

def calc_actual_width
  raise NotImplementedError
end

#calculate_input_width(max_width, label_width, spacing, cols_sum, c) ⇒ Object

Raises:

  • (NotImplementedError)


69
70
71
# File 'lib/ext_form/layouts/base_layout.rb', line 69

def calculate_input_width(max_width, label_width, spacing, cols_sum, c)
  raise NotImplementedError
end

#calculate_layout(config) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/ext_form/layouts/base_layout.rb', line 55

def calculate_layout(config)
  layout = []
  cols = config[:layout].split(':').map { |s| s.to_i }

  cols.each do |c|
    layout << [config[:label_width], calculate_input_width(config[:max_width], config[:label_width], config[:spacing], cols.sum, c)]
  end
  layout
end

#input_width(seq) ⇒ Object



77
78
79
# File 'lib/ext_form/layouts/base_layout.rb', line 77

def input_width(seq)
  self.layout[seq][1]
end

#label_width(seq) ⇒ Object



73
74
75
# File 'lib/ext_form/layouts/base_layout.rb', line 73

def label_width(seq)
  self.layout[seq][0]
end

#layout_available?(layout) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/ext_form/layouts/base_layout.rb', line 51

def layout_available?(layout)
  layout && layout.match(/^\d(:\d){0,2}$/)
end

#measure_available?(measure) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


47
48
49
# File 'lib/ext_form/layouts/base_layout.rb', line 47

def measure_available?(measure)
  raise NotImplementedError
end

#setup_config(config) ⇒ Object

Internal: setup layout configuration and setup the default layout of form, you can change it in a particular row.

config - layout config, default nil, it must be a hash.

:layout      - it's a string ,specify the scale of columns.
               for example: '1:1', '1:2', '1:1:1'
:spacing     - left and right padding of form, should be number or percentage.
:label_width - label width of input, should be number or percentage.
:max_width   - max width of form, should be number or percentage.

All width options should be all numbers or all percentages, or it will not
be calculated correctly.

I'm looking for a method to provide responding design. Any contribution will be
appreciated.


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ext_form/layouts/base_layout.rb', line 26

def setup_config(config)
  self.layout_config = if config && config.is_a?(Hash)
                         config.delete(:layout) unless layout_available?(config[:layout])
                         config.delete(:spacing) unless width_available?(config[:spacing])
                         config.delete(:label_width) unless width_available?(config[:label_width])
                         config.delete(:max_width) unless width_available?(config[:max_width])
                         config.delete(:measure) unless measure_available?(config[:measure])

                         config.reverse_merge(ExtForm.default_layout)
                       else
                         ExtForm.default_layout.dup
                       end

  self.layout = calculate_layout(layout_config)
  self.actual_width = calc_actual_width
end

#width_available?(width) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


43
44
45
# File 'lib/ext_form/layouts/base_layout.rb', line 43

def width_available?(width)
  raise NotImplementedError
end