Permalink
Browse files

initial content with some first organization, it does not still run d…

…ue to strange quantum phenomena
  • Loading branch information...
fxn committed Apr 7, 2011
0 parents commit b94e3cf4ed56eac30d2a45ca554cf23e917087db
Showing with 3,299 additions and 0 deletions.
  1. +42 −0 .gitignore
  2. +7 −0 README
  3. +852 −0 lib/action_view/helpers/prototype_helper.rb
  4. +263 −0 lib/action_view/helpers/scriptaculous_helper.rb
  5. +14 −0 lib/action_view/template/handlers/rjs.rb
  6. +22 −0 lib/rjs.rb
  7. +65 −0 lib/rjs/javascript_helper.rb
  8. +12 −0 lib/rjs/renderers.rb
  9. +13 −0 lib/rjs/rendering.rb
  10. +208 −0 lib/rjs/selector_assertions.rb
  11. +14 −0 rjs.gemspec
  12. +473 −0 test/assert_select_test.rb
  13. +41 −0 test/controller/caching_test.rb
  14. +16 −0 test/controller/content_type_test.rb
  15. +213 −0 test/controller/mime_responds_test.rb
  16. +19 −0 test/controller/new_base/content_type_test.rb
  17. +71 −0 test/controller/new_base/render_rjs_test.rb
  18. +24 −0 test/controller/render_js_test.rb
  19. +6 −0 test/fixtures/functional_caching/formatted_fragment_cached.js.rjs
  20. +1 −0 test/fixtures/functional_caching/js_fragment_cached_with_partial.js.rjs
  21. +1 −0 test/fixtures/old_content_type/render_default_for_rjs.rjs
  22. +1 −0 test/fixtures/respond_to/all_types_with_layout.js.rjs
  23. +1 −0 test/fixtures/respond_to/using_defaults.js.rjs
  24. +1 −0 test/fixtures/respond_to/using_defaults_with_type_list.js.rjs
  25. +1 −0 test/fixtures/respond_with/using_resource.js.rjs
  26. +2 −0 test/fixtures/test/delete_with_js.rjs
  27. +6 −0 test/fixtures/test/enum_rjs_test.rjs
  28. +1 −0 test/fixtures/test/greeting.js.rjs
  29. +1 −0 test/fixtures/test/render_explicit_html_template.js.rjs
  30. +1 −0 test/fixtures/test/render_implicit_html_template.js.rjs
  31. +61 −0 test/javascript_helper_test.rb
  32. +258 −0 test/render_other_test.rb
  33. +476 −0 test/template/prototype_helper_test.rb
  34. +26 −0 test/template/render_test.rb
  35. +86 −0 test/template/scriptaculous_helper_test.rb
