opal-jquery 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ ## 0.0.8 2013-05-02
2
+
3
+ * Depreceate Document in favor of $document global.
4
+
5
+ * Add HTTP.delete() for creating DELETE request.
6
+
1
7
  ## 0.0.7 2013-02-20
2
8
 
3
9
  * Add Element#method_missing which forwards missing calls to try and call
data/Gemfile CHANGED
@@ -2,4 +2,3 @@ source "https://rubygems.org"
2
2
  gemspec
3
3
 
4
4
  gem 'rake'
5
- gem 'opal-spec', '~> 0.2.12'
data/config.ru CHANGED
@@ -1,10 +1,9 @@
1
1
  require 'bundler'
2
2
  Bundler.require
3
3
 
4
- require 'opal/spec/server'
4
+ run Opal::Server.new { |s|
5
+ s.main = 'opal/spec/sprockets_runner'
6
+ s.append_path 'spec'
7
+ s.debug = false
8
+ }
5
9
 
6
- # Run in non-debug mode (faster, all files concatenated into 1 file)
7
- run Opal::Spec::Server.new(false)
8
-
9
- # Run in debug mode - all files loaded seperately, but slower
10
- # run Opal::Spec::Server.new
data/lib/opal-jquery.rb CHANGED
@@ -1 +1,4 @@
1
- require 'opal/jquery'
1
+ require 'opal'
2
+ require 'opal-jquery/version'
3
+
4
+ Opal.append_path File.expand_path('../../opal', __FILE__)
@@ -1,5 +1,5 @@
1
1
  module Opal
2
2
  module JQuery
3
- VERSION = '0.0.7'
3
+ VERSION = '0.0.8'
4
4
  end
5
5
  end
data/opal-jquery.gemspec CHANGED
@@ -1,5 +1,5 @@
1
1
  # -*- encoding: utf-8 -*-
2
- require File.expand_path('../lib/opal/jquery/version', __FILE__)
2
+ require File.expand_path('../lib/opal-jquery/version', __FILE__)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = 'opal-jquery'
@@ -15,5 +15,6 @@ 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.3.39'
18
+ s.add_runtime_dependency 'opal', '>= 0.3.43'
19
+ s.add_development_dependency 'opal-spec', '>= 0.2.15'
19
20
  end
@@ -0,0 +1,5 @@
1
+ require 'opal-jquery/document'
2
+ require 'opal-jquery/element'
3
+ require 'opal-jquery/event'
4
+ require 'opal-jquery/http'
5
+ require 'opal-jquery/kernel'
@@ -0,0 +1,28 @@
1
+ require 'opal-jquery/element'
2
+
3
+ $document = Element.find(`document`)
4
+
5
+ class << $document
6
+ def ready?(&block)
7
+ %x{
8
+ if (block === nil) {
9
+ return nil;
10
+ }
11
+
12
+ $(block);
13
+ return nil;
14
+ }
15
+ end
16
+
17
+ def title
18
+ `document.title`
19
+ end
20
+
21
+ def title=(title)
22
+ `document.title = title`
23
+ end
24
+ end
25
+
26
+ # Document is depreceated, use $document instead.
27
+ Document = $document
28
+
@@ -1,6 +1,8 @@
1
1
  # Instances of Element are just jquery instances, and wrap 1 or more
2
2
  # native dom elements.
3
3
  class Element < `jQuery`
4
+ include Enumerable
5
+
4
6
  def self.find(selector)
5
7
  `$(#{selector})`
6
8
  end
@@ -39,6 +41,10 @@ class Element < `jQuery`
39
41
  expose :after, :before, :parent, :parents, :prepend, :prev, :remove
40
42
  expose :hide, :show, :toggle, :children, :blur, :closest, :data
41
43
  expose :focus, :find, :next, :siblings, :text, :trigger, :append
44
+ expose :height, :width, :serialize, :is, :filter, :last, :first
45
+ expose :wrap, :stop, :clone
46
+
47
+ attr_reader :selector
42
48
 
43
49
  alias_native :[]=, :attr
44
50
  alias_native :add_class, :addClass
@@ -50,6 +56,14 @@ class Element < `jQuery`
50
56
  alias_native :text=, :text
51
57
  alias_native :toggle_class, :toggleClass
52
58
  alias_native :value=, :val
