opal-jquery 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5fe34adcb3d10d1ae28751e6f725a4b672de271c
4
- data.tar.gz: cffe3b6a9521fde67c56cba78e1e86ab8ae73080
3
+ metadata.gz: 4a9d9c033904e8b586417d0c7236116c3e6ebe62
4
+ data.tar.gz: 05ce1e402fa399dd553d121d6744a27804c0adf4
5
5
  SHA512:
6
- metadata.gz: 18f0077b6c30b76aed392779714ffa6a19a7de0e93d64707e8ac9bf21e1d472519a6877ca5bd497d4e696685971980ee2251a0ffe679a6461c1f5c63603f13f6
7
- data.tar.gz: ce4159eec47d034b697e6994dd7c135e0692680ae0d79c2a6874f1eb94a088442b991c2b05b130e950c756ff6b1b6953882adda00f6206f8bd54d42c519019c8
6
+ metadata.gz: 96ecfc84b0e87f36d24f9f45170878dc522c9d214a18985a3c329b7b032befb1a2d1ddd3b2b2102aecc498fde2230edb523395300f1054b7b29db879e906a336
7
+ data.tar.gz: dccc89db5ec46ccd303b749059d00ce6db1c7911b89a3163f9a750dc3e622c3920c21d69d5f0d4a0f880a67dbafe996c9c9df2b333f34ad21e82ad1e63c67b5c
data/CHANGELOG.md CHANGED
@@ -1,4 +1,12 @@
1
- ## EDGE
1
+ ## 0.4.0 2015-07-17
2
+
3
+ * `Element#[]=` now removes the attribute when the assigned value is nil.
4
+
5
+ * `Element#attr` now better follows jQuery's behaviour by checking the number of arguments passed. Also it's now just a proxy to `Element#[]` and `Element#[]=`
6
+
7
+ * `Element#[]` now returns `nil` only for attributes that are missing, better following jQuery behaviour (which is to return `undefined` on such attributes). `Element#has_attribute?` has been updated accordingly.
8
+
9
+ * Add `Element#prepend` method.
2
10
 
3
11
  ## 0.3.0 2015-02-03
4
12
 
data/README.md CHANGED
@@ -290,6 +290,22 @@ request.errback { |response|
290
290
  }
291
291
  ```
292
292
 
293
+ ## Usage of JQuery plugins
294
+ Extra plugins used for JQuery aren't available to ruby code by default, you will have to `expose` these functions to opal-jquery.
295
+
296
+ ```ruby
297
+ Element.expose :cool_plugin
298
+ ```
299
+
300
+ Arguments to a `exposed` function will be passed as they are, so these arguments will have to be passed as native JS instead of ruby code. A conversion to native JS can be done with the `.to_n` method.
301
+
302
+ ```ruby
303
+ Element.expose :cool_plugin
304
+
305
+ el = Element['.html_element']
306
+ el.cool_plugin({argument: 'value', argument1: 1000}.to_n)
307
+ ```
308
+
293
309
  ## License
294
310
 
295
311
  (The MIT License)
@@ -244,6 +244,11 @@ class Element < `#{JQUERY_CLASS.to_n}`
244
244
  # @param content [String, Element]
245
245
  alias_native :append
246
246
 
247
+ # @!method prepend(content)
248
+ #
249
+ # @param content [String, Element]
250
+ alias_native :prepend
251
+
247
252
  # @!method serialize
248
253
  alias_native :serialize
249
254
 
@@ -296,13 +301,6 @@ class Element < `#{JQUERY_CLASS.to_n}`
296
301
  alias succ next
297
302
  alias << append
298
303
 
299
- # @!method []=(attr, value)
300
- #
301
- # Set the given attribute `attr` on each element in this collection.
302
- #
303
- # @see http://api.jquery.com/attr/
304
- alias_native :[]=, :attr
305
-
306
304
  # @!method add_class(class_name)
307
305
  alias_native :add_class, :addClass
308
306
 
@@ -320,12 +318,21 @@ class Element < `#{JQUERY_CLASS.to_n}`
320
318
  # @param content [String, Element]
321
319
  alias_native :html=, :html
322
320
 
321
+ # @!method index(selector_or_element = nil)
322
+ alias_native :index
323
+
324
+ # @!method is?(selector)
325
+ alias_native :is?, :is
326
+
323
327
  # @!method remove_attr(attr)
324
328
  alias_native :remove_attr, :removeAttr
325
329
 
326
330
  # @!method remove_class(class_name)
327
331
  alias_native :remove_class, :removeClass
328
332
 
333
+ # @!method submit()
334
+ alias_native :submit
335
+
329
336
  # @!method text=(text)
330
337
  #
331
338
  # Set text content of each element in this collection.
@@ -384,19 +391,39 @@ class Element < `#{JQUERY_CLASS.to_n}`
384
391
  end
385
392
 