@@ -0,0 +1,42 @@
+# rcov generated
+coverage
+
+# rdoc generated
+rdoc
+
+# yard generated
+doc
+.yardoc
+
+# bundler
+.bundle
+
+# jeweler generated
+pkg
+
+# Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore:
+#
+# * Create a file at ~/.gitignore
+# * Include files you want ignored
+# * Run: git config --global core.excludesfile ~/.gitignore
+#
+# After doing this, these files will be ignored in all your git projects,
+# saving you from having to 'pollute' every project you touch with them
+#
+# Not sure what to needs to be ignored for particular editors/OSes? Here's some ideas to get you started. (Remember, remove the leading # of the line)
+#
+# For MacOS:
+#
+#.DS_Store
+#
+# For TextMate
+#*.tmproj
+#tmtags
+#
+# For emacs:
+#*~
+#\#*
+#.\#*
+#
+# For vim:
+#*.swp
7 README
@@ -0,0 +1,7 @@
+RJS is being extracted here for Rails 3.1.
+
+New applications using this may want to add
+
+ config.action_view.debug_rjs = true
+
+to their config/environments/development.rb.

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -0,0 +1,263 @@
+require 'action_view/helpers/javascript_helper'
+require 'active_support/json'
+
+module ActionView
+ # = Action View Scriptaculous Helpers
+ module Helpers
+ # Provides a set of helpers for calling Scriptaculous[http://script.aculo.us/]
+ # JavaScript functions, including those which create Ajax controls and visual
+ # effects.
+ #
+ # To be able to use these helpers, you must include the Prototype
+ # JavaScript framework and the Scriptaculous JavaScript library in your
+ # pages. See the documentation for ActionView::Helpers::JavaScriptHelper
+ # for more information on including the necessary JavaScript.
+ #
+ # The Scriptaculous helpers' behavior can be tweaked with various options.
+ #
+ # See the documentation at http://script.aculo.us for more information on
+ # using these helpers in your application.
+ module ScriptaculousHelper
+ TOGGLE_EFFECTS = [:toggle_appear, :toggle_slide, :toggle_blind]
+
+ # Returns a JavaScript snippet to be used on the Ajax callbacks for
+ # starting visual effects.
+ #
+ # If no +element_id+ is given, it assumes "element" which should be a local
+ # variable in the generated JavaScript execution context. This can be
+ # used for example with +drop_receiving_element+:
+ #
+ # <%= drop_receiving_element (...), :loading => visual_effect(:fade) %>
+ #
+ # This would fade the element that was dropped on the drop receiving
+ # element.
+ #
+ # For toggling visual effects, you can use <tt>:toggle_appear</tt>, <tt>:toggle_slide</tt>, and
+ # <tt>:toggle_blind</tt> which will alternate between appear/fade, slidedown/slideup, and
+ # blinddown/blindup respectively.
+ #
+ # You can change the behaviour with various options, see
+ # http://script.aculo.us for more documentation.
+ def visual_effect(name, element_id = false, js_options = {})
+ element = element_id ? ActiveSupport::JSON.encode(element_id) : "element"
+
+ js_options[:queue] = if js_options[:queue].is_a?(Hash)
+ '{' + js_options[:queue].map {|k, v| k == :limit ? "#{k}:#{v}" : "#{k}:'#{v}'" }.join(',') + '}'
+ elsif js_options[:queue]
+ "'#{js_options[:queue]}'"
+ end if js_options[:queue]
+
+ [:endcolor, :direction, :startcolor, :scaleMode, :restorecolor].each do |option|
+ js_options[option] = "'#{js_options[option]}'" if js_options[option]
+ end
+
+ if TOGGLE_EFFECTS.include? name.to_sym
+ "Effect.toggle(#{element},'#{name.to_s.gsub(/^toggle_/,'')}',#{options_for_javascript(js_options)});"
+ else
+ "new Effect.#{name.to_s.camelize}(#{element},#{options_for_javascript(js_options)});"
+ end
+ end
+
+ # Makes the element with the DOM ID specified by +element_id+ sortable
+ # by drag-and-drop and make an Ajax call whenever the sort order has
+ # changed. By default, the action called gets the serialized sortable
+ # element as parameters.
+ #
+ # Example:
+ #
+ # <%= sortable_element("my_list", :url => { :action => "order" }) %>
+ #
+ # In the example, the action gets a "my_list" array parameter
+ # containing the values of the ids of elements the sortable consists
+ # of, in the current order.
+ #
+ # Important: For this to work, the sortable elements must have id
+ # attributes in the form "string_identifier". For example, "item_1". Only
+ # the identifier part of the id attribute will be serialized.
+ #
+ # Additional +options+ are:
+ #
+ # * <tt>:format</tt> - A regular expression to determine what to send as the
+ # serialized id to the server (the default is <tt>/^[^_]*_(.*)$/</tt>).
+ #
+ # * <tt>:constraint</tt> - Whether to constrain the dragging to either
+ # <tt>:horizontal</tt> or <tt>:vertical</tt> (or false to make it unconstrained).
+ #
+ # * <tt>:overlap</tt> - Calculate the item overlap in the <tt>:horizontal</tt>
+ # or <tt>:vertical</tt> direction.
+ #
+ # * <tt>:tag</tt> - Which children of the container element to treat as
+ # sortable (default is <tt>li</tt>).
+ #
+ # * <tt>:containment</tt> - Takes an element or array of elements to treat as
+ # potential drop targets (defaults to the original target element).
+ #
+ # * <tt>:only</tt> - A CSS class name or array of class names used to filter
+ # out child elements as candidates.
+ #
+ # * <tt>:scroll</tt> - Determines whether to scroll the list during drag
+ # operations if the list runs past the visual border.
+ #
+ # * <tt>:tree</tt> - Determines whether to treat nested lists as part of the
+ # main sortable list. This means that you can create multi-layer lists,
+ # and not only sort items at the same level, but drag and sort items
+ # between levels.
+ #
+ # * <tt>:hoverclass</tt> - If set, the Droppable will have this additional CSS class
+ # when an accepted Draggable is hovered over it.
+ #
+ # * <tt>:handle</tt> - Sets whether the element should only be draggable by an
+ # embedded handle. The value may be a string referencing a CSS class value
+ # (as of script.aculo.us V1.5). The first child/grandchild/etc. element
+ # found within the element that has this CSS class value will be used as
+ # the handle.
+ #
+ # * <tt>:ghosting</tt> - Clones the element and drags the clone, leaving
+ # the original in place until the clone is dropped (default is <tt>false</tt>).
+ #
+ # * <tt>:dropOnEmpty</tt> - If true the Sortable container will be made into
+ # a Droppable, that can receive a Draggable (as according to the containment
+ # rules) as a child element when there are no more elements inside (default
+ # is <tt>false</tt>).
+ #
+ # * <tt>:onChange</tt> - Called whenever the sort order changes while dragging. When
+ # dragging from one Sortable to another, the callback is called once on each
+ # Sortable. Gets the affected element as its parameter.
+ #
+ # * <tt>:onUpdate</tt> - Called when the drag ends and the Sortable's order is
+ # changed in any way. When dragging from one Sortable to another, the callback
+ # is called once on each Sortable. Gets the container as its parameter.
+ #
+ # See http://script.aculo.us for more documentation.
+ def sortable_element(element_id, options = {})
+ javascript_tag(sortable_element_js(element_id, options).chop!)
+ end
+
+ def sortable_element_js(element_id, options = {}) #:nodoc:
+ options[:with] ||= "Sortable.serialize(#{ActiveSupport::JSON.encode(element_id)})"
+ options[:onUpdate] ||= "function(){" + remote_function(options) + "}"
+ options.delete_if { |key, value| PrototypeHelper::AJAX_OPTIONS.include?(key) }
+
+ [:tag, :overlap, :constraint, :handle].each do |option|
+ options[option] = "'#{options[option]}'" if options[option]
+ end
+
+ options[:containment] = array_or_string_for_javascript(options[:containment]) if options[:containment]
+ options[:only] = array_or_string_for_javascript(options[:only]) if options[:only]
+
+ %(Sortable.create(#{ActiveSupport::JSON.encode(element_id)}, #{options_for_javascript(options)});)
+ end
+
+ # Makes the element with the DOM ID specified by +element_id+ draggable.
+ #
+ # Example:
+ # <%= draggable_element("my_image", :revert => true)
+ #
+ # You can change the behaviour with various options, see
+ # http://script.aculo.us for more documentation.
+ def draggable_element(element_id, options = {})
+ javascript_tag(draggable_element_js(element_id, options).chop!)
+ end
+
+ def draggable_element_js(element_id, options = {}) #:nodoc:
+ %(new Draggable(#{ActiveSupport::JSON.encode(element_id)}, #{options_for_javascript(options)});)
+ end
+
+ # Makes the element with the DOM ID specified by +element_id+ receive
+ # dropped draggable elements (created by +draggable_element+).
+ # and make an AJAX call. By default, the action called gets the DOM ID
+ # of the element as parameter.
+ #
+ # Example:
+ # <%= drop_receiving_element("my_cart", :url =>
+ # { :controller => "cart", :action => "add" }) %>
+ #
+ # You can change the behaviour with various options, see
+ # http://script.aculo.us for more documentation.
+ #
+ # Some of these +options+ include:
+ # * <tt>:accept</tt> - Set this to a string or an array of strings describing the
+ # allowable CSS classes that the +draggable_element+ must have in order
+ # to be accepted by this +drop_receiving_element+.
+ #
+ # * <tt>:confirm</tt> - Adds a confirmation dialog. Example:
+ #
+ # :confirm => "Are you sure you want to do this?"
+ #
+ # * <tt>:hoverclass</tt> - If set, the +drop_receiving_element+ will have
+ # this additional CSS class when an accepted +draggable_element+ is
+ # hovered over it.
+ #
+ # * <tt>:onDrop</tt> - Called when a +draggable_element+ is dropped onto
+ # this element. Override this callback with a JavaScript expression to
+ # change the default drop behaviour. Example:
+ #
+ # :onDrop => "function(draggable_element, droppable_element, event) { alert('I like bananas') }"
+ #
+ # This callback gets three parameters: The Draggable element, the Droppable
+ # element and the Event object. You can extract additional information about
+ # the drop - like if the Ctrl or Shift keys were pressed - from the Event object.
+ #
+ # * <tt>:with</tt> - A JavaScript expression specifying the parameters for
+ # the XMLHttpRequest. Any expressions should return a valid URL query string.
+ def drop_receiving_element(element_id, options = {})
+ javascript_tag(drop_receiving_element_js(element_id, options).chop!)
+ end
+
+ def drop_receiving_element_js(element_id, options = {}) #:nodoc:
+ options[:with] ||= "'id=' + encodeURIComponent(element.id)"
+ options[:onDrop] ||= "function(element){" + remote_function(options) + "}"
+ options.delete_if { |key, value| PrototypeHelper::AJAX_OPTIONS.include?(key) }
+
+ options[:accept] = array_or_string_for_javascript(options[:accept]) if options[:accept]
+ options[:hoverclass] = "'#{options[:hoverclass]}'" if options[:hoverclass]
+
+ # Confirmation happens during the onDrop callback, so it can be removed from the options
+ options.delete(:confirm) if options[:confirm]
+
+ %(Droppables.add(#{ActiveSupport::JSON.encode(element_id)}, #{options_for_javascript(options)});)
+ end
+
+ protected
+ def array_or_string_for_javascript(option)
+ if option.kind_of?(Array)
+ "['#{option.join('\',\'')}']"
+ elsif !option.nil?
+ "'#{option}'"
+ end
+ end
+ end
+
+ module PrototypeHelper
+ class JavaScriptGenerator
+ module GeneratorMethods
+ # Starts a script.aculo.us visual effect. See
+ # ActionView::Helpers::ScriptaculousHelper for more information.
+ def visual_effect(name, id = nil, options = {})
+ record @context.send(:visual_effect, name, id, options)
+ end
+
+ # Creates a script.aculo.us sortable element. Useful
+ # to recreate sortable elements after items get added
+ # or deleted.
+ # See ActionView::Helpers::ScriptaculousHelper for more information.
+ def sortable(id, options = {})
+ record @context.send(:sortable_element_js, id, options)
+ end
+
+ # Creates a script.aculo.us draggable element.
+ # See ActionView::Helpers::ScriptaculousHelper for more information.
+ def draggable(id, options = {})
+ record @context.send(:draggable_element_js, id, options)
+ end
+
+ # Creates a script.aculo.us drop receiving element.
+ # See ActionView::Helpers::ScriptaculousHelper for more information.
+ def drop_receiving(id, options = {})
+ record @context.send(:drop_receiving_element_js, id, options)
+ end
+ end
+ end
+ end
+ end
+end
@@ -0,0 +1,14 @@
+module ActionView
+ module Template::Handlers
+ class RJS
+ # Default format used by RJS.
+ class_attribute :default_format
+ self.default_format = Mime::JS
+
+ def call(template)
+ "update_page do |page|;#{template.source}\nend"
+ end
+ end
+ end
+end
+
@@ -0,0 +1,22 @@
+ActiveSupport.on_load(:action_controller) do
+ ActiveSupport.on_load(:action_view) do
+ require 'action_view/helpers/prototype_helper'
+ require 'action_view/helpers/scriptaculous_helper'
+ require 'action_view/template/handlers/rjs'
+
+ require 'rjs/selector_assertions'
+ require 'rjs/renderers'
+ require 'rjs/javascript_helper'
+ require 'rjs/rendering'
+
+ ActionView::Base.class_eval do
+ cattr_accessor :debug_rjs
+ @@debug_rjs = false
+ end
+
+ ActionView::Helpers.send(:include, ActionView::Helpers::PrototypeHelper)
+ ActionView::Helpers.send(:include, ActionView::Helpers::ScriptaculousHelper)
+
+ ActionView::Template.register_template_handler :rjs, ActionView::Template::Handlers::RJS.new
+ end
+end
Oops, something went wrong.

0 comments on commit b94e3cf

Please sign in to comment.