59
+ alias_native :scroll_left=, :scrollLeft
60
+ alias_native :scroll_left, :scrollLeft
61
+ alias_native :remove_attribute, :removeAttr
62
+ alias_native :slide_down, :slideDown
63
+ alias_native :slide_up, :slideUp
64
+ alias_native :slide_toggle, :slideToggle
65
+ alias_native :fade_toggle, :fadeToggle
66
+
53
67
 
54
68
  # Missing methods are assumed to be jquery plugins. These are called by
55
69
  # the given symbol name.
@@ -59,7 +73,7 @@ class Element < `jQuery`
59
73
  return #{self}[#{symbol}].apply(#{self}, args);
60
74
  }
61
75
  }
62
-
76
+
63
77
  super
64
78
  end
65
79
 
@@ -67,8 +81,16 @@ class Element < `jQuery`
67
81
  `#{self}.attr(name) || ""`
68
82
  end
69
83
 
84
+ def add_attribute name
85
+ self[name] = ''
86
+ end
87
+
88
+ def has_attribute? name
89
+ `!!#{self}.attr(name)`
90
+ end
91
+
70
92
  alias << append
71
-
93
+
72
94
  def append_to_body
73
95
  `#{self}.appendTo(document.body)`
74
96
  end
@@ -162,13 +184,13 @@ class Element < `jQuery`
162
184
  # @return [String, DOM] returns css value or the receiver
163
185
  def css(name, value=nil)
164
186
  if value.nil? && name.is_a?(String)
165
- return `$(#{self}).css(name)`
187
+ return `#{self}.css(name)`
166
188
  else
167
- name.is_a?(Hash) ? `$(#{self}).css(#{name.to_native})` : `$(#{self}).css(name, value)`
189
+ name.is_a?(Hash) ? `#{self}.css(#{name.to_native})` : `#{self}.css(name, value)`
168
190
  end
169
191
  self
170
192
  end
171
-
193
+
172
194
  # Set css values over time to create animations. The first parameter is a
173
195
  # set of css properties and values to animate to. The first parameter
174
196
  # also accepts a special :speed value to set animation speed. If a block
@@ -181,16 +203,34 @@ class Element < `jQuery`
181
203
  # bar.animate :top => "30px", :speed => 100 do
182
204
  # bar.add_class "finished"
183
205
  # end
184
- #
206
+ #
185
207
  # @param [Hash] css properties and and values. Also accepts speed param.
186
208
  # @return [DOM] receiver
187
209
  def animate(params, &block)
188
210
  speed = params.has_key?(:speed) ? params.delete(:speed) : 400
189
211
  %x{
190
- $(#{self}).animate(#{params.to_native}, #{speed}, function() {
212
+ #{self}.animate(#{params.to_native}, #{speed}, function() {
191
213
  #{block.call if block_given?}
192
214
  })
193
- }
215
+ }
216
+ end
217
+
218
+ # Start a visual effect (e.g. fadeIn, fadeOut, …) passing its name.
219
+ # Underscored style is automatically converted (e.g. `effect(:fade_in)`).
220
+ # Also accepts additional arguments and a block for the finished callback.
221
+ def effect(name, *args, &block)
222
+ name = name.gsub(/_\w/) { |match| match[1].upcase }
223
+ args = args.map { |a| a.to_native if a.respond_to? :to_native }.compact
224
+ args << `function() { #{block.call if block_given?} }`
225
+ `#{self}[#{name}].apply(#{self}, #{args})`
226
+ end
227
+
228
+ def visible?
229
+ `#{self}.is(':visible')`
230
+ end
231
+
232
+ def offset
233
+ Hash.from_native(`#{self}.offset()`)
194
234
  end
195
235
 
196
236
  # Yields each element in #{self} collection in turn. The yielded element
@@ -208,11 +248,11 @@ class Element < `jQuery`
208
248
  self
209
249
  end
210
250
 
211
- # return an opal array mapped with block yielded for any element
251
+ # return an opal array mapped with block yielded for any element
212
252
  #
213
253
  # @example
214
254
  #
215
- # list = Document.find('table.players td.surname').map {|el| el.html }
255
+ # list = Document.find('table.players td.surname').map {|el| el.html }
216
256
  #
217
257
  # @return an Array
218
258
  def map
@@ -259,6 +299,10 @@ class Element < `jQuery`
259
299
  }
260
300
  end
261
301
 
