Class: Opal::Connect::ConnectPlugins::Dom::Instance

Inherits:
Object
  • Object
show all
Defined in:
lib/opal/connect/plugins/dom.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(selector, cache, scope) ⇒ Instance

Returns a new instance of Instance.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/opal/connect/plugins/dom.rb', line 80

def initialize(selector, cache, scope)
  @selector = selector
  @cache    = cache
  @scope    = scope

  if selector.is_a?(String)
    if RUBY_ENGINE == 'opal'
      @dom = Element[selector]
    else
      # multi-line
      if selector["\n"] || selector['html']
        @dom = Oga.parse_html(selector)
      else
        @dom = cache[:html]
        @dom = dom.css(selector) unless selector == 'html'
      end
    end
  else
    @dom = selector
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

This allows you to use all the oga or opal jquery methods if a global one isn’t set



324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/opal/connect/plugins/dom.rb', line 324

def method_missing(method, *args, &block)
  if RUBY_ENGINE == 'opal' && node.respond_to?(method, true)
    n = node.send(method, *args, &block)
  elsif RUBY_ENGINE != 'opal'
    if node.is_a?(Oga::XML::NodeSet) && node.first.respond_to?(method, true)
      n = node.first.send(method, *args, &block)
    elsif node.respond_to?(method, true)
      n = node.send(method, *args, &block)
    else
      super
    end
  else
    super
  end

  if RUBY_ENGINE == 'opal'
    n.is_a?(Element) ? Instance.new(n, cache, scope) : n
  else
    n.class.name['Oga::'] ? Instance.new(n, cache, scope) : n
  end
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



78
79
80
# File 'lib/opal/connect/plugins/dom.rb', line 78

def cache
  @cache
end

#domObject (readonly) Also known as: element

Returns the value of attribute dom.



78
79
80
# File 'lib/opal/connect/plugins/dom.rb', line 78

def dom
  @dom
end

#scopeObject (readonly)

Returns the value of attribute scope.



78
79
80
# File 'lib/opal/connect/plugins/dom.rb', line 78

def scope
  @scope
end

#selectorObject (readonly)

Returns the value of attribute selector.



78
79
80
# File 'lib/opal/connect/plugins/dom.rb', line 78

def selector
  @selector
end

Instance Method Details

#append(content = false, &block) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/opal/connect/plugins/dom.rb', line 223

def append(content = false, &block)
  # content becomes scope in this case
  content = HTML::DSL.scope!(content).html!(&block).to_html if block_given?

  if RUBY_ENGINE == 'opal'
    if content.is_a? Dom::Instance
      node.append(content.node)
    else
      node.append(content)
    end
  else
    if content.is_a? Dom::Instance
      content = content.node.children
    else
      content = Oga.parse_html(content).children
    end

    if node.is_a?(Oga::XML::NodeSet)
      node.each { |n| n.children = (n.children + content) }
    else
      node.children = (node.children + content)
    end
  end

  # self
end

#attr(key, value = false) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/opal/connect/plugins/dom.rb', line 184

def attr(key, value = false)
  if value
    value = value.to_s

    if node.respond_to? :set
      node.set(key, value)
    else
      node.each { |n| n.set(key, value) }
    end

    self
  else
    if node.respond_to? :get
      node.get(key)
    else
      node.first.get(key)
    end
  end
end

#eachObject



310
311
312
# File 'lib/opal/connect/plugins/dom.rb', line 310

def each
  node.each { |n| yield Instance.new(n, cache, scope) }
end

#find(selector) ⇒ Object



296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/opal/connect/plugins/dom.rb', line 296

def find(selector)
  new_node = if RUBY_ENGINE == 'opal'
    node.find(selector)
  else
    if node.is_a? Oga::XML::NodeSet
      node.first.css(selector)
    else
      node.css(selector)
    end
  end

  Instance.new(new_node, cache, scope)
end

#html(content = false, &block) ⇒ Object



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/opal/connect/plugins/dom.rb', line 273

