Browse files

much cleaner and flexible

  • Loading branch information...
1 parent 2e8b36d commit 492dc03de1c480f5b2d6172378e48ecdcb5d9ade @kristianmandrup committed Sep 5, 2012
Showing with 122 additions and 101 deletions.
  1. +25 −19 README.rdoc
  2. +97 −82 lib/apotomo/javascript_generator.rb
View
44 README.rdoc
@@ -181,29 +181,35 @@ render js: top_item + append_to(:_top_item, markup)
Will select `.item:first` under the widget container element as a variable `_apo_top_item` and then append the markup to the DOM element(s) pointed to by that variable.
```
-* update_text(id, selector, markup)
-* append(id, selector, markup)
-* prepend(id, selector, markup)
+Inverse jQuery actions
+
* append_to(selector, markup)
* prepend_to(selector, markup)
-* after(selector, markup)
-* before(selector, markup)
* replace_all(selector, markup)
-* unwrap(selector)
-* wrap(selector, markup)
-* wrap_inner(selector, markup)
-* wrap_all(selector, markup)
-* remove(selector)
-* remove_class(selector, *classes)
-* add_class(selector, *classes)
-* toggle_class(selector, *classes)
-* toggle_class_fun(selector, fun)
-* get_attr(selector, name)
-* get_prop(selector, name)
-* get_val selector
-* get_html(selector)
-* empty(selector)
+Normal jQuery action
+
+* update_text(id, selector, markup)
+* append(id, selector, markup)
+* prepend(id, selector, markup)
+* after(id, selector, markup)
+* before(id, selector, markup)
+* unwrap(id, selector)
+* wrap(id, selector, markup)
+* wrap_inner(id, selector, markup)
+* wrap_all(id, selector, markup)
+* remove(id, selector)
+* remove_class(id, selector, *classes)
+* add_class(id, selector, *classes)
+* toggle_class(id, selector, *classes)
+* toggle_class_fun(id, selector, fun)
+* get_attr(id, selector, name)
+* get_prop(id, selector, name)
+* get_val(id, selector)
+* get_html(id, selector)
+* empty(id, selector)
+
+Note: The first argument id is always optional
== Testing
View
179 lib/apotomo/javascript_generator.rb
@@ -61,137 +61,152 @@ def update_text(id, selector, markup)
def selector_for var, id, selector
raise ArgumentError, "Must not be an _apo_ selector here: #{selector}" if apo_match?(selector)
- "var _apo_#{var} = " + element(id) + ".find('#{selector}');"
+ "var _apo_#{var} = " + element("##{id}") + ".find('#{selector}');"
end
- def find_element id, selector
- element(id) + ".find('#{selector}')"
+ def find_element id, selector
+ if id == nil || apo_selector?(selector)
+ return element(calc_selector selector)
+ end
+ element("##{id}") + ".find('#{selector}')"
end
- def append(id, selector, markup)
- find_element(id, selector) + ".append(#{escaped(markup)});"
+ [:replace_all, :prepend_to, :append_to].each do |name|
+ define_method name do |selector, markup|
+ _jq_inverse_action(selector, markup, name.to_sym)
+ end
end
- def prepend(id, selector, markup)
- find_element(id, selector) + ".prepend(#{escaped(markup)});"
+ [:append, :prepend, :after, :before, :wrap, :wrap_inner, :wrap_all].each do |name|
+ define_method name do |args|
+ _jquery_action *args, name
+ end
end
- def append_to(selector, markup)
- selector = calc_selector selector
- "$(#{escaped(markup)}).appendTo(#{selector});"
- end
-
- def prepend_to(selector, markup)
- selector = calc_selector selector
- "$(#{escaped(markup)}).prependTo(#{selector});"
- end
-
- def after selector, markup
- selector = calc_selector selector
- element(selector) + ".after(#{escaped(markup)});"
- end
-
- def before selector, markup
- selector = calc_selector selector
- element(selector) + ".before(#{escaped(markup)});"
- end
-
- def replace_all(selector, markup)
- selector = calc_selector selector
- "$(#{escaped(markup)}).replaceAll(#{selector});"
- end
-
- def unwrap(selector)
- selector = calc_selector selector
- element(selector) + ".unwrap();"
- end
-
- def wrap(selector, markup)
- selector = calc_selector selector
- element(selector) + ".wrap(#{escaped(markup)});"
- end
-
- def wrap_inner(selector, markup)
- selector = calc_selector selector
- element(selector) + ".wrapInner(#{escaped(markup)});"
- end
-
- def wrap_all(selector, markup)
- selector = calc_selector selector
- element(selector) + ".wrap_all(#{escaped(markup)});"
+ def unwrap *args
+ jquery_action *args, 'unwrap()'
end
- def remove(selector)
- selector = calc_selector selector
- element(selector) + ".remove();"
+ def remove *args
+ _jquery_action *args, 'remove()'
end
- def remove_class(selector, *classes)
+ def remove_class(id, selector, *classes)
classes = classes.flatten.join(' ')
- selector = calc_selector selector
- element(selector) + ".removeClass('#{classes}');"
+ find_element(id, selector) + _js_action("removeClass('#{classes}')")
end
alias_method :remove_classes, :remove_class
- def add_class(selector, *classes)
+ def add_class(id, selector, *classes)
classes = classes.flatten.join(' ')
- selector = calc_selector selector
- element(selector) + ".addClass('#{classes}');"
+ find_element(id, selector) + _js_action("addClass('#{classes}')"
end
alias_method :add_classes, :add_class
- def toggle_class(selector, *classes)
+ def toggle_class(id, selector, *classes)
classes = classes.flatten.join(' ')
- selector = calc_selector selector
- element(selector) + ".toggleClass('#{classes}');"
+ find_element(id, selector) + _js_action("toggleClass('#{classes}')")
end
alias_method :toggle_classes, :toggle_class
- def toggle_class_fun(selector, fun)
- selector = calc_selector selector
- element(selector) + ".toggleClass(function() {#{fun}});"
+ def toggle_class_fun(id, selector, fun)
+ find_element(id, selector) + _js_action("toggleClass(function() {#{fun}})")
end
- def get_attr(selector, name)
- selector = calc_selector selector
- element(selector) + ".attr('#{name}');"
+ def get_attr(id. selector, name)
+ find_element(id, selector) + _js_action("attr('#{name}')")
end
- def get_prop(selector, name)
- selector = calc_selector selector
- element(selector) + ".prop('#{name}');"
+ def get_prop(selector, name)
+ find_element(id, selector) + _js_action("prop('#{name}')")
end
- def get_val selector
- selector = calc_selector selector
- element(selector) + ".val();"
+ def get_val selector
+ find_element(id, selector) + _js_action("val()")
end
def get_html(selector)
- selector = calc_selector selector
- element(selector) + ".html();"
+ find_element(id, selector) + _js_action("html()")
end
def empty(selector)
- selector = calc_selector selector
- element(selector) + ".empty();"
+ find_element(id, selector) + _js_action("empty()")
end
private
+ def _jq_inverse_action(selector, markup, action)
+ selector = calc_selector selector
+ action = _js_camelize(action)
+ "$(#{escaped(markup)}).#{action}(#{selector});"
+ end
+
+ def _jquery_action *args, &block
+ args = args.flatten
+ [id, selector, markup, action] = _extract_args(*args)
+ action ||= yield if block_given?
+ raise ArgumentError, "Must take action block or Symbol as last argument" unless action
+ elem_action = case action
+ when String
+ action = _js_camelize(action)
+ ".#{action};"
+ else
+ action = _js_camelize(action)
+ _action(action, markup)
+ end
+ find_element(id, selector) + elem_action
+ end
+
def escaped markup
"'#{escape(markup)}'"
+ end
+
+ def _action name, markup
+ _js_action "#{name}(#{_escaped(markup)})"
+ end
+
+ def _js_action action
+ ".#{action};"
+ end
+
+ def _js_camelize str
+ str.to_s.camelize.sub(/^\w/, s[0].downcase)
+ end
+
+ # id, selector, markup, action
+ def _extract_args *args
+ [_extract_id(*args), _extract_selector(*args), _extract_markup(*args), _extract_action(*args)]
+ end
+
+ def _extract_id *args
+ args = args.flatten
+ args.size == 4 ? args.first : nil
+ end
+
+ def _extract_selector *args
+ args = args.flatten
+ args.size == 4 ? args[1] : args[0]
+ end
+
+ def _extract_markup(*args)
+ args = args.flatten
+ args.size == 4 ? args[2] : args.last
+ end
+
+ def _extract_action(*args)
+ args = args.flatten
+ args.size == 4 ? args.last : nil
end
- def apo_match? selector
+ def _apo_match? selector
selector.to_s =~ /^_apo_/
end
- def calc_selector selector
- selector = apo_selector?(selector) ? "_apo_#{selector}" : "'#{selector}'"
+ def _calc_selector selector
+ selector = _apo_selector?(selector) ? "_apo_#{selector}" : "'#{selector}'"
end
- def apo_selector? selector
+ def _apo_selector? selector
selector.kind_of?(Symbol) && selector.to_s[0..1] == '_'
end
end

0 comments on commit 492dc03

Please sign in to comment.