Class: Weaver::Elements
- Inherits:
-
Object
show all
- Defined in:
- lib/weaver/elements.rb,
lib/weaver/element_types/row.rb,
lib/weaver/element_types/accordion.rb,
lib/weaver/element_types/modal_dialog.rb,
lib/weaver/element_types/dynamic_table.rb
Overview
Instance Method Summary
collapse
-
#_button(options = {}, &block) ⇒ Object
-
#accordion(&block) ⇒ Object
-
#background(&block) ⇒ Object
-
#badge(label, options = {}) ⇒ Object
-
#big_button(anIcon, title = {}, options = {}, &block) ⇒ Object
-
#big_embossed_button(anIcon, title = {}, options = {}, &block) ⇒ Object
-
#block_button(anIcon, title = {}, options = {}, &block) ⇒ Object
-
#breadcrumb(patharray) ⇒ Object
-
#center(content = nil, options = {}, &block) ⇒ Object
-
#circle_button(anIcon, title = {}, options = {}, &block) ⇒ Object
-
#clear_default_meta! ⇒ Object
-
#col(occupies, options = {}, &block) ⇒ Object
-
#crossfade_image(image_normal, image_hover) ⇒ Object
-
#embossed_button(anIcon, title = {}, options = {}, &block) ⇒ Object
-
#gallery(images, thumbnails = images, options = {}) ⇒ Object
-
#generate ⇒ Object
-
#half(&block) ⇒ Object
-
#head(&block) ⇒ Object
-
#hyperlink(url, title = nil, &block) ⇒ Object
-
#ibox(options = {}, &block) ⇒ Object
-
#icon(type) ⇒ Object
-
#image(name, options = {}) ⇒ Object
-
#initialize(page, anchors) ⇒ Elements
constructor
A new instance of Elements.
-
#jumbotron(options = {}, &block) ⇒ Object
-
#math(string) ⇒ Object
-
#meta(options = {}, &block) ⇒ Object
-
#method_missing(name, *args, &block) ⇒ Object
-
#modal(id = nil, &block) ⇒ Object
-
#normal_button(anIcon, title = {}, options = {}, &block) ⇒ Object
-
#on_page_load(script) ⇒ Object
-
#outline_button(anIcon, title = {}, options = {}, &block) ⇒ Object
-
#p(*args, &block) ⇒ Object
-
#panel(title, &block) ⇒ Object
-
#quarter(&block) ⇒ Object
-
#request_css(path) ⇒ Object
-
#request_js(path) ⇒ Object
-
#root ⇒ Object
-
#rounded_button(anIcon, title = {}, options = {}, &block) ⇒ Object
-
#row(options = {}, &block) ⇒ Object
-
#script(options = {}, &block) ⇒ Object
-
#set_favicon_path(path) ⇒ Object
-
#set_favicon_type(v) ⇒ Object
-
#small_button(anIcon, title = {}, options = {}, &block) ⇒ Object
-
#syntax(lang = :javascript, options = {}, &block) ⇒ Object
-
#table(options = {}, &block) ⇒ Object
-
#table_from_hashes(hashes, options = {}) ⇒ Object
-
#table_from_source(url, options = {}, &block) ⇒ Object
-
#tabs(&block) ⇒ Object
-
#text(theText) ⇒ Object
-
#third(&block) ⇒ Object
-
#tiny_button(anIcon, title = {}, options = {}, &block) ⇒ Object
-
#top(&block) ⇒ Object
-
#twothirds(&block) ⇒ Object
-
#wform(options = {}, &block) ⇒ Object
-
#widget(options = {}, &block) ⇒ Object
-
#write_script_once(script) ⇒ Object
Constructor Details
#initialize(page, anchors) ⇒ Elements
Returns a new instance of Elements.
6
7
8
9
10
|
# File 'lib/weaver/elements.rb', line 6
def initialize(page, anchors)
@inner_content = []
@anchors = anchors
@page = page
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/weaver/elements.rb', line 12
def method_missing(name, *args, &block)
tag = "<#{name} />"
inner = args.shift if args[0].is_a? String
if block
elem = Elements.new(@page, @anchors)
elem.instance_eval(&block)
inner = elem.generate
end
if !inner
options = args[0] || []
opts = options.map { |key, value| "#{key}=\"#{value}\"" }.join ' '
tag = "<#{name} #{opts} />"
elsif args.empty?
tag = "<#{name}>#{inner}</#{name}>"
elsif (args.length == 1) && args[0].is_a?(Hash)
options = args[0]
opts = options.map { |key, value| "#{key}=\"#{value}\"" }.join ' '
tag = "<#{name} #{opts}>#{inner}</#{name}>"
end
@inner_content << tag
tag
end
|
Instance Method Details
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
|
# File 'lib/weaver/elements.rb', line 305
def _button(options = {}, &block)
anIcon = options[:icon]
title = options[:title]
if title.is_a? Hash
options.merge! title
title = anIcon
anIcon = nil
end
style = options[:style] || :primary
size = "btn-#{options[:size]}" if options[:size]
blockstyle = 'btn-block' if options[:block]
outline = 'btn-outline' if options[:outline]
dim = 'dim' if options[:threedee]
dim = 'dim btn-large-dim' if options[:bigthreedee]
dim = 'btn-rounded' if options[:rounded]
dim = 'btn-circle' if options[:circle]
buttonOptions = {
type: options[:type] || 'button',
class: "btn btn-#{style} #{size} #{blockstyle} #{outline} #{dim}",
id: options[:id],
style: "#{options[:css_style]}"
}
if block
closer = ''
closer = '; return false;' if options[:nosubmit]
action = Action.new(@page, @anchors, &block)
buttonOptions[:onclick] = "#{action.name}(this)"
if options[:data]
buttonOptions[:onclick] = "#{action.name}(this, #{options[:data]})#{closer}"
end
@page.scripts << action.generate
end
type = :button
buttonOptions[:"data-toggle"] = 'button' if options[:toggle]
type = :a if options[:toggle]
method_missing type, buttonOptions do
if title.is_a? Symbol
icon title
else
icon anIcon if anIcon
text ' ' if anIcon
text title
end
end
end
|
#accordion(&block) ⇒ Object
119
120
121
122
123
124
|
# File 'lib/weaver/element_types/accordion.rb', line 119
def accordion(&block)
acc = Accordion.new(@page, @anchors)
acc.instance_eval(&block)
@inner_content << acc.generate
end
|
#background(&block) ⇒ Object
87
88
89
|
# File 'lib/weaver/elements.rb', line 87
def background(&block)
@page.background(block)
end
|
#badge(label, options = {}) ⇒ Object
225
226
227
228
229
230
231
232
233
234
235
236
|
# File 'lib/weaver/elements.rb', line 225
def badge(label, options = {})
options[:type] ||= 'plain'
kind = 'label'
kind = 'badge' if options[:rounded]
tag_options = options.clone
tag_options[:class] = "#{kind} #{kind}-#{options[:type]}"
span tag_options do
text label
end
end
|
380
381
382
383
384
385
|
# File 'lib/weaver/elements.rb', line 380
def big_button(anIcon, title = {}, options = {}, &block)
options[:size] = :lg
options[:icon] = anIcon
options[:title] = title
_button(options, &block)
end
|
408
409
410
411
412
413
|
# File 'lib/weaver/elements.rb', line 408
def big_embossed_button(anIcon, title = {}, options = {}, &block)
options[:bigthreedee] = true
options[:icon] = anIcon
options[:title] = title
_button(options, &block)
end
|
366
367
368
369
370
371
|
# File 'lib/weaver/elements.rb', line 366
def block_button(anIcon, title = {}, options = {}, &block)
options[:block] = true
options[:icon] = anIcon
options[:title] = title
_button(options, &block)
end
|
#breadcrumb(patharray) ⇒ Object
209
210
211
212
213
214
215
|
# File 'lib/weaver/elements.rb', line 209
def breadcrumb(patharray)
ol class: 'breadcrumb' do
patharray.each do |path|
li path
end
end
end
|
#center(content = nil, options = {}, &block) ⇒ Object
115
116
117
118
119
120
121
122
123
124
|
# File 'lib/weaver/elements.rb', line 115
def center(content = nil, options = {}, &block)
options[:style] = '' unless options[:style]
options[:style] += '; text-align: center;'
if !content
div options, &block
else
div content, options, &block
end
end
|
422
423
424
425
426
427
|
# File 'lib/weaver/elements.rb', line 422
def circle_button(anIcon, title = {}, options = {}, &block)
options[:circle] = true
options[:icon] = anIcon
options[:title] = title
_button(options, &block)
end
|
43
44
45
|
# File 'lib/weaver/elements.rb', line 43
def clear_default_meta!
@page.clear_default_meta!
end
|
#col(occupies, options = {}, &block) ⇒ Object
114
115
116
117
|
# File 'lib/weaver/element_types/row.rb', line 114
def col(occupies, options = {}, &block)
column = Column.new(occupies, @page, @anchors, options, &block)
text column.generate
end
|
#crossfade_image(image_normal, image_hover) ⇒ Object
174
175
176
177
178
179
180
181
|
# File 'lib/weaver/elements.rb', line 174
def crossfade_image(image_normal, image_hover)
div class: 'crossfade' do
image image_hover, class: 'bottom'
image image_normal, class: 'top'
end
image image_hover
@page.request_css 'css/crossfade_style.css'
end
|
401
402
403
404
405
406
|
# File 'lib/weaver/elements.rb', line 401
def embossed_button(anIcon, title = {}, options = {}, &block)
options[:threedee] = true
options[:icon] = anIcon
options[:title] = title
_button(options, &block)
end
|
#gallery(images, thumbnails = images, options = {}) ⇒ Object
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
# File 'lib/weaver/elements.rb', line 183
def gallery(images, thumbnails = images, options = {})
@page.request_css 'css/plugins/blueimp/css/blueimp-gallery.min.css'
div class: 'lightBoxGallery' do
(0...images.length).to_a.each do |index|
title = options[:titles][index] if options[:titles]
a href: (images[index]).to_s, title: title.to_s, "data-gallery": '' do
img src: (thumbnails[index]).to_s, style: 'margin: 5px;'
end
end
div id: 'blueimp-gallery', class: 'blueimp-gallery' do
div class: 'slides' do end
h3 class: 'title' do end
a class: 'prev' do end
a class: 'next' do end
a class: 'close' do end
a class: 'play-pause' do end
ol class: 'indicator' do end
end
end
@page.request_js 'js/plugins/blueimp/jquery.blueimp-gallery.min.js'
end
|
#generate ⇒ Object
433
434
435
|
# File 'lib/weaver/elements.rb', line 433
def generate
@inner_content.join
end
|
#half(&block) ⇒ Object
81
82
83
84
85
86
87
88
89
90
|
# File 'lib/weaver/element_types/row.rb', line 81
def half(&block)
opts =
{
xs: 12,
sm: 12,
md: 12,
lg: 6
}
col(4, opts, &block)
end
|
#head(&block) ⇒ Object
47
48
49
|
# File 'lib/weaver/elements.rb', line 47
def head(&block)
@page.head(&block)
end
|
#hyperlink(url, title = nil, &block) ⇒ Object
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
|
# File 'lib/weaver/elements.rb', line 238
def hyperlink(url, title = nil, &block)
url = url.dup
title ||= url
if url.start_with? '/'
url.sub!(%r{^/}, @page.root)
if block
a href: url, &block
else
a title, href: url
end
else
if block
a href: url, target: '_blank' do
span do
span &block
icon :external_link
end
end
else
a href: url, target: '_blank' do
span do
text title
text ' '
icon :external_link
end
end
end
end
end
|
#ibox(options = {}, &block) ⇒ Object
109
110
111
112
113
|
# File 'lib/weaver/elements.rb', line 109
def ibox(options = {}, &block)
panel = Panel.new(@page, @anchors, options)
panel.instance_eval(&block)
@inner_content << panel.generate
end
|
#icon(type) ⇒ Object
91
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/weaver/elements.rb', line 91
def icon(type)
iconname = type.to_s.tr('_', '-')
if type.is_a? Symbol
i class: "fa fa-#{iconname}" do
end
else
i class: 'fa' do
text type
end
end
end
|
#image(name, options = {}) ⇒ Object
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
# File 'lib/weaver/elements.rb', line 151
def image(name, options = {})
style = (options[:style]).to_s
if options[:rounded_corners] == true
style += ' border-radius: 8px'
elsif options[:rounded_corners] == :top
style += ' border-radius: 8px 8px 0px 0px'
else
if options[:rounded_corners]
style += " border-radius: #{options[:rounded_corners]}px"
end
end
img_options = {
class: "img-responsive #{options[:class]}",
src: "#{@page.root}images/#{name}",
style: style
}
img_options[:id] = options[:id] if options[:id]
img img_options
end
|
#jumbotron(options = {}, &block) ⇒ Object
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
# File 'lib/weaver/elements.rb', line 285
def jumbotron(options = {}, &block)
additional_style = ''
if options[:background]
additional_style += " background-image: url('#{@page.root}images/#{options[:background]}'); background-position: center center; background-size: cover;"
end
additional_style += " height: #{options[:height]}px;" if options[:height]
if options[:min_height]
additional_style += " min-height: #{options[:min_height]}px;"
end
if options[:max_height]
additional_style += " max-height: #{options[:max_height]}px;"
end
div class: 'jumbotron', style: additional_style, &block
end
|
#math(string) ⇒ Object
429
430
431
|
# File 'lib/weaver/elements.rb', line 429
def math(string)
text "$$$MATH$$$#{string}$$$ENDMATH$$$"
end
|
55
56
57
|
# File 'lib/weaver/elements.rb', line 55
def meta(options = {}, &block)
method_missing(:meta, options, &block)
end
|
#modal(id = nil, &block) ⇒ Object
65
66
67
68
|
# File 'lib/weaver/element_types/modal_dialog.rb', line 65
def modal(id = nil, &block)
mm = ModalDialog.new(@page, @anchors, id, &block)
@inner_content << mm.generate
end
|
360
361
362
363
364
|
# File 'lib/weaver/elements.rb', line 360
def normal_button(anIcon, title = {}, options = {}, &block)
options[:icon] = anIcon
options[:title] = title
_button(options, &block)
end
|
#on_page_load(script) ⇒ Object
79
80
81
|
# File 'lib/weaver/elements.rb', line 79
def on_page_load(script)
@page.on_page_load(script)
end
|
373
374
375
376
377
378
|
# File 'lib/weaver/elements.rb', line 373
def outline_button(anIcon, title = {}, options = {}, &block)
options[:outline] = true
options[:icon] = anIcon
options[:title] = title
_button(options, &block)
end
|
#p(*args, &block) ⇒ Object
217
218
219
|
# File 'lib/weaver/elements.rb', line 217
def p(*args, &block)
method_missing(:p, *args, &block)
end
|
#panel(title, &block) ⇒ Object
126
127
128
129
130
131
132
133
|
# File 'lib/weaver/elements.rb', line 126
def panel(title, &block)
div class: 'panel panel-default' do
div class: 'panel-heading' do
h5 title
end
div class: 'panel-body', &block
end
end
|
#quarter(&block) ⇒ Object
103
104
105
106
107
108
109
110
111
112
|
# File 'lib/weaver/element_types/row.rb', line 103
def quarter(&block)
opts =
{
xs: 12,
sm: 12,
md: 6,
lg: 3
}
col(3, opts, &block)
end
|
#request_css(path) ⇒ Object
67
68
69
|
# File 'lib/weaver/elements.rb', line 67
def request_css(path)
@page.request_css(path)
end
|
#request_js(path) ⇒ Object
63
64
65
|
# File 'lib/weaver/elements.rb', line 63
def request_js(path)
@page.request_js(path)
end
|
#root ⇒ Object
39
40
41
|
# File 'lib/weaver/elements.rb', line 39
def root
@page.root
end
|
415
416
417
418
419
420
|
# File 'lib/weaver/elements.rb', line 415
def rounded_button(anIcon, title = {}, options = {}, &block)
options[:rounded] = true
options[:icon] = anIcon
options[:title] = title
_button(options, &block)
end
|
#row(options = {}, &block) ⇒ Object
63
64
65
66
67
68
|
# File 'lib/weaver/element_types/row.rb', line 63
def row(options = {}, &block)
options[:class] = 'row'
div options do
instance_eval(&block)
end
end
|
#script(options = {}, &block) ⇒ Object
59
60
61
|
# File 'lib/weaver/elements.rb', line 59
def script(options = {}, &block)
method_missing(:script, options, &block)
end
|
#set_favicon_path(path) ⇒ Object
71
72
73
|
# File 'lib/weaver/elements.rb', line 71
def set_favicon_path(path)
@page.set_favicon_path(path)
end
|
#set_favicon_type(v) ⇒ Object
75
76
77
|
# File 'lib/weaver/elements.rb', line 75
def set_favicon_type(v)
@page.set_favicon_type(v)
end
|
387
388
389
390
391
392
|
# File 'lib/weaver/elements.rb', line 387
def small_button(anIcon, title = {}, options = {}, &block)
options[:size] = :sm
options[:icon] = anIcon
options[:title] = title
_button(options, &block)
end
|
#syntax(lang = :javascript, options = {}, &block) ⇒ Object
142
143
144
145
146
147
148
149
|
# File 'lib/weaver/elements.rb', line 142
def syntax(lang = :javascript, options = {}, &block)
code = Code.new(@page, @anchors, lang, options)
code.instance_eval(&block)
@inner_content << code.generate
@page.scripts << code.generate_script
end
|
#table(options = {}, &block) ⇒ Object
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
|
# File 'lib/weaver/element_types/dynamic_table.rb', line 224
def table(options = {}, &block)
table_name = options[:id] || @page.create_anchor('table')
table_style = ''
table_style = options[:style] unless options[:style].nil?
classname = 'table'
classname += ' table-bordered' if options[:bordered]
classname += ' table-hover' if options[:hover]
classname += ' table-striped' if options[:striped]
table_options = {
class: classname,
id: table_name,
style: table_style
}
if options[:system] == :data_table
@page.request_js 'js/plugins/dataTables/jquery.dataTables.js'
@page.request_js 'js/plugins/dataTables/dataTables.bootstrap.js'
@page.request_js 'js/plugins/dataTables/dataTables.responsive.js'
@page.request_js 'js/plugins/dataTables/dataTables.tableTools.min.js'
@page.request_css 'css/plugins/dataTables/dataTables.bootstrap.css'
@page.request_css 'css/plugins/dataTables/dataTables.responsive.css'
@page.request_css 'css/plugins/dataTables/dataTables.tableTools.min.css'
@page.scripts << <<-DATATABLE_SCRIPT
$('##{table_name}').DataTable();
DATATABLE_SCRIPT
end
if options[:system] == :foo_table
table_options[:"data-filtering"] = true
table_options[:"data-sorting"] = true
table_options[:"data-paging"] = true
table_options[:"data-show-toggle"] = true
table_options[:"data-toggle-column"] = 'last'
table_options[:"data-paging-size"] = (options[:max_items_per_page] || 8).to_i.to_s
table_options[:class] = table_options[:class] + ' toggle-arrow-tiny'
@page.request_js 'js/plugins/footable/footable.all.min.js'
@page.request_css 'css/plugins/footable/footable.core.css'
@page.scripts << <<-DATATABLE_SCRIPT
$('##{table_name}').footable({
paging: {
size: #{(options[:max_items_per_page] || 8).to_i}
}
});
$('##{table_name}').append(this.html).trigger('footable_redraw');
DATATABLE_SCRIPT
@page.onload_scripts << <<-SCRIPT
SCRIPT
end
method_missing(:table, table_options, &block)
ul class: 'pagination'
end
|
#table_from_hashes(hashes, options = {}) ⇒ Object
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
|
# File 'lib/weaver/element_types/dynamic_table.rb', line 299
def table_from_hashes(hashes, options = {})
keys = {}
hashes.each do |hash|
hash.each do |key, _value|
keys[key] = ''
end
end
table options do
thead do
keys.each do |key, _|
th key.to_s
end
end
tbody do
hashes.each do |hash|
tr do
keys.each do |key, _|
td (hash[key]).to_s || ' '
end
end
end
end
end
end
|
#table_from_source(url, options = {}, &block) ⇒ Object
291
292
293
294
295
296
297
|
# File 'lib/weaver/element_types/dynamic_table.rb', line 291
def table_from_source(url, options = {}, &block)
dyn_table = DynamicTable.new(@page, @anchors, url, options, &block)
text dyn_table.generate_table
@page.scripts << dyn_table.generate_script
end
|
#tabs(&block) ⇒ Object
135
136
137
138
139
140
|
# File 'lib/weaver/elements.rb', line 135
def tabs(&block)
tabs = Tabs.new(@page, @anchors)
tabs.instance_eval(&block)
@inner_content << tabs.generate
end
|
#text(theText) ⇒ Object
221
222
223
|
# File 'lib/weaver/elements.rb', line 221
def text(theText)
@inner_content << theText
end
|
#third(&block) ⇒ Object
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/weaver/element_types/row.rb', line 92
def third(&block)
opts =
{
xs: 12,
sm: 12,
md: 4,
lg: 4
}
col(4, opts, &block)
end
|
394
395
396
397
398
399
|
# File 'lib/weaver/elements.rb', line 394
def tiny_button(anIcon, title = {}, options = {}, &block)
options[:size] = :xs
options[:icon] = anIcon
options[:title] = title
_button(options, &block)
end
|
#top(&block) ⇒ Object
51
52
53
|
# File 'lib/weaver/elements.rb', line 51
def top(&block)
@page.top(&block)
end
|
#twothirds(&block) ⇒ Object
70
71
72
73
74
75
76
77
78
79
|
# File 'lib/weaver/element_types/row.rb', line 70
def twothirds(&block)
opts =
{
xs: 12,
sm: 12,
md: 8,
lg: 8
}
col(4, opts, &block)
end
|
103
104
105
106
107
|
# File 'lib/weaver/elements.rb', line 103
def wform(options = {}, &block)
theform = Form.new(@page, @anchors, options, &block)
@inner_content << theform.generate
@page.scripts << theform.generate_script
end
|
270
271
272
273
274
275
276
277
278
279
280
281
282
283
|
# File 'lib/weaver/elements.rb', line 270
def widget(options = {}, &block)
color = "#{options[:color]}-bg" || 'navy-bg'
div class: "widget style1 #{color}", &block
end
|
#write_script_once(script) ⇒ Object
83
84
85
|
# File 'lib/weaver/elements.rb', line 83
def write_script_once(script)
@page.write_script_once(script)
end
|