386
393
  def [](name)
387
- `self.attr(name) || nil`
394
+ %x{
395
+ var value = self.attr(name);
396
+ if(value === undefined) return nil;
397
+ return value;
398
+ }
388
399
  end
389
400
 
390
- def attr(name, value=nil)
391
- if value.nil?
392
- `self.attr(name) || nil`
393
- else
394
- `self.attr(name, value)`
395
- end
401
+ # Set the given attribute `attr` on each element in this collection.
402
+ #
403
+ # @see http://api.jquery.com/attr/
404
+ def []=(name, value)
405
+ `return self.removeAttr(name)` if value.nil?
406
+ `self.attr(name, value)`
407
+ end
408
+
409
+ def attr(*args)
410
+ %x{
411
+ var size = args.length;
412
+ switch (size) {
413
+ case 1:
414
+ return #{self[`args[0]`]};
415
+ break;
416
+ case 2:
417
+ return #{self[`args[0]`] = `args[1]`};
418
+ break;
419
+ default:
420
+ #{raise ArgumentError, '#attr only accepts 1 or 2 arguments'}
421
+ }
422
+ }
396
423
  end
397
424
 
398
425
  def has_attribute?(name)
399
- `!!self.attr(name)`
426
+ `self.attr(name) !== undefined`
400
427
  end
401
428
 
402
429
  def append_to_body
@@ -635,6 +662,29 @@ class Element < `#{JQUERY_CLASS.to_n}`
635
662
  block
636
663
  end
637
664
 
665
+ def one(name, sel = nil, &block)
666
+ %x{
667
+ var wrapper = function(evt) {
668
+ if (evt.preventDefault) {
669
+ evt = #{Event.new `evt`};
670
+ }
671
+
672
+ return block.apply(null, arguments);
673
+ };
674
+
675
+ block._jq_wrap = wrapper;
676
+
677
+ if (sel == nil) {
678
+ self.one(name, wrapper);
679
+ }
680
+ else {
681
+ self.one(name, sel, wrapper);
682
+ }
683
+ }
684
+
685
+ block
686
+ end
687
+
638
688
  def off(name, sel, block = nil)