302
+ def tag_name
303
+ `#{self}.length > 0 ? #{self}[0].tagName.toLowerCase() : #{nil}`
304
+ end
305
+
262
306
  def inspect
263
307
  %x{
264
308
  var val, el, str, result = [];
@@ -281,13 +325,23 @@ class Element < `jQuery`
281
325
  `#{self}.length`
282
326
  end
283
327
 
328
+ def any?
329
+ `#{self}.length > 0`
330
+ end
331
+
332
+ def empty?
333
+ `#{self}.length === 0`
334
+ end
335
+
336
+ alias empty? none?
337
+
284
338
  def on(name, sel = nil, &block)
285
339
  `sel === nil ? #{self}.on(name, block) : #{self}.on(name, sel, block)`
286
340
  block
287
341
  end
288
342
 
289
- def off(name, sel = nil, &block)
290
- `sel === nil ? #{self}.off(name, block) : #{self}.off(name, sel, block)`
343
+ def off(name, sel, block = nil)
344
+ `block === nil ? #{self}.off(name, sel) : #{self}.off(name, sel, block)`
291
345
  end
292
346
 
293
347
  alias size length
@@ -8,7 +8,9 @@ class Event < `$.Event`
8
8
  `$(#{self}.currentTarget)`
9
9
  end
10
10
 
11
- alias_native :default_prevented?, :isDefaultPrevented
11
+ def default_prevented?
12
+ `#{self}.isDefaultPrevented()`
13
+ end
12
14
 
13
15
  # Stops propagation and prevents default action.
14
16
  def kill
@@ -5,7 +5,7 @@
5
5
  # end
6
6
  #
7
7
  class HTTP
8
- attr_reader :body, :error_message, :method, :status_code, :url
8
+ attr_reader :body, :error_message, :method, :status_code, :url, :xhr
9
9
 
10
10
  def self.get(url, opts={}, &block)
11
11
  self.new(url, :GET, opts, block).send!
@@ -19,10 +19,15 @@ class HTTP
19
19
  self.new(url, :PUT, opts, block).send!
20
20
  end
21
21
 
22
+ def self.delete(url, opts={}, &block)
23
+ self.new(url, :DELETE, opts, block).send!
24
+ end
25
+
22
26
  def initialize(url, method, options, handler=nil)
23
27
  @url = url
24
28
  @method = method
25
29
  @ok = true
30
+ @xhr = nil
26
31
  http = self
27
32
  payload = options.delete :payload
28
33
  settings = options.to_native
@@ -43,17 +48,21 @@ class HTTP
43
48
  settings.url = url;
44
49
  settings.type = method;
45
50
 
46
- settings.success = function(str) {
47
- http.body = str;
51
+ settings.success = function(data, status, xhr) {
52
+ http.body = data;
53
+ http.xhr = xhr;
48
54
 
49
- if (typeof(str) === 'object') {
50
- http.json = #{ JSON.from_object `str` };
55
+ if (typeof(data) === 'object') {
56
+ http.json = #{ JSON.from_object `data` };
51
57
  }
52
58
 
53
59
  return #{ http.succeed };
54
60
  };
55
61
 
56
- settings.error = function(xhr, str) {
62
+ settings.error = function(xhr, status, error) {
63
+ http.body = xhr.responseText;
64
+ http.xhr = xhr;
65
+
57
66
  return #{ http.fail };
58
67
  };
59
68
  }
@@ -118,4 +127,12 @@ class HTTP
118
127
  def succeed
119
128
  @callback.call self if @callback
120
129
  end
130
+
131
+ # Returns the value of the specified response header.
132
+ #
133
+ # @param [String] name of the header to get
134
+ # @return [String] value of the header
135
+ def get_header(key)
136
+ `#{xhr}.getResponseHeader(#{key});`
137
+ end
121
138
  end
@@ -1,12 +1,12 @@
1
1
  require "spec_helper"
2
2
 
3
- describe Document do
3
+ describe $document do
4
4
  describe '.title and .title=' do
5
5
  it 'sets/gets the page title' do
6
- current = Document.title
7
- Document.title = 'hello from opal'
8
- Document.title.should == 'hello from opal'
9
- Document.title = current
6
+ current = $document.title
7
+ $document.title = 'hello from opal'
8
+ $document.title.should == 'hello from opal'
9
+ $document.title = current
10
10
  end
11
11
  end
12
12
  end
data/spec/http_spec.rb CHANGED
@@ -3,7 +3,7 @@ require "spec_helper"
3
3
  describe HTTP do
4
4
  describe '#body' do
5
5
  async 'returns the response body as a string' do
6
- HTTP.get('data/simple.txt') do |response|
6
+ HTTP.get('spec/data/simple.txt') do |response|
7
7
  run_async { response.body.should == "hey" }
8
8
  end
9
9
  end
@@ -11,7 +11,7 @@ describe HTTP do
11
11
 
12
12
  describe '#callback' do
13
13
  async 'can add a success callback after the request is sent' do
14
- http = HTTP.get('data/simple.txt')
14
+ http = HTTP.get('spec/data/simple.txt')
15
15
  http.callback do |response|
16
16
  run_async { response.ok?.should be_true }
17
17
  end
@@ -20,7 +20,7 @@ describe HTTP do
20
20
 
21
21
  describe '#callback' do
22
22
  async 'can add a failure callback after the request is sent' do
23
- http = HTTP.get('data/bad_url.txt')
23
+ http = HTTP.get('spec/data/bad_url.txt')
24
24
  http.errback do |response|
25
25
  run_async { response.ok?.should be_false }
26
26
  end
@@ -29,7 +29,7 @@ describe HTTP do
29
29
 
30
30
  describe '#json' do
31
31
  async 'returns the json converted into native ruby objects' do
32
- HTTP.get('data/user.json') do |response|
32
+ HTTP.get('spec/data/user.json') do |response|
33
33
  run_async { response.json.should == { 'name' => 'Adam', 'age' => 26 } }
34
34
  end
35
35
  end
@@ -37,13 +37,13 @@ describe HTTP do
37
37
 
38
38
  describe '#ok?' do
39
39
  async 'returns true when the request was a sucess' do
40
- HTTP.get('data/simple.txt') do |response|
40
+ HTTP.get('spec/data/simple.txt') do |response|
41
41
  run_async { response.ok?.should be_true }
42
42
  end
43
43
  end
44
44
 
45
45
  async 'returns false when the request failed' do
46
- HTTP.get('data/non_existant.txt') do |response|
46
+ HTTP.get('spec/data/non_existant.txt') do |response|
47
47
  run_async { response.ok?.should be_false }
48
48
  end
49
49
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opal-jquery
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,24 +9,40 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-20 00:00:00.000000000 Z
12
+ date: 2013-05-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: opal
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
- - - ~>
19
+ - - ! '>='
20
20
  - !ruby/object:Gem::Version
21
- version: 0.3.39
21
+ version: 0.3.43
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
25
25
  none: false
26
26
  requirements:
27
- - - ~>
27
+ - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
- version: 0.3.39
29
+ version: 0.3.43
30
+ - !ruby/object:Gem::Dependency
31
+ name: opal-spec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 0.2.15
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 0.2.15
30
46
  description: Opal DOM library for jquery
31
47
  email: adam.beynon@gmail.com
32
48
  executables: []
@@ -41,18 +57,15 @@ files:
41
57
  - README.md
42
58
  - Rakefile
43
59
  - config.ru
44
- - lib/assets/javascripts/opal-jquery.rb
45
- - lib/assets/javascripts/opal/jquery.rb
46
- - lib/assets/javascripts/opal/jquery/document.rb
47
- - lib/assets/javascripts/opal/jquery/element.rb
48
- - lib/assets/javascripts/opal/jquery/event.rb
49
- - lib/assets/javascripts/opal/jquery/http.rb
50
- - lib/assets/javascripts/opal/jquery/kernel.rb
51
- - lib/assets/javascripts/opal/jquery/local_storage.rb
52
60
  - lib/opal-jquery.rb
53
- - lib/opal/jquery.rb
54
- - lib/opal/jquery/version.rb
61
+ - lib/opal-jquery/version.rb
55
62
  - opal-jquery.gemspec
63
+ - opal/opal-jquery.rb
64
+ - opal/opal-jquery/document.rb
65
+ - opal/opal-jquery/element.rb
66
+ - opal/opal-jquery/event.rb
67
+ - opal/opal-jquery/http.rb
68
+ - opal/opal-jquery/kernel.rb
56
69
  - spec/data/simple.txt
57
70
  - spec/data/user.json
58
71
  - spec/document_spec.rb
@@ -75,7 +88,6 @@ files:
75
88
  - spec/event_spec.rb
76
89
  - spec/http_spec.rb
77
90
  - spec/jquery.js
78
- - spec/local_storage_spec.rb
79
91
  - spec/spec_helper.rb
80
92
  homepage: http://opalrb.org
81
93
  licenses: []
@@ -97,7 +109,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
97
109
  version: '0'
98
110
  requirements: []
99
111
  rubyforge_project:
100
- rubygems_version: 1.8.23
112
+ rubygems_version: 1.8.24
101
113
  signing_key:
102
114
  specification_version: 3
103
115
  summary: Opal access to jquery
@@ -124,5 +136,4 @@ test_files:
124
136
  - spec/event_spec.rb
125
137
  - spec/http_spec.rb
126
138
  - spec/jquery.js
127
- - spec/local_storage_spec.rb
128
139
  - spec/spec_helper.rb
@@ -1 +0,0 @@
1
- require 'opal/jquery'
@@ -1,6 +0,0 @@
1
- require 'opal/jquery/document'
2
- require 'opal/jquery/element'
3
- require 'opal/jquery/event'
4
- require 'opal/jquery/http'
5
- require 'opal/jquery/kernel'
6
- require 'opal/jquery/local_storage'
@@ -1,20 +0,0 @@
1
- module Document
2
- def self.ready?(&block)
3
- %x{
4
- if (block === nil) {
5
- return nil;
6
- }
7
-
8
- $(block);
9
- return nil;
10
- }
11
- end
12
-
13
- def self.title
14
- `document.title`
15
- end
16
-
17
- def self.title=(title)
18
- `document.title = title`
19
- end
20
- end
@@ -1,25 +0,0 @@
1
- module LocalStorage
2
- def self.[](key)
3
- %x{
4
- var val = localStorage.getItem(key);
5
- return val === null ? nil : val;
6
- }
7
- end
8
-
9
- def self.[]=(key, value)
10
- `localStorage.setItem(key, value)`
11
- end
12
-
13
- def self.clear
14
- `localStorage.clear()`
15
- self
16
- end
17
-
18
- def self.delete(key)
19
- %x{
20
- var val = localStorage.getItem(key);
21
- localStorage.removeItem(key);
22
- return val === null ? nil : val;
23
- }
24
- end
25
- end
data/lib/opal/jquery.rb DELETED
@@ -1,5 +0,0 @@
1
- require 'opal'
2
- require 'opal/jquery/version'
3
-
4
- # Just register our opal code path with opal build tools
5
- Opal.append_path File.join(File.dirname(__FILE__), '..', 'assets', 'javascripts')
@@ -1,53 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe LocalStorage do
4
- before do
5
- LocalStorage.clear
6
- end
7
-
8
- describe '.[]' do
9
- it 'returns nil when accessing an undefined value' do
10
- LocalStorage['woosh'].should be_nil
11
- end
12
-
13
- it 'returns an empty string when set value was also ""' do
14
- LocalStorage['empty'] = ''
15
- LocalStorage['empty'].should == ''
16
- end
17
- end
18
-
19
- describe '.[]=' do
20
- it 'sets values in the localstorage' do
21
- LocalStorage['foo'] = 'Hello World'
22
- LocalStorage['foo'].should == 'Hello World'
23
- end
24
-
25
- it 'stores all values as strings' do
26
- LocalStorage['foo'] = 3.142
27
- LocalStorage['foo'].should be_kind_of(String)
28
- end
29
- end
30
-
31
- describe '.clear' do
32
- it 'removes all values from the store' do
33
- LocalStorage['foo'] = 'wow'
34
- LocalStorage['bar'] = 'pow'
35
- LocalStorage.clear
36
- LocalStorage['foo'].should be_nil
37
- LocalStorage['bar'].should be_nil
38
- end
39
- end
40
-
41
- describe '.delete' do
42
- it 'deletes the given key from localstorage' do
43
- LocalStorage['deletable'] = 'Hey there'
44
- LocalStorage.delete 'deletable'
45
- LocalStorage['deletable'].should be_nil
46
- end
47
-
48
- it 'returns the deleted value' do
49
- LocalStorage['deletable'] = 'Hey there'
50
- LocalStorage.delete('deletable').should == 'Hey there'
51
- end
52
- end
53
- end