Class: Mack::JavaScript::ScriptGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/mack-javascript/helpers/script_generator.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session_id = nil) ⇒ ScriptGenerator

Returns a new instance of ScriptGenerator.



9
10
11
12
# File 'lib/mack-javascript/helpers/script_generator.rb', line 9

def initialize(session_id = nil)
  @lines = []
  @session_id = session_id
end

Instance Attribute Details

#session_idObject (readonly)

Returns the value of attribute session_id.



7
8
9
# File 'lib/mack-javascript/helpers/script_generator.rb', line 7

def session_id
  @session_id
end

Class Method Details

.ajax_frameworkObject



123
124
125
126
127
# File 'lib/mack-javascript/helpers/script_generator.rb', line 123

def ajax_framework
  ivar_cache('ajax_framework') do
    "Mack::JavaScript::Framework::#{framework_name}Ajax".constantize
  end
end

.framework=(args) ⇒ Object



135
136
137
138
139
# File 'lib/mack-javascript/helpers/script_generator.rb', line 135

def framework=(args)
  @ajax_framework = nil
  @selector_framework = nil
  @@framework_name = args.camelcase
end

.selector_frameworkObject



129
130
131
132
133
# File 'lib/mack-javascript/helpers/script_generator.rb', line 129

def selector_framework
  ivar_cache('selector_framework') do
    "Mack::JavaScript::Framework::#{framework_name}Selector".constantize
  end
end

Instance Method Details

#<<(s, options = {}) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/mack-javascript/helpers/script_generator.rb', line 38

def <<(s, options = {})
  if options[:add_to_last]
    @lines.last << s
  else
    @lines << s
  end
end

#ajax(options) ⇒ Object



31
32
33
34
35
36
# File 'lib/mack-javascript/helpers/script_generator.rb', line 31

def ajax(options)
  unless configatron.mack.disable_forgery_detector || !session_id
    options.merge!(:authenticity_token => Mack::Utils::AuthenticityTokenDispenser.instance.dispense_token(session_id))
  end
  self << self.class.ajax_framework.remote_function(options)
end

#alert(message) ⇒ Object



52
53
54
# File 'lib/mack-javascript/helpers/script_generator.rb', line 52

def alert(message)
  self << "alert('#{message}')"
end

#assign(variable, value) ⇒ Object



67
68
69
# File 'lib/mack-javascript/helpers/script_generator.rb', line 67

def assign(variable, value)
  self << "#{variable} = #{value.to_json}"
end

#call(*args, &block) ⇒ Object



56
57
58
59
60
61
# File 'lib/mack-javascript/helpers/script_generator.rb', line 56

def call(*args, &block)
  s = args.shift + '('
  a = []
  args.each {|arg| a << arg.to_json}
  self << s + a.join(',') + ')'
end

#delay(seconds = 1, &block) ⇒ Object



71
72
73
# File 'lib/mack-javascript/helpers/script_generator.rb', line 71

def delay(seconds = 1, &block)
  self << "setTimeout(#{function.body(&block)}, #{(seconds * 1000).to_i})"
end

#function(*args) ⇒ Object



63
64
65
# File 'lib/mack-javascript/helpers/script_generator.rb', line 63

def function(*args)
  Mack::JavaScript::Function.new(session_id, *args)
end

#hide(*ids) ⇒ Object



107
108
109
110
111
112
# File 'lib/mack-javascript/helpers/script_generator.rb', line 107

def hide(*ids)
  deprecate_method(:hide, 'page.select("#id1", "#id2").hide', '0.8.3')
  ids = [ids] if ids.is_a? String
  ids.collect! {|id| "##{id}"}
  self.select(*ids).hide
end

#insert_html(position, id, html) ⇒ Object

– Deprecated Methods –#



78
79
80
81
# File 'lib/mack-javascript/helpers/script_generator.rb', line 78

def insert_html(position, id, html)
  deprecate_method(:insert_html, 'page.select("#id").insert(position, html)', '0.8.3')
  self.select("##{id}").insert(position, html)
end

#remove(*ids) ⇒ Object



93
94
95
96
97
98
# File 'lib/mack-javascript/helpers/script_generator.rb', line 93

def remove(*ids)
  deprecate_method(:remove, 'page.select("#id1", "#id2").remove', '0.8.3')
  ids = [ids] if ids.is_a? String
  ids.collect! {|id| "##{id}"}
  self.select(*ids).remove
end

#replace(id, html) ⇒ Object



88
89
90
91
# File 'lib/mack-javascript/helpers/script_generator.rb', line 88

def replace(id, html)
  deprecate_method(:replace, 'page.select("#id").replace(:outer, html)', '0.8.3')
  self.select("##{id}").replace(:outer, html).to_s
end

#replace_html(id, html) ⇒ Object



83
84
85
86
# File 'lib/mack-javascript/helpers/script_generator.rb', line 83

def replace_html(id, html)
  deprecate_method(:replace_html, 'page.select("#id").replace(:inner, html)', '0.8.3')
  self.select("##{id}").replace(:inner, html).to_s
end

#select(*selector) ⇒ Object

selects elements on the page using css3 selectors For more info: www.w3.org/TR/css3-selectors/ A few useful examples: ‘div’ would select all divs. ‘.full’ would select all elements with class ‘full’. ‘div.blah’ would select all divs with class ‘blah’. ‘#foo’ would select the element with id ‘foo’

select can take multiple selector strings. For instance page.select(‘ul.blah’, ‘#foo’, ‘.full’) would give you access to a collection of elements containing all uls with class ‘blah’, the element with id ‘foo’ and every element with class ‘full’. See JquerySelector or PrototypeSelector for available methods on the returned collection.



27
28
29
# File 'lib/mack-javascript/helpers/script_generator.rb', line 27

def select(*selector)
  self.class.selector_framework.new(self, *selector)
end

#show(*ids) ⇒ Object



100
101
102
103
104
105
# File 'lib/mack-javascript/helpers/script_generator.rb', line 100

def show(*ids)
  deprecate_method(:show, 'page.select("#id1", "#id2").show', '0.8.3')
  ids = [ids] if ids.is_a? String
  ids.collect! {|id| "##{id}"}
  self.select(*ids).show
end

#to_sObject



46
47
48
49
50
# File 'lib/mack-javascript/helpers/script_generator.rb', line 46

def to_s
  string = @lines.join(';')
  string << ';' unless string =~ /;$/
  string
end

#toggle(*ids) ⇒ Object



114
115
116
117
118
119
# File 'lib/mack-javascript/helpers/script_generator.rb', line 114

def toggle(*ids)
  deprecate_method(:toggle, 'page.select("#id1", "#id2").toggle', '0.8.3')
  ids = [ids] if ids.is_a? String
  ids.collect! {|id| "##{id}"}
  self.select(*ids).toggle
end