639
689
  %x{
640
690
  if (sel == null) {
@@ -649,6 +699,11 @@ class Element < `#{JQUERY_CLASS.to_n}`
649
699
  }
650
700
  end
651
701
 
702
+ # returns an Array of Hashes
703
+ def serialize_array
704
+ `self.serializeArray()`.map { |e| Hash.new(e) }
705
+ end
706
+
652
707
  alias size length
653
708
 
654
709
  def value
@@ -1,5 +1,5 @@
1
1
  module Opal
2
2
  module JQuery
3
- VERSION = '0.3.0'
3
+ VERSION = '0.4.0'
4
4
  end
5
5
  end
data/opal-jquery.gemspec CHANGED
@@ -15,7 +15,7 @@ Gem::Specification.new do |s|
15
15
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
16
  s.require_paths = ['lib']
17
17
 
18
- s.add_runtime_dependency 'opal', '~> 0.7.0'
18
+ s.add_runtime_dependency 'opal', '>= 0.7.0', '< 0.9.0'
19
19
  s.add_development_dependency 'opal-rspec', '~> 0.4.0'
20
20
  s.add_development_dependency 'yard'
21
21
  s.add_development_dependency 'rake'
@@ -11,6 +11,8 @@ describe Element do
11
11
  <div id="attr-baz" title=""></div>
12
12
  <div id="attr-woosh"></div>
13
13
  <div id="attr-kapow" title="Apples"></div>
14
+ <div id="attr-empty" attr-empty-value=""></div>
15
+ <div id="attr-missing" attr-auto-value></div>
14
16
 
15
17
  <div id="has-foo" class="apples"></div>
16
18
  <div id="has-bar" class="lemons bananas"></div>
@@ -45,9 +47,15 @@ describe Element do
45
47
  Element.find('#attr-foo')[:title].should == "Hello there!"
46
48
  end
47
49
 
48
- it 'should return nil for an empty attribute' do
49
- expect(Element.find('#attr-bar')[:title]).to be_nil
50
- expect(Element.find('#attr-baz')[:title]).to be_nil
50
+ it 'should return nil for a missing attribute' do
51
+ expect(Element.find('#attr-missing')['attr-missing-value']).to be_nil
52
+ end
53
+
54
+ it 'should return "" for an attribute with empty value' do
55
+ expect(Element.find('#attr-empty')['attr-empty-value']).to eq("")
56
+
57
+ # Not sure if this is browser dependant
58
+ expect(Element.find('#attr-missing')['attr-auto-value']).to eq("")
51
59
  end
52
60
  end
53
61
 
@@ -56,9 +64,15 @@ describe Element do
56
64
  expect(Element.find('#attr-foo').attr(:title)).to eq('Hello there!')
57
65
  end
58
66
 
59
- it 'returns nil for empty attributes' do
60
- expect(Element.find('#attr-bar').attr(:title)).to be_nil
61
- expect(Element.find('#attr-baz').attr(:title)).to be_nil
67
+ it 'should return nil for a missing attribute' do
68
+ expect(Element.find('#attr-missing').attr('attr-missing-value')).to be_nil
69
+ end
70
+
71
+ it 'should return "" for an attribute with empty value' do
72
+ expect(Element.find('#attr-empty').attr('attr-empty-value')).to eq("")
73
+
74
+ # Not sure if this is browser dependant
75
+ expect(Element.find('#attr-missing').attr('attr-auto-value')).to eq("")
62
76
  end
63
77
  end
64
78
 
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Element#prepend' do
4
+ html <<-HTML
5
+ <div id="foo"></div>
6
+ <div id="bar">
7
+ <p>Something</p>
8
+ </div>
9
+ HTML
10
+
11
+ it 'inserts the given html to beginning of element' do
12
+ foo = Element.find '#foo'
13
+ bar = Element.find '#bar'
14
+
15
+ foo.prepend '<span>Bore Da</span>'
16
+ expect(foo.children.first.text).to eq 'Bore Da'
17
+
18
+ bar.prepend '<span>Charlie</span>'
19
+ expect(bar.children.first.text).to eq 'Charlie'
20
+ end
21
+ end
data/spec/element_spec.rb CHANGED
@@ -224,3 +224,38 @@ describe "Element#html" do
224
224
  expect(foo.html).to include('different content')
225
225
  end
226
226
  end
227
+
228
+ describe 'Element.expose' do
229
+ subject(:element) { Element.new }
230
+ before do
231
+ `$.fn.exposableMethod = function() {return 123}`
232
+ `$.fn.exposableMethod2 = function() {return 12}`
233
+ end
234
+
235
+ after do
236
+ `delete $.fn.exposableMethod; delete $.fn.$exposableMethod;`
237
+ `delete $.fn.exposableMethod2; delete $.fn.$exposableMethod2;`
238
+ end
239
+
240
+ it 'exposes methods defined on $.fn' do
241
+ expect(element).not_to respond_to(:exposableMethod)
242
+ Element.expose :exposableMethod
243
+ expect(element).to respond_to(:exposableMethod)
244
+ expect(element.exposableMethod).to eq(123)
245
+ end
246
+
247
+ it 'work if exposing the same method multiple times' do
248
+ Element.expose :exposableMethod
249
+ Element.expose :exposableMethod
250
+ expect(element.exposableMethod).to eq(123)
251
+
252
+ Element.expose :exposableMethod, :exposableMethod
253
+ expect(element.exposableMethod).to eq(123)
254
+ end
255
+
256
+ it 'work if exposing multiple methods' do
257
+ Element.expose :exposableMethod, :exposableMethod2
258
+ expect(element.exposableMethod).to eq(123)
259
+ expect(element.exposableMethod2).to eq(12)
260
+ end
261
+ end
metadata CHANGED
@@ -1,29 +1,35 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opal-jquery
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Beynon
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-03 00:00:00.000000000 Z
11
+ date: 2015-07-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: opal
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: 0.7.0
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: 0.9.0
20
23
  type: :runtime
21
24
  prerelease: false
22
25
  version_requirements: !ruby/object:Gem::Requirement
23
26
  requirements:
24
- - - "~>"
27
+ - - ">="
25
28
  - !ruby/object:Gem::Version
26
29
  version: 0.7.0
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: 0.9.0
27
33
  - !ruby/object:Gem::Dependency
28
34
  name: opal-rspec
29
35
  requirement: !ruby/object:Gem::Requirement
@@ -116,6 +122,7 @@ files:
116
122
  - spec/element/iterable_spec.rb
117
123
  - spec/element/length_spec.rb
118
124
  - spec/element/method_missing_spec.rb
125
+ - spec/element/prepend_spec.rb
119
126
  - spec/element/to_s_spec.rb
120
127
  - spec/element/traversing_spec.rb
121
128
  - spec/element_spec.rb
@@ -149,7 +156,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
149
156
  version: '0'
150
157
  requirements: []
151
158
  rubyforge_project:
152
- rubygems_version: 2.4.5
159
+ rubygems_version: 2.4.6
153
160
  signing_key:
154
161
  specification_version: 4
155
162
  summary: Opal access to jquery
@@ -170,6 +177,7 @@ test_files:
170
177
  - spec/element/iterable_spec.rb
171
178
  - spec/element/length_spec.rb
172
179
  - spec/element/method_missing_spec.rb
180
+ - spec/element/prepend_spec.rb
173
181
  - spec/element/to_s_spec.rb
174
182
  - spec/element/traversing_spec.rb
175
183
  - spec/element_spec.rb