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.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/opal/connect/plugins/dom.rb', line 87

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



317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/opal/connect/plugins/dom.rb', line 317

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.



85
86
87
# File 'lib/opal/connect/plugins/dom.rb', line 85

def cache
  @cache
end

#domObject (readonly) Also known as: element

Returns the value of attribute dom.



85
86
87
# File 'lib/opal/connect/plugins/dom.rb', line 85

def dom
  @dom
end

#scopeObject (readonly)

Returns the value of attribute scope.



85
86
87
# File 'lib/opal/connect/plugins/dom.rb', line 85

def scope
  @scope
end

#selectorObject (readonly)

Returns the value of attribute selector.



85
86
87
# File 'lib/opal/connect/plugins/dom.rb', line 85

def selector
  @selector
end

Instance Method Details

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



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/opal/connect/plugins/dom.rb', line 216

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



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/opal/connect/plugins/dom.rb', line 177

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



303
304
305
# File 'lib/opal/connect/plugins/dom.rb', line 303

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

#find(selector) ⇒ Object



289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/opal/connect/plugins/dom.rb', line 289

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



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/opal/connect/plugins/dom.rb', line 266

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!



118
119
120
# File 'lib/opal/connect/plugins/dom.rb', line 118

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

#nodeObject



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

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

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



146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/opal/connect/plugins/dom.rb', line 146

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



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/opal/connect/plugins/dom.rb', line 243

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



197
198
199
200
201
202
203
204
205
# File 'lib/opal/connect/plugins/dom.rb', line 197

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!



123
124
125
126
127
128
129
130
# File 'lib/opal/connect/plugins/dom.rb', line 123

def save template_name = false, remove = true
  if template_name
    cache[:"#{template_name}"] = self.to_html
    dom.remove if remove
  else
    cache[:html] = self.to_html
  end
end

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



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

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

#text(content) ⇒ Object



167
168
169
170
171
172
173
174
175
# File 'lib/opal/connect/plugins/dom.rb', line 167

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

  self
end

#tmpl(name) ⇒ Object



208
209
210
211
212
213
214
# File 'lib/opal/connect/plugins/dom.rb', line 208

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



133
134
135
136
137
138
139
140
141
142
143
# File 'lib/opal/connect/plugins/dom.rb', line 133

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



159
160
161
162
163
164
165
# File 'lib/opal/connect/plugins/dom.rb', line 159

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