opal-jquery 0.0.1

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.
@@ -0,0 +1,380 @@
1
+ # Instances of Element are just jquery instances, and wrap 1 or more
2
+ # native dom elements.
3
+ class Element < `jQuery`
4
+ def self.find(selector)
5
+ `$(selector)`
6
+ end
7
+
8
+ def self.id(id)
9
+ Document.id(id)
10
+ end
11
+
12
+ def self.new(tag = 'div')
13
+ `$(document.createElement(tag))`
14
+ end
15
+
16
+ def self.parse(str)
17
+ `$(str)`
18
+ end
19
+
20
+ def [](name)
21
+ `#{self}.attr(name) || ""`
22
+ end
23
+
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
41
+
42
+ alias_native :add_class, :addClass
43
+
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 <<
60
+
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
72
+
73
+ def append_to_body
74
+ `#{self}.appendTo(document.body)`
75
+ end
76
+
77
+ def append_to_head
78
+ `#{self}.appendTo(document.head)`
79
+ end
80
+
81
+ # Returns the element at the given index as a new `DOM` instance.
82
+ # Negative indexes can be used and are counted from the end. If the
83
+ # given index is outside the range then `nil` is returned.
84
+ #
85
+ # @example
86
+ #
87
+ # DOM('.foo')[0] # => first element in collection
88
+ # DOM('.foo')[-1] # => last element from collection
89
+ # DOM('.foo')[100] # => returns nil if index outside range
90
+ #
91
+ # @param [Numeric] index the index to get
92
+ # @return [DOM, nil] returns new collection with returned element
93
+ def at(index)
94
+ %x{
95
+ var length = #{self}.length;
96
+
97
+ if (index < 0) {
98
+ index += length;
99
+ }
100
+
101
+ if (index < 0 || index >= length) {
102
+ return nil;
103
+ }
104
+
105
+ return $(#{self}[index]);
106
+ }
107
+ end
108
+
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
+ # Returns the CSS class name of the firt element in #{self} collection.
137
+ # If the collection is empty then an empty string is returned. Only
138
+ # the class name of the first element will ever be returned.
139
+ #
140
+ # @example
141
+ #
142
+ # DOM('<p class="foo"></p>').class_name
143
+ # # => "foo"
144
+ #
145
+ # @return [String] the class name
146
+ def class_name
147
+ %x{
148
+ var first = #{self}[0];
149
+
150
+ if (!first) {
151
+ return "";
152
+ }
153
+
154
+ return first.className || "";
155
+ }
156
+ end
157
+
158
+ # Sets the CSS class name of every element in #{self} collection to the
159
+ # given string. #{self} does not append the class names, it replaces
160
+ # the entire current class name.
161
+ #
162
+ # @example
163
+ #
164
+ # DOM('#foo').class_name = "title"
165
+ #
166
+ # @param [String] name the class name to set on each element
167
+ # @return [DOM] returns the receiver
168
+ def class_name=(name)
169
+ %x{
170
+ for (var i = 0, length = #{self}.length; i < length; i++) {
171
+ #{self}[i].className = name;
172
+ }
173
+ }
174
+ self
175
+ end
176
+
177
+ # Get or set css properties on each element in #{self} collection. If
178
+ # only the `name` is given, then that css property name is read from
179
+ # the first element in the collection and returned. If the `value`
180
+ # property is also given then the given css property is set to the
181
+ # given value for each of the elements in #{self} collection. The
182
+ # property can also be a hash of properties and values.
183
+ #
184
+ # @example
185
+ #
186
+ # foo = DOM '#foo'
187
+ # foo.css 'background-color' # => "red"
188
+ # foo.css 'background-color', 'green'
189
+ # foo.css 'background-color' # => "green"
190
+ # foo.css :width => '200px'
191
+ #
192
+ # @param [String] name the css property to get/set
193
+ # @param [String] value optional value to set
194
+ # @param [Hash] set of css properties and values
195
+ # @return [String, DOM] returns css value or the receiver
196
+ def css(name, value=nil)
197
+ if value.nil? && name.is_a?(String)
198
+ return `$(#{self}).css(name)`
199
+ else
200
+ name.is_a?(Hash) ? `$(#{self}).css(#{name.to_native})` : `$(#{self}).css(name, value)`
201
+ end
202
+ self
203
+ end
204
+
205
+ # Set css values over time to create animations. The first parameter is a
206
+ # set of css properties and values to animate to. The first parameter
207
+ # also accepts a special :speed value to set animation speed. If a block
208
+ # is given, the block is run as a callback when the animation finishes.
209
+ #
210
+ # @example
211
+ #
212
+ # foo = DOM "#foo"
213
+ # foo.animate :height => "200px", "margin-left" => "10px"
214
+ # bar.animate :top => "30px", :speed => 100 do
215
+ # bar.add_class "finished"
216
+ # end
217
+ #
218
+ # @param [Hash] css properties and and values. Also accepts speed param.
219
+ # @return [DOM] receiver
220
+ def animate(params, &block)
221
+ speed = params.has_key?(:speed) ? params.delete(:speed) : 400
222
+ %x{
223
+ $(#{self}).animate(#{params.to_native}, #{speed}, function() {
224
+ #{block.call if block_given?}
225
+ })
226
+ }
227
+ end
228
+
229
+ # Yields each element in #{self} collection in turn. The yielded element
230
+ # is wrapped as a `DOM` instance.
231
+ #
232
+ # @example
233
+ #
234
+ # DOM('.foo').each { |e| puts "The element id: #{e.id}" }
235
+ #
236
+ # @return returns the receiver
237
+ def each
238
+ `for (var i = 0, length = #{self}.length; i < length; i++) {`
239
+ yield `$(#{self}[i])`
240
+ `}`
241
+ self
242
+ end
243
+
244
+ # Find all the elements that match the given `selector` within the
245
+ # scope of elements in #{self} given collection. Might return an empty
246
+ # collection if no elements match.
247
+ #
248
+ # @example
249
+ #
250
+ # form = DOM('#login-form')
251
+ # form.find 'input, select'
252
+ #
253
+ # @param [String] selector the selector to match elements against
254
+ # @return [DOM] returns new collection
255
+ alias_native :find, :find
256
+
257
+ def first
258
+ `#{self}.length ? #{self}.first() : nil`
259
+ end
260
+
261
+ alias_native :focus, :focus
262
+
263
+ alias_native :has_class?, :hasClass
264
+
265
+ def html
266
+ `#{self}.html() || ""`
267
+ end
268
+
269
+ alias_native :html=, :html
270
+
271
+ def id
272
+ %x{
273
+ var first = #{self}[0];
274
+
275
+ if (!first) {
276
+ return "";
277
+ }
278
+
279
+ return first.id || "";
280
+ }
281
+ end
282
+
283
+ def id=(id)
284
+ %x{
285
+ var first = #{self}[0];
286
+
287
+ if (first) {
288
+ first.id = id;
289
+ }
290
+
291
+ return #{self};
292
+ }
293
+ end
294
+
295
+ def inspect
296
+ %x{
297
+ var val, el, str, result = [];
298
+
299
+ for (var i = 0, length = #{self}.length; i < length; i++) {
300
+ el = #{self}[i];
301
+ str = "<" + el.tagName.toLowerCase();
302
+
303
+ if (val = el.id) str += (' id="' + val + '"');
304
+ if (val = el.className) str += (' class="' + val + '"');
305
+
306
+ result.push(str + '>');
307
+ }
308
+
309
+ return '[' + result.join(', ') + ']';
310
+ }
311
+ end
312
+
313
+ def length
314
+ `#{self}.length`
315
+ end
316
+
317
+ alias_native :next, :next
318
+
319
+ def off(event_name, selector, handler=nil)
320
+ %x{
321
+ if (handler === nil) {
322
+ handler = selector;
323
+ #{self}.off(event_name, handler._jq);
324
+ }
325
+ else {
326
+ #{self}.off(event_name, selector, handler._jq);
327
+ }
328
+ }
329
+
330
+ handler
331
+ end
332
+
333
+ def on(event_name, selector=nil, &block)
334
+ return unless block_given?
335
+
336
+ %x{
337
+ var handler = function(e) { return #{ block.call `e` } };
338
+ block._jq = handler;
339
+
340
+ if (selector === nil) {
341
+ #{self}.on(event_name, handler);
342
+ }
343
+ else {
344
+ #{self}.on(event_name, selector, handler);
345
+ }
346
+ }
347
+
348
+ block
349
+ end
350
+
351
+ alias_native :parent, :parent
352
+
353
+ alias_native :prev, :prev
354
+
355
+ alias_native :remove, :remove
356
+
357
+ alias_native :remove_class, :removeClass
358
+
359
+ alias size length
360
+
361
+ alias succ next
362
+
363
+ alias_native :text=, :text
364
+
365
+ alias_native :toggle_class, :toggleClass
366
+
367
+ alias_native :trigger, :trigger
368
+
369
+ def value
370
+ `#{self}.val() || ""`
371
+ end
372
+
373
+ alias_native :value=, :val
374
+
375
+ # display functions
376
+ alias_native :hide, :hide
377
+ alias_native :show, :show
378
+ alias_native :toggle, :toggle
379
+
380
+ end
@@ -0,0 +1,39 @@
1
+ class Event < `$.Event`
2
+ def current_target
3
+ `$(#{self}.currentTarget)`
4
+ end
5
+
6
+ alias_native :default_prevented?, :isDefaultPrevented
7
+
8
+ alias_native :prevent_default, :preventDefault
9
+
10
+ def page_x
11
+ `#{self}.pageX`
12
+ end
13
+
14
+ def page_y
15
+ `#{self}.pageY`
16
+ end
17
+
18
+ alias_native :propagation_stopped?, :propagationStopped
19
+
20
+ alias_native :stop_propagation, :stopPropagation
21
+
22
+ def target
23
+ %x{
24
+ if (#{self}._opalTarget) {
25
+ return #{self}._opalTarget;
26
+ }
27
+
28
+ return #{self}._opalTarget = $(#{self}.target);
29
+ }
30
+ end
31
+
32
+ def type
33
+ `#{self}.type`
34
+ end
35
+
36
+ def which
37
+ `#{self}.which`
38
+ end
39
+ end
@@ -0,0 +1,113 @@
1
+ # Wraps jQuery's ajax request into a ruby class.
2
+ #
3
+ # HTTP.get("/users/1.json") do |response|
4
+ # puts "Got response!"
5
+ # end
6
+ #
7
+ class HTTP
8
+ attr_reader :body
9
+ attr_reader :error_message
10
+ attr_reader :method
11
+ attr_reader :status_code
12
+ attr_reader :url
13
+
14
+ def self.get(url, opts={}, &block)
15
+ self.new(url, :GET, opts, block).send!
16
+ end
17
+
18
+ def self.post(url, opts={}, &block)
19
+ self.new(url, :POST, opts, block).send!
20
+ end
21
+
22
+ def initialize(url, method, options, handler=nil)
23
+ @url = url
24
+ @method = method
25
+ @ok = true
26
+ http = self
27
+ settings = options.to_native
28
+
29
+ if handler
30
+ @callback = @errback = handler
31
+ end
32
+
33
+ %x{
34
+ settings.data = settings.payload;
35
+ settings.url = url;
36
+ settings.type = method;
37
+
38
+ settings.success = function(str) {
39
+ http.body = str;
40
+
41
+ if (typeof(str) === 'object') {
42
+ http.json = #{ JSON.from_object `str` };
43
+ }
44
+
45
+ return #{ http.succeed };
46
+ };
47
+
48
+ settings.error = function(xhr, str) {
49
+ return #{ http.fail };
50
+ };
51
+ }
52
+
53
+ @settings = settings
54
+ end
55
+
56
+ def callback(&block)
57
+ @callback = block
58
+ self
59
+ end
60
+
61
+ def errback(&block)
62
+ @errback = block
63
+ self
64
+ end
65
+
66
+ def fail
67
+ @ok = false
68
+ @errback.call self if @errback
69
+ end
70
+
71
+ # Parses the http response body through json. If the response is not
72
+ # valid JSON then an error will very likely be thrown.
73
+ #
74
+ # @example
75
+ # # Getting JSON content
76
+ # HTTP.get("api.json") do |response|
77
+ # puts response.json
78
+ # end
79
+ #
80
+ # # => {"key" => 1, "bar" => 2, ... }
81
+ #
82
+ # @return [Object] returns the parsed json
83
+ def json
84
+ @json || JSON.parse(@body)
85
+ end
86
+
87
+ # Returns true if the request succeeded, false otherwise.
88
+ #
89
+ # @example
90
+ # HTTP.get("/some/url") do |response|
91
+ # if response.ok?
92
+ # alert "Yay!"
93
+ # else
94
+ # alert "Aww :("
95
+ # end
96
+ #
97
+ # @return [Boolean] true if request was successful
98
+ def ok?
99
+ @ok
100
+ end
101
+
102
+ # Actually send this request
103
+ #
104
+ # @return [HTTP] returns self
105
+ def send!
106
+ `$.ajax(#{ @settings })`
107
+ self
108
+ end
109
+
110
+ def succeed
111
+ @callback.call self if @callback
112
+ end
113
+ end