card-mod-bootstrap 0.11.6 → 0.12.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 62fbdd4108c0e8999f3bb0f2d7b3e8a75ecf1de381217011e119de12ff615e65
4
- data.tar.gz: 42d4dc983aaea4ae69f9ff01fefe468e1f2f206644b77c96e0a313080b324d28
3
+ metadata.gz: c958ede5306d8ad27fc2eabd5b52081202d49ebdfbc2581f23a6196adeeaa18c
4
+ data.tar.gz: 6fb46afc92a6fede6948991c58e20501f4e09ecdf4e0477a4d78d418c71c6ecd
5
5
  SHA512:
6
- metadata.gz: 87dbcd645c23be680664d908cc1d2a7a4dd1a9b0312267c79e1abeb7f7f077d97ca1835eb925d65a805f1284fad6e22fe080d219c94627ffac29004c318dbded
7
- data.tar.gz: 8ac729279b110d63de947bbbbe3ec5362dc0a41d7da42f416bd9fd79e9e76f9411c8c97d8e077497d05370b990480a87e77d1f199247bde7a802bcfc6a1aa8f0
6
+ metadata.gz: 695eaca2a8e8caf883a135bba58d1c401286531be65d764a6e0cd4a998335ccd703a26a539774e2d97777fffe5dc68ab1495ed195dee9ca1224df2a98121f910
7
+ data.tar.gz: c539996dc2e5b04c639b5f13083b6f8e3cf9d80b5744c87ac6704748b6e7b249cde10b0a9d9b8a80166d64845a6869231f9184c9e12e7e0019fb98f74daca279
@@ -0,0 +1,17 @@
1
+ class Card
2
+ class Bootstrap
3
+ include Delegate
4
+ extend ComponentLoader
5
+ load_components
6
+
7
+ attr_reader :context
8
+
9
+ def initialize context=nil
10
+ @context = context
11
+ end
12
+
13
+ def render *args, &block
14
+ instance_exec(*args, &block)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,26 @@
1
+ class Card
2
+ class Bootstrap
3
+ module BasicTags
4
+ def html content
5
+ add_content String(content).html_safe
6
+ ""
7
+ end
8
+
9
+ Component.def_div_method :div, nil do |opts, extra_args|
10
+ prepend_class opts, extra_args.first if extra_args.present?
11
+ opts
12
+ end
13
+
14
+ Component.def_div_method :span, nil do |opts, extra_args|
15
+ prepend_class opts, extra_args.first if extra_args.present?
16
+ opts
17
+ end
18
+
19
+ Component.def_tag_method :tag, nil, tag: :yield do |opts, extra_args|
20
+ prepend_class opts, extra_args[1] if extra_args[1].present?
21
+ opts[:tag] = extra_args[0]
22
+ opts
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,110 @@
1
+ class Card
2
+ class Bootstrap
3
+ # render components of bootstrap library
4
+ class Component
5
+ extend ComponentKlass
6
+
7
+ def initialize context, *args, &block
8
+ @context = context
9
+ @content = ["".html_safe]
10
+ @args = args
11
+ @child_args = []
12
+ @append = []
13
+ @wrap = []
14
+ @build_block = block
15
+ @html = Builder::XmlMarkup.new
16
+ end
17
+
18
+ def render
19
+ @rendered = render_content
20
+ end
21
+
22
+ def prepend &block
23
+ tmp = @content.pop
24
+ instance_exec(&block)
25
+ @content << tmp
26
+ end
27
+
28
+ def insert &block
29
+ instance_exec(&block)
30
+ end
31
+
32
+ def append &block
33
+ @append[-1] << block
34
+ end
35
+
36
+ def wrap tag=nil, &block
37
+ @wrap[-1] << (block_given? ? block : tag)
38
+ end
39
+
40
+ def card
41
+ @context.context.card
42
+ end
43
+
44
+ private
45
+
46
+ def tag_method_opts args, html_class, tag_opts, &tag_opts_block
47
+ opts = {}
48
+ _blah, opts, _blah = standardize_args args, &tag_opts_block if block_given?
49
+ add_classes opts, html_class, tag_opts.delete(:optional_classes)
50
+ opts
51
+ end
52
+
53
+ def render_content
54
+ # if @build_block.arity > 0
55
+ instance_exec(*@args, &@build_block)
56
+ end
57
+
58
+ def generate_content content, processor, &block
59
+ content = instance_exec(&block) if block.present?
60
+ return content if !processor || !content.is_a?(Array)
61
+
62
+ content.each { |item| send processor, item }
63
+ ""
64
+ end
65
+
66
+ def with_child_args args
67
+ @child_args << args if args.present?
68
+ yield.tap { @child_args.pop if args.present? }
69
+ end
70
+
71
+ def add_content content
72
+ @content[-1] << "\n#{content}".html_safe if content.present?
73
+ end
74
+
75
+ def standardize_args args, &block
76
+ opts = standardize_opts args
77
+ items = items_from_args args
78
+ opts, args = standardize_block_args opts, args, &block if block.present?
79
+
80
+ [items, opts, args]
81
+ end
82
+
83
+ def standardize_opts args
84
+ args.last.is_a?(Hash) ? args.pop : {}
85
+ end
86
+
87
+ def items_from_args args
88
+ ((args.one? && args.last.is_a?(String)) || args.last.is_a?(Array)) && args.pop
89
+ end
90
+
91
+ def standardize_block_args opts, args, &block
92
+ instance_exec(opts, args, &block).tap do |s_opts, _s_args|
93
+ unless s_opts.is_a? Hash
94
+ raise Card::Error, "first return value of a tag block has to be a hash"
95
+ end
96
+ end
97
+ end
98
+
99
+ def add_classes opts, html_class, optional_classes
100
+ prepend_class opts, html_class if html_class
101
+ Array.wrap(optional_classes).each do |k, v|
102
+ prepend_class opts, v if opts.delete k
103
+ end
104
+ end
105
+
106
+ include BasicTags
107
+ include Delegate
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,78 @@
1
+ class Card
2
+ class Bootstrap
3
+ class Component
4
+ class Carousel < Component
5
+ def render_content
6
+ carousel(*@args, &@build_block)
7
+ end
8
+
9
+ def carousel id, active_index, &block
10
+ @id = id
11
+ @active_item_index = active_index
12
+ @items = []
13
+ instance_exec(&block)
14
+
15
+ @html.div class: "carousel slide", id: id, "data-ride" => "carousel" do
16
+ indicators
17
+ items
18
+ control_prev
19
+ control_next
20
+ end
21
+ end
22
+
23
+ def item content=nil, &block
24
+ @items << (content || block)
25
+ end
26
+
27
+ def items
28
+ @html.div class: "carousel-inner", role: "listbox" do
29
+ @items.each_with_index do |item, index|
30
+ carousel_item item, carousel_item_opts(index)
31
+ end
32
+ end
33
+ end
34
+
35
+ def carousel_item_opts index
36
+ { class: "carousel-item" }.tap do |opts|
37
+ add_class opts, "active" if index == @active_item_index
38
+ end
39
+ end
40
+
41
+ def carousel_item item, html_opts
42
+ @html.div html_opts do
43
+ item = item.call if item.respond_to?(:call)
44
+ @html << item if item.is_a?(String)
45
+ end
46
+ end
47
+
48
+ def control_prev
49
+ @html.a class: "carousel-control-prev", href: "##{@id}", role: "button",
50
+ "data-slide" => "prev" do
51
+ @html.span class: "carousel-control-prev-icon", "aria-hidden" => "true"
52
+ @html.span "Previous", class: "sr-only"
53
+ end
54
+ end
55
+
56
+ def control_next
57
+ @html.a class: "carousel-control-next", href: "##{@id}", role: "button",
58
+ "data-slide": "next" do
59
+ @html.span class: "carousel-control-next-icon", "aria-hidden" => "true"
60
+ @html.span "Next", class: "sr-only"
61
+ end
62
+ end
63
+
64
+ def indicators
65
+ @html.ol class: "carousel-indicators" do
66
+ @items.size.times { |i| indicator i }
67
+ end
68
+ end
69
+
70
+ def indicator index
71
+ html_opts = { "data-slide-to" => index, "data-target": "##{@id}" }
72
+ add_class html_opts, "active" if index == @active_item_index
73
+ @html.li html_opts
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,67 @@
1
+ class Card
2
+ class Bootstrap
3
+ class Component
4
+ class Form < Component
5
+ def render_content *args
6
+ form(*args, &@build_block)
7
+ end
8
+
9
+ #
10
+ # def_tag_method :form, nil, optional_classes: {
11
+ # horizontal: "form-horizontal",
12
+ # inline: "form-inline"
13
+ # }
14
+ # def_div_method :group, "form-group"
15
+ # def_tag_method :label, nil
16
+ # def_tag_method :input, "form-control" do |opts, extra_args|
17
+ # type, label = extra_args
18
+ # prepend { label label, for: opts[:id] } if label
19
+ # opts[:type] = type
20
+ # opts
21
+ # end
22
+
23
+ def form opts={}, &block
24
+ add_class opts, "form-horizontal" if opts.delete(:horizontal)
25
+ add_class opts, "form-inline" if opts.delete(:inline)
26
+ @html.form opts do
27
+ instance_exec(&block)
28
+ end
29
+ end
30
+
31
+ def group text=nil, &block
32
+ @html.div text, class: "form-group" do
33
+ instance_exec(&block)
34
+ end
35
+ end
36
+
37
+ def label text=nil, &block
38
+ @html.label text, &block
39
+ end
40
+
41
+ def input type, text: nil, label: nil, id: nil
42
+ @html.input id: id, class: "form-control", type: type do
43
+ @html.label label, for: id if label
44
+ @html << text if text
45
+ end
46
+ end
47
+
48
+ %i[text password datetime datetime-local date month time
49
+ week number email url search tel color].each do |tag|
50
+ # def_tag_method tag, "form-control", attributes: { type: tag },
51
+ # tag: :input do |opts, extra_args|
52
+ # label, = extra_args
53
+ # prepend { label label, for: opts[:id] } if label
54
+ # opts
55
+ # end
56
+
57
+ define_method tag do |id:, label:, text: nil|
58
+ @html.input id: id, class: "form-control", type: tag do
59
+ @html.label label, for: id if label
60
+ @html << text
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,65 @@
1
+ class Card
2
+ class Bootstrap
3
+ class Component
4
+ class HorizontalForm < Form
5
+ def left_col_width
6
+ @child_args.last && @child_args.last[0] || 2
7
+ end
8
+
9
+ def right_col_width
10
+ @child_args.last && @child_args.last[1] || 10
11
+ end
12
+
13
+ def_tag_method :form, "form-horizontal"
14
+
15
+ def_tag_method :label, "control-label" do |opts, _extra_args|
16
+ prepend_class opts, "col-sm-#{left_col_width}"
17
+ opts
18
+ end
19
+
20
+ # def_div_method :input, nil do |opts, extra_args, &block|
21
+ # type, label = extra_args
22
+ # prepend { tag(:label, nil, for: opts[:id]) { label } } if label
23
+ # insert { inner_input opts.merge(type: type) }
24
+ # { class: "col-sm-#{right_col_width}" }
25
+ # end
26
+
27
+ def label_col label, id:
28
+ @html.label label, for: id, class: "col-sm-#{left_col_width} control-label"
29
+ end
30
+
31
+ def input type, label:, id:
32
+ label_col label, id: id
33
+ @html.div class: "col-sm-#{right_col_width}" do
34
+ @html.input type: type, id: id, class: "form-control"
35
+ end
36
+ # block.call class: "col-sm-#{right_col_width}" do
37
+ # inner_input opts.merge(type: type)
38
+ # end
39
+ end
40
+
41
+ def_tag_method :inner_input, "form-control", tag: :input
42
+ def_div_method :inner_checkbox, "checkbox"
43
+
44
+ def_div_method :checkbox, nil do |opts, extra_args|
45
+ inner_checkbox do
46
+ label do
47
+ inner_input "checkbox", extra_args.first, opts
48
+ end
49
+ end
50
+ { class: "col-sm-offset-#{left_col_width} col-sm-#{right_col_width}" }
51
+ end
52
+
53
+ def checkbox _text, _extra_args
54
+ @html.div class: "col-sm-offset-#{left_col_width} col-sm-#{right_col_width}" do
55
+ @html.div class: "checkbox" do
56
+ label_cllabel do
57
+ inner_input "checkbox"
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,107 @@
1
+ class Card
2
+ class Bootstrap
3
+ class Component
4
+ # generate bootstrap column layout
5
+ # @example
6
+ # layout container: true, fluid: true, class: "hidden" do
7
+ # row 6, 6, class: "unicorn" do
8
+ # column "horn",
9
+ # column "rainbow", class: "colorful"
10
+ # end
11
+ # end
12
+ # @example
13
+ # layout do
14
+ # row 3, 3, 4, 2, class: "unicorn" do
15
+ # [ "horn", "body", "tail", "rainbow"]
16
+ # end
17
+ # add_html "<span> some extra html</span>"
18
+ # row 6, 6, ["unicorn", "rainbow"], class: "horn"
19
+ # end
20
+ class Layout < OldComponent
21
+ def render
22
+ @rendered = begin
23
+ render_content
24
+ @content[-1]
25
+ end
26
+ end
27
+
28
+ def render_content
29
+ content = instance_exec(*@args, &@build_block)
30
+ add_content content
31
+ opts = @args.first
32
+ return unless opts&.delete(:container)
33
+
34
+ content = @content.pop
35
+ @content = ["".html_safe]
36
+ container content, opts
37
+ end
38
+
39
+ add_div_method :container, nil do |opts, _extra_args|
40
+ prepend_class opts, opts.delete(:fluid) ? "container-fluid" : "container"
41
+ opts
42
+ end
43
+
44
+ # @param args column widths, column content and html attributes
45
+ # @example
46
+ # row 6, 6, ["col one", "col two"], class: "count", id: "count"
47
+ # @example
48
+ # row md: 12, xs: 8, "single column content"
49
+ # @example
50
+ # row md: [1, 11], xs: [2, 10] do
51
+ # col "A"
52
+ # col "B"
53
+ # end
54
+ add_div_method :row, "row", content_processor: :column do |opts, extra_args|
55
+ cols_content = extra_args.pop if extra_args.last.is_a? Array
56
+ [opts, col_widths(extra_args, opts), cols_content].compact
57
+ end
58
+
59
+ # default column width type is for medium devices (col-md-)
60
+ add_div_method :column, nil do |opts, _extra_args|
61
+ @child_args.last.each do |medium, size|
62
+ if medium == :xs
63
+ prepend_class opts, "col-#{size.shift}"
64
+ else
65
+ prepend_class opts, "col-#{medium}-#{size.shift}"
66
+ end
67
+ end
68
+ opts
69
+ end
70
+
71
+ alias_method :col, :column
72
+
73
+ private
74
+
75
+ def standardize_row_args args
76
+ opts = args.last.is_a?(Hash) ? args.pop : {}
77
+ cols = (args.last.is_a?(Array) || args.last.is_a?(String)) &&
78
+ Array.wrap(args.pop)
79
+ [cols, opts, col_widths(args, opts)]
80
+ end
81
+
82
+ def col_widths args, opts
83
+ opts = args.pop if args.one? && args.last.is_a?(Hash)
84
+ if args.present?
85
+ col_widths_from_args args
86
+ else
87
+ col_widths_from_opts opts
88
+ end
89
+ end
90
+
91
+ def col_widths_from_args args
92
+ raise Error, "bad argument" unless args.all? { |a| a.is_a? Integer }
93
+
94
+ { md: Array.wrap(args) }
95
+ end
96
+
97
+ def col_widths_from_opts opts
98
+ %i[lg xs sm md].each_with_object({}) do |k, cols_w|
99
+ next unless (widths = opts.delete(k))
100
+
101
+ cols_w[k] = Array.wrap widths
102
+ end
103
+ end
104
+ end
105
+ end
106
+ end
107
+ end