Class: DYI::Script::EcmaScript::EventListener
- Inherits:
-
Function
- Object
- SimpleScript
- Function
- DYI::Script::EcmaScript::EventListener
- Defined in:
- lib/dyi/script/ecmascript.rb
Overview
Class representing a event listener of ECMAScript. The scripting becomes effective only when it is output by SVG format.
Instance Attribute Summary
Attributes inherited from Function
Attributes inherited from SimpleScript
Instance Method Summary collapse
-
#contents ⇒ String
Returns string expression of this function in ECMAScript.
-
#initialize(body, name = nil, argument = 'evt') ⇒ EventListener
constructor
A new instance of EventListener.
-
#related_to(event) ⇒ Object
Relates this object to an event.
-
#unrelated_to(event) ⇒ Object
Removes the relation to an event.
Methods inherited from SimpleScript
#append_body, #has_uri_reference?, #include_external_file?, #write_as
Constructor Details
#initialize(body, name = nil, argument = 'evt') ⇒ EventListener
Returns a new instance of EventListener.
383 384 385 386 |
# File 'lib/dyi/script/ecmascript.rb', line 383 def initialize(body, name=nil, argument='evt') super @events = [] end |
Instance Method Details
#contents ⇒ String
Returns string expression of this function in ECMAScript
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 |
# File 'lib/dyi/script/ecmascript.rb', line 405 def contents if name super else parts = [] parts << "document.addEventListener(\"DOMContentLoaded\", function(evt){\n" @events.each do |event| if event.event_name == :load parts << @body else if event.target.root_element? parts << ' document.documentElement.addEventListener("' else parts << ' document.getElementById("' parts << event.target.id parts << '").addEventListener("' end parts << event.event_name parts << '", ' parts << super parts << ", false);\n" end end parts << "\n}, false);\n" parts.join end end |
#related_to(event) ⇒ Object
Relates this object to an event.
390 391 392 393 394 |
# File 'lib/dyi/script/ecmascript.rb', line 390 def (event) unless @events.include?(event) @events << event end end |
#unrelated_to(event) ⇒ Object
Removes the relation to an event.
398 399 400 |
# File 'lib/dyi/script/ecmascript.rb', line 398 def (event) @events.delete(event) end |