Skip to content

Commit

Permalink
Add some useful class methods for aliasing methods
Browse files Browse the repository at this point in the history
  • Loading branch information
adambeynon committed Oct 19, 2013
1 parent 89ef05f commit 9e02efd
Showing 1 changed file with 29 additions and 118 deletions.
147 changes: 29 additions & 118 deletions opal/opal-jquery/element.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ def find(selector)
self.new `$(#{selector})`
end

def [](selector)
self.new `$(#{selector})`
end
alias [] find
alias parse find

def id(id)
%x{
Expand All @@ -22,8 +21,24 @@ def id(id)
}
end

def parse(str)
self.new `$(str)`
def define_manipulation(name)
define_method(name) do |content|
%x{
if (!content._isString) {
content = content['native'];
}
#@native[name](content);
}

self
end
end

def define_traversing(name)
define_method(name) do
Element.new `#@native[name]()`
end
end
end

Expand All @@ -35,75 +50,22 @@ def initialize(str)
end
end

def after(content)
%x{
if (!content._isString) {
content = content['native'];
}
#@native.after(content);
}

self
end

def before(content)
%x{
if (!content._isString) {
content = content['native'];
}
#@native.before(content);
}
define_manipulation :after
define_manipulation :before
define_manipulation :append
define_manipulation :prepend

self
end

def append(content)
%x{
if (!content._isString) {
content = content['native'];
}
#@native.append(content);
}

self
end

def prepend(content)
%x{
if (!content._isString) {
content = content['native'];
}
#@native.prepend(content);
}

self
end
define_traversing :next
define_traversing :prev
define_traversing :parent
define_traversing :parents
define_traversing :children

def remove
`#@native.remove()`
self
end

def next
Element.new `#@native.next()`
end

def prev
Element.new `#@native.prev()`
end

def parent
Element.new `#@native.parent()`
end

def parents
Element.new `#@native.parents()`
end

def hide
`#@native.hide()`
end
Expand All @@ -116,10 +78,6 @@ def toggle
`#@native.toggle()`
end

def children
Element.new `#@native.children()`
end

def siblings(sel=nil)
if sel
Element.new `#@native.siblings(sel)`
Expand Down Expand Up @@ -251,15 +209,6 @@ def append_to_head
# Returns the element at the given index as a new `Element` instance.
# Negative indexes can be used and are counted from the end. If the
# given index is outside the range then `nil` is returned.
#
# @example
#
# Element.find('.foo')[0] # => first element in collection
# Element.find('.foo')[-1] # => last element from collection
# Element.find('.foo')[100] # => returns nil if index outside range
#
# @param [Numeric] index the index to get
# @return [Element, nil] returns new collection with returned element
def at(index)
%x{
var length = #@native.length;
Expand All @@ -279,13 +228,6 @@ def at(index)
# Returns the CSS class name of the firt element in #{self} collection.
# If the collection is empty then an empty string is returned. Only
# the class name of the first element will ever be returned.
#
# @example
#
# Element.find('<p class="foo"></p>').class_name
# # => "foo"
#
# @return [String] the class name
def class_name
%x{
var first = #@native[0];
Expand All @@ -296,13 +238,6 @@ def class_name
# Sets the CSS class name of every element in #{self} collection to the
# given string. #{self} does not append the class names, it replaces
# the entire current class name.
#
# @example
#
# Element.find('#foo').class_name = "title"
#
# @param [String] name the class name to set on each element
# @return [Element] returns the receiver
def class_name=(name)
%x{
for (var i = 0, length = #@native.length; i < length; i++) {
Expand All @@ -318,19 +253,6 @@ def class_name=(name)
# property is also given then the given css property is set to the
# given value for each of the elements in #{self} collection. The
# property can also be a hash of properties and values.
#
# @example
#
# foo = Element.find '#foo'
# foo.css 'background-color' # => "red"
# foo.css 'background-color', 'green'
# foo.css 'background-color' # => "green"
# foo.css :width => '200px'
#
# @param [String] name the css property to get/set
# @param [String] value optional value to set
# @param [Hash] set of css properties and values
# @return [String, Element] returns css value or the receiver
def css(name, value=nil)
if value.nil? && name.is_a?(String)
return `#@native.css(name)`
Expand All @@ -344,17 +266,6 @@ def css(name, value=nil)
# 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.
#
# @example
#
# foo = Element.find "#foo"
# foo.animate :height => "200px", "margin-left" => "10px"
# bar.animate :top => "30px", :speed => 100 do
# bar.add_class "finished"
# end
#
# @param [Hash] css properties and and values. Also accepts speed param.
# @return [Element] receiver
def animate(params, &block)
speed = params.delete(:speed) || 400
%x{
Expand Down

0 comments on commit 9e02efd

Please sign in to comment.