Skip to content

Commit

Permalink
Cleanup some more element code
Browse files Browse the repository at this point in the history
  • Loading branch information
adambeynon committed Oct 18, 2013
1 parent 03dbe5d commit 89ef05f
Show file tree
Hide file tree
Showing 2 changed files with 174 additions and 96 deletions.
238 changes: 174 additions & 64 deletions opal/opal-jquery/element.rb
@@ -1,35 +1,37 @@
class Element
include Enumerable

def self.find(selector)
self.new `$(#{selector})`
end
class << self
def find(selector)
self.new `$(#{selector})`
end

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

def self.id(id)
%x{
var el = document.getElementById(id);
def id(id)
%x{
var el = document.getElementById(id);
if (!el) {
return nil;
}
if (!el) {
return nil;
}
return #{new `el`};
}
end
return #{new `el`};
}
end

def self.parse(str)
self.new `$(str)`
def parse(str)
self.new `$(str)`
end
end

def initialize(el)
if String === el
@native = `$(document.createElement(#{el}))`
def initialize(str)
if String === str
@native = `$(document.createElement(#{str}))`
else
@native = `$(#{el})`
@native = `$(#{str})`
end
end

Expand Down Expand Up @@ -69,6 +71,18 @@ def append(content)
self
end

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

self
end

def remove
`#@native.remove()`
self
Expand All @@ -86,6 +100,10 @@ def parent
Element.new `#@native.parent()`
end

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

def hide
`#@native.hide()`
end
Expand Down Expand Up @@ -122,39 +140,49 @@ def append_to(target)
self
end

attr_reader :selector

# Bridged functions - we just expose all core jquery functions as ruby
# methods on this class.
# expose :after, :before, :parent, :parents, :prepend, :prev, :remove
# expose :hide, :show, :toggle, :children, :blur, :closest, :data
# expose :focus, :find, :next, :siblings, :text, :trigger, :append
# expose :height, :width, :serialize, :is, :filter, :last, :first
# expose :wrap, :stop, :clone, :empty
def selector
`#@native.selector`
end

# We alias some jquery methods to common ruby method names.
alias succ next
alias << append

# Here we map the remaining jquery methods, but change their names to
# snake_case to be more consistent with ruby.
# alias_native :[]=, :attr
# alias_native :add_class, :addClass
# alias_native :append_to, :appendTo
# alias_native :has_class?, :hasClass
# alias_native :html=, :html
# alias_native :remove_attr, :removeAttr
# alias_native :remove_class, :removeClass
# alias_native :text=, :text
# alias_native :toggle_class, :toggleClass
# alias_native :value=, :val
# alias_native :scroll_left=, :scrollLeft
# alias_native :scroll_left, :scrollLeft
# alias_native :remove_attribute, :removeAttr
# alias_native :slide_down, :slideDown
# alias_native :slide_up, :slideUp
# alias_native :slide_toggle, :slideToggle
# alias_native :fade_toggle, :fadeToggle
def focus
`#@native.focus()`
self
end

def height
`#@native.height()`
end

def width
`#@native.width()`
end

def serialize
`#@native.serialize()`
end

def is(selector)
`#@native.is(selector)`
end

def data(key, val=undefined)
%x{
if (val == undefined) {
return #@native.data(key);
}
else {
return #@native.data(key, val);
}
}
end

def closest(selector)
Element.new `#@native.closest(selector)`
end

def find(content)
Element.new `#@native.find(content)`
Expand Down Expand Up @@ -191,11 +219,24 @@ def []=(name, value)
`#@native.attr(name, value)`
end

def add_attribute name
def attr(name, val=nil)
if val
self[name] = val
else
self[name]
end
end

def add_attribute(name)
`#@native.attr(name, '')`
end

def has_attribute? name
def remove_attr(name)
`#@native.removeAttr(name)`
self
end

def has_attribute?(name)
`!!#@native.attr(name)`
end

Expand All @@ -207,18 +248,18 @@ def append_to_head
`#@native.appendTo(document.head)`
end

# Returns the element at the given index as a new `DOM` instance.
# 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
#
# DOM('.foo')[0] # => first element in collection
# DOM('.foo')[-1] # => last element from collection
# DOM('.foo')[100] # => returns nil if index outside range
# 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 [DOM, nil] returns new collection with returned element
# @return [Element, nil] returns new collection with returned element
def at(index)
%x{
var length = #@native.length;
Expand All @@ -241,7 +282,7 @@ def at(index)
#
# @example
#
# DOM('<p class="foo"></p>').class_name
# Element.find('<p class="foo"></p>').class_name
# # => "foo"
#
# @return [String] the class name
Expand All @@ -258,10 +299,10 @@ def class_name
#
# @example
#
# DOM('#foo').class_name = "title"
# Element.find('#foo').class_name = "title"
#
# @param [String] name the class name to set on each element
# @return [DOM] returns the receiver
# @return [Element] returns the receiver
def class_name=(name)
%x{
for (var i = 0, length = #@native.length; i < length; i++) {
Expand All @@ -280,7 +321,7 @@ def class_name=(name)
#
# @example
#
# foo = DOM '#foo'
# foo = Element.find '#foo'
# foo.css 'background-color' # => "red"
# foo.css 'background-color', 'green'
# foo.css 'background-color' # => "green"
Expand All @@ -289,7 +330,7 @@ def class_name=(name)
# @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, DOM] returns css value or the receiver
# @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 @@ -306,16 +347,16 @@ def css(name, value=nil)
#
# @example
#
# foo = DOM "#foo"
# 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 [DOM] receiver
# @return [Element] receiver
def animate(params, &block)
speed = params.has_key?(:speed) ? params.delete(:speed) : 400
speed = params.delete(:speed) || 400
%x{
#@native.animate(#{params.to_n}, #{speed}, function() {
#{block.call if block_given?}
Expand Down Expand Up @@ -348,10 +389,42 @@ def each
self
end

def filter(selector)
Element.new `#@native.filter(selector)`
end

def first
`#@native.length ? #{Element.new `#@native.first()`} : nil`
end

def last
Element.new `#@native.last()` unless size == 0
end

def stop(*args)
`#@native.stop.apply(#@native, args)`
self
end

def wrap(content)
%x{
if (!content._isString) {
content = content['native'];
}
return #{Element.new `#@native.wrap(content)`};
}
end

def text
`#@native.text() || ""`
end

def text=(text)
`#@native.text(text)`
self
end

def html
`#@native.html() || ""`
end
Expand Down Expand Up @@ -423,6 +496,10 @@ def empty?

alias empty? none?

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

def on(name, sel = nil, &block)
`sel == nil ? #@native.on(name, block) : #@native.on(name, sel, block)`
block
Expand All @@ -448,4 +525,37 @@ def val=(name, val)
val
end
alias value= val=

def scroll_left=(left)
`#@native.scrollLeft(left)`
left
end

def scroll_top=(top)
`#@native.scrollTop(top)`
end

def scroll_left
`#@native.scrollLeft()`
end

def scroll_top
`#@native.scrollTop()`
end

def slide_down(*args)
`#@native.slideDown.apply(#@native, args)`
end

def slide_up(*args)
`#@native.slideUp.apply(#@native, args)`
end

def slide_toggle(*args)
`#@native.slideToggle.apply(#@native, args)`
end

def fade_toggle(*args)
`#@native.fadeToggle.apply(#@native, args)`
end
end

0 comments on commit 89ef05f

Please sign in to comment.