lib/opal/jquery/element.rb in opal-jquery-0.4.2 vs lib/opal/jquery/element.rb in opal-jquery-0.4.3

- old
+ new

@@ -296,14 +296,16 @@ alias_native :empty # @!method get alias_native :get - # @!method prop(name, value = nil) + # @!method prop(name, value = undefined) # # Get or set the property `name` on each element in collection. - alias_native :prop + def prop(*args) + Native.call(self, :prop, *args) + end alias succ next alias << append # @!method add_class(class_name) @@ -336,10 +338,13 @@ alias_native :remove_class, :removeClass # @!method submit() alias_native :submit + # @!method click() + alias_native :click + # @!method text=(text) # # Set text content of each element in this collection. # # @see #text @@ -414,14 +419,15 @@ def attr(*args) %x{ var size = args.length; switch (size) { case 1: - return #{self[`args[0]`]}; + var result = self.attr(args[0]); + return( (result == null) ? nil : result ); break; case 2: - return #{self[`args[0]`] = `args[1]`}; + return self.attr(args[0], args[1]); break; default: #{raise ArgumentError, '#attr only accepts 1 or 2 arguments'} } } @@ -506,20 +512,25 @@ # set of css properties and values to animate to. The first parameter # also accepts a special :speed value to set animation speed. If a block # is given, the block is run as a callback when the animation finishes. def animate(params, &block) speed = params.has_key?(:speed) ? params.delete(:speed) : 400 - %x{ - self.animate(#{params.to_n}, #{speed}, function() { - #{block.call if block_given?} - }) - } + if block_given? + `self.animate(#{params.to_n}, #{speed}, block)` + else + `self.animate(#{params.to_n}, #{speed})` + end end def data(*args) %x{ var result = self.data.apply(self, args); + if ( + (typeof(result) === 'object') && !(result instanceof #{JQUERY_CLASS}) + ) { + result = #{ JSON.from_object `result` }; + } return result == null ? nil : result; } end # Start a visual effect (e.g. fadeIn, fadeOut, …) passing its name. @@ -644,19 +655,26 @@ alias empty? none? def on(name, sel = nil, &block) %x{ - var wrapper = function(evt) { - if (evt.preventDefault) { - evt = #{Event.new `evt`}; + var has_args = #{block.arity} !== 0; + + var wrapper = function() { + for(var args = new Array(arguments.length), i = 0, ii = args.length; i < ii; i++) { + args[i] = arguments[i]; } - return block.apply(null, arguments); + // Use preventDefault as a canary for native events + if (has_args && args[0].preventDefault) { + args[0] = #{Event.new `args[0]`}; + } + + return block.apply(null, args); }; - block._jq_wrap = wrapper; + block.$$jqwrap = wrapper; if (sel == nil) { self.on(name, wrapper); } else { @@ -667,19 +685,26 @@ block end def one(name, sel = nil, &block) %x{ - var wrapper = function(evt) { - if (evt.preventDefault) { - evt = #{Event.new `evt`}; + var has_args = #{block.arity} !== 0; + + var wrapper = function() { + for(var args = new Array(arguments.length), i = 0, ii = args.length; i < ii; i++) { + args[i] = arguments[i]; } - return block.apply(null, arguments); + // Use preventDefault as a canary for native events + if (has_args && args[0].preventDefault) { + args[0] = #{Event.new `args[0]`}; + } + + return block.apply(null, args); }; - block._jq_wrap = wrapper; + block.$$jqwrap = wrapper; if (sel == nil) { self.one(name, wrapper); } else { @@ -694,14 +719,14 @@ %x{ if (sel == null) { return self.off(name); } else if (block === nil) { - return self.off(name, sel._jq_wrap); + return self.off(name, sel.$$jqwrap); } else { - return self.off(name, sel, block._jq_wrap); + return self.off(name, sel, block.$$jqwrap); } } end # Serializes a form into an Array of Hash objects. @@ -712,20 +737,48 @@ end alias size length def value - `self.val() || ""` + `self.val()` || "" end def height - `self.height() || nil` + `self.height()` || nil end def width - `self.width() || nil` + `self.width()` || nil end def position Native(`self.position()`) + end + + def ==(other) + `self.is(other)` + end + + def respond_to_missing?(name, _) + %x{ + var method = self[#{name}]; + if (typeof(method) === 'function') { + return true; + } else { + return #{super}; + } + } + end + + def method_missing(name, *args, &block) + args << block if block_given? + + %x{ + var method = self[#{name}]; + if (typeof(method) === 'function') { + return method.apply(self, #{args.to_n}); + } else { + return #{super}; + } + } end end