Class: EasyModalWindow::Dialog

Inherits:
Object
  • Object
show all
Defined in:
lib/easy_modal_window.rb

Constant Summary collapse

ALLOVED_OPTIONS =
[:window_selector, :title_selector, :render_template, :formats,
:resizable, :height, :width, :buttons, :before_close, :success_message,
:object, :container_class, :element_class, :errors_group_class].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Dialog

Returns a new instance of Dialog.



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
38
39
# File 'lib/easy_modal_window.rb', line 13

def initialize(args)
  @modal_options = {}
  @modal_options[:window_selector] = '#ajax-modal'
  @modal_options[:title_selector] = 'h3.title'
  @modal_options[:render_template] = ''
  @modal_options[:formats] = [:html]
  @modal_options[:resizable] = true
  @modal_options[:height] = 'auto'
  @modal_options[:width] = 'auto'
  # hash with button descriptions: {first_button: {name: 'Name',
  #                                                condition: -> {...},
  #                                                action: 'location.reload();'},
  #                                 second_button: ...}
  @modal_options[:buttons] = {}
  # dialog callbacks
  # {condition: ->{...}, action: '...'}
  @modal_options[:before_close] = {}

  # options for result templates
  @modal_options[:success_message] = 'All right'
  @modal_options[:object] = nil
  @modal_options[:container_class] = ''
  @modal_options[:element_class] = ''
  @modal_options[:errors_group_class] = ''

  @modal_options.merge!(args.slice(*EasyModalWindow::Dialog::ALLOVED_OPTIONS))
end

Instance Attribute Details

Returns the value of attribute modal_options.



7
8
9
# File 'lib/easy_modal_window.rb', line 7

def modal_options
  @modal_options
end

Instance Method Details

#before_closeObject

TODO: simplify this method



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/easy_modal_window.rb', line 50

def before_close
  result = {}
  if @modal_options[:before_close].has_key?(:action) && @modal_options[:before_close][:action].present?
    result[:action] = @modal_options[:before_close][:action]
  else
    result[:condition] = false
    result[:action] = ''
    return result
  end

  result[:condition] = @modal_options[:before_close][:condition].nil? || value_of(@modal_options[:before_close][:condition])

  result
end

#buttonsObject



65
66
67
68
69
70
71
72
73
74
# File 'lib/easy_modal_window.rb', line 65

def buttons
  @modal_options[:buttons].inject([]) do |h, v|
    option = {}
    option[:name] = v.last[:name] || 'unknown'
    option[:condition] = value_of(v.last[:condition]).nil? || value_of(v.last[:condition])
    option[:action] = v.last[:action] || ''
    h << option
    h
  end
end