opal-jquery 0.3.0.beta1 → 0.3.0.beta2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,96 +0,0 @@
1
- require 'opal-jquery/constants'
2
-
3
- # Wraps native jQuery event objects.
4
- class Event
5
- `var $ = #{JQUERY_SELECTOR.to_n}` # cache $ for SPEED
6
-
7
- def initialize(native)
8
- @native = native
9
- end
10
-
11
- def [](name)
12
- `#@native[name]`
13
- end
14
-
15
- def type
16
- `#@native.type`
17
- end
18
-
19
- ##
20
- # Element
21
-
22
- def current_target
23
- `$(#@native.currentTarget)`
24
- end
25
-
26
- def target
27
- `$(#@native.target)`
28
- end
29
-
30
- ##
31
- # Propagation
32
-
33
- def prevented?
34
- `#@native.isDefaultPrevented()`
35
- end
36
-
37
- def prevent
38
- `#@native.preventDefault()`
39
- end
40
-
41
- def stopped?
42
- `#@native.isPropagationStopped()`
43
- end
44
-
45
- def stop
46
- `#@native.stopPropagation()`
47
- end
48
-
49
- def stop_immediate
50
- `#@native.stopImmediatePropagation()`
51
- end
52
-
53
- # Stops propagation and prevents default action.
54
- def kill
55
- stop
56
- prevent
57
- end
58
-
59
- # to be removed?
60
- alias default_prevented? prevented?
61
- alias prevent_default prevent
62
- alias propagation_stopped? stopped?
63
- alias stop_propagation stop
64
- alias stop_immediate_propagation stop_immediate
65
-
66
- ##
67
- # Keyboard/Mouse/Touch
68
-
69
- def page_x
70
- `#@native.pageX`
71
- end
72
-
73
- def page_y
74
- `#@native.pageY`
75
- end
76
-
77
- def touch_x
78
- `#@native.originalEvent.touches[0].pageX`
79
- end
80
-
81
- def touch_y
82
- `#@native.originalEvent.touches[0].pageY`
83
- end
84
-
85
- def ctrl_key
86
- `#@native.ctrlKey`
87
- end
88
-
89
- def key_code
90
- `#@native.keyCode`
91
- end
92
-
93
- def which
94
- `#@native.which`
95
- end
96
- end
@@ -1,164 +0,0 @@
1
- require 'json'
2
- require 'native'
3
- require 'promise'
4
- require 'opal-jquery/constants'
5
-
6
- class HTTP
7
- `var $ = #{JQUERY_SELECTOR.to_n}` # cache $ for SPEED
8
-
9
- def self.setup
10
- Hash.new(`$.ajaxSetup()`)
11
- end
12
-
13
- def self.setup= settings
14
- `$.ajaxSetup(#{settings.to_n})`
15
- end
16
-
17
-
18
- attr_reader :body, :error_message, :method, :status_code, :url, :xhr
19
-
20
- def self.get(url, opts={}, &block)
21
- build_request url, :GET, opts, block
22
- end
23
-
24
- def self.post(url, opts={}, &block)
25
- build_request url, :POST, opts, block
26
- end
27
-
28
- def self.put(url, opts={}, &block)
29
- build_request url, :PUT, opts, block
30
- end
31
-
32
- def self.delete(url, opts={}, &block)
33
- build_request url, :DELETE, opts, block
34
- end
35
-
36
- def self.patch(url, opts={}, &block)
37
- build_request url, :PATCH, opts, block
38
- end
39
-
40
- def self.head(url, opts={}, &block)
41
- build_request url, :HEAD, opts, block
42
- end
43
-
44
- def self.build_request(url, method, options, block)
45
- unless block
46
- promise = ::Promise.new
47
- block = proc do |response|
48
- if response.ok?
49
- promise.resolve response
50
- else
51
- promise.reject response
52
- end
53
- end
54
- end
55
-
56
- http = new(url, method, options, block).send!
57
- promise || http
58
- end
59
-
60
- def initialize(url, method, options, handler=nil)
61
- @url = url
62
- @method = method
63
- @ok = true
64
- @xhr = nil
65
- http = self
66
- payload = options.delete :payload
67
- settings = options.to_n
68
-
69
- if handler
70
- @callback = @errback = handler
71
- end
72
-
73
- %x{
74
- if (typeof(payload) === 'string') {
75
- settings.data = payload;
76
- }
77
- else if (payload != nil) {
78
- settings.data = payload.$to_json();
79
- settings.contentType = 'application/json';
80
- }
81
-
82
- settings.url = url;
83
- settings.type = method;
84
-
85
- settings.success = function(data, status, xhr) {
86
- http.body = data;
87
- http.xhr = xhr;
88
- http.status_code = xhr.status;
89
-
90
- if (typeof(data) === 'object') {
91
- http.json = #{ JSON.from_object `data` };
92
- }
93
-
94
- return #{ http.succeed };
95
- };
96
-
97
- settings.error = function(xhr, status, error) {
98
- http.body = xhr.responseText;
99
- http.xhr = xhr;
100
- http.status_code = xhr.status;
101
-
102
- return #{ http.fail };
103
- };
104
- }
105
-
106
- @settings = settings
107
- end
108
-
109
- def fail
110
- @ok = false
111
- @errback.call self if @errback
112
- end
113
-
114
- # Parses the http response body through json. If the response is not
115
- # valid JSON then an error will very likely be thrown.
116
- #
117
- # @example
118
- # # Getting JSON content
119
- # HTTP.get("api.json") do |response|
120
- # puts response.json
121
- # end
122
- #
123
- # # => {"key" => 1, "bar" => 2, ... }
124
- #
125
- # @return [Object] returns the parsed json
126
- def json
127
- @json || JSON.parse(@body)
128
- end
129
-
130
- # Returns true if the request succeeded, false otherwise.
131
- #
132
- # @example
133
- # HTTP.get("/some/url") do |response|
134
- # if response.ok?
135
- # alert "Yay!"
136
- # else
137
- # alert "Aww :("
138
- # end
139
- #
140
- # @return [Boolean] true if request was successful
141
- def ok?
142
- @ok
143
- end
144
-
145
- # Actually send this request
146
- #
147
- # @return [HTTP] returns self
148
- def send!
149
- `$.ajax(#{ @settings })`
150
- self
151
- end
152
-
153
- def succeed
154
- @callback.call self if @callback
155
- end
156
-
157
- # Returns the value of the specified response header.
158
- #
159
- # @param [String] name of the header to get
160
- # @return [String] value of the header
161
- def get_header(key)
162
- `#{xhr}.getResponseHeader(#{key});`
163
- end
164
- end
@@ -1,6 +0,0 @@
1
- module Kernel
2
- def alert(msg)
3
- `alert(msg)`
4
- nil
5
- end
6
- end
@@ -1,31 +0,0 @@
1
- module DOM
2
- class LocalStorage
3
- def initialize(storage)
4
- @storage = storage
5
- end
6
-
7
- def []=(key, value)
8
- %x{
9
- #@storage.setItem(key, value);
10
- return value;
11
- }
12
- end
13
-
14
- def [](key)
15
- %x{
16
- var value = #@storage.getItem(key);
17
- return value == null ? nil : value;
18
- }
19
- end
20
-
21
- def delete(key)
22
- `#@storage.removeItem(key)`
23
- end
24
-
25
- def clear
26
- `#@storage.clear()`
27
- end
28
- end
29
- end
30
-
31
- LocalStorage = DOM::LocalStorage.new(`window.localStorage`)
@@ -1,4 +0,0 @@
1
- require 'opal-jquery/element'
2
-
3
- Window = Element.find `window`
4
- $window = Window