def html(content = false, &block)
  # content becomes scope in this case
  content = HTML::DSL.scope!(content).html!(&block).to_html if block_given?

  if RUBY_ENGINE == 'opal'
    node.html(content)
  else
    if content.is_a? Dom::Instance
      content = content.children
    else
      content = Oga.parse_html(content).children
    end

    if node.is_a?(Oga::XML::NodeSet)
      node.each { |n| n.children = content }
    else
      node.children = content
    end
  end

  self
end

#load(html) ⇒ Object Also known as: load!



111
112
113
# File 'lib/opal/connect/plugins/dom.rb', line 111

def load html
  Instance.new(html, cache, scope)
end

#nodeObject



314
315
316
317
318
319
320
# File 'lib/opal/connect/plugins/dom.rb', line 314

def node
  if self.dom.respond_to? :dom
    self.dom.dom
  else
    self.dom
  end
end

#on(name, selector = false, &handler) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/opal/connect/plugins/dom.rb', line 139

def on(name, selector = false, &handler)
  if scope.respond_to?(:connect_events_started)
    wrapper = -> (e) do
      scope.connect_events_started(e, name, selector)
      scope.instance_exec(e, &handler)
    end

    node.on(name, selector, &wrapper)
  else
    node.on(name, selector, &handler)
  end
end

#prepend(content = false, &block) ⇒ Object



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/opal/connect/plugins/dom.rb', line 250

def prepend(content = false, &block)
  # content becomes scope in this case
  content = HTML::DSL.scope!(content).html!(&block).to_html if block_given?

  if RUBY_ENGINE == 'opal'
    node.prepend(content)
  else
    if content.is_a? Dom::Instance
      content = content.children
    else
      content = Oga.parse_html(content).children
    end

    if node.is_a?(Oga::XML::NodeSet)
      node.each { |n| n.children = (content + n.children) }
    else
      node.children = (content + children)
    end
  end

  self
end

#removeObject



204
205
206
207
208
209
210
211
212
# File 'lib/opal/connect/plugins/dom.rb', line 204

def remove
  if node.respond_to? :remove
    node.remove
  else
    node.each { |n| n.remove }
  end

  self
end

#save(template_name = false, remove = true) ⇒ Object Also known as: save!



116
117
118
119
120
121
122
123
# File 'lib/opal/connect/plugins/dom.rb', line 116

def save template_name = false, remove = true
  if template_name
    cache[:"#{template_name}"] = self.to_html
    dom.remove if !dom.is_a?(Oga::XML::Document) && remove
  else
    cache[:html] = self.to_html
  end
end

#set(html) ⇒ Object Also known as: set!



104
105
106
107
108
# File 'lib/opal/connect/plugins/dom.rb', line 104

def set html
  @dom = Instance.new(html, cache, scope)
  @dom.save!
  @dom
end

#text(content = false) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/opal/connect/plugins/dom.rb', line 160

def text(content = false)
  if content
    if node.respond_to?(:inner_text)
      node.inner_text = content
    else
      node.each { |n| n.inner_text = content }
    end

    self
  else
    if node.respond_to?(:inner_text)
      node.inner_text
    else
      text = ''
      node.each { |n| text << n.inner_text }
      text
    end
  end
end

#tmpl(name) ⇒ Object



215
216
217
218
219
220
221
# File 'lib/opal/connect/plugins/dom.rb', line 215

def tmpl(name)
  if cached_tmpl = cache[:"#{name}"]
    Instance.new(cached_tmpl, cache, scope)
  else
    puts "There is no template `#{name}`"
  end
end

#to_htmlObject



126
127
128
129
130
131
132
133
134
135
136
# File 'lib/opal/connect/plugins/dom.rb', line 126

def to_html
  if RUBY_ENGINE == 'opal'
    node.html
  else
    if node.respond_to?(:first)
      node.first.to_xml
    else
      node.to_xml
    end
  end
end

#to_sObject



152
153
154
155
156
157
158
# File 'lib/opal/connect/plugins/dom.rb', line 152

def to_s
  if dom.respond_to?(:first)
    dom.first.to_xml
  else
    dom.to_xml
  end
end

#val(value = false) ⇒ Object



180
181
182
# File 'lib/opal/connect/plugins/dom.rb', line 180

def val(value = false)
  self.attr('value', value)
end