Changesets can be listed by changeset number.
The Git repository is here.
Changeset 341
Lots of changes to Hub to make it Rails 2.3.11 compatible. This is being
checked in as one large changeset because it doesn't really split down into
smaller components without having something that doesn't function in the
intermediate steps.
- Comitted by: rool
- Date: Saturday March 19 19:11:39 2011 (over 13 years ago)
Affected files:
- rool/rails/hub/trunk/app/views/stylesheets/hub.css.erb (from rool/rails/hub/trunk/public/stylesheets/risc_os_open.css:257)
- rool/rails/hub/trunk/app/controllers/application_controller.rb (from rool/rails/hub/trunk/app/controllers/application.rb:257)
- rool/rails/hub/trunk/app/views/account/change_details.html.erb (from rool/rails/hub/trunk/app/views/account/change_details.rhtml:257)
- rool/rails/hub/trunk/app/views/account/change_password.html.erb (from rool/rails/hub/trunk/app/views/account/change_password.rhtml:257)
- rool/rails/hub/trunk/app/views/account/delete.html.erb (from rool/rails/hub/trunk/app/views/account/delete.rhtml:257)
- rool/rails/hub/trunk/app/views/account/edit_roles.html.erb (from rool/rails/hub/trunk/app/views/account/edit_roles.rhtml:257)
- rool/rails/hub/trunk/app/views/account/enumerate.html.erb (from rool/rails/hub/trunk/app/views/account/enumerate.rhtml:257)
- rool/rails/hub/trunk/app/views/account/forgot_password.html.erb (from rool/rails/hub/trunk/app/views/account/forgot_password.rhtml:257)
- rool/rails/hub/trunk/app/views/account/list.html.erb (from rool/rails/hub/trunk/app/views/account/list.rhtml:257)
- rool/rails/hub/trunk/app/views/account/login.html.erb (from rool/rails/hub/trunk/app/views/account/login.rhtml:257)
- rool/rails/hub/trunk/app/views/account/reset_password.html.erb (from rool/rails/hub/trunk/app/views/account/reset_password.rhtml:257)
- rool/rails/hub/trunk/app/views/account/show.html.erb (from rool/rails/hub/trunk/app/views/account/show.rhtml:257)
- rool/rails/hub/trunk/app/views/account/signup.html.erb (from rool/rails/hub/trunk/app/views/account/signup.rhtml:257)
- rool/rails/hub/trunk/app/views/layouts/default.html.erb (from rool/rails/hub/trunk/app/views/layouts/default.rhtml:259)
- rool/rails/hub/trunk/app/views/shared/_powered.html.erb (from rool/rails/hub/trunk/app/views/shared/_powered.rhtml:257)
- rool/rails/hub/trunk/app/views/shared/_theme.html.erb (from rool/rails/hub/trunk/app/views/shared/_theme.rhtml:257)
- rool/rails/hub/trunk/app/views/tasks/index.html.erb (from rool/rails/hub/trunk/app/views/tasks/index.rhtml:257)
- rool/rails/hub/trunk/app/views/tasks/service.html.erb (from rool/rails/hub/trunk/app/views/tasks/service.rhtml:257)
- rool/rails/hub/trunk/app/views/user_notifier/activation.txt.erb (from rool/rails/hub/trunk/app/views/user_notifier/activation.rhtml:257)
- rool/rails/hub/trunk/app/views/user_notifier/destruction.txt.erb (from rool/rails/hub/trunk/app/views/user_notifier/destruction.rhtml:257)
- rool/rails/hub/trunk/app/views/user_notifier/forgot_password.txt.erb (from rool/rails/hub/trunk/app/views/user_notifier/forgot_password.rhtml:257)
- rool/rails/hub/trunk/app/views/user_notifier/reset_password.txt.erb (from rool/rails/hub/trunk/app/views/user_notifier/reset_password.rhtml:257)
- rool/rails/hub/trunk/app/views/user_notifier/signup_notification.txt.erb (from rool/rails/hub/trunk/app/views/user_notifier/signup_notification.rhtml:257)
- rool/rails/hub/trunk/app/views/stylesheets/
- rool/rails/hub/trunk/config/initializers/
- rool/rails/hub/trunk/lib/
- rool/rails/hub/trunk/lib/patches/
- rool/rails/hub/trunk/lib/tasks/
- rool/rails/hub/trunk/CHANGELOG
- rool/rails/hub/trunk/config/initializers/00_always_run_first.rb
- rool/rails/hub/trunk/config/initializers/01_backtrace_silencers.rb
- rool/rails/hub/trunk/config/initializers/01_inflections.rb
- rool/rails/hub/trunk/config/initializers/01_mime_types.rb
- rool/rails/hub/trunk/config/initializers/01_new_rails_defaults.rb
- rool/rails/hub/trunk/config/initializers/01_session_store.rb
- rool/rails/hub/trunk/config/initializers/50_general_settings.rb
- rool/rails/hub/trunk/config/initializers/99_sanity_checker.rb
- rool/rails/hub/trunk/lib/patches/rack.rb
- rool/rails/hub/trunk/lib/patches/session_store.rb
- rool/rails/hub/trunk/lib/tasks/yaml_db_tasks.rake
- rool/rails/hub/trunk/public/401.html
- rool/rails/hub/trunk/script/dbconsole
- rool/rails/hub/trunk/components/
- rool/rails/hub/trunk/public/stylesheets/
- rool/rails/hub/trunk/tmp/cache/
- rool/rails/hub/trunk/tmp/sessions/
- rool/rails/hub/trunk/tmp/sockets/
- rool/rails/hub/trunk/README (diff)
- rool/rails/hub/trunk/VERSION (diff)
- rool/rails/hub/trunk/app/controllers/account_controller.rb (diff)
- rool/rails/hub/trunk/app/controllers/tasks_controller.rb (diff)
- rool/rails/hub/trunk/app/helpers/application_helper.rb (diff)
- rool/rails/hub/trunk/app/models/user_notifier.rb (diff)
- rool/rails/hub/trunk/config/boot.rb (diff)
- rool/rails/hub/trunk/config/environment.rb (diff)
- rool/rails/hub/trunk/config/environments/development.rb (diff)
- rool/rails/hub/trunk/config/environments/production.rb (diff)
- rool/rails/hub/trunk/config/environments/test.rb (diff)
- rool/rails/hub/trunk/config/routes.rb (diff)
- rool/rails/hub/trunk/db/migrate/001_create_users.rb (diff)
- rool/rails/hub/trunk/db/schema.rb (diff)
- rool/rails/hub/trunk/doc/README_FOR_APP (diff)
- rool/rails/hub/trunk/public/404.html (diff)
- rool/rails/hub/trunk/public/500.html (diff)
- rool/rails/hub/trunk/public/javascripts/application.js (diff)
- rool/rails/hub/trunk/public/javascripts/controls.js (diff)
- rool/rails/hub/trunk/public/javascripts/dragdrop.js (diff)
- rool/rails/hub/trunk/public/javascripts/effects.js (diff)
- rool/rails/hub/trunk/public/javascripts/prototype.js (diff)
rool/rails/hub/trunk/README:
prev. | current | |
1 | ||
1 | == Welcome to Hub | |
3 | ||
4 | ||
5 | ||
6 | ||
7 | ||
8 | ||
9 | ||
10 | ||
11 | ||
13 | ||
14 | ||
15 | ||
16 | ||
17 | ||
4 | Hub is a single sign-on solution for multiple Ruby On Rails applications which | |
5 | are running under the same domain but different paths within that domain. When | |
6 | a user signs into Hub, they don't have to individually sign in to other | |
7 | applications on the domain provided that those applications are integrated | |
8 | with the Hub mechanism. This requires software development effort on behalf of | |
9 | the application installer or application developer. | |
19 | ||
20 | ||
21 | ||
22 | ||
23 | ||
24 | ||
25 | ||
11 | Hub provides a solution to a different problem from that addressed by cross | |
12 | domain single sign-on solutions such as OpenID, wherein a user will typically | |
13 | have to sign in to domain individually, but is at least able to do so using a | |
14 | single set of identifying credentials. | |
28 | ||
17 | === Further reading | |
30 | ||
31 | ||
32 | ||
19 | For more information about Hub, please see: | |
21 | * http://hub.pond.org.uk/ | |
35 | ||
23 | For more information about Ruby On Rails, please see: | |
37 | ||
38 | ||
25 | * http://www.rubyonrails.org/ | |
40 | ||
41 | ||
42 | ||
43 | ||
44 | ||
27 | For more software from Hipposoft, please visit: | |
46 | ||
47 | ||
48 | ||
49 | ||
50 | ||
51 | ||
52 | ||
53 | ||
54 | ||
55 | ||
56 | ||
57 | ||
58 | | |
59 | | |
60 | | |
61 | | |
62 | | |
63 | | |
64 | | |
65 | | |
66 | | |
67 | | |
68 | | |
69 | | |
70 | ||
71 | ||
72 | ||
73 | ||
74 | ||
75 | ||
76 | ||
77 | ||
78 | ||
79 | ||
80 | ||
81 | ||
82 | ||
83 | ||
84 | ||
85 | ||
86 | ||
87 | ||
88 | ||
89 | ||
90 | ||
91 | | |
92 | | |
93 | | |
94 | | |
95 | | |
96 | | |
97 | | |
98 | ||
99 | ||
100 | ||
101 | ||
102 | ||
103 | | |
104 | | |
105 | | |
106 | | |
107 | | |
108 | ||
109 | ||
110 | ||
111 | | |
112 | | |
113 | | |
114 | | |
115 | ||
116 | ||
117 | ||
118 | ||
119 | ||
120 | ||
121 | ||
122 | ||
123 | ||
124 | ||
125 | ||
126 | ||
127 | ||
128 | ||
129 | ||
130 | ||
131 | ||
132 | ||
133 | ||
134 | | |
135 | ||
136 | ||
137 | | |
138 | | |
139 | | |
140 | ||
141 | ||
142 | | |
143 | | |
144 | | |
145 | ||
146 | | |
147 | | |
148 | | |
149 | | |
150 | | |
151 | ||
152 | | |
153 | ||
154 | ||
155 | | |
156 | ||
157 | ||
158 | | |
159 | ||
160 | ||
161 | | |
162 | ||
163 | ||
164 | | |
165 | | |
166 | ||
167 | ||
168 | | |
169 | | |
170 | | |
171 | ||
172 | | |
173 | | |
174 | ||
175 | ||
176 | | |
177 | ||
178 | ||
179 | | |
180 | ||
181 | ||
182 | | |
183 | | |
29 | * http://hipposoft.pond.org.uk/ |
rool/rails/hub/trunk/VERSION:
prev. | current | |
1 | ||
1 | 1.0.0 (31-Jan-2011) |
rool/rails/hub/trunk/app/controllers/account_controller.rb:
prev. | current | |
1 | ####################################################################### | |
2 | # File: account_controller.rb # | |
3 | # (C) Hipposoft 2006-2011 # | |
4 | # # | |
5 | # Purpose: Hub account management. # | |
6 | # # | |
7 | # Author: A.D.Hodgkinson # | |
8 | # # | |
9 | # History: 31-Jan-2011 (ADH): Comment header added; prior history # | |
10 | # not recorded. # | |
11 | ####################################################################### | |
12 | ||
class AccountController < ApplicationController | ||
3 | | |
15 | layout 'default.html.erb' | |
# Cache the logged in and out PNG images in RAM; they're only small. | ||
7 | | |
8 | | |
19 | @@logged_in_image = File.read("#{RAILS_ROOT}/public/images/icons/logged_in.png") | |
20 | @@logged_out_image = File.read("#{RAILS_ROOT}/public/images/icons/logged_out.png") | |
# Action permissions for this class as a class variable, exposed | ||
# to the public through a class method. | ||
... | ... | |
@@hubssolib_permissions | ||
end | ||
29 | | |
30 | ||
31 | | |
32 | ||
# HTTPS enforcement for all methods, except the login indicator; if someone | ||
# is on an HTTP page, the login indicator needs to be fetched by HTTP too so | ||
# it can show "logged out" as the secure-only cookies won't get sent. It is | ||
... | ... | |
hubssolib_set_flash( | ||
:notice, | ||
135 | | |
143 | "Your #{INSTITUTION_NAME_LONG} web site account is now active." | |
) | ||
hubssolib_redirect_back_or_default(:controller => 'tasks', :action => nil) | ||
else | ||
hubssolib_set_flash( | ||
:alert, | ||
142 | | |
143 | | |
144 | | |
150 | "Unable to activate your #{INSTITUTION_NAME_LONG} web site account. " << | |
151 | 'Is the activation code correct, or has it already been used? ' << | |
152 | "If in doubt please try to sign up again. Contact #{INSTITUTION_NAME_SHORT} if you " << | |
'keep having trouble.' | ||
) | ||
... | ... | |
def list | ||
@title = 'List of user accounts' | ||
289 | | |
297 | # Page zero means 'all'. | |
291 | | |
292 | ||
293 | | |
294 | | |
295 | | |
299 | if (params.has_key?(:page) && params[:page] == '0' ) | |
300 | page = 1 | |
301 | per_page = User.count | |
else | ||
297 | | |
298 | | |
303 | page = params[:page] | |
304 | per_page = 20 | |
end | ||
301 | | |
302 | | |
303 | | |
304 | | |
305 | | |
307 | @users = User.paginate :page => page, | |
308 | :per_page => per_page, | |
309 | :order => 'created_at DESC' | |
end | ||
# Enumerate active users (those users known to the DRb server). | ||
# | ||
def enumerate | ||
@title = 'Active users' | ||
312 | | |
313 | | |
316 | @users = hubssolib_enumerate_users | |
317 | @users = [] if @users.nil? | |
# Map the user objects returned from the HubSsoLib Gem to | ||
# internal users. | ||
318 | | |
319 | | |
320 | | |
322 | @users.map! { |user| to_real_user(user) } | |
322 | | |
324 | # Page number zero is magic; it indicates "all items". | |
324 | | |
325 | ||
326 | | |
327 | ||
328 | | |
329 | | |
330 | | |
331 | | |
332 | | |
333 | | |
334 | | |
335 | ||
336 | | |
337 | | |
338 | | |
339 | ||
326 | if (params.has_key?(:page) && params[:page] == '0' ) | |
327 | page = 1 | |
328 | per_page = @users.count | |
else | ||
341 | ||
342 | | |
343 | ||
330 | page = params[:page] | |
331 | per_page = 20 | |
end | ||
333 | ||
334 | @users = @users.paginate :page => page, | |
335 | :per_page => per_page, | |
336 | :order => 'created_at DESC' | |
end | ||
# Show details of a specific user account. | ||
... | ... | |
if (hubssolib_get_user_id == user.id) | ||
hubssolib_set_flash( | ||
:alert, | ||
402 | | |
394 | 'Please use the normal control panel below to delete your own account.' | |
) | ||
396 | redirect_to root_path | |
397 | return | |
elsif (user.roles.to_authenticated_roles.include?(:admin)) | ||
hubssolib_set_flash( | ||
:alert, | ||
... | ... | |
# even if the HTML stays cached. | ||
# | ||
def login_indication | ||
428 | | |
429 | | |
422 | headers['Pragma'] = 'no-cache' | |
423 | headers['Cache-Control'] = 'no-cache, must-revalidate' | |
send_data hubssolib_logged_in? ? @@logged_in_image : @@logged_out_image, | ||
:type => 'image/png', | ||
... | ... | |
real_user = User.find(user.user_id) | ||
raise 'No equivalent real user' unless real_user | ||
469 | | |
470 | | |
471 | | |
472 | | |
473 | | |
474 | | |
475 | | |
476 | | |
477 | | |
478 | | |
479 | | |
480 | | |
463 | real_user.activated_at = Time.zone.parse(user.user_activated_at) | |
464 | real_user.activation_code = user.user_activation_code | |
465 | real_user.created_at = Time.zone.parse(user.user_created_at) | |
466 | real_user.crypted_password = user.user_crypted_password | |
467 | real_user.email = user.user_email | |
468 | real_user.member_id = user.user_member_id | |
469 | real_user.password_reset_code = user.user_password_reset_code | |
470 | real_user.password_reset_code_expires_at = Time.zone.parse(user.user_password_reset_code_expires_at) | |
471 | real_user.real_name = user.user_real_name | |
472 | real_user.remember_token = user.user_remember_token | |
473 | real_user.remember_token_expires_at = Time.zone.parse(user.user_remember_token_expires_at) | |
474 | real_user.roles = user.user_roles | |
475 | real_user.salt = user.user_salt | |
476 | real_user.updated_at = Time.zone.parse(user.user_updated_at) | |
return real_user | ||
end | ||
... | ... | |
user = HubSsoLib::User.new | ||
493 | | |
494 | | |
495 | | |
496 | | |
497 | | |
498 | | |
499 | | |
489 | user.user_activated_at = real_user.activated_at.to_s | |
user.user_activation_code = real_user.activation_code | ||
501 | | |
491 | user.user_created_at = real_user.created_at.to_s | |
492 | user.user_crypted_password = real_user.crypted_password | |
493 | user.user_email = real_user.email | |
user.user_id = real_user.id | ||
495 | user.user_member_id = real_user.member_id | |
user.user_password_reset_code = real_user.password_reset_code | ||
497 | user.user_password_reset_code_expires_at = real_user.password_reset_code_expires_at.to_s | |
498 | user.user_real_name = real_user.real_name | |
user.user_remember_token = real_user.remember_token | ||
505 | | |
506 | | |
507 | | |
500 | user.user_remember_token_expires_at = real_user.remember_token_expires_at.to_s | |
501 | user.user_roles = real_user.roles | |
502 | user.user_salt = real_user.salt | |
503 | user.user_updated_at = real_user.updated_at.to_s | |
return user | ||
end |
rool/rails/hub/trunk/app/controllers/tasks_controller.rb:
prev. | current | |
# File: tasks_controller.rb # | ||
# (C) Hipposoft 2006 # | ||
# # | ||
5 | ||
5 | # Purpose: Provide hub user account high-level task management. # | |
# # | ||
# Author: A.D.Hodgkinson # | ||
# # | ||
... | ... | |
class TasksController < ApplicationController | ||
helper :Tasks | ||
14 | | |
14 | layout 'default.html.erb' | |
skip_before_filter :hubssolib_login_required | ||
17 | skip_before_filter :hubssolib_beforehand, :only => :service | |
18 | skip_after_filter :hubssolib_afterwards, :only => :service | |
def index | ||
# Generate a list of available tasks. | ||
... | ... | |
@title = 'Service failure' | ||
@exception_data = hubssolib_get_exception_message(params[:id]) | ||
31 | @service_fault = true # To tell the template not to talk to Hub | |
end | ||
end |
rool/rails/hub/trunk/app/helpers/application_helper.rb:
prev. | current | |
def make_action_link(controller, action, width, height, text, uri = nil) | ||
if (uri.nil?) | ||
22 | | |
22 | html = link_to(image_tag(image_path("#{controller}/#{action}.png"), :size => "#{width}x#{height}", :border => 0), | |
{:controller => controller, :action => action}, :class => 'image') | ||
unless (text.nil? or text.empty?) | ||
... | ... | |
else | ||
32 | | |
32 | html = content_tag("a", image_tag(image_path("#{controller}/#{action}.png"), :size => "#{width}x#{height}", :border => 0), | |
{ :href => uri, :class => 'image' }) | ||
unless (text.nil? or text.empty?) | ||
html << '<br />' | ||
... | ... | |
# If you don't want the text link adding, pass 'nil' or an empty string. | ||
# | ||
def make_protected_action_link(controller, action, id, text, onclick = nil) | ||
55 | | |
opts = {:type => 'image', | ||
:name => 'submit', | ||
58 | | |
59 | | |
57 | :alt => action.humanize, | |
58 | :src => image_path("#{controller}/#{action}.png")} | |
unless (onclick.nil? or onclick.empty?) | ||
opts[:onclick] = "return confirm('#{onclick}');" | ||
end | ||
64 | html = form_tag({:controller => controller, :action => action, :id => id}) | |
html << tag('input', opts) | ||
66 | | |
66 | html << '</form>' | |
unless (text.nil? or text.empty?) | ||
html << text | ||
end | ||
71 | ||
72 | return html | |
end | ||
end |
rool/rails/hub/trunk/app/models/user_notifier.rb:
prev. | current | |
class UserNotifier < ActionMailer::Base | ||
2 | ||
3 | helper :application | |
4 | ||
5 | EMAIL_PLAIN_TEXT_LINE_WIDTH = 70 | |
6 | ||
7 | # Return a string formatted according to e-mail text width resrtrictions. | |
8 | # | |
9 | def self.formatted( text ) | |
10 | # The strange 'extend' stuff is a way of calling through to a helper. | |
11 | Object.new.extend( ActionView::Helpers::TextHelper ).word_wrap( | |
12 | text, | |
13 | :line_width => EMAIL_PLAIN_TEXT_LINE_WIDTH | |
14 | ) | |
15 | end | |
16 | ||
def signup_notification(user) | ||
3 | | |
4 | | |
5 | | |
18 | setup_mail( | |
19 | user, | |
20 | "[#{INSTITUTION_NAME_SHORT}] Please activate your new web site account", | |
21 | url_for(:controller => :account, | |
22 | :action => :activate, | |
23 | :id => user.activation_code, | |
24 | :protocol => 'https') | |
25 | ) | |
end | ||
def activation(user) | ||
9 | | |
10 | | |
11 | | |
29 | setup_mail( | |
30 | user, | |
31 | "[#{INSTITUTION_NAME_SHORT}] Your web site account has been activated", | |
32 | root_url() | |
33 | ) | |
end | ||
def forgot_password(user) | ||
15 | | |
16 | | |
17 | | |
37 | setup_mail( | |
38 | user, | |
39 | "[#{INSTITUTION_NAME_SHORT}] Request to change a web site account password", | |
40 | url_for(:controller => :account, | |
41 | :action => :reset_password, | |
42 | :id => user.password_reset_code, | |
43 | :protocol => 'https') | |
44 | ) | |
end | ||
def reset_password(user) | ||
21 | | |
22 | | |
23 | | |
48 | setup_mail( | |
49 | user, | |
50 | "[#{INSTITUTION_NAME_SHORT}] Your web site account password has been reset", | |
51 | EMAIL_ADMIN | |
52 | ) | |
end | ||
def destruction(user) | ||
27 | | |
28 | | |
29 | | |
56 | setup_mail( | |
57 | user, | |
58 | "[#{INSTITUTION_NAME_SHORT}] Your web site account has been deleted", | |
59 | EMAIL_ADMIN | |
60 | ) | |
end | ||
32 | | |
63 | protected | |
34 | | |
35 | | |
36 | | |
37 | | |
38 | | |
39 | | |
65 | # Prepare for and render an e-mail message view. Pass the recipient User | |
66 | # object, subject text and an associated URL which will be shown in the body. | |
67 | # | |
68 | # Passes parameter 'url' through as local "@url" through to the view. | |
69 | # | |
70 | def setup_mail( user, subject, url ) | |
71 | subject subject | |
72 | from EMAIL_ADMIN | |
73 | recipients user.email | |
74 | sent_on Time.now | |
75 | content_type 'text/plain' | |
76 | body render( | |
77 | :file => "user_notifier/#{ action_name }.txt.erb", | |
78 | :body => { :url => url } | |
79 | ) | |
end | ||
end |
rool/rails/hub/trunk/config/boot.rb:
prev. | current | |
1 | ||
1 | # Don't change this file! | |
2 | # Configure your app in config/environment.rb and config/environments/*.rb | |
3 | ||
4 | | |
4 | RAILS_ROOT = "#{File.dirname(__FILE__)}/.." unless defined?(RAILS_ROOT) | |
6 | | |
7 | | |
8 | | |
6 | module Rails | |
7 | class << self | |
8 | def boot! | |
9 | unless booted? | |
10 | preinitialize | |
11 | pick_boot.run | |
12 | end | |
13 | end | |
14 | ||
15 | def booted? | |
16 | defined? Rails::Initializer | |
17 | end | |
18 | ||
19 | def pick_boot | |
20 | (vendor_rails? ? VendorBoot : GemBoot).new | |
21 | end | |
22 | ||
23 | def vendor_rails? | |
24 | File.exist?("#{RAILS_ROOT}/vendor/rails") | |
25 | end | |
26 | ||
27 | def preinitialize | |
28 | load(preinitializer_path) if File.exist?(preinitializer_path) | |
29 | end | |
30 | ||
31 | def preinitializer_path | |
32 | "#{RAILS_ROOT}/config/preinitializer.rb" | |
33 | end | |
end | ||
11 | | |
12 | ||
36 | class Boot | |
37 | def run | |
38 | load_initializer | |
39 | Rails::Initializer.run(:set_load_path) | |
40 | end | |
41 | end | |
14 | ||
15 | | |
16 | | |
17 | | |
18 | | |
43 | class VendorBoot < Boot | |
44 | def load_initializer | |
45 | require "#{RAILS_ROOT}/vendor/rails/railties/lib/initializer" | |
46 | Rails::Initializer.run(:install_gem_spec_stubs) | |
47 | Rails::GemDependency.add_frozen_gem_path | |
48 | end | |
49 | end | |
20 | | |
21 | | |
22 | | |
51 | class GemBoot < Boot | |
52 | def load_initializer | |
53 | self.class.load_rubygems | |
54 | load_rails_gem | |
55 | require 'initializer' | |
56 | end | |
24 | | |
25 | | |
58 | def load_rails_gem | |
59 | if version = self.class.gem_version | |
60 | gem 'rails', version | |
61 | else | |
62 | gem 'rails' | |
63 | end | |
64 | rescue Gem::LoadError => load_error | |
65 | $stderr.puts %(Missing the Rails #{version} gem. Please `gem install -v=#{version} rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed.) | |
66 | exit 1 | |
67 | end | |
27 | | |
28 | | |
29 | | |
30 | | |
31 | | |
32 | | |
33 | | |
34 | | |
69 | class << self | |
70 | def rubygems_version | |
71 | Gem::RubyGemsVersion rescue nil | |
72 | end | |
73 | ||
74 | def gem_version | |
75 | if defined? RAILS_GEM_VERSION | |
76 | RAILS_GEM_VERSION | |
77 | elsif ENV.include?('RAILS_GEM_VERSION') | |
78 | ENV['RAILS_GEM_VERSION'] | |
79 | else | |
80 | parse_gem_version(read_environment_rb) | |
81 | end | |
82 | end | |
83 | ||
84 | def load_rubygems | |
85 | min_version = '1.3.2' | |
86 | require 'rubygems' | |
87 | unless rubygems_version >= min_version | |
88 | $stderr.puts %Q(Rails requires RubyGems >= #{min_version} (you have #{rubygems_version}). Please `gem update --system` and try again.) | |
89 | exit 1 | |
90 | end | |
91 | ||
92 | rescue LoadError | |
93 | $stderr.puts %Q(Rails requires RubyGems >= #{min_version}. Please install RubyGems and try again: http://rubygems.rubyforge.org) | |
exit 1 | ||
end | ||
37 | | |
38 | | |
39 | | |
96 | ||
97 | def parse_gem_version(text) | |
98 | $1 if text =~ /^[^#]*RAILS_GEM_VERSION\s*=\s*["']([!~<>=]*\s*[\d.]+)["']/ | |
99 | end | |
100 | ||
101 | private | |
102 | def read_environment_rb | |
103 | File.read("#{RAILS_ROOT}/config/environment.rb") | |
104 | end | |
end | ||
end | ||
107 | end | |
43 | | |
44 | ||
109 | # All that for this: | |
110 | Rails.boot! |
rool/rails/hub/trunk/config/environment.rb:
prev. | current | |
# Be sure to restart your web server when you modify this file. | ||
3 | ||
4 | ||
5 | ||
6 | ||
# Specifies gem version of Rails to use when vendor/rails is not present | ||
8 | ||
4 | RAILS_GEM_VERSION = '2.3.11' unless defined? RAILS_GEM_VERSION | |
# Bootstrap the Rails environment, frameworks, and default configuration | ||
require File.join(File.dirname(__FILE__), 'boot') | ||
13 | ||
14 | ||
15 | ||
16 | ||
17 | ||
18 | ||
19 | ||
Rails::Initializer.run do |config| | ||
21 | | |
22 | | |
23 | | |
24 | | |
10 | # Settings in config/environments/* take precedence over those specified here. | |
11 | # Application configuration should go into files in config/initializers | |
12 | # -- all .rb files in that directory are automatically loaded. | |
26 | | |
27 | ||
28 | | |
29 | | |
30 | ||
# Add additional load paths for your own custom dirs | ||
32 | | |
15 | # config.autoload_paths += %W( #{RAILS_ROOT}/extras ) | |
34 | | |
35 | | |
36 | | |
17 | # Mandatory gems | |
18 | # ============== | |
19 | # | |
20 | # Ya2yaml: | |
21 | # http://github.com/ptb/ya2yaml/tree/master | |
22 | # http://rubyforge.org/projects/ya2yaml/ | |
23 | # | |
24 | # Locale: | |
25 | # http://www.yotabanana.com/hiki/ruby-locale-rails.html | |
26 | # http://rubyforge.org/projects/locale/ | |
27 | # | |
28 | # Mislav's Will Paginate: | |
29 | # http://github.com/mislav/will_paginate | |
30 | # sudo gem sources -a http://gems.github.com (if you haven't already) | |
31 | # sudo gem install will_paginate | |
32 | # | |
33 | # Jason King's Good Sort: | |
34 | # http://github.com/JasonKing/good_sort | |
35 | # sudo gem sources -a http://gems.github.com (if you haven't already) | |
36 | # sudo gem install good_sort | |
38 | | |
39 | | |
40 | | |
38 | config.gem 'hubssolib', :version => '>= 0.2.7', :lib => 'hub_sso_lib' | |
39 | config.gem 'ya2yaml', :version => '>= 0.26' | |
40 | config.gem 'locale', :version => '>= 2.0.0' | |
41 | config.gem 'will_paginate', :version => '>= 2.3.15' | |
42 | config.gem 'good_sort', :version => '>= 0.2.4' | |
42 | | |
43 | | |
44 | | |
45 | | |
44 | # Only load the plugins named here, in the order given (default is alphabetical). | |
45 | # :all can be used as a placeholder for all plugins not explicitly named | |
46 | # config.plugins = [ :exception_notification, :ssl_requirement, :all ] | |
48 | # Skip frameworks you're not going to use. To use Rails without a database, | |
49 | # you must remove the Active Record framework. | |
50 | # config.frameworks -= [ :active_record, :active_resource, :action_mailer ] | |
51 | ||
# Activate observers that should always be running | ||
48 | | |
53 | config.active_record.observers = :user_observer | |
50 | | |
51 | | |
55 | # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone. | |
56 | # Run "rake -D time" for a list of tasks for finding time zone names. | |
57 | config.time_zone = 'UTC' | |
53 | | |
59 | # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded. | |
60 | # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}')] | |
61 | # config.i18n.default_locale = :de | |
end | ||
56 | ||
57 | ||
58 | ||
59 | ||
60 | ||
61 | ||
62 | ||
63 | ||
64 | ||
65 | ||
66 | ||
67 | ||
68 | ||
69 | ||
70 | ||
71 | ||
72 | ||
73 | ||
74 | ||
75 | ||
76 | ||
77 | ||
78 | ||
79 | ||
80 | ||
81 | ||
82 | ||
83 | ||
84 | ||
85 | ||
86 | ||
87 | ||
88 | ||
89 | ||
90 | ||
91 | ||
92 | ||
93 | ||
94 | ||
95 | ||
96 | ||
64 | # Please also see "environments/*.rb". |
rool/rails/hub/trunk/config/environments/development.rb:
prev. | current | |
# Log error messages when you accidentally call methods on nil. | ||
config.whiny_nils = true | ||
11 | ||
12 | ||
13 | ||
14 | ||
11 | # Show full error reports and disable caching. | |
config.action_controller.consider_all_requests_local = true | ||
13 | config.action_view.debug_rjs = true | |
config.action_controller.perform_caching = false | ||
17 | ||
18 | ||
20 | ||
21 | ||
16 | config.action_mailer.raise_delivery_errors = true | |
17 | # config.action_mailer.delivery_method = :sendmail | |
18 | ||
19 | # Hub configuration | |
20 | # ================= | |
21 | ||
22 | # Administrator e-mail address to use as the 'From' address in | |
23 | # account notification e-mail messages. | |
24 | EMAIL_ADMIN = 'info@riscosopen.org' | |
25 | ||
26 | # Time limit, *in seconds*, for password reset e-mail codes. The | |
27 | # codes persist in the database indefinitely but will be rejected | |
28 | # if too old when someone tries to use one. | |
29 | RESET_TIME_LIMIT = 2 * 24 * 60 * 60 |
rool/rails/hub/trunk/config/environments/production.rb:
prev. | current | |
# Disable delivery errors if you bad email addresses should just be ignored | ||
# config.action_mailer.raise_delivery_errors = false | ||
19 | ||
20 | # Settings specified here will take precedence over those in config/environment.rb | |
21 | ||
22 | # The production environment is meant for finished, "live" apps. | |
23 | # Code is not reloaded between requests | |
24 | config.cache_classes = true | |
25 | ||
26 | # Full error reports are disabled and caching is turned on | |
27 | config.action_controller.consider_all_requests_local = false | |
28 | config.action_controller.perform_caching = true | |
29 | ||
30 | # See everything in the log (default is :info) | |
31 | # config.log_level = :debug | |
32 | ||
33 | # Use a different logger for distributed setups | |
34 | # config.logger = SyslogLogger.new | |
35 | ||
36 | # Use a different cache store in production | |
37 | # config.cache_store = :mem_cache_store | |
38 | ||
39 | # Enable serving of images, stylesheets, and javascripts from an asset server | |
40 | # config.action_controller.asset_host = "http://assets.example.com" | |
41 | ||
42 | # Disable delivery errors, bad email addresses will be ignored | |
43 | # config.action_mailer.raise_delivery_errors = false | |
44 | ||
45 | config.action_mailer.raise_delivery_errors = true | |
46 | # config.action_mailer.delivery_method = :sendmail | |
47 | ||
48 | # Enable threaded mode | |
49 | # config.threadsafe! | |
50 | ||
51 | # Hub configuration | |
52 | # ================= | |
53 | ||
54 | # Administrator e-mail address to use as the 'From' address in | |
55 | # account notification e-mail messages. | |
56 | EMAIL_ADMIN = 'info@riscosopen.org' | |
57 | ||
58 | # Time limit, *in seconds*, for password reset e-mail codes. The | |
59 | # codes persist in the database indefinitely but will be rejected | |
60 | # if too old when someone tries to use one. | |
61 | RESET_TIME_LIMIT = 2 * 24 * 60 * 60 |
rool/rails/hub/trunk/config/environments/test.rb:
prev. | current | |
config.action_controller.consider_all_requests_local = true | ||
config.action_controller.perform_caching = false | ||
16 | ||
16 | # Disable request forgery protection in test environment | |
17 | config.action_controller.allow_forgery_protection = false | |
18 | ||
19 | # Tell Action Mailer not to deliver emails to the real world. | |
# The :test delivery method accumulates sent emails in the | ||
# ActionMailer::Base.deliveries array. | ||
19 | ||
23 | config.action_mailer.delivery_method = :test | |
24 | ||
25 | # Use SQL instead of Active Record's schema dumper when creating the test database. | |
26 | # This is necessary if your schema can't be completely dumped by the schema dumper, | |
27 | # like if you have constraints or database-specific column types | |
28 | # config.active_record.schema_format = :sql | |
29 | ||
30 | # Mandatory gems | |
31 | # ============== | |
32 | # | |
33 | # Lorem (for the bulk product creation script in test/tools) | |
34 | # http://github.com/jnunemaker/lorem | |
35 | # | |
36 | # Webrat (for tests) | |
37 | # http://github.com/brynary/webrat | |
38 | # | |
39 | # Launchy (so webrat can send error pages to your web browser) | |
40 | # http://copiousfreetime.rubyforge.org/launchy/ | |
41 | ||
42 | config.gem 'lorem', :version => '>= 0.1.2' | |
43 | config.gem 'webrat', :version => '>= 0.7.1' | |
44 | ||
45 | # Optional gems | |
46 | # ============= | |
47 | # | |
48 | # Launchy (so webrat can send error pages to your web browser) | |
49 | # http://copiousfreetime.rubyforge.org/launchy/ | |
50 | ||
51 | config.gem 'launchy', :version => '>= 0.3.5' | |
52 | ||
53 | # Hub configuration | |
54 | # ================= | |
55 | # | |
56 | # See development.rb or production.rb for details. | |
57 | ||
58 | EMAIL_ADMIN = 'example@test.com' | |
59 | RESET_TIME_LIMIT = 2 * 24 * 60 * 60 |
rool/rails/hub/trunk/config/routes.rb:
prev. | current | |
1 | ||
2 | | |
3 | | |
1 | ActionController::Routing::Routes.draw do | map | | |
2 | map.root :controller => :tasks, :action => :index | |
3 | map.resources :stylesheets, :only => :show | |
4 | map.connect '/:controller/:action/:id' | |
end |
rool/rails/hub/trunk/db/migrate/001_create_users.rb:
prev. | current | |
drop_table "users" | ||
end | ||
end | ||
22 |
rool/rails/hub/trunk/db/schema.rb:
prev. | current | |
1 | ||
2 | ||
1 | # This file is auto-generated from the current state of the database. Instead of editing this file, | |
2 | # please use the migrations feature of Active Record to incrementally modify your database, and | |
# then regenerate this schema definition. | ||
4 | # | |
5 | # Note that this schema.rb definition is the authoritative source for your database schema. If you need | |
6 | # to create the application database on another system, you should be using db:schema:load, not running | |
7 | # all the migrations from scratch. The latter is a flawed and unsustainable approach (the more migrations | |
8 | # you'll amass, the slower it'll run and the greater likelihood for issues). | |
9 | # | |
10 | # It's strongly recommended to check this file into your version control system. | |
ActiveRecord::Schema.define(:version => 3) do | ||
create_table "users", :force => true do |t| | ||
8 | | |
9 | | |
10 | | |
11 | | |
12 | | |
13 | | |
14 | | |
15 | | |
16 | | |
17 | | |
18 | | |
19 | | |
20 | | |
21 | | |
15 | t.string "email" | |
16 | t.string "crypted_password", :limit => 40 | |
17 | t.string "salt", :limit => 40 | |
18 | t.datetime "created_at" | |
19 | t.datetime "updated_at" | |
20 | t.string "remember_token" | |
21 | t.datetime "remember_token_expires_at" | |
22 | t.string "activation_code", :limit => 40 | |
23 | t.datetime "activated_at" | |
24 | t.string "roles" | |
25 | t.integer "member_id" | |
26 | t.string "password_reset_code", :limit => 40 | |
27 | t.datetime "password_reset_code_expires_at" | |
28 | t.string "real_name", :limit => 128 | |
end | ||
end |
rool/rails/hub/trunk/doc/README_FOR_APP:
prev. | current | |
1 | ||
2 | ||
2 | Hub consists of this Ruby On Rails application for managing user accounts | |
3 | including the sign-up and sign-in process, a DRb server which remembers which | |
4 | users are logged in at run-time and provides this information over a Unix | |
5 | domain socket to other Hub-integrated applications, and a gem which provides | |
6 | library facilities which make it easy to talk to the DRb server. | |
7 | ||
8 | For more information about Hub, the Hub DRb server and the Hub gem, please see: | |
9 | ||
10 | * http://hub.pond.org.uk/ |
rool/rails/hub/trunk/public/404.html:
prev. | current | |
1 | ||
2 | | |
3 | ||
1 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" | |
2 | "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> | |
3 | ||
4 | <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> | |
5 | ||
6 | <head> | |
7 | <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> | |
8 | <title>The page you were looking for doesn't exist (404)</title> | |
9 | <style type="text/css"> | |
10 | body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; } | |
11 | div.dialog { | |
12 | width: 25em; | |
13 | padding: 0 4em; | |
14 | margin: 4em auto 0 auto; | |
15 | border: 1px solid #ccc; | |
16 | border-right-color: #999; | |
17 | border-bottom-color: #999; | |
18 | } | |
19 | h1 { font-size: 100%; color: #f00; line-height: 1.5em; } | |
20 | </style> | |
21 | </head> | |
22 | ||
<body> | ||
5 | | |
6 | | |
24 | <!-- This file lives in public/404.html --> | |
25 | <div class="dialog"> | |
26 | <h1>The page you were looking for doesn't exist.</h1> | |
27 | <p>You may have mistyped the address or the page may have moved.</p> | |
28 | </div> | |
29 | ||
30 | <p /> | |
31 | <div align="center"> | |
32 | <strong><a href="/">Go to the Home page</a></strong> | |
33 | </div> | |
</body> | ||
</html> | ||
rool/rails/hub/trunk/public/500.html:
prev. | current | |
1 | ||
2 | | |
3 | ||
1 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" | |
2 | "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> | |
3 | ||
4 | <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> | |
5 | ||
6 | <head> | |
7 | <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> | |
8 | <title>We're sorry, but something went wrong (500)</title> | |
9 | <style type="text/css"> | |
10 | body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; } | |
11 | div.dialog { | |
12 | width: 25em; | |
13 | padding: 0 4em; | |
14 | margin: 4em auto 0 auto; | |
15 | border: 1px solid #ccc; | |
16 | border-right-color: #999; | |
17 | border-bottom-color: #999; | |
18 | } | |
19 | h1 { font-size: 100%; color: #f00; line-height: 1.5em; } | |
20 | </style> | |
21 | </head> | |
22 | ||
<body> | ||
5 | | |
6 | | |
24 | <!-- This file lives in public/500.html --> | |
25 | <div class="dialog"> | |
26 | <h1>We're sorry, but something went wrong.</h1> | |
27 | <p>We've been notified about this issue and we'll take a look at it shortly.</p> | |
28 | <p><small>(If you're the administrator of this website, then please read | |
29 | the log file "<%= "<%s>" % "%=h RAILS_ENV %" %>.log" | |
30 | to find out what went wrong.)</small></p> | |
31 | </div> | |
32 | ||
33 | <p /> | |
34 | <div align="center"> | |
35 | <strong><a href="/">Go to the Home page</a></strong> | |
36 | </div> | |
</body> | ||
</html> | ||
rool/rails/hub/trunk/public/javascripts/application.js:
prev. | current | |
1 | ||
2 | ||
1 | /* | |
2 | * Registers a callback which copies the csrf token into the | |
3 | * X-CSRF-Token header with each ajax request. Necessary to | |
4 | * work with rails applications which have fixed | |
5 | * CVE-2011-0447 | |
6 | */ | |
7 | ||
8 | Ajax.Responders.register({ | |
9 | onCreate: function(request) { | |
10 | var csrf_meta_tag = $$('meta[name=csrf-token]')[0]; | |
11 | ||
12 | if (csrf_meta_tag) { | |
13 | var header = 'X-CSRF-Token', | |
14 | token = csrf_meta_tag.readAttribute('content'); | |
15 | ||
16 | if (!request.options.requestHeaders) { | |
17 | request.options.requestHeaders = {}; | |
18 | } | |
19 | request.options.requestHeaders[header] = token; | |
20 | } | |
21 | } | |
22 | }); | |
rool/rails/hub/trunk/public/javascripts/controls.js:
prev. | current | |
1 | ||
2 | ||
3 | ||
1 | // Copyright (c) 2005-2008 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us) | |
2 | // (c) 2005-2008 Ivan Krstic (http://blogs.law.harvard.edu/ivan) | |
3 | // (c) 2005-2008 Jon Tirsen (http://www.tirsen.com) | |
// Contributors: | ||
// Richard Livsey | ||
// Rahul Bhargava | ||
// Rob Wills | ||
8 | ||
9 | ||
8 | // | |
9 | // script.aculo.us is freely distributable under the terms of an MIT-style license. | |
10 | // For details, see the script.aculo.us web site: http://script.aculo.us/ | |
11 | ||
12 | // Autocompleter.Base handles all the autocompletion functionality | |
// that's independent of the data source for autocompletion. This | ||
// includes drawing the autocompletion menu, observing keyboard | ||
// and mouse events, and similar. | ||
// | ||
16 | ||
17 | // Specific autocompleters need to provide, at the very least, | |
// a getUpdatedChoices function that will be invoked every time | ||
18 | ||
19 | // the text inside the monitored textbox changes. This method | |
// should get the text for which to provide autocompletion by | ||
// invoking this.getToken(), NOT by directly accessing | ||
// this.element.value. This is to allow incremental tokenized | ||
... | ... | |
// will incrementally autocomplete with a comma as the token. | ||
// Additionally, ',' in the above example can be replaced with | ||
// a token array, e.g. { tokens: [',', '\n'] } which | ||
32 | ||
33 | ||
33 | // enables autocompletion on multiple tokens. This is most | |
34 | // useful when one of the tokens is \n (a newline), as it | |
// allows smart autocompletion after linebreaks. | ||
36 | ||
37 | ||
38 | ||
37 | if(typeof Effect == 'undefined') | |
38 | throw("controls.js requires including script.aculo.us' effects.js library"); | |
39 | ||
40 | var Autocompleter = { }; | |
41 | Autocompleter.Base = Class.create({ | |
baseInitialize: function(element, update, options) { | ||
40 | | |
41 | | |
42 | | |
43 | | |
44 | | |
45 | | |
43 | element = $(element); | |
44 | this.element = element; | |
45 | this.update = $(update); | |
46 | this.hasFocus = false; | |
47 | this.changed = false; | |
48 | this.active = false; | |
49 | this.index = 0; | |
this.entryCount = 0; | ||
51 | this.oldElementValue = this.element.value; | |
48 | | |
53 | if(this.setOptions) | |
this.setOptions(options); | ||
else | ||
51 | | |
56 | this.options = options || { }; | |
this.options.paramName = this.options.paramName || this.element.name; | ||
this.options.tokens = this.options.tokens || []; | ||
this.options.frequency = this.options.frequency || 0.4; | ||
this.options.minChars = this.options.minChars || 1; | ||
57 | | |
58 | | |
59 | | |
60 | | |
61 | | |
62 | | |
63 | | |
64 | | |
65 | | |
66 | | |
62 | this.options.onShow = this.options.onShow || | |
63 | function(element, update){ | |
64 | if(!update.style.position || update.style.position=='absolute') { | |
65 | update.style.position = 'absolute'; | |
66 | Position.clone(element, update, { | |
67 | setHeight: false, | |
68 | offsetTop: element.offsetHeight | |
69 | }); | |
70 | } | |
71 | Effect.Appear(update,{duration:0.15}); | |
72 | }; | |
73 | this.options.onHide = this.options.onHide || | |
74 | function(element, update){ new Effect.Fade(update,{duration:0.15}) }; | |
68 | | |
76 | if(typeof(this.options.tokens) == 'string') | |
this.options.tokens = new Array(this.options.tokens); | ||
78 | // Force carriage returns as token delimiters anyway | |
79 | if (!this.options.tokens.include('\n')) | |
80 | this.options.tokens.push('\n'); | |
this.observer = null; | ||
72 | | |
83 | ||
this.element.setAttribute('autocomplete','off'); | ||
Element.hide(this.update); | ||
77 | | |
78 | | |
88 | Event.observe(this.element, 'blur', this.onBlur.bindAsEventListener(this)); | |
89 | Event.observe(this.element, 'keydown', this.onKeyPress.bindAsEventListener(this)); | |
}, | ||
show: function() { | ||
if(Element.getStyle(this.update, 'display')=='none') this.options.onShow(this.element, this.update); | ||
83 | | |
84 | | |
85 | | |
94 | if(!this.iefix && | |
95 | (Prototype.Browser.IE) && | |
(Element.getStyle(this.update, 'position')=='absolute')) { | ||
87 | | |
97 | new Insertion.After(this.update, | |
'<iframe id="' + this.update.id + '_iefix" '+ | ||
'style="display:none;position:absolute;filter:progid:DXImageTransform.Microsoft.Alpha(opacity=0);" ' + | ||
'src="javascript:false;" frameborder="0" scrolling="no"></iframe>'); | ||
... | ... | |
} | ||
if(this.iefix) setTimeout(this.fixIEOverlapping.bind(this), 50); | ||
}, | ||
95 | | |
105 | ||
fixIEOverlapping: function() { | ||
97 | | |
107 | Position.clone(this.update, this.iefix, {setTop:(!this.update.style.height)}); | |
this.iefix.style.zIndex = 1; | ||
this.update.style.zIndex = 2; | ||
Element.show(this.iefix); | ||
... | ... | |
case Event.KEY_UP: | ||
this.markPrevious(); | ||
this.render(); | ||
135 | | |
145 | Event.stop(event); | |
return; | ||
case Event.KEY_DOWN: | ||
this.markNext(); | ||
this.render(); | ||
140 | | |
150 | Event.stop(event); | |
return; | ||
} | ||
143 | | |
144 | | |
145 | | |
153 | else | |
154 | if(event.keyCode==Event.KEY_TAB || event.keyCode==Event.KEY_RETURN || | |
155 | (Prototype.Browser.WebKit > 0 && event.keyCode == 0)) return; | |
this.changed = true; | ||
this.hasFocus = true; | ||
if(this.observer) clearTimeout(this.observer); | ||
151 | | |
161 | this.observer = | |
setTimeout(this.onObserverEvent.bind(this), this.options.frequency*1000); | ||
}, | ||
... | ... | |
onHover: function(event) { | ||
var element = Event.findElement(event, 'LI'); | ||
163 | | |
173 | if(this.index != element.autocompleteIndex) | |
{ | ||
this.index = element.autocompleteIndex; | ||
this.render(); | ||
} | ||
Event.stop(event); | ||
}, | ||
170 | | |
180 | ||
onClick: function(event) { | ||
var element = Event.findElement(event, 'LI'); | ||
this.index = element.autocompleteIndex; | ||
this.selectEntry(); | ||
this.hide(); | ||
}, | ||
177 | | |
187 | ||
onBlur: function(event) { | ||
// needed to make click events working | ||
setTimeout(this.hide.bind(this), 250); | ||
this.hasFocus = false; | ||
182 | | |
183 | | |
184 | | |
192 | this.active = false; | |
193 | }, | |
194 | ||
render: function() { | ||
if(this.entryCount > 0) { | ||
for (var i = 0; i < this.entryCount; i++) | ||
188 | | |
189 | | |
198 | this.index==i ? | |
199 | Element.addClassName(this.getEntry(i),"selected") : | |
Element.removeClassName(this.getEntry(i),"selected"); | ||
191 | | |
192 | | |
201 | if(this.hasFocus) { | |
this.show(); | ||
this.active = true; | ||
} | ||
... | ... | |
this.hide(); | ||
} | ||
}, | ||
201 | | |
210 | ||
markPrevious: function() { | ||
203 | | |
212 | if(this.index > 0) this.index--; | |
else this.index = this.entryCount-1; | ||
214 | this.getEntry(this.index).scrollIntoView(true); | |
}, | ||
206 | | |
216 | ||
markNext: function() { | ||
208 | | |
218 | if(this.index < this.entryCount-1) this.index++; | |
else this.index = 0; | ||
220 | this.getEntry(this.index).scrollIntoView(false); | |
}, | ||
211 | | |
222 | ||
getEntry: function(index) { | ||
return this.update.firstChild.childNodes[index]; | ||
}, | ||
215 | | |
226 | ||
getCurrentEntry: function() { | ||
return this.getEntry(this.index); | ||
}, | ||
219 | | |
230 | ||
selectEntry: function() { | ||
this.active = false; | ||
this.updateElement(this.getCurrentEntry()); | ||
... | ... | |
} | ||
var value = ''; | ||
if (this.options.select) { | ||
232 | | |
243 | var nodes = $(selectedElement).select('.' + this.options.select) || []; | |
if(nodes.length>0) value = Element.collectTextNodes(nodes[0], this.options.select); | ||
} else | ||
value = Element.collectTextNodesIgnoreClass(selectedElement, 'informal'); | ||
236 | | |
237 | | |
238 | | |
239 | | |
240 | | |
247 | ||
248 | var bounds = this.getTokenBounds(); | |
249 | if (bounds[0] != -1) { | |
250 | var newValue = this.element.value.substr(0, bounds[0]); | |
251 | var whitespace = this.element.value.substr(bounds[0]).match(/^\s+/); | |
if (whitespace) | ||
newValue += whitespace[0]; | ||
243 | | |
254 | this.element.value = newValue + value + this.element.value.substr(bounds[1]); | |
} else { | ||
this.element.value = value; | ||
} | ||
258 | this.oldElementValue = this.element.value; | |
this.element.focus(); | ||
248 | | |
260 | ||
if (this.options.afterUpdateElement) | ||
this.options.afterUpdateElement(this.element, selectedElement); | ||
}, | ||
... | ... | |
if(!this.changed && this.hasFocus) { | ||
this.update.innerHTML = choices; | ||
Element.cleanWhitespace(this.update); | ||
257 | | |
269 | Element.cleanWhitespace(this.update.down()); | |
259 | | |
260 | | |
261 | | |
271 | if(this.update.firstChild && this.update.down().childNodes) { | |
272 | this.entryCount = | |
273 | this.update.down().childNodes.length; | |
for (var i = 0; i < this.entryCount; i++) { | ||
var entry = this.getEntry(i); | ||
entry.autocompleteIndex = i; | ||
this.addObservers(entry); | ||
} | ||
267 | | |
279 | } else { | |
this.entryCount = 0; | ||
} | ||
this.stopIndicator(); | ||
284 | this.index = 0; | |
273 | | |
274 | | |
286 | if(this.entryCount==1 && this.options.autoSelect) { | |
287 | this.selectEntry(); | |
288 | this.hide(); | |
289 | } else { | |
290 | this.render(); | |
291 | } | |
} | ||
}, | ||
... | ... | |
}, | ||
onObserverEvent: function() { | ||
284 | | |
301 | this.changed = false; | |
302 | this.tokenBounds = null; | |
if(this.getToken().length>=this.options.minChars) { | ||
286 | | |
this.getUpdatedChoices(); | ||
} else { | ||
this.active = false; | ||
this.hide(); | ||
} | ||
309 | this.oldElementValue = this.element.value; | |
}, | ||
getToken: function() { | ||
295 | | |
296 | | |
297 | | |
298 | | |
299 | | |
300 | ||
301 | | |
313 | var bounds = this.getTokenBounds(); | |
314 | return this.element.value.substring(bounds[0], bounds[1]).strip(); | |
}, | ||
304 | | |
305 | | |
306 | ||
307 | | |
308 | | |
309 | | |
310 | | |
317 | getTokenBounds: function() { | |
318 | if (null != this.tokenBounds) return this.tokenBounds; | |
319 | var value = this.element.value; | |
320 | if (value.strip().empty()) return [-1, 0]; | |
321 | var diff = arguments.callee.getFirstDifferencePos(value, this.oldElementValue); | |
322 | var offset = (diff == this.oldElementValue.length ? 1 : 0); | |
323 | var prevTokenPos = -1, nextTokenPos = value.length; | |
324 | var tp; | |
325 | for (var index = 0, l = this.options.tokens.length; index < l; ++index) { | |
326 | tp = value.lastIndexOf(this.options.tokens[index], diff + offset - 1); | |
327 | if (tp > prevTokenPos) prevTokenPos = tp; | |
328 | tp = value.indexOf(this.options.tokens[index], diff + offset); | |
329 | if (-1 != tp && tp < nextTokenPos) nextTokenPos = tp; | |
} | ||
312 | | |
331 | return (this.tokenBounds = [prevTokenPos + 1, nextTokenPos]); | |
} | ||
314 | ||
333 | }); | |
316 | ||
317 | ||
335 | Autocompleter.Base.prototype.getTokenBounds.getFirstDifferencePos = function(newS, oldS) { | |
336 | var boundary = Math.min(newS.length, oldS.length); | |
337 | for (var index = 0; index < boundary; ++index) | |
338 | if (newS[index] != oldS[index]) | |
339 | return index; | |
340 | return boundary; | |
341 | }; | |
342 | ||
343 | Ajax.Autocompleter = Class.create(Autocompleter.Base, { | |
initialize: function(element, update, url, options) { | ||
this.baseInitialize(element, update, options); | ||
this.options.asynchronous = true; | ||
... | ... | |
}, | ||
getUpdatedChoices: function() { | ||
327 | | |
353 | this.startIndicator(); | |
354 | ||
355 | var entry = encodeURIComponent(this.options.paramName) + '=' + | |
encodeURIComponent(this.getToken()); | ||
this.options.parameters = this.options.callback ? | ||
this.options.callback(this.element, entry) : entry; | ||
333 | | |
361 | if(this.options.defaultParams) | |
this.options.parameters += '&' + this.options.defaultParams; | ||
new Ajax.Request(this.url, this.options); | ||
... | ... | |
onComplete: function(request) { | ||
this.updateChoices(request.responseText); | ||
} | ||
342 | ||
}); | ||
// The local array autocompleter. Used when you'd prefer to | ||
... | ... | |
// - choices - How many autocompletion choices to offer | ||
// | ||
// - partialSearch - If false, the autocompleter will match entered | ||
358 | ||
385 | // text only at the beginning of strings in the | |
// autocomplete array. Defaults to true, which will | ||
// match text at the beginning of any *word* in the | ||
// strings in the autocomplete array. If you want to | ||
... | ... | |
// - ignoreCase - Whether to ignore case when autocompleting. | ||
// Defaults to true. | ||
// | ||
375 | ||
402 | // It's possible to pass in a custom function as the 'selector' | |
// option, if you prefer to write your own autocompletion logic. | ||
// In that case, the other options above will not apply unless | ||
// you support them. | ||
380 | ||
381 | ||
407 | Autocompleter.Local = Class.create(Autocompleter.Base, { | |
initialize: function(element, update, array, options) { | ||
this.baseInitialize(element, update, options); | ||
this.options.array = array; | ||
... | ... | |
var entry = instance.getToken(); | ||
var count = 0; | ||
404 | | |
405 | | |
430 | for (var i = 0; i < instance.options.array.length && | |
431 | ret.length < instance.options.choices ; i++) { | |
var elem = instance.options.array[i]; | ||
408 | | |
409 | | |
434 | var foundPos = instance.options.ignoreCase ? | |
435 | elem.toLowerCase().indexOf(entry.toLowerCase()) : | |
elem.indexOf(entry); | ||
while (foundPos != -1) { | ||
413 | | |
414 | | |
439 | if (foundPos == 0 && elem.length != entry.length) { | |
440 | ret.push("<li><strong>" + elem.substr(0, entry.length) + "</strong>" + | |
elem.substr(entry.length) + "</li>"); | ||
break; | ||
417 | | |
443 | } else if (entry.length >= instance.options.partialChars && | |
instance.options.partialSearch && foundPos != -1) { | ||
if (instance.options.fullSearch || /\s/.test(elem.substr(foundPos-1,1))) { | ||
partial.push("<li>" + elem.substr(0, foundPos) + "<strong>" + | ||
... | ... | |
} | ||
} | ||
427 | | |
428 | | |
453 | foundPos = instance.options.ignoreCase ? | |
454 | elem.toLowerCase().indexOf(entry.toLowerCase(), foundPos + 1) : | |
elem.indexOf(entry, foundPos + 1); | ||
} | ||
} | ||
if (partial.length) | ||
434 | | |
460 | ret = ret.concat(partial.slice(0, instance.options.choices - ret.length)); | |
return "<ul>" + ret.join('') + "</ul>"; | ||
} | ||
437 | | |
463 | }, options || { }); | |
} | ||
}); | ||
441 | ||
442 | ||
443 | ||
467 | // AJAX in-place editor and collection editor | |
468 | // Full rewrite by Christophe Porteneuve <tdd@tddsworld.com> (April 2007). | |
// Use this if you notice weird scrolling problems on some browsers, | ||
// the DOM might be a bit confused when this gets called so do this | ||
... | ... | |
setTimeout(function() { | ||
Field.activate(field); | ||
}, 1); | ||
452 | ||
477 | }; | |
454 | ||
455 | ||
456 | ||
479 | Ajax.InPlaceEditor = Class.create({ | |
initialize: function(element, url, options) { | ||
this.url = url; | ||
459 | | |
460 | ||
461 | | |
462 | | |
463 | | |
464 | | |
465 | | |
466 | | |
467 | | |
468 | | |
469 | | |
470 | | |
471 | | |
472 | | |
473 | | |
474 | | |
475 | | |
476 | | |
477 | | |
478 | | |
479 | | |
480 | | |
481 | | |
482 | | |
483 | | |
484 | | |
485 | | |
486 | | |
487 | | |
488 | | |
489 | | |
490 | | |
491 | ||
492 | | |
493 | | |
494 | | |
495 | | |
496 | | |
497 | | |
482 | this.element = element = $(element); | |
483 | this.prepareOptions(); | |
484 | this._controls = { }; | |
485 | arguments.callee.dealWithDeprecatedOptions(options); // DEPRECATION LAYER!!! | |
486 | Object.extend(this.options, options || { }); | |
487 | if (!this.options.formId && this.element.id) { | |
488 | this.options.formId = this.element.id + '-inplaceeditor'; | |
489 | if ($(this.options.formId)) | |
490 | this.options.formId = ''; | |
} | ||
499 | | |
500 | | |
492 | if (this.options.externalControl) | |
this.options.externalControl = $(this.options.externalControl); | ||
502 | | |
503 | | |
504 | | |
505 | | |
506 | | |
507 | | |
508 | | |
494 | if (!this.options.externalControl) | |
495 | this.options.externalControlOnly = false; | |
496 | this._originalBackground = this.element.getStyle('background-color') || 'transparent'; | |
this.element.title = this.options.clickToEditText; | ||
510 | | |
511 | | |
512 | | |
513 | | |
514 | | |
515 | | |
516 | | |
517 | | |
518 | | |
519 | | |
520 | | |
521 | | |
498 | this._boundCancelHandler = this.handleFormCancellation.bind(this); | |
499 | this._boundComplete = (this.options.onComplete || Prototype.emptyFunction).bind(this); | |
500 | this._boundFailureHandler = this.handleAJAXFailure.bind(this); | |
501 | this._boundSubmitHandler = this.handleFormSubmission.bind(this); | |
502 | this._boundWrapperHandler = this.wrapUp.bind(this); | |
503 | this.registerListeners(); | |
}, | ||
523 | | |
524 | | |
525 | | |
526 | | |
527 | | |
528 | | |
529 | | |
530 | | |
531 | | |
532 | | |
533 | | |
534 | | |
535 | | |
536 | | |
537 | | |
538 | | |
539 | | |
505 | checkForEscapeOrReturn: function(e) { | |
506 | if (!this._editing || e.ctrlKey || e.altKey || e.shiftKey) return; | |
507 | if (Event.KEY_ESC == e.keyCode) | |
508 | this.handleFormCancellation(e); | |
509 | else if (Event.KEY_RETURN == e.keyCode) | |
510 | this.handleFormSubmission(e); | |
}, | ||
541 | | |
542 | | |
543 | | |
544 | | |
545 | | |
546 | ||
547 | | |
548 | ||
549 | | |
550 | | |
551 | | |
512 | createControl: function(mode, handler, extraClasses) { | |
513 | var control = this.options[mode + 'Control']; | |
514 | var text = this.options[mode + 'Text']; | |
515 | if ('button' == control) { | |
516 | var btn = document.createElement('input'); | |
517 | btn.type = 'submit'; | |
518 | btn.value = text; | |
519 | btn.className = 'editor_' + mode + '_button'; | |
520 | if ('cancel' == mode) | |
521 | btn.onclick = this._boundCancelHandler; | |
522 | this._form.appendChild(btn); | |
523 | this._controls[mode] = btn; | |
524 | } else if ('link' == control) { | |
525 | var link = document.createElement('a'); | |
526 | link.href = '#'; | |
527 | link.appendChild(document.createTextNode(text)); | |
528 | link.onclick = 'cancel' == mode ? this._boundCancelHandler : this._boundSubmitHandler; | |
529 | link.className = 'editor_' + mode + '_link'; | |
530 | if (extraClasses) | |
531 | link.className += ' ' + extraClasses; | |
532 | this._form.appendChild(link); | |
533 | this._controls[mode] = link; | |
} | ||
553 | ||
554 | | |
555 | | |
556 | | |
557 | | |
558 | | |
559 | | |
560 | | |
561 | ||
562 | | |
563 | | |
564 | | |
565 | | |
566 | | |
567 | | |
568 | | |
569 | | |
}, | ||
571 | | |
572 | | |
573 | | |
574 | | |
575 | | |
576 | | |
577 | | |
createEditField: function() { | ||
579 | | |
580 | | |
581 | | |
582 | | |
583 | | |
584 | | |
585 | ||
586 | | |
587 | | |
588 | | |
589 | | |
590 | | |
591 | | |
592 | | |
593 | | |
594 | | |
595 | | |
596 | | |
537 | var text = (this.options.loadTextURL ? this.options.loadingText : this.getText()); | |
538 | var fld; | |
539 | if (1 >= this.options.rows && !/\r|\n/.test(this.getText())) { | |
540 | fld = document.createElement('input'); | |
541 | fld.type = 'text'; | |
var size = this.options.size || this.options.cols || 0; | ||
598 | | |
599 | | |
600 | | |
601 | | |
543 | if (0 < size) fld.size = size; | |
} else { | ||
603 | | |
604 | | |
605 | | |
606 | | |
607 | | |
608 | | |
609 | | |
610 | | |
611 | | |
612 | | |
613 | | |
545 | fld = document.createElement('textarea'); | |
546 | fld.rows = (1 >= this.options.rows ? this.options.autoRows : this.options.rows); | |
547 | fld.cols = this.options.cols || 40; | |
} | ||
615 | | |
616 | | |
549 | fld.name = this.options.paramName; | |
550 | fld.value = text; // No HTML breaks conversion anymore | |
551 | fld.className = 'editor_field'; | |
552 | if (this.options.submitOnBlur) | |
553 | fld.onblur = this._boundSubmitHandler; | |
554 | this._controls.editor = fld; | |
555 | if (this.options.loadTextURL) | |
this.loadExternalText(); | ||
618 | | |
619 | | |
557 | this._form.appendChild(this._controls.editor); | |
}, | ||
621 | | |
622 | | |
559 | createForm: function() { | |
560 | var ipe = this; | |
561 | function addText(mode, condition) { | |
562 | var text = ipe.options['text' + mode + 'Controls']; | |
563 | if (!text || condition === false) return; | |
564 | ipe._form.appendChild(document.createTextNode(text)); | |
565 | }; | |
566 | this._form = $(document.createElement('form')); | |
567 | this._form.id = this.options.formId; | |
568 | this._form.addClassName(this.options.formClassName); | |
569 | this._form.onsubmit = this._boundSubmitHandler; | |
570 | this.createEditField(); | |
571 | if ('textarea' == this._controls.editor.tagName.toLowerCase()) | |
572 | this._form.appendChild(document.createElement('br')); | |
573 | if (this.options.onFormCustomization) | |
574 | this.options.onFormCustomization(this, this._form); | |
575 | addText('Before', this.options.okControl || this.options.cancelControl); | |
576 | this.createControl('ok', this._boundSubmitHandler); | |
577 | addText('Between', this.options.okControl && this.options.cancelControl); | |
578 | this.createControl('cancel', this._boundCancelHandler, 'editor_cancel'); | |
579 | addText('After', this.options.okControl || this.options.cancelControl); | |
}, | ||
624 | | |
625 | | |
626 | | |
627 | | |
628 | | |
629 | | |
630 | | |
631 | | |
632 | | |
633 | | |
581 | destroy: function() { | |
582 | if (this._oldInnerHTML) | |
583 | this.element.innerHTML = this._oldInnerHTML; | |
584 | this.leaveEditMode(); | |
585 | this.unregisterListeners(); | |
}, | ||
635 | | |
636 | | |
637 | | |
638 | | |
587 | enterEditMode: function(e) { | |
588 | if (this._saving || this._editing) return; | |
589 | this._editing = true; | |
590 | this.triggerCallback('onEnterEditMode'); | |
591 | if (this.options.externalControl) | |
592 | this.options.externalControl.hide(); | |
593 | this.element.hide(); | |
594 | this.createForm(); | |
595 | this.element.parentNode.insertBefore(this._form, this.element); | |
596 | if (!this.options.loadTextURL) | |
597 | this.postProcessEditField(); | |
598 | if (e) Event.stop(e); | |
}, | ||
640 | | |
641 | | |
642 | | |
643 | | |
600 | enterHover: function(e) { | |
601 | if (this.options.hoverClassName) | |
602 | this.element.addClassName(this.options.hoverClassName); | |
603 | if (this._saving) return; | |
604 | this.triggerCallback('onEnterHover'); | |
}, | ||
645 | | |
646 | | |
647 | | |
648 | | |
649 | | |
606 | getText: function() { | |
607 | return this.element.innerHTML.unescapeHTML(); | |
608 | }, | |
609 | handleAJAXFailure: function(transport) { | |
610 | this.triggerCallback('onFailure', transport); | |
611 | if (this._oldInnerHTML) { | |
612 | this.element.innerHTML = this._oldInnerHTML; | |
613 | this._oldInnerHTML = null; | |
} | ||
651 | | |
}, | ||
653 | | |
654 | | |
655 | | |
656 | | |
657 | | |
658 | | |
659 | | |
660 | | |
661 | | |
662 | | |
663 | | |
664 | | |
665 | | |
666 | | |
667 | | |
668 | | |
669 | | |
670 | | |
671 | | |
672 | | |
673 | | |
674 | | |
675 | | |
676 | | |
677 | | |
678 | | |
679 | | |
680 | | |
681 | | |
616 | handleFormCancellation: function(e) { | |
617 | this.wrapUp(); | |
618 | if (e) Event.stop(e); | |
619 | }, | |
620 | handleFormSubmission: function(e) { | |
621 | var form = this._form; | |
622 | var value = $F(this._controls.editor); | |
623 | this.prepareSubmission(); | |
624 | var params = this.options.callback(form, value) || ''; | |
625 | if (Object.isString(params)) | |
626 | params = params.toQueryParams(); | |
627 | params.editorId = this.element.id; | |
628 | if (this.options.htmlResponse) { | |
629 | var options = Object.extend({ evalScripts: true }, this.options.ajaxOptions); | |
630 | Object.extend(options, { | |
631 | parameters: params, | |
632 | onComplete: this._boundWrapperHandler, | |
633 | onFailure: this._boundFailureHandler | |
634 | }); | |
635 | new Ajax.Updater({ success: this.element }, this.url, options); | |
636 | } else { | |
637 | var options = Object.extend({ method: 'get' }, this.options.ajaxOptions); | |
638 | Object.extend(options, { | |
639 | parameters: params, | |
640 | onComplete: this._boundWrapperHandler, | |
641 | onFailure: this._boundFailureHandler | |
642 | }); | |
643 | new Ajax.Request(this.url, options); | |
} | ||
683 | | |
684 | | |
685 | | |
686 | | |
687 | | |
645 | if (e) Event.stop(e); | |
}, | ||
689 | | |
690 | | |
647 | leaveEditMode: function() { | |
648 | this.element.removeClassName(this.options.savingClassName); | |
this.removeForm(); | ||
this.leaveHover(); | ||
651 | this.element.style.backgroundColor = this._originalBackground; | |
652 | this.element.show(); | |
653 | if (this.options.externalControl) | |
654 | this.options.externalControl.show(); | |
655 | this._saving = false; | |
656 | this._editing = false; | |
657 | this._oldInnerHTML = null; | |
658 | this.triggerCallback('onLeaveEditMode'); | |
659 | }, | |
660 | leaveHover: function(e) { | |
661 | if (this.options.hoverClassName) | |
662 | this.element.removeClassName(this.options.hoverClassName); | |
663 | if (this._saving) return; | |
664 | this.triggerCallback('onLeaveHover'); | |
665 | }, | |
666 | loadExternalText: function() { | |
667 | this._form.addClassName(this.options.loadingClassName); | |
668 | this._controls.editor.disabled = true; | |
669 | var options = Object.extend({ method: 'get' }, this.options.ajaxOptions); | |
670 | Object.extend(options, { | |
671 | parameters: 'editorId=' + encodeURIComponent(this.element.id), | |
672 | onComplete: Prototype.emptyFunction, | |
673 | onSuccess: function(transport) { | |
674 | this._form.removeClassName(this.options.loadingClassName); | |
675 | var text = transport.responseText; | |
676 | if (this.options.stripLoadedTextTags) | |
677 | text = text.stripTags(); | |
678 | this._controls.editor.value = text; | |
679 | this._controls.editor.disabled = false; | |
680 | this.postProcessEditField(); | |
681 | }.bind(this), | |
682 | onFailure: this._boundFailureHandler | |
683 | }); | |
684 | new Ajax.Request(this.options.loadTextURL, options); | |
685 | }, | |
686 | postProcessEditField: function() { | |
687 | var fpc = this.options.fieldPostCreation; | |
688 | if (fpc) | |
689 | $(this._controls.editor)['focus' == fpc ? 'focus' : 'activate'](); | |
690 | }, | |
691 | prepareOptions: function() { | |
692 | this.options = Object.clone(Ajax.InPlaceEditor.DefaultOptions); | |
693 | Object.extend(this.options, Ajax.InPlaceEditor.DefaultCallbacks); | |
694 | [this._extraDefaultOptions].flatten().compact().each(function(defs) { | |
695 | Object.extend(this.options, defs); | |
696 | }.bind(this)); | |
697 | }, | |
698 | prepareSubmission: function() { | |
699 | this._saving = true; | |
700 | this.removeForm(); | |
701 | this.leaveHover(); | |
this.showSaving(); | ||
}, | ||
704 | registerListeners: function() { | |
705 | this._listeners = { }; | |
706 | var listener; | |
707 | $H(Ajax.InPlaceEditor.Listeners).each(function(pair) { | |
708 | listener = this[pair.value].bind(this); | |
709 | this._listeners[pair.key] = listener; | |
710 | if (!this.options.externalControlOnly) | |
711 | this.element.observe(pair.key, listener); | |
712 | if (this.options.externalControl) | |
713 | this.options.externalControl.observe(pair.key, listener); | |
714 | }.bind(this)); | |
715 | }, | |
716 | removeForm: function() { | |
717 | if (!this._form) return; | |
718 | this._form.remove(); | |
719 | this._form = null; | |
720 | this._controls = { }; | |
721 | }, | |
showSaving: function() { | ||
696 | | |
723 | this._oldInnerHTML = this.element.innerHTML; | |
this.element.innerHTML = this.options.savingText; | ||
698 | | |
699 | | |
700 | | |
725 | this.element.addClassName(this.options.savingClassName); | |
726 | this.element.style.backgroundColor = this._originalBackground; | |
727 | this.element.show(); | |
}, | ||
702 | | |
703 | | |
704 | | |
705 | | |
729 | triggerCallback: function(cbName, arg) { | |
730 | if ('function' == typeof this.options[cbName]) { | |
731 | this.options[cbName](this, arg); | |
} | ||
}, | ||
708 | | |
709 | | |
710 | | |
711 | | |
712 | | |
713 | | |
714 | | |
734 | unregisterListeners: function() { | |
735 | $H(this._listeners).each(function(pair) { | |
736 | if (!this.options.externalControlOnly) | |
737 | this.element.stopObserving(pair.key, pair.value); | |
738 | if (this.options.externalControl) | |
739 | this.options.externalControl.stopObserving(pair.key, pair.value); | |
740 | }.bind(this)); | |
}, | ||
716 | | |
717 | | |
718 | | |
719 | | |
720 | | |
721 | | |
722 | | |
723 | | |
724 | | |
725 | | |
742 | wrapUp: function(transport) { | |
743 | this.leaveEditMode(); | |
744 | // Can't use triggerCallback due to backward compatibility: requires | |
745 | // binding + direct element | |
746 | this._boundComplete(transport, this.element); | |
747 | } | |
748 | }); | |
749 | ||
750 | Object.extend(Ajax.InPlaceEditor.prototype, { | |
751 | dispose: Ajax.InPlaceEditor.prototype.destroy | |
752 | }); | |
753 | ||
754 | Ajax.InPlaceCollectionEditor = Class.create(Ajax.InPlaceEditor, { | |
755 | initialize: function($super, element, url, options) { | |
756 | this._extraDefaultOptions = Ajax.InPlaceCollectionEditor.DefaultOptions; | |
757 | $super(element, url, options); | |
758 | }, | |
759 | ||
760 | createEditField: function() { | |
761 | var list = document.createElement('select'); | |
762 | list.name = this.options.paramName; | |
763 | list.size = 1; | |
764 | this._controls.editor = list; | |
765 | this._collection = this.options.collection || []; | |
766 | if (this.options.loadCollectionURL) | |
767 | this.loadCollection(); | |
768 | else | |
769 | this.checkForExternalText(); | |
770 | this._form.appendChild(this._controls.editor); | |
771 | }, | |
772 | ||
773 | loadCollection: function() { | |
774 | this._form.addClassName(this.options.loadingClassName); | |
775 | this.showLoadingText(this.options.loadingCollectionText); | |
776 | var options = Object.extend({ method: 'get' }, this.options.ajaxOptions); | |
777 | Object.extend(options, { | |
778 | parameters: 'editorId=' + encodeURIComponent(this.element.id), | |
779 | onComplete: Prototype.emptyFunction, | |
780 | onSuccess: function(transport) { | |
781 | var js = transport.responseText.strip(); | |
782 | if (!/^\[.*\]$/.test(js)) // TODO: improve sanity check | |
783 | throw('Server returned an invalid collection representation.'); | |
784 | this._collection = eval(js); | |
785 | this.checkForExternalText(); | |
786 | }.bind(this), | |
787 | onFailure: this.onFailure | |
}); | ||
789 | new Ajax.Request(this.options.loadCollectionURL, options); | |
}, | ||
728 | | |
729 | | |
730 | | |
731 | | |
732 | | |
733 | | |
734 | | |
735 | | |
791 | ||
792 | showLoadingText: function(text) { | |
793 | this._controls.editor.disabled = true; | |
794 | var tempOption = this._controls.editor.firstChild; | |
795 | if (!tempOption) { | |
796 | tempOption = document.createElement('option'); | |
797 | tempOption.value = ''; | |
798 | this._controls.editor.appendChild(tempOption); | |
799 | tempOption.selected = true; | |
} | ||
737 | | |
738 | | |
739 | | |
740 | | |
801 | tempOption.update((text || '').stripScripts().stripTags()); | |
}, | ||
742 | | |
743 | | |
744 | | |
803 | ||
804 | checkForExternalText: function() { | |
805 | this._text = this.getText(); | |
806 | if (this.options.loadTextURL) | |
807 | this.loadExternalText(); | |
808 | else | |
809 | this.buildOptionList(); | |
}, | ||
746 | | |
747 | | |
748 | | |
749 | | |
750 | | |
751 | | |
752 | | |
753 | | |
754 | | |
755 | | |
756 | | |
757 | | |
758 | | |
759 | | |
760 | | |
811 | ||
812 | loadExternalText: function() { | |
813 | this.showLoadingText(this.options.loadingText); | |
814 | var options = Object.extend({ method: 'get' }, this.options.ajaxOptions); | |
815 | Object.extend(options, { | |
816 | parameters: 'editorId=' + encodeURIComponent(this.element.id), | |
817 | onComplete: Prototype.emptyFunction, | |
818 | onSuccess: function(transport) { | |
819 | this._text = transport.responseText.strip(); | |
820 | this.buildOptionList(); | |
821 | }.bind(this), | |
822 | onFailure: this.onFailure | |
823 | }); | |
824 | new Ajax.Request(this.options.loadTextURL, options); | |
825 | }, | |
826 | ||
827 | buildOptionList: function() { | |
828 | this._form.removeClassName(this.options.loadingClassName); | |
829 | this._collection = this._collection.map(function(entry) { | |
830 | return 2 === entry.length ? entry : [entry, entry].flatten(); | |
831 | }); | |
832 | var marker = ('value' in this.options) ? this.options.value : this._text; | |
833 | var textFound = this._collection.any(function(entry) { | |
834 | return entry[0] == marker; | |
835 | }.bind(this)); | |
836 | this._controls.editor.update(''); | |
837 | var option; | |
838 | this._collection.each(function(entry, index) { | |
839 | option = document.createElement('option'); | |
840 | option.value = entry[0]; | |
841 | option.selected = textFound ? entry[0] == marker : 0 == index; | |
842 | option.appendChild(document.createTextNode(entry[1])); | |
843 | this._controls.editor.appendChild(option); | |
844 | }.bind(this)); | |
845 | this._controls.editor.disabled = false; | |
846 | Field.scrollFreeActivate(this._controls.editor); | |
} | ||
848 | }); | |
849 | ||
850 | //**** DEPRECATION LAYER FOR InPlace[Collection]Editor! **** | |
851 | //**** This only exists for a while, in order to let **** | |
852 | //**** users adapt to the new API. Read up on the new **** | |
853 | //**** API and convert your code to it ASAP! **** | |
854 | ||
855 | Ajax.InPlaceEditor.prototype.initialize.dealWithDeprecatedOptions = function(options) { | |
856 | if (!options) return; | |
857 | function fallback(name, expr) { | |
858 | if (name in options || expr === undefined) return; | |
859 | options[name] = expr; | |
860 | }; | |
861 | fallback('cancelControl', (options.cancelLink ? 'link' : (options.cancelButton ? 'button' : | |
862 | options.cancelLink == options.cancelButton == false ? false : undefined))); | |
863 | fallback('okControl', (options.okLink ? 'link' : (options.okButton ? 'button' : | |
864 | options.okLink == options.okButton == false ? false : undefined))); | |
865 | fallback('highlightColor', options.highlightcolor); | |
866 | fallback('highlightEndColor', options.highlightendcolor); | |
}; | ||
764 | ||
765 | ||
766 | ||
767 | | |
768 | | |
769 | | |
770 | | |
771 | | |
772 | | |
773 | | |
774 | | |
775 | | |
776 | | |
777 | | |
778 | | |
779 | | |
869 | Object.extend(Ajax.InPlaceEditor, { | |
870 | DefaultOptions: { | |
871 | ajaxOptions: { }, | |
872 | autoRows: 3, // Use when multi-line w/ rows == 1 | |
873 | cancelControl: 'link', // 'link'|'button'|false | |
874 | cancelText: 'cancel', | |
875 | clickToEditText: 'Click to edit', | |
876 | externalControl: null, // id|elt | |
877 | externalControlOnly: false, | |
878 | fieldPostCreation: 'activate', // 'activate'|'focus'|false | |
879 | formClassName: 'inplaceeditor-form', | |
880 | formId: null, // id|elt | |
881 | highlightColor: '#ffff99', | |
882 | highlightEndColor: '#ffffff', | |
883 | hoverClassName: '', | |
884 | htmlResponse: true, | |
885 | loadingClassName: 'inplaceeditor-loading', | |
886 | loadingText: 'Loading...', | |
887 | okControl: 'button', // 'link'|'button'|false | |
888 | okText: 'ok', | |
889 | paramName: 'value', | |
890 | rows: 1, // If 1 and multi-line, uses autoRows | |
891 | savingClassName: 'inplaceeditor-saving', | |
892 | savingText: 'Saving...', | |
893 | size: 0, | |
894 | stripLoadedTextTags: false, | |
895 | submitOnBlur: false, | |
896 | textAfterControls: '', | |
897 | textBeforeControls: '', | |
898 | textBetweenControls: '' | |
899 | }, | |
900 | DefaultCallbacks: { | |
901 | callback: function(form) { | |
902 | return Form.serialize(form); | |
903 | }, | |
904 | onComplete: function(transport, element) { | |
905 | // For backward compatibility, this one is bound to the IPE, and passes | |
906 | // the element directly. It was too often customized, so we don't break it. | |
907 | new Effect.Highlight(element, { | |
908 | startcolor: this.options.highlightColor, keepBackgroundImage: true }); | |
909 | }, | |
910 | onEnterEditMode: null, | |
911 | onEnterHover: function(ipe) { | |
912 | ipe.element.style.backgroundColor = ipe.options.highlightColor; | |
913 | if (ipe._effect) | |
914 | ipe._effect.cancel(); | |
915 | }, | |
916 | onFailure: function(transport, ipe) { | |
917 | alert('Error communication with the server: ' + transport.responseText.stripTags()); | |
918 | }, | |
919 | onFormCustomization: null, // Takes the IPE and its generated form, after editor, before controls. | |
920 | onLeaveEditMode: null, | |
921 | onLeaveHover: function(ipe) { | |
922 | ipe._effect = new Effect.Highlight(ipe.element, { | |
923 | startcolor: ipe.options.highlightColor, endcolor: ipe.options.highlightEndColor, | |
924 | restorecolor: ipe._originalBackground, keepBackgroundImage: true | |
925 | }); | |
} | ||
781 | ||
782 | | |
783 | | |
784 | | |
785 | | |
786 | | |
787 | | |
927 | }, | |
928 | Listeners: { | |
929 | click: 'enterEditMode', | |
930 | keydown: 'checkForEscapeOrReturn', | |
931 | mouseover: 'enterHover', | |
932 | mouseout: 'leaveHover' | |
} | ||
}); | ||
791 | ||
936 | Ajax.InPlaceCollectionEditor.DefaultOptions = { | |
937 | loadingCollectionText: 'Loading options...' | |
938 | }; | |
939 | ||
940 | // Delayed observer, like Form.Element.Observer, | |
// but waits for delay after last key input | ||
// Ideal for live-search fields | ||
795 | ||
796 | ||
944 | Form.Element.DelayedObserver = Class.create({ | |
initialize: function(element, delay, callback) { | ||
this.delay = delay || 0.5; | ||
this.element = $(element); | ||
this.callback = callback; | ||
this.timer = null; | ||
802 | | |
950 | this.lastValue = $F(this.element); | |
Event.observe(this.element,'keyup',this.delayedListener.bindAsEventListener(this)); | ||
}, | ||
delayedListener: function(event) { | ||
... | ... | |
this.timer = null; | ||
this.callback(this.element, $F(this.element)); | ||
} | ||
815 | ||
963 | }); | |
rool/rails/hub/trunk/public/javascripts/dragdrop.js:
prev. | current | |
1 | ||
2 | ||
3 | ||
4 | ||
1 | // Copyright (c) 2005-2008 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us) | |
2 | // (c) 2005-2008 Sammi Williams (http://www.oriontransfer.co.nz, sammi@oriontransfer.co.nz) | |
3 | // | |
4 | // script.aculo.us is freely distributable under the terms of an MIT-style license. | |
5 | // For details, see the script.aculo.us web site: http://script.aculo.us/ | |
6 | ||
7 | if(Object.isUndefined(Effect)) | |
8 | throw("dragdrop.js requires including script.aculo.us' effects.js library"); | |
var Droppables = { | ||
drops: [], | ||
... | ... | |
greedy: true, | ||
hoverclass: null, | ||
tree: false | ||
21 | | |
23 | }, arguments[1] || { }); | |
// cache containers | ||
if(options.containment) { | ||
options._containers = []; | ||
var containment = options.containment; | ||
27 | | |
28 | | |
29 | if(Object.isArray(containment)) { | |
containment.each( function(c) { options._containers.push($(c)) }); | ||
} else { | ||
options._containers.push($(containment)); | ||
} | ||
} | ||
34 | | |
35 | ||
if(options.accept) options.accept = [options.accept].flatten(); | ||
Element.makePositioned(element); // fix IE | ||
... | ... | |
this.drops.push(options); | ||
}, | ||
42 | | |
43 | ||
findDeepestChild: function(drops) { | ||
deepest = drops[0]; | ||
45 | | |
46 | ||
for (i = 1; i < drops.length; ++i) | ||
if (Element.isParent(drops[i].element, deepest.element)) | ||
deepest = drops[i]; | ||
49 | | |
50 | ||
return deepest; | ||
}, | ||
isContained: function(element, drop) { | ||
var containmentNode; | ||
if(drop.tree) { | ||
56 | | |
57 | containmentNode = element.treeNode; | |
} else { | ||
containmentNode = element.parentNode; | ||
} | ||
return drop._containers.detect(function(c) { return containmentNode == c }); | ||
}, | ||
62 | | |
63 | ||
isAffected: function(point, element, drop) { | ||
return ( | ||
(drop.element!=element) && | ||
((!drop._containers) || | ||
this.isContained(element, drop)) && | ||
((!drop.accept) || | ||
69 | | |
70 | (Element.classNames(element).detect( | |
function(v) { return drop.accept.include(v) } ) )) && | ||
Position.within(drop.element, point[0], point[1]) ); | ||
}, | ||
... | ... | |
show: function(point, element) { | ||
if(!this.drops.length) return; | ||
88 | | |
89 | | |
90 | | |
89 | var drop, affected = []; | |
90 | ||
this.drops.each( function(drop) { | ||
if(Droppables.isAffected(point, element, drop)) | ||
affected.push(drop); | ||
}); | ||
95 | | |
96 | | |
95 | ||
96 | if(affected.length>0) | |
drop = Droppables.findDeepestChild(affected); | ||
98 | ||
99 | if(this.last_active && this.last_active != drop) this.deactivate(this.last_active); | |
100 | if (drop) { | |
Position.within(drop.element, point[0], point[1]); | ||
if(drop.onHover) | ||
drop.onHover(element, drop.element, Position.overlap(drop.overlap, drop.element)); | ||
101 | | |
102 | | |
104 | ||
105 | if (drop != this.last_active) Droppables.activate(drop); | |
} | ||
}, | ||
... | ... | |
Position.prepare(); | ||
if (this.isAffected([Event.pointerX(event), Event.pointerY(event)], element, this.last_active)) | ||
111 | | |
114 | if (this.last_active.onDrop) { | |
this.last_active.onDrop(element, this.last_active.element, event); | ||
116 | return true; | |
117 | } | |
}, | ||
reset: function() { | ||
if(this.last_active) | ||
this.deactivate(this.last_active); | ||
} | ||
119 | ||
124 | }; | |
var Draggables = { | ||
drags: [], | ||
observers: [], | ||
124 | | |
129 | ||
register: function(draggable) { | ||
if(this.drags.length == 0) { | ||
this.eventMouseUp = this.endDrag.bindAsEventListener(this); | ||
this.eventMouseMove = this.updateDrag.bindAsEventListener(this); | ||
this.eventKeypress = this.keyPress.bindAsEventListener(this); | ||
130 | | |
135 | ||
Event.observe(document, "mouseup", this.eventMouseUp); | ||
Event.observe(document, "mousemove", this.eventMouseMove); | ||
Event.observe(document, "keypress", this.eventKeypress); | ||
} | ||
this.drags.push(draggable); | ||
}, | ||
137 | | |
142 | ||
unregister: function(draggable) { | ||
this.drags = this.drags.reject(function(d) { return d==draggable }); | ||
if(this.drags.length == 0) { | ||
... | ... | |
Event.stopObserving(document, "keypress", this.eventKeypress); | ||
} | ||
}, | ||
146 | | |
151 | ||
activate: function(draggable) { | ||
148 | | |
149 | | |
153 | if(draggable.options.delay) { | |
154 | this._timeout = setTimeout(function() { | |
155 | Draggables._timeout = null; | |
156 | window.focus(); | |
157 | Draggables.activeDraggable = draggable; | |
158 | }.bind(this), draggable.options.delay); | |
159 | } else { | |
160 | window.focus(); // allows keypress events if window isn't currently focused, fails for Safari | |
161 | this.activeDraggable = draggable; | |
162 | } | |
}, | ||
151 | | |
164 | ||
deactivate: function() { | ||
this.activeDraggable = null; | ||
}, | ||
155 | | |
168 | ||
updateDrag: function(event) { | ||
if(!this.activeDraggable) return; | ||
var pointer = [Event.pointerX(event), Event.pointerY(event)]; | ||
... | ... | |
// the same coordinates, prevent needless redrawing (moz bug?) | ||
if(this._lastPointer && (this._lastPointer.inspect() == pointer.inspect())) return; | ||
this._lastPointer = pointer; | ||
176 | ||
this.activeDraggable.updateDrag(event, pointer); | ||
}, | ||
165 | | |
179 | ||
endDrag: function(event) { | ||
181 | if(this._timeout) { | |
182 | clearTimeout(this._timeout); | |
183 | this._timeout = null; | |
184 | } | |
if(!this.activeDraggable) return; | ||
this._lastPointer = null; | ||
this.activeDraggable.endDrag(event); | ||
this.activeDraggable = null; | ||
}, | ||
172 | | |
190 | ||
keyPress: function(event) { | ||
if(this.activeDraggable) | ||
this.activeDraggable.keyPress(event); | ||
}, | ||
177 | | |
195 | ||
addObserver: function(observer) { | ||
this.observers.push(observer); | ||
this._cacheObserverCallbacks(); | ||
}, | ||
182 | | |
200 | ||
removeObserver: function(element) { // element instead of observer fixes mem leaks | ||
this.observers = this.observers.reject( function(o) { return o.element==element }); | ||
this._cacheObserverCallbacks(); | ||
}, | ||
187 | | |
205 | ||
notify: function(eventName, draggable, event) { // 'onStart', 'onEnd', 'onDrag' | ||
if(this[eventName+'Count'] > 0) | ||
this.observers.each( function(o) { | ||
if(o[eventName]) o[eventName](eventName, draggable, event); | ||
}); | ||
211 | if(draggable.options[eventName]) draggable.options[eventName](draggable, event); | |
}, | ||
194 | | |
213 | ||
_cacheObserverCallbacks: function() { | ||
['onStart','onEnd','onDrag'].each( function(eventName) { | ||
Draggables[eventName+'Count'] = Draggables.observers.select( | ||
... | ... | |
).length; | ||
}); | ||
} | ||
202 | ||
221 | }; | |
/*--------------------------------------------------------------------------*/ | ||
206 | ||
207 | ||
225 | var Draggable = Class.create({ | |
initialize: function(element) { | ||
209 | | |
227 | var defaults = { | |
handle: false, | ||
211 | | |
212 | | |
213 | | |
reverteffect: function(element, top_offset, left_offset) { | ||
var dur = Math.sqrt(Math.abs(top_offset^2)+Math.abs(left_offset^2))*0.02; | ||
216 | | |
231 | new Effect.Move(element, { x: -left_offset, y: -top_offset, duration: dur, | |
232 | queue: {scope:'_draggable', position:'end'} | |
233 | }); | |
}, | ||
218 | | |
219 | | |
235 | endeffect: function(element) { | |
236 | var toOpacity = Object.isNumber(element._opacity) ? element._opacity : 1.0; | |
237 | new Effect.Opacity(element, {duration:0.2, from:0.7, to:toOpacity, | |
238 | queue: {scope:'_draggable', position:'end'}, | |
239 | afterFinish: function(){ | |
240 | Draggable._dragging[element] = false | |
241 | } | |
242 | }); | |
}, | ||
zindex: 1000, | ||
revert: false, | ||
246 | quiet: false, | |
scroll: false, | ||
scrollSensitivity: 20, | ||
scrollSpeed: 15, | ||
226 | | |
227 | | |
250 | snap: false, // false, or xy or [x,y] or function(x,y){ return [x,y] } | |
251 | delay: 0 | |
252 | }; | |
254 | if(!arguments[1] || Object.isUndefined(arguments[1].endeffect)) | |
255 | Object.extend(defaults, { | |
256 | starteffect: function(element) { | |
257 | element._opacity = Element.getOpacity(element); | |
258 | Draggable._dragging[element] = true; | |
259 | new Effect.Opacity(element, {duration:0.2, from:element._opacity, to:0.7}); | |
260 | } | |
261 | }); | |
262 | ||
263 | var options = Object.extend(defaults, arguments[1] || { }); | |
264 | ||
this.element = $(element); | ||
230 | | |
231 | | |
232 | | |
233 | | |
234 | | |
266 | ||
267 | if(options.handle && Object.isString(options.handle)) | |
268 | this.handle = this.element.down('.'+options.handle, 0); | |
269 | ||
if(!this.handle) this.handle = $(options.handle); | ||
if(!this.handle) this.handle = this.element; | ||
237 | | |
238 | | |
272 | ||
273 | if(options.scroll && !options.scroll.scrollTo && !options.scroll.outerHTML) { | |
options.scroll = $(options.scroll); | ||
275 | this._isScrollChild = Element.childOf(this.element, options.scroll); | |
276 | } | |
241 | | |
278 | Element.makePositioned(this.element); // fix IE | |
243 | | |
this.options = options; | ||
245 | | |
281 | this.dragging = false; | |
this.eventMouseDown = this.initDrag.bindAsEventListener(this); | ||
Event.observe(this.handle, "mousedown", this.eventMouseDown); | ||
249 | | |
285 | ||
Draggables.register(this); | ||
}, | ||
252 | | |
288 | ||
destroy: function() { | ||
Event.stopObserving(this.handle, "mousedown", this.eventMouseDown); | ||
Draggables.unregister(this); | ||
}, | ||
257 | | |
293 | ||
currentDelta: function() { | ||
return([ | ||
parseInt(Element.getStyle(this.element,'left') || '0'), | ||
parseInt(Element.getStyle(this.element,'top') || '0')]); | ||
}, | ||
263 | | |
299 | ||
initDrag: function(event) { | ||
265 | | |
301 | if(!Object.isUndefined(Draggable._dragging[this.element]) && | |
302 | Draggable._dragging[this.element]) return; | |
303 | if(Event.isLeftClick(event)) { | |
// abort on form elements, fixes a Firefox issue | ||
var src = Event.element(event); | ||
268 | | |
269 | | |
270 | | |
271 | | |
272 | | |
273 | | |
274 | | |
275 | | |
276 | | |
277 | | |
278 | | |
279 | | |
306 | if((tag_name = src.tagName.toUpperCase()) && ( | |
307 | tag_name=='INPUT' || | |
308 | tag_name=='SELECT' || | |
309 | tag_name=='OPTION' || | |
310 | tag_name=='BUTTON' || | |
311 | tag_name=='TEXTAREA')) return; | |
312 | ||
var pointer = [Event.pointerX(event), Event.pointerY(event)]; | ||
var pos = Position.cumulativeOffset(this.element); | ||
this.offset = [0,1].map( function(i) { return (pointer[i] - pos[i]) }); | ||
283 | | |
316 | ||
Draggables.activate(this); | ||
Event.stop(event); | ||
} | ||
}, | ||
288 | | |
321 | ||
startDrag: function(event) { | ||
this.dragging = true; | ||
291 | | |
324 | if(!this.delta) | |
325 | this.delta = this.currentDelta(); | |
326 | ||
if(this.options.zindex) { | ||
this.originalZ = parseInt(Element.getStyle(this.element,'z-index') || 0); | ||
this.element.style.zIndex = this.options.zindex; | ||
} | ||
296 | | |
331 | ||
if(this.options.ghosting) { | ||
this._clone = this.element.cloneNode(true); | ||
299 | | |
334 | this._originallyAbsolute = (this.element.getStyle('position') == 'absolute'); | |
335 | if (!this._originallyAbsolute) | |
336 | Position.absolutize(this.element); | |
this.element.parentNode.insertBefore(this._clone, this.element); | ||
} | ||
302 | | |
339 | ||
if(this.options.scroll) { | ||
if (this.options.scroll == window) { | ||
var where = this._getWindowScroll(this.options.scroll); | ||
... | ... | |
this.originalScrollTop = this.options.scroll.scrollTop; | ||
} | ||
} | ||
313 | | |
350 | ||
Draggables.notify('onStart', this, event); | ||
352 | ||
if(this.options.starteffect) this.options.starteffect(this.element); | ||
}, | ||
317 | | |
355 | ||
updateDrag: function(event, pointer) { | ||
if(!this.dragging) this.startDrag(event); | ||
320 | | |
321 | | |
358 | ||
359 | if(!this.options.quiet){ | |
360 | Position.prepare(); | |
361 | Droppables.show(pointer, this.element); | |
362 | } | |
363 | ||
Draggables.notify('onDrag', this, event); | ||
365 | ||
this.draw(pointer); | ||
if(this.options.change) this.options.change(this); | ||
325 | | |
368 | ||
if(this.options.scroll) { | ||
this.stopScrolling(); | ||
328 | | |
371 | ||
var p; | ||
if (this.options.scroll == window) { | ||
with(this._getWindowScroll(this.options.scroll)) { p = [ left, top, left+width, top+height ]; } | ||
} else { | ||
p = Position.page(this.options.scroll); | ||
334 | | |
335 | | |
377 | p[0] += this.options.scroll.scrollLeft + Position.deltaX; | |
378 | p[1] += this.options.scroll.scrollTop + Position.deltaY; | |
p.push(p[0]+this.options.scroll.offsetWidth); | ||
p.push(p[1]+this.options.scroll.offsetHeight); | ||
} | ||
... | ... | |
if(pointer[1] > (p[3]-this.options.scrollSensitivity)) speed[1] = pointer[1]-(p[3]-this.options.scrollSensitivity); | ||
this.startScrolling(speed); | ||
} | ||
346 | | |
389 | ||
// fix AppleWebKit rendering | ||
348 | | |
349 | | |
391 | if(Prototype.Browser.WebKit) window.scrollBy(0,0); | |
392 | ||
Event.stop(event); | ||
}, | ||
352 | | |
395 | ||
finishDrag: function(event, success) { | ||
this.dragging = false; | ||
399 | if(this.options.quiet){ | |
400 | Position.prepare(); | |
401 | var pointer = [Event.pointerX(event), Event.pointerY(event)]; | |
402 | Droppables.show(pointer, this.element); | |
403 | } | |
404 | ||
if(this.options.ghosting) { | ||
357 | | |
406 | if (!this._originallyAbsolute) | |
407 | Position.relativize(this.element); | |
408 | delete this._originallyAbsolute; | |
Element.remove(this._clone); | ||
this._clone = null; | ||
} | ||
362 | | |
413 | var dropped = false; | |
414 | if(success) { | |
415 | dropped = Droppables.fire(event, this.element); | |
416 | if (!dropped) dropped = false; | |
417 | } | |
418 | if(dropped && this.options.onDropped) this.options.onDropped(this.element); | |
Draggables.notify('onEnd', this, event); | ||
var revert = this.options.revert; | ||
366 | | |
367 | | |
422 | if(revert && Object.isFunction(revert)) revert = revert(this.element); | |
423 | ||
var d = this.currentDelta(); | ||
if(revert && this.options.reverteffect) { | ||
370 | | |
371 | | |
426 | if (dropped == 0 || revert != 'failure') | |
427 | this.options.reverteffect(this.element, | |
428 | d[1]-this.delta[1], d[0]-this.delta[0]); | |
} else { | ||
this.delta = d; | ||
} | ||
... | ... | |
if(this.options.zindex) | ||
this.element.style.zIndex = this.originalZ; | ||
379 | | |
436 | if(this.options.endeffect) | |
this.options.endeffect(this.element); | ||
Draggables.deactivate(this); | ||
Droppables.reset(); | ||
}, | ||
385 | | |
442 | ||
keyPress: function(event) { | ||
if(event.keyCode!=Event.KEY_ESC) return; | ||
this.finishDrag(event, false); | ||
Event.stop(event); | ||
}, | ||
391 | | |
448 | ||
endDrag: function(event) { | ||
if(!this.dragging) return; | ||
this.stopScrolling(); | ||
this.finishDrag(event, true); | ||
Event.stop(event); | ||
}, | ||
398 | | |
455 | ||
draw: function(point) { | ||
var pos = Position.cumulativeOffset(this.element); | ||
458 | if(this.options.ghosting) { | |
459 | var r = Position.realOffset(this.element); | |
460 | pos[0] += r[0] - Position.deltaX; pos[1] += r[1] - Position.deltaY; | |
461 | } | |
462 | ||
var d = this.currentDelta(); | ||
pos[0] -= d[0]; pos[1] -= d[1]; | ||
403 | | |
404 | | |
465 | ||
466 | if(this.options.scroll && (this.options.scroll != window && this._isScrollChild)) { | |
pos[0] -= this.options.scroll.scrollLeft-this.originalScrollLeft; | ||
pos[1] -= this.options.scroll.scrollTop-this.originalScrollTop; | ||
} | ||
408 | | |
409 | | |
410 | | |
470 | ||
471 | var p = [0,1].map(function(i){ | |
472 | return (point[i]-pos[i]-this.offset[i]) | |
}.bind(this)); | ||
412 | | |
474 | ||
if(this.options.snap) { | ||
414 | | |
415 | | |
476 | if(Object.isFunction(this.options.snap)) { | |
477 | p = this.options.snap(p[0],p[1],this); | |
} else { | ||
417 | | |
479 | if(Object.isArray(this.options.snap)) { | |
p = p.map( function(v, i) { | ||
419 | | |
481 | return (v/this.options.snap[i]).round()*this.options.snap[i] }.bind(this)); | |
} else { | ||
p = p.map( function(v) { | ||
422 | | |
484 | return (v/this.options.snap).round()*this.options.snap }.bind(this)); | |
} | ||
}} | ||
425 | | |
487 | ||
var style = this.element.style; | ||
if((!this.options.constraint) || (this.options.constraint=='horizontal')) | ||
style.left = p[0] + "px"; | ||
if((!this.options.constraint) || (this.options.constraint=='vertical')) | ||
style.top = p[1] + "px"; | ||
493 | ||
if(style.visibility=="hidden") style.visibility = ""; // fix gecko rendering | ||
}, | ||
433 | | |
496 | ||
stopScrolling: function() { | ||
if(this.scrollInterval) { | ||
clearInterval(this.scrollInterval); | ||
... | ... | |
Draggables._lastScrollPointer = null; | ||
} | ||
}, | ||
441 | | |
504 | ||
startScrolling: function(speed) { | ||
506 | if(!(speed[0] || speed[1])) return; | |
this.scrollSpeed = [speed[0]*this.options.scrollSpeed,speed[1]*this.options.scrollSpeed]; | ||
this.lastScrolled = new Date(); | ||
this.scrollInterval = setInterval(this.scroll.bind(this), 10); | ||
}, | ||
447 | | |
511 | ||
scroll: function() { | ||
var current = new Date(); | ||
var delta = current - this.lastScrolled; | ||
... | ... | |
this.options.scroll.scrollLeft += this.scrollSpeed[0] * delta / 1000; | ||
this.options.scroll.scrollTop += this.scrollSpeed[1] * delta / 1000; | ||
} | ||
463 | | |
527 | ||
Position.prepare(); | ||
Droppables.show(Draggables._lastPointer, this.element); | ||
Draggables.notify('onDrag', this); | ||
467 | | |
468 | | |
469 | | |
470 | | |
471 | | |
472 | | |
473 | | |
474 | | |
475 | | |
531 | if (this._isScrollChild) { | |
532 | Draggables._lastScrollPointer = Draggables._lastScrollPointer || $A(Draggables._lastPointer); | |
533 | Draggables._lastScrollPointer[0] += this.scrollSpeed[0] * delta / 1000; | |
534 | Draggables._lastScrollPointer[1] += this.scrollSpeed[1] * delta / 1000; | |
535 | if (Draggables._lastScrollPointer[0] < 0) | |
536 | Draggables._lastScrollPointer[0] = 0; | |
537 | if (Draggables._lastScrollPointer[1] < 0) | |
538 | Draggables._lastScrollPointer[1] = 0; | |
539 | this.draw(Draggables._lastScrollPointer); | |
540 | } | |
541 | ||
if(this.options.change) this.options.change(this); | ||
}, | ||
478 | | |
544 | ||
_getWindowScroll: function(w) { | ||
var T, L, W, H; | ||
with (w.document) { | ||
... | ... | |
H = documentElement.clientHeight; | ||
} else { | ||
W = body.offsetWidth; | ||
497 | | |
563 | H = body.offsetHeight; | |
} | ||
} | ||
return { top: T, left: L, width: W, height: H }; | ||
} | ||
502 | ||
568 | }); | |
570 | Draggable._dragging = { }; | |
571 | ||
/*--------------------------------------------------------------------------*/ | ||
506 | ||
507 | ||
574 | var SortableObserver = Class.create({ | |
initialize: function(element, observer) { | ||
this.element = $(element); | ||
this.observer = observer; | ||
this.lastValue = Sortable.serialize(this.element); | ||
}, | ||
513 | | |
580 | ||
onStart: function() { | ||
this.lastValue = Sortable.serialize(this.element); | ||
}, | ||
517 | | |
584 | ||
onEnd: function() { | ||
Sortable.unmark(); | ||
if(this.lastValue != Sortable.serialize(this.element)) | ||
this.observer(this.element) | ||
} | ||
523 | ||
590 | }); | |
var Sortable = { | ||
526 | | |
527 | | |
593 | SERIALIZE_RULE: /^[^_\-](?:[A-Za-z0-9\-\_]*)[_](.*)$/, | |
594 | ||
595 | sortables: { }, | |
596 | ||
_findRootElement: function(element) { | ||
529 | | |
598 | while (element.tagName.toUpperCase() != "BODY") { | |
if(element.id && Sortable.sortables[element.id]) return element; | ||
element = element.parentNode; | ||
} | ||
... | ... | |
if(!element) return; | ||
return Sortable.sortables[element.id]; | ||
}, | ||
540 | | |
609 | ||
destroy: function(element){ | ||
542 | | |
543 | | |
611 | element = $(element); | |
612 | var s = Sortable.sortables[element.id]; | |
613 | ||
if(s) { | ||
Draggables.removeObserver(s.element); | ||
s.droppables.each(function(d){ Droppables.remove(d) }); | ||
s.draggables.invoke('destroy'); | ||
548 | | |
618 | ||
delete Sortable.sortables[s.element.id]; | ||
} | ||
}, | ||
create: function(element) { | ||
element = $(element); | ||
555 | | |
625 | var options = Object.extend({ | |
element: element, | ||
tag: 'li', // assumes li children, override with tag: 'tagname' | ||
dropOnEmpty: false, | ||
... | ... | |
containment: element, // also takes array of elements (or id's); or false | ||
handle: false, // or a CSS class | ||
only: false, | ||
636 | delay: 0, | |
hoverclass: null, | ||
ghosting: false, | ||
639 | quiet: false, | |
scroll: false, | ||
scrollSensitivity: 20, | ||
scrollSpeed: 15, | ||
571 | | |
643 | format: this.SERIALIZE_RULE, | |
644 | ||
645 | // these take arrays of elements or ids and can be | |
646 | // used for better initialization performance | |
647 | elements: false, | |
648 | handles: false, | |
649 | ||
onChange: Prototype.emptyFunction, | ||
onUpdate: Prototype.emptyFunction | ||
574 | | |
652 | }, arguments[1] || { }); | |
// clear any old sortable with same element | ||
this.destroy(element); | ||
... | ... | |
// build options for the draggables | ||
var options_for_draggable = { | ||
revert: true, | ||
660 | quiet: options.quiet, | |
scroll: options.scroll, | ||
scrollSpeed: options.scrollSpeed, | ||
scrollSensitivity: options.scrollSensitivity, | ||
664 | delay: options.delay, | |
ghosting: options.ghosting, | ||
constraint: options.constraint, | ||
handle: options.handle }; | ||
... | ... | |
if(options.zindex) | ||
options_for_draggable.zindex = options.zindex; | ||
606 | | |
686 | // build options for the droppables | |
var options_for_droppable = { | ||
overlap: options.overlap, | ||
containment: options.containment, | ||
tree: options.tree, | ||
hoverclass: options.hoverclass, | ||
onHover: Sortable.onHover | ||
613 | | |
614 | | |
615 | | |
693 | }; | |
694 | ||
var options_for_tree = { | ||
onHover: Sortable.onEmptyHover, | ||
overlap: options.overlap, | ||
containment: options.containment, | ||
hoverclass: options.hoverclass | ||
621 | | |
700 | }; | |
// fix for gecko engine | ||
624 | | |
703 | Element.cleanWhitespace(element); | |
options.draggables = []; | ||
options.droppables = []; | ||
... | ... | |
options.droppables.push(element); | ||
} | ||
635 | | |
636 | | |
637 | | |
638 | | |
714 | (options.elements || this.findElements(element, options) || []).each( function(e,i) { | |
715 | var handle = options.handles ? $(options.handles[i]) : | |
716 | (options.handle ? $(e).select('.' + options.handle)[0] : e); | |
options.draggables.push( | ||
new Draggable(e, Object.extend(options_for_draggable, { handle: handle }))); | ||
Droppables.add(e, options_for_droppable); | ||
if(options.tree) e.treeNode = element; | ||
643 | | |
721 | options.droppables.push(e); | |
}); | ||
645 | | |
723 | ||
if(options.tree) { | ||
(Sortable.findTreeElements(element, options) || []).each( function(e) { | ||
Droppables.add(e, options_for_tree); | ||
... | ... | |
return Element.findChildren( | ||
element, options.only, options.tree ? true : false, options.tag); | ||
}, | ||
667 | | |
745 | ||
findTreeElements: function(element, options) { | ||
return Element.findChildren( | ||
element, options.only, options.tree ? true : false, options.treeTag); | ||
... | ... | |
var oldParentNode = element.parentNode; | ||
element.style.visibility = "hidden"; // fix gecko rendering | ||
dropon.parentNode.insertBefore(element, dropon); | ||
684 | | |
762 | if(dropon.parentNode!=oldParentNode) | |
Sortable.options(oldParentNode).onChange(element); | ||
Sortable.options(dropon.parentNode).onChange(element); | ||
} | ||
... | ... | |
var oldParentNode = element.parentNode; | ||
element.style.visibility = "hidden"; // fix gecko rendering | ||
dropon.parentNode.insertBefore(element, nextElement); | ||
695 | | |
773 | if(dropon.parentNode!=oldParentNode) | |
Sortable.options(oldParentNode).onChange(element); | ||
Sortable.options(dropon.parentNode).onChange(element); | ||
} | ||
} | ||
}, | ||
701 | | |
779 | ||
onEmptyHover: function(element, dropon, overlap) { | ||
var oldParentNode = element.parentNode; | ||
var droponOptions = Sortable.options(dropon); | ||
705 | | |
783 | ||
if(!Element.isParent(dropon, element)) { | ||
var index; | ||
708 | | |
709 | | |
786 | ||
787 | var children = Sortable.findElements(dropon, {tag: droponOptions.tag, only: droponOptions.only}); | |
var child = null; | ||
711 | | |
789 | ||
if(children) { | ||
var offset = Element.offsetSize(dropon, droponOptions.overlap) * (1.0 - overlap); | ||
714 | | |
792 | ||
for (index = 0; index < children.length; index += 1) { | ||
if (offset - Element.offsetSize (children[index], droponOptions.overlap) >= 0) { | ||
offset -= Element.offsetSize (children[index], droponOptions.overlap); | ||
... | ... | |
} | ||
} | ||
} | ||
727 | | |
805 | ||
dropon.insertBefore(element, child); | ||
729 | | |
807 | ||
Sortable.options(oldParentNode).onChange(element); | ||
droponOptions.onChange(element); | ||
} | ||
}, | ||
unmark: function() { | ||
736 | | |
814 | if(Sortable._marker) Sortable._marker.hide(); | |
}, | ||
mark: function(dropon, position) { | ||
// mark on ghosting only | ||
var sortable = Sortable.options(dropon.parentNode); | ||
742 | | |
820 | if(sortable && !sortable.ghosting) return; | |
if(!Sortable._marker) { | ||
745 | | |
746 | | |
747 | | |
748 | | |
823 | Sortable._marker = | |
824 | ($('dropmarker') || Element.extend(document.createElement('DIV'))). | |
825 | hide().addClassName('dropmarker').setStyle({position:'absolute'}); | |
document.getElementsByTagName("body").item(0).appendChild(Sortable._marker); | ||
750 | | |
827 | } | |
var offsets = Position.cumulativeOffset(dropon); | ||
752 | | |
753 | | |
754 | | |
829 | Sortable._marker.setStyle({left: offsets[0]+'px', top: offsets[1] + 'px'}); | |
830 | ||
if(position=='after') | ||
756 | | |
757 | | |
832 | if(sortable.overlap == 'horizontal') | |
833 | Sortable._marker.setStyle({left: (offsets[0]+dropon.clientWidth) + 'px'}); | |
else | ||
759 | | |
760 | | |
761 | | |
835 | Sortable._marker.setStyle({top: (offsets[1]+dropon.clientHeight) + 'px'}); | |
836 | ||
837 | Sortable._marker.show(); | |
}, | ||
763 | | |
839 | ||
_tree: function(element, options, parent) { | ||
var children = Sortable.findElements(element, options) || []; | ||
766 | | |
842 | ||
for (var i = 0; i < children.length; ++i) { | ||
var match = children[i].id.match(options.format); | ||
if (!match) continue; | ||
771 | | |
847 | ||
var child = { | ||
id: encodeURIComponent(match ? match[1] : null), | ||
element: element, | ||
parent: parent, | ||
776 | | |
852 | children: [], | |
position: parent.children.length, | ||
778 | | |
779 | | |
780 | | |
854 | container: $(children[i]).down(options.treeTag) | |
855 | }; | |
856 | ||
/* Get the element containing the children and recurse over it */ | ||
if (child.container) | ||
783 | | |
784 | | |
859 | this._tree(child.container, options, child); | |
860 | ||
parent.children.push (child); | ||
} | ||
788 | | |
864 | return parent; | |
}, | ||
791 | | |
792 | | |
793 | | |
794 | | |
795 | | |
796 | | |
797 | | |
798 | | |
799 | | |
800 | | |
801 | ||
tree: function(element) { | ||
element = $(element); | ||
var sortableOptions = this.options(element); | ||
... | ... | |
only: sortableOptions.only, | ||
name: element.id, | ||
format: sortableOptions.format | ||
811 | | |
812 | | |
876 | }, arguments[1] || { }); | |
877 | ||
var root = { | ||
id: null, | ||
parent: null, | ||
816 | | |
881 | children: [], | |
container: element, | ||
position: 0 | ||
819 | | |
820 | | |
821 | | |
884 | }; | |
885 | ||
886 | return Sortable._tree(element, options, root); | |
}, | ||
/* Construct a [i] index for a particular node */ | ||
... | ... | |
sequence: function(element) { | ||
element = $(element); | ||
835 | | |
836 | | |
900 | var options = Object.extend(this.options(element), arguments[1] || { }); | |
901 | ||
return $(this.findElements(element, options) || []).map( function(item) { | ||
return item.id.match(options.format) ? item.id.match(options.format)[1] : ''; | ||
}); | ||
... | ... | |
setSequence: function(element, new_sequence) { | ||
element = $(element); | ||
844 | | |
845 | | |
846 | | |
909 | var options = Object.extend(this.options(element), arguments[2] || { }); | |
910 | ||
911 | var nodeMap = { }; | |
this.findElements(element, options).each( function(n) { | ||
if (n.id.match(options.format)) | ||
nodeMap[n.id.match(options.format)[1]] = [n, n.parentNode]; | ||
n.parentNode.removeChild(n); | ||
}); | ||
852 | | |
917 | ||
new_sequence.each(function(ident) { | ||
var n = nodeMap[ident]; | ||
if (n) { | ||
... | ... | |
} | ||
}); | ||
}, | ||
861 | | |
926 | ||
serialize: function(element) { | ||
element = $(element); | ||
864 | | |
929 | var options = Object.extend(Sortable.options(element), arguments[1] || { }); | |
var name = encodeURIComponent( | ||
(arguments[1] && arguments[1].name) ? arguments[1].name : element.id); | ||
867 | | |
932 | ||
if (options.tree) { | ||
return Sortable.tree(element, arguments[1]).children.map( function (item) { | ||
870 | | |
935 | return [name + Sortable._constructIndex(item) + "[id]=" + | |
encodeURIComponent(item.id)].concat(item.children.map(arguments.callee)); | ||
}).flatten().join('&'); | ||
} else { | ||
... | ... | |
}).join('&'); | ||
} | ||
} | ||
879 | ||
944 | }; | |
881 | ||
946 | // Returns true if child is contained within element | |
Element.isParent = function(child, element) { | ||
if (!child.parentNode || child == element) return false; | ||
884 | ||
if (child.parentNode == element) return true; | ||
886 | ||
return Element.isParent(child.parentNode, element); | ||
888 | ||
951 | }; | |
890 | ||
953 | Element.findChildren = function(element, only, recursive, tagName) { | |
if(!element.hasChildNodes()) return null; | ||
tagName = tagName.toUpperCase(); | ||
if(only) only = [only].flatten(); | ||
... | ... | |
}); | ||
return (elements.length>0 ? elements.flatten() : []); | ||
906 | ||
969 | }; | |
Element.offsetSize = function (element, type) { | ||
909 | | |
910 | | |
911 | | |
912 | | |
913 | ||
973 | return element['offset' + ((type=='vertical' || type=='height') ? 'Height' : 'Width')]; | |
974 | }; | |
rool/rails/hub/trunk/public/javascripts/effects.js:
prev. | current | |
1 | ||
1 | // Copyright (c) 2005-2008 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us) | |
// Contributors: | ||
// Justin Palmer (http://encytemedia.com/) | ||
// Mark Pilgrim (http://diveintomark.org/) | ||
// Martin Bialasinki | ||
6 | ||
7 | ||
6 | // | |
7 | // script.aculo.us is freely distributable under the terms of an MIT-style license. | |
8 | // For details, see the script.aculo.us web site: http://script.aculo.us/ | |
9 | ||
10 | ||
11 | ||
12 | | |
13 | | |
14 | | |
15 | | |
16 | | |
17 | | |
18 | | |
19 | | |
20 | | |
21 | | |
22 | | |
23 | ||
10 | // converts rgb() and #xxx to #xxxxxx format, | |
11 | // returns self (or first argument) if not convertable | |
12 | String.prototype.parseColor = function() { | |
13 | var color = '#'; | |
14 | if (this.slice(0,4) == 'rgb(') { | |
15 | var cols = this.slice(4,this.length-1).split(','); | |
16 | var i=0; do { color += parseInt(cols[i]).toColorPart() } while (++i<3); | |
17 | } else { | |
18 | if (this.slice(0,1) == '#') { | |
19 | if (this.length==4) for(var i=1;i<4;i++) color += (this.charAt(i) + this.charAt(i)).toLowerCase(); | |
20 | if (this.length==7) color = this.toLowerCase(); | |
21 | } | |
22 | } | |
23 | return (color.length==7 ? color : (arguments[0] || this)); | |
24 | }; | |
/*--------------------------------------------------------------------------*/ | ||
27 | ||
28 | Element.collectTextNodes = function(element) { | |
return $A($(element).childNodes).collect( function(node) { | ||
29 | | |
30 | return (node.nodeType==3 ? node.nodeValue : | |
(node.hasChildNodes() ? Element.collectTextNodes(node) : '')); | ||
}).flatten().join(''); | ||
32 | ||
33 | }; | |
34 | ||
35 | Element.collectTextNodesIgnoreClass = function(element, className) { | |
return $A($(element).childNodes).collect( function(node) { | ||
36 | | |
37 | | |
37 | return (node.nodeType==3 ? node.nodeValue : | |
38 | ((node.hasChildNodes() && !Element.hasClassName(node,className)) ? | |
Element.collectTextNodesIgnoreClass(node, className) : '')); | ||
}).flatten().join(''); | ||
40 | ||
41 | }; | |
Element.setContentZoom = function(element, percent) { | ||
43 | | |
44 | | |
45 | | |
46 | ||
44 | element = $(element); | |
45 | element.setStyle({fontSize: (percent/100) + 'em'}); | |
46 | if (Prototype.Browser.WebKit) window.scrollBy(0,0); | |
47 | return element; | |
48 | }; | |
48 | ||
49 | | |
50 | | |
51 | | |
52 | | |
53 | | |
54 | | |
55 | ||
56 | ||
57 | ||
58 | | |
59 | | |
60 | | |
61 | | |
62 | | |
63 | | |
64 | | |
65 | | |
66 | | |
67 | | |
68 | | |
69 | | |
70 | | |
71 | | |
72 | | |
73 | ||
74 | | |
75 | ||
50 | Element.getInlineOpacity = function(element){ | |
return $(element).style.opacity || ''; | ||
77 | ||
52 | }; | |
79 | ||
80 | | |
81 | | |
82 | | |
83 | | |
84 | | |
85 | | |
86 | ||
87 | ||
Element.forceRerendering = function(element) { | ||
try { | ||
element = $(element); | ||
... | ... | |
/*--------------------------------------------------------------------------*/ | ||
99 | ||
100 | | |
101 | | |
102 | ||
103 | ||
104 | ||
105 | ||
var Effect = { | ||
66 | _elementDoesNotExistError: { | |
67 | name: 'ElementDoesNotExistError', | |
68 | message: 'The specified DOM element does not exist, but is required for this effect to operate' | |
69 | }, | |
70 | Transitions: { | |
71 | linear: Prototype.K, | |
72 | sinoidal: function(pos) { | |
73 | return (-Math.cos(pos*Math.PI)/2) + .5; | |
74 | }, | |
75 | reverse: function(pos) { | |
76 | return 1-pos; | |
77 | }, | |
78 | flicker: function(pos) { | |
79 | var pos = ((-Math.cos(pos*Math.PI)/4) + .75) + Math.random()/4; | |
80 | return pos > 1 ? 1 : pos; | |
81 | }, | |
82 | wobble: function(pos) { | |
83 | return (-Math.cos(pos*Math.PI*(9*pos))/2) + .5; | |
84 | }, | |
85 | pulse: function(pos, pulses) { | |
86 | return (-Math.cos((pos*((pulses||5)-.5)*2)*Math.PI)/2) + .5; | |
87 | }, | |
88 | spring: function(pos) { | |
89 | return 1 - (Math.cos(pos * 4.5 * Math.PI) * Math.exp(-pos * 6)); | |
90 | }, | |
91 | none: function(pos) { | |
92 | return 0; | |
93 | }, | |
94 | full: function(pos) { | |
95 | return 1; | |
96 | } | |
97 | }, | |
98 | DefaultOptions: { | |
99 | duration: 1.0, // seconds | |
100 | fps: 100, // 100= assume 66fps max. | |
101 | sync: false, // true for combining | |
102 | from: 0.0, | |
103 | to: 1.0, | |
104 | delay: 0.0, | |
105 | queue: 'parallel' | |
106 | }, | |
tagifyText: function(element) { | ||
var tagifyStyle = 'position:relative'; | ||
109 | | |
109 | if (Prototype.Browser.IE) tagifyStyle += ';zoom:1'; | |
110 | ||
element = $(element); | ||
$A(element.childNodes).each( function(child) { | ||
112 | | |
113 | if (child.nodeType==3) { | |
child.nodeValue.toArray().each( function(character) { | ||
element.insertBefore( | ||
115 | | |
116 | | |
116 | new Element('span', {style: tagifyStyle}).update( | |
117 | character == ' ' ? String.fromCharCode(160) : character), | |
child); | ||
}); | ||
Element.remove(child); | ||
... | ... | |
}, | ||
multiple: function(element, effect) { | ||
var elements; | ||
125 | | |
126 | | |
126 | if (((typeof element == 'object') || | |
127 | Object.isFunction(element)) && | |
(element.length)) | ||
elements = element; | ||
else | ||
elements = $(element).childNodes; | ||
131 | | |
132 | ||
var options = Object.extend({ | ||
speed: 0.1, | ||
delay: 0.0 | ||
135 | | |
136 | }, arguments[2] || { }); | |
var masterDelay = options.delay; | ||
$A(elements).each( function(element, index) { | ||
... | ... | |
effect = (effect || 'appear').toLowerCase(); | ||
var options = Object.extend({ | ||
queue: { position:'end', scope:(element.id || 'global'), limit: 1 } | ||
152 | | |
153 | | |
153 | }, arguments[2] || { }); | |
154 | Effect[element.visible() ? | |
Effect.PAIRS[effect][1] : Effect.PAIRS[effect][0]](element, options); | ||
} | ||
}; | ||
158 | ||
159 | Effect.DefaultOptions.transition = Effect.Transitions.sinoidal; | |
160 | ||
161 | ||
162 | ||
163 | ||
164 | ||
165 | | |
166 | ||
167 | ||
168 | | |
169 | ||
170 | ||
171 | | |
172 | ||
173 | ||
174 | | |
175 | ||
176 | ||
177 | | |
178 | ||
179 | ||
180 | | |
181 | | |
182 | ||
183 | ||
184 | | |
185 | ||
186 | ||
187 | | |
188 | ||
189 | ||
/* ------------- core effects ------------- */ | ||
192 | ||
193 | ||
163 | Effect.ScopedQueue = Class.create(Enumerable, { | |
initialize: function() { | ||
this.effects = []; | ||
this.interval = null; | ||
... | ... | |
}, | ||
add: function(effect) { | ||
var timestamp = new Date().getTime(); | ||
203 | | |
204 | | |
173 | ||
174 | var position = Object.isString(effect.options.queue) ? | |
effect.options.queue : effect.options.queue.position; | ||
206 | | |
176 | ||
switch(position) { | ||
case 'front': | ||
209 | | |
179 | // move unstarted effects after this effect | |
this.effects.findAll(function(e){ return e.state=='idle' }).each( function(e) { | ||
e.startOn += effect.finishOn; | ||
e.finishOn += effect.finishOn; | ||
}); | ||
break; | ||
185 | case 'with-last': | |
186 | timestamp = this.effects.pluck('startOn').max() || timestamp; | |
187 | break; | |
case 'end': | ||
// start effect after last queued effect has finished | ||
timestamp = this.effects.pluck('finishOn').max() || timestamp; | ||
break; | ||
} | ||
220 | | |
193 | ||
effect.startOn += timestamp; | ||
effect.finishOn += timestamp; | ||
224 | | |
197 | if (!effect.options.queue.limit || (this.effects.length < effect.options.queue.limit)) | |
this.effects.push(effect); | ||
226 | | |
227 | | |
228 | | |
199 | ||
200 | if (!this.interval) | |
201 | this.interval = setInterval(this.loop.bind(this), 15); | |
}, | ||
remove: function(effect) { | ||
this.effects = this.effects.reject(function(e) { return e==effect }); | ||
232 | | |
205 | if (this.effects.length == 0) { | |
clearInterval(this.interval); | ||
this.interval = null; | ||
} | ||
}, | ||
loop: function() { | ||
var timePos = new Date().getTime(); | ||
239 | | |
212 | for(var i=0, len=this.effects.length;i<len;i++) | |
213 | this.effects[i] && this.effects[i].loop(timePos); | |
} | ||
}); | ||
Effect.Queues = { | ||
instances: $H(), | ||
get: function(queueName) { | ||
246 | | |
247 | | |
248 | | |
249 | | |
250 | | |
251 | | |
220 | if (!Object.isString(queueName)) return queueName; | |
221 | ||
222 | return this.instances.get(queueName) || | |
223 | this.instances.set(queueName, new Effect.ScopedQueue()); | |
} | ||
253 | ||
225 | }; | |
Effect.Queue = Effect.Queues.get('global'); | ||
256 | ||
257 | | |
258 | | |
259 | | |
260 | | |
261 | | |
262 | | |
263 | | |
264 | | |
265 | ||
266 | ||
267 | ||
268 | ||
228 | Effect.Base = Class.create({ | |
position: null, | ||
start: function(options) { | ||
271 | | |
231 | function codeForEvent(options,eventName){ | |
232 | return ( | |
233 | (options[eventName+'Internal'] ? 'this.options.'+eventName+'Internal(this);' : '') + | |
234 | (options[eventName] ? 'this.options.'+eventName+'(this);' : '') | |
235 | ); | |
236 | } | |
237 | if (options && options.transition === false) options.transition = Effect.Transitions.linear; | |
238 | this.options = Object.extend(Object.extend({ },Effect.DefaultOptions), options || { }); | |
this.currentFrame = 0; | ||
this.state = 'idle'; | ||
this.startOn = this.options.delay*1000; | ||
275 | | |
242 | this.finishOn = this.startOn+(this.options.duration*1000); | |
243 | this.fromToDelta = this.options.to-this.options.from; | |
244 | this.totalTime = this.finishOn-this.startOn; | |
245 | this.totalFrames = this.options.fps*this.options.duration; | |
246 | ||
247 | this.render = (function() { | |
248 | function dispatch(effect, eventName) { | |
249 | if (effect.options[eventName + 'Internal']) | |
250 | effect.options[eventName + 'Internal'](effect); | |
251 | if (effect.options[eventName]) | |
252 | effect.options[eventName](effect); | |
253 | } | |
254 | ||
255 | return function(pos) { | |
256 | if (this.state === "idle") { | |
257 | this.state = "running"; | |
258 | dispatch(this, 'beforeSetup'); | |
259 | if (this.setup) this.setup(); | |
260 | dispatch(this, 'afterSetup'); | |
261 | } | |
262 | if (this.state === "running") { | |
263 | pos = (this.options.transition(pos) * this.fromToDelta) + this.options.from; | |
264 | this.position = pos; | |
265 | dispatch(this, 'beforeUpdate'); | |
266 | if (this.update) this.update(pos); | |
267 | dispatch(this, 'afterUpdate'); | |
268 | } | |
269 | }; | |
270 | })(); | |
271 | ||
this.event('beforeStart'); | ||
277 | | |
278 | | |
273 | if (!this.options.sync) | |
274 | Effect.Queues.get(Object.isString(this.options.queue) ? | |
'global' : this.options.queue.scope).add(this); | ||
}, | ||
loop: function(timePos) { | ||
282 | | |
283 | | |
278 | if (timePos >= this.startOn) { | |
279 | if (timePos >= this.finishOn) { | |
this.render(1.0); | ||
this.cancel(); | ||
this.event('beforeFinish'); | ||
287 | | |
283 | if (this.finish) this.finish(); | |
this.event('afterFinish'); | ||
289 | | |
285 | return; | |
} | ||
291 | | |
292 | | |
293 | | |
287 | var pos = (timePos - this.startOn) / this.totalTime, | |
288 | frame = (pos * this.totalFrames).round(); | |
289 | if (frame > this.currentFrame) { | |
this.render(pos); | ||
this.currentFrame = frame; | ||
} | ||
} | ||
}, | ||
299 | | |
300 | | |
301 | | |
302 | | |
303 | | |
304 | | |
305 | | |
306 | | |
307 | | |
308 | | |
309 | | |
310 | | |
311 | | |
312 | | |
313 | | |
314 | | |
315 | | |
cancel: function() { | ||
317 | | |
318 | | |
296 | if (!this.options.sync) | |
297 | Effect.Queues.get(Object.isString(this.options.queue) ? | |
'global' : this.options.queue.scope).remove(this); | ||
this.state = 'finished'; | ||
}, | ||
event: function(eventName) { | ||
323 | | |
324 | | |
302 | if (this.options[eventName + 'Internal']) this.options[eventName + 'Internal'](this); | |
303 | if (this.options[eventName]) this.options[eventName](this); | |
}, | ||
inspect: function() { | ||
327 | | |
306 | var data = $H(); | |
307 | for(property in this) | |
308 | if (!Object.isFunction(this[property])) data.set(property, this[property]); | |
309 | return '#<Effect:' + data.inspect() + ',options:' + $H(this.options).inspect() + '>'; | |
} | ||
329 | ||
311 | }); | |
331 | ||
332 | ||
313 | Effect.Parallel = Class.create(Effect.Base, { | |
initialize: function(effects) { | ||
this.effects = effects || []; | ||
this.start(arguments[1]); | ||
... | ... | |
effect.render(1.0); | ||
effect.cancel(); | ||
effect.event('beforeFinish'); | ||
345 | | |
326 | if (effect.finish) effect.finish(position); | |
effect.event('afterFinish'); | ||
}); | ||
} | ||
}); | ||
351 | ||
352 | ||
332 | Effect.Tween = Class.create(Effect.Base, { | |
333 | initialize: function(object, from, to) { | |
334 | object = Object.isString(object) ? $(object) : object; | |
335 | var args = $A(arguments), method = args.last(), | |
336 | options = args.length == 5 ? args[3] : null; | |
337 | this.method = Object.isFunction(method) ? method.bind(object) : | |
338 | Object.isFunction(object[method]) ? object[method].bind(object) : | |
339 | function(value) { object[method] = value }; | |
340 | this.start(Object.extend({ from: from, to: to }, options || { })); | |
341 | }, | |
342 | update: function(position) { | |
343 | this.method(position); | |
344 | } | |
345 | }); | |
346 | ||
347 | Effect.Event = Class.create(Effect.Base, { | |
348 | initialize: function() { | |
349 | this.start(Object.extend({ duration: 0 }, arguments[0] || { })); | |
350 | }, | |
351 | update: Prototype.emptyFunction | |
352 | }); | |
353 | ||
354 | Effect.Opacity = Class.create(Effect.Base, { | |
initialize: function(element) { | ||
this.element = $(element); | ||
357 | if (!this.element) throw(Effect._elementDoesNotExistError); | |
// make this work on IE on elements without 'layout' | ||
356 | | |
359 | if (Prototype.Browser.IE && (!this.element.currentStyle.hasLayout)) | |
this.element.setStyle({zoom: 1}); | ||
var options = Object.extend({ | ||
from: this.element.getOpacity() || 0.0, | ||
to: 1.0 | ||
361 | | |
364 | }, arguments[1] || { }); | |
this.start(options); | ||
}, | ||
update: function(position) { | ||
... | ... | |
} | ||
}); | ||
369 | ||
370 | ||
372 | Effect.Move = Class.create(Effect.Base, { | |
initialize: function(element) { | ||
this.element = $(element); | ||
375 | if (!this.element) throw(Effect._elementDoesNotExistError); | |
var options = Object.extend({ | ||
x: 0, | ||
y: 0, | ||
mode: 'relative' | ||
377 | | |
380 | }, arguments[1] || { }); | |
this.start(options); | ||
}, | ||
setup: function() { | ||
381 | | |
382 | | |
383 | | |
384 | | |
this.element.makePositioned(); | ||
this.originalLeft = parseFloat(this.element.getStyle('left') || '0'); | ||
this.originalTop = parseFloat(this.element.getStyle('top') || '0'); | ||
388 | | |
389 | | |
387 | if (this.options.mode == 'absolute') { | |
this.options.x = this.options.x - this.originalLeft; | ||
this.options.y = this.options.y - this.originalTop; | ||
} | ||
}, | ||
update: function(position) { | ||
this.element.setStyle({ | ||
396 | | |
397 | | |
394 | left: (this.options.x * position + this.originalLeft).round() + 'px', | |
395 | top: (this.options.y * position + this.originalTop).round() + 'px' | |
}); | ||
} | ||
}); | ||
// for backwards compatibility | ||
Effect.MoveBy = function(element, toTop, toLeft) { | ||
404 | | |
405 | | |
402 | return new Effect.Move(element, | |
403 | Object.extend({ x: toLeft, y: toTop }, arguments[3] || { })); | |
}; | ||
408 | ||
409 | ||
406 | Effect.Scale = Class.create(Effect.Base, { | |
initialize: function(element, percent) { | ||
411 | | |
408 | this.element = $(element); | |
409 | if (!this.element) throw(Effect._elementDoesNotExistError); | |
var options = Object.extend({ | ||
scaleX: true, | ||
scaleY: true, | ||
scaleContent: true, | ||
scaleFromCenter: false, | ||
417 | | |
415 | scaleMode: 'box', // 'box' or 'contents' or { } with provided values | |
scaleFrom: 100.0, | ||
scaleTo: percent | ||
420 | | |
418 | }, arguments[2] || { }); | |
this.start(options); | ||
}, | ||
setup: function() { | ||
this.restoreAfterFinish = this.options.restoreAfterFinish || false; | ||
this.elementPositioning = this.element.getStyle('position'); | ||
426 | | |
427 | | |
424 | ||
425 | this.originalStyle = { }; | |
['top','left','width','height','fontSize'].each( function(k) { | ||
this.originalStyle[k] = this.element.style[k]; | ||
}.bind(this)); | ||
431 | | |
429 | ||
this.originalTop = this.element.offsetTop; | ||
this.originalLeft = this.element.offsetLeft; | ||
434 | | |
432 | ||
var fontSize = this.element.getStyle('font-size') || '100%'; | ||
436 | | |
437 | | |
434 | ['em','px','%','pt'].each( function(fontSizeType) { | |
435 | if (fontSize.indexOf(fontSizeType)>0) { | |
this.fontSize = parseFloat(fontSize); | ||
this.fontSizeType = fontSizeType; | ||
} | ||
}.bind(this)); | ||
442 | | |
440 | ||
this.factor = (this.options.scaleTo - this.options.scaleFrom)/100; | ||
444 | | |
442 | ||
this.dims = null; | ||
446 | | |
444 | if (this.options.scaleMode=='box') | |
this.dims = [this.element.offsetHeight, this.element.offsetWidth]; | ||
448 | | |
446 | if (/^content/.test(this.options.scaleMode)) | |
this.dims = [this.element.scrollHeight, this.element.scrollWidth]; | ||
450 | | |
448 | if (!this.dims) | |
this.dims = [this.options.scaleMode.originalHeight, | ||
this.options.scaleMode.originalWidth]; | ||
}, | ||
update: function(position) { | ||
var currentScale = (this.options.scaleFrom/100.0) + (this.factor * position); | ||
456 | | |
454 | if (this.options.scaleContent && this.fontSize) | |
this.element.setStyle({fontSize: this.fontSize * currentScale + this.fontSizeType }); | ||
this.setDimensions(this.dims[0] * currentScale, this.dims[1] * currentScale); | ||
}, | ||
... | ... | |
if (this.restoreAfterFinish) this.element.setStyle(this.originalStyle); | ||
}, | ||
setDimensions: function(height, width) { | ||
464 | | |
465 | | |
466 | | |
467 | | |
462 | var d = { }; | |
463 | if (this.options.scaleX) d.width = width.round() + 'px'; | |
464 | if (this.options.scaleY) d.height = height.round() + 'px'; | |
465 | if (this.options.scaleFromCenter) { | |
var topd = (height - this.dims[0])/2; | ||
var leftd = (width - this.dims[1])/2; | ||
470 | | |
471 | | |
472 | | |
468 | if (this.elementPositioning == 'absolute') { | |
469 | if (this.options.scaleY) d.top = this.originalTop-topd + 'px'; | |
470 | if (this.options.scaleX) d.left = this.originalLeft-leftd + 'px'; | |
} else { | ||
474 | | |
475 | | |
472 | if (this.options.scaleY) d.top = -topd + 'px'; | |
473 | if (this.options.scaleX) d.left = -leftd + 'px'; | |
} | ||
} | ||
this.element.setStyle(d); | ||
} | ||
}); | ||
482 | ||
483 | ||
480 | Effect.Highlight = Class.create(Effect.Base, { | |
initialize: function(element) { | ||
this.element = $(element); | ||
486 | | |
483 | if (!this.element) throw(Effect._elementDoesNotExistError); | |
484 | var options = Object.extend({ startcolor: '#ffff99' }, arguments[1] || { }); | |
this.start(options); | ||
}, | ||
setup: function() { | ||
// Prevent executing on elements not in the layout flow | ||
491 | | |
489 | if (this.element.getStyle('display')=='none') { this.cancel(); return; } | |
// Disable background image during the effect | ||
493 | | |
494 | | |
495 | | |
496 | | |
491 | this.oldStyle = { }; | |
492 | if (!this.options.keepBackgroundImage) { | |
493 | this.oldStyle.backgroundImage = this.element.getStyle('background-image'); | |
494 | this.element.setStyle({backgroundImage: 'none'}); | |
495 | } | |
496 | if (!this.options.endcolor) | |
this.options.endcolor = this.element.getStyle('background-color').parseColor('#ffffff'); | ||
498 | | |
498 | if (!this.options.restorecolor) | |
this.options.restorecolor = this.element.getStyle('background-color'); | ||
// init color calculations | ||
this._base = $R(0,2).map(function(i){ return parseInt(this.options.startcolor.slice(i*2+1,i*2+3),16) }.bind(this)); | ||
... | ... | |
}, | ||
update: function(position) { | ||
this.element.setStyle({backgroundColor: $R(0,2).inject('#',function(m,v,i){ | ||
506 | | |
506 | return m+((this._base[i]+(this._delta[i]*position)).round().toColorPart()); }.bind(this)) }); | |
}, | ||
finish: function() { | ||
this.element.setStyle(Object.extend(this.oldStyle, { | ||
... | ... | |
} | ||
}); | ||
515 | ||
516 | ||
517 | | |
518 | | |
519 | | |
520 | | |
521 | | |
522 | | |
523 | | |
524 | | |
525 | | |
526 | | |
527 | | |
528 | | |
529 | | |
530 | | |
531 | | |
532 | | |
533 | | |
534 | | |
535 | | |
536 | | |
537 | | |
538 | ||
515 | Effect.ScrollTo = function(element) { | |
516 | var options = arguments[1] || { }, | |
517 | scrollOffsets = document.viewport.getScrollOffsets(), | |
518 | elementOffsets = $(element).cumulativeOffset(); | |
520 | if (options.offset) elementOffsets[1] += options.offset; | |
521 | ||
522 | return new Effect.Tween(null, | |
523 | scrollOffsets.top, | |
524 | elementOffsets[1], | |
525 | options, | |
526 | function(p){ scrollTo(scrollOffsets.left, p.round()); } | |
527 | ); | |
528 | }; | |
529 | ||
/* ------------- combination effects ------------- */ | ||
Effect.Fade = function(element) { | ||
element = $(element); | ||
var oldOpacity = element.getInlineOpacity(); | ||
var options = Object.extend({ | ||
546 | | |
547 | | |
548 | | |
549 | | |
550 | | |
551 | | |
552 | | |
536 | from: element.getOpacity() || 1.0, | |
537 | to: 0.0, | |
538 | afterFinishInternal: function(effect) { | |
539 | if (effect.options.to!=0) return; | |
540 | effect.element.hide().setStyle({opacity: oldOpacity}); | |
541 | } | |
542 | }, arguments[1] || { }); | |
return new Effect.Opacity(element,options); | ||
554 | ||
544 | }; | |
Effect.Appear = function(element) { | ||
element = $(element); | ||
... | ... | |
effect.element.forceRerendering(); | ||
}, | ||
beforeSetup: function(effect) { | ||
566 | | |
567 | | |
568 | | |
556 | effect.element.setOpacity(effect.options.from).show(); | |
557 | }}, arguments[1] || { }); | |
return new Effect.Opacity(element,options); | ||
570 | ||
559 | }; | |
Effect.Puff = function(element) { | ||
element = $(element); | ||
574 | | |
563 | var oldStyle = { | |
564 | opacity: element.getInlineOpacity(), | |
565 | position: element.getStyle('position'), | |
566 | top: element.style.top, | |
567 | left: element.style.left, | |
568 | width: element.style.width, | |
569 | height: element.style.height | |
570 | }; | |
return new Effect.Parallel( | ||
576 | | |
577 | | |
578 | | |
579 | | |
572 | [ new Effect.Scale(element, 200, | |
573 | { sync: true, scaleFromCenter: true, scaleContent: true, restoreAfterFinish: true }), | |
574 | new Effect.Opacity(element, { sync: true, to: 0.0 } ) ], | |
575 | Object.extend({ duration: 1.0, | |
beforeSetupInternal: function(effect) { | ||
581 | | |
577 | Position.absolutize(effect.effects[0].element); | |
578 | }, | |
afterFinishInternal: function(effect) { | ||
583 | | |
584 | | |
585 | | |
580 | effect.effects[0].element.hide().setStyle(oldStyle); } | |
581 | }, arguments[1] || { }) | |
); | ||
587 | ||
583 | }; | |
Effect.BlindUp = function(element) { | ||
element = $(element); | ||
element.makeClipping(); | ||
592 | | |
593 | | |
594 | | |
588 | return new Effect.Scale(element, 0, | |
589 | Object.extend({ scaleContent: false, | |
590 | scaleX: false, | |
restoreAfterFinish: true, | ||
afterFinishInternal: function(effect) { | ||
597 | | |
598 | | |
599 | | |
600 | | |
593 | effect.element.hide().undoClipping(); | |
594 | } | |
595 | }, arguments[1] || { }) | |
); | ||
602 | ||
597 | }; | |
Effect.BlindDown = function(element) { | ||
element = $(element); | ||
var elementDimensions = element.getDimensions(); | ||
607 | | |
608 | | |
609 | | |
610 | | |
611 | | |
612 | | |
613 | | |
614 | | |
615 | | |
616 | | |
617 | | |
618 | | |
619 | | |
620 | | |
621 | | |
622 | | |
623 | ||
602 | return new Effect.Scale(element, 100, Object.extend({ | |
603 | scaleContent: false, | |
604 | scaleX: false, | |
605 | scaleFrom: 0, | |
606 | scaleMode: {originalHeight: elementDimensions.height, originalWidth: elementDimensions.width}, | |
607 | restoreAfterFinish: true, | |
608 | afterSetup: function(effect) { | |
609 | effect.element.makeClipping().setStyle({height: '0px'}).show(); | |
610 | }, | |
611 | afterFinishInternal: function(effect) { | |
612 | effect.element.undoClipping(); | |
613 | } | |
614 | }, arguments[1] || { })); | |
615 | }; | |
Effect.SwitchOff = function(element) { | ||
element = $(element); | ||
var oldOpacity = element.getInlineOpacity(); | ||
628 | | |
620 | return new Effect.Appear(element, Object.extend({ | |
duration: 0.4, | ||
from: 0, | ||
transition: Effect.Transitions.flicker, | ||
afterFinishInternal: function(effect) { | ||
633 | | |
625 | new Effect.Scale(effect.element, 1, { | |
duration: 0.3, scaleFromCenter: true, | ||
scaleX: false, scaleContent: false, restoreAfterFinish: true, | ||
636 | | |
637 | | |
638 | | |
628 | beforeSetup: function(effect) { | |
629 | effect.element.makePositioned().makeClipping(); | |
}, | ||
afterFinishInternal: function(effect) { | ||
641 | | |
642 | | |
643 | | |
644 | | |
632 | effect.element.hide().undoClipping().undoPositioned().setStyle({opacity: oldOpacity}); | |
} | ||
646 | | |
634 | }); | |
} | ||
648 | | |
649 | ||
636 | }, arguments[1] || { })); | |
637 | }; | |
Effect.DropOut = function(element) { | ||
element = $(element); | ||
... | ... | |
left: element.getStyle('left'), | ||
opacity: element.getInlineOpacity() }; | ||
return new Effect.Parallel( | ||
658 | | |
646 | [ new Effect.Move(element, {x: 0, y: 100, sync: true }), | |
new Effect.Opacity(element, { sync: true, to: 0.0 }) ], | ||
Object.extend( | ||
{ duration: 0.5, | ||
beforeSetup: function(effect) { | ||
663 | | |
651 | effect.effects[0].element.makePositioned(); | |
}, | ||
afterFinishInternal: function(effect) { | ||
666 | | |
667 | | |
668 | | |
669 | | |
670 | | |
671 | ||
654 | effect.effects[0].element.hide().undoPositioned().setStyle(oldStyle); | |
655 | } | |
656 | }, arguments[1] || { })); | |
657 | }; | |
Effect.Shake = function(element) { | ||
element = $(element); | ||
661 | var options = Object.extend({ | |
662 | distance: 20, | |
663 | duration: 0.5 | |
664 | }, arguments[1] || {}); | |
665 | var distance = parseFloat(options.distance); | |
666 | var split = parseFloat(options.duration) / 10.0; | |
var oldStyle = { | ||
top: element.getStyle('top'), | ||
left: element.getStyle('left') }; | ||
678 | | |
679 | | |
670 | return new Effect.Move(element, | |
671 | { x: distance, y: 0, duration: split, afterFinishInternal: function(effect) { | |
new Effect.Move(effect.element, | ||
681 | | |
673 | { x: -distance*2, y: 0, duration: split*2, afterFinishInternal: function(effect) { | |
new Effect.Move(effect.element, | ||
683 | | |
675 | { x: distance*2, y: 0, duration: split*2, afterFinishInternal: function(effect) { | |
new Effect.Move(effect.element, | ||
685 | | |
677 | { x: -distance*2, y: 0, duration: split*2, afterFinishInternal: function(effect) { | |
new Effect.Move(effect.element, | ||
687 | | |
679 | { x: distance*2, y: 0, duration: split*2, afterFinishInternal: function(effect) { | |
new Effect.Move(effect.element, | ||
689 | | |
690 | | |
691 | | |
692 | | |
693 | ||
681 | { x: -distance, y: 0, duration: split, afterFinishInternal: function(effect) { | |
682 | effect.element.undoPositioned().setStyle(oldStyle); | |
683 | }}); }}); }}); }}); }}); }}); | |
684 | }; | |
Effect.SlideDown = function(element) { | ||
696 | | |
697 | | |
687 | element = $(element).cleanWhitespace(); | |
// SlideDown need to have the content of the element wrapped in a container element with fixed height! | ||
699 | | |
689 | var oldInnerBottom = element.down().getStyle('bottom'); | |
var elementDimensions = element.getDimensions(); | ||
701 | | |
702 | | |
703 | | |
704 | | |
691 | return new Effect.Scale(element, 100, Object.extend({ | |
692 | scaleContent: false, | |
693 | scaleX: false, | |
694 | scaleFrom: window.opera ? 0 : 1, | |
scaleMode: {originalHeight: elementDimensions.height, originalWidth: elementDimensions.width}, | ||
restoreAfterFinish: true, | ||
afterSetup: function(effect) { | ||
effect.element.makePositioned(); | ||
709 | | |
710 | | |
711 | | |
712 | | |
713 | | |
699 | effect.element.down().makePositioned(); | |
700 | if (window.opera) effect.element.setStyle({top: ''}); | |
701 | effect.element.makeClipping().setStyle({height: '0px'}).show(); | |
702 | }, | |
afterUpdateInternal: function(effect) { | ||
715 | | |
716 | | |
704 | effect.element.down().setStyle({bottom: | |
705 | (effect.dims[0] - effect.element.clientHeight) + 'px' }); | |
}, | ||
afterFinishInternal: function(effect) { | ||
719 | | |
720 | | |
721 | | |
722 | | |
723 | | |
724 | | |
725 | | |
726 | | |
727 | | |
728 | | |
729 | | |
708 | effect.element.undoClipping().undoPositioned(); | |
709 | effect.element.down().undoPositioned().setStyle({bottom: oldInnerBottom}); } | |
710 | }, arguments[1] || { }) | |
); | ||
731 | ||
732 | | |
712 | }; | |
713 | ||
Effect.SlideUp = function(element) { | ||
734 | | |
735 | | |
736 | | |
737 | | |
738 | | |
739 | | |
715 | element = $(element).cleanWhitespace(); | |
716 | var oldInnerBottom = element.down().getStyle('bottom'); | |
717 | var elementDimensions = element.getDimensions(); | |
718 | return new Effect.Scale(element, window.opera ? 0 : 1, | |
719 | Object.extend({ scaleContent: false, | |
720 | scaleX: false, | |
scaleMode: 'box', | ||
scaleFrom: 100, | ||
723 | scaleMode: {originalHeight: elementDimensions.height, originalWidth: elementDimensions.width}, | |
restoreAfterFinish: true, | ||
743 | | |
725 | afterSetup: function(effect) { | |
effect.element.makePositioned(); | ||
745 | | |
746 | | |
747 | | |
748 | | |
727 | effect.element.down().makePositioned(); | |
728 | if (window.opera) effect.element.setStyle({top: ''}); | |
729 | effect.element.makeClipping().show(); | |
730 | }, | |
afterUpdateInternal: function(effect) { | ||
750 | | |
751 | | |
732 | effect.element.down().setStyle({bottom: | |
733 | (effect.dims[0] - effect.element.clientHeight) + 'px' }); | |
734 | }, | |
afterFinishInternal: function(effect) { | ||
753 | | |
754 | | |
755 | | |
756 | | |
757 | | |
758 | | |
736 | effect.element.hide().undoClipping().undoPositioned(); | |
737 | effect.element.down().undoPositioned().setStyle({bottom: oldInnerBottom}); | |
738 | } | |
739 | }, arguments[1] || { }) | |
); | ||
760 | ||
741 | }; | |
762 | ||
743 | // Bug in opera makes the TD containing this element expand for a instance after finish | |
Effect.Squish = function(element) { | ||
764 | | |
765 | | |
766 | | |
767 | | |
768 | | |
769 | | |
770 | | |
745 | return new Effect.Scale(element, window.opera ? 1 : 0, { | |
746 | restoreAfterFinish: true, | |
747 | beforeSetup: function(effect) { | |
748 | effect.element.makeClipping(); | |
749 | }, | |
750 | afterFinishInternal: function(effect) { | |
751 | effect.element.hide().undoClipping(); | |
752 | } | |
}); | ||
772 | ||
754 | }; | |
Effect.Grow = function(element) { | ||
element = $(element); | ||
... | ... | |
moveTransition: Effect.Transitions.sinoidal, | ||
scaleTransition: Effect.Transitions.sinoidal, | ||
opacityTransition: Effect.Transitions.full | ||
781 | | |
763 | }, arguments[1] || { }); | |
var oldStyle = { | ||
top: element.style.top, | ||
left: element.style.left, | ||
... | ... | |
width: element.style.width, | ||
opacity: element.getInlineOpacity() }; | ||
789 | | |
771 | var dims = element.getDimensions(); | |
var initialMoveX, initialMoveY; | ||
var moveX, moveY; | ||
792 | | |
774 | ||
switch (options.direction) { | ||
case 'top-left': | ||
795 | | |
777 | initialMoveX = initialMoveY = moveX = moveY = 0; | |
break; | ||
case 'top-right': | ||
initialMoveX = dims.width; | ||
... | ... | |
moveY = -dims.height / 2; | ||
break; | ||
} | ||
820 | | |
802 | ||
return new Effect.Move(element, { | ||
x: initialMoveX, | ||
y: initialMoveY, | ||
824 | | |
806 | duration: 0.01, | |
beforeSetup: function(effect) { | ||
826 | | |
827 | | |
828 | | |
808 | effect.element.hide().makeClipping().makePositioned(); | |
}, | ||
afterFinishInternal: function(effect) { | ||
new Effect.Parallel( | ||
[ new Effect.Opacity(effect.element, { sync: true, to: 1.0, from: 0.0, transition: options.opacityTransition }), | ||
new Effect.Move(effect.element, { x: moveX, y: moveY, sync: true, transition: options.moveTransition }), | ||
new Effect.Scale(effect.element, 100, { | ||
835 | | |
815 | scaleMode: { originalHeight: dims.height, originalWidth: dims.width }, | |
sync: true, scaleFrom: window.opera ? 1 : 0, transition: options.scaleTransition, restoreAfterFinish: true}) | ||
], Object.extend({ | ||
beforeSetup: function(effect) { | ||
839 | | |
840 | | |
819 | effect.effects[0].element.setStyle({height: '0px'}).show(); | |
}, | ||
afterFinishInternal: function(effect) { | ||
843 | | |
844 | | |
845 | | |
822 | effect.effects[0].element.undoClipping().undoPositioned().setStyle(oldStyle); | |
} | ||
}, options) | ||
848 | | |
825 | ); | |
} | ||
}); | ||
851 | ||
828 | }; | |
Effect.Shrink = function(element) { | ||
element = $(element); | ||
... | ... | |
moveTransition: Effect.Transitions.sinoidal, | ||
scaleTransition: Effect.Transitions.sinoidal, | ||
opacityTransition: Effect.Transitions.none | ||
860 | | |
837 | }, arguments[1] || { }); | |
var oldStyle = { | ||
top: element.style.top, | ||
left: element.style.left, | ||
... | ... | |
var dims = element.getDimensions(); | ||
var moveX, moveY; | ||
870 | | |
847 | ||
switch (options.direction) { | ||
case 'top-left': | ||
moveX = moveY = 0; | ||
... | ... | |
moveX = dims.width; | ||
moveY = dims.height; | ||
break; | ||
887 | | |
864 | case 'center': | |
moveX = dims.width / 2; | ||
moveY = dims.height / 2; | ||
break; | ||
} | ||
892 | | |
869 | ||
return new Effect.Parallel( | ||
[ new Effect.Opacity(element, { sync: true, to: 0.0, from: 1.0, transition: options.opacityTransition }), | ||
new Effect.Scale(element, window.opera ? 1 : 0, { sync: true, transition: options.scaleTransition, restoreAfterFinish: true}), | ||
new Effect.Move(element, { x: moveX, y: moveY, sync: true, transition: options.moveTransition }) | ||
897 | | |
874 | ], Object.extend({ | |
beforeStartInternal: function(effect) { | ||
899 | | |
900 | | |
876 | effect.effects[0].element.makePositioned().makeClipping(); | |
877 | }, | |
afterFinishInternal: function(effect) { | ||
902 | | |
903 | | |
904 | | |
905 | | |
879 | effect.effects[0].element.hide().undoClipping().undoPositioned().setStyle(oldStyle); } | |
}, options) | ||
); | ||
908 | ||
882 | }; | |
Effect.Pulsate = function(element) { | ||
element = $(element); | ||
912 | | |
913 | | |
914 | | |
915 | | |
916 | | |
917 | | |
918 | | |
886 | var options = arguments[1] || { }, | |
887 | oldOpacity = element.getInlineOpacity(), | |
888 | transition = options.transition || Effect.Transitions.linear, | |
889 | reverser = function(pos){ | |
890 | return 1 - transition((-Math.cos((pos*(options.pulses||5)*2)*Math.PI)/2) + .5); | |
891 | }; | |
892 | ||
893 | return new Effect.Opacity(element, | |
894 | Object.extend(Object.extend({ duration: 2.0, from: 0, | |
afterFinishInternal: function(effect) { effect.element.setStyle({opacity: oldOpacity}); } | ||
}, options), {transition: reverser})); | ||
921 | ||
897 | }; | |
Effect.Fold = function(element) { | ||
element = $(element); | ||
... | ... | |
left: element.style.left, | ||
width: element.style.width, | ||
height: element.style.height }; | ||
930 | | |
931 | | |
906 | element.makeClipping(); | |
907 | return new Effect.Scale(element, 5, Object.extend({ | |
scaleContent: false, | ||
scaleX: false, | ||
afterFinishInternal: function(effect) { | ||
935 | | |
936 | | |
911 | new Effect.Scale(element, 1, { | |
912 | scaleContent: false, | |
scaleY: false, | ||
afterFinishInternal: function(effect) { | ||
939 | | |
940 | | |
941 | | |
915 | effect.element.hide().undoClipping().setStyle(oldStyle); | |
} }); | ||
943 | | |
917 | }}, arguments[1] || { })); | |
}; | ||
946 | ||
947 | | |
948 | | |
949 | ||
920 | Effect.Morph = Class.create(Effect.Base, { | |
921 | initialize: function(element) { | |
922 | this.element = $(element); | |
923 | if (!this.element) throw(Effect._elementDoesNotExistError); | |
924 | var options = Object.extend({ | |
925 | style: { } | |
926 | }, arguments[1] || { }); | |
951 | ||
952 | | |
953 | | |
954 | | |
955 | | |
928 | if (!Object.isString(options.style)) this.style = $H(options.style); | |
929 | else { | |
930 | if (options.style.include(':')) | |
931 | this.style = options.style.parseStyle(); | |
932 | else { | |
933 | this.element.addClassName(options.style); | |
934 | this.style = $H(this.element.getStyles()); | |
935 | this.element.removeClassName(options.style); | |
936 | var css = this.element.getStyles(); | |
937 | this.style = this.style.reject(function(style) { | |
938 | return style.value == css[style.key]; | |
939 | }); | |
940 | options.afterFinishInternal = function(effect) { | |
941 | effect.element.addClassName(effect.options.style); | |
942 | effect.transforms.each(function(transform) { | |
943 | effect.element.style[transform.style] = ''; | |
944 | }); | |
945 | }; | |
946 | } | |
947 | } | |
948 | this.start(options); | |
949 | }, | |
950 | ||
951 | setup: function(){ | |
952 | function parseColor(color){ | |
953 | if (!color || ['rgba(0, 0, 0, 0)','transparent'].include(color)) color = '#ffffff'; | |
954 | color = color.parseColor(); | |
955 | return $R(0,2).map(function(i){ | |
956 | return parseInt( color.slice(i*2+1,i*2+3), 16 ); | |
957 | }); | |
958 | } | |
959 | this.transforms = this.style.map(function(pair){ | |
960 | var property = pair[0], value = pair[1], unit = null; | |
961 | ||
962 | if (value.parseColor('#zzzzzz') != '#zzzzzz') { | |
963 | value = value.parseColor(); | |
964 | unit = 'color'; | |
965 | } else if (property == 'opacity') { | |
966 | value = parseFloat(value); | |
967 | if (Prototype.Browser.IE && (!this.element.currentStyle.hasLayout)) | |
968 | this.element.setStyle({zoom: 1}); | |
969 | } else if (Element.CSS_LENGTH.test(value)) { | |
970 | var components = value.match(/^([\+\-]?[0-9\.]+)(.*)$/); | |
971 | value = parseFloat(components[1]); | |
972 | unit = (components.length == 3) ? components[2] : null; | |
973 | } | |
974 | ||
975 | var originalValue = this.element.getStyle(property); | |
976 | return { | |
977 | style: property.camelize(), | |
978 | originalValue: unit=='color' ? parseColor(originalValue) : parseFloat(originalValue || 0), | |
979 | targetValue: unit=='color' ? parseColor(value) : value, | |
980 | unit: unit | |
981 | }; | |
982 | }.bind(this)).reject(function(transform){ | |
983 | return ( | |
984 | (transform.originalValue == transform.targetValue) || | |
985 | ( | |
986 | transform.unit != 'color' && | |
987 | (isNaN(transform.originalValue) || isNaN(transform.targetValue)) | |
988 | ) | |
989 | ); | |
990 | }); | |
991 | }, | |
992 | update: function(position) { | |
993 | var style = { }, transform, i = this.transforms.length; | |
994 | while(i--) | |
995 | style[(transform = this.transforms[i]).style] = | |
996 | transform.unit=='color' ? '#'+ | |
997 | (Math.round(transform.originalValue[0]+ | |
998 | (transform.targetValue[0]-transform.originalValue[0])*position)).toColorPart() + | |
999 | (Math.round(transform.originalValue[1]+ | |
1000 | (transform.targetValue[1]-transform.originalValue[1])*position)).toColorPart() + | |
1001 | (Math.round(transform.originalValue[2]+ | |
1002 | (transform.targetValue[2]-transform.originalValue[2])*position)).toColorPart() : | |
1003 | (transform.originalValue + | |
1004 | (transform.targetValue - transform.originalValue) * position).toFixed(3) + | |
1005 | (transform.unit === null ? '' : transform.unit); | |
1006 | this.element.setStyle(style, true); | |
1007 | } | |
1008 | }); | |
1009 | ||
1010 | Effect.Transform = Class.create({ | |
1011 | initialize: function(tracks){ | |
1012 | this.tracks = []; | |
1013 | this.options = arguments[1] || { }; | |
1014 | this.addTracks(tracks); | |
1015 | }, | |
1016 | addTracks: function(tracks){ | |
1017 | tracks.each(function(track){ | |
1018 | track = $H(track); | |
1019 | var data = track.values().first(); | |
1020 | this.tracks.push($H({ | |
1021 | ids: track.keys().first(), | |
1022 | effect: Effect.Morph, | |
1023 | options: { style: data } | |
1024 | })); | |
1025 | }.bind(this)); | |
1026 | return this; | |
1027 | }, | |
1028 | play: function(){ | |
1029 | return new Effect.Parallel( | |
1030 | this.tracks.map(function(track){ | |
1031 | var ids = track.get('ids'), effect = track.get('effect'), options = track.get('options'); | |
1032 | var elements = [$(ids) || $$(ids)].flatten(); | |
1033 | return elements.map(function(e){ return new effect(e, Object.extend({ sync:true }, options)) }); | |
1034 | }).flatten(), | |
1035 | this.options | |
1036 | ); | |
1037 | } | |
1038 | }); | |
1039 | ||
1040 | Element.CSS_PROPERTIES = $w( | |
1041 | 'backgroundColor backgroundPosition borderBottomColor borderBottomStyle ' + | |
1042 | 'borderBottomWidth borderLeftColor borderLeftStyle borderLeftWidth ' + | |
1043 | 'borderRightColor borderRightStyle borderRightWidth borderSpacing ' + | |
1044 | 'borderTopColor borderTopStyle borderTopWidth bottom clip color ' + | |
1045 | 'fontSize fontWeight height left letterSpacing lineHeight ' + | |
1046 | 'marginBottom marginLeft marginRight marginTop markerOffset maxHeight '+ | |
1047 | 'maxWidth minHeight minWidth opacity outlineColor outlineOffset ' + | |
1048 | 'outlineWidth paddingBottom paddingLeft paddingRight paddingTop ' + | |
1049 | 'right textIndent top width wordSpacing zIndex'); | |
1050 | ||
1051 | Element.CSS_LENGTH = /^(([\+\-]?[0-9\.]+)(em|ex|px|in|cm|mm|pt|pc|\%))|0$/; | |
1052 | ||
1053 | String.__parseStyleElement = document.createElement('div'); | |
1054 | String.prototype.parseStyle = function(){ | |
1055 | var style, styleRules = $H(); | |
1056 | if (Prototype.Browser.WebKit) | |
1057 | style = new Element('div',{style:this}).style; | |
1058 | else { | |
1059 | String.__parseStyleElement.innerHTML = '<div style="' + this + '"></div>'; | |
1060 | style = String.__parseStyleElement.childNodes[0].style; | |
1061 | } | |
1062 | ||
1063 | Element.CSS_PROPERTIES.each(function(property){ | |
1064 | if (style[property]) styleRules.set(property, style[property]); | |
1065 | }); | |
1066 | ||
1067 | if (Prototype.Browser.IE && this.include('opacity')) | |
1068 | styleRules.set('opacity', this.match(/opacity:\s*((?:0|1)?(?:\.\d*)?)/)[1]); | |
1069 | ||
1070 | return styleRules; | |
}; | ||
958 | ||
1074 | if (document.defaultView && document.defaultView.getComputedStyle) { | |
1075 | Element.getStyles = function(element) { | |
1076 | var css = document.defaultView.getComputedStyle($(element), null); | |
1077 | return Element.CSS_PROPERTIES.inject({ }, function(styles, property) { | |
1078 | styles[property] = css[property]; | |
1079 | return styles; | |
1080 | }); | |
1081 | }; | |
1082 | } else { | |
1083 | Element.getStyles = function(element) { | |
1084 | element = $(element); | |
1085 | var css = element.currentStyle, styles; | |
1086 | styles = Element.CSS_PROPERTIES.inject({ }, function(results, property) { | |
1087 | results[property] = css[property]; | |
1088 | return results; | |
1089 | }); | |
1090 | if (!styles.opacity) styles.opacity = element.getOpacity(); | |
1091 | return styles; | |
1092 | }; | |
1093 | } | |
1094 | ||
1095 | Effect.Methods = { | |
1096 | morph: function(element, style) { | |
1097 | element = $(element); | |
1098 | new Effect.Morph(element, Object.extend({ style: style }, arguments[2] || { })); | |
1099 | return element; | |
1100 | }, | |
1101 | visualEffect: function(element, effect, options) { | |
1102 | element = $(element); | |
1103 | var s = effect.dasherize().camelize(), klass = s.charAt(0).toUpperCase() + s.substring(1); | |
1104 | new Effect[klass](element, options); | |
1105 | return element; | |
1106 | }, | |
1107 | highlight: function(element, options) { | |
1108 | element = $(element); | |
1109 | new Effect.Highlight(element, options); | |
1110 | return element; | |
1111 | } | |
1112 | }; | |
1113 | ||
1114 | $w('fade appear grow shrink fold blindUp blindDown slideUp slideDown '+ | |
1115 | 'pulsate shake puff squish switchOff dropOut').each( | |
1116 | function(effect) { | |
1117 | Effect.Methods[effect] = function(element, options){ | |
1118 | element = $(element); | |
1119 | Effect[effect.charAt(0).toUpperCase() + effect.substring(1)](element, options); | |
1120 | return element; | |
1121 | }; | |
1122 | } | |
1123 | ); | |
1124 | ||
1125 | $w('getInlineOpacity forceRerendering setContentZoom collectTextNodes collectTextNodesIgnoreClass getStyles').each( | |
1126 | function(f) { Effect.Methods[f] = Element[f]; } | |
1127 | ); | |
1128 | ||
1129 | Element.addMethods(Effect.Methods); | |
rool/rails/hub/trunk/public/javascripts/prototype.js:
prev. | current | |
1 | ||
2 | | |
1 | /* Prototype JavaScript framework, version 1.6.0.3 | |
2 | * (c) 2005-2008 Sam Stephenson | |
* | ||
* Prototype is freely distributable under the terms of an MIT-style license. | ||
5 | | |
5 | * For details, see the Prototype web site: http://www.prototypejs.org/ | |
* | ||
7 | ||
7 | *--------------------------------------------------------------------------*/ | |
var Prototype = { | ||
10 | | |
11 | | |
10 | Version: '1.6.0.3', | |
13 | | |
14 | | |
15 | ||
12 | Browser: { | |
13 | IE: !!(window.attachEvent && | |
14 | navigator.userAgent.indexOf('Opera') === -1), | |
15 | Opera: navigator.userAgent.indexOf('Opera') > -1, | |
16 | WebKit: navigator.userAgent.indexOf('AppleWebKit/') > -1, | |
17 | Gecko: navigator.userAgent.indexOf('Gecko') > -1 && | |
18 | navigator.userAgent.indexOf('KHTML') === -1, | |
19 | MobileSafari: !!navigator.userAgent.match(/Apple.*Mobile.*Safari/) | |
20 | }, | |
22 | BrowserFeatures: { | |
23 | XPath: !!document.evaluate, | |
24 | SelectorsAPI: !!document.querySelector, | |
25 | ElementExtensions: !!window.HTMLElement, | |
26 | SpecificElementExtensions: | |
27 | document.createElement('div')['__proto__'] && | |
28 | document.createElement('div')['__proto__'] !== | |
29 | document.createElement('form')['__proto__'] | |
30 | }, | |
31 | ||
32 | ScriptFragment: '<script[^>]*>([\\S\\s]*?)<\/script>', | |
33 | JSONFilter: /^\/\*-secure-([\s\S]*)\*\/\s*$/, | |
34 | ||
35 | emptyFunction: function() { }, | |
36 | K: function(x) { return x } | |
37 | }; | |
38 | ||
39 | if (Prototype.Browser.MobileSafari) | |
40 | Prototype.BrowserFeatures.SpecificElementExtensions = false; | |
41 | ||
42 | ||
43 | /* Based on Alex Arnell's inheritance implementation. */ | |
var Class = { | ||
create: function() { | ||
19 | | |
46 | var parent = null, properties = $A(arguments); | |
47 | if (Object.isFunction(properties[0])) | |
48 | parent = properties.shift(); | |
49 | ||
50 | function klass() { | |
this.initialize.apply(this, arguments); | ||
} | ||
53 | ||
54 | Object.extend(klass, Class.Methods); | |
55 | klass.superclass = parent; | |
56 | klass.subclasses = []; | |
57 | ||
58 | if (parent) { | |
59 | var subclass = function() { }; | |
60 | subclass.prototype = parent.prototype; | |
61 | klass.prototype = new subclass; | |
62 | parent.subclasses.push(klass); | |
63 | } | |
64 | ||
65 | for (var i = 0; i < properties.length; i++) | |
66 | klass.addMethods(properties[i]); | |
67 | ||
68 | if (!klass.prototype.initialize) | |
69 | klass.prototype.initialize = Prototype.emptyFunction; | |
70 | ||
71 | klass.prototype.constructor = klass; | |
72 | ||
73 | return klass; | |
} | ||
23 | ||
75 | }; | |
25 | ||
77 | Class.Methods = { | |
78 | addMethods: function(source) { | |
79 | var ancestor = this.superclass && this.superclass.prototype; | |
80 | var properties = Object.keys(source); | |
82 | if (!Object.keys({ toString: true }).length) | |
83 | properties.push("toString", "valueOf"); | |
84 | ||
85 | for (var i = 0, length = properties.length; i < length; i++) { | |
86 | var property = properties[i], value = source[property]; | |
87 | if (ancestor && Object.isFunction(value) && | |
88 | value.argumentNames().first() == "$super") { | |
89 | var method = value; | |
90 | value = (function(m) { | |
91 | return function() { return ancestor[m].apply(this, arguments) }; | |
92 | })(property).wrap(method); | |
93 | ||
94 | value.valueOf = method.valueOf.bind(method); | |
95 | value.toString = method.toString.bind(method); | |
96 | } | |
97 | this.prototype[property] = value; | |
98 | } | |
99 | ||
100 | return this; | |
101 | } | |
102 | }; | |
103 | ||
104 | var Abstract = { }; | |
105 | ||
Object.extend = function(destination, source) { | ||
28 | | |
107 | for (var property in source) | |
destination[property] = source[property]; | ||
30 | | |
return destination; | ||
32 | ||
110 | }; | |
34 | ||
35 | | |
36 | | |
37 | | |
38 | | |
39 | | |
40 | | |
41 | | |
42 | | |
43 | ||
112 | Object.extend(Object, { | |
113 | inspect: function(object) { | |
114 | try { | |
115 | if (Object.isUndefined(object)) return 'undefined'; | |
116 | if (object === null) return 'null'; | |
117 | return object.inspect ? object.inspect() : String(object); | |
118 | } catch (e) { | |
119 | if (e instanceof RangeError) return '...'; | |
120 | throw e; | |
121 | } | |
122 | }, | |
45 | ||
46 | | |
47 | | |
48 | | |
49 | | |
50 | ||
124 | toJSON: function(object) { | |
125 | var type = typeof object; | |
126 | switch (type) { | |
127 | case 'undefined': | |
128 | case 'function': | |
129 | case 'unknown': return; | |
130 | case 'boolean': return object.toString(); | |
131 | } | |
52 | ||
53 | | |
54 | | |
55 | | |
133 | if (object === null) return 'null'; | |
134 | if (object.toJSON) return object.toJSON(); | |
135 | if (Object.isElement(object)) return; | |
136 | ||
137 | var results = []; | |
138 | for (var property in object) { | |
139 | var value = Object.toJSON(object[property]); | |
140 | if (!Object.isUndefined(value)) | |
141 | results.push(property.toJSON() + ': ' + value); | |
142 | } | |
143 | ||
144 | return '{' + results.join(', ') + '}'; | |
145 | }, | |
146 | ||
147 | toQueryString: function(object) { | |
148 | return $H(object).toQueryString(); | |
149 | }, | |
150 | ||
151 | toHTML: function(object) { | |
152 | return object && object.toHTML ? object.toHTML() : String.interpret(object); | |
153 | }, | |
154 | ||
155 | keys: function(object) { | |
156 | var keys = []; | |
157 | for (var property in object) | |
158 | keys.push(property); | |
159 | return keys; | |
160 | }, | |
161 | ||
162 | values: function(object) { | |
163 | var values = []; | |
164 | for (var property in object) | |
165 | values.push(object[property]); | |
166 | return values; | |
167 | }, | |
168 | ||
169 | clone: function(object) { | |
170 | return Object.extend({ }, object); | |
171 | }, | |
172 | ||
173 | isElement: function(object) { | |
174 | return !!(object && object.nodeType == 1); | |
175 | }, | |
176 | ||
177 | isArray: function(object) { | |
178 | return object != null && typeof object == "object" && | |
179 | 'splice' in object && 'join' in object; | |
180 | }, | |
181 | ||
182 | isHash: function(object) { | |
183 | return object instanceof Hash; | |
184 | }, | |
185 | ||
186 | isFunction: function(object) { | |
187 | return typeof object == "function"; | |
188 | }, | |
189 | ||
190 | isString: function(object) { | |
191 | return typeof object == "string"; | |
192 | }, | |
193 | ||
194 | isNumber: function(object) { | |
195 | return typeof object == "number"; | |
196 | }, | |
197 | ||
198 | isUndefined: function(object) { | |
199 | return typeof object == "undefined"; | |
} | ||
57 | ||
201 | }); | |
59 | ||
60 | | |
61 | | |
62 | | |
63 | | |
203 | Object.extend(Function.prototype, { | |
204 | argumentNames: function() { | |
205 | var names = this.toString().match(/^[\s\(]*function[^(]*\(([^\)]*)\)/)[1] | |
206 | .replace(/\s+/g, '').split(','); | |
207 | return names.length == 1 && !names[0] ? [] : names; | |
}, | ||
66 | | |
67 | | |
210 | bind: function() { | |
211 | if (arguments.length < 2 && Object.isUndefined(arguments[0])) return this; | |
212 | var __method = this, args = $A(arguments), object = args.shift(); | |
213 | return function() { | |
214 | return __method.apply(object, args.concat($A(arguments))); | |
215 | } | |
}, | ||
70 | | |
71 | | |
72 | | |
218 | bindAsEventListener: function() { | |
219 | var __method = this, args = $A(arguments), object = args.shift(); | |
220 | return function(event) { | |
221 | return __method.apply(object, [event || window.event].concat(args)); | |
222 | } | |
223 | }, | |
224 | ||
225 | curry: function() { | |
226 | if (!arguments.length) return this; | |
227 | var __method = this, args = $A(arguments); | |
228 | return function() { | |
229 | return __method.apply(this, args.concat($A(arguments))); | |
230 | } | |
231 | }, | |
232 | ||
233 | delay: function() { | |
234 | var __method = this, args = $A(arguments), timeout = args.shift() * 1000; | |
235 | return window.setTimeout(function() { | |
236 | return __method.apply(__method, args); | |
237 | }, timeout); | |
238 | }, | |
239 | ||
240 | defer: function() { | |
241 | var args = [0.01].concat($A(arguments)); | |
242 | return this.delay.apply(this, args); | |
243 | }, | |
244 | ||
245 | wrap: function(wrapper) { | |
246 | var __method = this; | |
247 | return function() { | |
248 | return wrapper.apply(this, [__method.bind(this)].concat($A(arguments))); | |
249 | } | |
250 | }, | |
251 | ||
252 | methodize: function() { | |
253 | if (this._methodized) return this._methodized; | |
254 | var __method = this; | |
255 | return this._methodized = function() { | |
256 | return __method.apply(null, [this].concat($A(arguments))); | |
257 | }; | |
} | ||
}); | ||
261 | Date.prototype.toJSON = function() { | |
262 | return '"' + this.getUTCFullYear() + '-' + | |
263 | (this.getUTCMonth() + 1).toPaddedString(2) + '-' + | |
264 | this.getUTCDate().toPaddedString(2) + 'T' + | |
265 | this.getUTCHours().toPaddedString(2) + ':' + | |
266 | this.getUTCMinutes().toPaddedString(2) + ':' + | |
267 | this.getUTCSeconds().toPaddedString(2) + 'Z"'; | |
268 | }; | |
269 | ||
var Try = { | ||
these: function() { | ||
var returnValue; | ||
80 | | |
274 | for (var i = 0, length = arguments.length; i < length; i++) { | |
var lambda = arguments[i]; | ||
try { | ||
returnValue = lambda(); | ||
break; | ||
85 | | |
279 | } catch (e) { } | |
} | ||
return returnValue; | ||
} | ||
90 | ||
284 | }; | |
286 | RegExp.prototype.match = RegExp.prototype.test; | |
287 | ||
288 | RegExp.escape = function(str) { | |
289 | return String(str).replace(/([.*+?^=!:${}()|[\]\/\\])/g, '\\$1'); | |
290 | }; | |
291 | ||
/*--------------------------------------------------------------------------*/ | ||
94 | ||
95 | ||
294 | var PeriodicalExecuter = Class.create({ | |
initialize: function(callback, frequency) { | ||
this.callback = callback; | ||
this.frequency = frequency; | ||
... | ... | |
}, | ||
registerCallback: function() { | ||
105 | | |
304 | this.timer = setInterval(this.onTimerEvent.bind(this), this.frequency * 1000); | |
}, | ||
307 | execute: function() { | |
308 | this.callback(this); | |
309 | }, | |
310 | ||
311 | stop: function() { | |
312 | if (!this.timer) return; | |
313 | clearInterval(this.timer); | |
314 | this.timer = null; | |
315 | }, | |
316 | ||
onTimerEvent: function() { | ||
if (!this.currentlyExecuting) { | ||
try { | ||
this.currentlyExecuting = true; | ||
112 | | |
321 | this.execute(); | |
} finally { | ||
this.currentlyExecuting = false; | ||
} | ||
} | ||
} | ||
118 | ||
327 | }); | |
328 | Object.extend(String, { | |
329 | interpret: function(value) { | |
330 | return value == null ? '' : String(value); | |
331 | }, | |
332 | specialChar: { | |
333 | '\b': '\\b', | |
334 | '\t': '\\t', | |
335 | '\n': '\\n', | |
336 | '\f': '\\f', | |
337 | '\r': '\\r', | |
338 | '\\': '\\\\' | |
339 | } | |
340 | }); | |
341 | ||
Object.extend(String.prototype, { | ||
gsub: function(pattern, replacement) { | ||
var result = '', source = this, match; | ||
... | ... | |
while (source.length > 0) { | ||
if (match = source.match(pattern)) { | ||
result += source.slice(0, match.index); | ||
127 | | |
350 | result += String.interpret(replacement(match)); | |
source = source.slice(match.index + match[0].length); | ||
} else { | ||
result += source, source = ''; | ||
... | ... | |
sub: function(pattern, replacement, count) { | ||
replacement = this.gsub.prepareReplacement(replacement); | ||
138 | | |
361 | count = Object.isUndefined(count) ? 1 : count; | |
return this.gsub(pattern, function(match) { | ||
if (--count < 0) return match[0]; | ||
... | ... | |
scan: function(pattern, iterator) { | ||
this.gsub(pattern, iterator); | ||
148 | | |
371 | return String(this); | |
}, | ||
truncate: function(length, truncation) { | ||
length = length || 30; | ||
153 | | |
376 | truncation = Object.isUndefined(truncation) ? '...' : truncation; | |
return this.length > length ? | ||
155 | | |
378 | this.slice(0, length - truncation.length) + truncation : String(this); | |
}, | ||
strip: function() { | ||
... | ... | |
}, | ||
escapeHTML: function() { | ||
183 | | |
184 | | |
185 | | |
186 | | |
406 | var self = arguments.callee; | |
407 | self.text.data = this; | |
408 | return self.div.innerHTML; | |
}, | ||
unescapeHTML: function() { | ||
190 | | |
412 | var div = new Element('div'); | |
div.innerHTML = this.stripTags(); | ||
192 | | |
414 | return div.childNodes[0] ? (div.childNodes.length > 1 ? | |
415 | $A(div.childNodes).inject('', function(memo, node) { return memo+node.nodeValue }) : | |
416 | div.childNodes[0].nodeValue) : ''; | |
}, | ||
195 | | |
196 | | |
197 | | |
198 | | |
199 | | |
200 | | |
419 | toQueryParams: function(separator) { | |
420 | var match = this.strip().match(/([^?#]*)(#.*)?$/); | |
421 | if (!match) return { }; | |
422 | ||
423 | return match[1].split(separator || '&').inject({ }, function(hash, pair) { | |
424 | if ((pair = pair.split('='))[0]) { | |
425 | var key = decodeURIComponent(pair.shift()); | |
426 | var value = pair.length > 1 ? pair.join('=') : pair[0]; | |
427 | if (value != undefined) value = decodeURIComponent(value); | |
428 | ||
429 | if (key in hash) { | |
430 | if (!Object.isArray(hash[key])) hash[key] = [hash[key]]; | |
431 | hash[key].push(value); | |
432 | } | |
433 | else hash[key] = value; | |
434 | } | |
435 | return hash; | |
}); | ||
}, | ||
... | ... | |
return this.split(''); | ||
}, | ||
443 | succ: function() { | |
444 | return this.slice(0, this.length - 1) + | |
445 | String.fromCharCode(this.charCodeAt(this.length - 1) + 1); | |
446 | }, | |
447 | ||
448 | times: function(count) { | |
449 | return count < 1 ? '' : new Array(count + 1).join(this); | |
450 | }, | |
451 | ||
camelize: function() { | ||
209 | | |
210 | | |
453 | var parts = this.split('-'), len = parts.length; | |
454 | if (len == 1) return parts[0]; | |
212 | | |
213 | | |
214 | | |
456 | var camelized = this.charAt(0) == '-' | |
457 | ? parts[0].charAt(0).toUpperCase() + parts[0].substring(1) | |
458 | : parts[0]; | |
216 | | |
217 | | |
218 | | |
219 | | |
460 | for (var i = 1; i < len; i++) | |
461 | camelized += parts[i].charAt(0).toUpperCase() + parts[i].substring(1); | |
221 | | |
463 | return camelized; | |
}, | ||
224 | | |
225 | | |
466 | capitalize: function() { | |
467 | return this.charAt(0).toUpperCase() + this.substring(1).toLowerCase(); | |
468 | }, | |
469 | ||
470 | underscore: function() { | |
471 | return this.gsub(/::/, '/').gsub(/([A-Z]+)([A-Z][a-z])/,'#{1}_#{2}').gsub(/([a-z\d])([A-Z])/,'#{1}_#{2}').gsub(/-/,'_').toLowerCase(); | |
472 | }, | |
473 | ||
474 | dasherize: function() { | |
475 | return this.gsub(/_/,'-'); | |
476 | }, | |
477 | ||
478 | inspect: function(useDoubleQuotes) { | |
479 | var escapedString = this.gsub(/[\x00-\x1f\\]/, function(match) { | |
480 | var character = String.specialChar[match[0]]; | |
481 | return character ? character : '\\u00' + match[0].charCodeAt().toPaddedString(2, 16); | |
482 | }); | |
483 | if (useDoubleQuotes) return '"' + escapedString.replace(/"/g, '\\"') + '"'; | |
484 | return "'" + escapedString.replace(/'/g, '\\\'') + "'"; | |
485 | }, | |
486 | ||
487 | toJSON: function() { | |
488 | return this.inspect(true); | |
489 | }, | |
490 | ||
491 | unfilterJSON: function(filter) { | |
492 | return this.sub(filter || Prototype.JSONFilter, '#{1}'); | |
493 | }, | |
494 | ||
495 | isJSON: function() { | |
496 | var str = this; | |
497 | if (str.blank()) return false; | |
498 | str = this.replace(/\\./g, '@').replace(/"[^"\\\n\r]*"/g, ''); | |
499 | return (/^[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]*$/).test(str); | |
500 | }, | |
501 | ||
502 | evalJSON: function(sanitize) { | |
503 | var json = this.unfilterJSON(); | |
504 | try { | |
505 | if (!sanitize || json.isJSON()) return eval('(' + json + ')'); | |
506 | } catch (e) { } | |
507 | throw new SyntaxError('Badly formed JSON string: ' + this.inspect()); | |
508 | }, | |
509 | ||
510 | include: function(pattern) { | |
511 | return this.indexOf(pattern) > -1; | |
512 | }, | |
513 | ||
514 | startsWith: function(pattern) { | |
515 | return this.indexOf(pattern) === 0; | |
516 | }, | |
517 | ||
518 | endsWith: function(pattern) { | |
519 | var d = this.length - pattern.length; | |
520 | return d >= 0 && this.lastIndexOf(pattern) === d; | |
521 | }, | |
522 | ||
523 | empty: function() { | |
524 | return this == ''; | |
525 | }, | |
526 | ||
527 | blank: function() { | |
528 | return /^\s*$/.test(this); | |
529 | }, | |
530 | ||
531 | interpolate: function(object, pattern) { | |
532 | return new Template(this, pattern).evaluate(object); | |
} | ||
}); | ||
536 | if (Prototype.Browser.WebKit || Prototype.Browser.IE) Object.extend(String.prototype, { | |
537 | escapeHTML: function() { | |
538 | return this.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>'); | |
539 | }, | |
540 | unescapeHTML: function() { | |
541 | return this.stripTags().replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>'); | |
542 | } | |
543 | }); | |
544 | ||
String.prototype.gsub.prepareReplacement = function(replacement) { | ||
230 | | |
546 | if (Object.isFunction(replacement)) return replacement; | |
var template = new Template(replacement); | ||
return function(match) { return template.evaluate(match) }; | ||
233 | ||
549 | }; | |
String.prototype.parseQuery = String.prototype.toQueryParams; | ||
237 | ||
238 | ||
239 | ||
553 | Object.extend(String.prototype.escapeHTML, { | |
554 | div: document.createElement('div'), | |
555 | text: document.createTextNode('') | |
556 | }); | |
557 | ||
558 | String.prototype.escapeHTML.div.appendChild(String.prototype.escapeHTML.text); | |
559 | ||
560 | var Template = Class.create({ | |
initialize: function(template, pattern) { | ||
this.template = template.toString(); | ||
242 | | |
563 | this.pattern = pattern || Template.Pattern; | |
}, | ||
evaluate: function(object) { | ||
567 | if (Object.isFunction(object.toTemplateReplacements)) | |
568 | object = object.toTemplateReplacements(); | |
569 | ||
return this.template.gsub(this.pattern, function(match) { | ||
247 | | |
571 | if (object == null) return ''; | |
572 | ||
573 | var before = match[1] || ''; | |
if (before == '\\') return match[2]; | ||
249 | | |
575 | ||
576 | var ctx = object, expr = match[3]; | |
577 | var pattern = /^([^.[]+|\[((?:.*?[^\\])?)\])(\.|\[|$)/; | |
578 | match = pattern.exec(expr); | |
579 | if (match == null) return before; | |
580 | ||
581 | while (match != null) { | |
582 | var comp = match[1].startsWith('[') ? match[2].gsub('\\\\]', ']') : match[1]; | |
583 | ctx = ctx[comp]; | |
584 | if (null == ctx || '' == match[3]) break; | |
585 | expr = expr.substring('[' == match[3] ? match[1].length : match[0].length); | |
586 | match = pattern.exec(expr); | |
587 | } | |
588 | ||
589 | return before + String.interpret(ctx); | |
}); | ||
} | ||
252 | ||
592 | }); | |
593 | Template.Pattern = /(^|.|\r|\n)(#\{(.*?)\})/; | |
254 | ||
255 | ||
595 | var $break = { }; | |
var Enumerable = { | ||
258 | | |
598 | each: function(iterator, context) { | |
var index = 0; | ||
try { | ||
this._each(function(value) { | ||
262 | | |
263 | | |
264 | | |
265 | | |
266 | | |
602 | iterator.call(context, value, index++); | |
}); | ||
} catch (e) { | ||
if (e != $break) throw e; | ||
} | ||
607 | return this; | |
}, | ||
273 | | |
610 | eachSlice: function(number, iterator, context) { | |
611 | var index = -number, slices = [], array = this.toArray(); | |
612 | if (number < 1) return array; | |
613 | while ((index += number) < array.length) | |
614 | slices.push(array.slice(index, index+number)); | |
615 | return slices.collect(iterator, context); | |
616 | }, | |
617 | ||
618 | all: function(iterator, context) { | |
619 | iterator = iterator || Prototype.K; | |
var result = true; | ||
this.each(function(value, index) { | ||
276 | | |
622 | result = result && !!iterator.call(context, value, index); | |
if (!result) throw $break; | ||
}); | ||
return result; | ||
}, | ||
282 | | |
283 | | |
628 | any: function(iterator, context) { | |
629 | iterator = iterator || Prototype.K; | |
630 | var result = false; | |
this.each(function(value, index) { | ||
285 | | |
632 | if (result = !!iterator.call(context, value, index)) | |
throw $break; | ||
}); | ||
return result; | ||
}, | ||
291 | | |
638 | collect: function(iterator, context) { | |
639 | iterator = iterator || Prototype.K; | |
var results = []; | ||
this.each(function(value, index) { | ||
294 | | |
642 | results.push(iterator.call(context, value, index)); | |
}); | ||
return results; | ||
}, | ||
299 | | |
647 | detect: function(iterator, context) { | |
var result; | ||
this.each(function(value, index) { | ||
302 | | |
650 | if (iterator.call(context, value, index)) { | |
result = value; | ||
throw $break; | ||
} | ||
... | ... | |
return result; | ||
}, | ||
310 | | |
658 | findAll: function(iterator, context) { | |
var results = []; | ||
this.each(function(value, index) { | ||
313 | | |
661 | if (iterator.call(context, value, index)) | |
results.push(value); | ||
}); | ||
return results; | ||
}, | ||
319 | | |
667 | grep: function(filter, iterator, context) { | |
668 | iterator = iterator || Prototype.K; | |
var results = []; | ||
670 | ||
671 | if (Object.isString(filter)) | |
672 | filter = new RegExp(filter); | |
673 | ||
this.each(function(value, index) { | ||
322 | | |
323 | | |
324 | | |
325 | | |
675 | if (filter.match(value)) | |
676 | results.push(iterator.call(context, value, index)); | |
677 | }); | |
return results; | ||
}, | ||
include: function(object) { | ||
682 | if (Object.isFunction(this.indexOf)) | |
683 | if (this.indexOf(object) != -1) return true; | |
684 | ||
var found = false; | ||
this.each(function(value) { | ||
if (value == object) { | ||
... | ... | |
return found; | ||
}, | ||
340 | | |
695 | inGroupsOf: function(number, fillWith) { | |
696 | fillWith = Object.isUndefined(fillWith) ? null : fillWith; | |
697 | return this.eachSlice(number, function(slice) { | |
698 | while(slice.length < number) slice.push(fillWith); | |
699 | return slice; | |
700 | }); | |
701 | }, | |
702 | ||
703 | inject: function(memo, iterator, context) { | |
this.each(function(value, index) { | ||
342 | | |
705 | memo = iterator.call(context, memo, value, index); | |
}); | ||
return memo; | ||
}, | ||
invoke: function(method) { | ||
var args = $A(arguments).slice(1); | ||
349 | | |
712 | return this.map(function(value) { | |
return value[method].apply(value, args); | ||
}); | ||
}, | ||
354 | | |
717 | max: function(iterator, context) { | |
718 | iterator = iterator || Prototype.K; | |
var result; | ||
this.each(function(value, index) { | ||
357 | | |
358 | | |
721 | value = iterator.call(context, value, index); | |
722 | if (result == null || value >= result) | |
result = value; | ||
}); | ||
return result; | ||
}, | ||
364 | | |
728 | min: function(iterator, context) { | |
729 | iterator = iterator || Prototype.K; | |
var result; | ||
this.each(function(value, index) { | ||
367 | | |
368 | | |
732 | value = iterator.call(context, value, index); | |
733 | if (result == null || value < result) | |
result = value; | ||
}); | ||
return result; | ||
}, | ||
374 | | |
739 | partition: function(iterator, context) { | |
740 | iterator = iterator || Prototype.K; | |
var trues = [], falses = []; | ||
this.each(function(value, index) { | ||
377 | | |
743 | (iterator.call(context, value, index) ? | |
trues : falses).push(value); | ||
}); | ||
return [trues, falses]; | ||
... | ... | |
pluck: function(property) { | ||
var results = []; | ||
385 | | |
751 | this.each(function(value) { | |
results.push(value[property]); | ||
}); | ||
return results; | ||
}, | ||
391 | | |
757 | reject: function(iterator, context) { | |
var results = []; | ||
this.each(function(value, index) { | ||
394 | | |
760 | if (!iterator.call(context, value, index)) | |
results.push(value); | ||
}); | ||
return results; | ||
}, | ||
400 | | |
401 | | |
402 | | |
766 | sortBy: function(iterator, context) { | |
767 | return this.map(function(value, index) { | |
768 | return { | |
769 | value: value, | |
770 | criteria: iterator.call(context, value, index) | |
771 | }; | |
}).sort(function(left, right) { | ||
var a = left.criteria, b = right.criteria; | ||
return a < b ? -1 : a > b ? 1 : 0; | ||
... | ... | |
}, | ||
toArray: function() { | ||
410 | | |
779 | return this.map(); | |
}, | ||
zip: function() { | ||
var iterator = Prototype.K, args = $A(arguments); | ||
415 | | |
784 | if (Object.isFunction(args.last())) | |
iterator = args.pop(); | ||
var collections = [this].concat(args).map($A); | ||
... | ... | |
}); | ||
}, | ||
793 | size: function() { | |
794 | return this.toArray().length; | |
795 | }, | |
796 | ||
inspect: function() { | ||
return '#<Enumerable:' + this.toArray().inspect() + '>'; | ||
} | ||
427 | ||
800 | }; | |
Object.extend(Enumerable, { | ||
map: Enumerable.collect, | ||
find: Enumerable.detect, | ||
select: Enumerable.findAll, | ||
806 | filter: Enumerable.findAll, | |
member: Enumerable.include, | ||
434 | | |
808 | entries: Enumerable.toArray, | |
809 | every: Enumerable.all, | |
810 | some: Enumerable.any | |
}); | ||
436 | ||
812 | function $A(iterable) { | |
if (!iterable) return []; | ||
438 | | |
439 | | |
440 | | |
441 | | |
442 | | |
443 | | |
814 | if (iterable.toArray) return iterable.toArray(); | |
815 | var length = iterable.length || 0, results = new Array(length); | |
816 | while (length--) results[length] = iterable[length]; | |
817 | return results; | |
818 | } | |
819 | ||
820 | if (Prototype.Browser.WebKit) { | |
821 | $A = function(iterable) { | |
822 | if (!iterable) return []; | |
823 | // In Safari, only use the `toArray` method if it's not a NodeList. | |
824 | // A NodeList is a function, has an function `item` property, and a numeric | |
825 | // `length` property. Adapted from Google Doctype. | |
826 | if (!(typeof iterable === 'function' && typeof iterable.length === | |
827 | 'number' && typeof iterable.item === 'function') && iterable.toArray) | |
828 | return iterable.toArray(); | |
829 | var length = iterable.length || 0, results = new Array(length); | |
830 | while (length--) results[length] = iterable[length]; | |
return results; | ||
445 | | |
832 | }; | |
} | ||
835 | Array.from = $A; | |
836 | ||
Object.extend(Array.prototype, Enumerable); | ||
450 | ||
451 | | |
839 | if (!Array.prototype._reverse) Array.prototype._reverse = Array.prototype.reverse; | |
Object.extend(Array.prototype, { | ||
_each: function(iterator) { | ||
455 | | |
843 | for (var i = 0, length = this.length; i < length; i++) | |
iterator(this[i]); | ||
}, | ||
... | ... | |
compact: function() { | ||
return this.select(function(value) { | ||
474 | | |
862 | return value != null; | |
}); | ||
}, | ||
flatten: function() { | ||
return this.inject([], function(array, value) { | ||
480 | | |
868 | return array.concat(Object.isArray(value) ? | |
value.flatten() : [value]); | ||
}); | ||
}, | ||
... | ... | |
}); | ||
}, | ||
492 | | |
493 | | |
494 | | |
495 | | |
496 | | |
497 | ||
reverse: function(inline) { | ||
return (inline !== false ? this : this.toArray())._reverse(); | ||
}, | ||
884 | reduce: function() { | |
885 | return this.length > 1 ? this : this[0]; | |
886 | }, | |
887 | ||
888 | uniq: function(sorted) { | |
889 | return this.inject([], function(array, value, index) { | |
890 | if (0 == index || (sorted ? array.last() != value : !array.include(value))) | |
891 | array.push(value); | |
892 | return array; | |
893 | }); | |
894 | }, | |
895 | ||
896 | intersect: function(array) { | |
897 | return this.uniq().findAll(function(item) { | |
898 | return array.detect(function(value) { return item === value }); | |
899 | }); | |
900 | }, | |
901 | ||
902 | clone: function() { | |
903 | return [].concat(this); | |
904 | }, | |
905 | ||
906 | size: function() { | |
907 | return this.length; | |
908 | }, | |
909 | ||
inspect: function() { | ||
return '[' + this.map(Object.inspect).join(', ') + ']'; | ||
912 | }, | |
913 | ||
914 | toJSON: function() { | |
915 | var results = []; | |
916 | this.each(function(object) { | |
917 | var value = Object.toJSON(object); | |
918 | if (!Object.isUndefined(value)) results.push(value); | |
919 | }); | |
920 | return '[' + results.join(', ') + ']'; | |
} | ||
}); | ||
506 | ||
507 | | |
508 | | |
509 | | |
510 | | |
512 | | |
513 | | |
514 | | |
515 | | |
924 | // use native browser JS 1.6 implementation if available | |
925 | if (Object.isFunction(Array.prototype.forEach)) | |
926 | Array.prototype._each = Array.prototype.forEach; | |
927 | ||
928 | if (!Array.prototype.indexOf) Array.prototype.indexOf = function(item, i) { | |
929 | i || (i = 0); | |
930 | var length = this.length; | |
931 | if (i < 0) i = length + i; | |
932 | for (; i < length; i++) | |
933 | if (this[i] === item) return i; | |
934 | return -1; | |
935 | }; | |
936 | ||
937 | if (!Array.prototype.lastIndexOf) Array.prototype.lastIndexOf = function(item, i) { | |
938 | i = isNaN(i) ? this.length : (i < 0 ? this.length + i : i) + 1; | |
939 | var n = this.slice(0, i).reverse().indexOf(item); | |
940 | return (n < 0) ? n : i - n - 1; | |
941 | }; | |
942 | ||
943 | Array.prototype.toArray = Array.prototype.clone; | |
944 | ||
945 | function $w(string) { | |
946 | if (!Object.isString(string)) return []; | |
947 | string = string.strip(); | |
948 | return string ? string.split(/\s+/) : []; | |
949 | } | |
950 | ||
951 | if (Prototype.Browser.Opera){ | |
952 | Array.prototype.concat = function() { | |
953 | var array = []; | |
954 | for (var i = 0, length = this.length; i < length; i++) array.push(this[i]); | |
955 | for (var i = 0, length = arguments.length; i < length; i++) { | |
956 | if (Object.isArray(arguments[i])) { | |
957 | for (var j = 0, arrayLength = arguments[i].length; j < arrayLength; j++) | |
958 | array.push(arguments[i][j]); | |
959 | } else { | |
960 | array.push(arguments[i]); | |
961 | } | |
} | ||
963 | return array; | |
964 | }; | |
965 | } | |
966 | Object.extend(Number.prototype, { | |
967 | toColorPart: function() { | |
968 | return this.toPaddedString(2, 16); | |
}, | ||
519 | | |
520 | | |
971 | succ: function() { | |
972 | return this + 1; | |
}, | ||
523 | | |
524 | | |
975 | times: function(iterator, context) { | |
976 | $R(0, this, true).each(iterator, context); | |
977 | return this; | |
}, | ||
527 | | |
528 | | |
529 | | |
530 | | |
531 | | |
980 | toPaddedString: function(length, radix) { | |
981 | var string = this.toString(radix || 10); | |
982 | return '0'.times(length - string.length) + string; | |
}, | ||
534 | | |
535 | | |
536 | | |
537 | | |
538 | | |
985 | toJSON: function() { | |
986 | return isFinite(this) ? this.toString() : 'null'; | |
987 | } | |
988 | }); | |
540 | | |
541 | | |
542 | | |
543 | | |
990 | $w('abs round ceil floor').each(function(method){ | |
991 | Number.prototype[method] = Math[method].methodize(); | |
992 | }); | |
993 | function $H(object) { | |
994 | return new Hash(object); | |
995 | }; | |
996 | ||
997 | var Hash = Class.create(Enumerable, (function() { | |
998 | ||
999 | function toQueryPair(key, value) { | |
1000 | if (Object.isUndefined(value)) return key; | |
1001 | return key + '=' + encodeURIComponent(String.interpret(value)); | |
} | ||
545 | ||
547 | ||
548 | | |
549 | | |
550 | | |
551 | | |
552 | ||
553 | ||
554 | ||
555 | ||
1004 | return { | |
1005 | initialize: function(object) { | |
1006 | this._object = Object.isHash(object) ? object.toObject() : Object.clone(object); | |
1007 | }, | |
1008 | ||
1009 | _each: function(iterator) { | |
1010 | for (var key in this._object) { | |
1011 | var value = this._object[key], pair = [key, value]; | |
1012 | pair.key = key; | |
1013 | pair.value = value; | |
1014 | iterator(pair); | |
1015 | } | |
1016 | }, | |
1017 | ||
1018 | set: function(key, value) { | |
1019 | return this._object[key] = value; | |
1020 | }, | |
1021 | ||
1022 | get: function(key) { | |
1023 | // simulating poorly supported hasOwnProperty | |
1024 | if (this._object[key] !== Object.prototype[key]) | |
1025 | return this._object[key]; | |
1026 | }, | |
1027 | ||
1028 | unset: function(key) { | |
1029 | var value = this._object[key]; | |
1030 | delete this._object[key]; | |
1031 | return value; | |
1032 | }, | |
1033 | ||
1034 | toObject: function() { | |
1035 | return Object.clone(this._object); | |
1036 | }, | |
1037 | ||
1038 | keys: function() { | |
1039 | return this.pluck('key'); | |
1040 | }, | |
1041 | ||
1042 | values: function() { | |
1043 | return this.pluck('value'); | |
1044 | }, | |
1045 | ||
1046 | index: function(value) { | |
1047 | var match = this.detect(function(pair) { | |
1048 | return pair.value === value; | |
1049 | }); | |
1050 | return match && match.key; | |
1051 | }, | |
1052 | ||
1053 | merge: function(object) { | |
1054 | return this.clone().update(object); | |
1055 | }, | |
1056 | ||
1057 | update: function(object) { | |
1058 | return new Hash(object).inject(this, function(result, pair) { | |
1059 | result.set(pair.key, pair.value); | |
1060 | return result; | |
1061 | }); | |
1062 | }, | |
1063 | ||
1064 | toQueryString: function() { | |
1065 | return this.inject([], function(results, pair) { | |
1066 | var key = encodeURIComponent(pair.key), values = pair.value; | |
1067 | ||
1068 | if (values && typeof values == 'object') { | |
1069 | if (Object.isArray(values)) | |
1070 | return results.concat(values.map(toQueryPair.curry(key))); | |
1071 | } else results.push(toQueryPair(key, values)); | |
1072 | return results; | |
1073 | }).join('&'); | |
1074 | }, | |
1075 | ||
1076 | inspect: function() { | |
1077 | return '#<Hash:{' + this.map(function(pair) { | |
1078 | return pair.map(Object.inspect).join(': '); | |
1079 | }).join(', ') + '}>'; | |
1080 | }, | |
1081 | ||
1082 | toJSON: function() { | |
1083 | return Object.toJSON(this.toObject()); | |
1084 | }, | |
1085 | ||
1086 | clone: function() { | |
1087 | return new Hash(this); | |
1088 | } | |
1089 | } | |
1090 | })()); | |
1091 | ||
1092 | Hash.prototype.toTemplateReplacements = Hash.prototype.toObject; | |
1093 | Hash.from = $H; | |
1094 | var ObjectRange = Class.create(Enumerable, { | |
initialize: function(start, end, exclusive) { | ||
this.start = start; | ||
this.end = end; | ||
... | ... | |
_each: function(iterator) { | ||
var value = this.start; | ||
564 | | |
1103 | while (this.include(value)) { | |
iterator(value); | ||
value = value.succ(); | ||
567 | | |
1106 | } | |
}, | ||
include: function(value) { | ||
... | ... | |
var $R = function(start, end, exclusive) { | ||
return new ObjectRange(start, end, exclusive); | ||
581 | ||
1120 | }; | |
var Ajax = { | ||
getTransport: function() { | ||
... | ... | |
}, | ||
activeRequestCount: 0 | ||
593 | ||
1132 | }; | |
Ajax.Responders = { | ||
responders: [], | ||
... | ... | |
this.responders._each(iterator); | ||
}, | ||
602 | | |
603 | | |
604 | | |
1141 | register: function(responder) { | |
1142 | if (!this.include(responder)) | |
1143 | this.responders.push(responder); | |
}, | ||
607 | | |
608 | | |
1146 | unregister: function(responder) { | |
1147 | this.responders = this.responders.without(responder); | |
}, | ||
dispatch: function(callback, request, transport, json) { | ||
this.each(function(responder) { | ||
613 | | |
1152 | if (Object.isFunction(responder[callback])) { | |
try { | ||
responder[callback].apply(responder, [request, transport, json]); | ||
616 | | |
1155 | } catch (e) { } | |
} | ||
}); | ||
} | ||
... | ... | |
Object.extend(Ajax.Responders, Enumerable); | ||
Ajax.Responders.register({ | ||
625 | | |
626 | | |
627 | | |
628 | ||
629 | | |
630 | | |
631 | | |
1164 | onCreate: function() { Ajax.activeRequestCount++ }, | |
1165 | onComplete: function() { Ajax.activeRequestCount-- } | |
}); | ||
634 | ||
635 | ||
636 | | |
1168 | Ajax.Base = Class.create({ | |
1169 | initialize: function(options) { | |
this.options = { | ||
method: 'post', | ||
asynchronous: true, | ||
contentType: 'application/x-www-form-urlencoded', | ||
641 | | |
642 | | |
643 | | |
644 | | |
1174 | encoding: 'UTF-8', | |
1175 | parameters: '', | |
1176 | evalJSON: true, | |
1177 | evalJS: true | |
1178 | }; | |
1179 | Object.extend(this.options, options || { }); | |
646 | | |
647 | | |
648 | | |
649 | | |
650 | | |
1181 | this.options.method = this.options.method.toLowerCase(); | |
652 | | |
653 | | |
1183 | if (Object.isString(this.options.parameters)) | |
1184 | this.options.parameters = this.options.parameters.toQueryParams(); | |
1185 | else if (Object.isHash(this.options.parameters)) | |
1186 | this.options.parameters = this.options.parameters.toObject(); | |
} | ||
655 | ||
1188 | }); | |
657 | ||
658 | ||
659 | | |
1190 | Ajax.Request = Class.create(Ajax.Base, { | |
1191 | _complete: false, | |
661 | ||
662 | | |
1193 | initialize: function($super, url, options) { | |
1194 | $super(options); | |
this.transport = Ajax.getTransport(); | ||
664 | | |
this.request(url); | ||
}, | ||
request: function(url) { | ||
669 | | |
670 | | |
1200 | this.url = url; | |
1201 | this.method = this.options.method; | |
1202 | var params = Object.clone(this.options.parameters); | |
1204 | if (!['get', 'post'].include(this.method)) { | |
1205 | // simulate other verbs over post | |
1206 | params['_method'] = this.method; | |
1207 | this.method = 'post'; | |
1208 | } | |
1209 | ||
1210 | this.parameters = params; | |
1211 | ||
1212 | if (params = Object.toQueryString(params)) { | |
1213 | // when GET, append parameters to URL | |
1214 | if (this.method == 'get') | |
1215 | this.url += (this.url.include('?') ? '&' : '?') + params; | |
1216 | else if (/Konqueror|Safari|KHTML/.test(navigator.userAgent)) | |
1217 | params += '&_='; | |
1218 | } | |
1219 | ||
try { | ||
673 | | |
674 | | |
675 | | |
1221 | var response = new Ajax.Response(this); | |
1222 | if (this.options.onCreate) this.options.onCreate(response); | |
1223 | Ajax.Responders.dispatch('onCreate', this, response); | |
677 | | |
678 | ||
679 | | |
1225 | this.transport.open(this.method.toUpperCase(), this.url, | |
this.options.asynchronous); | ||
682 | | |
683 | | |
684 | | |
685 | | |
1228 | if (this.options.asynchronous) this.respondToReadyState.bind(this).defer(1); | |
1230 | this.transport.onreadystatechange = this.onStateChange.bind(this); | |
this.setRequestHeaders(); | ||
689 | | |
690 | | |
1233 | this.body = this.method == 'post' ? (this.options.postBody || params) : null; | |
1234 | this.transport.send(this.body); | |
692 | | |
1236 | /* Force Firefox to handle ready state 4 for synchronous requests */ | |
1237 | if (!this.options.asynchronous && this.transport.overrideMimeType) | |
1238 | this.onStateChange(); | |
1239 | ||
1240 | } | |
1241 | catch (e) { | |
this.dispatchException(e); | ||
} | ||
}, | ||
1246 | onStateChange: function() { | |
1247 | var readyState = this.transport.readyState; | |
1248 | if (readyState > 1 && !((readyState == 4) && this._complete)) | |
1249 | this.respondToReadyState(this.transport.readyState); | |
1250 | }, | |
1251 | ||
setRequestHeaders: function() { | ||
698 | | |
699 | | |
700 | | |
701 | | |
1253 | var headers = { | |
1254 | 'X-Requested-With': 'XMLHttpRequest', | |
1255 | 'X-Prototype-Version': Prototype.Version, | |
1256 | 'Accept': 'text/javascript, text/html, application/xml, text/xml, */*' | |
1257 | }; | |
703 | | |
704 | | |
1259 | if (this.method == 'post') { | |
1260 | headers['Content-type'] = this.options.contentType + | |
1261 | (this.options.encoding ? '; charset=' + this.options.encoding : ''); | |
706 | | |
707 | | |
708 | | |
1263 | /* Force "Connection: close" for older Mozilla browsers to work | |
1264 | * around a bug where XMLHttpRequest sends an incorrect | |
1265 | * Content-length header. See Mozilla Bugzilla #246651. | |
*/ | ||
710 | | |
711 | | |
1267 | if (this.transport.overrideMimeType && | |
1268 | (navigator.userAgent.match(/Gecko\/(\d{4})/) || [0,2005])[1] < 2005) | |
1269 | headers['Connection'] = 'close'; | |
} | ||
714 | | |
715 | | |
1272 | // user-defined headers | |
1273 | if (typeof this.options.requestHeaders == 'object') { | |
1274 | var extras = this.options.requestHeaders; | |
717 | | |
718 | | |
719 | | |
1276 | if (Object.isFunction(extras.push)) | |
1277 | for (var i = 0, length = extras.length; i < length; i += 2) | |
1278 | headers[extras[i]] = extras[i+1]; | |
1279 | else | |
1280 | $H(extras).each(function(pair) { headers[pair.key] = pair.value }); | |
1281 | } | |
721 | | |
722 | | |
723 | | |
724 | | |
1283 | for (var name in headers) | |
1284 | this.transport.setRequestHeader(name, headers[name]); | |
}, | ||
727 | | |
728 | | |
729 | | |
730 | | |
1287 | success: function() { | |
1288 | var status = this.getStatus(); | |
1289 | return !status || (status >= 200 && status < 300); | |
}, | ||
733 | | |
1292 | getStatus: function() { | |
try { | ||
735 | | |
736 | | |
1294 | return this.transport.status || 0; | |
1295 | } catch (e) { return 0 } | |
}, | ||
739 | | |
740 | | |
741 | | |
742 | | |
743 | | |
744 | | |
745 | | |
746 | ||
respondToReadyState: function(readyState) { | ||
748 | | |
749 | | |
1299 | var state = Ajax.Request.Events[readyState], response = new Ajax.Response(this); | |
751 | | |
1301 | if (state == 'Complete') { | |
try { | ||
753 | | |
754 | | |
755 | | |
1303 | this._complete = true; | |
1304 | (this.options['on' + response.status] | |
1305 | || this.options['on' + (this.success() ? 'Success' : 'Failure')] | |
1306 | || Prototype.emptyFunction)(response, response.headerJSON); | |
} catch (e) { | ||
this.dispatchException(e); | ||
} | ||
760 | | |
1311 | var contentType = response.getHeader('Content-type'); | |
1312 | if (this.options.evalJS == 'force' | |
1313 | || (this.options.evalJS && this.isSameOrigin() && contentType | |
1314 | && contentType.match(/^\s*(text|application)\/(x-)?(java|ecma)script(;.*)?\s*$/i))) | |
this.evalResponse(); | ||
} | ||
try { | ||
765 | | |
766 | | |
1319 | (this.options['on' + state] || Prototype.emptyFunction)(response, response.headerJSON); | |
1320 | Ajax.Responders.dispatch('on' + state, this, response, response.headerJSON); | |
} catch (e) { | ||
this.dispatchException(e); | ||
} | ||
771 | | |
772 | | |
1325 | if (state == 'Complete') { | |
1326 | // avoid memory leak in MSIE: clean up | |
this.transport.onreadystatechange = Prototype.emptyFunction; | ||
1328 | } | |
}, | ||
1331 | isSameOrigin: function() { | |
1332 | var m = this.url.match(/^\s*https?:\/\/[^\/]*/); | |
1333 | return !m || (m[0] == '#{protocol}//#{domain}#{port}'.interpolate({ | |
1334 | protocol: location.protocol, | |
1335 | domain: document.domain, | |
1336 | port: location.port ? ':' + location.port : '' | |
1337 | })); | |
1338 | }, | |
1339 | ||
1340 | getHeader: function(name) { | |
1341 | try { | |
1342 | return this.transport.getResponseHeader(name) || null; | |
1343 | } catch (e) { return null } | |
1344 | }, | |
1345 | ||
1346 | evalResponse: function() { | |
1347 | try { | |
1348 | return eval((this.transport.responseText || '').unfilterJSON()); | |
1349 | } catch (e) { | |
1350 | this.dispatchException(e); | |
1351 | } | |
1352 | }, | |
1353 | ||
dispatchException: function(exception) { | ||
(this.options.onException || Prototype.emptyFunction)(this, exception); | ||
Ajax.Responders.dispatch('onException', this, exception); | ||
} | ||
}); | ||
782 | ||
1360 | Ajax.Request.Events = | |
1361 | ['Uninitialized', 'Loading', 'Loaded', 'Interactive', 'Complete']; | |
784 | ||
785 | | |
786 | | |
787 | | |
788 | | |
789 | | |
1363 | Ajax.Response = Class.create({ | |
1364 | initialize: function(request){ | |
1365 | this.request = request; | |
1366 | var transport = this.transport = request.transport, | |
1367 | readyState = this.readyState = transport.readyState; | |
1368 | ||
1369 | if((readyState > 2 && !Prototype.Browser.IE) || readyState == 4) { | |
1370 | this.status = this.getStatus(); | |
1371 | this.statusText = this.getStatusText(); | |
1372 | this.responseText = String.interpret(transport.responseText); | |
1373 | this.headerJSON = this._getHeaderJSON(); | |
} | ||
792 | | |
793 | | |
1376 | if(readyState == 4) { | |
1377 | var xml = transport.responseXML; | |
1378 | this.responseXML = Object.isUndefined(xml) ? null : xml; | |
1379 | this.responseJSON = this._getResponseJSON(); | |
1380 | } | |
1381 | }, | |
795 | | |
796 | | |
797 | | |
798 | | |
799 | | |
1383 | status: 0, | |
1384 | statusText: '', | |
801 | | |
1386 | getStatus: Ajax.Request.prototype.getStatus, | |
1387 | ||
1388 | getStatusText: function() { | |
1389 | try { | |
1390 | return this.transport.statusText || ''; | |
1391 | } catch (e) { return '' } | |
}, | ||
804 | | |
805 | | |
806 | | |
807 | | |
1394 | getHeader: Ajax.Request.prototype.getHeader, | |
809 | | |
810 | | |
1396 | getAllHeaders: function() { | |
1397 | try { | |
1398 | return this.getAllResponseHeaders(); | |
1399 | } catch (e) { return null } | |
1400 | }, | |
812 | | |
813 | | |
814 | | |
815 | | |
816 | | |
817 | | |
1402 | getResponseHeader: function(name) { | |
1403 | return this.transport.getResponseHeader(name); | |
1404 | }, | |
1405 | ||
1406 | getAllResponseHeaders: function() { | |
1407 | return this.transport.getAllResponseHeaders(); | |
1408 | }, | |
1409 | ||
1410 | _getHeaderJSON: function() { | |
1411 | var json = this.getHeader('X-JSON'); | |
1412 | if (!json) return null; | |
1413 | json = decodeURIComponent(escape(json)); | |
1414 | try { | |
1415 | return json.evalJSON(this.request.options.sanitizeJSON || | |
1416 | !this.request.isSameOrigin()); | |
1417 | } catch (e) { | |
1418 | this.request.dispatchException(e); | |
} | ||
1420 | }, | |
820 | | |
821 | | |
822 | | |
1422 | _getResponseJSON: function() { | |
1423 | var options = this.request.options; | |
1424 | if (!options.evalJSON || (options.evalJSON != 'force' && | |
1425 | !(this.getHeader('Content-type') || '').include('application/json')) || | |
1426 | this.responseText.blank()) | |
1427 | return null; | |
1428 | try { | |
1429 | return this.responseText.evalJSON(options.sanitizeJSON || | |
1430 | !this.request.isSameOrigin()); | |
1431 | } catch (e) { | |
1432 | this.request.dispatchException(e); | |
} | ||
} | ||
}); | ||
827 | ||
828 | ||
829 | | |
830 | | |
1437 | Ajax.Updater = Class.create(Ajax.Request, { | |
1438 | initialize: function($super, container, url, options) { | |
1439 | this.container = { | |
1440 | success: (container.success || container), | |
1441 | failure: (container.failure || (container.success ? null : container)) | |
1442 | }; | |
1443 | ||
1444 | options = Object.clone(options); | |
1445 | var onComplete = options.onComplete; | |
1446 | options.onComplete = (function(response, json) { | |
1447 | this.updateContent(response.responseText); | |
1448 | if (Object.isFunction(onComplete)) onComplete(response, json); | |
1449 | }).bind(this); | |
1450 | ||
1451 | $super(url, options); | |
1452 | }, | |
1453 | ||
1454 | updateContent: function(responseText) { | |
1455 | var receiver = this.container[this.success() ? 'success' : 'failure'], | |
1456 | options = this.options; | |
1457 | ||
1458 | if (!options.evalScripts) responseText = responseText.stripScripts(); | |
1459 | ||
1460 | if (receiver = $(receiver)) { | |
1461 | if (options.insertion) { | |
1462 | if (Object.isString(options.insertion)) { | |
1463 | var insertion = { }; insertion[options.insertion] = responseText; | |
1464 | receiver.insert(insertion); | |
1465 | } | |
1466 | else options.insertion(receiver, responseText); | |
1467 | } | |
1468 | else receiver.update(responseText); | |
1469 | } | |
1470 | } | |
1471 | }); | |
1472 | ||
1473 | Ajax.PeriodicalUpdater = Class.create(Ajax.Base, { | |
1474 | initialize: function($super, container, url, options) { | |
1475 | $super(options); | |
this.onComplete = this.options.onComplete; | ||
this.frequency = (this.options.frequency || 2); | ||
this.decay = (this.options.decay || 1); | ||
836 | | |
1481 | this.updater = { }; | |
this.container = container; | ||
this.url = url; | ||
... | ... | |
}, | ||
stop: function() { | ||
849 | | |
1494 | this.updater.options.onComplete = undefined; | |
clearTimeout(this.timer); | ||
(this.onComplete || Prototype.emptyFunction).apply(this, arguments); | ||
}, | ||
854 | | |
1499 | updateComplete: function(response) { | |
if (this.options.decay) { | ||
856 | | |
1501 | this.decay = (response.responseText == this.lastText ? | |
this.decay * this.options.decay : 1); | ||
859 | | |
1504 | this.lastText = response.responseText; | |
} | ||
861 | | |
862 | | |
1506 | this.timer = this.onTimerEvent.bind(this).delay(this.decay * this.frequency); | |
}, | ||
onTimerEvent: function() { | ||
this.updater = new Ajax.Updater(this.container, this.url, this.options); | ||
} | ||
}); | ||
869 | ||
870 | | |
871 | | |
872 | | |
873 | | |
874 | | |
875 | | |
1513 | function $(element) { | |
1514 | if (arguments.length > 1) { | |
1515 | for (var i = 0, elements = [], length = arguments.length; i < length; i++) | |
1516 | elements.push($(arguments[i])); | |
1517 | return elements; | |
} | ||
877 | | |
1519 | if (Object.isString(element)) | |
1520 | element = document.getElementById(element); | |
1521 | return Element.extend(element); | |
} | ||
880 | ||
881 | | |
882 | | |
883 | | |
884 | | |
885 | | |
886 | | |
1524 | if (Prototype.BrowserFeatures.XPath) { | |
1525 | document._getElementsByXPath = function(expression, parentElement) { | |
1526 | var results = []; | |
1527 | var query = document.evaluate(expression, $(parentElement) || document, | |
1528 | null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); | |
1529 | for (var i = 0, length = query.snapshotLength; i < length; i++) | |
1530 | results.push(Element.extend(query.snapshotItem(i))); | |
1531 | return results; | |
1532 | }; | |
} | ||
/*--------------------------------------------------------------------------*/ | ||
891 | ||
892 | | |
1537 | if (!window.Node) var Node = { }; | |
894 | ||
895 | | |
896 | | |
1539 | if (!Node.ELEMENT_NODE) { | |
1540 | // DOM level 2 ECMAScript Language Binding | |
1541 | Object.extend(Node, { | |
1542 | ELEMENT_NODE: 1, | |
1543 | ATTRIBUTE_NODE: 2, | |
1544 | TEXT_NODE: 3, | |
1545 | CDATA_SECTION_NODE: 4, | |
1546 | ENTITY_REFERENCE_NODE: 5, | |
1547 | ENTITY_NODE: 6, | |
1548 | PROCESSING_INSTRUCTION_NODE: 7, | |
1549 | COMMENT_NODE: 8, | |
1550 | DOCUMENT_NODE: 9, | |
1551 | DOCUMENT_TYPE_NODE: 10, | |
1552 | DOCUMENT_FRAGMENT_NODE: 11, | |
1553 | NOTATION_NODE: 12 | |
1554 | }); | |
1555 | } | |
898 | | |
899 | | |
900 | | |
901 | | |
902 | | |
903 | | |
1557 | (function() { | |
1558 | var element = this.Element; | |
1559 | this.Element = function(tagName, attributes) { | |
1560 | attributes = attributes || { }; | |
1561 | tagName = tagName.toLowerCase(); | |
1562 | var cache = Element.cache; | |
1563 | if (Prototype.Browser.IE && attributes.name) { | |
1564 | tagName = '<' + tagName + ' name="' + attributes.name + '">'; | |
1565 | delete attributes.name; | |
1566 | return Element.writeAttribute(document.createElement(tagName), attributes); | |
} | ||
905 | | |
1568 | if (!cache[tagName]) cache[tagName] = Element.extend(document.createElement(tagName)); | |
1569 | return Element.writeAttribute(cache[tagName].cloneNode(false), attributes); | |
1570 | }; | |
1571 | Object.extend(this.Element, element || { }); | |
1572 | if (element) this.Element.prototype = element.prototype; | |
1573 | }).call(window); | |
907 | | |
908 | | |
909 | ||
1575 | Element.cache = { }; | |
911 | ||
912 | | |
913 | | |
914 | | |
915 | | |
916 | | |
917 | ||
918 | ||
Element.Methods = { | ||
visible: function(element) { | ||
return $(element).style.display != 'none'; | ||
}, | ||
924 | | |
925 | | |
926 | | |
927 | | |
928 | | |
1582 | toggle: function(element) { | |
1583 | element = $(element); | |
1584 | Element[Element.visible(element) ? 'hide' : 'show'](element); | |
1585 | return element; | |
}, | ||
931 | | |
932 | | |
933 | | |
934 | | |
935 | | |
1588 | hide: function(element) { | |
1589 | element = $(element); | |
1590 | element.style.display = 'none'; | |
1591 | return element; | |
}, | ||
938 | | |
939 | | |
940 | | |
941 | | |
942 | | |
1594 | show: function(element) { | |
1595 | element = $(element); | |
1596 | element.style.display = ''; | |
1597 | return element; | |
}, | ||
remove: function(element) { | ||
element = $(element); | ||
element.parentNode.removeChild(element); | ||
1603 | return element; | |
}, | ||
950 | | |
951 | | |
952 | | |
1606 | update: function(element, content) { | |
1607 | element = $(element); | |
1608 | if (content && content.toElement) content = content.toElement(); | |
1609 | if (Object.isElement(content)) return element.update().insert(content); | |
1610 | content = Object.toHTML(content); | |
1611 | element.innerHTML = content.stripScripts(); | |
1612 | content.evalScripts.bind(content).defer(); | |
1613 | return element; | |
}, | ||
955 | | |
1616 | replace: function(element, content) { | |
element = $(element); | ||
957 | | |
958 | | |
959 | | |
1618 | if (content && content.toElement) content = content.toElement(); | |
1619 | else if (!Object.isElement(content)) { | |
1620 | content = Object.toHTML(content); | |
var range = element.ownerDocument.createRange(); | ||
961 | | |
962 | | |
963 | | |
1622 | range.selectNode(element); | |
1623 | content.evalScripts.bind(content).defer(); | |
1624 | content = range.createContextualFragment(content.stripScripts()); | |
} | ||
965 | | |
1626 | element.parentNode.replaceChild(content, element); | |
1627 | return element; | |
}, | ||
968 | | |
1630 | insert: function(element, insertions) { | |
element = $(element); | ||
970 | | |
1632 | ||
1633 | if (Object.isString(insertions) || Object.isNumber(insertions) || | |
1634 | Object.isElement(insertions) || (insertions && (insertions.toElement || insertions.toHTML))) | |
1635 | insertions = {bottom:insertions}; | |
1636 | ||
1637 | var content, insert, tagName, childNodes; | |
1638 | ||
1639 | for (var position in insertions) { | |
1640 | content = insertions[position]; | |
1641 | position = position.toLowerCase(); | |
1642 | insert = Element._insertionTranslations[position]; | |
1643 | ||
1644 | if (content && content.toElement) content = content.toElement(); | |
1645 | if (Object.isElement(content)) { | |
1646 | insert(element, content); | |
1647 | continue; | |
1648 | } | |
1649 | ||
1650 | content = Object.toHTML(content); | |
1651 | ||
1652 | tagName = ((position == 'before' || position == 'after') | |
1653 | ? element.parentNode : element).tagName.toUpperCase(); | |
1654 | ||
1655 | childNodes = Element._getContentFromAnonymousElement(tagName, content.stripScripts()); | |
1656 | ||
1657 | if (position == 'top' || position == 'after') childNodes.reverse(); | |
1658 | childNodes.each(insert.curry(element)); | |
1659 | ||
1660 | content.evalScripts.bind(content).defer(); | |
1661 | } | |
1662 | ||
1663 | return element; | |
}, | ||
1666 | wrap: function(element, wrapper, attributes) { | |
1667 | element = $(element); | |
1668 | if (Object.isElement(wrapper)) | |
1669 | $(wrapper).writeAttribute(attributes || { }); | |
1670 | else if (Object.isString(wrapper)) wrapper = new Element(wrapper, attributes); | |
1671 | else wrapper = new Element('div', wrapper); | |
1672 | if (element.parentNode) | |
1673 | element.parentNode.replaceChild(wrapper, element); | |
1674 | wrapper.appendChild(element); | |
1675 | return wrapper; | |
1676 | }, | |
1677 | ||
1678 | inspect: function(element) { | |
1679 | element = $(element); | |
1680 | var result = '<' + element.tagName.toLowerCase(); | |
1681 | $H({'id': 'id', 'className': 'class'}).each(function(pair) { | |
1682 | var property = pair.first(), attribute = pair.last(); | |
1683 | var value = (element[property] || '').toString(); | |
1684 | if (value) result += ' ' + attribute + '=' + value.inspect(true); | |
1685 | }); | |
1686 | return result + '>'; | |
1687 | }, | |
1688 | ||
1689 | recursivelyCollect: function(element, property) { | |
1690 | element = $(element); | |
1691 | var elements = []; | |
1692 | while (element = element[property]) | |
1693 | if (element.nodeType == 1) | |
1694 | elements.push(Element.extend(element)); | |
1695 | return elements; | |
1696 | }, | |
1697 | ||
1698 | ancestors: function(element) { | |
1699 | return $(element).recursivelyCollect('parentNode'); | |
1700 | }, | |
1701 | ||
1702 | descendants: function(element) { | |
1703 | return $(element).select("*"); | |
1704 | }, | |
1705 | ||
1706 | firstDescendant: function(element) { | |
1707 | element = $(element).firstChild; | |
1708 | while (element && element.nodeType != 1) element = element.nextSibling; | |
1709 | return $(element); | |
1710 | }, | |
1711 | ||
1712 | immediateDescendants: function(element) { | |
1713 | if (!(element = $(element).firstChild)) return []; | |
1714 | while (element && element.nodeType != 1) element = element.nextSibling; | |
1715 | if (element) return [element].concat($(element).nextSiblings()); | |
1716 | return []; | |
1717 | }, | |
1718 | ||
1719 | previousSiblings: function(element) { | |
1720 | return $(element).recursivelyCollect('previousSibling'); | |
1721 | }, | |
1722 | ||
1723 | nextSiblings: function(element) { | |
1724 | return $(element).recursivelyCollect('nextSibling'); | |
1725 | }, | |
1726 | ||
1727 | siblings: function(element) { | |
1728 | element = $(element); | |
1729 | return element.previousSiblings().reverse().concat(element.nextSiblings()); | |
1730 | }, | |
1731 | ||
1732 | match: function(element, selector) { | |
1733 | if (Object.isString(selector)) | |
1734 | selector = new Selector(selector); | |
1735 | return selector.match($(element)); | |
1736 | }, | |
1737 | ||
1738 | up: function(element, expression, index) { | |
1739 | element = $(element); | |
1740 | if (arguments.length == 1) return $(element.parentNode); | |
1741 | var ancestors = element.ancestors(); | |
1742 | return Object.isNumber(expression) ? ancestors[expression] : | |
1743 | Selector.findElement(ancestors, expression, index); | |
1744 | }, | |
1745 | ||
1746 | down: function(element, expression, index) { | |
1747 | element = $(element); | |
1748 | if (arguments.length == 1) return element.firstDescendant(); | |
1749 | return Object.isNumber(expression) ? element.descendants()[expression] : | |
1750 | Element.select(element, expression)[index || 0]; | |
1751 | }, | |
1752 | ||
1753 | previous: function(element, expression, index) { | |
1754 | element = $(element); | |
1755 | if (arguments.length == 1) return $(Selector.handlers.previousElementSibling(element)); | |
1756 | var previousSiblings = element.previousSiblings(); | |
1757 | return Object.isNumber(expression) ? previousSiblings[expression] : | |
1758 | Selector.findElement(previousSiblings, expression, index); | |
1759 | }, | |
1760 | ||
1761 | next: function(element, expression, index) { | |
1762 | element = $(element); | |
1763 | if (arguments.length == 1) return $(Selector.handlers.nextElementSibling(element)); | |
1764 | var nextSiblings = element.nextSiblings(); | |
1765 | return Object.isNumber(expression) ? nextSiblings[expression] : | |
1766 | Selector.findElement(nextSiblings, expression, index); | |
1767 | }, | |
1768 | ||
1769 | select: function() { | |
1770 | var args = $A(arguments), element = $(args.shift()); | |
1771 | return Selector.findChildElements(element, args); | |
1772 | }, | |
1773 | ||
1774 | adjacent: function() { | |
1775 | var args = $A(arguments), element = $(args.shift()); | |
1776 | return Selector.findChildElements(element.parentNode, args).without(element); | |
1777 | }, | |
1778 | ||
1779 | identify: function(element) { | |
1780 | element = $(element); | |
1781 | var id = element.readAttribute('id'), self = arguments.callee; | |
1782 | if (id) return id; | |
1783 | do { id = 'anonymous_element_' + self.counter++ } while ($(id)); | |
1784 | element.writeAttribute('id', id); | |
1785 | return id; | |
1786 | }, | |
1787 | ||
1788 | readAttribute: function(element, name) { | |
1789 | element = $(element); | |
1790 | if (Prototype.Browser.IE) { | |
1791 | var t = Element._attributeTranslations.read; | |
1792 | if (t.values[name]) return t.values[name](element, name); | |
1793 | if (t.names[name]) name = t.names[name]; | |
1794 | if (name.include(':')) { | |
1795 | return (!element.attributes || !element.attributes[name]) ? null : | |
1796 | element.attributes[name].value; | |
1797 | } | |
1798 | } | |
1799 | return element.getAttribute(name); | |
1800 | }, | |
1801 | ||
1802 | writeAttribute: function(element, name, value) { | |
1803 | element = $(element); | |
1804 | var attributes = { }, t = Element._attributeTranslations.write; | |
1805 | ||
1806 | if (typeof name == 'object') attributes = name; | |
1807 | else attributes[name] = Object.isUndefined(value) ? true : value; | |
1808 | ||
1809 | for (var attr in attributes) { | |
1810 | name = t.names[attr] || attr; | |
1811 | value = attributes[attr]; | |
1812 | if (t.values[attr]) name = t.values[attr](element, value); | |
1813 | if (value === false || value === null) | |
1814 | element.removeAttribute(name); | |
1815 | else if (value === true) | |
1816 | element.setAttribute(name, name); | |
1817 | else element.setAttribute(name, value); | |
1818 | } | |
1819 | return element; | |
1820 | }, | |
1821 | ||
1822 | getHeight: function(element) { | |
1823 | return $(element).getDimensions().height; | |
1824 | }, | |
1825 | ||
1826 | getWidth: function(element) { | |
1827 | return $(element).getDimensions().width; | |
1828 | }, | |
1829 | ||
classNames: function(element) { | ||
return new Element.ClassNames(element); | ||
}, | ||
hasClassName: function(element, className) { | ||
if (!(element = $(element))) return; | ||
979 | | |
1836 | var elementClassName = element.className; | |
1837 | return (elementClassName.length > 0 && (elementClassName == className || | |
1838 | new RegExp("(^|\\s)" + className + "(\\s|$)").test(elementClassName))); | |
}, | ||
addClassName: function(element, className) { | ||
if (!(element = $(element))) return; | ||
984 | | |
1843 | if (!element.hasClassName(className)) | |
1844 | element.className += (element.className ? ' ' : '') + className; | |
1845 | return element; | |
}, | ||
removeClassName: function(element, className) { | ||
if (!(element = $(element))) return; | ||
989 | | |
1850 | element.className = element.className.replace( | |
1851 | new RegExp("(^|\\s+)" + className + "(\\s+|$)"), ' ').strip(); | |
1852 | return element; | |
}, | ||
1855 | toggleClassName: function(element, className) { | |
1856 | if (!(element = $(element))) return; | |
1857 | return element[element.hasClassName(className) ? | |
1858 | 'removeClassName' : 'addClassName'](className); | |
1859 | }, | |
1860 | ||
// removes whitespace-only text node children | ||
cleanWhitespace: function(element) { | ||
element = $(element); | ||
995 | | |
996 | | |
1864 | var node = element.firstChild; | |
1865 | while (node) { | |
1866 | var nextNode = node.nextSibling; | |
if (node.nodeType == 3 && !/\S/.test(node.nodeValue)) | ||
998 | | |
1868 | element.removeChild(node); | |
1869 | node = nextNode; | |
} | ||
1871 | return element; | |
}, | ||
empty: function(element) { | ||
1003 | | |
1875 | return $(element).innerHTML.blank(); | |
}, | ||
1006 | | |
1878 | descendantOf: function(element, ancestor) { | |
element = $(element), ancestor = $(ancestor); | ||
1880 | ||
1881 | if (element.compareDocumentPosition) | |
1882 | return (element.compareDocumentPosition(ancestor) & 8) === 8; | |
1883 | ||
1884 | if (ancestor.contains) | |
1885 | return ancestor.contains(element) && ancestor !== element; | |
1886 | ||
while (element = element.parentNode) | ||
if (element == ancestor) return true; | ||
1889 | ||
return false; | ||
}, | ||
scrollTo: function(element) { | ||
element = $(element); | ||
1015 | | |
1016 | | |
1017 | | |
1895 | var pos = element.cumulativeOffset(); | |
1896 | window.scrollTo(pos[0], pos[1]); | |
1897 | return element; | |
}, | ||
getStyle: function(element, style) { | ||
element = $(element); | ||
1022 | | |
1023 | | |
1024 | | |
1025 | | |
1026 | | |
1027 | | |
1028 | | |
1029 | | |
1902 | style = style == 'float' ? 'cssFloat' : style.camelize(); | |
1903 | var value = element.style[style]; | |
1904 | if (!value || value == 'auto') { | |
1905 | var css = document.defaultView.getComputedStyle(element, null); | |
1906 | value = css ? css[style] : null; | |
} | ||
1908 | if (style == 'opacity') return value ? parseFloat(value) : 1.0; | |
1909 | return value == 'auto' ? null : value; | |
1910 | }, | |
1032 | | |
1033 | | |
1912 | getOpacity: function(element) { | |
1913 | return $(element).getStyle('opacity'); | |
1914 | }, | |
1035 | | |
1916 | setStyle: function(element, styles) { | |
1917 | element = $(element); | |
1918 | var elementStyle = element.style, match; | |
1919 | if (Object.isString(styles)) { | |
1920 | element.style.cssText += ';' + styles; | |
1921 | return styles.include('opacity') ? | |
1922 | element.setOpacity(styles.match(/opacity:\s*(\d?\.?\d*)/)[1]) : element; | |
1923 | } | |
1924 | for (var property in styles) | |
1925 | if (property == 'opacity') element.setOpacity(styles[property]); | |
1926 | else | |
1927 | elementStyle[(property == 'float' || property == 'cssFloat') ? | |
1928 | (Object.isUndefined(elementStyle.styleFloat) ? 'cssFloat' : 'styleFloat') : | |
1929 | property] = styles[property]; | |
1930 | ||
1931 | return element; | |
}, | ||
1038 | | |
1934 | setOpacity: function(element, value) { | |
element = $(element); | ||
1040 | | |
1041 | | |
1936 | element.style.opacity = (value == 1 || value === '') ? '' : | |
1937 | (value < 0.00001) ? 0 : value; | |
1938 | return element; | |
}, | ||
getDimensions: function(element) { | ||
element = $(element); | ||
1046 | | |
1943 | var display = element.getStyle('display'); | |
1944 | if (display != 'none' && display != null) // Safari bug | |
return {width: element.offsetWidth, height: element.offsetHeight}; | ||
// All *Width and *Height properties give 0 on elements with display none, | ||
... | ... | |
var els = element.style; | ||
var originalVisibility = els.visibility; | ||
var originalPosition = els.position; | ||
1952 | var originalDisplay = els.display; | |
els.visibility = 'hidden'; | ||
els.position = 'absolute'; | ||
1056 | | |
1955 | els.display = 'block'; | |
var originalWidth = element.clientWidth; | ||
var originalHeight = element.clientHeight; | ||
1059 | | |
1958 | els.display = originalDisplay; | |
els.position = originalPosition; | ||
els.visibility = originalVisibility; | ||
return {width: originalWidth, height: originalHeight}; | ||
... | ... | |
element.style.position = 'relative'; | ||
// Opera returns the offset relative to the positioning context, when an | ||
// element is position relative but top and left have not been defined | ||
1073 | | |
1972 | if (Prototype.Browser.Opera) { | |
element.style.top = 0; | ||
element.style.left = 0; | ||
} | ||
} | ||
1977 | return element; | |
}, | ||
undoPositioned: function(element) { | ||
... | ... | |
element.style.bottom = | ||
element.style.right = ''; | ||
} | ||
1990 | return element; | |
}, | ||
makeClipping: function(element) { | ||
element = $(element); | ||
1094 | | |
1095 | | |
1096 | | |
1995 | if (element._overflow) return element; | |
1996 | element._overflow = Element.getStyle(element, 'overflow') || 'auto'; | |
1997 | if (element._overflow !== 'hidden') | |
element.style.overflow = 'hidden'; | ||
1999 | return element; | |
}, | ||
undoClipping: function(element) { | ||
element = $(element); | ||
1102 | | |
1103 | | |
1104 | | |
1105 | | |
1106 | ||
2004 | if (!element._overflow) return element; | |
2005 | element.style.overflow = element._overflow == 'auto' ? '' : element._overflow; | |
2006 | element._overflow = null; | |
2007 | return element; | |
2008 | }, | |
1108 | ||
2010 | cumulativeOffset: function(element) { | |
2011 | var valueT = 0, valueL = 0; | |
2012 | do { | |
2013 | valueT += element.offsetTop || 0; | |
2014 | valueL += element.offsetLeft || 0; | |
2015 | element = element.offsetParent; | |
2016 | } while (element); | |
2017 | return Element._returnOffset(valueL, valueT); | |
2018 | }, | |
1110 | ||
2020 | positionedOffset: function(element) { | |
2021 | var valueT = 0, valueL = 0; | |
2022 | do { | |
2023 | valueT += element.offsetTop || 0; | |
2024 | valueL += element.offsetLeft || 0; | |
2025 | element = element.offsetParent; | |
2026 | if (element) { | |
2027 | if (element.tagName.toUpperCase() == 'BODY') break; | |
2028 | var p = Element.getStyle(element, 'position'); | |
2029 | if (p !== 'static') break; | |
2030 | } | |
2031 | } while (element); | |
2032 | return Element._returnOffset(valueL, valueT); | |
2033 | }, | |
1112 | ||
1113 | | |
1114 | | |
1115 | ||
2035 | absolutize: function(element) { | |
2036 | element = $(element); | |
2037 | if (element.getStyle('position') == 'absolute') return element; | |
2038 | // Position.prepare(); // To be done manually by Scripty when it needs it. | |
1117 | ||
1118 | | |
2040 | var offsets = element.positionedOffset(); | |
2041 | var top = offsets[1]; | |
2042 | var left = offsets[0]; | |
2043 | var width = element.clientWidth; | |
2044 | var height = element.clientHeight; | |
1120 | | |
1121 | | |
1122 | | |
1123 | | |
1124 | | |
1125 | | |
2046 | element._originalLeft = left - parseFloat(element.style.left || 0); | |
2047 | element._originalTop = top - parseFloat(element.style.top || 0); | |
2048 | element._originalWidth = element.style.width; | |
2049 | element._originalHeight = element.style.height; | |
2050 | ||
2051 | element.style.position = 'absolute'; | |
2052 | element.style.top = top + 'px'; | |
2053 | element.style.left = left + 'px'; | |
2054 | element.style.width = width + 'px'; | |
2055 | element.style.height = height + 'px'; | |
2056 | return element; | |
2057 | }, | |
2058 | ||
2059 | relativize: function(element) { | |
2060 | element = $(element); | |
2061 | if (element.getStyle('position') == 'relative') return element; | |
2062 | // Position.prepare(); // To be done manually by Scripty when it needs it. | |
2063 | ||
2064 | element.style.position = 'relative'; | |
2065 | var top = parseFloat(element.style.top || 0) - (element._originalTop || 0); | |
2066 | var left = parseFloat(element.style.left || 0) - (element._originalLeft || 0); | |
2067 | ||
2068 | element.style.top = top + 'px'; | |
2069 | element.style.left = left + 'px'; | |
2070 | element.style.height = element._originalHeight; | |
2071 | element.style.width = element._originalWidth; | |
2072 | return element; | |
2073 | }, | |
2074 | ||
2075 | cumulativeScrollOffset: function(element) { | |
2076 | var valueT = 0, valueL = 0; | |
2077 | do { | |
2078 | valueT += element.scrollTop || 0; | |
2079 | valueL += element.scrollLeft || 0; | |
2080 | element = element.parentNode; | |
2081 | } while (element); | |
2082 | return Element._returnOffset(valueL, valueT); | |
2083 | }, | |
2084 | ||
2085 | getOffsetParent: function(element) { | |
2086 | if (element.offsetParent) return $(element.offsetParent); | |
2087 | if (element == document.body) return $(element); | |
2088 | ||
2089 | while ((element = element.parentNode) && element != document.body) | |
2090 | if (Element.getStyle(element, 'position') != 'static') | |
2091 | return $(element); | |
2092 | ||
2093 | return $(document.body); | |
2094 | }, | |
2095 | ||
2096 | viewportOffset: function(forElement) { | |
2097 | var valueT = 0, valueL = 0; | |
2098 | ||
2099 | var element = forElement; | |
2100 | do { | |
2101 | valueT += element.offsetTop || 0; | |
2102 | valueL += element.offsetLeft || 0; | |
2103 | ||
2104 | // Safari fix | |
2105 | if (element.offsetParent == document.body && | |
2106 | Element.getStyle(element, 'position') == 'absolute') break; | |
2107 | ||
2108 | } while (element = element.offsetParent); | |
2109 | ||
2110 | element = forElement; | |
2111 | do { | |
2112 | if (!Prototype.Browser.Opera || (element.tagName && (element.tagName.toUpperCase() == 'BODY'))) { | |
2113 | valueT -= element.scrollTop || 0; | |
2114 | valueL -= element.scrollLeft || 0; | |
2115 | } | |
2116 | } while (element = element.parentNode); | |
2117 | ||
2118 | return Element._returnOffset(valueL, valueT); | |
2119 | }, | |
2120 | ||
2121 | clonePosition: function(element, source) { | |
2122 | var options = Object.extend({ | |
2123 | setLeft: true, | |
2124 | setTop: true, | |
2125 | setWidth: true, | |
2126 | setHeight: true, | |
2127 | offsetTop: 0, | |
2128 | offsetLeft: 0 | |
2129 | }, arguments[2] || { }); | |
2130 | ||
2131 | // find page position of source | |
2132 | source = $(source); | |
2133 | var p = source.viewportOffset(); | |
2134 | ||
2135 | // find coordinate system to use | |
2136 | element = $(element); | |
2137 | var delta = [0, 0]; | |
2138 | var parent = null; | |
2139 | // delta [0,0] will do fine with position: fixed elements, | |
2140 | // position:absolute needs offsetParent deltas | |
2141 | if (Element.getStyle(element, 'position') == 'absolute') { | |
2142 | parent = element.getOffsetParent(); | |
2143 | delta = parent.viewportOffset(); | |
} | ||
1127 | | |
2145 | ||
2146 | // correct by body offsets (fixes Safari) | |
2147 | if (parent == document.body) { | |
2148 | delta[0] -= document.body.offsetLeft; | |
2149 | delta[1] -= document.body.offsetTop; | |
2150 | } | |
2151 | ||
2152 | // set position | |
2153 | if (options.setLeft) element.style.left = (p[0] - delta[0] + options.offsetLeft) + 'px'; | |
2154 | if (options.setTop) element.style.top = (p[1] - delta[1] + options.offsetTop) + 'px'; | |
2155 | if (options.setWidth) element.style.width = source.offsetWidth + 'px'; | |
2156 | if (options.setHeight) element.style.height = source.offsetHeight + 'px'; | |
2157 | return element; | |
} | ||
1129 | ||
2159 | }; | |
1131 | ||
2161 | Element.Methods.identify.counter = 1; | |
1133 | ||
1134 | ||
2163 | Object.extend(Element.Methods, { | |
2164 | getElementsBySelector: Element.Methods.select, | |
2165 | childElements: Element.Methods.immediateDescendants | |
2166 | }); | |
1136 | ||
2168 | Element._attributeTranslations = { | |
2169 | write: { | |
2170 | names: { | |
2171 | className: 'class', | |
2172 | htmlFor: 'for' | |
2173 | }, | |
2174 | values: { } | |
2175 | } | |
2176 | }; | |
1138 | ||
1139 | | |
2178 | if (Prototype.Browser.Opera) { | |
2179 | Element.Methods.getStyle = Element.Methods.getStyle.wrap( | |
2180 | function(proceed, element, style) { | |
2181 | switch (style) { | |
2182 | case 'left': case 'top': case 'right': case 'bottom': | |
2183 | if (proceed(element, 'position') === 'static') return null; | |
2184 | case 'height': case 'width': | |
2185 | // returns '0px' for hidden elements; we want it to return null | |
2186 | if (!Element.visible(element)) return null; | |
2187 | ||
2188 | // returns the border-box dimensions rather than the content-box | |
2189 | // dimensions, so we subtract padding and borders from the value | |
2190 | var dim = parseInt(proceed(element, style), 10); | |
2191 | ||
2192 | if (dim !== element['offset' + style.capitalize()]) | |
2193 | return dim + 'px'; | |
2194 | ||
2195 | var properties; | |
2196 | if (style === 'height') { | |
2197 | properties = ['border-top-width', 'padding-top', | |
2198 | 'padding-bottom', 'border-bottom-width']; | |
2199 | } | |
2200 | else { | |
2201 | properties = ['border-left-width', 'padding-left', | |
2202 | 'padding-right', 'border-right-width']; | |
2203 | } | |
2204 | return properties.inject(dim, function(memo, property) { | |
2205 | var val = proceed(element, property); | |
2206 | return val === null ? memo : memo - parseInt(val, 10); | |
2207 | }) + 'px'; | |
2208 | default: return proceed(element, style); | |
2209 | } | |
2210 | } | |
2211 | ); | |
2212 | ||
2213 | Element.Methods.readAttribute = Element.Methods.readAttribute.wrap( | |
2214 | function(proceed, element, attribute) { | |
2215 | if (attribute === 'title') return element.title; | |
2216 | return proceed(element, attribute); | |
2217 | } | |
2218 | ); | |
} | ||
1142 | ||
1143 | | |
1144 | | |
1145 | | |
2221 | else if (Prototype.Browser.IE) { | |
2222 | // IE doesn't report offsets correctly for static elements, so we change them | |
2223 | // to "relative" to get the values, then change them back. | |
2224 | Element.Methods.getOffsetParent = Element.Methods.getOffsetParent.wrap( | |
2225 | function(proceed, element) { | |
2226 | element = $(element); | |
2227 | // IE throws an error if element is not in document | |
2228 | try { element.offsetParent } | |
2229 | catch(e) { return $(document.body) } | |
2230 | var position = element.getStyle('position'); | |
2231 | if (position !== 'static') return proceed(element); | |
2232 | element.setStyle({ position: 'relative' }); | |
2233 | var value = proceed(element); | |
2234 | element.setStyle({ position: position }); | |
2235 | return value; | |
2236 | } | |
2237 | ); | |
1147 | | |
1148 | | |
1149 | | |
1150 | | |
1151 | | |
1152 | | |
1153 | | |
1154 | | |
1155 | | |
2239 | $w('positionedOffset viewportOffset').each(function(method) { | |
2240 | Element.Methods[method] = Element.Methods[method].wrap( | |
2241 | function(proceed, element) { | |
2242 | element = $(element); | |
2243 | try { element.offsetParent } | |
2244 | catch(e) { return Element._returnOffset(0,0) } | |
2245 | var position = element.getStyle('position'); | |
2246 | if (position !== 'static') return proceed(element); | |
2247 | // Trigger hasLayout on the offset parent so that IE6 reports | |
2248 | // accurate offsetTop and offsetLeft values for position: fixed. | |
2249 | var offsetParent = element.getOffsetParent(); | |
2250 | if (offsetParent && offsetParent.getStyle('position') === 'fixed') | |
2251 | offsetParent.setStyle({ zoom: 1 }); | |
2252 | element.setStyle({ position: 'relative' }); | |
2253 | var value = proceed(element); | |
2254 | element.setStyle({ position: position }); | |
2255 | return value; | |
2256 | } | |
2257 | ); | |
2258 | }); | |
2259 | ||
2260 | Element.Methods.cumulativeOffset = Element.Methods.cumulativeOffset.wrap( | |
2261 | function(proceed, element) { | |
2262 | try { element.offsetParent } | |
2263 | catch(e) { return Element._returnOffset(0,0) } | |
2264 | return proceed(element); | |
2265 | } | |
2266 | ); | |
2267 | ||
2268 | Element.Methods.getStyle = function(element, style) { | |
2269 | element = $(element); | |
2270 | style = (style == 'float' || style == 'cssFloat') ? 'styleFloat' : style.camelize(); | |
2271 | var value = element.style[style]; | |
2272 | if (!value && element.currentStyle) value = element.currentStyle[style]; | |
2273 | ||
2274 | if (style == 'opacity') { | |
2275 | if (value = (element.getStyle('filter') || '').match(/alpha\(opacity=(.*)\)/)) | |
2276 | if (value[1]) return parseFloat(value[1]) / 100; | |
2277 | return 1.0; | |
2278 | } | |
2279 | ||
2280 | if (value == 'auto') { | |
2281 | if ((style == 'width' || style == 'height') && (element.getStyle('display') != 'none')) | |
2282 | return element['offset' + style.capitalize()] + 'px'; | |
2283 | return null; | |
2284 | } | |
2285 | return value; | |
2286 | }; | |
2287 | ||
2288 | Element.Methods.setOpacity = function(element, value) { | |
2289 | function stripAlpha(filter){ | |
2290 | return filter.replace(/alpha\([^\)]*\)/gi,''); | |
2291 | } | |
2292 | element = $(element); | |
2293 | var currentStyle = element.currentStyle; | |
2294 | if ((currentStyle && !currentStyle.hasLayout) || | |
2295 | (!currentStyle && element.style.zoom == 'normal')) | |
2296 | element.style.zoom = 1; | |
2297 | ||
2298 | var filter = element.getStyle('filter'), style = element.style; | |
2299 | if (value == 1 || value === '') { | |
2300 | (filter = stripAlpha(filter)) ? | |
2301 | style.filter = filter : style.removeAttribute('filter'); | |
2302 | return element; | |
2303 | } else if (value < 0.00001) value = 0; | |
2304 | style.filter = stripAlpha(filter) + | |
2305 | 'alpha(opacity=' + (value * 100) + ')'; | |
2306 | return element; | |
2307 | }; | |
2308 | ||
2309 | Element._attributeTranslations = { | |
2310 | read: { | |
2311 | names: { | |
2312 | 'class': 'className', | |
2313 | 'for': 'htmlFor' | |
2314 | }, | |
2315 | values: { | |
2316 | _getAttr: function(element, attribute) { | |
2317 | return element.getAttribute(attribute, 2); | |
2318 | }, | |
2319 | _getAttrNode: function(element, attribute) { | |
2320 | var node = element.getAttributeNode(attribute); | |
2321 | return node ? node.value : ""; | |
2322 | }, | |
2323 | _getEv: function(element, attribute) { | |
2324 | attribute = element.getAttribute(attribute); | |
2325 | return attribute ? attribute.toString().slice(23, -2) : null; | |
2326 | }, | |
2327 | _flag: function(element, attribute) { | |
2328 | return $(element).hasAttribute(attribute) ? attribute : null; | |
2329 | }, | |
2330 | style: function(element) { | |
2331 | return element.style.cssText.toLowerCase(); | |
2332 | }, | |
2333 | title: function(element) { | |
2334 | return element.title; | |
} | ||
} | ||
1158 | | |
1159 | | |
1160 | | |
1161 | | |
} | ||
2338 | }; | |
1164 | | |
2340 | Element._attributeTranslations.write = { | |
2341 | names: Object.extend({ | |
2342 | cellpadding: 'cellPadding', | |
2343 | cellspacing: 'cellSpacing' | |
2344 | }, Element._attributeTranslations.read.names), | |
2345 | values: { | |
2346 | checked: function(element, value) { | |
2347 | element.checked = !!value; | |
2348 | }, | |
2349 | ||
2350 | style: function(element, value) { | |
2351 | element.style.cssText = value ? value : ''; | |
2352 | } | |
2353 | } | |
2354 | }; | |
2355 | ||
2356 | Element._attributeTranslations.has = {}; | |
2357 | ||
2358 | $w('colSpan rowSpan vAlign dateTime accessKey tabIndex ' + | |
2359 | 'encType maxLength readOnly longDesc frameBorder').each(function(attr) { | |
2360 | Element._attributeTranslations.write.names[attr.toLowerCase()] = attr; | |
2361 | Element._attributeTranslations.has[attr.toLowerCase()] = attr; | |
2362 | }); | |
2363 | ||
2364 | (function(v) { | |
2365 | Object.extend(v, { | |
2366 | href: v._getAttr, | |
2367 | src: v._getAttr, | |
2368 | type: v._getAttr, | |
2369 | action: v._getAttrNode, | |
2370 | disabled: v._flag, | |
2371 | checked: v._flag, | |
2372 | readonly: v._flag, | |
2373 | multiple: v._flag, | |
2374 | onload: v._getEv, | |
2375 | onunload: v._getEv, | |
2376 | onclick: v._getEv, | |
2377 | ondblclick: v._getEv, | |
2378 | onmousedown: v._getEv, | |
2379 | onmouseup: v._getEv, | |
2380 | onmouseover: v._getEv, | |
2381 | onmousemove: v._getEv, | |
2382 | onmouseout: v._getEv, | |
2383 | onfocus: v._getEv, | |
2384 | onblur: v._getEv, | |
2385 | onkeypress: v._getEv, | |
2386 | onkeydown: v._getEv, | |
2387 | onkeyup: v._getEv, | |
2388 | onsubmit: v._getEv, | |
2389 | onreset: v._getEv, | |
2390 | onselect: v._getEv, | |
2391 | onchange: v._getEv | |
2392 | }); | |
2393 | })(Element._attributeTranslations.read.values); | |
2394 | } | |
2395 | ||
2396 | else if (Prototype.Browser.Gecko && /rv:1\.8\.0/.test(navigator.userAgent)) { | |
2397 | Element.Methods.setOpacity = function(element, value) { | |
2398 | element = $(element); | |
2399 | element.style.opacity = (value == 1) ? 0.999999 : | |
2400 | (value === '') ? '' : (value < 0.00001) ? 0 : value; | |
2401 | return element; | |
2402 | }; | |
2403 | } | |
2404 | ||
2405 | else if (Prototype.Browser.WebKit) { | |
2406 | Element.Methods.setOpacity = function(element, value) { | |
2407 | element = $(element); | |
2408 | element.style.opacity = (value == 1 || value === '') ? '' : | |
2409 | (value < 0.00001) ? 0 : value; | |
2410 | ||
2411 | if (value == 1) | |
2412 | if(element.tagName.toUpperCase() == 'IMG' && element.width) { | |
2413 | element.width++; element.width--; | |
2414 | } else try { | |
2415 | var n = document.createTextNode(' '); | |
2416 | element.appendChild(n); | |
2417 | element.removeChild(n); | |
2418 | } catch (e) { } | |
2419 | ||
2420 | return element; | |
2421 | }; | |
2422 | ||
2423 | // Safari returns margins on body which is incorrect if the child is absolutely | |
2424 | // positioned. For performance reasons, redefine Element#cumulativeOffset for | |
2425 | // KHTML/WebKit only. | |
2426 | Element.Methods.cumulativeOffset = function(element) { | |
2427 | var valueT = 0, valueL = 0; | |
2428 | do { | |
2429 | valueT += element.offsetTop || 0; | |
2430 | valueL += element.offsetLeft || 0; | |
2431 | if (element.offsetParent == document.body) | |
2432 | if (Element.getStyle(element, 'position') == 'absolute') break; | |
2433 | ||
2434 | element = element.offsetParent; | |
2435 | } while (element); | |
2436 | ||
2437 | return Element._returnOffset(valueL, valueT); | |
2438 | }; | |
2439 | } | |
2440 | ||
2441 | if (Prototype.Browser.IE || Prototype.Browser.Opera) { | |
2442 | // IE and Opera are missing .innerHTML support for TABLE-related and SELECT elements | |
2443 | Element.Methods.update = function(element, content) { | |
2444 | element = $(element); | |
2445 | ||
2446 | if (content && content.toElement) content = content.toElement(); | |
2447 | if (Object.isElement(content)) return element.update().insert(content); | |
2448 | ||
2449 | content = Object.toHTML(content); | |
2450 | var tagName = element.tagName.toUpperCase(); | |
2451 | ||
2452 | if (tagName in Element._insertionTranslations.tags) { | |
2453 | $A(element.childNodes).each(function(node) { element.removeChild(node) }); | |
2454 | Element._getContentFromAnonymousElement(tagName, content.stripScripts()) | |
2455 | .each(function(node) { element.appendChild(node) }); | |
2456 | } | |
2457 | else element.innerHTML = content.stripScripts(); | |
2458 | ||
2459 | content.evalScripts.bind(content).defer(); | |
2460 | return element; | |
2461 | }; | |
2462 | } | |
2463 | ||
2464 | if ('outerHTML' in document.createElement('div')) { | |
2465 | Element.Methods.replace = function(element, content) { | |
2466 | element = $(element); | |
2467 | ||
2468 | if (content && content.toElement) content = content.toElement(); | |
2469 | if (Object.isElement(content)) { | |
2470 | element.parentNode.replaceChild(content, element); | |
2471 | return element; | |
2472 | } | |
2473 | ||
2474 | content = Object.toHTML(content); | |
2475 | var parent = element.parentNode, tagName = parent.tagName.toUpperCase(); | |
2476 | ||
2477 | if (Element._insertionTranslations.tags[tagName]) { | |
2478 | var nextSibling = element.next(); | |
2479 | var fragments = Element._getContentFromAnonymousElement(tagName, content.stripScripts()); | |
2480 | parent.removeChild(element); | |
2481 | if (nextSibling) | |
2482 | fragments.each(function(node) { parent.insertBefore(node, nextSibling) }); | |
2483 | else | |
2484 | fragments.each(function(node) { parent.appendChild(node) }); | |
2485 | } | |
2486 | else element.outerHTML = content.stripScripts(); | |
2487 | ||
2488 | content.evalScripts.bind(content).defer(); | |
2489 | return element; | |
2490 | }; | |
2491 | } | |
2492 | ||
2493 | Element._returnOffset = function(l, t) { | |
2494 | var result = [l, t]; | |
2495 | result.left = l; | |
2496 | result.top = t; | |
2497 | return result; | |
2498 | }; | |
2499 | ||
2500 | Element._getContentFromAnonymousElement = function(tagName, html) { | |
2501 | var div = new Element('div'), t = Element._insertionTranslations.tags[tagName]; | |
2502 | if (t) { | |
2503 | div.innerHTML = t[0] + html + t[1]; | |
2504 | t[2].times(function() { div = div.firstChild }); | |
2505 | } else div.innerHTML = html; | |
2506 | return $A(div.childNodes); | |
2507 | }; | |
2508 | ||
2509 | Element._insertionTranslations = { | |
2510 | before: function(element, node) { | |
2511 | element.parentNode.insertBefore(node, element); | |
}, | ||
2513 | top: function(element, node) { | |
2514 | element.insertBefore(node, element.firstChild); | |
2515 | }, | |
2516 | bottom: function(element, node) { | |
2517 | element.appendChild(node); | |
2518 | }, | |
2519 | after: function(element, node) { | |
2520 | element.parentNode.insertBefore(node, element.nextSibling); | |
2521 | }, | |
2522 | tags: { | |
2523 | TABLE: ['<table>', '</table>', 1], | |
2524 | TBODY: ['<table><tbody>', '</tbody></table>', 2], | |
2525 | TR: ['<table><tbody><tr>', '</tr></tbody></table>', 3], | |
2526 | TD: ['<table><tbody><tr><td>', '</td></tr></tbody></table>', 4], | |
2527 | SELECT: ['<select>', '</select>', 1] | |
2528 | } | |
2529 | }; | |
1167 | | |
1168 | | |
1169 | | |
1170 | | |
2531 | (function() { | |
2532 | Object.extend(this.tags, { | |
2533 | THEAD: this.tags.TBODY, | |
2534 | TFOOT: this.tags.TBODY, | |
2535 | TH: this.tags.TD | |
2536 | }); | |
2537 | }).call(Element._insertionTranslations); | |
2538 | ||
2539 | Element.Methods.Simulated = { | |
2540 | hasAttribute: function(element, attribute) { | |
2541 | attribute = Element._attributeTranslations.has[attribute] || attribute; | |
2542 | var node = $(element).getAttributeNode(attribute); | |
2543 | return !!(node && node.specified); | |
} | ||
2545 | }; | |
2546 | ||
2547 | Element.Methods.ByTag = { }; | |
2548 | ||
2549 | Object.extend(Element, Element.Methods); | |
2550 | ||
2551 | if (!Prototype.BrowserFeatures.ElementExtensions && | |
2552 | document.createElement('div')['__proto__']) { | |
2553 | window.HTMLElement = { }; | |
2554 | window.HTMLElement.prototype = document.createElement('div')['__proto__']; | |
2555 | Prototype.BrowserFeatures.ElementExtensions = true; | |
} | ||
1174 | ||
2558 | Element.extend = (function() { | |
2559 | if (Prototype.BrowserFeatures.SpecificElementExtensions) | |
2560 | return Prototype.K; | |
1176 | ||
1177 | ||
1178 | | |
1179 | | |
1180 | | |
2562 | var Methods = { }, ByTag = Element.Methods.ByTag; | |
1182 | | |
1183 | | |
1184 | | |
1185 | | |
2564 | var extend = Object.extend(function(element) { | |
2565 | if (!element || element._extendedByPrototype || | |
2566 | element.nodeType != 1 || element == window) return element; | |
2567 | ||
2568 | var methods = Object.clone(Methods), | |
2569 | tagName = element.tagName.toUpperCase(), property, value; | |
2570 | ||
2571 | // extend methods for specific tags | |
2572 | if (ByTag[tagName]) Object.extend(methods, ByTag[tagName]); | |
2573 | ||
2574 | for (property in methods) { | |
2575 | value = methods[property]; | |
2576 | if (Object.isFunction(value) && !(property in element)) | |
2577 | element[property] = value.methodize(); | |
2578 | } | |
2579 | ||
2580 | element._extendedByPrototype = Prototype.emptyFunction; | |
2581 | return element; | |
2582 | ||
2583 | }, { | |
2584 | refresh: function() { | |
2585 | // extend methods for all tags (Safari doesn't need this) | |
2586 | if (!Prototype.BrowserFeatures.ElementExtensions) { | |
2587 | Object.extend(Methods, Element.Methods); | |
2588 | Object.extend(Methods, Element.Methods.Simulated); | |
2589 | } | |
2590 | } | |
2591 | }); | |
2592 | ||
2593 | extend.refresh(); | |
2594 | return extend; | |
2595 | })(); | |
2596 | ||
2597 | Element.hasAttribute = function(element, attribute) { | |
2598 | if (element.hasAttribute) return element.hasAttribute(attribute); | |
2599 | return Element.Methods.Simulated.hasAttribute(element, attribute); | |
2600 | }; | |
2601 | ||
2602 | Element.addMethods = function(methods) { | |
2603 | var F = Prototype.BrowserFeatures, T = Element.Methods.ByTag; | |
2604 | ||
2605 | if (!methods) { | |
2606 | Object.extend(Form, Form.Methods); | |
2607 | Object.extend(Form.Element, Form.Element.Methods); | |
2608 | Object.extend(Element.Methods.ByTag, { | |
2609 | "FORM": Object.clone(Form.Methods), | |
2610 | "INPUT": Object.clone(Form.Element.Methods), | |
2611 | "SELECT": Object.clone(Form.Element.Methods), | |
2612 | "TEXTAREA": Object.clone(Form.Element.Methods) | |
2613 | }); | |
} | ||
1187 | ||
1189 | ||
1190 | ||
1191 | | |
1192 | | |
1193 | | |
1194 | | |
2616 | if (arguments.length == 2) { | |
2617 | var tagName = methods; | |
2618 | methods = arguments[1]; | |
2619 | } | |
1196 | | |
1197 | | |
1198 | | |
1199 | | |
2621 | if (!tagName) Object.extend(Element.Methods, methods || { }); | |
2622 | else { | |
2623 | if (Object.isArray(tagName)) tagName.each(extend); | |
2624 | else extend(tagName); | |
} | ||
1201 | ||
1203 | ||
1204 | ||
1205 | | |
1206 | | |
1207 | | |
1208 | | |
2627 | function extend(tagName) { | |
2628 | tagName = tagName.toUpperCase(); | |
2629 | if (!Element.Methods.ByTag[tagName]) | |
2630 | Element.Methods.ByTag[tagName] = { }; | |
2631 | Object.extend(Element.Methods.ByTag[tagName], methods); | |
2632 | } | |
1210 | | |
1211 | | |
1212 | | |
1213 | | |
2634 | function copy(methods, destination, onlyIfAbsent) { | |
2635 | onlyIfAbsent = onlyIfAbsent || false; | |
2636 | for (var property in methods) { | |
2637 | var value = methods[property]; | |
2638 | if (!Object.isFunction(value)) continue; | |
2639 | if (!onlyIfAbsent || !(property in destination)) | |
2640 | destination[property] = value.methodize(); | |
2641 | } | |
} | ||
1215 | ||
1217 | ||
1218 | ||
1219 | | |
1220 | | |
1221 | | |
2644 | function findDOMClass(tagName) { | |
2645 | var klass; | |
2646 | var trans = { | |
2647 | "OPTGROUP": "OptGroup", "TEXTAREA": "TextArea", "P": "Paragraph", | |
2648 | "FIELDSET": "FieldSet", "UL": "UList", "OL": "OList", "DL": "DList", | |
2649 | "DIR": "Directory", "H1": "Heading", "H2": "Heading", "H3": "Heading", | |
2650 | "H4": "Heading", "H5": "Heading", "H6": "Heading", "Q": "Quote", | |
2651 | "INS": "Mod", "DEL": "Mod", "A": "Anchor", "IMG": "Image", "CAPTION": | |
2652 | "TableCaption", "COL": "TableCol", "COLGROUP": "TableCol", "THEAD": | |
2653 | "TableSection", "TFOOT": "TableSection", "TBODY": "TableSection", "TR": | |
2654 | "TableRow", "TH": "TableCell", "TD": "TableCell", "FRAMESET": | |
2655 | "FrameSet", "IFRAME": "IFrame" | |
2656 | }; | |
2657 | if (trans[tagName]) klass = 'HTML' + trans[tagName] + 'Element'; | |
2658 | if (window[klass]) return window[klass]; | |
2659 | klass = 'HTML' + tagName + 'Element'; | |
2660 | if (window[klass]) return window[klass]; | |
2661 | klass = 'HTML' + tagName.capitalize() + 'Element'; | |
2662 | if (window[klass]) return window[klass]; | |
1223 | | |
1224 | | |
1225 | | |
1226 | | |
1227 | | |
2664 | window[klass] = { }; | |
2665 | window[klass].prototype = document.createElement(tagName)['__proto__']; | |
2666 | return window[klass]; | |
} | ||
1229 | ||
1231 | ||
2669 | if (F.ElementExtensions) { | |
2670 | copy(Element.Methods, HTMLElement.prototype); | |
2671 | copy(Element.Methods.Simulated, HTMLElement.prototype, true); | |
2672 | } | |
1233 | ||
1234 | ||
1235 | | |
1236 | | |
1237 | | |
2674 | if (F.SpecificElementExtensions) { | |
2675 | for (var tag in Element.Methods.ByTag) { | |
2676 | var klass = findDOMClass(tag); | |
2677 | if (Object.isUndefined(klass)) continue; | |
2678 | copy(T[tag], klass.prototype); | |
2679 | } | |
2680 | } | |
1239 | | |
1240 | | |
1241 | | |
1242 | | |
1243 | | |
2682 | Object.extend(Element, Element.Methods); | |
2683 | delete Element.ByTag; | |
1245 | | |
1246 | | |
2685 | if (Element.extend.refresh) Element.extend.refresh(); | |
2686 | Element.cache = { }; | |
2687 | }; | |
2688 | ||
2689 | document.viewport = { | |
2690 | getDimensions: function() { | |
2691 | var dimensions = { }, B = Prototype.Browser; | |
2692 | $w('width height').each(function(d) { | |
2693 | var D = d.capitalize(); | |
2694 | if (B.WebKit && !document.evaluate) { | |
2695 | // Safari <3.0 needs self.innerWidth/Height | |
2696 | dimensions[d] = self['inner' + D]; | |
2697 | } else if (B.Opera && parseFloat(window.opera.version()) < 9.5) { | |
2698 | // Opera <9.5 needs document.body.clientWidth/Height | |
2699 | dimensions[d] = document.body['client' + D] | |
2700 | } else { | |
2701 | dimensions[d] = document.documentElement['client' + D]; | |
2702 | } | |
2703 | }); | |
2704 | return dimensions; | |
}, | ||
1249 | | |
1250 | | |
1251 | | |
2707 | getWidth: function() { | |
2708 | return this.getDimensions().width; | |
}, | ||
1254 | | |
1255 | | |
1256 | | |
1257 | | |
1258 | | |
2711 | getHeight: function() { | |
2712 | return this.getDimensions().height; | |
}, | ||
1261 | | |
1262 | | |
2715 | getScrollOffsets: function() { | |
2716 | return Element._returnOffset( | |
2717 | window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft, | |
2718 | window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop); | |
} | ||
1264 | ||
2720 | }; | |
2721 | /* Portions of the Selector class are derived from Jack Slocum's DomQuery, | |
2722 | * part of YUI-Ext version 0.40, distributed under the terms of an MIT-style | |
2723 | * license. Please see http://www.yui-ext.com/ for more information. */ | |
1266 | ||
1267 | ||
1268 | ||
2725 | var Selector = Class.create({ | |
initialize: function(expression) { | ||
1270 | | |
1271 | | |
1272 | | |
1273 | | |
2727 | this.expression = expression.strip(); | |
2728 | ||
2729 | if (this.shouldUseSelectorsAPI()) { | |
2730 | this.mode = 'selectorsAPI'; | |
2731 | } else if (this.shouldUseXPath()) { | |
2732 | this.mode = 'xpath'; | |
2733 | this.compileXPathMatcher(); | |
2734 | } else { | |
2735 | this.mode = "normal"; | |
2736 | this.compileMatcher(); | |
2737 | } | |
2738 | ||
}, | ||
1276 | | |
1277 | | |
2741 | shouldUseXPath: function() { | |
2742 | if (!Prototype.BrowserFeatures.XPath) return false; | |
1279 | | |
2744 | var e = this.expression; | |
1281 | | |
1282 | | |
1283 | | |
1284 | | |
1285 | | |
2746 | // Safari 3 chokes on :*-of-type and :empty | |
2747 | if (Prototype.Browser.WebKit && | |
2748 | (e.include("-of-type") || e.include(":empty"))) | |
2749 | return false; | |
2750 | ||
2751 | // XPath can't do namespaced attributes, nor can it read | |
2752 | // the "checked" property from DOM nodes | |
2753 | if ((/(\[[\w-]*?:|:checked)/).test(e)) | |
2754 | return false; | |
2755 | ||
2756 | return true; | |
2757 | }, | |
2758 | ||
2759 | shouldUseSelectorsAPI: function() { | |
2760 | if (!Prototype.BrowserFeatures.SelectorsAPI) return false; | |
2761 | ||
2762 | if (!Selector._div) Selector._div = new Element('div'); | |
2763 | ||
2764 | // Make sure the browser treats the selector as valid. Test on an | |
2765 | // isolated element to minimize cost of this check. | |
2766 | try { | |
2767 | Selector._div.querySelector(this.expression); | |
2768 | } catch(e) { | |
2769 | return false; | |
} | ||
1288 | | |
2772 | return true; | |
2773 | }, | |
1290 | | |
1291 | | |
1292 | | |
1293 | | |
1294 | | |
1295 | | |
1296 | | |
1297 | | |
2775 | compileMatcher: function() { | |
2776 | var e = this.expression, ps = Selector.patterns, h = Selector.handlers, | |
2777 | c = Selector.criteria, le, p, m; | |
2778 | ||
2779 | if (Selector._cache[e]) { | |
2780 | this.matcher = Selector._cache[e]; | |
2781 | return; | |
2782 | } | |
2783 | ||
2784 | this.matcher = ["this.matcher = function(root) {", | |
2785 | "var r = root, h = Selector.handlers, c = false, n;"]; | |
2786 | ||
2787 | while (e && le != e && (/\S/).test(e)) { | |
2788 | le = e; | |
2789 | for (var i in ps) { | |
2790 | p = ps[i]; | |
2791 | if (m = e.match(p)) { | |
2792 | this.matcher.push(Object.isFunction(c[i]) ? c[i](m) : | |
2793 | new Template(c[i]).evaluate(m)); | |
2794 | e = e.replace(m[0], ''); | |
2795 | break; | |
2796 | } | |
} | ||
1299 | | |
} | ||
1302 | | |
2800 | this.matcher.push("return h.unique(n);\n}"); | |
2801 | eval(this.matcher.join('\n')); | |
2802 | Selector._cache[this.expression] = this.matcher; | |
}, | ||
1305 | | |
1306 | | |
2805 | compileXPathMatcher: function() { | |
2806 | var e = this.expression, ps = Selector.patterns, | |
2807 | x = Selector.xpath, le, m; | |
1308 | | |
1309 | | |
1310 | | |
1311 | | |
1312 | | |
1313 | | |
1314 | | |
1315 | | |
1316 | | |
1317 | | |
1318 | | |
1319 | | |
1320 | | |
1321 | | |
1322 | | |
2809 | if (Selector._cache[e]) { | |
2810 | this.xpath = Selector._cache[e]; return; | |
2811 | } | |
1324 | | |
1325 | | |
1326 | | |
1327 | | |
1328 | | |
1329 | | |
1330 | | |
1331 | | |
1332 | | |
1333 | | |
2813 | this.matcher = ['.//*']; | |
2814 | while (e && le != e && (/\S/).test(e)) { | |
2815 | le = e; | |
2816 | for (var i in ps) { | |
2817 | if (m = e.match(ps[i])) { | |
2818 | this.matcher.push(Object.isFunction(x[i]) ? x[i](m) : | |
2819 | new Template(x[i]).evaluate(m)); | |
2820 | e = e.replace(m[0], ''); | |
2821 | break; | |
} | ||
1335 | | |
2823 | } | |
} | ||
1338 | | |
2826 | this.xpath = this.matcher.join(''); | |
2827 | Selector._cache[this.expression] = this.xpath; | |
}, | ||
1341 | | |
1342 | | |
1343 | | |
2830 | findElements: function(root) { | |
2831 | root = root || document; | |
2832 | var e = this.expression, results; | |
2833 | ||
2834 | switch (this.mode) { | |
2835 | case 'selectorsAPI': | |
2836 | // querySelectorAll queries document-wide, then filters to descendants | |
2837 | // of the context element. That's not what we want. | |
2838 | // Add an explicit context to the selector if necessary. | |
2839 | if (root !== document) { | |
2840 | var oldId = root.id, id = $(root).identify(); | |
2841 | e = "#" + id + " " + e; | |
2842 | } | |
2843 | ||
2844 | results = $A(root.querySelectorAll(e)).map(Element.extend); | |
2845 | root.id = oldId; | |
2846 | ||
2847 | return results; | |
2848 | case 'xpath': | |
2849 | return document._getElementsByXPath(this.xpath, root); | |
2850 | default: | |
2851 | return this.matcher(root); | |
2852 | } | |
}, | ||
1346 | | |
1347 | | |
2855 | match: function(element) { | |
2856 | this.tokens = []; | |
1349 | | |
1350 | | |
1351 | | |
1352 | | |
2858 | var e = this.expression, ps = Selector.patterns, as = Selector.assertions; | |
2859 | var le, p, m; | |
1354 | | |
2861 | while (e && le !== e && (/\S/).test(e)) { | |
2862 | le = e; | |
2863 | for (var i in ps) { | |
2864 | p = ps[i]; | |
2865 | if (m = e.match(p)) { | |
2866 | // use the Selector.assertions methods unless the selector | |
2867 | // is too complex. | |
2868 | if (as[i]) { | |
2869 | this.tokens.push([i, Object.clone(m)]); | |
2870 | e = e.replace(m[0], ''); | |
2871 | } else { | |
2872 | // reluctantly do a document-wide search | |
2873 | // and look for a match in the array | |
2874 | return this.findElements(document).include(element); | |
2875 | } | |
2876 | } | |
2877 | } | |
2878 | } | |
1356 | | |
1357 | | |
1358 | | |
1359 | | |
2880 | var match = true, name, matches; | |
2881 | for (var i = 0, token; token = this.tokens[i]; i++) { | |
2882 | name = token[0], matches = token[1]; | |
2883 | if (!Selector.assertions[name](element, matches)) { | |
2884 | match = false; break; | |
2885 | } | |
2886 | } | |
1361 | | |
2888 | return match; | |
}, | ||
toString: function() { | ||
return this.expression; | ||
2893 | }, | |
2894 | ||
2895 | inspect: function() { | |
2896 | return "#<Selector:" + this.expression.inspect() + ">"; | |
} | ||
1367 | ||
2898 | }); | |
1369 | ||
1370 | | |
1371 | | |
1372 | | |
1373 | | |
1374 | | |
1375 | | |
1376 | ||
1377 | ||
1378 | | |
1379 | | |
1380 | | |
2900 | Object.extend(Selector, { | |
2901 | _cache: { }, | |
2902 | ||
2903 | xpath: { | |
2904 | descendant: "//*", | |
2905 | child: "/*", | |
2906 | adjacent: "/following-sibling::*[1]", | |
2907 | laterSibling: '/following-sibling::*', | |
2908 | tagName: function(m) { | |
2909 | if (m[1] == '*') return ''; | |
2910 | return "[local-name()='" + m[1].toLowerCase() + | |
2911 | "' or local-name()='" + m[1].toUpperCase() + "']"; | |
2912 | }, | |
2913 | className: "[contains(concat(' ', @class, ' '), ' #{1} ')]", | |
2914 | id: "[@id='#{1}']", | |
2915 | attrPresence: function(m) { | |
2916 | m[1] = m[1].toLowerCase(); | |
2917 | return new Template("[@#{1}]").evaluate(m); | |
2918 | }, | |
2919 | attr: function(m) { | |
2920 | m[1] = m[1].toLowerCase(); | |
2921 | m[3] = m[5] || m[6]; | |
2922 | return new Template(Selector.xpath.operators[m[2]]).evaluate(m); | |
2923 | }, | |
2924 | pseudo: function(m) { | |
2925 | var h = Selector.xpath.pseudos[m[1]]; | |
2926 | if (!h) return ''; | |
2927 | if (Object.isFunction(h)) return h(m); | |
2928 | return new Template(Selector.xpath.pseudos[m[1]]).evaluate(m); | |
2929 | }, | |
2930 | operators: { | |
2931 | '=': "[@#{1}='#{3}']", | |
2932 | '!=': "[@#{1}!='#{3}']", | |
2933 | '^=': "[starts-with(@#{1}, '#{3}')]", | |
2934 | '$=': "[substring(@#{1}, (string-length(@#{1}) - string-length('#{3}') + 1))='#{3}']", | |
2935 | '*=': "[contains(@#{1}, '#{3}')]", | |
2936 | '~=': "[contains(concat(' ', @#{1}, ' '), ' #{3} ')]", | |
2937 | '|=': "[contains(concat('-', @#{1}, '-'), '-#{3}-')]" | |
2938 | }, | |
2939 | pseudos: { | |
2940 | 'first-child': '[not(preceding-sibling::*)]', | |
2941 | 'last-child': '[not(following-sibling::*)]', | |
2942 | 'only-child': '[not(preceding-sibling::* or following-sibling::*)]', | |
2943 | 'empty': "[count(*) = 0 and (count(text()) = 0)]", | |
2944 | 'checked': "[@checked]", | |
2945 | 'disabled': "[(@disabled) and (@type!='hidden')]", | |
2946 | 'enabled': "[not(@disabled) and (@type!='hidden')]", | |
2947 | 'not': function(m) { | |
2948 | var e = m[6], p = Selector.patterns, | |
2949 | x = Selector.xpath, le, v; | |
2950 | ||
2951 | var exclusion = []; | |
2952 | while (e && le != e && (/\S/).test(e)) { | |
2953 | le = e; | |
2954 | for (var i in p) { | |
2955 | if (m = e.match(p[i])) { | |
2956 | v = Object.isFunction(x[i]) ? x[i](m) : new Template(x[i]).evaluate(m); | |
2957 | exclusion.push("(" + v.substring(1, v.length - 1) + ")"); | |
2958 | e = e.replace(m[0], ''); | |
2959 | break; | |
2960 | } | |
2961 | } | |
2962 | } | |
2963 | return "[not(" + exclusion.join(" and ") + ")]"; | |
2964 | }, | |
2965 | 'nth-child': function(m) { | |
2966 | return Selector.xpath.pseudos.nth("(count(./preceding-sibling::*) + 1) ", m); | |
2967 | }, | |
2968 | 'nth-last-child': function(m) { | |
2969 | return Selector.xpath.pseudos.nth("(count(./following-sibling::*) + 1) ", m); | |
2970 | }, | |
2971 | 'nth-of-type': function(m) { | |
2972 | return Selector.xpath.pseudos.nth("position() ", m); | |
2973 | }, | |
2974 | 'nth-last-of-type': function(m) { | |
2975 | return Selector.xpath.pseudos.nth("(last() + 1 - position()) ", m); | |
2976 | }, | |
2977 | 'first-of-type': function(m) { | |
2978 | m[6] = "1"; return Selector.xpath.pseudos['nth-of-type'](m); | |
2979 | }, | |
2980 | 'last-of-type': function(m) { | |
2981 | m[6] = "1"; return Selector.xpath.pseudos['nth-last-of-type'](m); | |
2982 | }, | |
2983 | 'only-of-type': function(m) { | |
2984 | var p = Selector.xpath.pseudos; return p['first-of-type'](m) + p['last-of-type'](m); | |
2985 | }, | |
2986 | nth: function(fragment, m) { | |
2987 | var mm, formula = m[6], predicate; | |
2988 | if (formula == 'even') formula = '2n+0'; | |
2989 | if (formula == 'odd') formula = '2n+1'; | |
2990 | if (mm = formula.match(/^(\d+)$/)) // digit only | |
2991 | return '[' + fragment + "= " + mm[1] + ']'; | |
2992 | if (mm = formula.match(/^(-?\d*)?n(([+-])(\d+))?/)) { // an+b | |
2993 | if (mm[1] == "-") mm[1] = -1; | |
2994 | var a = mm[1] ? Number(mm[1]) : 1; | |
2995 | var b = mm[2] ? Number(mm[2]) : 0; | |
2996 | predicate = "[((#{fragment} - #{b}) mod #{a} = 0) and " + | |
2997 | "((#{fragment} - #{b}) div #{a} >= 0)]"; | |
2998 | return new Template(predicate).evaluate({ | |
2999 | fragment: fragment, a: a, b: b }); | |
3000 | } | |
3001 | } | |
3002 | } | |
}, | ||
1383 | | |
1384 | | |
3005 | criteria: { | |
3006 | tagName: 'n = h.tagName(n, r, "#{1}", c); c = false;', | |
3007 | className: 'n = h.className(n, r, "#{1}", c); c = false;', | |
3008 | id: 'n = h.id(n, r, "#{1}", c); c = false;', | |
3009 | attrPresence: 'n = h.attrPresence(n, r, "#{1}", c); c = false;', | |
3010 | attr: function(m) { | |
3011 | m[3] = (m[5] || m[6]); | |
3012 | return new Template('n = h.attr(n, r, "#{1}", "#{3}", "#{2}", c); c = false;').evaluate(m); | |
3013 | }, | |
3014 | pseudo: function(m) { | |
3015 | if (m[6]) m[6] = m[6].replace(/"/g, '\\"'); | |
3016 | return new Template('n = h.pseudo(n, "#{1}", "#{6}", r, c); c = false;').evaluate(m); | |
3017 | }, | |
3018 | descendant: 'c = "descendant";', | |
3019 | child: 'c = "child";', | |
3020 | adjacent: 'c = "adjacent";', | |
3021 | laterSibling: 'c = "laterSibling";' | |
}, | ||
1387 | | |
1388 | | |
1389 | | |
1390 | | |
3024 | patterns: { | |
3025 | // combinators must be listed first | |
3026 | // (and descendant needs to be last combinator) | |
3027 | laterSibling: /^\s*~\s*/, | |
3028 | child: /^\s*>\s*/, | |
3029 | adjacent: /^\s*\+\s*/, | |
3030 | descendant: /^\s/, | |
3031 | ||
3032 | // selectors follow | |
3033 | tagName: /^\s*(\*|[\w\-]+)(\b|$)?/, | |
3034 | id: /^#([\w\-\*]+)(\b|$)/, | |
3035 | className: /^\.([\w\-\*]+)(\b|$)/, | |
3036 | pseudo: | |
3037 | /^:((first|last|nth|nth-last|only)(-child|-of-type)|empty|checked|(en|dis)abled|not)(\((.*?)\))?(\b|$|(?=\s|[:+~>]))/, | |
3038 | attrPresence: /^\[((?:[\w]+:)?[\w]+)\]/, | |
3039 | attr: /\[((?:[\w-]*:)?[\w-]+)\s*(?:([!^$*~|]?=)\s*((['"])([^\4]*?)\4|([^'"][^\]]*?)))?\]/ | |
}, | ||
1393 | | |
1394 | | |
3042 | // for Selector.match and Element#match | |
3043 | assertions: { | |
3044 | tagName: function(element, matches) { | |
3045 | return matches[1].toUpperCase() == element.tagName.toUpperCase(); | |
3046 | }, | |
3047 | ||
3048 | className: function(element, matches) { | |
3049 | return Element.hasClassName(element, matches[1]); | |
3050 | }, | |
3051 | ||
3052 | id: function(element, matches) { | |
3053 | return element.id === matches[1]; | |
3054 | }, | |
3055 | ||
3056 | attrPresence: function(element, matches) { | |
3057 | return Element.hasAttribute(element, matches[1]); | |
3058 | }, | |
3059 | ||
3060 | attr: function(element, matches) { | |
3061 | var nodeValue = Element.readAttribute(element, matches[1]); | |
3062 | return nodeValue && Selector.operators[matches[2]](nodeValue, matches[5] || matches[6]); | |
3063 | } | |
}, | ||
1397 | | |
1398 | | |
1399 | | |
1400 | | |
1401 | | |
1402 | | |
1403 | ||
3066 | handlers: { | |
3067 | // UTILITY FUNCTIONS | |
3068 | // joins two collections | |
3069 | concat: function(a, b) { | |
3070 | for (var i = 0, node; node = b[i]; i++) | |
3071 | a.push(node); | |
3072 | return a; | |
3073 | }, | |
1405 | ||
3075 | // marks an array of nodes for counting | |
3076 | mark: function(nodes) { | |
3077 | var _true = Prototype.emptyFunction; | |
3078 | for (var i = 0, node; node = nodes[i]; i++) | |
3079 | node._countedByPrototype = _true; | |
3080 | return nodes; | |
3081 | }, | |
1407 | ||
1408 | | |
1409 | | |
1410 | | |
3083 | unmark: function(nodes) { | |
3084 | for (var i = 0, node; node = nodes[i]; i++) | |
3085 | node._countedByPrototype = undefined; | |
3086 | return nodes; | |
3087 | }, | |
1412 | | |
1413 | | |
1414 | | |
1415 | | |
3089 | // mark each child node with its position (for nth calls) | |
3090 | // "ofType" flag indicates whether we're indexing for nth-of-type | |
3091 | // rather than nth-child | |
3092 | index: function(parentNode, reverse, ofType) { | |
3093 | parentNode._countedByPrototype = Prototype.emptyFunction; | |
3094 | if (reverse) { | |
3095 | for (var nodes = parentNode.childNodes, i = nodes.length - 1, j = 1; i >= 0; i--) { | |
3096 | var node = nodes[i]; | |
3097 | if (node.nodeType == 1 && (!ofType || node._countedByPrototype)) node.nodeIndex = j++; | |
3098 | } | |
3099 | } else { | |
3100 | for (var i = 0, j = 1, nodes = parentNode.childNodes; node = nodes[i]; i++) | |
3101 | if (node.nodeType == 1 && (!ofType || node._countedByPrototype)) node.nodeIndex = j++; | |
3102 | } | |
3103 | }, | |
3104 | ||
3105 | // filters out duplicates and extends all nodes | |
3106 | unique: function(nodes) { | |
3107 | if (nodes.length == 0) return nodes; | |
3108 | var results = [], n; | |
3109 | for (var i = 0, l = nodes.length; i < l; i++) | |
3110 | if (!(n = nodes[i])._countedByPrototype) { | |
3111 | n._countedByPrototype = Prototype.emptyFunction; | |
3112 | results.push(Element.extend(n)); | |
3113 | } | |
3114 | return Selector.handlers.unmark(results); | |
3115 | }, | |
3116 | ||
3117 | // COMBINATOR FUNCTIONS | |
3118 | descendant: function(nodes) { | |
3119 | var h = Selector.handlers; | |
3120 | for (var i = 0, results = [], node; node = nodes[i]; i++) | |
3121 | h.concat(results, node.getElementsByTagName('*')); | |
3122 | return results; | |
3123 | }, | |
3124 | ||
3125 | child: function(nodes) { | |
3126 | var h = Selector.handlers; | |
3127 | for (var i = 0, results = [], node; node = nodes[i]; i++) { | |
3128 | for (var j = 0, child; child = node.childNodes[j]; j++) | |
3129 | if (child.nodeType == 1 && child.tagName != '!') results.push(child); | |
3130 | } | |
3131 | return results; | |
3132 | }, | |
3133 | ||
3134 | adjacent: function(nodes) { | |
3135 | for (var i = 0, results = [], node; node = nodes[i]; i++) { | |
3136 | var next = this.nextElementSibling(node); | |
3137 | if (next) results.push(next); | |
3138 | } | |
3139 | return results; | |
3140 | }, | |
3141 | ||
3142 | laterSibling: function(nodes) { | |
3143 | var h = Selector.handlers; | |
3144 | for (var i = 0, results = [], node; node = nodes[i]; i++) | |
3145 | h.concat(results, Element.nextSiblings(node)); | |
3146 | return results; | |
3147 | }, | |
3148 | ||
3149 | nextElementSibling: function(node) { | |
3150 | while (node = node.nextSibling) | |
3151 | if (node.nodeType == 1) return node; | |
3152 | return null; | |
3153 | }, | |
3154 | ||
3155 | previousElementSibling: function(node) { | |
3156 | while (node = node.previousSibling) | |
3157 | if (node.nodeType == 1) return node; | |
3158 | return null; | |
3159 | }, | |
3160 | ||
3161 | // TOKEN FUNCTIONS | |
3162 | tagName: function(nodes, root, tagName, combinator) { | |
3163 | var uTagName = tagName.toUpperCase(); | |
3164 | var results = [], h = Selector.handlers; | |
3165 | if (nodes) { | |
3166 | if (combinator) { | |
3167 | // fastlane for ordinary descendant combinators | |
3168 | if (combinator == "descendant") { | |
3169 | for (var i = 0, node; node = nodes[i]; i++) | |
3170 | h.concat(results, node.getElementsByTagName(tagName)); | |
3171 | return results; | |
3172 | } else nodes = this[combinator](nodes); | |
3173 | if (tagName == "*") return nodes; | |
3174 | } | |
3175 | for (var i = 0, node; node = nodes[i]; i++) | |
3176 | if (node.tagName.toUpperCase() === uTagName) results.push(node); | |
3177 | return results; | |
3178 | } else return root.getElementsByTagName(tagName); | |
3179 | }, | |
3180 | ||
3181 | id: function(nodes, root, id, combinator) { | |
3182 | var targetNode = $(id), h = Selector.handlers; | |
3183 | if (!targetNode) return []; | |
3184 | if (!nodes && root == document) return [targetNode]; | |
3185 | if (nodes) { | |
3186 | if (combinator) { | |
3187 | if (combinator == 'child') { | |
3188 | for (var i = 0, node; node = nodes[i]; i++) | |
3189 | if (targetNode.parentNode == node) return [targetNode]; | |
3190 | } else if (combinator == 'descendant') { | |
3191 | for (var i = 0, node; node = nodes[i]; i++) | |
3192 | if (Element.descendantOf(targetNode, node)) return [targetNode]; | |
3193 | } else if (combinator == 'adjacent') { | |
3194 | for (var i = 0, node; node = nodes[i]; i++) | |
3195 | if (Selector.handlers.previousElementSibling(targetNode) == node) | |
3196 | return [targetNode]; | |
3197 | } else nodes = h[combinator](nodes); | |
3198 | } | |
3199 | for (var i = 0, node; node = nodes[i]; i++) | |
3200 | if (node == targetNode) return [targetNode]; | |
3201 | return []; | |
3202 | } | |
3203 | return (targetNode && Element.descendantOf(targetNode, root)) ? [targetNode] : []; | |
3204 | }, | |
3205 | ||
3206 | className: function(nodes, root, className, combinator) { | |
3207 | if (nodes && combinator) nodes = this[combinator](nodes); | |
3208 | return Selector.handlers.byClassName(nodes, root, className); | |
3209 | }, | |
3210 | ||
3211 | byClassName: function(nodes, root, className) { | |
3212 | if (!nodes) nodes = Selector.handlers.descendant([root]); | |
3213 | var needle = ' ' + className + ' '; | |
3214 | for (var i = 0, results = [], node, nodeClassName; node = nodes[i]; i++) { | |
3215 | nodeClassName = node.className; | |
3216 | if (nodeClassName.length == 0) continue; | |
3217 | if (nodeClassName == className || (' ' + nodeClassName + ' ').include(needle)) | |
3218 | results.push(node); | |
3219 | } | |
3220 | return results; | |
3221 | }, | |
3222 | ||
3223 | attrPresence: function(nodes, root, attr, combinator) { | |
3224 | if (!nodes) nodes = root.getElementsByTagName("*"); | |
3225 | if (nodes && combinator) nodes = this[combinator](nodes); | |
3226 | var results = []; | |
3227 | for (var i = 0, node; node = nodes[i]; i++) | |
3228 | if (Element.hasAttribute(node, attr)) results.push(node); | |
3229 | return results; | |
3230 | }, | |
3231 | ||
3232 | attr: function(nodes, root, attr, value, operator, combinator) { | |
3233 | if (!nodes) nodes = root.getElementsByTagName("*"); | |
3234 | if (nodes && combinator) nodes = this[combinator](nodes); | |
3235 | var handler = Selector.operators[operator], results = []; | |
3236 | for (var i = 0, node; node = nodes[i]; i++) { | |
3237 | var nodeValue = Element.readAttribute(node, attr); | |
3238 | if (nodeValue === null) continue; | |
3239 | if (handler(nodeValue, value)) results.push(node); | |
3240 | } | |
3241 | return results; | |
3242 | }, | |
3243 | ||
3244 | pseudo: function(nodes, name, value, root, combinator) { | |
3245 | if (nodes && combinator) nodes = this[combinator](nodes); | |
3246 | if (!nodes) nodes = root.getElementsByTagName("*"); | |
3247 | return Selector.pseudos[name](nodes, value, root); | |
} | ||
3249 | }, | |
1418 | | |
3251 | pseudos: { | |
3252 | 'first-child': function(nodes, value, root) { | |
3253 | for (var i = 0, results = [], node; node = nodes[i]; i++) { | |
3254 | if (Selector.handlers.previousElementSibling(node)) continue; | |
3255 | results.push(node); | |
3256 | } | |
3257 | return results; | |
3258 | }, | |
3259 | 'last-child': function(nodes, value, root) { | |
3260 | for (var i = 0, results = [], node; node = nodes[i]; i++) { | |
3261 | if (Selector.handlers.nextElementSibling(node)) continue; | |
3262 | results.push(node); | |
3263 | } | |
3264 | return results; | |
3265 | }, | |
3266 | 'only-child': function(nodes, value, root) { | |
3267 | var h = Selector.handlers; | |
3268 | for (var i = 0, results = [], node; node = nodes[i]; i++) | |
3269 | if (!h.previousElementSibling(node) && !h.nextElementSibling(node)) | |
3270 | results.push(node); | |
3271 | return results; | |
3272 | }, | |
3273 | 'nth-child': function(nodes, formula, root) { | |
3274 | return Selector.pseudos.nth(nodes, formula, root); | |
3275 | }, | |
3276 | 'nth-last-child': function(nodes, formula, root) { | |
3277 | return Selector.pseudos.nth(nodes, formula, root, true); | |
3278 | }, | |
3279 | 'nth-of-type': function(nodes, formula, root) { | |
3280 | return Selector.pseudos.nth(nodes, formula, root, false, true); | |
3281 | }, | |
3282 | 'nth-last-of-type': function(nodes, formula, root) { | |
3283 | return Selector.pseudos.nth(nodes, formula, root, true, true); | |
3284 | }, | |
3285 | 'first-of-type': function(nodes, formula, root) { | |
3286 | return Selector.pseudos.nth(nodes, "1", root, false, true); | |
3287 | }, | |
3288 | 'last-of-type': function(nodes, formula, root) { | |
3289 | return Selector.pseudos.nth(nodes, "1", root, true, true); | |
3290 | }, | |
3291 | 'only-of-type': function(nodes, formula, root) { | |
3292 | var p = Selector.pseudos; | |
3293 | return p['last-of-type'](p['first-of-type'](nodes, formula, root), formula, root); | |
3294 | }, | |
3295 | ||
3296 | // handles the an+b logic | |
3297 | getIndices: function(a, b, total) { | |
3298 | if (a == 0) return b > 0 ? [b] : []; | |
3299 | return $R(1, total).inject([], function(memo, i) { | |
3300 | if (0 == (i - b) % a && (i - b) / a >= 0) memo.push(i); | |
3301 | return memo; | |
3302 | }); | |
3303 | }, | |
3304 | ||
3305 | // handles nth(-last)-child, nth(-last)-of-type, and (first|last)-of-type | |
3306 | nth: function(nodes, formula, root, reverse, ofType) { | |
3307 | if (nodes.length == 0) return []; | |
3308 | if (formula == 'even') formula = '2n+0'; | |
3309 | if (formula == 'odd') formula = '2n+1'; | |
3310 | var h = Selector.handlers, results = [], indexed = [], m; | |
3311 | h.mark(nodes); | |
3312 | for (var i = 0, node; node = nodes[i]; i++) { | |
3313 | if (!node.parentNode._countedByPrototype) { | |
3314 | h.index(node.parentNode, reverse, ofType); | |
3315 | indexed.push(node.parentNode); | |
3316 | } | |
3317 | } | |
3318 | if (formula.match(/^\d+$/)) { // just a number | |
3319 | formula = Number(formula); | |
3320 | for (var i = 0, node; node = nodes[i]; i++) | |
3321 | if (node.nodeIndex == formula) results.push(node); | |
3322 | } else if (m = formula.match(/^(-?\d*)?n(([+-])(\d+))?/)) { // an+b | |
3323 | if (m[1] == "-") m[1] = -1; | |
3324 | var a = m[1] ? Number(m[1]) : 1; | |
3325 | var b = m[2] ? Number(m[2]) : 0; | |
3326 | var indices = Selector.pseudos.getIndices(a, b, nodes.length); | |
3327 | for (var i = 0, node, l = indices.length; node = nodes[i]; i++) { | |
3328 | for (var j = 0; j < l; j++) | |
3329 | if (node.nodeIndex == indices[j]) results.push(node); | |
3330 | } | |
3331 | } | |
3332 | h.unmark(nodes); | |
3333 | h.unmark(indexed); | |
3334 | return results; | |
3335 | }, | |
3336 | ||
3337 | 'empty': function(nodes, value, root) { | |
3338 | for (var i = 0, results = [], node; node = nodes[i]; i++) { | |
3339 | // IE treats comments as element nodes | |
3340 | if (node.tagName == '!' || node.firstChild) continue; | |
3341 | results.push(node); | |
3342 | } | |
3343 | return results; | |
3344 | }, | |
3345 | ||
3346 | 'not': function(nodes, selector, root) { | |
3347 | var h = Selector.handlers, selectorType, m; | |
3348 | var exclusions = new Selector(selector).findElements(root); | |
3349 | h.mark(exclusions); | |
3350 | for (var i = 0, results = [], node; node = nodes[i]; i++) | |
3351 | if (!node._countedByPrototype) results.push(node); | |
3352 | h.unmark(exclusions); | |
3353 | return results; | |
3354 | }, | |
3355 | ||
3356 | 'enabled': function(nodes, value, root) { | |
3357 | for (var i = 0, results = [], node; node = nodes[i]; i++) | |
3358 | if (!node.disabled && (!node.type || node.type !== 'hidden')) | |
3359 | results.push(node); | |
3360 | return results; | |
3361 | }, | |
3362 | ||
3363 | 'disabled': function(nodes, value, root) { | |
3364 | for (var i = 0, results = [], node; node = nodes[i]; i++) | |
3365 | if (node.disabled) results.push(node); | |
3366 | return results; | |
3367 | }, | |
3368 | ||
3369 | 'checked': function(nodes, value, root) { | |
3370 | for (var i = 0, results = [], node; node = nodes[i]; i++) | |
3371 | if (node.checked) results.push(node); | |
3372 | return results; | |
3373 | } | |
}, | ||
1421 | | |
1422 | | |
1423 | | |
3376 | operators: { | |
3377 | '=': function(nv, v) { return nv == v; }, | |
3378 | '!=': function(nv, v) { return nv != v; }, | |
3379 | '^=': function(nv, v) { return nv == v || nv && nv.startsWith(v); }, | |
3380 | '$=': function(nv, v) { return nv == v || nv && nv.endsWith(v); }, | |
3381 | '*=': function(nv, v) { return nv == v || nv && nv.include(v); }, | |
3382 | '$=': function(nv, v) { return nv.endsWith(v); }, | |
3383 | '*=': function(nv, v) { return nv.include(v); }, | |
3384 | '~=': function(nv, v) { return (' ' + nv + ' ').include(' ' + v + ' '); }, | |
3385 | '|=': function(nv, v) { return ('-' + (nv || "").toUpperCase() + | |
3386 | '-').include('-' + (v || "").toUpperCase() + '-'); } | |
3387 | }, | |
1425 | | |
1426 | | |
1427 | | |
1428 | | |
3389 | split: function(expression) { | |
3390 | var expressions = []; | |
3391 | expression.scan(/(([\w#:.~>+()\s-]+|\*|\[.*?\])+)\s*(,|$)/, function(m) { | |
3392 | expressions.push(m[1].strip()); | |
3393 | }); | |
3394 | return expressions; | |
3395 | }, | |
3396 | ||
3397 | matchElements: function(elements, expression) { | |
3398 | var matches = $$(expression), h = Selector.handlers; | |
3399 | h.mark(matches); | |
3400 | for (var i = 0, results = [], element; element = elements[i]; i++) | |
3401 | if (element._countedByPrototype) results.push(element); | |
3402 | h.unmark(matches); | |
3403 | return results; | |
3404 | }, | |
3405 | ||
3406 | findElement: function(elements, expression, index) { | |
3407 | if (Object.isNumber(expression)) { | |
3408 | index = expression; expression = false; | |
} | ||
1430 | | |
3410 | return Selector.matchElements(elements, expression || '*')[index || 0]; | |
}, | ||
3413 | findChildElements: function(element, expressions) { | |
3414 | expressions = Selector.split(expressions.join(',')); | |
3415 | var results = [], h = Selector.handlers; | |
3416 | for (var i = 0, l = expressions.length, selector; i < l; i++) { | |
3417 | selector = new Selector(expressions[i].strip()); | |
3418 | h.concat(results, selector.findElements(element)); | |
3419 | } | |
3420 | return (l > 1) ? h.unique(results) : results; | |
3421 | } | |
3422 | }); | |
3423 | ||
3424 | if (Prototype.Browser.IE) { | |
3425 | Object.extend(Selector.handlers, { | |
3426 | // IE returns comment nodes on getElementsByTagName("*"). | |
3427 | // Filter them out. | |
3428 | concat: function(a, b) { | |
3429 | for (var i = 0, node; node = b[i]; i++) | |
3430 | if (node.tagName !== "!") a.push(node); | |
3431 | return a; | |
3432 | }, | |
3433 | ||
3434 | // IE improperly serializes _countedByPrototype in (inner|outer)HTML. | |
3435 | unmark: function(nodes) { | |
3436 | for (var i = 0, node; node = nodes[i]; i++) | |
3437 | node.removeAttribute('_countedByPrototype'); | |
3438 | return nodes; | |
3439 | } | |
3440 | }); | |
3441 | } | |
3442 | ||
3443 | function $$() { | |
3444 | return Selector.findChildElements(document, $A(arguments)); | |
3445 | } | |
3446 | var Form = { | |
3447 | reset: function(form) { | |
3448 | $(form).reset(); | |
3449 | return form; | |
3450 | }, | |
3451 | ||
3452 | serializeElements: function(elements, options) { | |
3453 | if (typeof options != 'object') options = { hash: !!options }; | |
3454 | else if (Object.isUndefined(options.hash)) options.hash = true; | |
3455 | var key, value, submitted = false, submit = options.submit; | |
3456 | ||
3457 | var data = elements.inject({ }, function(result, element) { | |
3458 | if (!element.disabled && element.name) { | |
3459 | key = element.name; value = $(element).getValue(); | |
3460 | if (value != null && element.type != 'file' && (element.type != 'submit' || (!submitted && | |
3461 | submit !== false && (!submit || key == submit) && (submitted = true)))) { | |
3462 | if (key in result) { | |
3463 | // a key is already present; construct an array of values | |
3464 | if (!Object.isArray(result[key])) result[key] = [result[key]]; | |
3465 | result[key].push(value); | |
3466 | } | |
3467 | else result[key] = value; | |
3468 | } | |
3469 | } | |
3470 | return result; | |
3471 | }); | |
3472 | ||
3473 | return options.hash ? data : Object.toQueryString(data); | |
3474 | } | |
3475 | }; | |
3476 | ||
3477 | Form.Methods = { | |
3478 | serialize: function(form, options) { | |
3479 | return Form.serializeElements(Form.getElements(form), options); | |
3480 | }, | |
3481 | ||
3482 | getElements: function(form) { | |
3483 | return $A($(form).getElementsByTagName('*')).inject([], | |
3484 | function(elements, child) { | |
3485 | if (Form.Element.Serializers[child.tagName.toLowerCase()]) | |
3486 | elements.push(Element.extend(child)); | |
3487 | return elements; | |
3488 | } | |
3489 | ); | |
3490 | }, | |
3491 | ||
getInputs: function(form, typeName, name) { | ||
form = $(form); | ||
var inputs = form.getElementsByTagName('input'); | ||
1437 | | |
1438 | | |
3496 | if (!typeName && !name) return $A(inputs).map(Element.extend); | |
1440 | | |
1441 | | |
3498 | for (var i = 0, matchingInputs = [], length = inputs.length; i < length; i++) { | |
var input = inputs[i]; | ||
1443 | | |
1444 | | |
3500 | if ((typeName && input.type != typeName) || (name && input.name != name)) | |
continue; | ||
1446 | | |
3502 | matchingInputs.push(Element.extend(input)); | |
} | ||
return matchingInputs; | ||
}, | ||
disable: function(form) { | ||
1453 | | |
1454 | | |
1455 | | |
1456 | | |
1457 | | |
1458 | | |
3509 | form = $(form); | |
3510 | Form.getElements(form).invoke('disable'); | |
3511 | return form; | |
}, | ||
enable: function(form) { | ||
1462 | | |
1463 | | |
1464 | | |
1465 | | |
1466 | | |
3515 | form = $(form); | |
3516 | Form.getElements(form).invoke('enable'); | |
3517 | return form; | |
}, | ||
findFirstElement: function(form) { | ||
1470 | | |
1471 | | |
1472 | | |
3521 | var elements = $(form).getElements().findAll(function(element) { | |
3522 | return 'hidden' != element.type && !element.disabled; | |
}); | ||
3524 | var firstByIndex = elements.findAll(function(element) { | |
3525 | return element.hasAttribute('tabIndex') && element.tabIndex >= 0; | |
3526 | }).sortBy(function(element) { return element.tabIndex }).first(); | |
3527 | ||
3528 | return firstByIndex ? firstByIndex : elements.find(function(element) { | |
3529 | return ['input', 'select', 'textarea'].include(element.tagName.toLowerCase()); | |
3530 | }); | |
}, | ||
focusFirstElement: function(form) { | ||
1477 | | |
3534 | form = $(form); | |
3535 | form.findFirstElement().activate(); | |
3536 | return form; | |
}, | ||
1480 | | |
1481 | | |
3539 | request: function(form, options) { | |
3540 | form = $(form), options = Object.clone(options || { }); | |
3541 | ||
3542 | var params = options.parameters, action = form.readAttribute('action') || ''; | |
3543 | if (action.blank()) action = window.location.href; | |
3544 | options.parameters = form.serialize(true); | |
3545 | ||
3546 | if (params) { | |
3547 | if (Object.isString(params)) params = params.toQueryParams(); | |
3548 | Object.extend(options.parameters, params); | |
3549 | } | |
3550 | ||
3551 | if (form.hasAttribute('method') && !options.method) | |
3552 | options.method = form.method; | |
3553 | ||
3554 | return new Ajax.Request(action, options); | |
} | ||
1483 | ||
3556 | }; | |
3558 | /*--------------------------------------------------------------------------*/ | |
3559 | ||
Form.Element = { | ||
3561 | focus: function(element) { | |
3562 | $(element).focus(); | |
3563 | return element; | |
3564 | }, | |
3565 | ||
3566 | select: function(element) { | |
3567 | $(element).select(); | |
3568 | return element; | |
3569 | } | |
3570 | }; | |
3571 | ||
3572 | Form.Element.Methods = { | |
serialize: function(element) { | ||
element = $(element); | ||
3575 | if (!element.disabled && element.name) { | |
3576 | var value = element.getValue(); | |
3577 | if (value != undefined) { | |
3578 | var pair = { }; | |
3579 | pair[element.name] = value; | |
3580 | return Object.toQueryString(pair); | |
3581 | } | |
3582 | } | |
3583 | return ''; | |
3584 | }, | |
3585 | ||
3586 | getValue: function(element) { | |
3587 | element = $(element); | |
var method = element.tagName.toLowerCase(); | ||
1489 | | |
3589 | return Form.Element.Serializers[method](element); | |
3590 | }, | |
1491 | | |
1492 | | |
1493 | | |
3592 | setValue: function(element, value) { | |
3593 | element = $(element); | |
3594 | var method = element.tagName.toLowerCase(); | |
3595 | Form.Element.Serializers[method](element, value); | |
3596 | return element; | |
3597 | }, | |
1495 | | |
1496 | | |
3599 | clear: function(element) { | |
3600 | $(element).value = ''; | |
3601 | return element; | |
3602 | }, | |
1498 | | |
1499 | | |
1500 | | |
1501 | | |
3604 | present: function(element) { | |
3605 | return $(element).value != ''; | |
}, | ||
1504 | | |
3608 | activate: function(element) { | |
element = $(element); | ||
1506 | | |
1507 | | |
3610 | try { | |
3611 | element.focus(); | |
3612 | if (element.select && (element.tagName.toLowerCase() != 'input' || | |
3613 | !['button', 'reset', 'submit'].include(element.type))) | |
3614 | element.select(); | |
3615 | } catch (e) { } | |
3616 | return element; | |
3617 | }, | |
1509 | | |
1510 | | |
3619 | disable: function(element) { | |
3620 | element = $(element); | |
3621 | element.disabled = true; | |
3622 | return element; | |
3623 | }, | |
3624 | ||
3625 | enable: function(element) { | |
3626 | element = $(element); | |
3627 | element.disabled = false; | |
3628 | return element; | |
} | ||
1512 | ||
3630 | }; | |
3632 | /*--------------------------------------------------------------------------*/ | |
3633 | ||
3634 | var Field = Form.Element; | |
3635 | var $F = Form.Element.Methods.getValue; | |
3636 | ||
3637 | /*--------------------------------------------------------------------------*/ | |
3638 | ||
Form.Element.Serializers = { | ||
1515 | | |
3640 | input: function(element, value) { | |
switch (element.type.toLowerCase()) { | ||
1517 | | |
1518 | | |
1519 | | |
1520 | | |
1521 | | |
case 'checkbox': | ||
case 'radio': | ||
1524 | | |
3644 | return Form.Element.Serializers.inputSelector(element, value); | |
3645 | default: | |
3646 | return Form.Element.Serializers.textarea(element, value); | |
} | ||
1526 | | |
}, | ||
1529 | | |
1530 | | |
1531 | | |
3650 | inputSelector: function(element, value) { | |
3651 | if (Object.isUndefined(value)) return element.checked ? element.value : null; | |
3652 | else element.checked = !!value; | |
}, | ||
1534 | | |
1535 | | |
3655 | textarea: function(element, value) { | |
3656 | if (Object.isUndefined(value)) return element.value; | |
3657 | else element.value = value; | |
}, | ||
1538 | | |
1539 | | |
1540 | | |
3660 | select: function(element, value) { | |
3661 | if (Object.isUndefined(value)) | |
3662 | return this[element.type == 'select-one' ? | |
3663 | 'selectOne' : 'selectMany'](element); | |
3664 | else { | |
3665 | var opt, currentValue, single = !Object.isArray(value); | |
3666 | for (var i = 0, length = element.length; i < length; i++) { | |
3667 | opt = element.options[i]; | |
3668 | currentValue = this.optionValue(opt); | |
3669 | if (single) { | |
3670 | if (currentValue == value) { | |
3671 | opt.selected = true; | |
3672 | return; | |
3673 | } | |
3674 | } | |
3675 | else opt.selected = value.include(currentValue); | |
3676 | } | |
3677 | } | |
}, | ||
selectOne: function(element) { | ||
1544 | | |
1545 | | |
1546 | | |
1547 | | |
1548 | | |
1549 | | |
3681 | var index = element.selectedIndex; | |
3682 | return index >= 0 ? this.optionValue(element.options[index]) : null; | |
}, | ||
selectMany: function(element) { | ||
1553 | | |
1554 | | |
3686 | var values, length = element.length; | |
3687 | if (!length) return null; | |
3688 | ||
3689 | for (var i = 0, values = []; i < length; i++) { | |
var opt = element.options[i]; | ||
1556 | | |
1557 | | |
3691 | if (opt.selected) values.push(this.optionValue(opt)); | |
} | ||
1559 | | |
3693 | return values; | |
3694 | }, | |
3695 | ||
3696 | optionValue: function(opt) { | |
3697 | // extend element because hasAttribute may not be native | |
3698 | return Element.extend(opt).hasAttribute('value') ? opt.value : opt.text; | |
} | ||
1561 | ||
3700 | }; | |
/*--------------------------------------------------------------------------*/ | ||
1565 | ||
1566 | ||
1567 | ||
1568 | ||
1569 | ||
1570 | ||
1571 | | |
1572 | | |
3704 | Abstract.TimedObserver = Class.create(PeriodicalExecuter, { | |
3705 | initialize: function($super, element, frequency, callback) { | |
3706 | $super(callback, frequency); | |
this.element = $(element); | ||
1574 | | |
1575 | ||
this.lastValue = this.getValue(); | ||
1577 | | |
}, | ||
1580 | | |
1581 | | |
1582 | | |
1583 | ||
1584 | | |
3711 | execute: function() { | |
var value = this.getValue(); | ||
1586 | | |
3713 | if (Object.isString(this.lastValue) && Object.isString(value) ? | |
3714 | this.lastValue != value : String(this.lastValue) != String(value)) { | |
this.callback(this.element, value); | ||
this.lastValue = value; | ||
} | ||
} | ||
1591 | ||
3719 | }); | |
1593 | ||
1594 | ||
3721 | Form.Element.Observer = Class.create(Abstract.TimedObserver, { | |
getValue: function() { | ||
return Form.Element.getValue(this.element); | ||
} | ||
}); | ||
1600 | ||
1601 | ||
3727 | Form.Observer = Class.create(Abstract.TimedObserver, { | |
getValue: function() { | ||
return Form.serialize(this.element); | ||
} | ||
... | ... | |
/*--------------------------------------------------------------------------*/ | ||
1609 | ||
1610 | ||
3735 | Abstract.EventObserver = Class.create({ | |
initialize: function(element, callback) { | ||
this.element = $(element); | ||
this.callback = callback; | ||
... | ... | |
}, | ||
registerFormCallbacks: function() { | ||
1631 | | |
1632 | | |
1633 | | |
3756 | Form.getElements(this.element).each(this.registerCallback, this); | |
}, | ||
registerCallback: function(element) { | ||
... | ... | |
case 'radio': | ||
Event.observe(element, 'click', this.onElementEvent.bind(this)); | ||
break; | ||
1643 | | |
1644 | | |
1645 | | |
1646 | | |
1647 | | |
3766 | default: | |
Event.observe(element, 'change', this.onElementEvent.bind(this)); | ||
break; | ||
} | ||
} | ||
} | ||
1653 | ||
3772 | }); | |
1655 | ||
1656 | ||
3774 | Form.Element.EventObserver = Class.create(Abstract.EventObserver, { | |
getValue: function() { | ||
return Form.Element.getValue(this.element); | ||
} | ||
}); | ||
1662 | ||
1663 | ||
3780 | Form.EventObserver = Class.create(Abstract.EventObserver, { | |
getValue: function() { | ||
return Form.serialize(this.element); | ||
} | ||
}); | ||
1668 | ||
1669 | | |
1670 | ||
3785 | if (!window.Event) var Event = { }; | |
Object.extend(Event, { | ||
KEY_BACKSPACE: 8, | ||
... | ... | |
KEY_RIGHT: 39, | ||
KEY_DOWN: 40, | ||
KEY_DELETE: 46, | ||
3797 | KEY_HOME: 36, | |
3798 | KEY_END: 35, | |
3799 | KEY_PAGEUP: 33, | |
3800 | KEY_PAGEDOWN: 34, | |
3801 | KEY_INSERT: 45, | |
1683 | | |
1684 | | |
1685 | | |
3803 | cache: { }, | |
1687 | | |
1688 | | |
1689 | | |
1690 | | |
3805 | relatedTarget: function(event) { | |
3806 | var element; | |
3807 | switch(event.type) { | |
3808 | case 'mouseover': element = event.fromElement; break; | |
3809 | case 'mouseout': element = event.toElement; break; | |
3810 | default: return null; | |
3811 | } | |
3812 | return Element.extend(element); | |
3813 | } | |
3814 | }); | |
1692 | | |
1693 | | |
1694 | | |
1695 | | |
3816 | Event.Methods = (function() { | |
3817 | var isButton; | |
1697 | | |
1698 | | |
1699 | | |
1700 | | |
3819 | if (Prototype.Browser.IE) { | |
3820 | var buttonMap = { 0: 1, 1: 4, 2: 2 }; | |
3821 | isButton = function(event, code) { | |
3822 | return event.button == buttonMap[code]; | |
3823 | }; | |
1702 | | |
1703 | | |
3825 | } else if (Prototype.Browser.WebKit) { | |
3826 | isButton = function(event, code) { | |
3827 | switch (code) { | |
3828 | case 0: return event.which == 1 && !event.metaKey; | |
3829 | case 1: return event.which == 1 && event.metaKey; | |
3830 | default: return false; | |
3831 | } | |
3832 | }; | |
3833 | ||
3834 | } else { | |
3835 | isButton = function(event, code) { | |
3836 | return event.which ? (event.which === code + 1) : (event.button === code); | |
3837 | }; | |
3838 | } | |
3839 | ||
3840 | return { | |
3841 | isLeftClick: function(event) { return isButton(event, 0) }, | |
3842 | isMiddleClick: function(event) { return isButton(event, 1) }, | |
3843 | isRightClick: function(event) { return isButton(event, 2) }, | |
3844 | ||
3845 | element: function(event) { | |
3846 | event = Event.extend(event); | |
3847 | ||
3848 | var node = event.target, | |
3849 | type = event.type, | |
3850 | currentTarget = event.currentTarget; | |
3851 | ||
3852 | if (currentTarget && currentTarget.tagName) { | |
3853 | // Firefox screws up the "click" event when moving between radio buttons | |
3854 | // via arrow keys. It also screws up the "load" and "error" events on images, | |
3855 | // reporting the document as the target instead of the original image. | |
3856 | if (type === 'load' || type === 'error' || | |
3857 | (type === 'click' && currentTarget.tagName.toLowerCase() === 'input' | |
3858 | && currentTarget.type === 'radio')) | |
3859 | node = currentTarget; | |
3860 | } | |
3861 | if (node.nodeType == Node.TEXT_NODE) node = node.parentNode; | |
3862 | return Element.extend(node); | |
3863 | }, | |
3864 | ||
3865 | findElement: function(event, expression) { | |
3866 | var element = Event.element(event); | |
3867 | if (!expression) return element; | |
3868 | var elements = [element].concat(element.ancestors()); | |
3869 | return Selector.findElement(elements, expression, 0); | |
3870 | }, | |
3871 | ||
3872 | pointer: function(event) { | |
3873 | var docElement = document.documentElement, | |
3874 | body = document.body || { scrollLeft: 0, scrollTop: 0 }; | |
3875 | return { | |
3876 | x: event.pageX || (event.clientX + | |
3877 | (docElement.scrollLeft || body.scrollLeft) - | |
3878 | (docElement.clientLeft || 0)), | |
3879 | y: event.pageY || (event.clientY + | |
3880 | (docElement.scrollTop || body.scrollTop) - | |
3881 | (docElement.clientTop || 0)) | |
3882 | }; | |
3883 | }, | |
3884 | ||
3885 | pointerX: function(event) { return Event.pointer(event).x }, | |
3886 | pointerY: function(event) { return Event.pointer(event).y }, | |
3887 | ||
3888 | stop: function(event) { | |
3889 | Event.extend(event); | |
event.preventDefault(); | ||
event.stopPropagation(); | ||
1706 | | |
1707 | | |
1708 | | |
3892 | event.stopped = true; | |
} | ||
1710 | | |
3894 | }; | |
3895 | })(); | |
1712 | | |
1713 | | |
1714 | | |
1715 | | |
1716 | | |
1717 | | |
1718 | | |
1719 | | |
1720 | | |
3897 | Event.extend = (function() { | |
3898 | var methods = Object.keys(Event.Methods).inject({ }, function(m, name) { | |
3899 | m[name] = Event.Methods[name].methodize(); | |
3900 | return m; | |
3901 | }); | |
1722 | | |
3903 | if (Prototype.Browser.IE) { | |
3904 | Object.extend(methods, { | |
3905 | stopPropagation: function() { this.cancelBubble = true }, | |
3906 | preventDefault: function() { this.returnValue = false }, | |
3907 | inspect: function() { return "[object Event]" } | |
3908 | }); | |
1724 | | |
1725 | | |
1726 | | |
1727 | | |
1728 | | |
1729 | | |
1730 | | |
1731 | | |
3910 | return function(event) { | |
3911 | if (!event) return false; | |
3912 | if (event._extendedByPrototype) return event; | |
3913 | ||
3914 | event._extendedByPrototype = Prototype.emptyFunction; | |
3915 | var pointer = Event.pointer(event); | |
3916 | Object.extend(event, { | |
3917 | target: event.srcElement, | |
3918 | relatedTarget: Event.relatedTarget(event), | |
3919 | pageX: pointer.x, | |
3920 | pageY: pointer.y | |
3921 | }); | |
3922 | return Object.extend(event, methods); | |
3923 | }; | |
3924 | ||
3925 | } else { | |
3926 | Event.prototype = Event.prototype || document.createEvent("HTMLEvents")['__proto__']; | |
3927 | Object.extend(Event.prototype, methods); | |
3928 | return Prototype.K; | |
3929 | } | |
3930 | })(); | |
3931 | ||
3932 | Object.extend(Event, (function() { | |
3933 | var cache = Event.cache; | |
3934 | ||
3935 | function getEventID(element) { | |
3936 | if (element._prototypeEventID) return element._prototypeEventID[0]; | |
3937 | arguments.callee.id = arguments.callee.id || 1; | |
3938 | return element._prototypeEventID = [++arguments.callee.id]; | |
3939 | } | |
3940 | ||
3941 | function getDOMEventName(eventName) { | |
3942 | if (eventName && eventName.include(':')) return "dataavailable"; | |
3943 | return eventName; | |
3944 | } | |
3945 | ||
3946 | function getCacheForID(id) { | |
3947 | return cache[id] = cache[id] || { }; | |
3948 | } | |
3949 | ||
3950 | function getWrappersForEventName(id, eventName) { | |
3951 | var c = getCacheForID(id); | |
3952 | return c[eventName] = c[eventName] || []; | |
3953 | } | |
3954 | ||
3955 | function createWrapper(element, eventName, handler) { | |
3956 | var id = getEventID(element); | |
3957 | var c = getWrappersForEventName(id, eventName); | |
3958 | if (c.pluck("handler").include(handler)) return false; | |
3959 | ||
3960 | var wrapper = function(event) { | |
3961 | if (!Event || !Event.extend || | |
3962 | (event.eventName && event.eventName != eventName)) | |
3963 | return false; | |
3964 | ||
3965 | Event.extend(event); | |
3966 | handler.call(element, event); | |
3967 | }; | |
3968 | ||
3969 | wrapper.handler = handler; | |
3970 | c.push(wrapper); | |
3971 | return wrapper; | |
3972 | } | |
3973 | ||
3974 | function findWrapper(id, eventName, handler) { | |
3975 | var c = getWrappersForEventName(id, eventName); | |
3976 | return c.find(function(wrapper) { return wrapper.handler == handler }); | |
3977 | } | |
3978 | ||
3979 | function destroyWrapper(id, eventName, handler) { | |
3980 | var c = getCacheForID(id); | |
3981 | if (!c[eventName]) return false; | |
3982 | c[eventName] = c[eventName].without(findWrapper(id, eventName, handler)); | |
3983 | } | |
3984 | ||
3985 | function destroyCache() { | |
3986 | for (var id in cache) | |
3987 | for (var eventName in cache[id]) | |
3988 | cache[id][eventName] = null; | |
3989 | } | |
3990 | ||
3991 | ||
3992 | // Internet Explorer needs to remove event handlers on page unload | |
3993 | // in order to avoid memory leaks. | |
3994 | if (window.attachEvent) { | |
3995 | window.attachEvent("onunload", destroyCache); | |
3996 | } | |
3997 | ||
3998 | // Safari has a dummy event handler on page unload so that it won't | |
3999 | // use its bfcache. Safari <= 3.1 has an issue with restoring the "document" | |
4000 | // object when page is returned to via the back button using its bfcache. | |
4001 | if (Prototype.Browser.WebKit) { | |
4002 | window.addEventListener('unload', Prototype.emptyFunction, false); | |
4003 | } | |
4004 | ||
4005 | return { | |
4006 | observe: function(element, eventName, handler) { | |
4007 | element = $(element); | |
4008 | var name = getDOMEventName(eventName); | |
4009 | ||
4010 | var wrapper = createWrapper(element, eventName, handler); | |
4011 | if (!wrapper) return element; | |
4012 | ||
4013 | if (element.addEventListener) { | |
4014 | element.addEventListener(name, wrapper, false); | |
4015 | } else { | |
4016 | element.attachEvent("on" + name, wrapper); | |
4017 | } | |
4018 | ||
4019 | return element; | |
4020 | }, | |
4021 | ||
4022 | stopObserving: function(element, eventName, handler) { | |
4023 | element = $(element); | |
4024 | var id = getEventID(element), name = getDOMEventName(eventName); | |
4025 | ||
4026 | if (!handler && eventName) { | |
4027 | getWrappersForEventName(id, eventName).each(function(wrapper) { | |
4028 | element.stopObserving(eventName, wrapper.handler); | |
4029 | }); | |
4030 | return element; | |
4031 | ||
4032 | } else if (!eventName) { | |
4033 | Object.keys(getCacheForID(id)).each(function(eventName) { | |
4034 | element.stopObserving(eventName); | |
4035 | }); | |
4036 | return element; | |
4037 | } | |
4038 | ||
4039 | var wrapper = findWrapper(id, eventName, handler); | |
4040 | if (!wrapper) return element; | |
4041 | ||
4042 | if (element.removeEventListener) { | |
4043 | element.removeEventListener(name, wrapper, false); | |
4044 | } else { | |
4045 | element.detachEvent("on" + name, wrapper); | |
4046 | } | |
4047 | ||
4048 | destroyWrapper(id, eventName, handler); | |
4049 | ||
4050 | return element; | |
4051 | }, | |
4052 | ||
4053 | fire: function(element, eventName, memo) { | |
4054 | element = $(element); | |
4055 | if (element == document && document.createEvent && !element.dispatchEvent) | |
4056 | element = document.documentElement; | |
4057 | ||
4058 | var event; | |
4059 | if (document.createEvent) { | |
4060 | event = document.createEvent("HTMLEvents"); | |
4061 | event.initEvent("dataavailable", true, true); | |
4062 | } else { | |
4063 | event = document.createEventObject(); | |
4064 | event.eventType = "ondataavailable"; | |
4065 | } | |
4066 | ||
4067 | event.eventName = eventName; | |
4068 | event.memo = memo || { }; | |
4069 | ||
4070 | if (document.createEvent) { | |
4071 | element.dispatchEvent(event); | |
4072 | } else { | |
4073 | element.fireEvent(event.eventType, event); | |
4074 | } | |
4075 | ||
4076 | return Event.extend(event); | |
} | ||
1733 | | |
4078 | }; | |
4079 | })()); | |
1735 | | |
1736 | | |
1737 | | |
1738 | | |
1739 | | |
4081 | Object.extend(Event, Event.Methods); | |
4082 | ||
4083 | Element.addMethods({ | |
4084 | fire: Event.fire, | |
4085 | observe: Event.observe, | |
4086 | stopObserving: Event.stopObserving | |
4087 | }); | |
4088 | ||
4089 | Object.extend(document, { | |
4090 | fire: Element.Methods.fire.methodize(), | |
4091 | observe: Element.Methods.observe.methodize(), | |
4092 | stopObserving: Element.Methods.stopObserving.methodize(), | |
4093 | loaded: false | |
4094 | }); | |
4095 | ||
4096 | (function() { | |
4097 | /* Support for the DOMContentLoaded event is based on work by Dan Webb, | |
4098 | Matthias Miller, Dean Edwards and John Resig. */ | |
4099 | ||
4100 | var timer; | |
4101 | ||
4102 | function fireContentLoadedEvent() { | |
4103 | if (document.loaded) return; | |
4104 | if (timer) window.clearInterval(timer); | |
4105 | document.fire("dom:loaded"); | |
4106 | document.loaded = true; | |
4107 | } | |
4108 | ||
4109 | if (document.addEventListener) { | |
4110 | if (Prototype.Browser.WebKit) { | |
4111 | timer = window.setInterval(function() { | |
4112 | if (/loaded|complete/.test(document.readyState)) | |
4113 | fireContentLoadedEvent(); | |
4114 | }, 0); | |
4115 | ||
4116 | Event.observe(window, "load", fireContentLoadedEvent); | |
4117 | ||
4118 | } else { | |
4119 | document.addEventListener("DOMContentLoaded", | |
4120 | fireContentLoadedEvent, false); | |
} | ||
1741 | | |
1742 | | |
1744 | | |
1745 | | |
1746 | | |
4123 | } else { | |
4124 | document.write("<script id=__onDOMContentLoaded defer src=//:><\/script>"); | |
4125 | $("__onDOMContentLoaded").onreadystatechange = function() { | |
4126 | if (this.readyState == "complete") { | |
4127 | this.onreadystatechange = null; | |
4128 | fireContentLoadedEvent(); | |
4129 | } | |
4130 | }; | |
4131 | } | |
4132 | })(); | |
4133 | /*------------------------------- DEPRECATED -------------------------------*/ | |
1748 | | |
1749 | | |
1750 | | |
1751 | | |
4135 | Hash.toQueryString = Object.toQueryString; | |
1753 | | |
4137 | var Toggle = { display: Element.toggle }; | |
4138 | ||
4139 | Element.Methods.childOf = Element.Methods.descendantOf; | |
4140 | ||
4141 | var Insertion = { | |
4142 | Before: function(element, content) { | |
4143 | return Element.insert(element, {before:content}); | |
}, | ||
1756 | | |
1757 | | |
1758 | | |
4146 | Top: function(element, content) { | |
4147 | return Element.insert(element, {top:content}); | |
4148 | }, | |
1760 | | |
1761 | | |
1762 | | |
1763 | | |
4150 | Bottom: function(element, content) { | |
4151 | return Element.insert(element, {bottom:content}); | |
4152 | }, | |
1765 | | |
1766 | | |
1767 | | |
1768 | | |
1769 | | |
4154 | After: function(element, content) { | |
4155 | return Element.insert(element, {after:content}); | |
} | ||
1771 | ||
4157 | }; | |
1773 | ||
1774 | ||
1775 | | |
4159 | var $continue = new Error('"throw $continue" is deprecated, use "return" instead'); | |
4160 | ||
4161 | // This should be moved to script.aculo.us; notice the deprecated methods | |
4162 | // further below, that map to the newer Element methods. | |
var Position = { | ||
// set to true if needed, warning: firefox performance problems | ||
// NOT neeeded for page scrolling, only if draggable contained in | ||
... | ... | |
|| 0; | ||
}, | ||
1795 | | |
1796 | | |
1797 | | |
1798 | | |
1799 | | |
1800 | | |
1801 | | |
1802 | | |
1803 | | |
1804 | ||
1805 | | |
1806 | | |
1807 | | |
1808 | | |
1809 | | |
1810 | | |
1811 | | |
1812 | | |
1813 | | |
1814 | ||
1815 | | |
1816 | | |
1817 | | |
1818 | | |
1819 | | |
1820 | | |
1821 | | |
1822 | | |
1823 | | |
1824 | | |
1825 | | |
1826 | | |
1827 | | |
1828 | ||
1829 | | |
1830 | | |
1831 | | |
1832 | ||
1833 | | |
1834 | | |
1835 | | |
1836 | ||
1837 | | |
1838 | | |
1839 | ||
// caches x/y coordinate pair to use with overlap | ||
within: function(element, x, y) { | ||
if (this.includeScrollOffsets) | ||
return this.withinIncludingScrolloffsets(element, x, y); | ||
this.xcomp = x; | ||
this.ycomp = y; | ||
1846 | | |
4188 | this.offset = Element.cumulativeOffset(element); | |
return (y >= this.offset[1] && | ||
y < this.offset[1] + element.offsetHeight && | ||
... | ... | |
}, | ||
withinIncludingScrolloffsets: function(element, x, y) { | ||
1855 | | |
4197 | var offsetcache = Element.cumulativeScrollOffset(element); | |
this.xcomp = x + offsetcache[0] - this.deltaX; | ||
this.ycomp = y + offsetcache[1] - this.deltaY; | ||
1859 | | |
4201 | this.offset = Element.cumulativeOffset(element); | |
return (this.ycomp >= this.offset[1] && | ||
this.ycomp < this.offset[1] + element.offsetHeight && | ||
... | ... | |
element.offsetWidth; | ||
}, | ||
1878 | | |
1879 | | |
1880 | | |
1881 | | |
1882 | | |
1883 | | |
1884 | | |
1885 | | |
1886 | | |
4220 | // Deprecation layer -- use newer Element methods now (1.5.2). | |
4221 | ||
4222 | cumulativeOffset: Element.Methods.cumulativeOffset, | |
4223 | ||
4224 | positionedOffset: Element.Methods.positionedOffset, | |
4225 | ||
4226 | absolutize: function(element) { | |
4227 | Position.prepare(); | |
4228 | return Element.absolutize(element); | |
}, | ||
1889 | | |
1890 | | |
4231 | relativize: function(element) { | |
4232 | Position.prepare(); | |
4233 | return Element.relativize(element); | |
4234 | }, | |
1892 | | |
1893 | | |
1894 | | |
1895 | | |
4236 | realOffset: Element.Methods.cumulativeScrollOffset, | |
1897 | | |
1898 | | |
1899 | | |
4238 | offsetParent: Element.Methods.getOffsetParent, | |
1901 | | |
4240 | page: Element.Methods.viewportOffset, | |
1903 | | |
1904 | | |
1905 | | |
1906 | | |
1907 | | |
4242 | clone: function(source, target, options) { | |
4243 | options = options || { }; | |
4244 | return Element.clonePosition(target, source, options); | |
4245 | } | |
4246 | }; | |
1909 | | |
1910 | | |
4248 | /*--------------------------------------------------------------------------*/ | |
1912 | | |
1913 | | |
1914 | | |
1915 | | |
1916 | | |
1917 | | |
1918 | | |
1919 | | |
1920 | | |
4250 | if (!document.getElementsByClassName) document.getElementsByClassName = function(instanceMethods){ | |
4251 | function iter(name) { | |
4252 | return name.blank() ? null : "[contains(concat(' ', @class, ' '), ' " + name + " ')]"; | |
4253 | } | |
1922 | | |
1923 | | |
1924 | | |
4255 | instanceMethods.getElementsByClassName = Prototype.BrowserFeatures.XPath ? | |
4256 | function(element, className) { | |
4257 | className = className.toString().strip(); | |
4258 | var cond = /\s/.test(className) ? $w(className).map(iter).join('') : iter(className); | |
4259 | return cond ? document._getElementsByXPath('.//*' + cond, element) : []; | |
4260 | } : function(element, className) { | |
4261 | className = className.toString().strip(); | |
4262 | var elements = [], classNames = (/\s/.test(className) ? $w(className) : null); | |
4263 | if (!classNames && !className) return elements; | |
1926 | | |
1927 | | |
1928 | | |
1929 | | |
1930 | | |
1931 | | |
1932 | | |
1933 | | |
1934 | | |
1935 | | |
4265 | var nodes = $(element).getElementsByTagName('*'); | |
4266 | className = ' ' + className + ' '; | |
1937 | | |
1938 | | |
1939 | | |
1940 | | |
4268 | for (var i = 0, child, cn; child = nodes[i]; i++) { | |
4269 | if (child.className && (cn = ' ' + child.className + ' ') && (cn.include(className) || | |
4270 | (classNames && classNames.all(function(name) { | |
4271 | return !name.toString().blank() && cn.include(' ' + name + ' '); | |
4272 | })))) | |
4273 | elements.push(Element.extend(child)); | |
} | ||
4275 | return elements; | |
4276 | }; | |
1943 | | |
1944 | | |
1945 | | |
1946 | | |
1947 | | |
1948 | | |
4278 | return function(className, parentElement) { | |
4279 | return $(parentElement || document.body).getElementsByClassName(className); | |
4280 | }; | |
4281 | }(Element.Methods); | |
1950 | | |
1951 | | |
1952 | | |
1953 | | |
4283 | /*--------------------------------------------------------------------------*/ | |
1955 | | |
1956 | | |
1957 | | |
1958 | | |
1959 | | |
4285 | Element.ClassNames = Class.create(); | |
4286 | Element.ClassNames.prototype = { | |
4287 | initialize: function(element) { | |
4288 | this.element = $(element); | |
4289 | }, | |
1961 | | |
1962 | | |
1963 | | |
1964 | | |
4291 | _each: function(iterator) { | |
4292 | this.element.className.split(/\s+/).select(function(name) { | |
4293 | return name.length > 0; | |
4294 | })._each(iterator); | |
4295 | }, | |
1966 | | |
1967 | | |
1968 | | |
1969 | | |
1970 | | |
4297 | set: function(className) { | |
4298 | this.element.className = className; | |
}, | ||
1973 | | |
1974 | | |
1975 | | |
1976 | | |
4301 | add: function(classNameToAdd) { | |
4302 | if (this.include(classNameToAdd)) return; | |
4303 | this.set($A(this).concat(classNameToAdd).join(' ')); | |
4304 | }, | |
1978 | | |
1979 | | |
1980 | | |
4306 | remove: function(classNameToRemove) { | |
4307 | if (!this.include(classNameToRemove)) return; | |
4308 | this.set($A(this).without(classNameToRemove).join(' ')); | |
4309 | }, | |
1982 | | |
1983 | | |
1984 | | |
1985 | | |
4311 | toString: function() { | |
4312 | return $A(this).join(' '); | |
} | ||
1987 | ||
4314 | }; | |
1989 | ||
1990 | ||
1991 | ||
1992 | ||
1993 | | |
1994 | | |
1995 | | |
1996 | | |
1997 | | |
1998 | | |
1999 | | |
4316 | Object.extend(Element.ClassNames.prototype, Enumerable); | |
2001 | | |
2002 | | |
4318 | /*--------------------------------------------------------------------------*/ | |
2004 | | |
2005 | | |
2006 | ||
4321 | Element.addMethods(); | |