lib/nitro/mixin/javascript.rb in nitro-0.20.0 vs lib/nitro/mixin/javascript.rb in nitro-0.21.0
- old
+ new
@@ -11,126 +11,126 @@
module JavascriptMixin
private
- unless const_defined? :DEFAULT_JAVASCRIPT_FILES
- DEFAULT_JAVASCRIPT_FILES = [
- 'js/behaviour.js',
- 'js/prototype.js',
- 'js/effects.js',
- 'js/dragdrop.js',
- 'js/controls.js'
- ]
- end
+ unless const_defined? :DEFAULT_JAVASCRIPT_FILES
+ DEFAULT_JAVASCRIPT_FILES = [
+ 'js/behaviour.js',
+ 'js/prototype.js',
+ 'js/effects.js',
+ 'js/dragdrop.js',
+ 'js/controls.js'
+ ]
+ end
- # :section: behaviour.js
-
- def behaviour(id, js)
- @_behaviours ||= []
- @_behaviours << [id, js]
- end
-
- # :section: prototype.js
+ # :section: behaviour.js
+
+ def behaviour(id, js)
+ @_behaviours ||= []
+ @_behaviours << [id, js]
+ end
+
+ # :section: prototype.js
- def live_request(id, options = {})
- if href = options.delete(:href)
- behaviour "##{id}", %{
- el.onclick = function() {
- new Ajax.Request('#{href}', #{hash_to_js(options)});
- return false;
- }
- }
- else
- behaviour "##{id}", %{
- el.onclick = function() {
- new Ajax.Request(el.href, #{hash_to_js(options)});
- return false;
- }
- }
- end
- end
- alias_method :live, :live_request
- alias_method :async, :live_request
-
- # :section: script.aculo.us dragdrop.js
-
- # Make the element dragable.
-
- def draggable(id, options = {})
- @_javascript ||= ''
- @_javascript << "\nnew Draggable('#{id}', #{hash_to_js(options)});"
- end
-
- # :section: script.aculo.us controls.js
-
- # Add autocomplete functionality to a text field.
-
- def auto_complete(id, options = {})
- update = options[:update] || "#{id}_auto_complete"
- url = options[:url] || "#{id}_auto_complete"
- @_javascript ||= ''
- @_javascript << "\nnew Ajax.Autocompleter('#{id}', '#{update}', '#{url}');"
-
- # Turn off the browser's autocomplete functionality to avoid
- # interference.
-
- behaviour "##{id}", %{
- el.autocomplete = 'off';
- }
- end
-
- # :section: general javascript helpers.
-
- # Include external javascript file.
-
- def include_script(files = DEFAULT_JAVASCRIPT_FILES)
- code = ''
-
- for file in [files].flatten
- code << %|<script src="#{file}" type="text/javascript">//</script>|
- end
-
- return code
- end
-
- # Escape carrier returns and single and double quotes for JavaScript segments.
+ def live_request(id, options = {})
+ if href = options.delete(:href)
+ behaviour "##{id}", %{
+ el.onclick = function() {
+ new Ajax.Request('#{href}', #{hash_to_js(options)});
+ return false;
+ }
+ }
+ else
+ behaviour "##{id}", %{
+ el.onclick = function() {
+ new Ajax.Request(el.href, #{hash_to_js(options)});
+ return false;
+ }
+ }
+ end
+ end
+ alias_method :live, :live_request
+ alias_method :async, :live_request
+ # :section: script.aculo.us dragdrop.js
+
+ # Make the element dragable.
+
+ def draggable(id, options = {})
+ @_javascript ||= ''
+ @_javascript << "\nnew Draggable('#{id}', #{hash_to_js(options)});"
+ end
+
+ # :section: script.aculo.us controls.js
+
+ # Add autocomplete functionality to a text field.
+
+ def auto_complete(id, options = {})
+ update = options[:update] || "#{id}_auto_complete"
+ url = options[:url] || "#{id}_auto_complete"
+ @_javascript ||= ''
+ @_javascript << "\nnew Ajax.Autocompleter('#{id}', '#{update}', '#{url}');"
+
+ # Turn off the browser's autocomplete functionality to avoid
+ # interference.
+
+ behaviour "##{id}", %{
+ el.autocomplete = 'off';
+ }
+ end
+
+ # :section: general javascript helpers.
+
+ # Include external javascript file.
+
+ def include_script(files = DEFAULT_JAVASCRIPT_FILES)
+ code = ''
+
+ for file in [files].flatten
+ code << %|<script src="#{file}" type="text/javascript">//</script>|
+ end
+
+ return code
+ end
+
+ # Escape carrier returns and single and double quotes for JavaScript segments.
+
def escape_javascript(js)
- (js || '').gsub(/\r\n|\n|\r/, "\\n").gsub(/["']/) { |m| "\\#{m}" }
+ (js || '').gsub(/\r\n|\n|\r/, "\\n").gsub(/["']/) { |m| "\\#{m}" }
end
- # Converts a Ruby hash to a Javascript hash.
-
- def hash_to_js(options)
+ # Converts a Ruby hash to a Javascript hash.
+
+ def hash_to_js(options)
'{' + options.map {|k, v| "#{k}:#{v}"}.join(', ') + '}'
end
- # Emits the aggregated helper javascript.
- #--
- # FIXME: refactor this!
- #++
-
- def helper_script
- code = %|<script type="text/javascript">\n|
- unless @_behaviours.empty?
- code << %|var _behaviours = {\n|
- compo = []
- for id, js in @_behaviours
- compo << %|'#{id}': function(el) { #{js} \n }|
- end
- code << compo.join(',')
- code << %|
- }
- Behaviour.register(_behaviours);
- |
- end
- code << %|
- #@_javascript
- </script>
- |
- end
-
+ # Emits the aggregated helper javascript.
+ #--
+ # FIXME: refactor this!
+ #++
+
+ def helper_script
+ code = %|<script type="text/javascript">\n|
+ unless @_behaviours.empty?
+ code << %|var _behaviours = {\n|
+ compo = []
+ for id, js in @_behaviours
+ compo << %|'#{id}': function(el) { #{js} \n }|
+ end
+ code << compo.join(',')
+ code << %|
+ }
+ Behaviour.register(_behaviours);
+ |
+ end
+ code << %|
+ #@_javascript
+ </script>
+ |
+ end
+
end
end
# * George Moschovitis <gm@navel.gr>