opal-jquery 0.0.6 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ ## 0.0.7 2013-02-20
2
+
3
+ * Add Element#method_missing which forwards missing calls to try and call
4
+ method as a native jquery function/plugin.
5
+
6
+ * Depreceate Document finder methods (Document.find, Document[]). The finder
7
+ methods on Element now replace them. Updated specs to suit.
data/Gemfile CHANGED
@@ -1,8 +1,5 @@
1
- source :rubygems
1
+ source "https://rubygems.org"
2
2
  gemspec
3
3
 
4
4
  gem 'rake'
5
- gem 'sprockets'
6
-
7
- gem 'opal', '~> 0.3.36'
8
- gem 'opal-spec', '~> 0.2.10'
5
+ gem 'opal-spec', '~> 0.2.12'
@@ -1,28 +1,4 @@
1
1
  module Document
2
- def self.[](selector)
3
- `$(#{selector})`
4
- end
5
-
6
- def self.find(selector)
7
- self[selector]
8
- end
9
-
10
- def self.id(id)
11
- %x{
12
- var el = document.getElementById(id);
13
-
14
- if (!el) {
15
- return nil;
16
- }
17
-
18
- return $(el);
19
- }
20
- end
21
-
22
- def self.parse(str)
23
- `$(str)`
24
- end
25
-
26
2
  def self.ready?(&block)
27
3
  %x{
28
4
  if (block === nil) {
@@ -2,11 +2,19 @@
2
2
  # native dom elements.
3
3
  class Element < `jQuery`
4
4
  def self.find(selector)
5
- `$(selector)`
5
+ `$(#{selector})`
6
6
  end
7
7
 
8
8
  def self.id(id)
9
- Document.id(id)
9
+ %x{
10
+ var el = document.getElementById(id);
11
+
12
+ if (!el) {
13
+ return nil;
14
+ }
15
+
16
+ return $(el);
17
+ }
10
18
  end
11
19
 
12
20
  def self.new(tag = 'div')
@@ -17,59 +25,50 @@ class Element < `jQuery`
17
25
  `$(str)`
18
26
  end
19
27
 
20
- def [](name)
21
- `#{self}.attr(name) || ""`
28
+ def self.expose(*methods)
29
+ %x{
30
+ for (var i = 0, length = methods.length, method; i < length; i++) {
31
+ method = methods[i];
32
+ #{self}.prototype['$' + method] = #{self}.prototype[method];
33
+ }
34
+
35
+ return nil;
36
+ }
22
37
  end
23
38
 
24
- alias_native :[]=, :attr
25
-
26
- # Add the given content to inside each element in the receiver. The
27
- # content may be a HTML string or a `DOM` instance. The inserted
28
- # content is added to the end of the receiver.
29
- #
30
- # @example Given HTML String
31
- #
32
- # DOM.find('ul').append '<li>list content</li>'
33
- #
34
- # @example Given an existing DOM node
35
- #
36
- # DOM.id('checkout') << Dom.id('total-price-label')
37
- #
38
- # @param [String, DOM] content HTML string or DOM content
39
- # @return [DOM] returns receiver
40
- alias_native :<<, :append
39
+ expose :after, :before, :parent, :parents, :prepend, :prev, :remove
40
+ expose :hide, :show, :toggle, :children, :blur, :closest, :data
41
+ expose :focus, :find, :next, :siblings, :text, :trigger, :append
41
42
 
43
+ alias_native :[]=, :attr
42
44
  alias_native :add_class, :addClass
45
+ alias_native :append_to, :appendTo
46
+ alias_native :has_class?, :hasClass
47
+ alias_native :html=, :html
48
+ alias_native :remove_attr, :removeAttr
49
+ alias_native :remove_class, :removeClass
50
+ alias_native :text=, :text
51
+ alias_native :toggle_class, :toggleClass
52
+ alias_native :value=, :val
43
53
 
44
- # Add the given content after each element in the receiver. The given
45
- # content may be a HTML string, or a `DOM` instance.
46
- #
47
- # @example Given HTML String
48
- #
49
- # DOM.find('.label').after '<p>Content after label</>'
50
- #
51
- # @example Given existing DOM nodes
52
- #
53
- # DOM.find('.price').after DOM.id('checkout-link')
54
- #
55
- # @param [String, DOM] content HTML string or dom content
56
- # @return [DOM] returns self
57
- alias_native :after, :after
58
-
59
- alias append <<
54
+ # Missing methods are assumed to be jquery plugins. These are called by
55
+ # the given symbol name.
56
+ def method_missing(symbol, *args, &block)
57
+ %x{
58
+ if (#{self}[#{symbol}]) {
59
+ return #{self}[#{symbol}].apply(#{self}, args);
60
+ }
61
+ }
62
+
63
+ super
64
+ end
60
65
 
61
- # Appends the elements in #{self} object into the target element. #{self}
62
- # method is the reverse of using `#append` on the target with #{self}
63
- # instance as the argument.
64
- #
65
- # @example
66
- #
67
- # DOM.parse('<p>Hello</p>').append_to DOM.id('foo')
68
- #
69
- # @param [DOM] target the target to insert into
70
- # @return [DOM] returns the receiver
71
- alias_native :append_to, :appendTo
66
+ def [](name)
67
+ `#{self}.attr(name) || ""`
68
+ end
72
69
 
70
+ alias << append
71
+
73
72
  def append_to_body
74
73
  `#{self}.appendTo(document.body)`
75
74
  end
@@ -106,33 +105,6 @@ class Element < `jQuery`
106
105
  }
107
106
  end
108
107
 
109
- # Insert the given content into the DOM before each element in #{self}
110
- # collection. The content may be a raw HTML string or a `DOM`
111
- # instance containing elements.
112
- #
113
- # @example
114
- #
115
- # # Given a string
116
- # DOM('.foo').before '<p class="title"></p>'
117
- #
118
- # # Using an existing element
119
- # DOM('.bar').before DOM('#other-title')
120
- #
121
- # @param [DOM, String] content the content to insert before
122
- # @return [DOM] returns the receiver
123
- alias_native :before, :before
124
-
125
- # Returns a new collection containing the immediate children of each
126
- # element in #{self} collection. The result may be empty if no children
127
- # are present.
128
- #
129
- # @example
130
- #
131
- # DOM('#foo').children # => DOM instance
132
- #
133
- # @return [DOM] returns new DOM collection
134
- alias_native :children, :children
135
-
136
108
  # Returns the CSS class name of the firt element in #{self} collection.
137
109
  # If the collection is empty then an empty string is returned. Only
138
110
  # the class name of the first element will ever be returned.
@@ -146,12 +118,7 @@ class Element < `jQuery`
146
118
  def class_name
147
119
  %x{
148
120
  var first = #{self}[0];
149
-
150
- if (!first) {
151
- return "";
152
- }
153
-
154
- return first.className || "";
121
+ return (first && first.className) || "";
155
122
  }
156
123
  end
157
124
 
@@ -226,12 +193,6 @@ class Element < `jQuery`
226
193
  }
