ruby-html-table 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 244c57db696c6f2e1e89481d89e5d91632e73e1cf9ca4e4e204ab4fde1de76e1
4
+ data.tar.gz: 25eb28e3b73f53165aab45a2fbef55a54303ea752ca70fe342e2162803ff4799
5
+ SHA512:
6
+ metadata.gz: d6612527c7bdea0562b53c552ad2ef903f9992466154dcf633668400f8be13a279cc4cb32a49e607303d631ca4cd002ab6fa28c71e894bdb40a65ac482cc1433
7
+ data.tar.gz: 4c9a3ec73fbe00c5553652675941bb727e053bbfa9d553045990e517c524de164492f9aec40a6e588a8fffbbddb5dcb4bb1977f5f996e59ea641c1845080618f
@@ -0,0 +1,136 @@
1
+ /* Table Styles */
2
+
3
+ .table-wrapper{
4
+ margin: 10px 10px 10px;
5
+ box-shadow: 0px 10px 10px rgba( 0, 0, 0, 0.2 );
6
+ }
7
+
8
+ .fl-table {
9
+ border-radius: 5px;
10
+ font-size: 12px;
11
+ font-weight: normal;
12
+ border: none;
13
+ border-collapse: collapse;
14
+ width: 100%;
15
+ max-width: 100%;
16
+ white-space: nowrap;
17
+ background-color: white;
18
+ }
19
+
20
+ .fl-table td, .fl-table th {
21
+ text-align: center;
22
+ padding: 8px;
23
+ }
24
+
25
+ .fl-table td {
26
+ border-right: 1px solid #f8f8f8;
27
+ font-size: 12px;
28
+ }
29
+
30
+ .fl-table thead th {
31
+ color: #ffffff;
32
+ /*PRIMARY_COLOR*/
33
+ }
34
+
35
+ #fl-table-even {
36
+ color: #ffffff;
37
+ /*PRIMARY_COLOR*/
38
+ }
39
+
40
+ #fl-table-odd {
41
+ color: #ffffff;
42
+ /*SECONDARY_COLOR*/
43
+ }
44
+
45
+
46
+ .fl-table thead th:nth-child(odd) {
47
+ color: #ffffff;
48
+ /*PRIMARY_COLOR*/
49
+ }
50
+
51
+ .fl-table tr:nth-child(even) {
52
+ background: #F8F8F8;
53
+ }
54
+
55
+ /* Responsive */
56
+
57
+ @media (max-width: 767px) {
58
+ .fl-table {
59
+ display: block;
60
+ width: 100%;
61
+ }
62
+ .table-wrapper:before{
63
+ content: "Scroll horizontally >";
64
+ display: block;
65
+ text-align: right;
66
+ font-size: 11px;
67
+ color: white;
68
+ padding: 0 0 10px;
69
+ }
70
+ .fl-table thead, .fl-table tbody, .fl-table thead th {
71
+ display: block;
72
+ }
73
+ .fl-table thead th:last-child{
74
+ border-bottom: none;
75
+ }
76
+ .fl-table thead {
77
+ float: left;
78
+ }
79
+ .fl-table tbody {
80
+ width: auto;
81
+ position: relative;
82
+ overflow-x: auto;
83
+ }
84
+ .fl-table td, .fl-table th {
85
+ padding: 20px .625em .625em .625em;
86
+ height: 60px;
87
+ vertical-align: middle;
88
+ box-sizing: border-box;
89
+ overflow-x: hidden;
90
+ overflow-y: auto;
91
+ width: 120px;
92
+ font-size: 13px;
93
+ text-overflow: ellipsis;
94
+ }
95
+ .fl-table thead th {
96
+ text-align: left;
97
+ border-bottom: 1px solid #f7f7f9;
98
+ }
99
+ .fl-table tbody tr {
100
+ display: table-cell;
101
+ }
102
+ .fl-table tbody tr:nth-child(odd) {
103
+ background: none;
104
+ }
105
+ .fl-table tr:nth-child(even) {
106
+ background: transparent;
107
+ }
108
+ .fl-table tr td:nth-child(odd) {
109
+ background: #F8F8F8;
110
+ border-right: 1px solid #E6E4E4;
111
+ }
112
+ .fl-table tr td:nth-child(even) {
113
+ border-right: 1px solid #E6E4E4;
114
+ }
115
+ .fl-table tbody td {
116
+ display: block;
117
+ text-align: center;
118
+ }
119
+ }
120
+
121
+ .pagination_footer ul {
122
+ width: fit-content;
123
+ margin-left: auto;
124
+ margin-right: 10px;
125
+ }
126
+
127
+ .pagination_footer ul li {
128
+ margin: 6px;
129
+ }
130
+
131
+ .pagination_header {
132
+ width: fit-content;
133
+ margin-left: auto;
134
+ margin-right: 10px;
135
+ margin-bottom: 3px;
136
+ }
@@ -0,0 +1 @@
1
+ data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEkAAAArCAIAAAC7LTLaAAAACXBIWXMAABYlAAAWJQFJUiTwAAACiElEQVRo3u1aO67rIBDlSllBpEh06VKyCq8hEpugyQpYQKpZg5v0dClZAWUalC7CUio6InG7yALbYOzkXaHnNplhzsyZD58f7z362Nd13e12u9/v8U/7/f5wOOx2u8+tjvzan1IKAJqmyTSgaRoAUEqtbglaERJjbKGjGWMrgvxZyMnX63W5XE6n0+PxmIhMwL2u667X69j/Mcbn8/l4PG42m3/DSWst53zMOM65lNJaO61BSsk5xxgP6uGcT2tYn5POubZtY1MIIQCgtS7QqbUGAEJIrLZtW+fcN7BJKWM3U0rXyhOlFKU0JoKU8oPYrLXxqpTSskAlwzi41iyKouJwNU2TRGWMEUIMWimEMMYkEQa9ZFYAUU52BcUdYyyEmCs1VvSTuSSECNyaI5XGZowJ8psxliSGUmqs9A0W1WSuWmsDTxFCkmFH+TzM4YNzDgBiBgKAlFJrLaUEgJilAJAMxVx70AQTguxK+sl7HwCbIE9MWgBI6jfGBBk4kR3D2IKmzDnP4bdSahbTYvbmiDjnYvNysQWSmXXJOfe2EmOc323LBKWUSXhoAtisgtsn2Nw+3g84Y6ysLcXw0ASwnAR7p0GBcWOumbXuBDy0HFhQeMpmP+dcTnmYBQ8tB+a9f9d0SmnxnFWsZAwe8t73h/oCYP0dYE4dz+kfc2UDeG3beu9Rv+CUAetjK5jWB0tfgXgAT0qJ+iNVsWVvDUv2BFrrt56yLWnfO4SQquNWc75VXicr72+VzyWVz5OV7wMq379Vvu+u/Lyk8nOuys8nKz9XXvE+IL8xfPU+YCKANdzj9N1f5/1bn6J//N70/313Xp78tXcKNb8vWcrJWe+Cns/ndrv92rugX48eV9CIZ3WwAAAAAElFTkSuQmCC
@@ -0,0 +1,141 @@
1
+ getPagination('#table-id');
2
+
3
+ function getPagination(table) {
4
+ var lastPage = 1;
5
+
6
+ $('#maxRows')
7
+ .on('change', function(evt) {
8
+ //$('.paginationprev').html(''); // reset pagination
9
+
10
+ lastPage = 1;
11
+ $('.pagination')
12
+ .find('li')
13
+ .slice(1, -1)
14
+ .remove();
15
+ var trnum = 0; // reset tr counter
16
+ var maxRows = parseInt($(this).val()); // get Max Rows from select option
17
+
18
+ if (maxRows >= 5000) {
19
+ $('.pagination').hide();
20
+ } else {
21
+ $('.pagination').show();
22
+ }
23
+
24
+ var totalRows = $(table + ' tbody tr').length; // numbers of rows
25
+ $(table + ' tr:gt(0)').each(function() {
26
+ // each TR in table and not the header
27
+ trnum++; // Start Counter
28
+ if (trnum > maxRows) {
29
+ // if tr number gt maxRows
30
+
31
+ $(this).hide(); // fade it out
32
+ }
33
+ if (trnum <= maxRows) {
34
+ $(this).show();
35
+ } // else fade in Important in case if it ..
36
+ }); // was fade out to fade it in
37
+ if (totalRows > maxRows) {
38
+ // if tr total rows gt max rows option
39
+ var pagenum = Math.ceil(totalRows / maxRows); // ceil total(rows/maxrows) to get ..
40
+ // numbers of pages
41
+ for (var i = 1; i <= pagenum; ) {
42
+ // for each page append pagination li
43
+ $('.pagination #prev')
44
+ .before(
45
+ '<li style=\'cursor: pointer; display: inline-block;\' data-page="' +
46
+ i +
47
+ '">\
48
+ <span>' +
49
+ i++ +
50
+ '<span class="sr-only">(current)</span></span>\
51
+ </li>'
52
+ )
53
+ .show();
54
+ } // end for i
55
+ } // end if row count > max rows
56
+ $('.pagination [data-page="1"]').addClass('active'); // add active class to the first li
57
+ $('.pagination li').on('click', function(evt) {
58
+ // on click each page
59
+ evt.stopImmediatePropagation();
60
+ evt.preventDefault();
61
+ var pageNum = $(this).attr('data-page'); // get it's number
62
+
63
+ var maxRows = parseInt($('#maxRows').val()); // get Max Rows from select option
64
+
65
+ if (pageNum == 'prev') {
66
+ if (lastPage == 1) {
67
+ return;
68
+ }
69
+ pageNum = --lastPage;
70
+ }
71
+ if (pageNum == 'next') {
72
+ if (lastPage == $('.pagination li').length - 2) {
73
+ return;
74
+ }
75
+ pageNum = ++lastPage;
76
+ }
77
+
78
+ lastPage = pageNum;
79
+ var trIndex = 0; // reset tr counter
80
+ $('.pagination li').removeClass('active'); // remove active class from all li
81
+ $('.pagination [data-page="' + lastPage + '"]').addClass('active'); // add active class to the clicked
82
+ // $(this).addClass('active'); // add active class to the clicked
83
+ limitPagging();
84
+ $(table + ' tr:gt(0)').each(function() {
85
+ // each tr in table not the header
86
+ trIndex++; // tr index counter
87
+ // if tr index gt maxRows*pageNum or lt maxRows*pageNum-maxRows fade if out
88
+ if (
89
+ trIndex > maxRows * pageNum ||
90
+ trIndex <= maxRows * pageNum - maxRows
91
+ ) {
92
+ $(this).hide();
93
+ } else {
94
+ $(this).show();
95
+ } //else fade in
96
+ }); // end of for each tr in table
97
+ }); // end of on click pagination list
98
+ limitPagging();
99
+ })
100
+ .val(5)
101
+ .change();
102
+
103
+ // end of on select change
104
+
105
+ // END OF PAGINATION
106
+ }
107
+
108
+ function limitPagging(){
109
+ // alert($('.pagination li').length)
110
+
111
+ if($('.pagination li').length > 7 ){
112
+ if( $('.pagination li.active').attr('data-page') <= 3 ){
113
+ $('.pagination li:gt(5)').hide();
114
+ $('.pagination li:lt(5)').show();
115
+ $('.pagination [data-page="next"]').show();
116
+ }if ($('.pagination li.active').attr('data-page') > 3){
117
+ $('.pagination li:gt(0)').hide();
118
+ $('.pagination [data-page="next"]').show();
119
+ for( let i = ( parseInt($('.pagination li.active').attr('data-page')) -2 ) ; i <= ( parseInt($('.pagination li.active').attr('data-page')) + 2 ) ; i++ ){
120
+ $('.pagination [data-page="'+i+'"]').show();
121
+
122
+ }
123
+
124
+ }
125
+ }
126
+ }
127
+
128
+ $(function() {
129
+ // Just to append id number for each row
130
+ $('#table-id tr:eq(0)').prepend('<th></th>');
131
+
132
+ var id = 0;
133
+
134
+ $('#table-id tr:gt(0)').each(function() {
135
+ id++;
136
+ $(this).prepend('<td>' + id + '</td>');
137
+ });
138
+ });
139
+
140
+ // Developed By Yasser Mas
141
+ // yasser.mas2@gmail.com
data/lib/html_table.rb ADDED
@@ -0,0 +1,220 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/all'
4
+ require 'securerandom'
5
+
6
+ ##
7
+ # Manage HTML table
8
+ #
9
+ class HtmlTable
10
+ #
11
+ # Constants
12
+ #
13
+ DEFAULT_COLOR = '#5bc0de'
14
+ DEFAULT_PAGINATION_OPTIONS = [5, 10, 20, 40].freeze
15
+
16
+ #
17
+ # Virtual attributes
18
+ #
19
+
20
+ attr_accessor :attributes, :base_url, :pagination_options, :primary_color, :resources, :resources_class,
21
+ :resources_count, :secondary_color, :show_resource_link, :table_id
22
+
23
+ ##
24
+ # Initialize class
25
+ #
26
+ def initialize(resources_class, resources, attributes, params = {})
27
+ @attributes = attributes&.to_a
28
+ @resources = resources&.to_a
29
+ @resources_class = resources_class
30
+ @resources_count = resources.size
31
+ @base_url = params[:base_url]
32
+ @table_id = params[:table_id] || SecureRandom.hex
33
+ @show_resource_link = params[:show_resource_link] != false && resources_class.method_defined?('id')
34
+ @pagination_options = params[:pagination_options] || DEFAULT_PAGINATION_OPTIONS
35
+ @primary_color = params[:primary_color] || DEFAULT_COLOR
36
+ @secondary_color = params[:secondary_color] || DEFAULT_COLOR
37
+ end
38
+
39
+ ##
40
+ # Generate HTML table
41
+ #
42
+ def generate!
43
+ return '' if invalid_data
44
+
45
+ complete_css + complete_div + complete_js
46
+ end
47
+
48
+ private
49
+
50
+ ##
51
+ # Complete table css
52
+ #
53
+ def complete_css
54
+ "<style>#{css_file}</style>"
55
+ end
56
+
57
+ ##
58
+ # Complete table divs
59
+ #
60
+ def complete_div
61
+ "<div class='table-wrapper'>
62
+ #{pagination_header}
63
+ <table class='fl-table' id='#{table_id}'>
64
+ <thead>
65
+ <tr>#{table_head}</tr>
66
+ </thead>
67
+ <tbody>
68
+ #{table_body}
69
+ <tbody>
70
+ </table>
71
+ </div>
72
+ #{pagination_footer}"
73
+ end
74
+
75
+ ##
76
+ # Complete table JS
77
+ #
78
+ def complete_js
79
+ "<script>#{js_file}</script>"
80
+ end
81
+
82
+ ##
83
+ # Pagination header
84
+ #
85
+ def pagination_header
86
+ "<div class='pagination_header'>Registros por página:
87
+ <select name='state' id='#{table_id}maxRows'>
88
+ <option value='#{resources_count}'>Mostrar Todos</option>
89
+ #{ pagination_options.map do |pagination_option|
90
+ "<option value='#{pagination_option}'>#{pagination_option}</option>"
91
+ end }
92
+ </select>
93
+ </div>"
94
+ end
95
+
96
+ ##
97
+ # Pagination Footer
98
+ #
99
+ def pagination_footer
100
+ "<div class='pagination_footer'><nav>
101
+ <ul class='#{table_id}pagination'>
102
+ <li style='cursor: pointer; display: inline-block;' data-page='prev'>
103
+ <span> <b><</b> <span class='sr-only'>(current)</span></span>
104
+ </li>
105
+ <li style='cursor: pointer; display: inline-block;' data-page='next' id='prev'>
106
+ <span> <b>></b> <span class='sr-only'>(current)</span></span>
107
+ </li>
108
+ </ul>
109
+ </nav></div>"
110
+ end
111
+
112
+ ##
113
+ # Table head
114
+ #
115
+ def table_head
116
+ block = String.new
117
+ attributes.each do |attribute|
118
+ block << '<th>' << head_cell_value(attribute) << '</th>'
119
+ end
120
+ block << "<th id='#{attributes.size.odd? ? 'fl-table-odd' : 'fl-table-even'}'></th>" if show_resource_link
121
+ block
122
+ end
123
+
124
+ ##
125
+ # Head cell value
126
+ #
127
+ def head_cell_value(attribute)
128
+ return '-' if attribute.blank?
129
+
130
+ result = if resources_class.respond_to?('translated_field')
131
+ resources_class.translated_field(attribute.to_s)
132
+ else
133
+ attribute
134
+ end
135
+ (result.present? ? result : '-').to_s.titleize
136
+ end
137
+
138
+ ##
139
+ # Table body
140
+ #
141
+ def table_body
142
+ block = String.new
143
+ resources.each do |resource|
144
+ block << '<tr>'
145
+ attributes.each do |attribute|
146
+ block << '<td>' << body_cell_value(resource, attribute) << '</td>'
147
+ end
148
+ block << resource_link(resource) if show_resource_link
149
+ block << '</tr>'
150
+ end
151
+ block
152
+ end
153
+
154
+ ##
155
+ # Body cell value
156
+ #
157
+ def body_cell_value(resource, attribute)
158
+ return '-' if resource.blank? || attribute.blank?
159
+
160
+ result = if resource.respond_to?('enum_translation')
161
+ resource.send('enum_translation', attribute, resource.send(attribute))
162
+ else
163
+ resource.send(attribute)
164
+ end
165
+ result.present? ? result : '-'
166
+ end
167
+
168
+ ##
169
+ # Generate link to resource
170
+ #
171
+ def resource_link(resource, size = 0.4)
172
+ block = String.new
173
+ block << "<td width='#{146 * size}px'>"
174
+ block << "<a href='#{base_url}/admin/#{resource.class.name.underscore}/#{resource.id}' target='_blank'>"
175
+ block << "<img width='#{73 * size}px' height='#{43 * size}px'
176
+ src='#{resource_link_icon}'>"
177
+ block << '</a></td>'
178
+ block
179
+ end
180
+
181
+ ##
182
+ # Open show icon
183
+ #
184
+ def resource_link_icon
185
+ File.open("#{__dir__}/assets/icons/show.icon").read
186
+ end
187
+
188
+ ##
189
+ # Parsed CSS file
190
+ #
191
+ def css_file
192
+ File.open("#{__dir__}/assets/css/table_template.css")
193
+ .read
194
+ .gsub('/*PRIMARY_COLOR*/', "background: #{primary_color};")
195
+ .gsub('/*SECONDARY_COLOR*/', "background: #{secondary_color};")
196
+ end
197
+
198
+ ##
199
+ # Parsed JS file
200
+ #
201
+ def js_file
202
+ js_file = File.open("#{__dir__}/assets/js/table_template.js").read
203
+ js_file.gsub('table-id', table_id)
204
+ .gsub('pagination', "#{table_id}pagination")
205
+ .gsub('maxRows', "#{table_id}maxRows")
206
+ .gsub('5000', resources_count.to_s)
207
+ end
208
+
209
+ ##
210
+ # Check if all parameters is valid
211
+ #
212
+ def invalid_data
213
+ if !resources.is_a?(Array) || !attributes.is_a?(Array)
214
+ true
215
+ elsif [resources_class, resources, attributes, table_id].any?(&:blank?) || attributes.any?(&:blank?)
216
+ true
217
+ end
218
+ false
219
+ end
220
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-html-table
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Arthur Ferraz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-04-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 4.2.6
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 4.2.6
27
+ description: A simple gem to generate a HTML table based on object array
28
+ email: guidance.team.dev@gmail.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/assets/css/table_template.css
34
+ - lib/assets/icons/show.icon
35
+ - lib/assets/js/table_template.js
36
+ - lib/html_table.rb
37
+ homepage: ''
38
+ licenses:
39
+ - MIT
40
+ metadata: {}
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '2.6'
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubygems_version: 3.1.4
57
+ signing_key:
58
+ specification_version: 4
59
+ summary: HTML Table
60
+ test_files: []