Class: SuperInteraction::Command

Inherits:
Struct
  • Object
show all
Defined in:
lib/super_interaction/command.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#commandsObject

Returns the value of attribute commands.



4
5
6
# File 'lib/super_interaction/command.rb', line 4

def commands
  @commands
end

#contextObject

Returns the value of attribute context

Returns:

  • (Object)

    the current value of context



2
3
4
# File 'lib/super_interaction/command.rb', line 2

def context
  @context
end

Instance Method Details

#ajax_get(url) ⇒ Object



62
63
64
# File 'lib/super_interaction/command.rb', line 62

def ajax_get(url)
  cmd("$.get('#{url}');")
end

#alert(message) ⇒ Object



26
27
28
# File 'lib/super_interaction/command.rb', line 26

def alert(message)
  cmd("alert('#{helpers.j(message)}');")
end

#bs_alert(class_type, text) ⇒ Object



66
67
68
# File 'lib/super_interaction/command.rb', line 66

def bs_alert(class_type, text)
  cmd("if (typeof($.alert) == undefined) { alert('#{helpers.j(text)}'); } else { $.alert.#{class_type}('#{helpers.j(text)}'); }")
end

#bs_danger(message) ⇒ Object



17
18
19
20
# File 'lib/super_interaction/command.rb', line 17

def bs_danger(message)
  bs_alert('danger', message)
  self
end

#bs_notice(message) ⇒ Object



12
13
14
15
# File 'lib/super_interaction/command.rb', line 12

def bs_notice(message)
  bs_alert('info', message)
  self
end

#bs_success(message) ⇒ Object



22
23
24
# File 'lib/super_interaction/command.rb', line 22

def bs_success(message)
  bs_alert('success', message)
end

#closeObject

關閉 Modal



40
41
42
# File 'lib/super_interaction/command.rb', line 40

def close
  cmd("$.modal.close();")
end

#cmd(js_code) ⇒ Object



70
71
72
73
74
# File 'lib/super_interaction/command.rb', line 70

def cmd(js_code)
  self.commands ||= []
  self.commands.push(js_code)
  self
end

modal 裡如果有 javascript 需寫在 .modal 層 size: sm / md / lg / xl / xxl 注意:不要包 respond_to :js 會有問題



33
34
35
36
37
# File 'lib/super_interaction/command.rb', line 33

def modal(partial: nil, size: 'md', title: '', desc: '')
  partial ||= context.action_name
  modal_html = context.render_to_string(partial, layout: 'modal.html.haml', locals: { bs_modal_size: size, title: title, desc: desc })
  cmd("$(function() { $.modal.show('#{helpers.j(modal_html)}'); });")
end


54
55
56
# File 'lib/super_interaction/command.rb', line 54

def modal_saved_rediret_to(message, redirect_url)
  close.alert(message).redirect_to(redirect_url)
end


58
59
60
# File 'lib/super_interaction/command.rb', line 58

def modal_saved_reload(message)
  close.alert(message).reload
end

#redirect_to(url) ⇒ Object

導入頁面



50
51
52
# File 'lib/super_interaction/command.rb', line 50

def redirect_to(url)
  cmd("Turbolinks.visit('#{url}');");
end

#reloadObject

重新讀取頁面



45
46
47
# File 'lib/super_interaction/command.rb', line 45

def reload
  cmd("Turbolinks.visit(location.toString());");
end

#runObject



8
9
10
# File 'lib/super_interaction/command.rb', line 8

def run
  context.render js: (commands || []).join(";")
end