227
194
  end
228
195
 
229
- alias_native :blur, :blur
230
-
231
- alias_native :closest, :closest
232
-
233
- alias_native :data, :data
234
-
235
196
  # Yields each element in #{self} collection in turn. The yielded element
236
197
  # is wrapped as a `DOM` instance.
237
198
  #
@@ -271,42 +232,18 @@ class Element < `jQuery`
271
232
  map {|el| el }
272
233
  end
273
234
 
274
- # Find all the elements that match the given `selector` within the
275
- # scope of elements in #{self} given collection. Might return an empty
276
- # collection if no elements match.
277
- #
278
- # @example
279
- #
280
- # form = DOM('#login-form')
281
- # form.find 'input, select'
282
- #
283
- # @param [String] selector the selector to match elements against
284
- # @return [DOM] returns new collection
285
- alias_native :find, :find
286
-
287
235
  def first
288
236
  `#{self}.length ? #{self}.first() : nil`
289
237
  end
290
238
 
291
- alias_native :focus, :focus
292
-
293
- alias_native :has_class?, :hasClass
294
-
295
239
  def html
296
240
  `#{self}.html() || ""`
297
241
  end
298
242
 
299
- alias_native :html=, :html
300
-
301
243
  def id
302
244
  %x{
303
245
  var first = #{self}[0];
304
-
305
- if (!first) {
306
- return "";
307
- }
308
-
309
- return first.id || "";
246
+ return (first && first.id) || "";
310
247
  }
311
248
  end
312
249
 
@@ -344,67 +281,20 @@ class Element < `jQuery`
344
281
  `#{self}.length`
345
282
  end
346
283
 
347
- alias_native :next, :next
348
-
349
- alias_native :siblings, :siblings
350
-
351
- def on(name, selector = nil, &handler)
352
- %x{
353
- if (selector === nil) {
354
- #{self}.on(name, handler);
355
- }
356
- else {
357
- #{self}.on(name, selector, handler);
358
- }
359
-
360
- return handler;
361
- }
284
+ def on(name, sel = nil, &block)
285
+ `sel === nil ? #{self}.on(name, block) : #{self}.on(name, sel, block)`
286
+ block
362
287
  end
363
288
 
364
- def off(name, selector = nil, &handler)
365
- %x{
366
- if (selector === nil) {
367
- return #{self}.off(name, handler);
368
- }
369
- else {
370
- return #{self}.off(name, selector, handler);
371
- }
372
- }
289
+ def off(name, sel = nil, &block)
290
+ `sel === nil ? #{self}.off(name, block) : #{self}.off(name, sel, block)`
373
291
  end
374
292
 
