Class: Element

Inherits:
Array
  • Object
show all
Defined in:
lib/red_query/element.rb

Overview

represents one or more jQuery Elements

Constant Summary collapse

@@element_id_count =
0

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(jq_native) ⇒ Element

Returns a new instance of Element.



23
24
25
26
# File 'lib/red_query/element.rb', line 23

def initialize(jq_native)
  `#{self}.__jq_native__ = #{jq_native}`
  @jq_native = jq_native
end

Class Method Details

.[](css_selector) ⇒ Object



108
109
110
# File 'lib/red_query/element.rb', line 108

def self.[](css_selector)
  self.find(css_selector)
end

.from_html(html) ⇒ Object



28
29
30
# File 'lib/red_query/element.rb', line 28

def self.from_html(html)
  Element.new(jq_native_from_html(html))
end

.jq_native_from_html(html) ⇒ Object



32
33
34
# File 'lib/red_query/element.rb', line 32

def self.jq_native_from_html(html)
  `jQuery(#{html}.__value__)`
end

Instance Method Details

#[](index) ⇒ Object



7
8
9
# File 'lib/red_query/element.rb', line 7

def [](index)
  Element.new(`jQuery(#{@jq_native}[#{index}])`)
end

#add_class(css_class) ⇒ Object



76
77
78
# File 'lib/red_query/element.rb', line 76

def add_class(css_class)
  `#{@jq_native}.addClass(#{css_class}.__value__)`
end

#after(elem) ⇒ Object



55
56
57
# File 'lib/red_query/element.rb', line 55

def after(elem)
  `#{@jq_native}.after(#{elem}.__jq_native__)`
end

#append(elem) ⇒ Object



47
48
49
# File 'lib/red_query/element.rb', line 47

def append(elem)
  `#{@jq_native}.append(#{elem}.__jq_native__)`
end

#attr(name, value = nil) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/red_query/element.rb', line 63

def attr(name, value = nil)
  if value.nil?
    String.new(`#{@jq_native}.attr(#{name}.__value__)`)
  else
    `#{@jq_native}.attr(#{name}.__value__, #{value}.__value__)`
  end
end

#before(elem) ⇒ Object



59
60
61
# File 'lib/red_query/element.rb', line 59

def before(elem)
  `#{@jq_native}.before(#{elem}.__jq_native__)`
end

#click(&block) ⇒ Object



71
72
73
74
# File 'lib/red_query/element.rb', line 71

def click(&block)
  callback = mouse_event(block)
  `#{@jq_native}.click(function (event) { return #{callback}.m$call(event); })`
end

#css(key, value = nil, debug = false) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/red_query/element.rb', line 88

def css(key, value = nil, debug = false)
  if value.nil?
    String.new(`#{@jq_native}.css(#{key}.__value__)`)
  else
    `#{@jq_native}.css(#{key}.__value__, #{value}.__value__)`
  end
end

#eachObject



19
20
21
# File 'lib/red_query/element.rb', line 19

def each
  length.times { |i| yield self[i] }
end

#find(css_selector) ⇒ Object



96
97
98
99
100
# File 'lib/red_query/element.rb', line 96

def find(css_selector)
  e = Element.new(`#{@jq_native}.find(#{css_selector}.__value__)`)
#    raise "[red_query/Element.find] Not found: #{css_selector}" unless e.length > 0
  return e
end

#find_first(css_selector) ⇒ Object



102
103
104
105
106
# File 'lib/red_query/element.rb', line 102

def find_first(css_selector)
  result = find(css_selector)
  # return nil unless result
  result[0]
end

#focusObject



112
113
114
# File 'lib/red_query/element.rb', line 112

def focus
  `#{@jq_native}.focus()`
end

#has_class(css_class) ⇒ Object



80
81
82
# File 'lib/red_query/element.rb', line 80

def has_class(css_class)
  `#{@jq_native}.hasClass(#{css_class}.__value__)`
end

#heightObject



124
125
126
# File 'lib/red_query/element.rb', line 124

def height
  `#{@jq_native}.height()`
end

#hover(over_block, out_block) ⇒ Object



190
191
192
193
194
# File 'lib/red_query/element.rb', line 190

def hover(over_block, out_block)
  over_fn = mouse_event(over_block)
  out_fn = mouse_event(out_block)
  `#{@jq_native}.hover(function(event){return #{over_fn}.m$call(event);}, function(event){return #{out_fn}.m$call(event);})`
end

#html(value = nil) ⇒ Object



116
117
118
119
120
121
122
# File 'lib/red_query/element.rb', line 116

def html(value = nil)
  if value.nil?
    String.new(`#{@jq_native}.html()`)
  else
    `#{@jq_native}.html(#{value}.__value__)`
  end
end

#idObject

returns attr(‘id’) of element. if none exists, one will be assigned



37
38
39
40
41
42
43
44
45
# File 'lib/red_query/element.rb', line 37

def id
  id = attr('id')
  return id unless id == ""
  
  @@element_id_count += 1
  id = "red_query_elem_#{@@element_id_count}"
  attr('id', id)
  return id
end

#key_down(&block) ⇒ Object



148
149
150
151
# File 'lib/red_query/element.rb', line 148

def key_down(&block)
  callback = key_event(block)
  `#{@jq_native}.keydown(function (event) { return #{callback}.m$call(event); })`
end

#key_event(block) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
# File 'lib/red_query/element.rb', line 136

def key_event(block)
  Proc.new { |native_event|
    block.call({
      :code  => `#{native_event}.keyCode`,
      :shift => `#{native_event}.shiftKey`,
      :ctrl  => `#{native_event}.ctrlKey`,
      :meta  => `#{native_event}.metaKey`,
      :prevent => Proc.new { `#{native_event}.preventDefault()` },
    })
  }
