Class: Weaver::Elements

Inherits:
Object
  • 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

Add tables to elements

Direct Known Subclasses

CreditCardForm, DynamicTableCell, FormElements, Panel, Row

Instance Method Summary collapse

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

#_button(options = {}, &block) ⇒ Object



289
290
291
292
293
294
295
296
297
298
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/weaver/elements.rb', line 289

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]
  }

  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



67
68
69
# File 'lib/weaver/elements.rb', line 67

def background(&block)
  @page.background(block)
end

#badge(label, options = {}) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/weaver/elements.rb', line 209

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

#big_button(anIcon, title = {}, options = {}, &block) ⇒ Object



363
364
365
366
367
368
# File 'lib/weaver/elements.rb', line 363

def big_button(anIcon, title = {}, options = {}, &block)
  options[:size] = :lg
  options[:icon] = anIcon
  options[:title] = title
  _button(options, &block)
end

#big_embossed_button(anIcon, title = {}, options = {}, &block) ⇒ Object



391
392
393
394
395
396
# File 'lib/weaver/elements.rb', line 391

def big_embossed_button(anIcon, title = {}, options = {}, &block)
  options[:bigthreedee] = true
  options[:icon] = anIcon
  options[:title] = title
  _button(options, &block)
end

#block_button(anIcon, title = {}, options = {}, &block) ⇒ Object



349
350
351
352
353
354
# File 'lib/weaver/elements.rb', line 349

def block_button(anIcon, title = {}, options = {}, &block)
  options[:block] = true
  options[:icon] = anIcon
  options[:title] = title
  _button(options, &block)
end


193
194
195
196
197
198
199
# File 'lib/weaver/elements.rb', line 193

def breadcrumb(patharray)
  ol class: 'breadcrumb' do
    patharray.each do |path|
      li path
    end
  end
end

#center(content = nil, options = {}, &block) ⇒ Object



99
100
101
102
103
104
105
106
107
108
# File 'lib/weaver/elements.rb', line 99

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

#circle_button(anIcon, title = {}, options = {}, &block) ⇒ Object



405
406
407
408
409
410
# File 'lib/weaver/elements.rb', line 405

def circle_button(anIcon, title = {}, options = {}, &block)
  options[:circle] = true
  options[:icon] = anIcon
  options[:title] = title
  _button(options, &block)
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



158
159
160
161
162
163
164
165
# File 'lib/weaver/elements.rb', line 158

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

#embossed_button(anIcon, title = {}, options = {}, &block) ⇒ Object



384
385
386
387
388
389
# File 'lib/weaver/elements.rb', line 384

def embossed_button(anIcon, title = {}, options = {}, &block)
  options[:threedee] = true
  options[:icon] = anIcon
  options[:title] = title
  _button(options, &block)
end


167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/weaver/elements.rb', line 167

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

#generateObject



416
417
418
# File 'lib/weaver/elements.rb', line 416

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


222
223
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
# File 'lib/weaver/elements.rb', line 222

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



93
94
95
96
97
# File 'lib/weaver/elements.rb', line 93

def ibox(options = {}, &block)
  panel = Panel.new(@page, @anchors, options)
  panel.instance_eval(&block)
  @inner_content << panel.generate
end

#icon(type) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/weaver/elements.rb', line 75

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



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/weaver/elements.rb', line 135

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



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/weaver/elements.rb', line 269

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



412
413
414
# File 'lib/weaver/elements.rb', line 412

def math(string)
  text "$$$MATH$$$#{string}$$$ENDMATH$$$"
end


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

#normal_button(anIcon, title = {}, options = {}, &block) ⇒ Object



343
344
345
346
347
# File 'lib/weaver/elements.rb', line 343

def normal_button(anIcon, title = {}, options = {}, &block)
  options[:icon] = anIcon
  options[:title] = title
  _button(options, &block)
end

#on_page_load(script) ⇒ Object



59
60
61
# File 'lib/weaver/elements.rb', line 59

def on_page_load(script)
  @page.on_page_load(script)
end

#outline_button(anIcon, title = {}, options = {}, &block) ⇒ Object



356
357
358
359
360
361
# File 'lib/weaver/elements.rb', line 356

def outline_button(anIcon, title = {}, options = {}, &block)
  options[:outline] = true
  options[:icon] = anIcon
  options[:title] = title
  _button(options, &block)
end

#p(*args, &block) ⇒ Object



