Class: Cyberweb::JavascriptMagic

Inherits:
Object
  • Object
show all
Defined in:
lib/cyberweb/javascript/javascript_magic.rb

Overview

Cyberweb::JavascriptMagic

Constant Summary collapse

ARRAY_APPLY_JAVASCRIPT_MAGIC_ON_THESE_ELEMENTS =
#

ARRAY_APPLY_JAVASCRIPT_MAGIC_ON_THESE_ELEMENTS

#
%w(
  on_mouse_over_underline
  popup
  focus_and_select
  redirect
)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(i = nil) ⇒ JavascriptMagic

#

initialize

#


39
40
41
42
43
44
45
46
47
# File 'lib/cyberweb/javascript/javascript_magic.rb', line 39

def initialize(
    i = nil
  )
  if i
    assign_to_original_input(i.dup)
    assign_to_string(@original_input)
    translate
  end
end

Class Method Details

.[](i) ⇒ Object

#

Cyberweb::JavascriptMagic[]

#


139
140
141
# File 'lib/cyberweb/javascript/javascript_magic.rb', line 139

def self.[](i)
  new(i)
end

.is_included?(i) ⇒ Boolean

#

Cyberweb::JavascriptMagic.is_included?

Query to the above Array.

#

Returns:

  • (Boolean)


32
33
34
# File 'lib/cyberweb/javascript/javascript_magic.rb', line 32

def self.is_included?(i)
  ARRAY_APPLY_JAVASCRIPT_MAGIC_ON_THESE_ELEMENTS.include? i
end

Instance Method Details

#_Object Also known as: translated?

#

_

#


111
112
113
# File 'lib/cyberweb/javascript/javascript_magic.rb', line 111

def _ # Easier access method to query for the @i variable.
  @i
end

#assign_to_original_input(i) ⇒ Object

#

assign_to_original_input

#


52
53
54
# File 'lib/cyberweb/javascript/javascript_magic.rb', line 52

def assign_to_original_input(i)
  @original_input = i
end

#assign_to_string(i) ⇒ Object

#

assign_to_string

#


59
60
61
# File 'lib/cyberweb/javascript/javascript_magic.rb', line 59

def assign_to_string(i) # use only this method when modifying @i.
  @i = i.dup
end

#test(this_string, be_verbose = false) ⇒ Object Also known as: test_this_string

#

test

The second argument determines whether we shall be verbose or whether we shall not.

#


121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/cyberweb/javascript/javascript_magic.rb', line 121

def test(this_string, be_verbose = false)
  case be_verbose
  when :be_verbose
    be_verbose = true
  end
  _ = this_string
  assign_to_original_input(_)
  translate(_)
  if be_verbose
    print "The string `#{_}` translates to: "
  end
  e @i
  @i
end

#translate(this_string = @i) ⇒ Object

#

translate

This is the powerhorse of the class. It is automatically called whenever a new instance is made.

Note that all the following .include? checks in the code below, must also be synced to the main ARRAY on top of this file.

#


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/cyberweb/javascript/javascript_magic.rb', line 72

def translate(
    this_string = @i
  ) # translate between the magic.
  _ = this_string
  assign_to_string(_)
  # ======================================================================= #
  # === underline
  # ======================================================================= #
  if _.include? 'on_mouse_over_underline'
    @i.gsub!(/on_mouse_over_underline/,
      'onmouseover="this.style.textDecoration=\'underline\';"')
  end
  # ======================================================================= #
  # === popup
  # ======================================================================= #
  if _.include? 'popup'
    @i.gsub!(/popup/, 'onclick="return popup(this.href)";')
  end
  # ======================================================================= #
  # === focus_and_select
  # ======================================================================= #
  if _.include? 'focus_and_select'
    @i.gsub!(/popup/, 'onclick="this.focus();this.select()";')
  end
  # ======================================================================= #
  # === redirect
  # ======================================================================= #
  if _.include? 'redirect' # handle redirect syntactic sugar.
    regex = /redirect_to\((.+)\)/
    match_data_object = regex.match(@i)
    match_data_object[1]
    @i.gsub!(regex, 'window.location.href='+match_data_object[1]+';')
  end
  @i
end