Class: DYI::Script::EcmaScript::EventListener

Inherits:
Function show all
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.

Since:

  • 1.0.0

Instance Attribute Summary

Attributes inherited from Function

#arguments, #name

Attributes inherited from SimpleScript

#body, #content_type

Instance Method Summary collapse

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.

Parameters:

  • body (String)

    body of client scripting

  • name (String) (defaults to: nil)

    a function name

  • argument (String) (defaults to: 'evt')

    argument’s name

Since:

  • 1.0.0



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

#contentsString

Returns string expression of this function in ECMAScript

Returns:

  • (String)

    expression in ECMAScript

Since:

  • 1.0.3



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

Relates this object to an event.

Parameters:

  • event (Event)

    an event that is related to

Since:

  • 1.0.0



390
391
392
393
394
# File 'lib/dyi/script/ecmascript.rb', line 390

def related_to(event)
  unless @events.include?(event)
    @events << event
  end
end

#unrelated_to(event) ⇒ Object

Removes the relation to an event.

Parameters:

  • event (Event)

    an event that is removed the relation to

Since:

  • 1.0.0



398
399
400
# File 'lib/dyi/script/ecmascript.rb', line 398

def unrelated_to(event)
  @events.delete(event)
end