375
- alias_native :remove_attr, :removeAttr
376
-
377
- alias_native :parent, :parent
378
-
379
- alias_native :parents, :parents
380
-
381
- alias_native :prepend, :prepend
382
-
383
- alias_native :prev, :prev
384
-
385
- alias_native :remove, :remove
386
-
387
- alias_native :remove_class, :removeClass
388
-
389
293
  alias size length
390
294
 
391
295
  alias succ next
392
296
 
393
- alias_native :text, :text
394
- alias_native :text=, :text
395
-
396
- alias_native :toggle_class, :toggleClass
397
-
398
- alias_native :trigger, :trigger
399
-
400
297
  def value
401
298
  `#{self}.val() || ""`
402
299
  end
403
-
404
- alias_native :value=, :val
405
-
406
- # display functions
407
- alias_native :hide, :hide
408
- alias_native :show, :show
409
- alias_native :toggle, :toggle
410
300
  end
@@ -1,5 +1,5 @@
1
1
  module Opal
2
2
  module JQuery
3
- VERSION = '0.0.6'
3
+ VERSION = '0.0.7'
4
4
  end
5
5
  end
data/opal-jquery.gemspec CHANGED
@@ -14,4 +14,6 @@ Gem::Specification.new do |s|
14
14
  s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
15
15
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
16
  s.require_paths = ['lib']
17
+
18
+ s.add_runtime_dependency 'opal', '~> 0.3.39'
17
19
  end
@@ -1,79 +1,6 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe Document do
4
- html <<-HTML
5
- <div id="foo" class="bar"></div>
6
- <div class="woosh"></div>
7
- <div class="woosh"></div>
8
- <div class="find-foo"></div>
9
- <div class="find-bar"></div>
10
- <div class="find-foo"></div>
11
- HTML
12
-
13
- describe ".[]" do
14
- it "should be able to find elements with given id" do
15
- Document['#foo'].class_name.should == "bar"
16
- Document['#foo'].size.should == 1
17
- end
18
-
19
- it "should be able to match any valid CSS selector" do
20
- Document['.woosh'].should be_kind_of(Element)
21
- Document['.woosh'].size.should == 2
22
- end
23
-
24
- it "should return an empty Elements instance when not matching any elements" do
25
- dom = Document['.some-non-existing-class']
26
-
27
- dom.should be_kind_of(Element)
28
- dom.size.should == 0
29
- end
30
-
31
- it "should accept an HTML string and parse it into a Elements instance" do
32
- el = Document['<div id="foo-bar-baz"></div>']
33
-
34
- el.should be_kind_of(Element)
35
- el.id.should == "foo-bar-baz"
36
- el.size.should == 1
37
- end
38
- end
39
-
40
- describe ".find" do
41
- it "should find all elements matching CSS selector" do
42
- foo = Document.find '.find-foo'
43
- foo.should be_kind_of(Element)
44
- foo.length.should == 2
45
-
46
- bar = Document.find '.find-bar'
47
- bar.should be_kind_of(Element)
48
- bar.length.should == 1
49
- end
50
-
51
- it "should return an empty Element instance with length 0 when no matching" do
52
- baz = Document.find '.find-baz'
53
- baz.should be_kind_of(Element)
54
- baz.length.should == 0
55
- end
56
- end
57
-
58
- describe '.id' do
59
- it "should return a new instance with the element with given id" do
60
- Document.id('foo').should be_kind_of(Element)
61
- Document.id('foo').id.should == 'foo'
62
- end
63
-
64
- it "should return nil if no element could be found" do
65
- Document.id('bad-element-id').should be_nil
66
- end
67
- end
68
-
69
- describe '.parse' do
70
- it "should return a new instance with parsed element as single element" do
71
- foo = Document.parse '<div id="foo" class="bar"></div>'
72
- foo.id.should == 'foo'
73
- foo.class_name.should == 'bar'
74
- end
75
- end
76
-
77
4
  describe '.title and .title=' do
78
5
  it 'sets/gets the page title' do
79
6
  current = Document.title
@@ -9,17 +9,17 @@ describe "Element#after" do
9
9
  HTML
10
10
 
11
11
  it "should insert the given html string after each element" do
12
- el = Document['.after-spec-first']
12
+ el = Element.find('.after-spec-first')
13
13
  el.size.should == 2
14
14
 
15
15
  el.after '<p class="woosh"></p>'
16
16
 
17
- Document.id('foo').next.class_name.should == "woosh"
18
- Document.id('bar').next.class_name.should == "woosh"
17
+ Element.find('#foo').next.class_name.should == "woosh"
18
+ Element.find('#bar').next.class_name.should == "woosh"
19
19
  end
20
20
 
21
21
  it "should insert the given DOM element after this element" do
22
- Document.id('baz').after Document.id('some-header')
23
- Document.id('baz').next.id.should == "some-header"
22
+ Element.find('#baz').after Element.find('#some-header')
23
+ Element.find('#baz').next.id.should == "some-header"
24
24
  end
25
25
  end