201
202
203
# File 'lib/weaver/elements.rb', line 201

def p(*args, &block)
  method_missing(:p, *args, &block)
end

#panel(title, &block) ⇒ Object



110
111
112
113
114
115
116
117
# File 'lib/weaver/elements.rb', line 110

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



47
48
49
# File 'lib/weaver/elements.rb', line 47

def request_css(path)
  @page.request_css(path)
end

#request_js(path) ⇒ Object



43
44
45
# File 'lib/weaver/elements.rb', line 43

def request_js(path)
  @page.request_js(path)
end

#rootObject



39
40
41
# File 'lib/weaver/elements.rb', line 39

def root
  @page.root
end

#rounded_button(anIcon, title = {}, options = {}, &block) ⇒ Object



398
399
400
401
402
403
# File 'lib/weaver/elements.rb', line 398

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

#set_favicon_path(path) ⇒ Object



51
52
53
# File 'lib/weaver/elements.rb', line 51

def set_favicon_path(path)
  @page.set_favicon_path(path)
end

#set_favicon_type(v) ⇒ Object



55
56
57
# File 'lib/weaver/elements.rb', line 55

def set_favicon_type(v)
  @page.set_favicon_type(v)
end

#small_button(anIcon, title = {}, options = {}, &block) ⇒ Object



370
371
372
373
374
375
# File 'lib/weaver/elements.rb', line 370

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



126
127
128
129
130
131
132
133
# File 'lib/weaver/elements.rb', line 126

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 << "    $('#\#{table_name}').DataTable();\n    DATATABLE_SCRIPT\n  end\n\n  if options[:system] == :foo_table\n    table_options[:\"data-filtering\"] = true\n    table_options[:\"data-sorting\"] = true\n    table_options[:\"data-paging\"] = true\n    table_options[:\"data-show-toggle\"] = true\n    table_options[:\"data-toggle-column\"] = 'last'\n\n    table_options[:\"data-paging-size\"] = (options[:max_items_per_page] || 8).to_i.to_s\n    table_options[:class] = table_options[:class] + ' toggle-arrow-tiny'\n\n    @page.request_js 'js/plugins/footable/footable.all.min.js'\n\n    @page.request_css 'css/plugins/footable/footable.core.css'\n\n    @page.scripts << <<-DATATABLE_SCRIPT\n    $('#\#{table_name}').footable({\n      paging: {\nsize: \#{(options[:max_items_per_page] || 8).to_i}\n      }\n    });\n    $('#\#{table_name}').append(this.html).trigger('footable_redraw');\n\n\n\n    DATATABLE_SCRIPT\n\n    @page.onload_scripts << <<-SCRIPT\n    SCRIPT\n  end\n\n  method_missing(:table, table_options, &block)\n  ul class: 'pagination'\nend\n"

#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 || '&nbsp;'
          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



119
120
121
122
123
124
# File 'lib/weaver/elements.rb', line 119

def tabs(&block)
  tabs = Tabs.new(@page, @anchors)
  tabs.instance_eval(&block)

  @inner_content << tabs.generate
end

#text(theText) ⇒ Object



205
206
207
# File 'lib/weaver/elements.rb', line 205

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

#tiny_button(anIcon, title = {}, options = {}, &block) ⇒ Object



377
378
379
380
381
382
# File 'lib/weaver/elements.rb', line 377

def tiny_button(anIcon, title = {}, options = {}, &block)
  options[:size] = :xs
  options[:icon] = anIcon
  options[:title] = title
  _button(options, &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

#wform(options = {}, &block) ⇒ Object



87
88
89
90
91
# File 'lib/weaver/elements.rb', line 87

def wform(options = {}, &block)
  theform = Form.new(@page, @anchors, options, &block)
  @inner_content << theform.generate
  @page.scripts << theform.generate_script
end

#widget(options = {}, &block) ⇒ Object



254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/weaver/elements.rb', line 254

def widget(options = {}, &block)
  # gray-bg
  # white-bg
  # navy-bg
  # blue-bg
  # lazur-bg
  # yellow-bg
  # red-bg
  # black-bg

  color = "#{options[:color]}-bg" || 'navy-bg'

  div class: "widget style1 #{color}", &block
end

#write_script_once(script) ⇒ Object



63
64
65
# File 'lib/weaver/elements.rb', line 63

def write_script_once(script)
  @page.write_script_once(script)
end