Class: Aurita::GUI::Javascript

Inherits:
Object
  • Object
show all
Defined in:
lib/aurita-gui/html.rb

Overview

A minimalistic generator for javascript calls. Usage:

my_element.onclick = Javascript.new("alert('an alert message');")

or just:

my_element.onclick = Javascript.my_function('foo', 2, 'bar')
--> my_function('foo', 2, 'bar');

For namespaces:

my_element.onclick = Javascript[:Foo].my_function(1,2)
--> Foo.my_function('foo', 2, 'bar');

Method #string renders the code passed in constructir, enclosed in <script> tags:

Javascript.new(“alert(‘message’);”).string

--> 
<script language="Javascript" type="text/javascript">
alert('message'); 
</script>

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(script) ⇒ Javascript

Returns a new instance of Javascript.



111
112
113
# File 'lib/aurita-gui/html.rb', line 111

def initialize(script)
  @script = script
end

Class Method Details

.[](namespace) ⇒ Object



127
128
129
# File 'lib/aurita-gui/html.rb', line 127

def self.[](namespace) 
  return self.new(namespace.to_s + '.')
end

.method_missing(meth, *args) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/aurita-gui/html.rb', line 131

def self.method_missing(meth, *args)
  args = args.collect { |arg|
    if arg.instance_of? String then
      '\'' << arg << '\''
    else
      arg
    end
  }
  args_string = args.join(',')
  meth.to_s << '(' << args_string + ');'
end

Instance Method Details

#code_blockObject

Renders script to a <script> block.



116
117
118
119
120
# File 'lib/aurita-gui/html.rb', line 116

def code_block
  '<script language="Javascript" type="text/javascript">' << "\n" << 
    @script + "\n" << 
  '</script>'
end

#stringObject Also known as: to_s



122
123
124
# File 'lib/aurita-gui/html.rb', line 122

def string
  @script
end