end

#key_press(&block) ⇒ Object



153
154
155
156
# File 'lib/red_query/element.rb', line 153

def key_press(&block)
  callback = key_event(block)
  `#{@jq_native}.keydown(function (event) { return #{callback}.m$call(event); })`
end

#key_up(&block) ⇒ Object



158
159
160
161
# File 'lib/red_query/element.rb', line 158

def key_up(&block)
  callback = key_event(block)
  `#{@jq_native}.keydown(function (event) { return #{callback}.m$call(event); })`
end

#left(pos = nil) ⇒ Object



128
129
130
131
132
133
134
# File 'lib/red_query/element.rb', line 128

def left(pos = nil)
  if pos.nil?
    `#{@jq_native}.offset().left`
  else
    `#{@jq_native}.left(#{pos})`
  end
end

#lengthObject



11
12
13
# File 'lib/red_query/element.rb', line 11

def length
  `#{@jq_native}.length`
end

#mouse_down(&block) ⇒ Object



180
181
182
183
# File 'lib/red_query/element.rb', line 180

def mouse_down(&block)
  callback = mouse_event(block)
  `#{@jq_native}.mousedown(function (event) { return #{callback}.m$call(event); })`
end

#mouse_event(block) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/red_query/element.rb', line 163

def mouse_event(block)
  preventer = Proc.new { `#{native_event}.preventDefault()` }
  
  Proc.new { |native_event|
    # `console.log(#{native_event})`
    block.call({
      :client_x => `#{native_event}.clientX`,
      :client_y => `#{native_event}.clientY`,
      :page_x => `#{native_event}.pageX`,
      :page_y => `#{native_event}.pageY`,
      :screen_x => `#{native_event}.screenX`,
      :screen_y => `#{native_event}.screenY`,
      :prevent => preventer,
    })
  }
end

#mouse_move(&block) ⇒ Object



212
213
214
215
# File 'lib/red_query/element.rb', line 212

def mouse_move(&block)
  callback = mouse_event(block)
  `#{@jq_native}.mousemove(function (event) { return #{callback}.m$call(event); })`
end

#mouse_out(&block) ⇒ Object



217
218
219
220
# File 'lib/red_query/element.rb', line 217

def mouse_out(&block)
  callback = mouse_event(block)
  `#{@jq_native}.mouseout(function (event) { return #{callback}.m$call(event); })`
end

#mouse_over(&block) ⇒ Object

only for jquery 1.3+ def mouse_enter(&block)

callback = mouse_event(block)
`#{@jq_native}.mouseenter(function (event) { return #{callback}.m$call(event); })`

end

def mouse_leave(&block)

callback = mouse_event(block)
`#{@jq_native}.mouseleave(function (event) { return #{callback}.m$call(event); })`

end



207
208
209
210
# File 'lib/red_query/element.rb', line 207

def mouse_over(&block)
  callback = mouse_event(block)
  `#{@jq_native}.mouseover(function (event) { return #{callback}.m$call(event); })`
end

#mouse_up(&block) ⇒ Object



185
186
187
188
# File 'lib/red_query/element.rb', line 185

def mouse_up(&block)
  callback = mouse_event(block)
  `#{@jq_native}.mouseup(function (event) { return #{callback}.m$call(event); })`
end

#nameObject



222
223
224
# File 'lib/red_query/element.rb', line 222

def name
  attr("name")
end

#name=(value) ⇒ Object



226
227
228
# File 'lib/red_query/element.rb', line 226

def name=(value)
  attr("name", value)
end

#parentObject



267
268
269
# File 'lib/red_query/element.rb', line 267

def parent
  Element.new(`jQuery(#{@jq_native}.parent().get(0))`)
end

#prepend(elem) ⇒ Object



51
52
53
# File 'lib/red_query/element.rb', line 51

def prepend(elem)
  `#{@jq_native}.prepend(#{elem}.__jq_native__)`
end

#removeObject



230
231
232
# File 'lib/red_query/element.rb', line 230

def remove
  `#{@jq_native}.remove()`
end

#remove_class(css_class) ⇒ Object



84
85
86
# File 'lib/red_query/element.rb', line 84

def remove_class(css_class)
  `#{@jq_native}.removeClass(#{css_class}.__value__)`
end

#scroll_top(pos = nil) ⇒ Object



246
247
248
249
250
251
252
253
# File 'lib/red_query/element.rb', line 246

def scroll_top(pos = nil)
  if pos.nil?
    `#{@jq_native}.scrollTop()`
  else
    # `#{@jq_native}.scrollTop(#{pos})`
    `#{@jq_native}.animate({scrollTop: #{pos}}, 500, "swing")`
  end
end

#sizeObject



15
16
17
# File 'lib/red_query/element.rb', line 15

def size
  length
end

#submit(&block) ⇒ Object



234
235
236
# File 'lib/red_query/element.rb', line 234

def submit(&block)
  `#{@jq_native}.submit(function () { return #{block.call} })`
end

#top(pos = nil) ⇒ Object



238
239
240
241
242
243
244
# File 'lib/red_query/element.rb', line 238

def top(pos = nil)
  if pos.nil?
    `#{@jq_native}.offset().top`
  else
    `#{@jq_native}.top(#{pos})`
  end
end

#value(str = nil) ⇒ Object



255
256
257
258
259
260
261
# File 'lib/red_query/element.rb', line 255

def value(str = nil)
  if str.nil?
    String.new(`#{@jq_native}.val()`)
  else
    `#{@jq_native}.val(str.__value__)`
  end
end

#widthObject



263
264
265
# File 'lib/red_query/element.rb', line 263

def width
  `#{@jq_native}.width()`
end