Changeset 1561
- Timestamp:
- 06/29/05 08:09:00 (3 years ago)
- Files:
-
- trunk/actionpack/lib/action_view/helpers/javascript_helper.rb (modified) (10 diffs)
- trunk/actionpack/README (modified) (2 diffs)
- trunk/actionpack/test/template/javascript_helper.rb (modified) (2 diffs)
- trunk/actionpack/test/template/upload_progress_helper_test.rb (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/actionpack/lib/action_view/helpers/javascript_helper.rb
r1552 r1561 3 3 module ActionView 4 4 module Helpers 5 # Provides a set of helpers for calling Java script functions and, most importantly, to call remote methods using what has5 # Provides a set of helpers for calling JavaScript functions and, most importantly, to call remote methods using what has 6 6 # been labelled Ajax[http://www.adaptivepath.com/publications/essays/archives/000385.php]. This means that you can call 7 7 # actions in your controllers without reloading the page, but still update certain parts of it using injections into the 8 8 # DOM. The common use case is having a form that adds a new element to a list without reloading the page. 9 9 # 10 # To be able to use the Java script helpers, you must either call <tt><%= define_javascript_functions %></tt> (which returns all11 # the Java script support functions in a <script> block) or reference the Javascript library using10 # To be able to use the JavaScript helpers, you must either call <tt><%= define_javascript_functions %></tt> (which returns all 11 # the JavaScript support functions in a <script> block) or reference the JavaScript library using 12 12 # <tt><%= javascript_include_tag "prototype" %></tt> (which looks for the library in /javascripts/prototype.js). The latter is 13 13 # recommended as the browser can then cache the library instead of fetching all the functions anew on every request. … … 15 15 # If you're the visual type, there's an Ajax movie[http://www.rubyonrails.com/media/video/rails-ajax.mov] demonstrating 16 16 # the use of form_remote_tag. 17 module Java scriptHelper17 module JavaScriptHelper 18 18 unless const_defined? :CALLBACKS 19 19 CALLBACKS = … … 102 102 # 103 103 # You can customize further browser side call logic by passing 104 # in Java script code snippets via some optional parameters. In104 # in JavaScript code snippets via some optional parameters. In 105 105 # their order of use these are: 106 106 # … … 127 127 128 128 # Returns a form tag that will submit using XMLHttpRequest in the background instead of the regular 129 # reloading POST arrangement. Even though it's using Java script to serialize the form elements, the form submission129 # reloading POST arrangement. Even though it's using JavaScript to serialize the form elements, the form submission 130 130 # will work just like a regular submission as viewed by the receiving side (all elements available in @params). 131 131 # The options for specifying the target with :url and defining callbacks is the same as link_to_remote. 132 132 # 133 # A "fall-through" target for browsers that doesn't do Java script can be specified with the :action/:method options on :html133 # A "fall-through" target for browsers that doesn't do JavaScript can be specified with the :action/:method options on :html 134 134 # 135 135 # form_remote_tag :html => { :action => url_for(:controller => "some", :action => "place") } … … 187 187 end 188 188 189 # Includes the Action Pack Java script libraries inside a single <script>189 # Includes the Action Pack JavaScript libraries inside a single <script> 190 190 # tag. The function first includes prototype.js and then its core extensions, 191 191 # (determined by filenames starting with "prototype"). … … 227 227 # innerHTML should be updated with the 228 228 # XMLHttpRequest response text. 229 # <tt>:with</tt>:: A Java script expression specifying the229 # <tt>:with</tt>:: A JavaScript expression specifying the 230 230 # parameters for the XMLHttpRequest. This defaults 231 231 # to 'value', which in the evaluated context … … 271 271 # entries returned by the Ajax request. 272 272 # Defaults to field_id + '_auto_complete' 273 # <tt>:with</tt>:: A Java script expression specifying the273 # <tt>:with</tt>:: A JavaScript expression specifying the 274 274 # parameters for the XMLHttpRequest. This defaults 275 275 # to 'value', which in the evaluated context … … 352 352 end 353 353 354 # Escape carrier returns and single and double quotes for Java script segments.354 # Escape carrier returns and single and double quotes for JavaScript segments. 355 355 def escape_javascript(javascript) 356 356 (javascript || '').gsub(/\r\n|\n|\r/, "\\n").gsub(/["']/) { |m| "\\#{m}" } 357 357 end 358 358 359 # Returns a Java script tag with the +content+ inside. Example:359 # Returns a JavaScript tag with the +content+ inside. Example: 360 360 # javascript_tag "alert('All is good')" # => <script type="text/javascript">alert('All is good')</script> 361 361 def javascript_tag(content) … … 374 374 js_options['method'] = method_option_to_s(options[:method]) if options[:method] 375 375 js_options['insertion'] = "Insertion.#{options[:position].to_s.camelize}" if options[:position] 376 js_options[' script']= options[:script] == true if options[:script]376 js_options['evalScripts'] = options[:script] == true if options[:script] 377 377 378 378 if options[:form] … … 438 438 end 439 439 end 440 441 JavascriptHelper = JavaScriptHelper unless const_defined? :JavascriptHelper 440 442 end 441 443 end trunk/actionpack/README
r1129 r1561 169 169 170 170 171 * Java script and Ajax integration.171 * JavaScript and Ajax integration. 172 172 173 173 link_to_function "Greeting", "alert('Hello world!')" … … 175 175 :url => { :action => "destroy", :id => post.id } 176 176 177 {Learn more}[link:classes/ActionView/Helpers/Java scriptHelper.html]177 {Learn more}[link:classes/ActionView/Helpers/JavaScriptHelper.html] 178 178 179 179 trunk/actionpack/test/template/javascript_helper.rb
r1522 r1561 1 1 require File.dirname(__FILE__) + '/../abstract_unit' 2 2 3 class Java scriptHelperTest < Test::Unit::TestCase4 include ActionView::Helpers::Java scriptHelper3 class JavaScriptHelperTest < Test::Unit::TestCase 4 include ActionView::Helpers::JavaScriptHelper 5 5 6 6 include ActionView::Helpers::UrlHelper … … 20 20 def test_define_javascript_functions 21 21 # check if prototype.js is included first 22 assert_not_nil define_javascript_functions.split("\n")[1].match(/Prototype : an object-oriented Javascript library/)22 assert_not_nil define_javascript_functions.split("\n")[1].match(/Prototype JavaScript framework/) 23 23 end 24 24 trunk/actionpack/test/template/upload_progress_helper_test.rb
r1553 r1561 40 40 include ActionView::Helpers::TagHelper 41 41 include ActionView::Helpers::UrlHelper 42 include ActionView::Helpers::Java scriptHelper42 include ActionView::Helpers::JavaScriptHelper 43 43 include ActionView::Helpers::UploadProgressHelper 44 44 … … 176 176 include ActionView::Helpers::TagHelper 177 177 include ActionView::Helpers::UrlHelper 178 include ActionView::Helpers::Java scriptHelper178 include ActionView::Helpers::JavaScriptHelper 179 179 include ActionView::Helpers::UploadProgressHelper 180 180 … … 259 259 def test_form_tag_with_upload_progress 260 260 assert_equal( 261 "<form action=\"http://www.example.com\" enctype=\"multipart/form-data\" method=\"post\" onsubmit=\"if (this.action.indexOf('upload_id') < 0){ this.action += '?upload_id=1'; }this.target = 'UploadTarget1';$('UploadStatus1').innerHTML='Upload starting...'; $('UploadProgressBar1').firstChild.firstChild.style.width='0%'; if (document.uploadStatus1) { document.uploadStatus1.stop(); }document.uploadStatus1 = new Ajax.PeriodicalUpdater('UploadStatus1','http://www.example.com', { script:true, onComplete:function(request){$('UploadStatus1').innerHTML='A message';$('UploadProgressBar1').firstChild.firstChild.style.width='100%';document.uploadStatus1 = null}, asynchronous:true}.extend({decay:1.8,freqency:2.0})); return true\"><iframe id=\"UploadTarget1\" name=\"UploadTarget1\" src=\"\" style=\"width:0px;height:0px;border:0\"></iframe>",261 "<form action=\"http://www.example.com\" enctype=\"multipart/form-data\" method=\"post\" onsubmit=\"if (this.action.indexOf('upload_id') < 0){ this.action += '?upload_id=1'; }this.target = 'UploadTarget1';$('UploadStatus1').innerHTML='Upload starting...'; $('UploadProgressBar1').firstChild.firstChild.style.width='0%'; if (document.uploadStatus1) { document.uploadStatus1.stop(); }document.uploadStatus1 = new Ajax.PeriodicalUpdater('UploadStatus1','http://www.example.com', {onComplete:function(request){$('UploadStatus1').innerHTML='A message';$('UploadProgressBar1').firstChild.firstChild.style.width='100%';document.uploadStatus1 = null}, evalScripts:true, asynchronous:true}.extend({decay:1.8,freqency:2.0})); return true\"><iframe id=\"UploadTarget1\" name=\"UploadTarget1\" src=\"\" style=\"width:0px;height:0px;border:0\"></iframe>", 262 262 form_tag_with_upload_progress 263 263 ) … … 266 266 def test_form_tag_with_upload_progress_custom 267 267 assert_equal( 268 "<form action=\"http://www.example.com\" enctype=\"multipart/form-data\" method=\"post\" onsubmit=\"if (this.action.indexOf('upload_id') < 0){ this.action += '?upload_id=5'; }this.target = 'awindow';$('UploadStatus0').innerHTML='Upload starting...'; $('UploadProgressBar0').firstChild.firstChild.style.width='0%'; alert('foo'); if (document.uploadStatus0) { document.uploadStatus0.stop(); }document.uploadStatus0 = new Ajax.PeriodicalUpdater('UploadStatus0','http://www.example.com', { script:true, onComplete:function(request){$('UploadStatus0').innerHTML='A message';$('UploadProgressBar0').firstChild.firstChild.style.width='100%';document.uploadStatus0 = null; alert('bar')}, asynchronous:true}.extend({decay:7,freqency:6})); return true\" target=\"awindow\">",268 "<form action=\"http://www.example.com\" enctype=\"multipart/form-data\" method=\"post\" onsubmit=\"if (this.action.indexOf('upload_id') < 0){ this.action += '?upload_id=5'; }this.target = 'awindow';$('UploadStatus0').innerHTML='Upload starting...'; $('UploadProgressBar0').firstChild.firstChild.style.width='0%'; alert('foo'); if (document.uploadStatus0) { document.uploadStatus0.stop(); }document.uploadStatus0 = new Ajax.PeriodicalUpdater('UploadStatus0','http://www.example.com', {onComplete:function(request){$('UploadStatus0').innerHTML='A message';$('UploadProgressBar0').firstChild.firstChild.style.width='100%';document.uploadStatus0 = null; alert('bar')}, evalScripts:true, asynchronous:true}.extend({decay:7,freqency:6})); return true\" target=\"awindow\">", 269 269 form_tag_with_upload_progress({:upload_id => 5}, {:begin => "alert('foo')", :finish => "alert('bar')", :frequency => 6, :decay => 7, :target => 'awindow'}) 270 270 )