remotipart 0.3.4 → 0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +35 -39
- data/Rakefile +3 -2
- data/VERSION_COMPATIBILITY.rdoc +38 -0
- data/lib/generators/remotipart/install/install_generator.rb +21 -0
- data/lib/remotipart.rb +3 -15
- data/lib/remotipart/rails.rb +10 -0
- data/lib/remotipart/rails/engine.rb +17 -0
- data/lib/remotipart/rails/railtie.rb +36 -0
- data/lib/remotipart/rails/version.rb +6 -0
- data/lib/remotipart/request_helper.rb +8 -0
- data/lib/remotipart/view_helper.rb +13 -0
- data/remotipart.gemspec +13 -7
- data/vendor/assets/javascripts/jquery.form.js +909 -0
- data/{lib/generators/templates → vendor/assets/javascripts}/jquery.remotipart.js +0 -0
- metadata +14 -8
- data/VERSION +0 -1
- data/lib/generators/remotipart_generator.rb +0 -14
data/README.rdoc
CHANGED
@@ -3,54 +3,49 @@
|
|
3
3
|
Remotipart is a Ruby on Rails gem enabling remote multipart forms (AJAX style file uploads) with jQuery.
|
4
4
|
This gem augments the native Rails jQuery remote form function enabling asynchronous file uploads with little to no modification to your application.
|
5
5
|
|
6
|
+
{View Homepage and Demos}[http://www.alfajango.com/blog/remotipart-rails-gem/]
|
7
|
+
|
6
8
|
== Dependencies
|
7
9
|
|
8
10
|
* {Rails 3}[http://github.com/rails/rails]
|
9
|
-
* {jQuery}[http://jquery.com]
|
10
|
-
* {The jquery-rails gem}[http://rubygems.org/gems/jquery-rails], which really just installs {the Rails jQuery driver (jquery-ujs)}[https://github.com/rails/jquery-ujs]
|
11
|
+
* {The jquery-rails gem}[http://rubygems.org/gems/jquery-rails], which really just installs {jQuery}[http://jquery.com] and the {Rails jQuery driver (jquery-ujs)}[https://github.com/rails/jquery-ujs]
|
11
12
|
|
12
|
-
|
13
|
-
* {The jQuery Form plugin}[http://jquery.malsup.com/form/]
|
13
|
+
== Installation
|
14
14
|
|
15
|
-
|
15
|
+
<b>If you're using an old version of the jquery-rails gem,
|
16
|
+
make sure you have a supported jquery-ujs (rails.js or jquery_ujs.js) version from VERSION_COMPATIBILITY.</b>
|
16
17
|
|
17
|
-
|
18
|
-
|
19
|
-
+---------------------------------------------------------------------+
|
20
|
-
| <= 0.3.1 | <= 498b35e24cdb14f2 |
|
21
|
-
| 0.3.2 | > 498b35e24cdb14f2, <= 7f2acc1811f62877 |
|
22
|
-
| 0.3.3 | > 7f2acc1811f62877, <= d2abd6f9df4e4a42 |
|
23
|
-
| 0.3.4 | > d2abd6f9df4e4a42 (i.e. latest) |
|
24
|
-
+---------------------------------------------------------------------+
|
18
|
+
[1.]
|
19
|
+
Install the Remotipart gem
|
25
20
|
|
26
|
-
*
|
27
|
-
* {498b35e24cdb14f2}[https://github.com/rails/jquery-ujs/raw/498b35e24cdb14f2d94486e8a1f4a1f661091426/src/rails.js]
|
28
|
-
* {7f2acc1811f62877}[https://github.com/rails/jquery-ujs/raw/7f2acc1811f62877611e16451530728b5e13dbe7/src/rails.js]
|
29
|
-
* {d2abd6f9df4e4a42}[https://github.com/rails/jquery-ujs/raw/d2abd6f9df4e4a426c17c218b7d5e05004c768d0/src/rails.js]
|
30
|
-
* {latest}[https://github.com/rails/jquery-ujs/raw/master/src/rails.js]
|
21
|
+
* Add this line to your GEMFILE (add the correct version from previous section if needed)
|
31
22
|
|
32
|
-
|
23
|
+
gem 'remotipart', '~> 0.4'
|
33
24
|
|
34
|
-
|
25
|
+
* And run
|
35
26
|
|
36
|
-
|
27
|
+
bundle install
|
37
28
|
|
38
|
-
|
29
|
+
=== Rails 3.0
|
39
30
|
|
40
|
-
|
31
|
+
[2.]
|
32
|
+
Run the Remotipart install generator to add jquery.form.js and jquery.remotipart.js to public/javascripts/
|
41
33
|
|
42
|
-
|
34
|
+
rails g remotipart:install
|
43
35
|
|
44
|
-
|
36
|
+
[3.]
|
37
|
+
The necessary js files will be added to your app's javascript <tt>:defaults</tt>, so make sure the following is in your application layout:
|
45
38
|
|
46
|
-
|
39
|
+
<%= javascript_include_tag :defaults %>
|
47
40
|
|
48
|
-
|
41
|
+
=== Rails 3.1
|
49
42
|
|
50
|
-
|
43
|
+
[2.]
|
44
|
+
The necessary js files will automatically be added to the asset pipeline, so add the following to app/assets/javascripts/application.js (right after <tt>//= require jquery_ujs</tt>):
|
45
|
+
|
46
|
+
//= require jquery.form
|
47
|
+
//= require jquery.remotipart
|
51
48
|
|
52
|
-
<%= javascript_include_tag 'jquery-1.4.4.min', 'rails', 'jquery.form', 'jquery.remotipart' %>
|
53
|
-
|
54
49
|
== Usage
|
55
50
|
|
56
51
|
* For multipart / forms with file inputs, set your form_for to remote as you would for a normal ajax form:
|
@@ -61,18 +56,14 @@ This gem augments the native Rails jQuery remote form function enabling asynchro
|
|
61
56
|
<%= remotipart_response do %> All Javascript response code goes here <% end %>
|
62
57
|
* The options available to the text_area_tag[http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-text_area_tag] can be passed to remotipart_response for further control over the response. For instance:
|
63
58
|
<%= remotipart_response({:escape => false}) do %> All Javascript response code goes here <% end %>
|
64
|
-
*
|
59
|
+
* Anything inside the +remotipart_response+ block will be rendered normally, unless the request actually did come from a remotipart-submitted form. So <tt>js.erb</tt> responses can be shared between remotipart and non-remotipart forms.
|
65
60
|
<%= remotipart_response do %>
|
66
|
-
|
67
|
-
// do one thing
|
68
|
-
else
|
69
|
-
// do another
|
70
|
-
end
|
61
|
+
// do stuff here
|
71
62
|
<% end %>
|
72
63
|
* If you need to determine if a particular request was made via a remotipart-enabled form...
|
73
64
|
* from your Rails controller or view:
|
74
65
|
|
75
|
-
if
|
66
|
+
if remotipart_submitted?
|
76
67
|
* from your javascript:
|
77
68
|
|
78
69
|
$(form).bind("ajax:success", function(){
|
@@ -103,8 +94,13 @@ sample_controller.rb
|
|
103
94
|
|
104
95
|
create.js.erb
|
105
96
|
<%= remotipart_response do %>
|
106
|
-
//Display a Javascript alert
|
97
|
+
// Display a Javascript alert
|
107
98
|
alert('success!');
|
99
|
+
<% if remotipart_submitted? %>
|
100
|
+
alert('submitted via remotipart')
|
101
|
+
<% else %>
|
102
|
+
alert('submitted via native jquery-ujs')
|
103
|
+
<% end %>
|
108
104
|
<% end %>
|
109
105
|
|
110
106
|
== Note on Patches/Pull Requests
|
@@ -121,4 +117,4 @@ create.js.erb
|
|
121
117
|
|
122
118
|
== Copyright
|
123
119
|
|
124
|
-
Copyright (c) 2011 Greg Leppert. See LICENSE for details.
|
120
|
+
Copyright (c) 2011 Greg Leppert, Steve Schwartz. See LICENSE for details.
|
data/Rakefile
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'rake'
|
3
|
+
require File.expand_path('../lib/remotipart/rails/version', __FILE__)
|
3
4
|
|
4
5
|
begin
|
5
6
|
require 'jeweler'
|
@@ -8,12 +9,12 @@ begin
|
|
8
9
|
gem.summary = %Q{Remotipart is a Ruby on Rails gem enabling remote multipart forms (AJAX style file uploads) with jQuery.}
|
9
10
|
gem.description = %Q{Remotipart is a Ruby on Rails gem enabling remote multipart forms (AJAX style file uploads) with jQuery.
|
10
11
|
This gem augments the native Rails jQuery remote form function enabling asynchronous file uploads with little to no modification to your application.
|
11
|
-
It requires jQuery (http://jquery.com), the Rails jQuery driver (http://github.com/rails/jquery-ujs), and the jQuery Form plugin (http://jquery.malsup.com/form/).
|
12
12
|
}
|
13
13
|
gem.email = %w{greg@formasfunction.com steve@alfajango.com}
|
14
14
|
gem.homepage = "http://github.com/formasfunction/remotipart"
|
15
15
|
gem.authors = ["Greg Leppert", "Steve Schwartz"]
|
16
16
|
gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
|
17
|
+
gem.version = Remotipart::Rails::VERSION
|
17
18
|
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
18
19
|
end
|
19
20
|
Jeweler::GemcutterTasks.new
|
@@ -47,7 +48,7 @@ task :default => :test
|
|
47
48
|
|
48
49
|
require 'rake/rdoctask'
|
49
50
|
Rake::RDocTask.new do |rdoc|
|
50
|
-
version =
|
51
|
+
version = Remotipart::Rails::VERSION
|
51
52
|
|
52
53
|
rdoc.rdoc_dir = 'rdoc'
|
53
54
|
rdoc.title = "remotipart #{version}"
|
@@ -0,0 +1,38 @@
|
|
1
|
+
= Version Compatibility
|
2
|
+
|
3
|
+
+---------------------------------------------------------------------+
|
4
|
+
| Version of remotipart.js | is compatible with versions of rails.js* |
|
5
|
+
+---------------------------------------------------------------------+
|
6
|
+
| <= 0.3.1 | <= 498b35e24cdb14f2 |
|
7
|
+
| 0.3.2 | > 498b35e24cdb14f2, <= 7f2acc1811f62877 |
|
8
|
+
| 0.3.3 | > 7f2acc1811f62877, <= d2abd6f9df4e4a42 |
|
9
|
+
| >= 0.3.4 | > d2abd6f9df4e4a42 | (current version of rails.js)
|
10
|
+
+---------------------------------------------------------------------+
|
11
|
+
|
12
|
+
*rails.js versions <em>(For a full list of Rails.js]versions and diffs, see {Rails jQuery UJS Changelog}[https://github.com/rails/jquery-ujs/blob/master/CHANGELOG.md])</em>:
|
13
|
+
* {498b35e24cdb14f2}[https://github.com/rails/jquery-ujs/raw/498b35e24cdb14f2d94486e8a1f4a1f661091426/src/rails.js]
|
14
|
+
* {7f2acc1811f62877}[https://github.com/rails/jquery-ujs/raw/7f2acc1811f62877611e16451530728b5e13dbe7/src/rails.js]
|
15
|
+
* {d2abd6f9df4e4a42}[https://github.com/rails/jquery-ujs/raw/d2abd6f9df4e4a426c17c218b7d5e05004c768d0/src/rails.js]
|
16
|
+
* {current}[https://github.com/rails/jquery-ujs/raw/master/src/rails.js]
|
17
|
+
|
18
|
+
== Installation for Remotipart v0.3.x
|
19
|
+
|
20
|
+
<b>Prerequisite: Make sure you have a supported jquery-ujs (rails.js) version from the previous section.</b>
|
21
|
+
|
22
|
+
1. Install the Remotipart gem
|
23
|
+
|
24
|
+
* Add this line to your GEMFILE (add the correct version from previous section if needed)
|
25
|
+
|
26
|
+
gem 'remotipart'
|
27
|
+
|
28
|
+
* And run
|
29
|
+
|
30
|
+
bundle install
|
31
|
+
|
32
|
+
2. Run the Remotipart generator to add jquery.remotipart.js to public/javascripts/
|
33
|
+
|
34
|
+
rails g remotipart
|
35
|
+
|
36
|
+
3. Add the Javascript files for jQuery, the Rails jQuery driver, jQuery Form plugin, and Remotipart to your template, making sure to include jquery.remotipart.js *after* jQuery and the Rails jQuery driver
|
37
|
+
|
38
|
+
<%= javascript_include_tag 'jquery-1.4.4.min', 'rails', 'jquery.form', 'jquery.remotipart' %>
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'rails'
|
2
|
+
|
3
|
+
module Remotipart
|
4
|
+
module Generators
|
5
|
+
class InstallGenerator < ::Rails::Generators::Base
|
6
|
+
|
7
|
+
desc "This generator installs Form.js #{Remotipart::Rails::FORMJS_VERSION} and Remotipart #{Remotipart::Rails::VERSION}"
|
8
|
+
source_root File.expand_path('../../../../../vendor/assets/javascripts', __FILE__)
|
9
|
+
|
10
|
+
def install_formjs
|
11
|
+
say_status "copying", "Form.js #{Remotipart::Rails::FORMJS_VERSION}", :green
|
12
|
+
copy_file "jquery.form.js", "public/javascripts/jquery.form.js"
|
13
|
+
end
|
14
|
+
|
15
|
+
def install_remotipart
|
16
|
+
say_status "copying", "Remotipart.js #{Remotipart::Rails::VERSION}", :green
|
17
|
+
copy_file 'jquery.remotipart.js', 'public/javascripts/jquery.remotipart.js'
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/lib/remotipart.rb
CHANGED
@@ -1,15 +1,3 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
if params[:remotipart_submitted]
|
5
|
-
response.content_type = Mime::HTML
|
6
|
-
text_area_tag('remotipart_response', String.new(content), options)
|
7
|
-
else
|
8
|
-
content
|
9
|
-
end
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
class ActionView::Base
|
14
|
-
include Remotipart
|
15
|
-
end
|
1
|
+
require 'remotipart/view_helper'
|
2
|
+
require 'remotipart/request_helper'
|
3
|
+
require 'remotipart/rails' if defined?(Rails)
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# Configure Rails 3.1
|
2
|
+
module Remotipart
|
3
|
+
module Rails
|
4
|
+
|
5
|
+
class Engine < ::Rails::Engine
|
6
|
+
initializer "remotipart.view_helper" do
|
7
|
+
ActionView::Base.send :include, RequestHelper
|
8
|
+
ActionView::Base.send :include, ViewHelper
|
9
|
+
end
|
10
|
+
|
11
|
+
initializer "remotipart.controller_helper" do
|
12
|
+
ActionController::Base.send :include, RequestHelper
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# Configure Rails 3.0 to use form.js and remotipart
|
2
|
+
module Remotipart
|
3
|
+
module Rails
|
4
|
+
|
5
|
+
class Railtie < ::Rails::Railtie
|
6
|
+
config.before_configuration do
|
7
|
+
# Files to be added to :defaults
|
8
|
+
FILES = ['jquery.form', 'jquery.remotipart']
|
9
|
+
|
10
|
+
# Figure out where rails.js (aka jquery_ujs.js if install by jquery-rails gem) is
|
11
|
+
# in the :defaults array
|
12
|
+
position = config.action_view.javascript_expansions[:defaults].index('rails') ||
|
13
|
+
config.action_view.javascript_expansions[:defaults].index('jquery_ujs')
|
14
|
+
|
15
|
+
# Merge form.js and then remotipart into :defaults array right after rails.js
|
16
|
+
if position && position > 0
|
17
|
+
config.action_view.javascript_expansions[:defaults].insert(position + 1, *FILES)
|
18
|
+
# If rails.js couldn't be found, it may have a custom filename, or not be in the :defaults.
|
19
|
+
# In that case, just try adding to the end of the :defaults array.
|
20
|
+
else
|
21
|
+
config.action_view.javascript_expansions[:defaults].push(*FILES)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
initializer "remotipart.view_helper" do
|
26
|
+
ActionView::Base.send :include, RequestHelper
|
27
|
+
ActionView::Base.send :include, ViewHelper
|
28
|
+
end
|
29
|
+
|
30
|
+
initializer "remotipart.controller_helper" do
|
31
|
+
ActionController::Base.send :include, RequestHelper
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module Remotipart
|
2
|
+
module ViewHelper
|
3
|
+
def remotipart_response(options = {}, &block)
|
4
|
+
content = with_output_buffer(&block)
|
5
|
+
if remotipart_submitted?
|
6
|
+
response.content_type = Mime::HTML
|
7
|
+
text_area_tag('remotipart_response', String.new(content), options)
|
8
|
+
else
|
9
|
+
content
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
data/remotipart.gemspec
CHANGED
@@ -5,14 +5,13 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{remotipart}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.4"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Greg Leppert", "Steve Schwartz"]
|
12
|
-
s.date = %q{2011-
|
12
|
+
s.date = %q{2011-07-12}
|
13
13
|
s.description = %q{Remotipart is a Ruby on Rails gem enabling remote multipart forms (AJAX style file uploads) with jQuery.
|
14
14
|
This gem augments the native Rails jQuery remote form function enabling asynchronous file uploads with little to no modification to your application.
|
15
|
-
It requires jQuery (http://jquery.com), the Rails jQuery driver (http://github.com/rails/jquery-ujs), and the jQuery Form plugin (http://jquery.malsup.com/form/).
|
16
15
|
}
|
17
16
|
s.email = ["greg@formasfunction.com", "steve@alfajango.com"]
|
18
17
|
s.extra_rdoc_files = [
|
@@ -26,13 +25,20 @@ Gem::Specification.new do |s|
|
|
26
25
|
"LICENSE",
|
27
26
|
"README.rdoc",
|
28
27
|
"Rakefile",
|
29
|
-
"
|
30
|
-
"lib/generators/
|
31
|
-
"lib/generators/templates/jquery.remotipart.js",
|
28
|
+
"VERSION_COMPATIBILITY.rdoc",
|
29
|
+
"lib/generators/remotipart/install/install_generator.rb",
|
32
30
|
"lib/remotipart.rb",
|
31
|
+
"lib/remotipart/rails.rb",
|
32
|
+
"lib/remotipart/rails/engine.rb",
|
33
|
+
"lib/remotipart/rails/railtie.rb",
|
34
|
+
"lib/remotipart/rails/version.rb",
|
35
|
+
"lib/remotipart/request_helper.rb",
|
36
|
+
"lib/remotipart/view_helper.rb",
|
33
37
|
"remotipart.gemspec",
|
34
38
|
"test/helper.rb",
|
35
|
-
"test/test_remotipart.rb"
|
39
|
+
"test/test_remotipart.rb",
|
40
|
+
"vendor/assets/javascripts/jquery.form.js",
|
41
|
+
"vendor/assets/javascripts/jquery.remotipart.js"
|
36
42
|
]
|
37
43
|
s.homepage = %q{http://github.com/formasfunction/remotipart}
|
38
44
|
s.rdoc_options = ["--charset=UTF-8"]
|
@@ -0,0 +1,909 @@
|
|
1
|
+
/*!
|
2
|
+
* jQuery Form Plugin
|
3
|
+
* version: 2.82 (15-JUN-2011)
|
4
|
+
* @requires jQuery v1.3.2 or later
|
5
|
+
*
|
6
|
+
* Examples and documentation at: http://malsup.com/jquery/form/
|
7
|
+
* Dual licensed under the MIT and GPL licenses:
|
8
|
+
* http://www.opensource.org/licenses/mit-license.php
|
9
|
+
* http://www.gnu.org/licenses/gpl.html
|
10
|
+
*/
|
11
|
+
;(function($) {
|
12
|
+
|
13
|
+
/*
|
14
|
+
Usage Note:
|
15
|
+
-----------
|
16
|
+
Do not use both ajaxSubmit and ajaxForm on the same form. These
|
17
|
+
functions are intended to be exclusive. Use ajaxSubmit if you want
|
18
|
+
to bind your own submit handler to the form. For example,
|
19
|
+
|
20
|
+
$(document).ready(function() {
|
21
|
+
$('#myForm').bind('submit', function(e) {
|
22
|
+
e.preventDefault(); // <-- important
|
23
|
+
$(this).ajaxSubmit({
|
24
|
+
target: '#output'
|
25
|
+
});
|
26
|
+
});
|
27
|
+
});
|
28
|
+
|
29
|
+
Use ajaxForm when you want the plugin to manage all the event binding
|
30
|
+
for you. For example,
|
31
|
+
|
32
|
+
$(document).ready(function() {
|
33
|
+
$('#myForm').ajaxForm({
|
34
|
+
target: '#output'
|
35
|
+
});
|
36
|
+
});
|
37
|
+
|
38
|
+
When using ajaxForm, the ajaxSubmit function will be invoked for you
|
39
|
+
at the appropriate time.
|
40
|
+
*/
|
41
|
+
|
42
|
+
/**
|
43
|
+
* ajaxSubmit() provides a mechanism for immediately submitting
|
44
|
+
* an HTML form using AJAX.
|
45
|
+
*/
|
46
|
+
$.fn.ajaxSubmit = function(options) {
|
47
|
+
// fast fail if nothing selected (http://dev.jquery.com/ticket/2752)
|
48
|
+
if (!this.length) {
|
49
|
+
log('ajaxSubmit: skipping submit process - no element selected');
|
50
|
+
return this;
|
51
|
+
}
|
52
|
+
|
53
|
+
var method, action, url, $form = this;;
|
54
|
+
|
55
|
+
if (typeof options == 'function') {
|
56
|
+
options = { success: options };
|
57
|
+
}
|
58
|
+
|
59
|
+
method = this.attr('method');
|
60
|
+
action = this.attr('action');
|
61
|
+
url = (typeof action === 'string') ? $.trim(action) : '';
|
62
|
+
url = url || window.location.href || '';
|
63
|
+
if (url) {
|
64
|
+
// clean url (don't include hash vaue)
|
65
|
+
url = (url.match(/^([^#]+)/)||[])[1];
|
66
|
+
}
|
67
|
+
|
68
|
+
options = $.extend(true, {
|
69
|
+
url: url,
|
70
|
+
success: $.ajaxSettings.success,
|
71
|
+
type: method || 'GET',
|
72
|
+
iframeSrc: /^https/i.test(window.location.href || '') ? 'javascript:false' : 'about:blank'
|
73
|
+
}, options);
|
74
|
+
|
75
|
+
// hook for manipulating the form data before it is extracted;
|
76
|
+
// convenient for use with rich editors like tinyMCE or FCKEditor
|
77
|
+
var veto = {};
|
78
|
+
this.trigger('form-pre-serialize', [this, options, veto]);
|
79
|
+
if (veto.veto) {
|
80
|
+
log('ajaxSubmit: submit vetoed via form-pre-serialize trigger');
|
81
|
+
return this;
|
82
|
+
}
|
83
|
+
|
84
|
+
// provide opportunity to alter form data before it is serialized
|
85
|
+
if (options.beforeSerialize && options.beforeSerialize(this, options) === false) {
|
86
|
+
log('ajaxSubmit: submit aborted via beforeSerialize callback');
|
87
|
+
return this;
|
88
|
+
}
|
89
|
+
|
90
|
+
var n,v,a = this.formToArray(options.semantic);
|
91
|
+
if (options.data) {
|
92
|
+
options.extraData = options.data;
|
93
|
+
for (n in options.data) {
|
94
|
+
if(options.data[n] instanceof Array) {
|
95
|
+
for (var k in options.data[n]) {
|
96
|
+
a.push( { name: n, value: options.data[n][k] } );
|
97
|
+
}
|
98
|
+
}
|
99
|
+
else {
|
100
|
+
v = options.data[n];
|
101
|
+
v = $.isFunction(v) ? v() : v; // if value is fn, invoke it
|
102
|
+
a.push( { name: n, value: v } );
|
103
|
+
}
|
104
|
+
}
|
105
|
+
}
|
106
|
+
|
107
|
+
// give pre-submit callback an opportunity to abort the submit
|
108
|
+
if (options.beforeSubmit && options.beforeSubmit(a, this, options) === false) {
|
109
|
+
log('ajaxSubmit: submit aborted via beforeSubmit callback');
|
110
|
+
return this;
|
111
|
+
}
|
112
|
+
|
113
|
+
// fire vetoable 'validate' event
|
114
|
+
this.trigger('form-submit-validate', [a, this, options, veto]);
|
115
|
+
if (veto.veto) {
|
116
|
+
log('ajaxSubmit: submit vetoed via form-submit-validate trigger');
|
117
|
+
return this;
|
118
|
+
}
|
119
|
+
|
120
|
+
var q = $.param(a);
|
121
|
+
|
122
|
+
if (options.type.toUpperCase() == 'GET') {
|
123
|
+
options.url += (options.url.indexOf('?') >= 0 ? '&' : '?') + q;
|
124
|
+
options.data = null; // data is null for 'get'
|
125
|
+
}
|
126
|
+
else {
|
127
|
+
options.data = q; // data is the query string for 'post'
|
128
|
+
}
|
129
|
+
|
130
|
+
var callbacks = [];
|
131
|
+
if (options.resetForm) {
|
132
|
+
callbacks.push(function() { $form.resetForm(); });
|
133
|
+
}
|
134
|
+
if (options.clearForm) {
|
135
|
+
callbacks.push(function() { $form.clearForm(); });
|
136
|
+
}
|
137
|
+
|
138
|
+
// perform a load on the target only if dataType is not provided
|
139
|
+
if (!options.dataType && options.target) {
|
140
|
+
var oldSuccess = options.success || function(){};
|
141
|
+
callbacks.push(function(data) {
|
142
|
+
var fn = options.replaceTarget ? 'replaceWith' : 'html';
|
143
|
+
$(options.target)[fn](data).each(oldSuccess, arguments);
|
144
|
+
});
|
145
|
+
}
|
146
|
+
else if (options.success) {
|
147
|
+
callbacks.push(options.success);
|
148
|
+
}
|
149
|
+
|
150
|
+
options.success = function(data, status, xhr) { // jQuery 1.4+ passes xhr as 3rd arg
|
151
|
+
var context = options.context || options; // jQuery 1.4+ supports scope context
|
152
|
+
for (var i=0, max=callbacks.length; i < max; i++) {
|
153
|
+
callbacks[i].apply(context, [data, status, xhr || $form, $form]);
|
154
|
+
}
|
155
|
+
};
|
156
|
+
|
157
|
+
// are there files to upload?
|
158
|
+
var fileInputs = $('input:file', this).length > 0;
|
159
|
+
var mp = 'multipart/form-data';
|
160
|
+
var multipart = ($form.attr('enctype') == mp || $form.attr('encoding') == mp);
|
161
|
+
|
162
|
+
// options.iframe allows user to force iframe mode
|
163
|
+
// 06-NOV-09: now defaulting to iframe mode if file input is detected
|
164
|
+
if (options.iframe !== false && (fileInputs || options.iframe || multipart)) {
|
165
|
+
// hack to fix Safari hang (thanks to Tim Molendijk for this)
|
166
|
+
// see: http://groups.google.com/group/jquery-dev/browse_thread/thread/36395b7ab510dd5d
|
167
|
+
if (options.closeKeepAlive) {
|
168
|
+
$.get(options.closeKeepAlive, function() { fileUpload(a); });
|
169
|
+
}
|
170
|
+
else {
|
171
|
+
fileUpload(a);
|
172
|
+
}
|
173
|
+
}
|
174
|
+
else {
|
175
|
+
// IE7 massage (see issue 57)
|
176
|
+
if ($.browser.msie && method == 'get') {
|
177
|
+
var ieMeth = $form[0].getAttribute('method');
|
178
|
+
if (typeof ieMeth === 'string')
|
179
|
+
options.type = ieMeth;
|
180
|
+
}
|
181
|
+
$.ajax(options);
|
182
|
+
}
|
183
|
+
|
184
|
+
// fire 'notify' event
|
185
|
+
this.trigger('form-submit-notify', [this, options]);
|
186
|
+
return this;
|
187
|
+
|
188
|
+
|
189
|
+
// private function for handling file uploads (hat tip to YAHOO!)
|
190
|
+
function fileUpload(a) {
|
191
|
+
var form = $form[0], i, s, g, id, $io, io, xhr, sub, n, timedOut, timeoutHandle;
|
192
|
+
|
193
|
+
if (a) {
|
194
|
+
// ensure that every serialized input is still enabled
|
195
|
+
for (i=0; i < a.length; i++) {
|
196
|
+
$(form[a[i].name]).attr('disabled', false);
|
197
|
+
}
|
198
|
+
}
|
199
|
+
|
200
|
+
if ($(':input[name=submit],:input[id=submit]', form).length) {
|
201
|
+
// if there is an input with a name or id of 'submit' then we won't be
|
202
|
+
// able to invoke the submit fn on the form (at least not x-browser)
|
203
|
+
alert('Error: Form elements must not have name or id of "submit".');
|
204
|
+
return;
|
205
|
+
}
|
206
|
+
|
207
|
+
s = $.extend(true, {}, $.ajaxSettings, options);
|
208
|
+
s.context = s.context || s;
|
209
|
+
id = 'jqFormIO' + (new Date().getTime());
|
210
|
+
if (s.iframeTarget) {
|
211
|
+
$io = $(s.iframeTarget);
|
212
|
+
n = $io.attr('name');
|
213
|
+
if (n == null)
|
214
|
+
$io.attr('name', id);
|
215
|
+
else
|
216
|
+
id = n;
|
217
|
+
}
|
218
|
+
else {
|
219
|
+
$io = $('<iframe name="' + id + '" src="'+ s.iframeSrc +'" />');
|
220
|
+
$io.css({ position: 'absolute', top: '-1000px', left: '-1000px' });
|
221
|
+
}
|
222
|
+
io = $io[0];
|
223
|
+
|
224
|
+
|
225
|
+
xhr = { // mock object
|
226
|
+
aborted: 0,
|
227
|
+
responseText: null,
|
228
|
+
responseXML: null,
|
229
|
+
status: 0,
|
230
|
+
statusText: 'n/a',
|
231
|
+
getAllResponseHeaders: function() {},
|
232
|
+
getResponseHeader: function() {},
|
233
|
+
setRequestHeader: function() {},
|
234
|
+
abort: function(status) {
|
235
|
+
var e = (status === 'timeout' ? 'timeout' : 'aborted');
|
236
|
+
log('aborting upload... ' + e);
|
237
|
+
this.aborted = 1;
|
238
|
+
$io.attr('src', s.iframeSrc); // abort op in progress
|
239
|
+
xhr.error = e;
|
240
|
+
s.error && s.error.call(s.context, xhr, e, status);
|
241
|
+
g && $.event.trigger("ajaxError", [xhr, s, e]);
|
242
|
+
s.complete && s.complete.call(s.context, xhr, e);
|
243
|
+
}
|
244
|
+
};
|
245
|
+
|
246
|
+
g = s.global;
|
247
|
+
// trigger ajax global events so that activity/block indicators work like normal
|
248
|
+
if (g && ! $.active++) {
|
249
|
+
$.event.trigger("ajaxStart");
|
250
|
+
}
|
251
|
+
if (g) {
|
252
|
+
$.event.trigger("ajaxSend", [xhr, s]);
|
253
|
+
}
|
254
|
+
|
255
|
+
if (s.beforeSend && s.beforeSend.call(s.context, xhr, s) === false) {
|
256
|
+
if (s.global) {
|
257
|
+
$.active--;
|
258
|
+
}
|
259
|
+
return;
|
260
|
+
}
|
261
|
+
if (xhr.aborted) {
|
262
|
+
return;
|
263
|
+
}
|
264
|
+
|
265
|
+
// add submitting element to data if we know it
|
266
|
+
sub = form.clk;
|
267
|
+
if (sub) {
|
268
|
+
n = sub.name;
|
269
|
+
if (n && !sub.disabled) {
|
270
|
+
s.extraData = s.extraData || {};
|
271
|
+
s.extraData[n] = sub.value;
|
272
|
+
if (sub.type == "image") {
|
273
|
+
s.extraData[n+'.x'] = form.clk_x;
|
274
|
+
s.extraData[n+'.y'] = form.clk_y;
|
275
|
+
}
|
276
|
+
}
|
277
|
+
}
|
278
|
+
|
279
|
+
var CLIENT_TIMEOUT_ABORT = 1;
|
280
|
+
var SERVER_ABORT = 2;
|
281
|
+
|
282
|
+
function getDoc(frame) {
|
283
|
+
var doc = frame.contentWindow ? frame.contentWindow.document : frame.contentDocument ? frame.contentDocument : frame.document;
|
284
|
+
return doc;
|
285
|
+
}
|
286
|
+
|
287
|
+
// take a breath so that pending repaints get some cpu time before the upload starts
|
288
|
+
function doSubmit() {
|
289
|
+
// make sure form attrs are set
|
290
|
+
var t = $form.attr('target'), a = $form.attr('action');
|
291
|
+
|
292
|
+
// update form attrs in IE friendly way
|
293
|
+
form.setAttribute('target',id);
|
294
|
+
if (!method) {
|
295
|
+
form.setAttribute('method', 'POST');
|
296
|
+
}
|
297
|
+
if (a != s.url) {
|
298
|
+
form.setAttribute('action', s.url);
|
299
|
+
}
|
300
|
+
|
301
|
+
// ie borks in some cases when setting encoding
|
302
|
+
if (! s.skipEncodingOverride && (!method || /post/i.test(method))) {
|
303
|
+
$form.attr({
|
304
|
+
encoding: 'multipart/form-data',
|
305
|
+
enctype: 'multipart/form-data'
|
306
|
+
});
|
307
|
+
}
|
308
|
+
|
309
|
+
// support timout
|
310
|
+
if (s.timeout) {
|
311
|
+
timeoutHandle = setTimeout(function() { timedOut = true; cb(CLIENT_TIMEOUT_ABORT); }, s.timeout);
|
312
|
+
}
|
313
|
+
|
314
|
+
// look for server aborts
|
315
|
+
function checkState() {
|
316
|
+
try {
|
317
|
+
var state = getDoc(io).readyState;
|
318
|
+
log('state = ' + state);
|
319
|
+
if (state.toLowerCase() == 'uninitialized')
|
320
|
+
setTimeout(checkState,50);
|
321
|
+
}
|
322
|
+
catch(e) {
|
323
|
+
log('Server abort: ' , e, ' (', e.name, ')');
|
324
|
+
cb(SERVER_ABORT);
|
325
|
+
timeoutHandle && clearTimeout(timeoutHandle);
|
326
|
+
timeoutHandle = undefined;
|
327
|
+
}
|
328
|
+
}
|
329
|
+
|
330
|
+
// add "extra" data to form if provided in options
|
331
|
+
var extraInputs = [];
|
332
|
+
try {
|
333
|
+
if (s.extraData) {
|
334
|
+
for (var n in s.extraData) {
|
335
|
+
extraInputs.push(
|
336
|
+
$('<input type="hidden" name="'+n+'" />').attr('value',s.extraData[n])
|
337
|
+
.appendTo(form)[0]);
|
338
|
+
}
|
339
|
+
}
|
340
|
+
|
341
|
+
if (!s.iframeTarget) {
|
342
|
+
// add iframe to doc and submit the form
|
343
|
+
$io.appendTo('body');
|
344
|
+
io.attachEvent ? io.attachEvent('onload', cb) : io.addEventListener('load', cb, false);
|
345
|
+
}
|
346
|
+
setTimeout(checkState,15);
|
347
|
+
form.submit();
|
348
|
+
}
|
349
|
+
finally {
|
350
|
+
// reset attrs and remove "extra" input elements
|
351
|
+
form.setAttribute('action',a);
|
352
|
+
if(t) {
|
353
|
+
form.setAttribute('target', t);
|
354
|
+
} else {
|
355
|
+
$form.removeAttr('target');
|
356
|
+
}
|
357
|
+
$(extraInputs).remove();
|
358
|
+
}
|
359
|
+
}
|
360
|
+
|
361
|
+
if (s.forceSync) {
|
362
|
+
doSubmit();
|
363
|
+
}
|
364
|
+
else {
|
365
|
+
setTimeout(doSubmit, 10); // this lets dom updates render
|
366
|
+
}
|
367
|
+
|
368
|
+
var data, doc, domCheckCount = 50, callbackProcessed;
|
369
|
+
|
370
|
+
function cb(e) {
|
371
|
+
if (xhr.aborted || callbackProcessed) {
|
372
|
+
return;
|
373
|
+
}
|
374
|
+
try {
|
375
|
+
doc = getDoc(io);
|
376
|
+
}
|
377
|
+
catch(ex) {
|
378
|
+
log('cannot access response document: ', ex);
|
379
|
+
e = SERVER_ABORT;
|
380
|
+
}
|
381
|
+
if (e === CLIENT_TIMEOUT_ABORT && xhr) {
|
382
|
+
xhr.abort('timeout');
|
383
|
+
return;
|
384
|
+
}
|
385
|
+
else if (e == SERVER_ABORT && xhr) {
|
386
|
+
xhr.abort('server abort');
|
387
|
+
return;
|
388
|
+
}
|
389
|
+
|
390
|
+
if (!doc || doc.location.href == s.iframeSrc) {
|
391
|
+
// response not received yet
|
392
|
+
if (!timedOut)
|
393
|
+
return;
|
394
|
+
}
|
395
|
+
io.detachEvent ? io.detachEvent('onload', cb) : io.removeEventListener('load', cb, false);
|
396
|
+
|
397
|
+
var status = 'success', errMsg;
|
398
|
+
try {
|
399
|
+
if (timedOut) {
|
400
|
+
throw 'timeout';
|
401
|
+
}
|
402
|
+
|
403
|
+
var isXml = s.dataType == 'xml' || doc.XMLDocument || $.isXMLDoc(doc);
|
404
|
+
log('isXml='+isXml);
|
405
|
+
if (!isXml && window.opera && (doc.body == null || doc.body.innerHTML == '')) {
|
406
|
+
if (--domCheckCount) {
|
407
|
+
// in some browsers (Opera) the iframe DOM is not always traversable when
|
408
|
+
// the onload callback fires, so we loop a bit to accommodate
|
409
|
+
log('requeing onLoad callback, DOM not available');
|
410
|
+
setTimeout(cb, 250);
|
411
|
+
return;
|
412
|
+
}
|
413
|
+
// let this fall through because server response could be an empty document
|
414
|
+
//log('Could not access iframe DOM after mutiple tries.');
|
415
|
+
//throw 'DOMException: not available';
|
416
|
+
}
|
417
|
+
|
418
|
+
//log('response detected');
|
419
|
+
var docRoot = doc.body ? doc.body : doc.documentElement;
|
420
|
+
xhr.responseText = docRoot ? docRoot.innerHTML : null;
|
421
|
+
xhr.responseXML = doc.XMLDocument ? doc.XMLDocument : doc;
|
422
|
+
if (isXml)
|
423
|
+
s.dataType = 'xml';
|
424
|
+
xhr.getResponseHeader = function(header){
|
425
|
+
var headers = {'content-type': s.dataType};
|
426
|
+
return headers[header];
|
427
|
+
};
|
428
|
+
// support for XHR 'status' & 'statusText' emulation :
|
429
|
+
if (docRoot) {
|
430
|
+
xhr.status = Number( docRoot.getAttribute('status') ) || xhr.status;
|
431
|
+
xhr.statusText = docRoot.getAttribute('statusText') || xhr.statusText;
|
432
|
+
}
|
433
|
+
|
434
|
+
var dt = s.dataType || '';
|
435
|
+
var scr = /(json|script|text)/.test(dt.toLowerCase());
|
436
|
+
if (scr || s.textarea) {
|
437
|
+
// see if user embedded response in textarea
|
438
|
+
var ta = doc.getElementsByTagName('textarea')[0];
|
439
|
+
if (ta) {
|
440
|
+
xhr.responseText = ta.value;
|
441
|
+
// support for XHR 'status' & 'statusText' emulation :
|
442
|
+
xhr.status = Number( ta.getAttribute('status') ) || xhr.status;
|
443
|
+
xhr.statusText = ta.getAttribute('statusText') || xhr.statusText;
|
444
|
+
}
|
445
|
+
else if (scr) {
|
446
|
+
// account for browsers injecting pre around json response
|
447
|
+
var pre = doc.getElementsByTagName('pre')[0];
|
448
|
+
var b = doc.getElementsByTagName('body')[0];
|
449
|
+
if (pre) {
|
450
|
+
xhr.responseText = pre.textContent ? pre.textContent : pre.innerHTML;
|
451
|
+
}
|
452
|
+
else if (b) {
|
453
|
+
xhr.responseText = b.innerHTML;
|
454
|
+
}
|
455
|
+
}
|
456
|
+
}
|
457
|
+
else if (s.dataType == 'xml' && !xhr.responseXML && xhr.responseText != null) {
|
458
|
+
xhr.responseXML = toXml(xhr.responseText);
|
459
|
+
}
|
460
|
+
|
461
|
+
try {
|
462
|
+
data = httpData(xhr, s.dataType, s);
|
463
|
+
}
|
464
|
+
catch (e) {
|
465
|
+
status = 'parsererror';
|
466
|
+
xhr.error = errMsg = (e || status);
|
467
|
+
}
|
468
|
+
}
|
469
|
+
catch (e) {
|
470
|
+
log('error caught: ',e);
|
471
|
+
status = 'error';
|
472
|
+
xhr.error = errMsg = (e || status);
|
473
|
+
}
|
474
|
+
|
475
|
+
if (xhr.aborted) {
|
476
|
+
log('upload aborted');
|
477
|
+
status = null;
|
478
|
+
}
|
479
|
+
|
480
|
+
if (xhr.status) { // we've set xhr.status
|
481
|
+
status = (xhr.status >= 200 && xhr.status < 300 || xhr.status === 304) ? 'success' : 'error';
|
482
|
+
}
|
483
|
+
|
484
|
+
// ordering of these callbacks/triggers is odd, but that's how $.ajax does it
|
485
|
+
if (status === 'success') {
|
486
|
+
s.success && s.success.call(s.context, data, 'success', xhr);
|
487
|
+
g && $.event.trigger("ajaxSuccess", [xhr, s]);
|
488
|
+
}
|
489
|
+
else if (status) {
|
490
|
+
if (errMsg == undefined)
|
491
|
+
errMsg = xhr.statusText;
|
492
|
+
s.error && s.error.call(s.context, xhr, status, errMsg);
|
493
|
+
g && $.event.trigger("ajaxError", [xhr, s, errMsg]);
|
494
|
+
}
|
495
|
+
|
496
|
+
g && $.event.trigger("ajaxComplete", [xhr, s]);
|
497
|
+
|
498
|
+
if (g && ! --$.active) {
|
499
|
+
$.event.trigger("ajaxStop");
|
500
|
+
}
|
501
|
+
|
502
|
+
s.complete && s.complete.call(s.context, xhr, status);
|
503
|
+
|
504
|
+
callbackProcessed = true;
|
505
|
+
if (s.timeout)
|
506
|
+
clearTimeout(timeoutHandle);
|
507
|
+
|
508
|
+
// clean up
|
509
|
+
setTimeout(function() {
|
510
|
+
if (!s.iframeTarget)
|
511
|
+
$io.remove();
|
512
|
+
xhr.responseXML = null;
|
513
|
+
}, 100);
|
514
|
+
}
|
515
|
+
|
516
|
+
var toXml = $.parseXML || function(s, doc) { // use parseXML if available (jQuery 1.5+)
|
517
|
+
if (window.ActiveXObject) {
|
518
|
+
doc = new ActiveXObject('Microsoft.XMLDOM');
|
519
|
+
doc.async = 'false';
|
520
|
+
doc.loadXML(s);
|
521
|
+
}
|
522
|
+
else {
|
523
|
+
doc = (new DOMParser()).parseFromString(s, 'text/xml');
|
524
|
+
}
|
525
|
+
return (doc && doc.documentElement && doc.documentElement.nodeName != 'parsererror') ? doc : null;
|
526
|
+
};
|
527
|
+
var parseJSON = $.parseJSON || function(s) {
|
528
|
+
return window['eval']('(' + s + ')');
|
529
|
+
};
|
530
|
+
|
531
|
+
var httpData = function( xhr, type, s ) { // mostly lifted from jq1.4.4
|
532
|
+
|
533
|
+
var ct = xhr.getResponseHeader('content-type') || '',
|
534
|
+
xml = type === 'xml' || !type && ct.indexOf('xml') >= 0,
|
535
|
+
data = xml ? xhr.responseXML : xhr.responseText;
|
536
|
+
|
537
|
+
if (xml && data.documentElement.nodeName === 'parsererror') {
|
538
|
+
$.error && $.error('parsererror');
|
539
|
+
}
|
540
|
+
if (s && s.dataFilter) {
|
541
|
+
data = s.dataFilter(data, type);
|
542
|
+
}
|
543
|
+
if (typeof data === 'string') {
|
544
|
+
if (type === 'json' || !type && ct.indexOf('json') >= 0) {
|
545
|
+
data = parseJSON(data);
|
546
|
+
} else if (type === "script" || !type && ct.indexOf("javascript") >= 0) {
|
547
|
+
$.globalEval(data);
|
548
|
+
}
|
549
|
+
}
|
550
|
+
return data;
|
551
|
+
};
|
552
|
+
}
|
553
|
+
};
|
554
|
+
|
555
|
+
/**
|
556
|
+
* ajaxForm() provides a mechanism for fully automating form submission.
|
557
|
+
*
|
558
|
+
* The advantages of using this method instead of ajaxSubmit() are:
|
559
|
+
*
|
560
|
+
* 1: This method will include coordinates for <input type="image" /> elements (if the element
|
561
|
+
* is used to submit the form).
|
562
|
+
* 2. This method will include the submit element's name/value data (for the element that was
|
563
|
+
* used to submit the form).
|
564
|
+
* 3. This method binds the submit() method to the form for you.
|
565
|
+
*
|
566
|
+
* The options argument for ajaxForm works exactly as it does for ajaxSubmit. ajaxForm merely
|
567
|
+
* passes the options argument along after properly binding events for submit elements and
|
568
|
+
* the form itself.
|
569
|
+
*/
|
570
|
+
$.fn.ajaxForm = function(options) {
|
571
|
+
// in jQuery 1.3+ we can fix mistakes with the ready state
|
572
|
+
if (this.length === 0) {
|
573
|
+
var o = { s: this.selector, c: this.context };
|
574
|
+
if (!$.isReady && o.s) {
|
575
|
+
log('DOM not ready, queuing ajaxForm');
|
576
|
+
$(function() {
|
577
|
+
$(o.s,o.c).ajaxForm(options);
|
578
|
+
});
|
579
|
+
return this;
|
580
|
+
}
|
581
|
+
// is your DOM ready? http://docs.jquery.com/Tutorials:Introducing_$(document).ready()
|
582
|
+
log('terminating; zero elements found by selector' + ($.isReady ? '' : ' (DOM not ready)'));
|
583
|
+
return this;
|
584
|
+
}
|
585
|
+
|
586
|
+
return this.ajaxFormUnbind().bind('submit.form-plugin', function(e) {
|
587
|
+
if (!e.isDefaultPrevented()) { // if event has been canceled, don't proceed
|
588
|
+
e.preventDefault();
|
589
|
+
$(this).ajaxSubmit(options);
|
590
|
+
}
|
591
|
+
}).bind('click.form-plugin', function(e) {
|
592
|
+
var target = e.target;
|
593
|
+
var $el = $(target);
|
594
|
+
if (!($el.is(":submit,input:image"))) {
|
595
|
+
// is this a child element of the submit el? (ex: a span within a button)
|
596
|
+
var t = $el.closest(':submit');
|
597
|
+
if (t.length == 0) {
|
598
|
+
return;
|
599
|
+
}
|
600
|
+
target = t[0];
|
601
|
+
}
|
602
|
+
var form = this;
|
603
|
+
form.clk = target;
|
604
|
+
if (target.type == 'image') {
|
605
|
+
if (e.offsetX != undefined) {
|
606
|
+
form.clk_x = e.offsetX;
|
607
|
+
form.clk_y = e.offsetY;
|
608
|
+
} else if (typeof $.fn.offset == 'function') { // try to use dimensions plugin
|
609
|
+
var offset = $el.offset();
|
610
|
+
form.clk_x = e.pageX - offset.left;
|
611
|
+
form.clk_y = e.pageY - offset.top;
|
612
|
+
} else {
|
613
|
+
form.clk_x = e.pageX - target.offsetLeft;
|
614
|
+
form.clk_y = e.pageY - target.offsetTop;
|
615
|
+
}
|
616
|
+
}
|
617
|
+
// clear form vars
|
618
|
+
setTimeout(function() { form.clk = form.clk_x = form.clk_y = null; }, 100);
|
619
|
+
});
|
620
|
+
};
|
621
|
+
|
622
|
+
// ajaxFormUnbind unbinds the event handlers that were bound by ajaxForm
|
623
|
+
$.fn.ajaxFormUnbind = function() {
|
624
|
+
return this.unbind('submit.form-plugin click.form-plugin');
|
625
|
+
};
|
626
|
+
|
627
|
+
/**
|
628
|
+
* formToArray() gathers form element data into an array of objects that can
|
629
|
+
* be passed to any of the following ajax functions: $.get, $.post, or load.
|
630
|
+
* Each object in the array has both a 'name' and 'value' property. An example of
|
631
|
+
* an array for a simple login form might be:
|
632
|
+
*
|
633
|
+
* [ { name: 'username', value: 'jresig' }, { name: 'password', value: 'secret' } ]
|
634
|
+
*
|
635
|
+
* It is this array that is passed to pre-submit callback functions provided to the
|
636
|
+
* ajaxSubmit() and ajaxForm() methods.
|
637
|
+
*/
|
638
|
+
$.fn.formToArray = function(semantic) {
|
639
|
+
var a = [];
|
640
|
+
if (this.length === 0) {
|
641
|
+
return a;
|
642
|
+
}
|
643
|
+
|
644
|
+
var form = this[0];
|
645
|
+
var els = semantic ? form.getElementsByTagName('*') : form.elements;
|
646
|
+
if (!els) {
|
647
|
+
return a;
|
648
|
+
}
|
649
|
+
|
650
|
+
var i,j,n,v,el,max,jmax;
|
651
|
+
for(i=0, max=els.length; i < max; i++) {
|
652
|
+
el = els[i];
|
653
|
+
n = el.name;
|
654
|
+
if (!n) {
|
655
|
+
continue;
|
656
|
+
}
|
657
|
+
|
658
|
+
if (semantic && form.clk && el.type == "image") {
|
659
|
+
// handle image inputs on the fly when semantic == true
|
660
|
+
if(!el.disabled && form.clk == el) {
|
661
|
+
a.push({name: n, value: $(el).val()});
|
662
|
+
a.push({name: n+'.x', value: form.clk_x}, {name: n+'.y', value: form.clk_y});
|
663
|
+
}
|
664
|
+
continue;
|
665
|
+
}
|
666
|
+
|
667
|
+
v = $.fieldValue(el, true);
|
668
|
+
if (v && v.constructor == Array) {
|
669
|
+
for(j=0, jmax=v.length; j < jmax; j++) {
|
670
|
+
a.push({name: n, value: v[j]});
|
671
|
+
}
|
672
|
+
}
|
673
|
+
else if (v !== null && typeof v != 'undefined') {
|
674
|
+
a.push({name: n, value: v});
|
675
|
+
}
|
676
|
+
}
|
677
|
+
|
678
|
+
if (!semantic && form.clk) {
|
679
|
+
// input type=='image' are not found in elements array! handle it here
|
680
|
+
var $input = $(form.clk), input = $input[0];
|
681
|
+
n = input.name;
|
682
|
+
if (n && !input.disabled && input.type == 'image') {
|
683
|
+
a.push({name: n, value: $input.val()});
|
684
|
+
a.push({name: n+'.x', value: form.clk_x}, {name: n+'.y', value: form.clk_y});
|
685
|
+
}
|
686
|
+
}
|
687
|
+
return a;
|
688
|
+
};
|
689
|
+
|
690
|
+
/**
|
691
|
+
* Serializes form data into a 'submittable' string. This method will return a string
|
692
|
+
* in the format: name1=value1&name2=value2
|
693
|
+
*/
|
694
|
+
$.fn.formSerialize = function(semantic) {
|
695
|
+
//hand off to jQuery.param for proper encoding
|
696
|
+
return $.param(this.formToArray(semantic));
|
697
|
+
};
|
698
|
+
|
699
|
+
/**
|
700
|
+
* Serializes all field elements in the jQuery object into a query string.
|
701
|
+
* This method will return a string in the format: name1=value1&name2=value2
|
702
|
+
*/
|
703
|
+
$.fn.fieldSerialize = function(successful) {
|
704
|
+
var a = [];
|
705
|
+
this.each(function() {
|
706
|
+
var n = this.name;
|
707
|
+
if (!n) {
|
708
|
+
return;
|
709
|
+
}
|
710
|
+
var v = $.fieldValue(this, successful);
|
711
|
+
if (v && v.constructor == Array) {
|
712
|
+
for (var i=0,max=v.length; i < max; i++) {
|
713
|
+
a.push({name: n, value: v[i]});
|
714
|
+
}
|
715
|
+
}
|
716
|
+
else if (v !== null && typeof v != 'undefined') {
|
717
|
+
a.push({name: this.name, value: v});
|
718
|
+
}
|
719
|
+
});
|
720
|
+
//hand off to jQuery.param for proper encoding
|
721
|
+
return $.param(a);
|
722
|
+
};
|
723
|
+
|
724
|
+
/**
|
725
|
+
* Returns the value(s) of the element in the matched set. For example, consider the following form:
|
726
|
+
*
|
727
|
+
* <form><fieldset>
|
728
|
+
* <input name="A" type="text" />
|
729
|
+
* <input name="A" type="text" />
|
730
|
+
* <input name="B" type="checkbox" value="B1" />
|
731
|
+
* <input name="B" type="checkbox" value="B2"/>
|
732
|
+
* <input name="C" type="radio" value="C1" />
|
733
|
+
* <input name="C" type="radio" value="C2" />
|
734
|
+
* </fieldset></form>
|
735
|
+
*
|
736
|
+
* var v = $(':text').fieldValue();
|
737
|
+
* // if no values are entered into the text inputs
|
738
|
+
* v == ['','']
|
739
|
+
* // if values entered into the text inputs are 'foo' and 'bar'
|
740
|
+
* v == ['foo','bar']
|
741
|
+
*
|
742
|
+
* var v = $(':checkbox').fieldValue();
|
743
|
+
* // if neither checkbox is checked
|
744
|
+
* v === undefined
|
745
|
+
* // if both checkboxes are checked
|
746
|
+
* v == ['B1', 'B2']
|
747
|
+
*
|
748
|
+
* var v = $(':radio').fieldValue();
|
749
|
+
* // if neither radio is checked
|
750
|
+
* v === undefined
|
751
|
+
* // if first radio is checked
|
752
|
+
* v == ['C1']
|
753
|
+
*
|
754
|
+
* The successful argument controls whether or not the field element must be 'successful'
|
755
|
+
* (per http://www.w3.org/TR/html4/interact/forms.html#successful-controls).
|
756
|
+
* The default value of the successful argument is true. If this value is false the value(s)
|
757
|
+
* for each element is returned.
|
758
|
+
*
|
759
|
+
* Note: This method *always* returns an array. If no valid value can be determined the
|
760
|
+
* array will be empty, otherwise it will contain one or more values.
|
761
|
+
*/
|
762
|
+
$.fn.fieldValue = function(successful) {
|
763
|
+
for (var val=[], i=0, max=this.length; i < max; i++) {
|
764
|
+
var el = this[i];
|
765
|
+
var v = $.fieldValue(el, successful);
|
766
|
+
if (v === null || typeof v == 'undefined' || (v.constructor == Array && !v.length)) {
|
767
|
+
continue;
|
768
|
+
}
|
769
|
+
v.constructor == Array ? $.merge(val, v) : val.push(v);
|
770
|
+
}
|
771
|
+
return val;
|
772
|
+
};
|
773
|
+
|
774
|
+
/**
|
775
|
+
* Returns the value of the field element.
|
776
|
+
*/
|
777
|
+
$.fieldValue = function(el, successful) {
|
778
|
+
var n = el.name, t = el.type, tag = el.tagName.toLowerCase();
|
779
|
+
if (successful === undefined) {
|
780
|
+
successful = true;
|
781
|
+
}
|
782
|
+
|
783
|
+
if (successful && (!n || el.disabled || t == 'reset' || t == 'button' ||
|
784
|
+
(t == 'checkbox' || t == 'radio') && !el.checked ||
|
785
|
+
(t == 'submit' || t == 'image') && el.form && el.form.clk != el ||
|
786
|
+
tag == 'select' && el.selectedIndex == -1)) {
|
787
|
+
return null;
|
788
|
+
}
|
789
|
+
|
790
|
+
if (tag == 'select') {
|
791
|
+
var index = el.selectedIndex;
|
792
|
+
if (index < 0) {
|
793
|
+
return null;
|
794
|
+
}
|
795
|
+
var a = [], ops = el.options;
|
796
|
+
var one = (t == 'select-one');
|
797
|
+
var max = (one ? index+1 : ops.length);
|
798
|
+
for(var i=(one ? index : 0); i < max; i++) {
|
799
|
+
var op = ops[i];
|
800
|
+
if (op.selected) {
|
801
|
+
var v = op.value;
|
802
|
+
if (!v) { // extra pain for IE...
|
803
|
+
v = (op.attributes && op.attributes['value'] && !(op.attributes['value'].specified)) ? op.text : op.value;
|
804
|
+
}
|
805
|
+
if (one) {
|
806
|
+
return v;
|
807
|
+
}
|
808
|
+
a.push(v);
|
809
|
+
}
|
810
|
+
}
|
811
|
+
return a;
|
812
|
+
}
|
813
|
+
return $(el).val();
|
814
|
+
};
|
815
|
+
|
816
|
+
/**
|
817
|
+
* Clears the form data. Takes the following actions on the form's input fields:
|
818
|
+
* - input text fields will have their 'value' property set to the empty string
|
819
|
+
* - select elements will have their 'selectedIndex' property set to -1
|
820
|
+
* - checkbox and radio inputs will have their 'checked' property set to false
|
821
|
+
* - inputs of type submit, button, reset, and hidden will *not* be effected
|
822
|
+
* - button elements will *not* be effected
|
823
|
+
*/
|
824
|
+
$.fn.clearForm = function() {
|
825
|
+
return this.each(function() {
|
826
|
+
$('input,select,textarea', this).clearFields();
|
827
|
+
});
|
828
|
+
};
|
829
|
+
|
830
|
+
/**
|
831
|
+
* Clears the selected form elements.
|
832
|
+
*/
|
833
|
+
$.fn.clearFields = $.fn.clearInputs = function() {
|
834
|
+
var re = /^(?:color|date|datetime|email|month|number|password|range|search|tel|text|time|url|week)$/i; // 'hidden' is not in this list
|
835
|
+
return this.each(function() {
|
836
|
+
var t = this.type, tag = this.tagName.toLowerCase();
|
837
|
+
if (re.test(t) || tag == 'textarea') {
|
838
|
+
this.value = '';
|
839
|
+
}
|
840
|
+
else if (t == 'checkbox' || t == 'radio') {
|
841
|
+
this.checked = false;
|
842
|
+
}
|
843
|
+
else if (tag == 'select') {
|
844
|
+
this.selectedIndex = -1;
|
845
|
+
}
|
846
|
+
});
|
847
|
+
};
|
848
|
+
|
849
|
+
/**
|
850
|
+
* Resets the form data. Causes all form elements to be reset to their original value.
|
851
|
+
*/
|
852
|
+
$.fn.resetForm = function() {
|
853
|
+
return this.each(function() {
|
854
|
+
// guard against an input with the name of 'reset'
|
855
|
+
// note that IE reports the reset function as an 'object'
|
856
|
+
if (typeof this.reset == 'function' || (typeof this.reset == 'object' && !this.reset.nodeType)) {
|
857
|
+
this.reset();
|
858
|
+
}
|
859
|
+
});
|
860
|
+
};
|
861
|
+
|
862
|
+
/**
|
863
|
+
* Enables or disables any matching elements.
|
864
|
+
*/
|
865
|
+
$.fn.enable = function(b) {
|
866
|
+
if (b === undefined) {
|
867
|
+
b = true;
|
868
|
+
}
|
869
|
+
return this.each(function() {
|
870
|
+
this.disabled = !b;
|
871
|
+
});
|
872
|
+
};
|
873
|
+
|
874
|
+
/**
|
875
|
+
* Checks/unchecks any matching checkboxes or radio buttons and
|
876
|
+
* selects/deselects and matching option elements.
|
877
|
+
*/
|
878
|
+
$.fn.selected = function(select) {
|
879
|
+
if (select === undefined) {
|
880
|
+
select = true;
|
881
|
+
}
|
882
|
+
return this.each(function() {
|
883
|
+
var t = this.type;
|
884
|
+
if (t == 'checkbox' || t == 'radio') {
|
885
|
+
this.checked = select;
|
886
|
+
}
|
887
|
+
else if (this.tagName.toLowerCase() == 'option') {
|
888
|
+
var $sel = $(this).parent('select');
|
889
|
+
if (select && $sel[0] && $sel[0].type == 'select-one') {
|
890
|
+
// deselect all other options
|
891
|
+
$sel.find('option').selected(false);
|
892
|
+
}
|
893
|
+
this.selected = select;
|
894
|
+
}
|
895
|
+
});
|
896
|
+
};
|
897
|
+
|
898
|
+
// helper fn for console logging
|
899
|
+
function log() {
|
900
|
+
var msg = '[jquery.form] ' + Array.prototype.join.call(arguments,'');
|
901
|
+
if (window.console && window.console.log) {
|
902
|
+
window.console.log(msg);
|
903
|
+
}
|
904
|
+
else if (window.opera && window.opera.postError) {
|
905
|
+
window.opera.postError(msg);
|
906
|
+
}
|
907
|
+
};
|
908
|
+
|
909
|
+
})(jQuery);
|