From fe7edbba5dff398b7eb357fc59f81c429b36b68d Mon Sep 17 00:00:00 2001 From: Seth Call Date: Wed, 22 Oct 2014 23:10:49 -0500 Subject: [PATCH] * many recording features/improvements: VRFS-2151, VRFS-1551, VRFS-2360, VRFS-2290, VRFS-2002, VRFS-2181 --- .../views/admin/batch_emails/_form.html.erb | 2 +- db/manifest | 1 + db/up/user_syncs_and_quick_mix.sql | 68 + ruby/lib/jam_ruby.rb | 4 + ruby/lib/jam_ruby/lib/s3_manager.rb | 12 + ruby/lib/jam_ruby/models/mix.rb | 46 +- ruby/lib/jam_ruby/models/quick_mix.rb | 269 + .../lib/jam_ruby/models/quick_mix_observer.rb | 83 + ruby/lib/jam_ruby/models/recorded_track.rb | 23 +- ruby/lib/jam_ruby/models/recording.rb | 150 +- ruby/lib/jam_ruby/models/search.rb | 1 + ruby/lib/jam_ruby/models/user.rb | 1 + ruby/lib/jam_ruby/models/user_sync.rb | 46 + ruby/lib/jam_ruby/resque/audiomixer.rb | 12 +- ruby/lib/jam_ruby/resque/quick_mixer.rb | 148 + .../resque/scheduled/audiomixer_retry.rb | 4 +- ruby/spec/factories.rb | 24 + ruby/spec/jam_ruby/models/quick_mix_spec.rb | 212 + .../jam_ruby/models/recorded_track_spec.rb | 1 + ruby/spec/jam_ruby/models/recording_spec.rb | 273 +- ruby/spec/jam_ruby/models/user_sync_spec.rb | 226 + ruby/spec/jam_ruby/resque/audiomixer_spec.rb | 47 +- ruby/spec/jam_ruby/resque/quick_mixer_spec.rb | 121 + ruby/spec/spec_helper.rb | 1 + web/app/assets/javascripts/application.js | 1 + .../javascripts/dialog/allSyncsDialog.js | 48 + .../dialog/recordingFinishedDialog.js | 5 + .../javascripts/dialog/whatsNextDialog.js | 64 - web/app/assets/javascripts/fakeJamClient.js | 8 + web/app/assets/javascripts/feedHelper.js | 10 + .../assets/javascripts/feed_item_recording.js | 8 + web/app/assets/javascripts/globals.js | 6 +- web/app/assets/javascripts/jam_rest.js | 38 + web/app/assets/javascripts/jquery.exists.js | 1 + web/app/assets/javascripts/landing/landing.js | 1 + .../assets/javascripts/notificationPanel.js | 1 + web/app/assets/javascripts/paginator.js | 18 +- .../assets/javascripts/recordingManager.js | 277 +- .../javascripts/recording_utils.js.coffee | 141 + web/app/assets/javascripts/session.js | 12 +- .../assets/javascripts/sync_viewer.js.coffee | 892 ++ web/app/assets/javascripts/utils.js | 61 +- web/app/assets/javascripts/web/web.js | 2 + web/app/assets/stylesheets/client/client.css | 1 + .../assets/stylesheets/client/common.css.scss | 167 + .../assets/stylesheets/client/help.css.scss | 108 +- .../client/recordingManager.css.scss | 30 +- .../stylesheets/client/screen_common.css.scss | 10 + .../stylesheets/client/session.css.scss | 29 +- .../dialogs/gettingStartDialog.css.scss | 2 +- .../dialogs/syncViewerDialog.css.scss | 3 + .../stylesheets/users/syncViewer.css.scss | 281 + .../stylesheets/web/audioWidgets.css.scss | 15 +- .../stylesheets/web/recordings.css.scss | 5 +- .../api_claimed_recordings_controller.rb | 19 +- .../controllers/api_recordings_controller.rb | 73 +- .../controllers/api_user_syncs_controller.rb | 41 + web/app/controllers/recordings_controller.rb | 6 +- web/app/helpers/feeds_helper.rb | 1 - web/app/helpers/recording_helper.rb | 7 + .../views/api_claimed_recordings/show.rabl | 16 +- web/app/views/api_feeds/show.rabl | 22 +- web/app/views/api_recordings/show.rabl | 40 +- .../api_recordings/show_recorded_track.rabl | 7 + web/app/views/api_user_syncs/index.rabl | 15 + web/app/views/api_user_syncs/show.rabl | 103 + web/app/views/api_users/show_minimal.rabl | 3 + web/app/views/clients/_help.html.erb | 22 + .../views/clients/_help_launcher.html.slim | 2 + web/app/views/clients/_play_controls.html.erb | 2 +- .../views/clients/_recordingManager.html.erb | 3 + .../clients/_sync_viewer_templates.html.slim | 206 + web/app/views/clients/index.html.erb | 8 +- .../views/dialogs/_allSyncsDialog.html.slim | 14 + web/app/views/dialogs/_dialogs.html.haml | 2 +- web/app/views/users/_feed_recording.html.haml | 10 +- .../users/_feed_recording_ajax.html.haml | 7 +- web/app/views/users/_latest.html.haml | 2 +- web/config/application.rb | 4 +- web/config/routes.rb | 10 + .../api_claimed_recordings_spec.rb | 91 +- .../api_recordings_controller_spec.rb | 2 +- .../api_user_syncs_controller_spec.rb | 163 + web/spec/factories.rb | 42 +- web/spec/features/alt_landing_spec.rb | 8 +- .../javascripts/fixtures/syncViewer.html.slim | 2 + .../fixtures/user_sync_track1.json | 174 + .../javascripts/helpers/jasmine-jquery.js | 700 -- web/spec/javascripts/spec_helper.js | 8 +- web/spec/javascripts/support/jasmine.yml | 76 - .../javascripts/sync_viewer_spec.js.coffee | 228 + web/spec/javascripts/vendor/jquery.js | 9405 ----------------- 92 files changed, 4995 insertions(+), 10569 deletions(-) create mode 100644 db/up/user_syncs_and_quick_mix.sql create mode 100644 ruby/lib/jam_ruby/models/quick_mix.rb create mode 100644 ruby/lib/jam_ruby/models/quick_mix_observer.rb create mode 100644 ruby/lib/jam_ruby/models/user_sync.rb create mode 100644 ruby/lib/jam_ruby/resque/quick_mixer.rb create mode 100644 ruby/spec/jam_ruby/models/quick_mix_spec.rb create mode 100644 ruby/spec/jam_ruby/models/user_sync_spec.rb create mode 100644 ruby/spec/jam_ruby/resque/quick_mixer_spec.rb create mode 100644 web/app/assets/javascripts/dialog/allSyncsDialog.js delete mode 100644 web/app/assets/javascripts/dialog/whatsNextDialog.js create mode 100644 web/app/assets/javascripts/jquery.exists.js create mode 100644 web/app/assets/javascripts/recording_utils.js.coffee create mode 100644 web/app/assets/javascripts/sync_viewer.js.coffee create mode 100644 web/app/assets/stylesheets/dialogs/syncViewerDialog.css.scss create mode 100644 web/app/assets/stylesheets/users/syncViewer.css.scss create mode 100644 web/app/controllers/api_user_syncs_controller.rb create mode 100644 web/app/views/api_recordings/show_recorded_track.rabl create mode 100644 web/app/views/api_user_syncs/index.rabl create mode 100644 web/app/views/api_user_syncs/show.rabl create mode 100644 web/app/views/api_users/show_minimal.rabl create mode 100644 web/app/views/clients/_help_launcher.html.slim create mode 100644 web/app/views/clients/_sync_viewer_templates.html.slim create mode 100644 web/app/views/dialogs/_allSyncsDialog.html.slim create mode 100644 web/spec/controllers/api_user_syncs_controller_spec.rb create mode 100644 web/spec/javascripts/fixtures/syncViewer.html.slim create mode 100644 web/spec/javascripts/fixtures/user_sync_track1.json delete mode 100644 web/spec/javascripts/helpers/jasmine-jquery.js create mode 100644 web/spec/javascripts/sync_viewer_spec.js.coffee diff --git a/admin/app/views/admin/batch_emails/_form.html.erb b/admin/app/views/admin/batch_emails/_form.html.erb index 18fd10b82..75f4e0dd9 100644 --- a/admin/app/views/admin/batch_emails/_form.html.erb +++ b/admin/app/views/admin/batch_emails/_form.html.erb @@ -1,6 +1,6 @@ <%= semantic_form_for([:admin, resource], :url => resource.new_record? ? admin_batch_emails_path : "#{Gon.global.prefix}/admin/batch_emails/#{resource.id}") do |f| %> <%= f.inputs do %> - <%= f.input(:from_email, :label => "From Email", :input_html => {:maxlength => 64}) %> + <%= f.input(:from_email, :label => "From Email", :input_html => {:maxlength => 64}, :hint => 'JamKazam ') %> <%= f.input(:subject, :label => "Subject", :input_html => {:maxlength => 128}) %> <%= f.input(:test_emails, :label => "Test Emails", :input_html => {:maxlength => 1024, :size => '3x3'}) %> <%= f.input(:body, :label => "Body", :input_html => {:maxlength => 60000, :size => '10x20'}) %> diff --git a/db/manifest b/db/manifest index 85518bffb..6342f4c83 100755 --- a/db/manifest +++ b/db/manifest @@ -223,3 +223,4 @@ recorded_videos.sql emails_from_update2.sql add_youtube_flag_to_claimed_recordings.sql add_session_create_type.sql +user_syncs_and_quick_mix.sql diff --git a/db/up/user_syncs_and_quick_mix.sql b/db/up/user_syncs_and_quick_mix.sql new file mode 100644 index 000000000..4b997310a --- /dev/null +++ b/db/up/user_syncs_and_quick_mix.sql @@ -0,0 +1,68 @@ +ALTER TABLE recordings ADD COLUMN has_stream_mix BOOLEAN DEFAULT FALSE NOT NULL; +ALTER TABLE recordings ADD COLUMN has_final_mix BOOLEAN DEFAULT FALSE NOT NULL; +UPDATE recordings SET has_final_mix = TRUE WHERE (SELECT count(mixes.id) FROM mixes WHERE recording_id = recordings.id) > 0; + +CREATE TABLE quick_mixes ( + id BIGINT PRIMARY KEY, + next_part_to_upload INTEGER NOT NULL DEFAULT 0, + fully_uploaded BOOLEAN NOT NULL DEFAULT FALSE, + upload_id VARCHAR(1024), + file_offset BIGINT DEFAULT 0, + is_part_uploading BOOLEAN NOT NULL DEFAULT FALSE, + upload_failures INTEGER DEFAULT 0, + part_failures INTEGER DEFAULT 0, + ogg_md5 VARCHAR(100), + ogg_length INTEGER, + ogg_url VARCHAR(1000), + mp3_md5 VARCHAR(100), + mp3_length INTEGER, + mp3_url VARCHAR(1000), + error_count INTEGER NOT NULL DEFAULT 0, + error_reason TEXT, + error_detail TEXT, + created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + started_at TIMESTAMP, + completed_at TIMESTAMP, + completed BOOLEAN NOT NULL DEFAULT FALSE, + should_retry BOOLEAN NOT NULL DEFAULT FALSE, + cleaned BOOLEAN NOT NULL DEFAULT FALSE, + user_id VARCHAR(64) REFERENCES users(id) ON DELETE SET NULL, + recording_id VARCHAR(64) REFERENCES recordings(id) ON DELETE CASCADE +); + +ALTER TABLE quick_mixes ALTER COLUMN id SET DEFAULT nextval('tracks_next_tracker_seq'); + +CREATE VIEW user_syncs AS + SELECT b.id AS recorded_track_id, + CAST(NULL as BIGINT) AS mix_id, + CAST(NULL as BIGINT) as quick_mix_id, + b.id AS unified_id, + a.user_id AS user_id, + b.fully_uploaded, + recordings.created_at AS created_at, + recordings.id AS recording_id + FROM recorded_tracks a INNER JOIN recordings ON a.recording_id = recordings.id AND duration IS NOT NULL AND all_discarded = FALSE INNER JOIN recorded_tracks b ON a.recording_id = b.recording_id + UNION ALL + SELECT CAST(NULL as BIGINT) AS recorded_track_id, + mixes.id AS mix_id, + CAST(NULL as BIGINT) AS quick_mix_id, + mixes.id AS unified_id, + claimed_recordings.user_id AS user_id, + NULL as fully_uploaded, + recordings.created_at AS created_at, + recordings.id AS recording_id + FROM mixes INNER JOIN recordings ON mixes.recording_id = recordings.id INNER JOIN claimed_recordings ON recordings.id = claimed_recordings.recording_id WHERE claimed_recordings.discarded = FALSE + UNION ALL + SELECT CAST(NULL as BIGINT) AS recorded_track_id, + CAST(NULL as BIGINT) AS mix_id, + quick_mixes.id AS quick_mix_id, + quick_mixes.id AS unified_id, + quick_mixes.user_id, + quick_mixes.fully_uploaded, + recordings.created_at AS created_at, + recordings.id AS recording_id + FROM quick_mixes INNER JOIN recordings ON quick_mixes.recording_id = recordings.id AND duration IS NOT NULL AND all_discarded = FALSE; + +ALTER TABLE recordings ADD COLUMN first_quick_mix_id BIGINT REFERENCES quick_mixes(id) ON DELETE SET NULL; + diff --git a/ruby/lib/jam_ruby.rb b/ruby/lib/jam_ruby.rb index 394975a34..98b93a4c1 100755 --- a/ruby/lib/jam_ruby.rb +++ b/ruby/lib/jam_ruby.rb @@ -38,6 +38,7 @@ require "jam_ruby/lib/em_helper" require "jam_ruby/lib/nav" require "jam_ruby/lib/html_sanitize" require "jam_ruby/resque/audiomixer" +require "jam_ruby/resque/quick_mixer" require "jam_ruby/resque/icecast_config_writer" require "jam_ruby/resque/resque_hooks" require "jam_ruby/resque/scheduled/audiomixer_retry" @@ -122,6 +123,8 @@ require "jam_ruby/models/recording_comment" require "jam_ruby/models/recording_liker" require "jam_ruby/models/recorded_track" require "jam_ruby/models/recorded_track_observer" +require "jam_ruby/models/quick_mix" +require "jam_ruby/models/quick_mix_observer" require "jam_ruby/models/share_token" require "jam_ruby/models/mix" require "jam_ruby/models/claimed_recording" @@ -174,6 +177,7 @@ require "jam_ruby/models/chat_message" require "jam_ruby/models/generic_state" require "jam_ruby/models/score_history" require "jam_ruby/models/jam_company" +require "jam_ruby/models/user_sync" require "jam_ruby/models/video_source" require "jam_ruby/models/recorded_video" diff --git a/ruby/lib/jam_ruby/lib/s3_manager.rb b/ruby/lib/jam_ruby/lib/s3_manager.rb index 2ff30aae4..8e31532d1 100644 --- a/ruby/lib/jam_ruby/lib/s3_manager.rb +++ b/ruby/lib/jam_ruby/lib/s3_manager.rb @@ -64,6 +64,10 @@ module JamRuby s3_bucket.objects[upload_filename].multipart_uploads[upload_id].parts[part] end + def exists?(filename) + s3_bucket.objects[filename].exists? + end + def delete(filename) s3_bucket.objects[filename].delete end @@ -76,6 +80,14 @@ module JamRuby s3_bucket.objects.with_prefix(folder).delete_all end + def download(key, filename) + File.open(filename, "wb") do |f| + s3_bucket.objects[key].read do |data| + f.write(data) + end + end + end + private def s3_bucket diff --git a/ruby/lib/jam_ruby/models/mix.rb b/ruby/lib/jam_ruby/models/mix.rb index 0d2282b0c..957e46ba4 100644 --- a/ruby/lib/jam_ruby/models/mix.rb +++ b/ruby/lib/jam_ruby/models/mix.rb @@ -49,8 +49,8 @@ module JamRuby mix.is_skip_mount_uploader = true mix.recording = recording mix.save - mix[:ogg_url] = construct_filename(mix.created_at, recording.id, mix.id, type='ogg') - mix[:mp3_url] = construct_filename(mix.created_at, recording.id, mix.id, type='mp3') + mix[:ogg_url] = construct_filename(recording.created_at, recording.id, mix.id, type='ogg') + mix[:mp3_url] = construct_filename(recording.created_at, recording.id, mix.id, type='mp3') if mix.save mix.enqueue end @@ -68,19 +68,53 @@ module JamRuby end # avoid db validations - Mix.where(:id => self.id).update_all(:started_at => Time.now) + Mix.where(:id => self.id).update_all(:started_at => Time.now, :should_retry => false) true end def can_download?(some_user) - !ClaimedRecording.find_by_user_id_and_recording_id(some_user.id, recording_id).nil? + claimed_recording = ClaimedRecording.find_by_user_id_and_recording_id(some_user.id, recording.id) + + if claimed_recording + !claimed_recording.discarded + else + false + end + end + + def mix_timeout? + Time.now - started_at > 60 * 30 # 30 minutes to mix is more than enough + end + + def state + return 'mixed' if completed + return 'stream-mix' if recording.has_stream_mix + return 'waiting-to-mix' if started_at.nil? + return 'error' if error_count > 0 || mix_timeout? + return 'mixing' + end + + def error + return nil if state != 'error' + return {error_count: error_count, error_reason: error_reason, error_detail: error_detail} if error_count > 0 + return {error_count: 1, error_reason: 'mix-timeout', error_detail: started_at} if mix_timeout? + return {error_count: 1, error_reason: 'unknown', error_detail: 'unknown'} + end + + def too_many_downloads? + (self.download_count < 0 || self.download_count > APP_CONFIG.max_audio_downloads) && !@current_user.admin end def errored(reason, detail) + self.started_at = nil self.error_reason = reason self.error_detail = detail self.error_count = self.error_count + 1 + if self.error_count <= 3 + self.should_retry = true + end + save end @@ -94,6 +128,8 @@ module JamRuby if save Notification.send_recording_master_mix_complete(recording) end + + Recording.where(:id => self.recording.id).update_all(:has_final_mix => true) end # valid for 1 day; because the s3 urls eventually expire @@ -154,7 +190,7 @@ module JamRuby def filename(type='ogg') # construct a path for s3 - Mix.construct_filename(self.created_at, self.recording_id, self.id, type) + Mix.construct_filename(recording.created_at, self.recording_id, self.id, type) end def update_download_count(count=1) diff --git a/ruby/lib/jam_ruby/models/quick_mix.rb b/ruby/lib/jam_ruby/models/quick_mix.rb new file mode 100644 index 000000000..182bb7482 --- /dev/null +++ b/ruby/lib/jam_ruby/models/quick_mix.rb @@ -0,0 +1,269 @@ +module JamRuby + class QuickMix < ActiveRecord::Base + include S3ManagerMixin + + MAX_MIX_TIME = 7200 # 2 hours + + attr_accessible :ogg_url, :should_retry, as: :admin + attr_accessor :marking_complete, :is_skip_mount_uploader + attr_writer :current_user + + + + + belongs_to :user, :class_name => "JamRuby::User", :inverse_of => :quick_mixes + belongs_to :recording, :class_name => "JamRuby::Recording", :inverse_of => :quick_mixes, :foreign_key => 'recording_id' + validates :ogg_md5, :presence => true, :if => :upload_starting? + validates :ogg_length, length: {minimum: 1, maximum: 1024 * 1024 * 256 }, if: :upload_starting? # 256 megs max. is this reasonable? surely... + validates :user, presence: true + validate :validate_fully_uploaded + validate :validate_part_complete + validate :validate_too_many_upload_failures + + before_destroy :delete_s3_files + + skip_callback :save, :before, :store_picture!, if: :is_skip_mount_uploader + + def too_many_upload_failures? + upload_failures >= APP_CONFIG.max_track_upload_failures + end + + def upload_starting? + next_part_to_upload_was == 0 && next_part_to_upload == 1 + end + + def validate_too_many_upload_failures + if upload_failures >= APP_CONFIG.max_track_upload_failures + errors.add(:upload_failures, ValidationMessages::UPLOAD_FAILURES_EXCEEDED) + end + end + + def validate_fully_uploaded + if marking_complete && fully_uploaded && fully_uploaded_was + errors.add(:fully_uploaded, ValidationMessages::ALREADY_UPLOADED) + end + end + + def validate_part_complete + + # if we see a transition from is_part_uploading from true to false, we validate + if is_part_uploading_was && !is_part_uploading + if next_part_to_upload_was + 1 != next_part_to_upload + errors.add(:next_part_to_upload, ValidationMessages::INVALID_PART_NUMBER_SPECIFIED) + end + + if file_offset > ogg_length + errors.add(:file_offset, ValidationMessages::FILE_OFFSET_EXCEEDS_LENGTH) + end + elsif next_part_to_upload_was + 1 == next_part_to_upload + # this makes sure we are only catching 'upload_part_complete' transitions, and not upload_start + if next_part_to_upload_was != 0 + # we see that the part number was ticked--but was is_part_upload set to true before this transition? + if !is_part_uploading_was && !is_part_uploading + errors.add(:next_part_to_upload, ValidationMessages::PART_NOT_STARTED) + end + end + end + end + + def sanitize_active_admin + self.user_id = nil if self.user_id == '' + end + + def upload_start(length, md5) + #self.upload_id set by the observer + self.next_part_to_upload = 1 + self.ogg_length = length + self.ogg_md5 = md5 + save + end + + # if for some reason the server thinks the client can't carry on with the upload, + # this resets everything to the initial state + def reset_upload + self.upload_failures = self.upload_failures + 1 + self.part_failures = 0 + self.file_offset = 0 + self.next_part_to_upload = 0 + self.upload_id = nil + self.ogg_md5 = nil + self.ogg_length = 0 + self.fully_uploaded = false + self.is_part_uploading = false + save :validate => false # skip validation because we need this to always work + end + + def upload_next_part(length, md5) + self.marking_complete = true + if next_part_to_upload == 0 + upload_start(length, md5) + end + self.is_part_uploading = true + save + end + + def upload_sign(content_md5) + s3_manager.upload_sign(self[:ogg_url], content_md5, next_part_to_upload, upload_id) + end + + def upload_part_complete(part, offset) + # validated by :validate_part_complete + self.marking_complete = true + self.is_part_uploading = false + self.next_part_to_upload = self.next_part_to_upload + 1 + self.file_offset = offset.to_i + self.part_failures = 0 + save + end + + def upload_complete + # validate from happening twice by :validate_fully_uploaded + self.fully_uploaded = true + self.marking_complete = true + enqueue + save + end + + def increment_part_failures(part_failure_before_error) + self.part_failures = part_failure_before_error + 1 + QuickMix.update_all("part_failures = #{self.part_failures}", "id = '#{self.id}'") + end + + def self.create(recording, user) + raise if recording.nil? + + mix = QuickMix.new + mix.is_skip_mount_uploader = true + mix.recording = recording + mix.user = user + mix.save + mix[:ogg_url] = construct_filename(mix.created_at, recording.id, mix.id, type='ogg') + mix[:mp3_url] = construct_filename(mix.created_at, recording.id, mix.id, type='mp3') + mix.save + mix.is_skip_mount_uploader = false + + mix + end + + def enqueue + begin + Resque.enqueue(QuickMixer, self.id, self.sign_put(3600 * 24, 'mp3')) + rescue Exception => e + # implies redis is down. we don't update started_at by bailing out here + false + end + + # avoid db validations + QuickMix.where(:id => self.id).update_all(:started_at => Time.now, :should_retry => false) + + true + end + + def mix_timeout? + Time.now - started_at > 60 * 30 # 30 minutes to mix is more than enough + end + + def state + return 'mixed' if completed + return 'waiting-to-mix' if started_at.nil? + return 'error' if error_count > 0 || mix_timeout? + return 'mixing' + end + + def error + return nil if state != 'error' + return {error_count: error_count, error_reason: error_reason, error_detail: error_detail} if error_count > 0 + return {error_count: 1, error_reason: 'mix-timeout', error_detail: started_at} if mix_timeout? + return {error_count: 1, error_reason: 'unknown', error_detail: 'unknown'} + end + + def errored(reason, detail) + self.started_at = nil + self.error_reason = reason + self.error_detail = detail + self.error_count = self.error_count + 1 + if self.error_count <= 3 + self.should_retry = true + end + save + end + + def finish(mp3_length, mp3_md5) + self.completed_at = Time.now + self.mp3_length = mp3_length + self.mp3_md5 = mp3_md5 + self.completed = true + save! + Recording.where(:id => self.recording.id).update_all(:has_stream_mix => true) + # only update first_quick_mix_id pointer if this is the 1st quick mix to complete for this recording + Recording.where(:id => self.recording.id).update_all(:first_quick_mix_id => self.id) if recording.first_quick_mix_id.nil? + end + + def s3_url(type='ogg') + if type == 'ogg' + s3_manager.s3_url(self[:ogg_url]) + else + s3_manager.s3_url(self[:mp3_url]) + end + end + + def is_completed + completed + end + + # if the url starts with http, just return it because it's in some other store. Otherwise it's a relative path in s3 and needs be signed + def resolve_url(url_field, mime_type, expiration_time) + self[url_field].start_with?('http') ? self[url_field] : s3_manager.sign_url(self[url_field], {:expires => expiration_time, :response_content_type => mime_type, :secure => false}) + end + + def sign_url(expiration_time = 120, type='ogg') + type ||= 'ogg' + # expire link in 1 minute--the expectation is that a client is immediately following this link + if type == 'ogg' + resolve_url(:ogg_url, 'audio/ogg', expiration_time) + else + resolve_url(:mp3_url, 'audio/mpeg', expiration_time) + end + end + + def sign_put(expiration_time = 3600 * 24, type='ogg') + type ||= 'ogg' + if type == 'ogg' + s3_manager.sign_url(self[:ogg_url], {:expires => expiration_time, :content_type => 'audio/ogg', :secure => false}, :put) + else + s3_manager.sign_url(self[:mp3_url], {:expires => expiration_time, :content_type => 'audio/mpeg', :secure => false}, :put) + end + end + + def self.cleanup_excessive_storage + QuickMix + .joins(:recording) + .includes(:recording) + .where("cleaned = FALSE AND completed = TRUE AND NOW() - completed_at > '7 days'::INTERVAL AND (has_final_mix = TRUE OR (has_stream_mix = TRUE AND quick_mixes.id IN (SELECT qm.id FROM quick_mixes qm WHERE qm.recording_id = recordings.id AND (recordings.first_quick_mix_id IS NULL OR recordings.first_quick_mix_id != qm.id))))").limit(1000).each do |quick_mix| + + quick_mix.delete_s3_files + + QuickMix.where(:id => quick_mix.id).update_all(:cleaned => true) + + if quick_mix.recording.first_quick_mix_id == quick_mix.id + Recording.where(:id => quick_mix.recording.id).update_all(:has_stream_mix => false, :first_quick_mix_id => nil) + end + end + end + + def filename(type='ogg') + # construct a path for s3 + QuickMix.construct_filename(self.created_at, self.recording_id, self.id, type) + end + + def delete_s3_files + s3_manager.delete(filename(type='ogg')) if self[:ogg_url] && s3_manager.exists?(filename(type='ogg')) + s3_manager.delete(filename(type='mp3')) if self[:mp3_url] && s3_manager.exists?(filename(type='mp3')) + end + + def self.construct_filename(created_at, recording_id, id, type='ogg') + raise "unknown ID" unless id + "recordings/#{created_at.strftime('%m-%d-%Y')}/#{recording_id}/stream-mix-#{id}.#{type}" + end + end +end \ No newline at end of file diff --git a/ruby/lib/jam_ruby/models/quick_mix_observer.rb b/ruby/lib/jam_ruby/models/quick_mix_observer.rb new file mode 100644 index 000000000..ccc63891a --- /dev/null +++ b/ruby/lib/jam_ruby/models/quick_mix_observer.rb @@ -0,0 +1,83 @@ +module JamRuby + class QuickMixObserver < ActiveRecord::Observer + + # if you change the this class, tests really should accompany. having alot of logic in observers is really tricky, as we do here + observe JamRuby::QuickMix + + def before_validation(quick_mix) + + # if we see that a part was just uploaded entirely, validate that we can find the part that was just uploaded + if quick_mix.is_part_uploading_was && !quick_mix.is_part_uploading + begin + aws_part = quick_mix.s3_manager.multiple_upload_find_part(quick_mix[:ogg_url], quick_mix.upload_id, quick_mix.next_part_to_upload - 1) + # calling size on a part that does not exist will throw an exception... that's what we want + aws_part.size + rescue SocketError => e + raise # this should cause a 500 error, which is what we want. The client will retry later on 500. + rescue Exception => e + quick_mix.errors.add(:next_part_to_upload, ValidationMessages::PART_NOT_FOUND_IN_AWS) + rescue RuntimeError => e + quick_mix.errors.add(:next_part_to_upload, ValidationMessages::PART_NOT_FOUND_IN_AWS) + rescue + quick_mix.errors.add(:next_part_to_upload, ValidationMessages::PART_NOT_FOUND_IN_AWS) + end + + end + + # if we detect that this just became fully uploaded -- if so, tell s3 to put the parts together + if quick_mix.marking_complete && !quick_mix.fully_uploaded_was && quick_mix.fully_uploaded + + multipart_success = false + begin + quick_mix.s3_manager.multipart_upload_complete(quick_mix[:ogg_url], quick_mix.upload_id) + multipart_success = true + rescue SocketError => e + raise # this should cause a 500 error, which is what we want. The client will retry later. + rescue Exception => e + #quick_mix.reload + quick_mix.reset_upload + quick_mix.errors.add(:upload_id, ValidationMessages::BAD_UPLOAD) + end + end + end + + def after_commit(quick_mix) + + end + + # here we tick upload failure counts, or revert the state of the model, as needed + def after_rollback(quick_mix) + # if fully uploaded, don't increment failures + if quick_mix.fully_uploaded + return + end + + # increment part failures if there is a part currently being uploaded + if quick_mix.is_part_uploading_was + #quick_mix.reload # we don't want anything else that the user set to get applied + quick_mix.increment_part_failures(quick_mix.part_failures_was) + if quick_mix.part_failures >= APP_CONFIG.max_track_part_upload_failures + # save upload id before we abort this bad boy + upload_id = quick_mix.upload_id + begin + quick_mix.s3_manager.multipart_upload_abort(quick_mix[:ogg_url], upload_id) + rescue => e + puts e.inspect + end + quick_mix.reset_upload + if quick_mix.upload_failures >= APP_CONFIG.max_track_upload_failures + # do anything? + end + end + end + + end + + def before_save(quick_mix) + # if we are on the 1st part, then we need to make sure we can save the upload_id + if quick_mix.next_part_to_upload == 1 + quick_mix.upload_id = quick_mix.s3_manager.multipart_upload_start(quick_mix[:ogg_url]) + end + end + end +end \ No newline at end of file diff --git a/ruby/lib/jam_ruby/models/recorded_track.rb b/ruby/lib/jam_ruby/models/recorded_track.rb index 69b6d48f7..ce3299d8b 100644 --- a/ruby/lib/jam_ruby/models/recorded_track.rb +++ b/ruby/lib/jam_ruby/models/recorded_track.rb @@ -61,7 +61,21 @@ module JamRuby end def can_download?(some_user) - !ClaimedRecording.find_by_user_id_and_recording_id(some_user.id, recording.id).nil? + claimed_recording = recording.claimed_recordings.find{|claimed_recording| claimed_recording.user == some_user } + + if claimed_recording + !claimed_recording.discarded + else + false + end + end + + def too_many_upload_failures? + upload_failures >= APP_CONFIG.max_track_upload_failures + end + + def too_many_downloads? + (self.download_count < 0 || self.download_count > APP_CONFIG.max_audio_downloads) && !@current_user.admin end def upload_starting? @@ -126,7 +140,7 @@ module JamRuby recorded_track.file_offset = 0 recorded_track.is_skip_mount_uploader = true recorded_track.save - recorded_track.url = construct_filename(recorded_track.created_at, recording.id, track.client_track_id) + recorded_track.url = construct_filename(recording.created_at, recording.id, track.client_track_id) recorded_track.save recorded_track.is_skip_mount_uploader = false recorded_track @@ -152,7 +166,6 @@ module JamRuby self.file_offset = 0 self.next_part_to_upload = 0 self.upload_id = nil - self.file_offset self.md5 = nil self.length = 0 self.fully_uploaded = false @@ -161,6 +174,7 @@ module JamRuby end def upload_next_part(length, md5) + self.marking_complete = true if next_part_to_upload == 0 upload_start(length, md5) end @@ -174,6 +188,7 @@ module JamRuby def upload_part_complete(part, offset) # validated by :validate_part_complete + self.marking_complete = true self.is_part_uploading = false self.next_part_to_upload = self.next_part_to_upload + 1 self.file_offset = offset.to_i @@ -195,7 +210,7 @@ module JamRuby def filename # construct a path from s3 - RecordedTrack.construct_filename(self.created_at, self.recording.id, self.client_track_id) + RecordedTrack.construct_filename(recording.created_at, self.recording.id, self.client_track_id) end def update_download_count(count=1) diff --git a/ruby/lib/jam_ruby/models/recording.rb b/ruby/lib/jam_ruby/models/recording.rb index 1d9c6fd9c..8ebe32436 100644 --- a/ruby/lib/jam_ruby/models/recording.rb +++ b/ruby/lib/jam_ruby/models/recording.rb @@ -3,11 +3,16 @@ module JamRuby self.primary_key = 'id' + + @@log = Logging.logger[Recording] + + attr_accessible :owner, :owner_id, :band, :band_id, :recorded_tracks_attributes, :mixes_attributes, :claimed_recordings_attributes, :name, :description, :genre, :is_public, :duration, as: :admin has_many :users, :through => :recorded_tracks, :class_name => "JamRuby::User" has_many :claimed_recordings, :class_name => "JamRuby::ClaimedRecording", :inverse_of => :recording, :foreign_key => 'recording_id', :dependent => :destroy has_many :mixes, :class_name => "JamRuby::Mix", :inverse_of => :recording, :foreign_key => 'recording_id', :dependent => :destroy + has_many :quick_mixes, :class_name => "JamRuby::QuickMix", :foreign_key => :recording_id, :dependent => :destroy has_many :recorded_tracks, :class_name => "JamRuby::RecordedTrack", :foreign_key => :recording_id, :dependent => :destroy has_many :recorded_videos, :class_name => "JamRuby::RecordedVideo", :foreign_key => :recording_id, :dependent => :destroy has_many :comments, :class_name => "JamRuby::RecordingComment", :foreign_key => "recording_id", :dependent => :destroy @@ -44,10 +49,34 @@ module JamRuby self.comments.size end - def has_mix? - self.mixes.length > 0 && self.mixes.first.completed + def high_quality_mix? + has_final_mix end + def has_mix? + # this is used by the UI to know whether it can show a play button. We prefer a real mix, but a stream mix will do + return true if high_quality_mix? + has_stream_mix + end + + # this should be a has-one relationship. until this, this is easiest way to get from recording > mix + def mix + self.mixes[0] if self.mixes.length > 0 + end + + def mix_state + mix.state if mix + end + + def mix_error + mix.error if mix + end + + def stream_mix + quick_mixes.find{|quick_mix| quick_mix.completed && !quick_mix.cleaned } + end + + # this can probably be done more efficiently, but David needs this asap for a video def grouped_tracks tracks = [] @@ -148,7 +177,12 @@ module JamRuby if recording.save GoogleAnalyticsEvent.report_band_recording(recording.band) - + + # make quick mixes *before* the audio/video tracks, because this will give them precedence in list_uploads + music_session.users.uniq.each do |user| + QuickMix.create(recording, user) + end + music_session.connections.each do |connection| connection.tracks.each do |track| recording.recorded_tracks << RecordedTrack.create_from_track(track, recording) @@ -179,6 +213,7 @@ module JamRuby end + # Called when a user wants to "claim" a recording. To do this, the user must have been one of the tracks in the recording. def claim(user, name, description, genre, is_public, upload_to_youtube=false) upload_to_youtube = !!upload_to_youtube # Correct where nil is borking save @@ -337,37 +372,74 @@ module JamRuby Arel::Nodes::As.new('video', Arel.sql('item_type')) ]).reorder("") - # Glue them together: - union = track_arel.union(vid_arel) + # Select fields for quick mix. Note that it must include + # the same number of fields as the track or video in order for + # the union to work: + quick_mix_arel = QuickMix.select([ + :id, + :recording_id, + :user_id, + :ogg_url, + :fully_uploaded, + :upload_failures, + Arel::Nodes::As.new('', Arel.sql('quick_mix_track_id')), + Arel::Nodes::As.new('stream_mix', Arel.sql('item_type')) + ]).reorder("") - # Create a table alias from the union so we can get back to arel: - utable = RecordedTrack.arel_table.create_table_alias(union, :recorded_items) + # Glue them together: + union = track_arel.union(vid_arel) + + utable = Arel::Nodes::TableAlias.new(union, :recorded_items) arel = track_arel.from(utable) + arel = arel.except(:select) + + # Remove the implicit select created by .from. It + # contains an ambigious "id" field: + arel = arel.except(:select) arel = arel.select([ - :id, - :recording_id, - :user_id, - :url, - :fully_uploaded, - :upload_failures, - :client_track_id, - :item_type - ]) + "recorded_items.id", + :recording_id, + :user_id, + :url, + :fully_uploaded, + :upload_failures, + :client_track_id, + :item_type + ]) + + # And repeat: + union_all = arel.union(quick_mix_arel) + utable_all = Arel::Nodes::TableAlias.new(union_all, :recorded_items_all) + arel = arel.from(utable_all) + + arel = arel.except(:select) + arel = arel.select([ + "recorded_items_all.id", + :recording_id, + :user_id, + :url, + :fully_uploaded, + :upload_failures, + :client_track_id, + :item_type + ]) + # Further joining and criteria for the unioned object: - arel = arel.joins("INNER JOIN (SELECT id as rec_id, all_discarded, duration FROM recordings) recs ON rec_id=recorded_items.recording_id") \ - .where('recorded_items.user_id' => user.id) \ - .where('recorded_items.fully_uploaded = ?', false) \ - .where('recorded_items.id > ?', since) \ + + arel = arel.joins("INNER JOIN recordings ON recordings.id=recorded_items_all.recording_id") \ + .where('recorded_items_all.user_id' => user.id) \ + .where('recorded_items_all.fully_uploaded = ?', false) \ + .where('recorded_items_all.id > ?', since) \ .where("upload_failures <= #{APP_CONFIG.max_track_upload_failures}") \ .where("duration IS NOT NULL") \ .where('all_discarded = false') \ - .order('recorded_items.id') \ + .order('recorded_items_all.id') \ .limit(limit) # Load into array: arel.each do |recorded_item| - if(recorded_item.item_type=='video') + if recorded_item.item_type=='video' # A video: uploads << ({ :type => "recorded_video", @@ -375,7 +447,7 @@ module JamRuby :recording_id => recorded_item.recording_id, :next => recorded_item.id }) - else + elsif recorded_item.item_type == 'track' # A track: uploads << ({ :type => "recorded_track", @@ -383,7 +455,16 @@ module JamRuby :recording_id => recorded_item.recording_id, :next => recorded_item.id }) + elsif recorded_item.item_type == 'stream_mix' + uploads << ({ + :type => "stream_mix", + :recording_id => recorded_item.recording_id, + :next => recorded_item.id + }) + else + end + end next_value = uploads.length > 0 ? uploads[-1][:next].to_s : nil @@ -397,18 +478,22 @@ module JamRuby } end + def preconditions_for_mix? + recorded_tracks.each do |recorded_track| + return false unless recorded_track.fully_uploaded + end + true + end + # Check to see if all files have been uploaded. If so, kick off a mix. def upload_complete # Don't allow multiple mixes for now. - raise JamRuby::JamArgumentError unless self.mixes.length == 0 + return if self.mixes.length > 0 # FIXME: There's a possible race condition here. If two users complete # uploads at the same time, we'll schedule 2 mixes. - recorded_tracks.each do |recorded_track| - return unless recorded_track.fully_uploaded - end - self.mixes << Mix.schedule(self) + self.mixes << Mix.schedule(self) if preconditions_for_mix? save end @@ -423,7 +508,14 @@ module JamRuby claimed_recordings.first end - private + # returns a ClaimedRecording that the user did not discard + def claim_for_user(user) + return nil unless user + claim = claimed_recordings.find{|claimed_recording| claimed_recording.user == user } + claim unless claim && claim.discarded + end + + private def self.validate_user_is_band_member(user, band) unless band.users.exists? user raise PermissionError, ValidationMessages::USER_NOT_BAND_MEMBER_VALIDATION_ERROR diff --git a/ruby/lib/jam_ruby/models/search.rb b/ruby/lib/jam_ruby/models/search.rb index 51e2db56c..c83730172 100644 --- a/ruby/lib/jam_ruby/models/search.rb +++ b/ruby/lib/jam_ruby/models/search.rb @@ -276,6 +276,7 @@ module JamRuby rel, page = self.relation_pagination(rel, params) rel = rel.includes([:instruments, :followings, :friends]) + # XXX: DOES THIS MEAN ALL MATCHING USERS ARE RETURNED? objs = rel.all srch = Search.new diff --git a/ruby/lib/jam_ruby/models/user.rb b/ruby/lib/jam_ruby/models/user.rb index de0592801..5216063db 100644 --- a/ruby/lib/jam_ruby/models/user.rb +++ b/ruby/lib/jam_ruby/models/user.rb @@ -104,6 +104,7 @@ module JamRuby # saved tracks has_many :recorded_tracks, :foreign_key => "user_id", :class_name => "JamRuby::RecordedTrack", :inverse_of => :user has_many :recorded_videos, :foreign_key => "user_id", :class_name => "JamRuby::RecordedVideo", :inverse_of => :user + has_many :quick_mixes, :foreign_key => "user_id", :class_name => "JamRuby::QuickMix", :inverse_of => :user # invited users has_many :invited_users, :foreign_key => "sender_id", :class_name => "JamRuby::InvitedUser" diff --git a/ruby/lib/jam_ruby/models/user_sync.rb b/ruby/lib/jam_ruby/models/user_sync.rb new file mode 100644 index 000000000..17925bcdf --- /dev/null +++ b/ruby/lib/jam_ruby/models/user_sync.rb @@ -0,0 +1,46 @@ +module JamRuby + class UserSync < ActiveRecord::Base + + belongs_to :recorded_track + belongs_to :mix + belongs_to :quick_mix + + def self.show(id, user_id) + self.index({user_id: user_id, id: id, limit: 1, offset: 0})[:query].first + end + + def self.index(params) + user_id = params[:user_id] + limit = params[:limit].to_i + limit = limit == 0 ? 20 : limit + offset = params[:offset].to_i + id = params[:id] + recording_id = params[:recording_id] + + page = (offset / limit) + 1 # one-based madness + + raise 'no user id specified' if user_id.blank? + + query = UserSync.includes(recorded_track: [{recording: [:owner, {claimed_recordings: [:share_token]}, {recorded_tracks: [:user]}, {comments:[:user]}, :likes, :plays, :mixes]}, user: [], instrument:[]], mix: [], quick_mix:[]).where(user_id: user_id).paginate(:page => page, :per_page => limit).order('created_at DESC, unified_id') + + + # allow selection of single user_sync, by ID + unless id.blank? + query = query.where(unified_id: id) + end + + unless recording_id.blank? + query = query.where(recording_id: recording_id) + end + + if query.length == 0 + { query:query, next: nil} + elsif query.length < limit + { query:query, next: nil} + else + { query:query, next: offset + limit} + end + end + end +end + diff --git a/ruby/lib/jam_ruby/resque/audiomixer.rb b/ruby/lib/jam_ruby/resque/audiomixer.rb index 37fb2496e..1b9065d3d 100644 --- a/ruby/lib/jam_ruby/resque/audiomixer.rb +++ b/ruby/lib/jam_ruby/resque/audiomixer.rb @@ -30,7 +30,7 @@ module JamRuby end def self.queue_jobs_needing_retry - Mix.find_each(:conditions => 'should_retry = TRUE or started_at is NULL', :batch_size => 100) do |mix| + Mix.find_each(:conditions => "completed = FALSE AND (should_retry = TRUE OR (started_at IS NOT NULL AND NOW() - started_at > '1 hour'::INTERVAL))", :batch_size => 100) do |mix| mix.enqueue end end @@ -130,6 +130,8 @@ module JamRuby # update manifest so that audiomixer writes here @manifest[:output][:filename] = @output_ogg_filename + # this is not used by audiomixer today; since today the Ruby code handles this + @manifest[:output][:filename_mp3] = @output_mp3_filename @@log.debug("output ogg file: #{@output_ogg_filename}, output mp3 file: #{@output_mp3_filename}") end @@ -228,8 +230,8 @@ module JamRuby end mix.errored(error_reason, error_detail) - rescue - @@log.error "unable to post back to the database the error" + rescue Exception => e + @@log.error "unable to post back to the database the error #{e}" end end @@ -260,9 +262,9 @@ module JamRuby execute(@manifest_file) postback - + post_success(mix) - + @@log.info("audiomixer job successful. mix_id #{mix_id}") rescue Exception => e diff --git a/ruby/lib/jam_ruby/resque/quick_mixer.rb b/ruby/lib/jam_ruby/resque/quick_mixer.rb new file mode 100644 index 000000000..4f949c6d3 --- /dev/null +++ b/ruby/lib/jam_ruby/resque/quick_mixer.rb @@ -0,0 +1,148 @@ +require 'json' +require 'resque' +require 'resque-retry' +require 'net/http' +require 'digest/md5' + +module JamRuby + + # executes a mix of tracks, creating a final output mix + class QuickMixer + + @queue = :quick_mixer + + @@log = Logging.logger[QuickMixer] + + attr_accessor :quick_mix_id, :quick_mix, :output_filename, :error_out_filename, + :postback_mp3_url, :error_reason, :error_detail + + def self.perform(quick_mix_id, postback_mp3_url) + + JamWebEventMachine.run_wait_stop do + audiomixer = QuickMixer.new + audiomixer.postback_mp3_url = postback_mp3_url + audiomixer.quick_mix_id = quick_mix_id + audiomixer.run + end + + end + + def self.find_jobs_needing_retry &blk + QuickMix.find_each(:conditions => "completed = FALSE AND (should_retry = TRUE OR (started_at IS NOT NULL AND NOW() - started_at > '1 hour'::INTERVAL))", :batch_size => 100) do |mix| + blk.call(mix) + end + end + + def self.queue_jobs_needing_retry + find_jobs_needing_retry do |mix| + mix.enqueue + end + end + + def initialize + @s3_manager = S3Manager.new(APP_CONFIG.aws_bucket, APP_CONFIG.aws_access_key_id, APP_CONFIG.aws_secret_access_key) + end + + def run + @@log.info("quickmixer job starting. quick_mix_id #{quick_mix_id}") + + @quick_mix = QuickMix.find(quick_mix_id) + + begin + # bailout check + if @quick_mix.completed + @@log.debug("quick mix is already completed. bailing") + return + end + + fetch_audio_files + + create_mp3(@input_ogg_filename, @output_mp3_filename) + + postback + + post_success(@quick_mix) + + @@log.info("audiomixer job successful. mix_id #{quick_mix_id}") + + rescue Exception => e + puts "EEOUOU #{e}" + post_error(@quick_mix, e) + raise + end + end + + + def postback + + @@log.debug("posting mp3 mix to #{@postback_mp3_url}") + + uri = URI.parse(@postback_mp3_url) + http = Net::HTTP.new(uri.host, uri.port) + request = Net::HTTP::Put.new(uri.request_uri) + + response = nil + File.open(@output_mp3_filename,"r") do |f| + request.body_stream=f + request["Content-Type"] = "audio/mpeg" + request.add_field('Content-Length', File.size(@output_mp3_filename)) + response = http.request(request) + end + + response_code = response.code.to_i + unless response_code >= 200 && response_code <= 299 + @error_reason = "postback-mp3-mix-to-s3" + raise "unable to put to url: #{@postback_mp3_url}, status: #{response.code}, body: #{response.body}" + end + + end + + def post_success(mix) + + mp3_length = File.size(@output_mp3_filename) + mp3_md5 = Digest::MD5.new + File.open(@output_mp3_filename, 'rb').each {|line| mp3_md5.update(line)} + + mix.finish(mp3_length, mp3_md5.to_s) + end + + def post_error(mix, e) + begin + + # if error_reason is null, assume this is an unhandled error + unless @error_reason + @error_reason = "unhandled-job-exception" + @error_detail = e.to_s + end + mix.errored(error_reason, error_detail) + + rescue Exception => e + @@log.error "unable to post back to the database the error #{e}" + end + end + + + def fetch_audio_files + @input_ogg_filename = Dir::Tmpname.make_tmpname( ["#{Dir.tmpdir}/quick_mixer_#{@quick_mix.id}}", '.ogg'], nil) + @output_mp3_filename = Dir::Tmpname.make_tmpname( ["#{Dir.tmpdir}/quick_mixer_#{@quick_mix.id}}", '.mp3'], nil) + @s3_manager.download(@quick_mix[:ogg_url], @input_ogg_filename) + end + + private + + def create_mp3(input_ogg_filename, output_mp3_filename) + ffmpeg_cmd = "#{APP_CONFIG.ffmpeg_path} -i \"#{input_ogg_filename}\" -ab 128k -metadata JamRecordingId=#{@quick_mix.recording_id} -metadata JamQuickMixId=#{@quick_mix_id} -metadata JamType=QuickMix \"#{output_mp3_filename}\"" + + system(ffmpeg_cmd) + + unless $? == 0 + @error_reason = 'ffmpeg-failed' + @error_detail = $?.to_s + error_msg = "ffmpeg failed status=#{$?} error_reason=#{@error_reason} error_detail=#{@error_detail}" + @@log.info(error_msg) + raise error_msg + end + + end + end +end \ No newline at end of file diff --git a/ruby/lib/jam_ruby/resque/scheduled/audiomixer_retry.rb b/ruby/lib/jam_ruby/resque/scheduled/audiomixer_retry.rb index c314c2f48..ba95aa774 100644 --- a/ruby/lib/jam_ruby/resque/scheduled/audiomixer_retry.rb +++ b/ruby/lib/jam_ruby/resque/scheduled/audiomixer_retry.rb @@ -6,7 +6,7 @@ require 'digest/md5' module JamRuby - # periodically scheduled to find jobs that need retrying + # periodically scheduled to find jobs that need retrying, and cleanup activities class AudioMixerRetry extend Resque::Plugins::LonelyJob @@ -21,6 +21,8 @@ module JamRuby def self.perform AudioMixer.queue_jobs_needing_retry + QuickMixer.queue_jobs_needing_retry + Recording.cleanup_excessive_storage end end diff --git a/ruby/spec/factories.rb b/ruby/spec/factories.rb index 26a4d5389..b91b9dee1 100644 --- a/ruby/spec/factories.rb +++ b/ruby/spec/factories.rb @@ -308,6 +308,30 @@ FactoryGirl.define do } end + factory :quick_mix, :class => JamRuby::QuickMix do + ignore do + autowire true + end + started_at nil + completed_at nil + ogg_md5 nil + ogg_length 0 + ogg_url nil + mp3_md5 nil + mp3_length 0 + mp3_url nil + completed false + + before(:create) {|mix, evaluator| + if evaluator.autowire + user = FactoryGirl.create(:user) + mix.user = user + mix.recording = FactoryGirl.create(:recording_with_track, owner: user) + mix.recording.claimed_recordings << FactoryGirl.create(:claimed_recording, user: user, recording: mix.recording) + end + } + end + factory :invited_user, :class => JamRuby::InvitedUser do sequence(:email) { |n| "user#{n}@someservice.com" } autofriend false diff --git a/ruby/spec/jam_ruby/models/quick_mix_spec.rb b/ruby/spec/jam_ruby/models/quick_mix_spec.rb new file mode 100644 index 000000000..b8d16d3ef --- /dev/null +++ b/ruby/spec/jam_ruby/models/quick_mix_spec.rb @@ -0,0 +1,212 @@ +require 'spec_helper' +require 'rest-client' + +describe QuickMix do + + include UsesTempFiles + + before do + @user = FactoryGirl.create(:user) + @connection = FactoryGirl.create(:connection, :user => @user) + @instrument = FactoryGirl.create(:instrument, :description => 'a great instrument') + @music_session = FactoryGirl.create(:active_music_session, :creator => @user, :musician_access => true) + @track = FactoryGirl.create(:track, :connection => @connection, :instrument => @instrument) + @recording = FactoryGirl.create(:recording, :music_session => @music_session, :owner => @user) + end + + it "should copy from a regular track properly" do + @quick_mix = QuickMix.create(@recording, @user) + @quick_mix.user.id.should == @track.connection.user.id + @quick_mix.next_part_to_upload.should == 0 + @quick_mix.fully_uploaded.should == false + end + + it "should update the next part to upload properly" do + @quick_mix = QuickMix.create(@recording, @user) + @quick_mix.upload_part_complete(1, 1000) + @quick_mix.errors.any?.should be_true + @quick_mix.errors[:ogg_length][0].should == "is too short (minimum is 1 characters)" + @quick_mix.errors[:ogg_md5][0].should == "can't be blank" + end + + + it "gets a url for the track" do + @quick_mix = QuickMix.create(@recording, @user) + @quick_mix.errors.any?.should be_false + @quick_mix[:ogg_url].should == "recordings/#{@recording.created_at.strftime('%m-%d-%Y')}/#{@recording.id}/stream-mix-#{@quick_mix.id}.ogg" + @quick_mix[:mp3_url].should == "recordings/#{@recording.created_at.strftime('%m-%d-%Y')}/#{@recording.id}/stream-mix-#{@quick_mix.id}.mp3" + end + + it "signs url" do + stub_const("APP_CONFIG", app_config) + @quick_mix = QuickMix.create(@recording, @user) + @quick_mix.sign_url.should_not be_nil + end + + describe "aws-based operations", :aws => true do + + def put_file_to_aws(signed_data, contents) + + begin + RestClient.put( signed_data[:url], + contents, + { + :'Content-Type' => 'audio/ogg', + :Date => signed_data[:datetime], + :'Content-MD5' => signed_data[:md5], + :Authorization => signed_data[:authorization] + }) + rescue => e + puts e.response + raise e + end + + end + # create a test file + upload_file='some_file.ogg' + in_directory_with_file(upload_file) + + upload_file_contents="ogg binary stuff in here" + md5 = Base64.encode64(Digest::MD5.digest(upload_file_contents)).chomp + test_config = app_config + s3_manager = S3Manager.new(test_config.aws_bucket, test_config.aws_access_key_id, test_config.aws_secret_access_key) + + + before do + stub_const("APP_CONFIG", app_config) + # this block of code will fully upload a sample file to s3 + content_for_file(upload_file_contents) + s3_manager.delete_folder('recordings') # keep the bucket clean to save cost, and make it easier if post-mortuem debugging + + + end + + it "cant mark a part complete without having started it" do + @quick_mix = QuickMix.create(@recording, @user) + @quick_mix.upload_start(1000, "abc") + @quick_mix.upload_part_complete(1, 1000) + @quick_mix.errors.any?.should be_true + @quick_mix.errors[:next_part_to_upload][0].should == ValidationMessages::PART_NOT_STARTED + end + + it "no parts" do + @quick_mix = QuickMix.create(@recording, @user) + @quick_mix.upload_start(1000, "abc") + @quick_mix.upload_next_part(1000, "abc") + @quick_mix.errors.any?.should be_false + @quick_mix.upload_part_complete(1, 1000) + @quick_mix.errors.any?.should be_true + @quick_mix.errors[:next_part_to_upload][0].should == ValidationMessages::PART_NOT_FOUND_IN_AWS + end + + it "enough part failures reset the upload" do + @quick_mix = QuickMix.create(@recording, @user) + @quick_mix.upload_start(File.size(upload_file), md5) + @quick_mix.upload_next_part(File.size(upload_file), md5) + @quick_mix.errors.any?.should be_false + APP_CONFIG.max_track_part_upload_failures.times do |i| + @quick_mix.upload_part_complete(@quick_mix.next_part_to_upload, File.size(upload_file)) + @quick_mix.errors[:next_part_to_upload] == [ValidationMessages::PART_NOT_FOUND_IN_AWS] + part_failure_rollover = i == APP_CONFIG.max_track_part_upload_failures - 1 + expected_is_part_uploading = !part_failure_rollover + expected_part_failures = part_failure_rollover ? 0 : i + 1 + @quick_mix.reload + @quick_mix.is_part_uploading.should == expected_is_part_uploading + @quick_mix.part_failures.should == expected_part_failures + end + + @quick_mix.reload + @quick_mix.upload_failures.should == 1 + @quick_mix.file_offset.should == 0 + @quick_mix.next_part_to_upload.should == 0 + @quick_mix.upload_id.should be_nil + @quick_mix.ogg_md5.should be_nil + @quick_mix.ogg_length.should == 0 + end + + it "enough upload failures fails the upload forever" do + APP_CONFIG.stub(:max_track_upload_failures).and_return(1) + APP_CONFIG.stub(:max_track_part_upload_failures).and_return(2) + @quick_mix = QuickMix.create(@recording, @user) + APP_CONFIG.max_track_upload_failures.times do |j| + @quick_mix.upload_start(File.size(upload_file), md5) + @quick_mix.upload_next_part(File.size(upload_file), md5) + @quick_mix.errors.any?.should be_false + APP_CONFIG.max_track_part_upload_failures.times do |i| + @quick_mix.upload_part_complete(@quick_mix.next_part_to_upload, File.size(upload_file)) + @quick_mix.errors[:next_part_to_upload] == [ValidationMessages::PART_NOT_FOUND_IN_AWS] + part_failure_rollover = i == APP_CONFIG.max_track_part_upload_failures - 1 + expected_is_part_uploading = part_failure_rollover ? false : true + expected_part_failures = part_failure_rollover ? 0 : i + 1 + @quick_mix.reload + @quick_mix.is_part_uploading.should == expected_is_part_uploading + @quick_mix.part_failures.should == expected_part_failures + end + @quick_mix.upload_failures.should == j + 1 + end + + @quick_mix.reload + @quick_mix.upload_failures.should == APP_CONFIG.max_track_upload_failures + @quick_mix.file_offset.should == 0 + @quick_mix.next_part_to_upload.should == 0 + @quick_mix.upload_id.should be_nil + @quick_mix.ogg_md5.should be_nil + @quick_mix.ogg_length.should == 0 + + # try to poke it and get the right kind of error back + @quick_mix.upload_next_part(File.size(upload_file), md5) + @quick_mix.errors[:upload_failures] = [ValidationMessages::UPLOAD_FAILURES_EXCEEDED] + end + + describe "correctly uploaded a file" do + + before do + @quick_mix = QuickMix.create(@recording, @user) + @quick_mix.upload_start(File.size(upload_file), md5) + @quick_mix.upload_next_part(File.size(upload_file), md5) + signed_data = @quick_mix.upload_sign(md5) + @response = put_file_to_aws(signed_data, upload_file_contents) + @quick_mix.upload_part_complete(@quick_mix.next_part_to_upload, File.size(upload_file)) + @quick_mix.errors.any?.should be_false + @quick_mix.upload_complete + @quick_mix.marking_complete = false # this is a side effect of using the same model throughout this process; controllers wouldn't see this + @quick_mix.finish(File.size(upload_file), md5) + @quick_mix.errors.any?.should be_false + + # let's verify that .finish sets everything it should + @quick_mix.mp3_length.should == File.size(upload_file) + @quick_mix.mp3_md5.should == md5 + @quick_mix.completed.should be_true + @quick_mix.recording.reload + @quick_mix.recording.first_quick_mix_id.should == @quick_mix.id + end + + it "can download an updated file" do + @response = RestClient.get @quick_mix.sign_url + @response.body.should == upload_file_contents + end + + it "can't mark completely uploaded twice" do + @quick_mix.upload_complete + @quick_mix.errors.any?.should be_true + @quick_mix.errors[:fully_uploaded][0].should == "already set" + @quick_mix.part_failures.should == 0 + end + + it "can't ask for a next part if fully uploaded" do + @quick_mix.upload_next_part(File.size(upload_file), md5) + @quick_mix.errors.any?.should be_true + @quick_mix.errors[:fully_uploaded][0].should == "already set" + @quick_mix.part_failures.should == 0 + end + + it "can't ask for mark part complete if fully uploaded" do + @quick_mix.upload_part_complete(1, 1000) + @quick_mix.errors.any?.should be_true + @quick_mix.errors[:fully_uploaded][0].should == "already set" + @quick_mix.part_failures.should == 0 + end + end + end +end + diff --git a/ruby/spec/jam_ruby/models/recorded_track_spec.rb b/ruby/spec/jam_ruby/models/recorded_track_spec.rb index fe9f4c2e9..13e7ce746 100644 --- a/ruby/spec/jam_ruby/models/recorded_track_spec.rb +++ b/ruby/spec/jam_ruby/models/recorded_track_spec.rb @@ -192,6 +192,7 @@ describe RecordedTrack do @recorded_track.errors.any?.should be_false @recorded_track.upload_complete @recorded_track.errors.any?.should be_false + @recorded_track.marking_complete = false end it "can download an updated file" do diff --git a/ruby/spec/jam_ruby/models/recording_spec.rb b/ruby/spec/jam_ruby/models/recording_spec.rb index 0989e777c..c093d5950 100644 --- a/ruby/spec/jam_ruby/models/recording_spec.rb +++ b/ruby/spec/jam_ruby/models/recording_spec.rb @@ -2,6 +2,11 @@ require 'spec_helper' describe Recording do + include UsesTempFiles + + + let(:s3_manager) { S3Manager.new(app_config.aws_bucket, app_config.aws_access_key_id, app_config.aws_secret_access_key) } + before do @user = FactoryGirl.create(:user) @instrument = FactoryGirl.create(:instrument, :description => 'a great instrument') @@ -9,7 +14,187 @@ describe Recording do @connection = FactoryGirl.create(:connection, :user => @user, :music_session => @music_session) @track = FactoryGirl.create(:track, :connection => @connection, :instrument => @instrument) end - + + describe "cleanup_excessive_storage" do + + sample_audio='sample.file' + in_directory_with_file(sample_audio) + + let(:user2) {FactoryGirl.create(:user)} + let(:quick_mix) { FactoryGirl.create(:quick_mix) } + let(:quick_mix2) { + recording.recorded_tracks << FactoryGirl.create(:recorded_track, recording: recording, user: user2) + recording.claimed_recordings << FactoryGirl.create(:claimed_recording, user: user2, recording: recording) + recording.save! + FactoryGirl.create(:quick_mix, autowire: false, recording:recording, user: user2) } + let(:recording) { quick_mix.recording } + + before(:each) do + content_for_file("ogg goodness") + end + + it "cleans up nothing without failing" do + QuickMix.cleanup_excessive_storage + end + + it "leaves untouched no completed mixes" do + + + recording.has_final_mix.should be_false + recording.has_stream_mix.should be_false + quick_mix.cleaned.should be_false + + QuickMix.cleanup_excessive_storage + recording.reload + quick_mix.reload + + recording.has_final_mix.should be_false + recording.has_stream_mix.should be_false + quick_mix.cleaned.should be_false + + end + + it "leaves untouched a completed quick mix if it's the only one and old enough" do + s3_manager.upload(quick_mix.filename('ogg'), sample_audio) + s3_manager.upload(quick_mix.filename('mp3'), sample_audio) + quick_mix.completed = true + quick_mix[:ogg_url] = quick_mix.filename('ogg') + quick_mix[:mp3_url] = quick_mix.filename('mp3') + quick_mix.completed_at = 10.days.ago + quick_mix.save! + recording.has_stream_mix = true + recording.first_quick_mix_id = quick_mix.id + recording.save! + quick_mix.recording_id.should == recording.id + + QuickMix.cleanup_excessive_storage + recording.reload + quick_mix.reload + + recording.has_final_mix.should be_false + recording.has_stream_mix.should be_true + quick_mix.cleaned.should be_false + quick_mix[:ogg_url].should_not be_nil + quick_mix[:mp3_url].should_not be_nil + end + + it "cleans up completed quick mix if their is a final mix" do + s3_manager.upload(quick_mix.filename('ogg'), sample_audio) + s3_manager.upload(quick_mix.filename('mp3'), sample_audio) + quick_mix.completed = true + quick_mix[:ogg_url] = quick_mix.filename('ogg') + quick_mix[:mp3_url] = quick_mix.filename('mp3') + quick_mix.completed_at = 10.days.ago + quick_mix.save! + recording.has_final_mix = true + recording.has_stream_mix = true + recording.first_quick_mix_id = quick_mix.id + recording.save! + quick_mix.recording_id.should == recording.id + + QuickMix.cleanup_excessive_storage + recording.reload + quick_mix.reload + + recording.has_final_mix.should be_true + recording.has_stream_mix.should be_false + recording.first_quick_mix_id.should be_nil + quick_mix.cleaned.should be_true + quick_mix[:ogg_url].should_not be_nil + quick_mix[:mp3_url].should_not be_nil + s3_manager.exists?(quick_mix.filename('ogg')).should be_false + s3_manager.exists?(quick_mix.filename('mp3')).should be_false + + end + + it "cleans up one of two quick mixes" do + # quick_mix2 should get cleaned up, but quick_mix1 should be left alone because we need at least one if has_final_mix = false + quick_mix2.touch + s3_manager.upload(quick_mix.filename('ogg'), sample_audio) + s3_manager.upload(quick_mix.filename('mp3'), sample_audio) + s3_manager.upload(quick_mix2.filename('ogg'), sample_audio) + s3_manager.upload(quick_mix2.filename('mp3'), sample_audio) + quick_mix.completed = true + quick_mix[:ogg_url] = quick_mix.filename('ogg') + quick_mix[:mp3_url] = quick_mix.filename('mp3') + quick_mix.completed_at = 10.days.ago + quick_mix.save! + quick_mix2.completed = true + quick_mix2[:ogg_url] = quick_mix.filename('ogg') + quick_mix2[:mp3_url] = quick_mix.filename('mp3') + quick_mix2.completed_at = 10.days.ago + quick_mix2.save! + recording.has_stream_mix = true + recording.first_quick_mix_id = quick_mix.id + recording.save! + quick_mix.recording_id.should == recording.id + + QuickMix.cleanup_excessive_storage + recording.reload + quick_mix.reload + quick_mix2.reload + + recording.has_final_mix.should be_false + recording.has_stream_mix.should be_true + recording.first_quick_mix_id.should_not be_nil + quick_mix.cleaned.should be_false + quick_mix[:ogg_url].should_not be_nil + quick_mix[:mp3_url].should_not be_nil + s3_manager.exists?(quick_mix.filename('ogg')).should be_true + s3_manager.exists?(quick_mix.filename('mp3')).should be_true + quick_mix2.cleaned.should be_true + quick_mix2[:ogg_url].should_not be_nil + quick_mix2[:mp3_url].should_not be_nil + s3_manager.exists?(quick_mix2.filename('ogg')).should be_false + s3_manager.exists?(quick_mix2.filename('mp3')).should be_false + + end + + it "cleans up all quick mixes if there is a final mix" do + # quick_mix2 should get cleaned up, but quick_mix1 should be left alone because we need at least one if has_final_mix = false + quick_mix2.touch + s3_manager.upload(quick_mix.filename('ogg'), sample_audio) + s3_manager.upload(quick_mix.filename('mp3'), sample_audio) + s3_manager.upload(quick_mix2.filename('ogg'), sample_audio) + s3_manager.upload(quick_mix2.filename('mp3'), sample_audio) + quick_mix.completed = true + quick_mix[:ogg_url] = quick_mix.filename('ogg') + quick_mix[:mp3_url] = quick_mix.filename('mp3') + quick_mix.completed_at = 10.days.ago + quick_mix.save! + quick_mix2.completed = true + quick_mix2[:ogg_url] = quick_mix.filename('ogg') + quick_mix2[:mp3_url] = quick_mix.filename('mp3') + quick_mix2.completed_at = 10.days.ago + quick_mix2.save! + recording.has_final_mix = true + recording.has_stream_mix = true + recording.first_quick_mix_id = quick_mix.id + recording.save! + quick_mix.recording_id.should == recording.id + + QuickMix.cleanup_excessive_storage + recording.reload + quick_mix.reload + quick_mix2.reload + + recording.has_final_mix.should be_true + recording.has_stream_mix.should be_false + recording.first_quick_mix_id.should be_nil + quick_mix.cleaned.should be_true + quick_mix[:ogg_url].should_not be_nil + quick_mix[:mp3_url].should_not be_nil + s3_manager.exists?(quick_mix.filename('ogg')).should be_false + s3_manager.exists?(quick_mix.filename('mp3')).should be_false + quick_mix2.cleaned.should be_true + quick_mix2[:ogg_url].should_not be_nil + quick_mix2[:mp3_url].should_not be_nil + s3_manager.exists?(quick_mix2.filename('ogg')).should be_false + s3_manager.exists?(quick_mix2.filename('mp3')).should be_false + + end + end + it "should allow finding of recorded tracks" do user2 = FactoryGirl.create(:user) connection2 = FactoryGirl.create(:connection, :user => user2, :music_session => @music_session) @@ -241,7 +426,7 @@ describe Recording do @genre = FactoryGirl.create(:genre) @recording.claim(@user, "Recording", "Recording Description", @genre, true) uploads = Recording.list_uploads(@user) - uploads["uploads"].length.should == 1 + uploads["uploads"].length.should == 2 # recorded_track and stream_mix Recording.list_uploads(@user, 10, uploads["next"])["uploads"].length.should == 0 end @@ -336,11 +521,12 @@ describe Recording do # We should have 2 items; a track and a video: uploads = Recording.list_uploads(@user) - uploads["uploads"].should have(2).items - uploads["uploads"][0][:type].should eq("recorded_track") - uploads["uploads"][1][:type].should eq("recorded_video") - uploads["uploads"][0].should include(:client_track_id) - uploads["uploads"][1].should include(:client_video_source_id) + uploads["uploads"].should have(3).items + uploads["uploads"][0][:type].should eq("stream_mix") + uploads["uploads"][1][:type].should eq("recorded_track") + uploads["uploads"][2][:type].should eq("recorded_video") + uploads["uploads"][1].should include(:client_track_id) + uploads["uploads"][2].should include(:client_video_source_id) # Next page should have nothing: uploads = Recording.list_uploads(@user, 10, uploads["next"]) @@ -358,13 +544,19 @@ describe Recording do # Limit to 1, so we can test pagination: uploads = Recording.list_uploads(@user, 1) uploads["uploads"].should have(1).items # First page + uploads["uploads"][0][:type].should == 'stream_mix' # Second page: uploads = Recording.list_uploads(@user, 1, uploads["next"]) - uploads["uploads"].should have(1).items + uploads["uploads"].should have(1).items + uploads["uploads"][0][:type].should == 'recorded_track' # Last page (should be empty): - uploads = Recording.list_uploads(@user, 10, uploads["next"]) + uploads = Recording.list_uploads(@user, 1, uploads["next"]) + uploads["uploads"].should have(1).items + uploads["uploads"][0][:type].should == 'recorded_video' + + uploads = Recording.list_uploads(@user, 1, uploads["next"]) uploads["uploads"].should have(0).items end @@ -384,15 +576,17 @@ describe Recording do @genre = FactoryGirl.create(:genre) @recording.claim(@user, "Recording", "Recording Description", @genre, true) - # We should have 2 items; a track and a video: + # We should have 3 items; a track and a video: uploads = Recording.list_uploads(@user) - uploads["uploads"].should have(2).items - uploads["uploads"][0][:type].should eq("recorded_track") - uploads["uploads"][1][:type].should eq("recorded_video") - uploads["uploads"][0].should include(:client_track_id) - uploads["uploads"][1].should include(:client_video_source_id) - uploads["uploads"][0][:client_track_id].should eq(@track.client_track_id) - uploads["uploads"][1][:client_video_source_id].should eq(video_source.client_video_source_id) + uploads["uploads"].should have(3).items + uploads["uploads"][0][:type].should eq("stream_mix") + uploads["uploads"][1][:type].should eq("recorded_track") + uploads["uploads"][2][:type].should eq("recorded_video") + uploads["uploads"][1].should include(:client_track_id) + uploads["uploads"][2].should include(:client_video_source_id) + + uploads["uploads"][1][:client_track_id].should eq(@track.client_track_id) + uploads["uploads"][2][:client_video_source_id].should eq(video_source.client_video_source_id) # Next page should have nothing: uploads = Recording.list_uploads(@user, 10, uploads["next"]) @@ -401,20 +595,49 @@ describe Recording do # List uploads for user2. We should have 2 items; a track and a video: # The track and video source IDs should be different than above: uploads = Recording.list_uploads(user2) - uploads["uploads"].should have(2).items - uploads["uploads"][0][:type].should eq("recorded_track") - uploads["uploads"][1][:type].should eq("recorded_video") - uploads["uploads"][0].should include(:client_track_id) - uploads["uploads"][1].should include(:client_video_source_id) - uploads["uploads"][0][:client_track_id].should eq(track2.client_track_id) - uploads["uploads"][1][:client_video_source_id].should eq(video_source2.client_video_source_id) + uploads["uploads"].should have(3).items + uploads["uploads"][0][:type].should eq("stream_mix") + uploads["uploads"][1][:type].should eq("recorded_track") + uploads["uploads"][2][:type].should eq("recorded_video") + uploads["uploads"][1].should include(:client_track_id) + uploads["uploads"][2].should include(:client_video_source_id) + uploads["uploads"][1][:client_track_id].should eq(track2.client_track_id) + uploads["uploads"][2][:client_video_source_id].should eq(video_source2.client_video_source_id) # Next page should have nothing: - uploads = Recording.list_uploads(@user, 10, uploads["next"]) + uploads = Recording.list_uploads(user2, 10, uploads["next"]) uploads["uploads"].should have(0).items end + it "stream_mix.fully_uploaded = true removes from list_uploads" do + @recording = Recording.start(@music_session, @user) + @recording.stop + @recording.reload + @genre = FactoryGirl.create(:genre) + @recording.claim(@user, "Recording", "Recording Description", @genre, true) + + # We should have 2 items; a track and a quick mix: + uploads = Recording.list_uploads(@user) + uploads["uploads"].should have(2).items + uploads["uploads"][0][:type].should eq("stream_mix") + uploads["uploads"][1][:type].should eq("recorded_track") + + @recording.quick_mixes[0].fully_uploaded = true + @recording.quick_mixes[0].save! + + uploads = Recording.list_uploads(@user) + uploads["uploads"].should have(1).items + uploads["uploads"][0][:type].should eq("recorded_track") + + + @recording.recorded_tracks[0].fully_uploaded = true + @recording.recorded_tracks[0].save! + + uploads = Recording.list_uploads(@user) + uploads["uploads"].should have(0).items + + end end diff --git a/ruby/spec/jam_ruby/models/user_sync_spec.rb b/ruby/spec/jam_ruby/models/user_sync_spec.rb new file mode 100644 index 000000000..4781200b5 --- /dev/null +++ b/ruby/spec/jam_ruby/models/user_sync_spec.rb @@ -0,0 +1,226 @@ +require 'spec_helper' + +describe UserSync do + + let(:user1) {FactoryGirl.create(:user)} + let(:user2) {FactoryGirl.create(:user)} + + before(:each) do + + end + + it "empty results" do + UserSync.all.length.should == 0 + end + + describe "index" do + it "empty results" do + data = UserSync.index({user_id: user1}) + data[:query].length.should == 0 + data[:next].should be_nil + end + + it "one mix" do + mix = FactoryGirl.create(:mix) + mix.recording.duration = 1 + mix.recording.save! + + data = UserSync.index({user_id: mix.recording.recorded_tracks[0].user.id}) + data[:next].should be_nil + user_syncs = data[:query] + user_syncs.length.should == 2 + user_syncs[0].recorded_track.should == mix.recording.recorded_tracks[0] + user_syncs[0].mix.should be_nil + user_syncs[1].mix.should == mix + user_syncs[1].recorded_track.should be_nil + end + + it "two mixes, one not belonging to querier" do + mix1 = FactoryGirl.create(:mix) + mix2 = FactoryGirl.create(:mix) + mix1.recording.duration = 1 + mix1.recording.save! + mix2.recording.duration = 1 + mix2.recording.save! + + data = UserSync.index({user_id: mix1.recording.recorded_tracks[0].user.id}) + data[:next].should be_nil + user_syncs = data[:query] + user_syncs.length.should == 2 + user_syncs[0].recorded_track.should == mix1.recording.recorded_tracks[0] + user_syncs[0].mix.should be_nil + user_syncs[1].mix.should == mix1 + user_syncs[1].recorded_track.should be_nil + + data = UserSync.index({user_id: mix2.recording.recorded_tracks[0].user.id}) + data[:next].should be_nil + user_syncs = data[:query] + user_syncs.length.should == 2 + user_syncs[0].recorded_track.should == mix2.recording.recorded_tracks[0] + user_syncs[0].mix.should be_nil + user_syncs[1].mix.should == mix2 + user_syncs[1].recorded_track.should be_nil + end + + describe "one recording with two users" do + let!(:recording1) { + recording = FactoryGirl.create(:recording, owner: user1, band: nil, duration:1) + recording.recorded_tracks << FactoryGirl.create(:recorded_track, recording: recording, user: recording.owner) + recording.recorded_tracks << FactoryGirl.create(:recorded_track, recording: recording, user: user2) + recording.save! + recording.reload + recording + } + + let(:sorted_tracks) { + Array.new(recording1.recorded_tracks).sort! {|a, b| + if a.created_at == b.created_at + a.id <=> b.id + else + a.created_at <=> b.created_at + end + } + } + + it "no claimed_recordings" do + # both user1 and user2 should be told about each others tracks, because both will need to upload their tracks + data = UserSync.index({user_id: user1.id}) + data[:next].should be_nil + user_syncs = data[:query] + user_syncs.length.should == 2 + user_syncs[0].recorded_track.should == sorted_tracks[0] + user_syncs[1].recorded_track.should == sorted_tracks[1] + + data = UserSync.index({user_id: user2.id}) + data[:next].should be_nil + user_syncs = data[:query] + user_syncs[0].recorded_track.should == sorted_tracks[0] + user_syncs[1].recorded_track.should == sorted_tracks[1] + end + + it "recording isn't over" do + recording1.duration = nil + recording1.save! + + # nothing should be returned, because the recording isn't over + data = UserSync.index({user_id: user1.id}) + data[:next].should be_nil + user_syncs = data[:query] + user_syncs.length.should == 0 + + data = UserSync.index({user_id: user2.id}) + data[:next].should be_nil + user_syncs = data[:query] + user_syncs.length.should == 0 + end + + it "one user decides to keep the recording" do + claimed_recording = FactoryGirl.create(:claimed_recording, user: user1, recording: recording1, discarded:false) + claimed_recording.recording.should == recording1 + + data = UserSync.index({user_id: user1.id}) + data[:next].should be_nil + user_syncs = data[:query] + user_syncs.length.should == 2 + user_syncs[0].recorded_track.should == sorted_tracks[0] + user_syncs[1].recorded_track.should == sorted_tracks[1] + + data = UserSync.index({user_id: user2.id}) + data[:next].should be_nil + user_syncs = data[:query] + user_syncs.length.should == 2 + user_syncs[0].recorded_track.should == sorted_tracks[0] + user_syncs[1].recorded_track.should == sorted_tracks[1] + end + + + it "one user decides to discard the recording" do + FactoryGirl.create(:claimed_recording, user: user1, recording: recording1, discarded:true) + + data = UserSync.index({user_id: user1.id}) + data[:next].should be_nil + user_syncs = data[:query] + user_syncs.length.should == 2 + user_syncs[0].recorded_track.should == sorted_tracks[0] + user_syncs[1].recorded_track.should == sorted_tracks[1] + + data = UserSync.index({user_id: user2.id}) + data[:next].should be_nil + user_syncs = data[:query] + user_syncs.length.should == 2 + user_syncs[0].recorded_track.should == sorted_tracks[0] + user_syncs[1].recorded_track.should == sorted_tracks[1] + end + + it "both users decide to discard the recording" do + recording1.all_discarded = true + recording1.save! + + data = UserSync.index({user_id: user1.id}) + data[:next].should be_nil + user_syncs = data[:query] + user_syncs.length.should == 0 + + data = UserSync.index({user_id: user2.id}) + data[:next].should be_nil + user_syncs = data[:query] + user_syncs.length.should == 0 + end + end + end + + describe "pagination" do + let!(:recording1) { + recording = FactoryGirl.create(:recording, owner: user1, band: nil, duration:1) + recording.recorded_tracks << FactoryGirl.create(:recorded_track, recording: recording, user: recording.owner) + recording.recorded_tracks << FactoryGirl.create(:recorded_track, recording: recording, user: user2) + recording.save! + recording.reload + recording + } + let(:sorted_tracks) { + Array.new(recording1.recorded_tracks).sort! {|a, b| + if a.created_at == b.created_at + a.id <=> b.id + else + a.created_at <=> b.created_at + end + } + } + + it "tiny page size" do + # get the 1st track + data = UserSync.index({user_id: user1.id, offset: 0, limit: 1}) + data[:next].should == 1 + user_syncs = data[:query] + user_syncs.length.should == 1 + user_syncs[0].recorded_track.should == sorted_tracks[0] + data[:query].total_entries.should == 2 + + # then get the second track, which happens to be on an edge, pagination wise + data = UserSync.index({user_id: user1.id, offset: 1, limit: 1}) + data[:next].should == 2 + user_syncs = data[:query] + user_syncs.length.should == 1 + user_syncs[0].recorded_track.should == sorted_tracks[1] + data[:query].total_entries.should == 2 + + # so prove it's on the edge by asking for 2 items instead of 1. + data = UserSync.index({user_id: user1.id, offset: 0, limit: 3}) + data[:next].should == nil + user_syncs = data[:query] + user_syncs.length.should == 2 + user_syncs[0].recorded_track.should == sorted_tracks[0] + user_syncs[1].recorded_track.should == sorted_tracks[1] + data[:query].total_entries.should == 2 + + data = UserSync.index({user_id: user1.id, offset: 2, limit: 1}) + data[:next].should == nil + user_syncs = data[:query] + user_syncs.length.should == 0 + data[:query].total_entries.should == 2 + + + end + end +end diff --git a/ruby/spec/jam_ruby/resque/audiomixer_spec.rb b/ruby/spec/jam_ruby/resque/audiomixer_spec.rb index 95e7b136f..ae63a9602 100644 --- a/ruby/spec/jam_ruby/resque/audiomixer_spec.rb +++ b/ruby/spec/jam_ruby/resque/audiomixer_spec.rb @@ -1,7 +1,6 @@ require 'spec_helper' require 'fileutils' -=begin # these tests avoid the use of ActiveRecord and FactoryGirl to do blackbox, non test-instrumented tests describe AudioMixer do @@ -157,7 +156,7 @@ describe AudioMixer do @music_session = FactoryGirl.create(:active_music_session, :creator => @user, :musician_access => true) # @music_session.connections << @connection @music_session.save - @connection.join_the_session(@music_session, true, nil) + @connection.join_the_session(@music_session, true, [@track], @user, 10) @recording = Recording.start(@music_session, @user) @recording.stop @recording.claim(@user, "name", "description", Genre.first, true) @@ -178,8 +177,10 @@ describe AudioMixer do # stub out methods that are environmentally sensitive (so as to skip s3, and not run an actual audiomixer) before(:each) do AudioMixer.any_instance.stub(:execute) do |manifest_file| - output_filename = JSON.parse(File.read(manifest_file))['output']['filename'] - FileUtils.touch output_filename + output_ogg_filename = JSON.parse(File.read(manifest_file))['output']['filename'] + FileUtils.touch output_ogg_filename + output_mp3_filename = JSON.parse(File.read(manifest_file))['output']['filename_mp3'] + FileUtils.touch output_mp3_filename end AudioMixer.any_instance.stub(:postback) # don't actually post resulting off file up @@ -194,18 +195,20 @@ describe AudioMixer do @mix = Mix.schedule(@recording) @mix.reload @mix.started_at.should_not be_nil - AudioMixer.perform(@mix.id, @mix.sign_url(60 * 60 * 24)) + AudioMixer.perform(@mix.id, @mix.sign_url(60 * 60 * 24, 'ogg'), @mix.sign_url(60 * 60 * 24, 'mp3')) @mix.reload @mix.completed.should be_true - @mix.length.should == 0 - @mix.md5.should == 'd41d8cd98f00b204e9800998ecf8427e' # that's the md5 of a touched file + @mix.ogg_length.should == 0 + @mix.ogg_md5.should == 'd41d8cd98f00b204e9800998ecf8427e' # that's the md5 of a touched file + @mix.mp3_length.should == 0 + @mix.mp3_md5.should == 'd41d8cd98f00b204e9800998ecf8427e' # that's the md5 of a touched file end it "errored" do local_files_manifest[:files][0][:filename] = '/some/path/to/nowhere' Mix.any_instance.stub(:manifest).and_return(local_files_manifest) @mix = Mix.schedule(@recording) - expect{ AudioMixer.perform(@mix.id, @mix.sign_url(60 * 60 * 24)) }.to raise_error + expect{ AudioMixer.perform(@mix.id, @mix.sign_url(60 * 60 * 24, 'ogg'), @mix.sign_url(60 * 60 * 24, 'mp3')) }.to raise_error @mix.reload @mix.completed.should be_false @mix.error_count.should == 1 @@ -283,8 +286,10 @@ describe AudioMixer do @s3_sample = @s3_manager.sign_url(key, :secure => false) AudioMixer.any_instance.stub(:execute) do |manifest_file| - output_filename = JSON.parse(File.read(manifest_file))['output']['filename'] - FileUtils.touch output_filename + output_filename_ogg = JSON.parse(File.read(manifest_file))['output']['filename'] + FileUtils.touch output_filename_ogg + output_filename_mp3 = JSON.parse(File.read(manifest_file))['output']['filename_mp3'] + FileUtils.touch output_filename_mp3 end end @@ -296,26 +301,12 @@ describe AudioMixer do @mix.reload @mix.completed.should be_true - @mix.length.should == 0 - @mix.md5.should == 'd41d8cd98f00b204e9800998ecf8427e' # that's the md5 of a touched file, which is what the stubbed :execute does - end - - it "fails" do - with_resque do - s3_manifest[:files][0][:filename] = @s3_manager.url('audiomixer/bogus.ogg', :secure => false) # take off some of the trailing chars of the url - Mix.any_instance.stub(:manifest).and_return(s3_manifest) - expect{ Mix.schedule(@recording) }.to raise_error - end - - @mix = Mix.order('id desc').limit(1).first() - @mix.reload - @mix.completed.should be_false - @mix.error_reason.should == "unable to download" - #ResqueFailedJobMailer::Mailer.deliveries.count.should == 1 + @mix.ogg_length.should == 0 + @mix.ogg_md5.should == 'd41d8cd98f00b204e9800998ecf8427e' # that's the md5 of a touched file, which is what the stubbed :execute does + @mix.mp3_length.should == 0 + @mix.mp3_md5.should == 'd41d8cd98f00b204e9800998ecf8427e' # that's the md5 of a touched file, which is what the stubbed :execute does end end end end - -=end \ No newline at end of file diff --git a/ruby/spec/jam_ruby/resque/quick_mixer_spec.rb b/ruby/spec/jam_ruby/resque/quick_mixer_spec.rb new file mode 100644 index 000000000..9a622ce1a --- /dev/null +++ b/ruby/spec/jam_ruby/resque/quick_mixer_spec.rb @@ -0,0 +1,121 @@ +require 'spec_helper' +require 'fileutils' + +# these tests avoid the use of ActiveRecord and FactoryGirl to do blackbox, non test-instrumented tests +describe AudioMixer do + + include UsesTempFiles + + let(:quick_mix) { FactoryGirl.create(:quick_mix, started_at: nil, should_retry: false) } + + + before(:each) do + stub_const("APP_CONFIG", app_config) + module EM + @next_tick_queue = [] + end + + MQRouter.client_exchange = double("client_exchange") + MQRouter.user_exchange = double("user_exchange") + MQRouter.client_exchange.should_receive(:publish).any_number_of_times + MQRouter.user_exchange.should_receive(:publish).any_number_of_times + end + + def assert_found_job_count(expected) + count = 0 + QuickMixer.find_jobs_needing_retry do + count = count + 1 + end + count.should == expected + end + + describe "queue_jobs_needing_retry" do + it "won't find anything if no quick mixes" do + assert_found_job_count(0) + end + + it "won't find a quick mix that hasn't been started" do + quick_mix.touch + + assert_found_job_count(0) + end + + it "won't find a quick mix that has should_retry flagged" do + quick_mix.should_retry = true + quick_mix.save! + + assert_found_job_count(1) + end + + it "won't find a quick mix that has should_retry flagged" do + quick_mix.should_retry = false + quick_mix.started_at = 2.hours.ago + quick_mix.save! + + assert_found_job_count(1) + end + end + + + # these tests try to run the job with minimal faking. Here's what we still fake: + # we don't run audiomixer. audiomixer is tested already + # we don't run redis and actual resque, because that's tested by resque/resque-spec + describe "full", :aws => true do + + + sample_ogg='sample.ogg' + in_directory_with_file(sample_ogg) + + + before(:each) do + content_for_file("ogg goodness") + @user = FactoryGirl.create(:user) + @connection = FactoryGirl.create(:connection, :user => @user) + @instrument = FactoryGirl.create(:instrument, :description => 'a great instrument') + @track = FactoryGirl.create(:track, :connection => @connection, :instrument => @instrument) + @music_session = FactoryGirl.create(:active_music_session, :creator => @user, :musician_access => true) + # @music_session.connections << @connection + @music_session.save + @connection.join_the_session(@music_session, true, [@track], @user, 10) + @recording = Recording.start(@music_session, @user) + @recording.stop + @recording.claim(@user, "name", "description", Genre.first, true) + @recording.errors.any?.should be_false + @recording.quick_mixes.length.should == 1 + @quick_mix = @recording.quick_mixes[0] + + test_config = app_config + + @s3_manager = S3Manager.new(test_config.aws_bucket, test_config.aws_access_key_id, test_config.aws_secret_access_key) + + # put a file in s3 + @s3_manager.upload(@quick_mix[:ogg_url], sample_ogg) + + # create a signed url that the job will use to fetch it back down + @s3_sample = @s3_manager.sign_url(@quick_mix[:ogg_url], :secure => false) + + QuickMixer.any_instance.stub(:create_mp3) do |input_file, output_file| + FileUtils.mv input_file, output_file + end + end + + it "completes" do + with_resque do + #QuickMix.any_instance.stub(:manifest).and_return(s3_manifest) + @mix = @recording.quick_mixes[0] + @mix.enqueue + end + + @mix.reload + @mix.completed.should be_true + @mix.mp3_length.should == "ogg goodness".length + @mix.mp3_md5.should == ::Digest::MD5.file(sample_ogg).hexdigest + + # fetch back the mp3 file from s3, check that it's equivalent to the sample_ogg file (which we did a FileUtils.mv of to make a fake mp3) + mp3_fetched_from_s3 = Tempfile.new('foo') + @s3_manager.download(@quick_mix[:mp3_url], mp3_fetched_from_s3.path) + @mix.mp3_md5.should == ::Digest::MD5.file(mp3_fetched_from_s3).hexdigest + + end + end +end diff --git a/ruby/spec/spec_helper.rb b/ruby/spec/spec_helper.rb index 3898c2462..73e9a3384 100644 --- a/ruby/spec/spec_helper.rb +++ b/ruby/spec/spec_helper.rb @@ -42,6 +42,7 @@ ActiveRecord::Base.add_observer InvitedUserObserver.instance ActiveRecord::Base.add_observer UserObserver.instance ActiveRecord::Base.add_observer FeedbackObserver.instance ActiveRecord::Base.add_observer RecordedTrackObserver.instance +ActiveRecord::Base.add_observer QuickMixObserver.instance #RecordedTrack.observers.disable :all # only a few tests want this observer active diff --git a/web/app/assets/javascripts/application.js b/web/app/assets/javascripts/application.js index ec05b467c..b971a1d5e 100644 --- a/web/app/assets/javascripts/application.js +++ b/web/app/assets/javascripts/application.js @@ -34,6 +34,7 @@ //= require jquery.pulse //= require jquery.browser //= require jquery.custom-protocol +//= require jquery.exists //= require jstz //= require class //= require AAC_underscore diff --git a/web/app/assets/javascripts/dialog/allSyncsDialog.js b/web/app/assets/javascripts/dialog/allSyncsDialog.js new file mode 100644 index 000000000..e6de6cf3f --- /dev/null +++ b/web/app/assets/javascripts/dialog/allSyncsDialog.js @@ -0,0 +1,48 @@ +(function (context, $) { + + "use strict"; + context.JK = context.JK || {}; + context.JK.AllSyncsDialog = function (app) { + var logger = context.JK.logger; + var rest = context.JK.Rest(); + var $dialog = null; + var $syncsHolder = null; + var syncViewer = null; + + + function registerEvents() { + + } + + function beforeShow() { + syncViewer.onShow() + } + + function beforeHide() { + syncViewer.onHide() + } + + function initialize(invitationDialogInstance) { + var dialogBindings = { + 'beforeShow': beforeShow, + 'beforeHide': beforeHide + }; + + app.bindDialog('all-syncs-dialog', dialogBindings); + + $dialog = $('#all-syncs-dialog'); + $syncsHolder = $dialog.find('.syncs') + + syncViewer = new context.JK.SyncViewer(app); + syncViewer.init($syncsHolder); + $syncsHolder.append(syncViewer.root) + registerEvents(); + + }; + + + this.initialize = initialize; + } + + return this; +})(window, jQuery); \ No newline at end of file diff --git a/web/app/assets/javascripts/dialog/recordingFinishedDialog.js b/web/app/assets/javascripts/dialog/recordingFinishedDialog.js index 201e1cc9f..5863d54e8 100644 --- a/web/app/assets/javascripts/dialog/recordingFinishedDialog.js +++ b/web/app/assets/javascripts/dialog/recordingFinishedDialog.js @@ -7,6 +7,7 @@ var rest = context.JK.Rest(); var playbackControls = null; var recording = null; // deferred object + var $dialog = null; function resetForm() { @@ -16,6 +17,7 @@ } function beforeShow() { + $dialog.data('result', null); if (recording == null) { alert("recording data should not be null"); app.layout.closeDialog('recordingFinished'); @@ -104,6 +106,7 @@ logger.error("recording discard by user failed. recordingId=%o. reason: %o", recording.id, jqXHR.responseText); }) .always(function () { + $dialog.data('result', {keep:false}); app.layout.closeDialog('recordingFinished') registerDiscardRecordingHandlers(true); }) @@ -128,6 +131,7 @@ is_public: is_public }) .done(function () { + $dialog.data('result', {keep:true}); app.layout.closeDialog('recordingFinished'); context.JK.GA.trackMakeRecording(); }) @@ -231,6 +235,7 @@ app.bindDialog('recordingFinished', dialogBindings); + $dialog = $('#recording-finished-dialog') playbackControls = new context.JK.PlaybackControls($('#recording-finished-dialog .recording-controls')); registerStaticEvents(); diff --git a/web/app/assets/javascripts/dialog/whatsNextDialog.js b/web/app/assets/javascripts/dialog/whatsNextDialog.js deleted file mode 100644 index e7c5fd5f2..000000000 --- a/web/app/assets/javascripts/dialog/whatsNextDialog.js +++ /dev/null @@ -1,64 +0,0 @@ -(function (context, $) { - - "use strict"; - context.JK = context.JK || {}; - context.JK.WhatsNextDialog = function (app) { - var logger = context.JK.logger; - var rest = context.JK.Rest(); - var invitationDialog = null; - - - function registerEvents() { - - $('#whatsnext-dialog a.facebook-invite').on('click', function (e) { - invitationDialog.showFacebookDialog(e); - }); - - $('#whatsnext-dialog a.google-invite').on('click', function (e) { - invitationDialog.showGoogleDialog(); - }); - - $('#whatsnext-dialog a.email-invite').on('click', function (e) { - invitationDialog.showEmailDialog(); - }); - } - - function beforeShow() { - } - - function beforeHide() { - var $dontShowWhatsNext = $('#show_whats_next'); - - if ($dontShowWhatsNext.is(':checked')) { - rest.updateUser({show_whats_next: false}) - } - - } - - function initializeButtons() { - var dontShow = $('#show_whats_next'); - - context.JK.checkbox(dontShow); - } - - function initialize(invitationDialogInstance) { - var dialogBindings = { - 'beforeShow': beforeShow, - 'beforeHide': beforeHide - }; - - app.bindDialog('whatsNext', dialogBindings); - - registerEvents(); - - invitationDialog = invitationDialogInstance; - - initializeButtons(); - }; - - - this.initialize = initialize; - } - - return this; -})(window, jQuery); \ No newline at end of file diff --git a/web/app/assets/javascripts/fakeJamClient.js b/web/app/assets/javascripts/fakeJamClient.js index 393501e1b..294580721 100644 --- a/web/app/assets/javascripts/fakeJamClient.js +++ b/web/app/assets/javascripts/fakeJamClient.js @@ -336,6 +336,12 @@ dbg('AbortRecording'); fakeJamClientRecordings.AbortRecording(recordingId, errorReason, errorDetail); } + function OnTrySyncCommand(cmd) { + + } + function GetRecordingManagerState() { + return {running: false} + } function TestASIOLatency(s) { dbg('TestASIOLatency' + JSON.stringify(arguments)); } @@ -808,6 +814,8 @@ // Javascript Bridge seems to camel-case // Set the instance functions: this.AbortRecording = AbortRecording; + this.OnTrySyncCommand = OnTrySyncCommand; + this.GetRecordingManagerState = GetRecordingManagerState; this.GetASIODevices = GetASIODevices; this.GetOS = GetOS; this.GetOSAsString = GetOSAsString; diff --git a/web/app/assets/javascripts/feedHelper.js b/web/app/assets/javascripts/feedHelper.js index 18588f8b9..8dd78c9c7 100644 --- a/web/app/assets/javascripts/feedHelper.js +++ b/web/app/assets/javascripts/feedHelper.js @@ -9,6 +9,7 @@ var rest = new context.JK.Rest(); var EVENTS = context.JK.EVENTS; var ui = new context.JK.UIHelper(JK.app); + var recordingUtils = context.JK.RecordingUtils; var userId = null; var currentFeedPage = 0; var feedBatchSize = 10; @@ -424,6 +425,9 @@ logger.error("a recording in the feed should always have one claimed_recording") return; } + // pump some useful data about mixing into the feed item + feed.mix_info = recordingUtils.createMixInfo({state: feed.mix_state}) + var options = { feed_item: feed, candidate_claimed_recording: obtainCandidate(feed), @@ -433,6 +437,10 @@ var $feedItem = $(context._.template($('#template-feed-recording').html(), options, {variable: 'data'})); var $controls = $feedItem.find('.recording-controls'); + $controls.data('mix-state', feed.mix_info) // for recordingUtils helper methods + $controls.data('server-info', feed.mix) // for recordingUtils helper methods + $controls.data('view-context', 'feed') + $('.timeago', $feedItem).timeago(); context.JK.prettyPrintElements($('.duration', $feedItem)); context.JK.setInstrumentAssetPath($('.instrument-icon', $feedItem)); @@ -501,6 +509,8 @@ }).show(); } + context.JK.helpBubble($feedItem.find('.help-launcher'), recordingUtils.onMixHover, {}, {width:'450px', closeWhenOthersOpen: true, positions:['top', 'left', 'bottom', 'right'], offsetParent: $screen.parent()}) + // put the feed item on the page renderFeed($feedItem); diff --git a/web/app/assets/javascripts/feed_item_recording.js b/web/app/assets/javascripts/feed_item_recording.js index e9c138479..e9d1778de 100644 --- a/web/app/assets/javascripts/feed_item_recording.js +++ b/web/app/assets/javascripts/feed_item_recording.js @@ -6,10 +6,12 @@ context.JK.FeedItemRecording = function($parentElement, options){ var ui = new context.JK.UIHelper(JK.app); + var recordingUtils = context.JK.RecordingUtils; var claimedRecordingId = $parentElement.attr('data-claimed-recording-id'); var recordingId = $parentElement.attr('id'); var mode = $parentElement.attr('data-mode'); + var mixInfo = null; var $feedItem = $parentElement; var $name = $('.name', $feedItem); @@ -129,6 +131,12 @@ context.JK.bindHoverEvents($feedItem); //context.JK.bindProfileClickEvents($feedItem); + + mixInfo = recordingUtils.createMixInfo({state: options.mix_state}) + $controls.data('mix-state', mixInfo) // for recordingUtils helper methods + $controls.data('view-context', 'feed') + $controls.addClass(mixInfo.mixStateClass) + $status.text(mixInfo.mixStateMsg) } initialize(); diff --git a/web/app/assets/javascripts/globals.js b/web/app/assets/javascripts/globals.js index 0886cacf2..eb6fd0b27 100644 --- a/web/app/assets/javascripts/globals.js +++ b/web/app/assets/javascripts/globals.js @@ -35,7 +35,11 @@ RSVP_CANCELED : 'rsvp_canceled', USER_UPDATED : 'user_updated', SESSION_STARTED : 'session_started', - SESSION_ENDED : 'session_stopped' + SESSION_ENDED : 'session_stopped', + FILE_MANAGER_CMD_START : 'file_manager_cmd_start', + FILE_MANAGER_CMD_STOP : 'file_manager_cmd_stop', + FILE_MANAGER_CMD_PROGRESS : 'file_manager_cmd_progress', + FILE_MANAGER_CMD_ASAP_UPDATE : 'file_manager_cmd_asap_update' }; context.JK.ALERT_NAMES = { diff --git a/web/app/assets/javascripts/jam_rest.js b/web/app/assets/javascripts/jam_rest.js index 9c9cf107f..5c714bde5 100644 --- a/web/app/assets/javascripts/jam_rest.js +++ b/web/app/assets/javascripts/jam_rest.js @@ -840,7 +840,29 @@ }) } + function getUserSyncs(options) { + if(!options) { options = {}; } + var userId = getId(options) + return $.ajax({ + type: 'GET', + dataType: "json", + url: "/api/users/" + userId + "/syncs?" + $.param(options), + processData:false + }) + } + function getUserSync(options) { + if(!options) { options = {}; } + var userId = getId(options) + var userSyncId = options['user_sync_id'] + + return $.ajax({ + type: 'GET', + dataType: "json", + url: "/api/users/" + userId + "/syncs/" + userSyncId, + processData:false + }) + } function sendFriendRequest(app, userId, callback) { var url = "/api/users/" + context.JK.currentUserId + "/friend_requests"; @@ -979,6 +1001,18 @@ }) } + function getRecordedTrack(options) { + var recordingId = options["recording_id"]; + var trackId = options["track_id"]; + + return $.ajax({ + type: "GET", + dataType: "json", + contentType: 'application/json', + url: "/api/recordings/" + recordingId + "/tracks/" + trackId + }); + } + function getRecording(options) { var recordingId = options["id"]; @@ -1221,6 +1255,7 @@ }); } + function initialize() { return self; } @@ -1281,6 +1316,8 @@ this.getMusicianInvites = getMusicianInvites; this.postFeedback = postFeedback; this.getFeeds = getFeeds; + this.getUserSyncs = getUserSyncs; + this.getUserSync = getUserSync; this.serverHealthCheck = serverHealthCheck; this.sendFriendRequest = sendFriendRequest; this.getFriendRequest = getFriendRequest; @@ -1295,6 +1332,7 @@ this.startRecording = startRecording; this.stopRecording = stopRecording; this.getRecording = getRecording; + this.getRecordedTrack = getRecordedTrack; this.getClaimedRecordings = getClaimedRecordings; this.getClaimedRecording = getClaimedRecording; this.updateClaimedRecording = updateClaimedRecording; diff --git a/web/app/assets/javascripts/jquery.exists.js b/web/app/assets/javascripts/jquery.exists.js new file mode 100644 index 000000000..ff88124d0 --- /dev/null +++ b/web/app/assets/javascripts/jquery.exists.js @@ -0,0 +1 @@ +jQuery.fn.exists = function(){return this.length > 0;} diff --git a/web/app/assets/javascripts/landing/landing.js b/web/app/assets/javascripts/landing/landing.js index b8a0567dd..0afe196f0 100644 --- a/web/app/assets/javascripts/landing/landing.js +++ b/web/app/assets/javascripts/landing/landing.js @@ -18,6 +18,7 @@ //= require jquery.custom-protocol //= require jquery.ba-bbq //= require jquery.icheck +//= require jquery.exists //= require AAA_Log //= require AAC_underscore //= require alert diff --git a/web/app/assets/javascripts/notificationPanel.js b/web/app/assets/javascripts/notificationPanel.js index 08121983a..1e56483f3 100644 --- a/web/app/assets/javascripts/notificationPanel.js +++ b/web/app/assets/javascripts/notificationPanel.js @@ -345,6 +345,7 @@ else if (type === context.JK.MessageType.RECORDING_MASTER_MIX_COMPLETE) { $notification.find('#div-actions').hide(); + logger.debug("context.jamClient.OnDownloadAvailable!") context.jamClient.OnDownloadAvailable(); // poke backend, letting it know a download is available } diff --git a/web/app/assets/javascripts/paginator.js b/web/app/assets/javascripts/paginator.js index 7a2405460..963aee1e3 100644 --- a/web/app/assets/javascripts/paginator.js +++ b/web/app/assets/javascripts/paginator.js @@ -9,6 +9,8 @@ context.JK.Paginator = { + $templatePaginator: null, + /** returns a jquery object that encapsulates pagination markup. * It's left to the caller to append it to the page as they like. * @param pages the number of pages @@ -18,6 +20,16 @@ */ create:function(totalEntries, perPage, currentPage, onPageSelected) { + if(this.$templatePaginator === null) { + this.$templatePaginator = $('#template-paginator') + if(this.$templatePaginator.length == 0) { + throw "no #template-paginator" + } + } + + var $templatePaginator = this.$templatePaginator; + + function calculatePages(total, perPageValue) { return Math.ceil(total / perPageValue); } @@ -32,13 +44,13 @@ onPageSelected(targetPage) .done(function(data, textStatus, jqXHR) { - totalEntries = parseInt(jqXHR.getResponseHeader('total-entries')); + totalEntries = data.total_entries || parseInt(jqXHR.getResponseHeader('total-entries')); pages = calculatePages(totalEntries, perPage); options = { pages: pages, currentPage: targetPage }; // recreate the pagination, and - var newPaginator = $(context._.template($('#template-paginator').html(), options, { variable: 'data' })); + var newPaginator = $(context._.template($templatePaginator.html(), options, { variable: 'data' })); registerEvents(newPaginator); paginator.replaceWith(newPaginator); paginator = newPaginator; @@ -90,7 +102,7 @@ var options = { pages: pages, currentPage: currentPage }; - var paginator = $(context._.template($('#template-paginator').html(), options, { variable: 'data' })); + var paginator = $(context._.template($templatePaginator.html(), options, { variable: 'data' })); registerEvents(paginator); diff --git a/web/app/assets/javascripts/recordingManager.js b/web/app/assets/javascripts/recordingManager.js index 75de3e2ef..30b10da4c 100644 --- a/web/app/assets/javascripts/recordingManager.js +++ b/web/app/assets/javascripts/recordingManager.js @@ -2,121 +2,178 @@ * Recording Manager viewer * Although multiple instances could be made, only one should be */ -(function(context, $) { +(function (context, $) { - "use strict"; + "use strict"; - context.JK = context.JK || {}; + context.JK = context.JK || {}; - context.JK.RecordingManager = function(){ + context.JK.RecordingManager = function (app) { - var $parentElement = $('#recording-manager-viewer'); + var $parentElement = $('#recording-manager-viewer'); - var logger = context.JK.logger; - if($parentElement.length == 0) { - logger.debug("no $parentElement specified in RecordingManager"); - } + var logger = context.JK.logger; + var EVENTS = context.JK.EVENTS; - var $downloadCommand = $('#recording-manager-download', $parentElement); - var $downloadPercent = $('#recording-manager-download .percent', $parentElement); - var $uploadCommand = $('#recording-manager-upload', $parentElement); - var $uploadPercent = $('#recording-manager-upload .percent', $parentElement); - var $convertCommand = $('#recording-manager-convert', $parentElement); - var $convertPercent = $('#recording-manager-convert .percent', $parentElement); - - // keys come from backend - var lookup = { - SyncDownload: { command: $downloadCommand, percent: $downloadPercent }, - SyncUpload: { command: $uploadCommand, percent: $uploadPercent }, - SyncConvert: { command: $convertCommand, percent: $convertPercent } - } - - var $self = $(this); - - - function renderStartCommand($command) { - $command.css('visibility', 'visible'); - } - - function renderEndCommand($command) { - $command.css('visibility', 'hidden'); - } - - function renderPercentage($percent, value) { - $percent.text(Math.round(value * 100)); - } - - function onStartCommand(id, type) { - var command = lookup[type]; - if(!command) { return } - - var existingCommandId = command.command.data('command-id'); - - if(existingCommandId && existingCommandId != id) { - renderEndCommand(command.command); - } - - command.command.data('command-id', id); - renderStartCommand(command.command); - renderPercentage(command.percent, 0); - } - - function onStopCommand(id, type, success, reason, detail) { - var command = lookup[type]; - if(!command) { return } - - var existingCommandId = command.command.data('command-id'); - - if(!existingCommandId) { - command.command.data('command-id', id); - renderStartCommand(command.command); - } - else if(existingCommandId && existingCommandId != id) { - renderEndCommand(command.command); - command.command.data('command-id', id); - renderStartCommand(command.command); - } - - renderPercentage(command.percent, 1); - renderEndCommand(command.command); - command.command.data('command-id', null); - } - - function onCommandProgress(id, type, progress) { - var command = lookup[type]; - if(!command) { return } - - var existingCommandId = command.command.data('command-id'); - - if(!existingCommandId) { - command.command.data('command-id', id); - renderStartCommand(command.command); - } - else if(existingCommandId && existingCommandId != id) { - renderEndCommand(command.command); - command.command.data('command-id', id); - renderStartCommand(command.command); - } - - renderPercentage(command.percent, progress); - } - - function onCommandsChanged(id, type) { - - } - - context.JK.RecordingManagerCommandStart = onStartCommand; - context.JK.RecordingManagerCommandStop = onStopCommand; - context.JK.RecordingManagerCommandProgress = onCommandProgress; - context.JK.RecordingManagerCommandsChanged = onCommandsChanged; - - context.jamClient.RegisterRecordingManagerCallbacks( - "JK.RecordingManagerCommandStart", - "JK.RecordingManagerCommandProgress", - "JK.RecordingManagerCommandStop", - "JK.RecordingManagerCommandsChanged" - ) - - return this; + if ($parentElement.length == 0) { + logger.debug("no $parentElement specified in RecordingManager"); } + + var $downloadCommand = $('#recording-manager-download', $parentElement); + var $downloadPercent = $('#recording-manager-download .percent', $parentElement); + var $uploadCommand = $('#recording-manager-upload', $parentElement); + var $uploadPercent = $('#recording-manager-upload .percent', $parentElement); + var $convertCommand = $('#recording-manager-convert', $parentElement); + var $convertPercent = $('#recording-manager-convert .percent', $parentElement); + var $fileManager = $('#recording-manager-launcher', $parentElement); + + if($fileManager.length == 0) {throw "no file manager element"; } + + $downloadCommand.data('command-type', 'download') + $uploadCommand.data('command-type', 'upload') + $convertCommand.data('command-type', 'convert') + + // keys come from backend + var lookup = { + SyncDownload: { command: $downloadCommand, percent: $downloadPercent}, + SyncUpload: { command: $uploadCommand, percent: $uploadPercent}, + SyncConvert: { command: $convertCommand, percent: $convertPercent} + } + + var $self = $(this); + + if (context.jamClient.IsNativeClient()) { + $parentElement.addClass('native-client') + } + + function renderStartCommand($command) { + $command.css('visibility', 'visible').addClass('running'); + $parentElement.addClass('running') + $(document).triggerHandler(EVENTS.FILE_MANAGER_CMD_START, $command.data()) + } + + function renderEndCommand($command) { + $command.css('visibility', 'hidden').removeClass('running') + if ($parentElement.find('.recording-manager-command.running').length == 0) { + $parentElement.removeClass('running') + } + $(document).triggerHandler(EVENTS.FILE_MANAGER_CMD_STOP, $command.data()) + } + + function renderPercentage($command, $percent, value) { + var percentage = Math.round(value * 100) + if(percentage > 100) percentage = 100; + + $percent.text(percentage); + $command.data('percentage', percentage) + $(document).triggerHandler(EVENTS.FILE_MANAGER_CMD_PROGRESS, $command.data()) + } + + function onStartCommand(id, type, metadata) { + var command = lookup[type]; + if (!command) { + return + } + + var existingCommandId = command.command.data('command-id'); + + if (existingCommandId && existingCommandId != id) { + renderEndCommand(command.command); + } + + command.command.data('command-id', id) + command.command.data('command-metadata', metadata) + command.command.data('command-success', null); + command.command.data('command-reason', null); + command.command.data('command-detail', null); + + renderStartCommand(command.command); + renderPercentage(command.command, command.percent, 0); + } + + function onStopCommand(id, type, success, reason, detail) { + var command = lookup[type]; + if (!command) { + return + } + + var existingCommandId = command.command.data('command-id'); + + + if (!existingCommandId) { + command.command.data('command-id', id); + renderStartCommand(command.command); + } + else if (existingCommandId && existingCommandId != id) { + renderEndCommand(command.command); + command.command.data('command-id', id); + renderStartCommand(command.command); + } + + + command.command.data('command-success', success); + command.command.data('command-reason', reason); + command.command.data('command-detail', detail); + + renderPercentage(command.command, command.percent, 1); + renderEndCommand(command.command); + command.command.data('command-id', null); + } + + function onCommandProgress(id, type, progress) { + var command = lookup[type]; + if (!command) { + return + } + + var existingCommandId = command.command.data('command-id'); + + if (!existingCommandId) { + command.command.data('command-id', id); + renderStartCommand(command.command); + } + else if (existingCommandId && existingCommandId != id) { + renderEndCommand(command.command); + command.command.data('command-id', id); + renderStartCommand(command.command); + } + + renderPercentage(command.command, command.percent, progress); + } + + function onCommandsChanged(id, type) { + + } + + function onAsapCommandStatus(id, type, metadata, reason) { + $(document).triggerHandler(EVENTS.FILE_MANAGER_CMD_ASAP_UPDATE, {commandMetadata: metadata, commandId: id, commandReason: reason}) + } + + function onClick() { + app.layout.showDialog('all-syncs-dialog') + return false; + } + + $fileManager.click(onClick) + $convertCommand.click(onClick) + $downloadCommand.click(onClick) + $uploadCommand.click(onClick) + + + context.JK.RecordingManagerCommandStart = onStartCommand; + context.JK.RecordingManagerCommandStop = onStopCommand; + context.JK.RecordingManagerCommandProgress = onCommandProgress; + context.JK.RecordingManagerCommandsChanged = onCommandsChanged; + context.JK.RecordingManagerAsapCommandStatus = onAsapCommandStatus; + + context.jamClient.RegisterRecordingManagerCallbacks( + "JK.RecordingManagerCommandStart", + "JK.RecordingManagerCommandProgress", + "JK.RecordingManagerCommandStop", + "JK.RecordingManagerCommandsChanged", + "JK.RecordingManagerAsapCommandStatus" + ) + + return this; + } })(window, jQuery); \ No newline at end of file diff --git a/web/app/assets/javascripts/recording_utils.js.coffee b/web/app/assets/javascripts/recording_utils.js.coffee new file mode 100644 index 000000000..89c556a7c --- /dev/null +++ b/web/app/assets/javascripts/recording_utils.js.coffee @@ -0,0 +1,141 @@ +# +# Common utility functions. +# + +$ = jQuery +context = window +context.JK ||= {}; + +class RecordingUtils + constructor: () -> + @logger = context.JK.logger + + init: () => + @templateHoverMix = $('#template-sync-viewer-hover-mix') + + mixStateErrorDescription: (mix) => + summary = '' + if mix.error.error_reason == 'mix-timeout' + summary = "The mix was started, but it has taken too long to for it to finish. The mix was started #{$.timeago(mix.error.error_detail)}. JamKazam employees have been notified of the issue." + else if mix.error.error_reason == 'unknown' + summary = "The reason the mix failed is unknown. JamKazam employees have been notified of the issue. " + else if mix.error.error_reason == 'unable to download' + summary = "The mixer could not fetch one of the tracks from JamKazam servers. JamKazam employees have been notified of the issue." + else if mix.error.error_reason == 'unable-parse-error-out' + summary = "The mixer failed, but it could not report what went wrong. JamKazam employees have been notified of the issue." + else if mix.error.error_reason == 'postback-ogg-mix-to-s3' or mix.error_reason == 'postback-mp3-mix-to-s3' + summary = "The mixer made the mix, but could not publish it. JamKazam employees have been notified of the issue." + else if mix.error.error_reason == 'unhandled-job-exception' + summary = "The mixer had an error while mixing. JamKazam employees have been notified of the issue." + else if mix.error.error_reason == 'unhandled-job-exception' + summary = "The mixer had an error while mixing. JamKazam employees have been notified of the issue." + summary + + mixStateDefinition: (mixState) => + + definition = switch mixState + when 'still-uploading' then "STILL UPLOADING means the you or others in the recording have not uploaded enough information yet to make a mix." + when 'stream-mix' then "STREAM MIX means a user's real-time mix is available to listen to." + when 'discarded' then "DISCARDED means you chose to not keep this recording when the recording was over." + when 'mixed' then "MIXED means all tracks have been uploaded, and a final mix has been made." + when 'mixing' then "MIXING means all tracks have been uploaded, and the final mix is currently being made." + when 'waiting-to-mix' then "MIX QUEUED means all tracks have been uploaded, and the final mix should start being made very soon." + when 'error' then 'MIX FAILED means something went wrong with the mixer after all tracks were uploaded.' + else 'There is no help for this state' + + createMixInfo: (mix) => + mixStateMsg = 'UNKNOWN' + mixStateClass = 'unknown' + mixState = 'unknown' + + if mix? and mix.state? and !mix.fake + + # the '.download' property is only used for personalized views of a mix. + if !mix.download? mix.download.should_download + + if mix.state == 'error' + mixStateMsg = 'MIX FAILED' + mixStateClass = 'error' + mixState = 'error' + else if mix.state == 'stream-mix' + mixStateMsg = 'STREAM MIX' + mixStateClass = 'stream-mix' + mixState = 'stream-mix' + else if mix.state == 'waiting-to-mix' + mixStateMsg = 'MIX QUEUED' + mixStateClass = 'waiting-to-mix' + mixState = 'waiting-to-mix' + else if mix.state == 'mixing' + mixStateMsg = 'MIXING' + mixStateClass = 'mixing' + mixState = 'mixing' + else if mix.state == 'mixed' + mixStateMsg = 'MIXED' + mixStateClass = 'mixed' + mixState = 'mixed' + else + mixStateMsg = mix.state + mixStateClass = 'unknown' + mixState = 'unknown' + + + else + mixStateMsg = 'DISCARDED' + mixStateClass = 'discarded' + mixState = 'discarded' + else + mixStateMsg = 'STILL UPLOADING' + mixStateClass = 'still-uploading' + mixState = 'still-uploading' + + return { + mixStateMsg: mixStateMsg, + mixStateClass: mixStateClass, + mixState: mixState, + isError: mixState == 'error' + } + + onMixHover: () -> + $mix = $(this).closest('.mix') + mixStateInfo = $mix.data('mix-state') + viewContext = $mix.data('view-context') + if !mixStateInfo? # to support feed and syncs both using this same code, check .mix-state too + $mixState = $mix.find('.mix-state') + mixStateInfo = $mixState.data('mix-state') + + if !mixStateInfo? + logger.error('no mix-state anywhere', this) + throw 'no mix-state' + + mixStateMsg = mixStateInfo.mixStateMsg + mixStateClass = mixStateInfo.mixStateClass + mixState = mixStateInfo.mixState + + serverInfo = $mix.data('server-info') + + # lie if this is a virtualized mix (i.e., mix is created after recording is made) + mixState = 'still-uploading' if !serverInfo? or serverInfo.fake + + summary = '' + if mixState == 'still-uploading' + #summary = "No one in the recording has uploaded any files yet. Once tracks have been uploaded, a mix will be made." + else if mixState == 'discarded' + summary = "When this recording was made, you elected to not keep it. But at least one other person elected to keep it so your tracks are needed for the mix." + else if mixState == 'mixed' + #summary = 'All tracks have been uploaded, and a final mix has been made.' + else if mixState == 'stream-mix' + summary = 'A STREAM MIX the mix that someone heard in their headphones as the actual recording was made. In the case of a recording involving two or more people, it will contain any imperfections caused by the internet. Once the final, high-quality mix is available, we will automatically replace the stream mix with it.' + else if mixState == 'mixing' + #summary = 'All tracks have been uploaded, and the final mix is currently being made.' + else if mixState == 'waiting-to-mix' + #summary = 'All tracks have been uploaded, and the finale mix should start being made very soon.' + else if mixState == 'error' + summary = context.JK.RecordingUtils.mixStateErrorDescription(serverInfo) + + mixStateDefinition = context.JK.RecordingUtils.mixStateDefinition(mixState) + + context._.template(context.JK.RecordingUtils.templateHoverMix.html(), {summary: summary, mixStateDefinition: mixStateDefinition, mixStateMsg: mixStateMsg, mixStateClass: mixStateClass, viewContext: viewContext}, {variable: 'data'}) + + +# global instance +context.JK.RecordingUtils = new RecordingUtils() \ No newline at end of file diff --git a/web/app/assets/javascripts/session.js b/web/app/assets/javascripts/session.js index 5add060ae..a49148209 100644 --- a/web/app/assets/javascripts/session.js +++ b/web/app/assets/javascripts/session.js @@ -4,6 +4,7 @@ context.JK = context.JK || {}; context.JK.SessionScreen = function(app) { + var EVENTS = context.JK.EVENTS; var gearUtils = context.JK.GearUtils; var sessionUtils = context.JK.SessionUtils; var logger = context.JK.logger; @@ -35,7 +36,8 @@ var rateSessionDialog = null; var friendInput = null; var sessionPageDone = null; - + var $recordingManagerViewer = null; + var $screen = null; var rest = context.JK.Rest(); var RENDER_SESSION_DELAY = 750; // When I need to render a session, I have to wait a bit for the mixers to be there. @@ -1348,7 +1350,11 @@ rest.getRecording( {id: recordingId} ) .done(function(recording) { recordingFinishedDialog.setRecording(recording); - app.layout.showDialog('recordingFinished'); + app.layout.showDialog('recordingFinished').one(EVENTS.DIALOG_CLOSED, function(e, data) { + if(data.result && data.result.keep){ + context.JK.prodBubble($recordingManagerViewer, 'file-manager-poke', {}, {positions:['top', 'left', 'right', 'bottom'], offsetParent: $screen.parent()}) + } + }) }) .fail(app.ajaxError); } @@ -1462,6 +1468,8 @@ }; app.bindScreen('session', screenBindings); + $recordingManagerViewer = $('#recording-manager-viewer'); + $screen = $('#session-screen'); // make sure no previous plays are still going on by accident context.jamClient.SessionStopPlay(); if(context.jamClient.SessionRemoveAllPlayTracks) { diff --git a/web/app/assets/javascripts/sync_viewer.js.coffee b/web/app/assets/javascripts/sync_viewer.js.coffee new file mode 100644 index 000000000..728a892b3 --- /dev/null +++ b/web/app/assets/javascripts/sync_viewer.js.coffee @@ -0,0 +1,892 @@ +$ = jQuery +context = window +context.JK ||= {}; + +context.JK.SyncViewer = class SyncViewer + constructor: (@app) -> + @EVENTS = context.JK.EVENTS + @rest = context.JK.Rest() + @logger = context.JK.logger + @recordingUtils = context.JK.RecordingUtils + @since = 0 + @limit = 20 + @showing = false + @downloadCommandId = null + @downloadMetadata = null + @uploadCommandId = null + @uploadMetadata = null; + + init: () => + @root = $($('#template-sync-viewer').html()) + @inProgress = @root.find('.in-progress') + @downloadProgress = @inProgress.find('.download-progress') + @uploadProgress = @inProgress.find('.upload-progress') + @list = @root.find('.list') + @logList = @root.find('.log-list') + @templateRecordedTrack = $('#template-sync-viewer-recorded-track') + @templateStreamMix = $('#template-sync-viewer-stream-mix') + @templateMix = $('#template-sync-viewer-mix') + @templateNoSyncs = $('#template-sync-viewer-no-syncs') + @templateRecordingWrapperDetails = $('#template-sync-viewer-recording-wrapper-details') + @templateHoverRecordedTrack = $('#template-sync-viewer-hover-recorded-track') + @templateHoverMix = $('#template-sync-viewer-hover-mix') + @templateDownloadReset = $('#template-sync-viewer-download-progress-reset') + @templateUploadReset = $('#template-sync-viewer-upload-progress-reset') + @templateGenericCommand = $('#template-sync-viewer-generic-command') + @templateRecordedTrackCommand = $('#template-sync-viewer-recorded-track-command') + @templateLogItem = $('#template-sync-viewer-log-item') + @tabSelectors = @root.find('.dialog-tabs .tab') + @tabs = @root.find('.tab-content') + @logBadge = @tabSelectors.find('.badge') + @paginator = @root.find('.paginator-holder') + + @uploadStates = { + unknown: 'unknown', + too_many_upload_failures: 'too-many-upload-failures', + me_upload_soon: 'me-upload-soon', + them_upload_soon: 'them-upload-soon' + missing: 'missing', + me_uploaded: 'me-uploaded', + them_uploaded: 'them-uploaded' + } + @clientStates = { + unknown: 'unknown', + too_many_uploads: 'too-many-downloads', + hq: 'hq', + sq: 'sq', + missing: 'missing', + discarded: 'discarded' + } + + throw "no sync-viewer" if not @root.exists() + throw "no in-progress" if not @inProgress.exists() + throw "no list" if not @list.exists() + throw "no recorded track template" if not @templateRecordedTrack.exists() + throw "no stream mix template" if not @templateStreamMix.exists() + throw "no empty syncs template" if not @templateNoSyncs.exists() + + $(document).on(@EVENTS.FILE_MANAGER_CMD_START, this.fileManagerCmdStart) + $(document).on(@EVENTS.FILE_MANAGER_CMD_STOP, this.fileManagerCmdStop) + $(document).on(@EVENTS.FILE_MANAGER_CMD_PROGRESS, this.fileManagerCmdProgress) + $(document).on(@EVENTS.FILE_MANAGER_CMD_ASAP_UPDATE, this.fileManagerAsapCommandStatus) + + @tabSelectors.click((e) => + $tabSelected = $(e.target).closest('a.tab') + purpose = $tabSelected.attr('purpose') + + $otherTabSelectors = @tabSelectors.not('[purpose="' + purpose + '"]') + $otherTabs = @tabs.not('[purpose="' + purpose + '"]') + $tab = @tabs.filter('[purpose="' + purpose + '"]') + + $otherTabs.hide(); + $tab.show() + + $otherTabSelectors.removeClass('selected'); + $tabSelected.addClass('selected'); + + if purpose == 'log' + @logBadge.hide() + ) + + onShow: () => + @showing = true + this.load() + + onHide: () => + @showing = false + #$(document).off(@EVENTS.FILE_MANAGER_CMD_START, this.fileManagerCmdStart) + #$(document).off(@EVENTS.FILE_MANAGER_CMD_STOP, this.fileManagerCmdStop) + #$(document).off(@EVENTS.FILE_MANAGER_CMD_PROGRESS, this.fileManagerCmdProgress) + + getUserSyncs: (page) => + @rest.getUserSyncs({since: page * @limit, limit: @limit}) + .done(this.processUserSyncs) + + + load: () => + @list.empty() + @since = 0 + + this.renderHeader() + + this.getUserSyncs(0) + .done((response) => + $paginator = context.JK.Paginator.create(response.total_entries, @limit, 0, this.getUserSyncs) + @paginator.empty().append($paginator); + ) + + + renderHeader: () => + recordingManagerState = context.jamClient.GetRecordingManagerState() + if recordingManagerState.running + @uploadProgress.removeClass('quiet paused busy') + @downloadProgress.removeClass('quiet paused busy') + + if recordingManagerState.current_download + @downloadProgress.addClass('busy') + else + @downloadProgress.addClass('quiet') + @downloadProgress.find('.busy').empty() + + if recordingManagerState.current_upload + @uploadProgress.addClass('busy') + else + @uploadProgress.addClass('quiet') + @uploadProgress.find('.busy').empty() + else + @downloadProgress.removeClass('quiet paused busy').addClass('paused') + @uploadProgress.removeClass('quiet paused busy').addClass('paused') + @downloadProgress.find('.busy').empty() + @uploadProgress.find('.busy').empty() + + updateMixState: ($mix) => + serverInfo = $mix.data('server-info') + + mixInfo = @recordingUtils.createMixInfo(serverInfo) + + $mixState = $mix.find('.mix-state') + $mixStateMsg = $mixState.find('.msg') + $mixStateProgress = $mixState.find('.progress') + $mixState.removeClass('still-uploading discarded unknown mixed mixing waiting-to-mix error stream-mix').addClass(mixInfo.mixStateClass).attr('data-state', mixInfo.mixState).data('mix-state', mixInfo) + $mixStateMsg.text(mixInfo.mixStateMsg) + $mixStateProgress.css('width', '0') + + updateStreamMixState: ($streamMix) => + clientInfo = $streamMix.data('client-info') + serverInfo = $streamMix.data('server-info') + + # determine client state + clientStateMsg = 'UNKNOWN' + clientStateClass = 'unknown' + clientState = @clientStates.unknown + + if clientInfo? + if clientInfo.local_state == 'COMPRESSED' + clientStateMsg = 'STREAM QUALITY' + clientStateClass = 'sq' + clientState = @clientStates.sq + else if clientInfo.local_state == 'UNCOMPRESSED' + clientStateMsg = 'STREAM QUALITY' + clientStateClass = 'sq' + clientState = @clientStates.sq + else if clientInfo.local_state == 'MISSING' + clientStateMsg = 'MISSING' + clientStateClass = 'missing' + clientState = @clientStates.missing + + # determine upload state + uploadStateMsg = 'UNKNOWN' + uploadStateClass = 'unknown' + uploadState = @uploadStates.unknown + + if !serverInfo.fully_uploaded + if serverInfo.upload.too_many_upload_failures + uploadStateMsg = 'UPLOAD FAILURE' + uploadStateClass = 'error' + uploadState = @uploadStates.too_many_upload_failures + else + if clientInfo? + if clientInfo.local_state == 'UNCOMPRESSED' or clientInfo.local_state == 'COMPRESSED' + uploadStateMsg = 'PENDING UPLOAD' + uploadStateClass = 'upload-soon' + uploadState = @uploadStates.me_upload_soon + else + uploadStateMsg = 'MISSING' + uploadStateClass = 'missing' + uploadState = @uploadStates.missing + else + uploadStateMsg = 'MISSING' + uploadStateClass = 'missing' + uploadState = @uploadStates.missing + else + uploadStateMsg = 'UPLOADED' + uploadStateClass = 'uploaded' + uploadState = @uploadStates.me_uploaded + + $clientState = $streamMix.find('.client-state') + $clientStateMsg = $clientState.find('.msg') + $clientStateProgress = $clientState.find('.progress') + $uploadState = $streamMix.find('.upload-state') + $uploadStateMsg = $uploadState.find('.msg') + $uploadStateProgress = $uploadState.find('.progress') + + $clientState.removeClass('discarded missing sq hq unknown error').addClass(clientStateClass).attr('data-state', clientState).data('custom-class', clientStateClass) + $clientStateMsg.text(clientStateMsg) + $clientStateProgress.css('width', '0') + $uploadState.removeClass('upload-soon error unknown missing uploaded').addClass(uploadStateClass).attr('data-state', uploadState).data('custom-class', uploadStateClass) + $uploadStateMsg.text(uploadStateMsg) + $uploadStateProgress.css('width', '0') + + updateTrackState: ($track) => + clientInfo = $track.data('client-info') + serverInfo = $track.data('server-info') + + # determine client state + clientStateMsg = 'UNKNOWN' + clientStateClass = 'unknown' + clientState = @clientStates.unknown + + if serverInfo.download.should_download + if serverInfo.download.too_many_downloads + clientStateMsg = 'EXCESS DOWNLOADS' + clientStateClass = 'error' + clientState = @clientStates.too_many_uploads + else + if clientInfo? + if clientInfo.local_state == 'HQ' + clientStateMsg = 'HIGHEST QUALITY' + clientStateClass = 'hq' + clientState = @clientStates.hq + else + clientStateMsg = 'STREAM QUALITY' + clientStateClass = 'sq' + clientState = @clientStates.sq + else + clientStateMsg = 'MISSING' + clientStateClass = 'missing' + clientState = @clientStates.missing + else + clientStateClass = 'DISCARDED' + clientStateClass = 'discarded' + clientState = @clientStates.discarded + + # determine upload state + uploadStateMsg = 'UNKNOWN' + uploadStateClass = 'unknown' + uploadState = @uploadStates.unknown + + if !serverInfo.fully_uploaded + if serverInfo.upload.too_many_upload_failures + uploadStateMsg = 'UPLOAD FAILURE' + uploadStateClass = 'error' + uploadState = @uploadStates.too_many_upload_failures + else + if serverInfo.user.id == context.JK.currentUserId + if clientInfo? + if clientInfo.local_state == 'HQ' + uploadStateMsg = 'PENDING UPLOAD' + uploadStateClass = 'upload-soon' + uploadState = @uploadStates.me_upload_soon + else + uploadStateMsg = 'MISSING' + uploadStateClass = 'missing' + uploadState = @uploadStates.missing + else + uploadStateMsg = 'MISSING' + uploadStateClass = 'missing' + uploadState = @uploadStates.missing + else + uploadStateMsg = 'PENDING UPLOAD' + uploadStateClass = 'upload-soon' + uploadState = @uploadStates.them_upload_soon + else + uploadStateMsg = 'UPLOADED' + uploadStateClass = 'uploaded' + if serverInfo.user.id == context.JK.currentUserId + uploadState = @uploadStates.me_uploaded + else + uploadState = @uploadStates.them_uploaded + + $clientState = $track.find('.client-state') + $clientStateMsg = $clientState.find('.msg') + $clientStateProgress = $clientState.find('.progress') + $uploadState = $track.find('.upload-state') + $uploadStateMsg = $uploadState.find('.msg') + $uploadStateProgress = $uploadState.find('.progress') + + $clientState.removeClass('discarded missing sq hq unknown error').addClass(clientStateClass).attr('data-state', clientState).data('custom-class', clientStateClass) + $clientStateMsg.text(clientStateMsg) + $clientStateProgress.css('width', '0') + $uploadState.removeClass('upload-soon error unknown missing uploaded').addClass(uploadStateClass).attr('data-state', uploadState).data('custom-class', uploadStateClass) + $uploadStateMsg.text(uploadStateMsg) + $uploadStateProgress.css('width', '0') + + associateClientInfo: (recording) => + for clientInfo in recording.local_tracks + $track = @list.find(".recorded-track[data-recording-id='#{recording.recording_id}'][data-client-track-id='#{clientInfo.client_track_id}']") + $track.data('client-info', clientInfo) + + $track = @list.find(".mix[data-recording-id='#{recording.recording_id}']") + $track.data('client-info', recording.mix) + + $track = @list.find(".stream-mix[data-recording-id='#{recording.recording_id}']") + $track.data('client-info', recording.stream_mix) + + displayStreamMixHover: ($streamMix) => + $clientState = $streamMix.find('.client-state') + $clientStateMsg = $clientState.find('.msg') + clientStateClass = $clientState.data('custom-class') + clientState = $clientState.attr('data-state') + clientInfo = $streamMix.data('client-info') + + $uploadState = $streamMix.find('.upload-state') + $uploadStateMsg = $uploadState.find('.msg') + uploadStateClass = $uploadState.data('custom-class') + uploadState = $uploadState.attr('data-state') + serverInfo = $streamMix.data('server-info') + + # decide on special case strings first + + summary = '' + if clientState == @clientStates.sq && uploadState == @uploadStates.me_upload_soon + summary = "We will attempt to upload your stream mix so that others can hear the recording on the JamKazam site, even before the final mix is done. It will upload shortly." + else if clientState == @clientStates.sq && uploadState == @uploadStates.me_uploaded + # we have the SQ version, and the other user has uploaded the HQ version... it's coming soon! + summary = "We already uploaded your stream mix so that others can hear the recording on the JamKazam site, even before the final mix is done. Since it's uploaded, there is nothing else left to do with the stream mix... you're all done!" + else if clientState == @clientStates.missing + summary = "You do not have the stream mix on your computer anymore. This can happen if you change the computer that you run JamKazam on. It's important to note that once a final mix for the recording is available, there is no value in the stream mix." + + clientStateDefinition = switch clientState + when @clientStates.sq then "The stream mix is always STREAM QUALITY, because it's the version of the recording that you heard in your earphones as you made the recording." + when @clientStates.missing then "MISSING means you do not have the stream mix anymore." + else 'There is no help for this state' + + uploadStateDefinition = switch uploadState + when @uploadStates.too_many_upload_failures then "Failed attempts at uploading this stream mix has happened an unusually large times. No more uploads will be attempted." + when @uploadStates.me_upload_soon then "PENDING UPLOAD means your JamKazam application will upload this stream mix soon." + when @uploadStates.me_uploaded then "UPLOADED means you have already uploaded this stream mix." + when @uploadStates.missing then "MISSING means your JamKazam application does not have this stream mix, and the server does not either." + + context._.template(@templateHoverRecordedTrack.html(), + {summary: summary, + clientStateDefinition: clientStateDefinition, + uploadStateDefinition: uploadStateDefinition, + clientStateMsg: $clientStateMsg.text(), + uploadStateMsg: $uploadStateMsg.text(), + clientStateClass: clientStateClass, + uploadStateClass: uploadStateClass} + {variable: 'data'}) + + displayTrackHover: ($recordedTrack) => + $clientState = $recordedTrack.find('.client-state') + $clientStateMsg = $clientState.find('.msg') + clientStateClass = $clientState.data('custom-class') + clientState = $clientState.attr('data-state') + clientInfo = $recordedTrack.data('client-info') + + $uploadState = $recordedTrack.find('.upload-state') + $uploadStateMsg = $uploadState.find('.msg') + uploadStateClass = $uploadState.data('custom-class') + uploadState = $uploadState.attr('data-state') + serverInfo = $recordedTrack.data('server-info') + + # decide on special case strings first + + summary = '' + if clientState == @clientStates.sq && uploadState == @uploadStates.them_upload_soon + # we have the SQ version, and the other user hasn't uploaded it yet + summary = "#{serverInfo.user.name} has not yet uploaded the high-quality version of this track. Once he or she does, JamKazam will download it and replace your stream-quality version." + else if clientState == @clientStates.missing && uploadState == @uploadStates.them_upload_soon + # we don't have any version of the track at all, and the other user hasn't uploaded it yet + summary = "#{serverInfo.user.name} has not yet uploaded the high-quality version of this track. Once he or she does, JamKazam will download it and this track will no longer be missing." + else if clientState == @clientStates.sq && uploadState == @uploadStates.them_uploaded + # we have the SQ version, and the other user has uploaded the HQ version... it's coming soon! + summary = "#{serverInfo.user.name} has uploaded the high-quality version of this track. JamKazam will soon download it and replace your stream-quality version." + else if clientState == @clientStates.missing && uploadState == @uploadStates.them_uploaded + # we have no version of the track at all, and the other user has uploaded the HQ version... it's coming soon! + summary = "#{serverInfo.user.name} has uploaded the high-quality version of this track. JamKazam will soon restore it and then this track will no longer be missing." + else if clientState == @clientStates.sq && uploadState == @uploadStates.me_uploaded + # we have the SQ version, and the other user has uploaded the HQ version... it's coming soon! + summary = "You have previously uploaded the high-quality version of this track. JamKazam will soon restore it and replace your stream-quality version." + else if clientState == @clientStates.missing && uploadState == @uploadStates.me_uploaded + # we have no version of the track at all, and the other user has uploaded the HQ version... it's coming soon! + summary = "You have previously uploaded the high-quality version of this track. JamKazam will soon restore it and then this track will no longer be missing." + else if clientState == @clientStates.discarded && (uploadState == @uploadStates.me_uploaded or uploadState == @uploadStates.them_uploaded) + # we decided not to keep the recording... so it's important to clarify why they are seeing it at all + summary = "When this recording was made, you elected to not keep it. JamKazam already uploaded your high-quality tracks for the recording, because at least one other person decided to keep the recording and needs your tracks to make a high-quality mix." + else if clientState == @clientStates.discarded + # we decided not to keep the recording... so it's important to clarify why they are seeing it at all + summary = "When this recording was made, you elected to not keep it. JamKazam will still try to upload your high-quality tracks for the recording, because at least one other person decided to keep the recording and needs your tracks to make a high-quality mix." + else if clientState == @clientStates.hq and ( uploadState == @uploadStates.them_uploaded or uploadState == @uploadStates.me_uploaded ) + summary = "Both you and the JamKazam server have the high-quality version of this track. Once all the other tracks for this recording are also synchronized, then the final mix can be made." + + clientStateDefinition = switch clientState + when @clientStates.too_many_downloads then "This track has been downloaded a unusually large number of times. No more downloads are allowed." + when @clientStates.hq then "HIGHEST QUALITY means you have the original version of this track, as recorded by the user that made it." + when @clientStates.sq then "STREAM QUALITY means you have the version of the track that you received over the internet in real-time." + when @clientStates.missing then "MISSING means you do not have this track anymore." + when @clientStates.discarded then "DISCARDED means you chose to not keep this recording when the recording was over." + else 'There is no help for this state' + + uploadStateDefinition = switch uploadState + when @uploadStates.too_many_upload_failures then "Failed attempts at uploading this track has happened an unusually large times. No more uploads will be attempted." + when @uploadStates.me_upload_soon then "PENDING UPLOAD means your JamKazam application will upload this track soon." + when @uploadStates.them_up_soon then "PENDING UPLOAD means #{serverInfo.user.name} will upload this track soon." + when @uploadStates.me_uploaded then "UPLOADED means you have already uploaded this track." + when @uploadStates.them_uploaded then "UPLOADED means #{serverInfo.user.name} has already uploaded this track." + when @uploadStates.missing then "MISSING means your JamKazam application does not have this track, and the server does not either." + + context._.template(@templateHoverRecordedTrack.html(), + {summary: summary, + clientStateDefinition: clientStateDefinition, + uploadStateDefinition: uploadStateDefinition, + clientStateMsg: $clientStateMsg.text(), + uploadStateMsg: $uploadStateMsg.text(), + clientStateClass: clientStateClass, + uploadStateClass: uploadStateClass} + {variable: 'data'}) + + onHoverOfStateIndicator: () -> + $recordedTrack = $(this).closest('.recorded-track.sync') + self = $recordedTrack.data('sync-viewer') + self.displayTrackHover($recordedTrack) + + onStreamMixHover: () -> + $streamMix = $(this).closest('.stream-mix.sync') + self = $streamMix.data('sync-viewer') + self.displayStreamMixHover($streamMix) + + resetDownloadProgress: () => + @downloadProgress + + resetUploadProgress: () => + @uploadProgress + + sendCommand: ($retry, cmd) => + + if context.JK.CurrentSessionModel and context.JK.CurrentSessionModel.inSession() + context.JK.confirmBubble($retry, 'sync-viewer-paused', {}, {offsetParent: $retry.closest('.dialog')}) + else + context.jamClient.OnTrySyncCommand(cmd) + context.JK.confirmBubble($retry, 'sync-viewer-retry', {}, {offsetParent: $retry.closest('.dialog')}) + + + retryDownloadRecordedTrack: (e) => + $retry = $(e.target) + $track = $retry.closest('.recorded-track') + serverInfo = $track.data('server-info') + + this.sendCommand($retry, { + type: 'recorded_track', + action: 'download' + queue: 'download', + recording_id: serverInfo.recording_id + track_id: serverInfo.client_track_id + }) + + return false + + retryUploadRecordedTrack: (e) => + $retry = $(e.target) + $track = $retry.closest('.recorded-track') + serverInfo = $track.data('server-info') + + this.sendCommand($retry, { + type: 'recorded_track', + action: 'upload' + queue: 'upload', + recording_id: serverInfo.recording_id + track_id: serverInfo.client_track_id + }) + + return false + + createMix: (userSync) => + recordingInfo = null + if userSync == 'fake' + recordingInfo = arguments[1] + userSync = { recording_id: recordingInfo.id, duration: recordingInfo.duration, fake:true } + $mix = $(context._.template(@templateMix.html(), userSync, {variable: 'data'})) + else + $mix = $(context._.template(@templateMix.html(), userSync, {variable: 'data'})) + + $mix.data('server-info', userSync) + $mix.data('sync-viewer', this) + $mix.data('view-context', 'sync') + $mixState = $mix.find('.mix-state') + this.updateMixState($mix) + context.JK.hoverBubble($mixState, @recordingUtils.onMixHover, {width:'450px', closeWhenOthersOpen: true, positions:['left'], trigger:['hoverIntent', 'none']}) + $mix + + createTrack: (userSync) => + $track = $(context._.template(@templateRecordedTrack.html(), userSync, {variable: 'data'})) + $track.data('server-info', userSync) + $track.data('sync-viewer', this) + $clientState = $track.find('.client-state') + $uploadState = $track.find('.upload-state') + $clientState.find('.retry').click(this.retryDownloadRecordedTrack) + $uploadState.find('.retry').click(this.retryUploadRecordedTrack) + context.JK.bindHoverEvents($track) + context.JK.bindInstrumentHover($track, {positions:['top'], shrinkToFit: true}); + context.JK.hoverBubble($clientState, this.onHoverOfStateIndicator, {width:'450px', closeWhenOthersOpen: true, positions:['left']}) + context.JK.hoverBubble($uploadState, this.onHoverOfStateIndicator, {width:'450px', closeWhenOthersOpen: true, positions:['right']}) + $clientState.addClass('is-native-client') if context.jamClient.IsNativeClient() + $uploadState.addClass('is-native-client') if context.jamClient.IsNativeClient() + $track + + createStreamMix: (userSync) => + $track = $(context._.template(@templateStreamMix.html(), userSync, {variable: 'data'})) + $track.data('server-info', userSync) + $track.data('sync-viewer', this) + $clientState = $track.find('.client-state') + $uploadState = $track.find('.upload-state') + $uploadState.find('.retry').click(this.retryUploadRecordedTrack) + context.JK.hoverBubble($clientState, this.onStreamMixHover, {width:'450px', closeWhenOthersOpen: true, positions:['left']}) + context.JK.hoverBubble($uploadState, this.onStreamMixHover, {width:'450px', closeWhenOthersOpen: true, positions:['right']}) + $clientState.addClass('is-native-client') if context.jamClient.IsNativeClient() + $uploadState.addClass('is-native-client') if context.jamClient.IsNativeClient() + $track + + createRecordingWrapper: ($toWrap, recordingInfo) => + recordingInfo.recording_landing_url = "/recordings/#{recordingInfo.id}" + $wrapperDetails = $(context._.template(@templateRecordingWrapperDetails.html(), recordingInfo, {variable: 'data'})) + $wrapper = $('
') + $toWrap.wrapAll($wrapper) + $wrapper = $toWrap.closest('.recording-holder') + $wrapperDetails.prependTo($wrapper) + $mix = $wrapper.find('.mix.sync') + if $mix.length == 0 + # create a virtual mix so that the UI is consistent + $wrapper.append(this.createMix('fake', recordingInfo)) + + separateByRecording: () => + $recordedTracks = @list.find('.sync') + + currentRecordingId = null; + + queue = $([]); + + for recordedTrack in $recordedTracks + $recordedTrack = $(recordedTrack) + recordingId = $recordedTrack.attr('data-recording-id') + if recordingId != currentRecordingId + if queue.length > 0 + this.createRecordingWrapper(queue, $(queue.get(0)).data('server-info').recording) + queue = $([]) + + currentRecordingId = recordingId + queue = queue.add(recordedTrack) + + if queue.length > 0 + this.createRecordingWrapper(queue, $(queue.get(0)).data('server-info').recording) + + processUserSyncs: (response) => + + @list.empty() + + # check if no entries + if @since == 0 and response.entries.length == 0 + @list.append(context._.template(@templateNoSyncs.html(), {}, {variable: 'data'})) + else + recordings = {} # collect all unique recording + for userSync in response.entries + if userSync.type == 'recorded_track' + @list.append(this.createTrack(userSync)) + else if userSync.type == 'mix' + @list.append(this.createMix(userSync)) + else if userSync.type == 'stream_mix' + @list.append(this.createStreamMix(userSync)) + + recordings[userSync.recording_id] = userSync.recording + recordingsToResolve = [] + # resolve each track against backend data: + for recording_id, recording of recordings + recordingsToResolve.push(recording) + + clientRecordings = context.jamClient.GetLocalRecordingState(recordings: recordingsToResolve) + + if clientRecordings.error? + alert(clientRecordings.error) + else + this.associateClientInfo(turp) for turp in clientRecordings.recordings + + for track in @list.find('.recorded-track.sync') + this.updateTrackState($(track)) + for streamMix in @list.find('.stream-mix.sync') + this.updateStreamMixState($(streamMix)) + + this.separateByRecording() + + @since = response.next + + resolveTrack: (commandMetadata) => + recordingId = commandMetadata['recording_id'] + clientTrackId = commandMetadata['track_id'] + + matchingTrack = @list.find(".recorded-track[data-recording-id='#{recordingId}'][data-client-track-id='#{clientTrackId}']") + if matchingTrack.length == 0 + return @rest.getRecordedTrack({recording_id: recordingId, track_id: clientTrackId}) + else + deferred = $.Deferred(); + deferred.resolve(matchingTrack.data('server-info')) + return deferred + + renderFullUploadRecordedTrack: (serverInfo) => + $track = $(context._.template(@templateRecordedTrackCommand.html(), $.extend(serverInfo, {action:'UPLOADING'}), {variable: 'data'})) + $busy = @uploadProgress.find('.busy') + $busy.empty().append($track) + @uploadProgress.find('.progress').css('width', '0%') + + renderFullDownloadRecordedTrack: (serverInfo) => + $track = $(context._.template(@templateRecordedTrackCommand.html(), $.extend(serverInfo, {action:'DOWNLOADING'}), {variable: 'data'})) + $busy = @downloadProgress.find('.busy') + $busy.empty().append($track) + @downloadProgress.find('.progress').css('width', '0%') + + # this will either show a generic placeholder, or immediately show the whole track + renderDownloadRecordedTrack: (commandId, commandMetadata) => + # try to find the info in the list; if we can't find it, then resolve it + deferred = this.resolveTrack(commandMetadata) + if deferred.state() == 'pending' + this.renderGeneric(commandId, 'download', commandMetadata) + + deferred.done(this.renderFullDownloadRecordedTrack).fail(()=> @logger.error("unable to fetch recorded_track info") ) + + + renderUploadRecordedTrack: (commandId, commandMetadata) => + # try to find the info in the list; if we can't find it, then resolve it + deferred = this.resolveTrack(commandMetadata) + if deferred.state() == 'pending' + this.renderGeneric(commandId, 'upload', commandMetadata) + + deferred.done(this.renderFullUploadRecordedTrack).fail(()=> @logger.error("unable to fetch recorded_track info") ) + + renderGeneric: (commandId, category, commandMetadata) => + commandMetadata.displayType = this.displayName(commandMetadata) + + $generic = $(context._.template(@templateGenericCommand.html(), commandMetadata, {variable: 'data'})) + if category == 'download' + $busy = @downloadProgress.find('.busy') + $busy.empty().append($generic) + else if category == 'upload' + $busy = @uploadProgress.find('.busy') + $busy.empty().append($generic) + else + @logger.error("unknown category #{category}") + + + renderStartCommand: (commandId, commandType, commandMetadata) => + + #console.log("renderStartCommand", arguments) + unless commandMetadata? + managerState = context.jamClient.GetRecordingManagerState() + if commandType == 'download' + commandMetadata = managerState.current_download + else if commandType == 'upload' + commandMetadata = managerState.current_upload + else + @logger.error("unknown commandType #{commandType}") + + unless commandMetadata? + # we still have no metadata. we have to give up + @logger.error("no metadata found for current command #{commandId} #{commandType}. bailing out") + return + + if commandMetadata.queue == 'download' + @downloadCommandId = commandId + @downloadMetadata = commandMetadata + @downloadProgress.removeClass('quiet paused busy') + @downloadProgress.addClass('busy') + if commandMetadata.type == 'recorded_track' and commandMetadata.action == 'download' + this.renderDownloadRecordedTrack(commandId, commandMetadata) + else + this.renderGeneric(commandId, 'download', commandMetadata) + else if commandMetadata.queue == 'upload' + @uploadCommandId = commandId + @uploadMetadata = commandMetadata + @uploadProgress.removeClass('quiet paused busy') + @uploadProgress.addClass('busy') + if commandMetadata.type == 'recorded_track' and commandMetadata.action == 'upload' + this.renderUploadRecordedTrack(commandId, commandMetadata) + else + this.renderGeneric(commandId, 'upload', commandMetadata) + + renderSingleRecording: (userSyncs) => + return if userSyncs.entries.length == 0 + clientRecordings = context.jamClient.GetLocalRecordingState(recordings: [userSyncs.entries[0].recording]) + for userSync in userSyncs.entries + if userSync.type == 'recorded_track' + $track = @list.find(".sync[data-id='#{userSync.id}']") + continue if $track.length == 0 + $track.data('server-info', userSync) + this.associateClientInfo(clientRecordings.recordings[0]) + this.updateTrackState($track) + else if userSync.type == 'mix' + # check if there is a virtual mix 1st; if so, update it + $mix = @list.find(".mix.virtual[data-recording-id='#{userSync.recording.id}']") + if $mix.length == 0 + $mix = @list.find(".sync[data-id='#{userSync.id}']") + continue if $mix.length == 0 + $newMix = this.createMix(userSync) + this.associateClientInfo(clientRecordings.recordings[0]) + $mix.replaceWith($newMix) + else if userSync.type == 'stream_mix' + $streamMix = @list.find(".sync[data-id='#{userSync.id}']") + continue if $streamMix.length == 0 + $streamMix.data('server-info', userSync) + this.associateClientInfo(clientRecordings.recordings[0]) + this.updateStreamMixState($streamMix) + + updateSingleRecording: (recording_id) => + @rest.getUserSyncs({recording_id: recording_id}).done(this.renderSingleRecording) + + updateSingleRecordedTrack: ($track) => + serverInfo = $track.data('server-info') + @rest.getUserSync({user_sync_id: serverInfo.id}) + .done((userSync) => + # associate new server-info with this track + $track.data('server-info', userSync) + # associate new client-info with this track + clientRecordings = context.jamClient.GetLocalRecordingState(recordings: [userSync.recording]) + this.associateClientInfo(clientRecordings.recordings[0]) + + this.updateTrackState($track) + ) + .fail(@app.ajaxError) + + updateProgressOnSync: ($track, queue, percentage) => + state = if queue == 'upload' then '.upload-state' else '.client-state' + $progress = $track.find("#{state} .progress") + $progress.css('width', percentage + '%') + + renderFinishCommand: (commandId, data) => + + reason = data.commandReason + success = data.commandSuccess + + if commandId == @downloadCommandId + + this.logResult(@downloadMetadata, success, reason, false) + recordingId = @downloadMetadata['recording_id'] + this.updateSingleRecording(recordingId) if recordingId? + + else if commandId == @uploadCommandId + + this.logResult(@uploadMetadata, success, reason, false) + recordingId = @uploadMetadata['recording_id'] + this.updateSingleRecording(recordingId) if recordingId? + + else + @logger.error("unknown commandId in renderFinishCommand") + + # refresh the header when done. we need to leave this callback to let the command fully switch to off + #setTimeout(this.renderHeader, 1) + + this.renderHeader() + + renderPercentage: (commandId, commandType, percentage) => + + if commandId == @downloadCommandId + $progress = @downloadProgress.find('.progress') + $progress.css('width', percentage + '%') + + if @downloadMetadata.type == 'recorded_track' + clientTrackId = @downloadMetadata['track_id'] + recordingId = @downloadMetadata['recording_id'] + $matchingTrack = @list.find(".recorded-track.sync[data-recording-id='#{recordingId}'][data-client-track-id='#{clientTrackId}']") + if $matchingTrack.length > 0 + this.updateProgressOnSync($matchingTrack, 'download', percentage) + + else if commandId == @uploadCommandId + $progress = @uploadProgress.find('.progress') + $progress.css('width', percentage + '%') + + if @uploadMetadata.type == 'recorded_track' and @uploadMetadata.action == 'upload' + clientTrackId = @uploadMetadata['track_id'] + recordingId = @uploadMetadata['recording_id'] + $matchingTrack = @list.find(".recorded-track.sync[data-recording-id='#{recordingId}'][data-client-track-id='#{clientTrackId}']") + if $matchingTrack.length > 0 + this.updateProgressOnSync($matchingTrack, 'upload', percentage) + else if @uploadMetadata.type == 'stream_mix' and @uploadMetadata.action == 'upload' + recordingId = @uploadMetadata['recording_id'] + $matchingStreamMix = @list.find(".stream-mix.sync[data-recording-id='#{recordingId}']") + if $matchingStreamMix.length > 0 + this.updateProgressOnSync($matchingStreamMix, 'upload', percentage) + + else + @logger.error("unknown commandId in renderFinishCommand") + + + fileManagerCmdStart: (e, data) => + #console.log("fileManagerCmdStart", data) + commandId = data['commandId'] + commandType = data['commandType'] + commandMetadata = data['commandMetadata'] + + category = commandType == 'download' ? 'download' : 'upload' + + if category == 'download' && (@downloadCommandId != null && @downloadCommandId != commandId) + @logger.warn("received command-start for download but previous command did not send stop") + this.renderFinishCommand(commandId, category) + else if @uploadCommandId != null && @uploadCommandId != commandId + @logger.warn("received command-start for upload but previous command did not send stop") + this.renderFinishCommand(commandId, category) + + this.renderStartCommand(commandId, commandType, commandMetadata) + + fileManagerCmdStop: (e, data) => + #console.log("fileManagerCmdStop", data) + commandId = data['commandId'] + + if commandId == @downloadCommandId + category = 'download' + this.renderFinishCommand(commandId, data) + @downloadCommandId = null + @downloadMetadata = null; + else if commandId == @uploadCommandId + category = 'upload' + this.renderFinishCommand(commandId, data) + @uploadCommandId = null + @uploadMetadata = null; + else + @logger.warn("received command-stop for unknown command: #{commandId} #{@downloadCommandId} #{@uploadCommandId}" ) + + fileManagerCmdProgress: (e, data) => + #console.log("fileManagerCmdProgress", data) + commandId = data['commandId'] + + if commandId == @downloadCommandId + category = 'download' + this.renderPercentage(commandId, category, data.percentage) + + else if commandId == @uploadCommandId + category = 'upload' + this.renderPercentage(commandId, category, data.percentage) + else + @logger.warn("received command-percentage for unknown command") + + fileManagerAsapCommandStatus: (e, data) => + this.logResult(data.commandMetadata, false, data.commandReason, true) + + displayName: (metadata) => + if metadata.type == 'recorded_track' && metadata.action == 'download' + return 'DOWNLOADING TRACK' + else if metadata.type == 'recorded_track' && metadata.action == 'upload' + return 'UPLOADING TRACK' + else if metadata.type == 'mix' && metadata.action == 'download' + return 'DOWNLOADING MIX' + else if metadata.type == 'recorded_track' && metadata.action == 'convert' + return 'COMPRESSING TRACK' + else if metadata.type == 'recorded_track' && metadata.action == 'delete' + return 'CLEANUP TRACK' + else if metadata.type == 'stream_mix' && metadata.action == 'upload' + return 'UPLOADING STREAM MIX' + else + return "#{metadata.action} #{metadata.type}".toUpperCase() + + + shownTab: () => + @tabSelectors.filter('.selected') + + # create a log in the Log tab + logResult: (metadata, success, reason, isAsap) => + + # if an error comes in, and the log tab is not already showing, increment the badge + if not success and (!@showing || this.shownTab().attr('purpose') != 'log') + @logBadge.css('display', 'inline-block') + if @showing # don't do animation unless user can see it + @logBadge.pulse({'background-color' : '#868686'}, {pulses: 2}, () => @logBadge.css('background-color', '#980006')) + + displayReason = switch reason + when 'no-match-in-queue' then 'restart JamKazam' + when 'already-done' then 'ignored, already done' + when 'failed-convert' then 'failed previously' + else reason + + displaySuccess = if success then 'yes' else 'no' + $log = context._.template(@templateLogItem.html(), {isAsap: isAsap, command: this.displayName(metadata), success: success, displaySuccess: displaySuccess, detail: displayReason, when: new Date()}, {variable: 'data'}) + + @logList.prepend($log) + + + diff --git a/web/app/assets/javascripts/utils.js b/web/app/assets/javascripts/utils.js index 113fdf202..f21129f02 100644 --- a/web/app/assets/javascripts/utils.js +++ b/web/app/assets/javascripts/utils.js @@ -95,6 +95,12 @@ instrumentIconMap256[instrumentId] = {asset: "/assets/content/icon_instrument_" + icon + "256.png", name: instrumentId}; }); + context.JK.helpBubbleFunctionHelper = function(originalFunc) { + var helpText = originalFunc.apply(this); + var holder = $('
'); + holder.append(helpText); + return holder; + } /** * Associates a help bubble on hover (by default) with the specified $element, using jquery.bt.js (BeautyTips) * @param $element The element that should show the help when hovered @@ -111,18 +117,48 @@ } $element.on('remove', function() { - $element.btOff(); + $element.btOff(); // if the element goes away for some reason, get rid of the bubble too }) + var holder = null; + if (context._.isFunction(templateName)) { + holder = function wrapper() { + return context.JK.helpBubbleFunctionHelper.apply(this, [templateName]) + } + } + else { + var $template = $('#template-help-' + templateName) + if($template.length == 0) { + var helpText = templateName; + } + else { + var helpText = context._.template($template.html(), data, { variable: 'data' }); + } - var $template = $('#template-help-' + templateName); - if($template.length == 0) throw "no template by the name " + templateName; - var helpText = context._.template($template.html(), data, { variable: 'data' }); - var holder = $('
'); - holder.append(helpText); - context.JK.hoverBubble($element, helpText, options); + holder = $('
'); + holder = holder.append(helpText).html() + } + + context.JK.hoverBubble($element, holder, options); } + /** + * Meant to show a little bubble to confirm something happened, in a way less obtrusive than a app.notify + * @param $element The element that should show the help when hovered + * @param templateName the name of the help template (without the '#template-help' prefix). Add to _help.html.erb + * @param data (optional) data for your template, if applicable + * @param options (optional) You can override the default BeautyTips options: https://github.com/dillon-sellars/BeautyTips + */ + context.JK.confirmBubble = function($element, templateName, data, options) { + if(!options) options = {}; + options.spikeGirth = 0; + options.spikeLength = 0; + options.shrinkToFit = true + options.duration = 3000 + options.cornerRadius = 5 + + return context.JK.prodBubble($element, templateName, data, options); + } /** * Associates a help bubble immediately with the specified $element, using jquery.bt.js (BeautyTips) * By 'prod' it means to literally prod the user, to make them aware of something important because they did something else @@ -203,7 +239,14 @@ options = defaultOpts; } - $element.bt(text, options); + if(typeof text != 'string') { + options.contentSelector = text; + $element.bt(options); + } + else { + $element.bt(text, options); + } + } context.JK.bindProfileClickEvents = function($parent, dialogsToClose) { @@ -524,7 +567,7 @@ } context.JK.formatDateTime = function (dateString) { - var date = new Date(dateString); + var date = dateString instanceof Date ? dateString : new Date(dateString); return context.JK.padString(date.getMonth() + 1, 2) + "/" + context.JK.padString(date.getDate(), 2) + "/" + date.getFullYear() + " - " + date.toLocaleTimeString(); } diff --git a/web/app/assets/javascripts/web/web.js b/web/app/assets/javascripts/web/web.js index a3d159a15..f3e91abd4 100644 --- a/web/app/assets/javascripts/web/web.js +++ b/web/app/assets/javascripts/web/web.js @@ -19,6 +19,7 @@ //= require jquery.ba-bbq //= require jquery.icheck //= require jquery.bt +//= require jquery.exists //= require AAA_Log //= require AAC_underscore //= require alert @@ -47,6 +48,7 @@ //= require ga //= require jam_rest //= require session_utils +//= require recording_utils //= require helpBubbleHelper //= require facebook_rest //= require landing/init diff --git a/web/app/assets/stylesheets/client/client.css b/web/app/assets/stylesheets/client/client.css index 68b4073ce..75d262cbe 100644 --- a/web/app/assets/stylesheets/client/client.css +++ b/web/app/assets/stylesheets/client/client.css @@ -60,4 +60,5 @@ *= require web/sessions *= require jquery.Jcrop *= require icheck/minimal/minimal + *= require users/syncViewer */ \ No newline at end of file diff --git a/web/app/assets/stylesheets/client/common.css.scss b/web/app/assets/stylesheets/client/common.css.scss index fdd5027de..856ad5f21 100644 --- a/web/app/assets/stylesheets/client/common.css.scss +++ b/web/app/assets/stylesheets/client/common.css.scss @@ -44,6 +44,12 @@ $latencyBadgePoor: #980006; $latencyBadgeUnacceptable: #868686; $latencyBadgeUnknown: #868686; +$good: #71a43b; +$unknown: #868686; +$poor: #980006; +$error: #980006; +$fair: #cc9900; + @mixin border_box_sizing { -webkit-box-sizing: border-box; -moz-box-sizing: border-box; @@ -65,6 +71,55 @@ $latencyBadgeUnknown: #868686; background-clip: padding-box; /* stops bg color from leaking outside the border: */ } +/** IE10 and above only */ +@mixin vertical-align-row { + display: -webkit-box; + display: -moz-box; + display: -ms-flexbox; + display: -webkit-flex; + display: flex; + + -webkit-flex-pack:space-around; + -webkit-justify-content:space-around; + -moz-justify-content:space-around; + -ms-flex-pack:space-around; + justify-content:space-around; + + -webkit-flex-line-pack:center; + -ms-flex-line-pack:center; + -webkit-align-content:center; + align-content:center; + + -webkit-flex-direction:row; + -moz-flex-direction:row; + -ms-flex-direction:row; + flex-direction:row; +} + +@mixin vertical-align-column { + display: -webkit-box; + display: -moz-box; + display: -ms-flexbox; + display: -webkit-flex; + display: flex; + + -webkit-flex-pack:space-around; + -webkit-justify-content:space-around; + -moz-justify-content:space-around; + -ms-flex-pack:space-around; + justify-content:space-around; + + -webkit-flex-line-pack:center; + -ms-flex-line-pack:center; + -webkit-align-content:center; + align-content:center; + + -webkit-flex-direction:column; + -moz-flex-direction:column; + -ms-flex-direction:column; + flex-direction:column; +} + // Single side border-radius @mixin border-top-radius($radius) { @@ -122,3 +177,115 @@ $latencyBadgeUnknown: #868686; } } + + +@mixin client-state-box { + .client-state { + position:relative; + text-align:center; + padding:3px; + @include border_box_sizing; + color:white; + + &.unknown { + background-color: $error; + } + + &.error { + background-color: $error; + &.is-native-client { .retry { display:inline-block; } } + } + + &.hq { + background-color: $good; + } + + &.sq { + background-color: $good; + &.is-native-client { .retry { display:inline-block; } } + } + + &.missing { + background-color: $error; + &.is-native-client { .retry { display:inline-block; } } + } + + &.discarded { + background-color: $unknown; + } + + .retry { + display:none; + position:absolute; + line-height:28px; + top:4px; + right:8px; + z-index:1; + } + } +} + +@mixin upload-state-box { + + .upload-state { + position:relative; + text-align:center; + padding:3px; + @include border_box_sizing; + color:white; + + &.unknown { + background-color: $unknown; + } + + &.error { + background-color: $error; + &.is-native-client { .retry { display:inline-block; } } + } + + &.missing { + background-color: $error; + } + + &.upload-soon { + background-color: $fair; + &.is-native-client { .retry { display:inline-block; } } + } + + &.uploaded { + background-color: $good; + } + + .retry { + display:none; + position:absolute; + line-height:28px; + visibility: middle; + top:4px; + right:8px; + } + } +} + + +@mixin mix-state-box { + + .mix-state { + position:relative; + text-align:center; + padding:3px; + @include border_box_sizing; + color:white; + + &.still-uploading { background-color: $fair; } + &.discard {background-color: $unknown; } + &.unknown { background-color: $unknown; } + &.error { background-color: $error; } + &.mixed { background-color: $good; } + &.mixing {background-color: $good; } + &.waiting-to-mix {background-color: $good; } + &.stream-mix { background-color: $fair; } + } +} + + diff --git a/web/app/assets/stylesheets/client/help.css.scss b/web/app/assets/stylesheets/client/help.css.scss index 32074d802..4755d711f 100644 --- a/web/app/assets/stylesheets/client/help.css.scss +++ b/web/app/assets/stylesheets/client/help.css.scss @@ -1,4 +1,6 @@ -.screen, body.web { +@import "client/common"; + +body.jam, body.web, .dialog{ .bt-wrapper { .bt-content { @@ -43,6 +45,110 @@ } } + .help-hover-recorded-tracks, .help-hover-stream-mix { + + font-size:12px; + padding:5px; + @include border_box_sizing; + width:450px; + + .sync-definition { + padding:0 10px; + } + + .client-box { + float:left; + width:50%; + min-width:200px; + @include border_box_sizing; + } + + .upload-box { + float:left; + width:50%; + min-width:200px; + @include border_box_sizing; + + } + + @include client-state-box; + .client-state { + width:100%; + margin:5px 0 10px; + font-size:16px; + } + + @include upload-state-box; + .upload-state { + width: 100%; + margin:5px 0 10px; + font-size:16px; + } + + .client-state-info, .upload-state-info { + text-align:center; + font-size:12px; + } + + .client-state-definition { + margin-bottom:20px; + } + + .upload-state-definition { + margin-bottom:20px; + } + .summary { + margin-bottom:10px; + + .title { + text-align:center; + font-size:16px; + margin:10px 0 5px; + } + } + } + + .help-hover-mix { + + font-size:12px; + padding:5px; + @include border_box_sizing; + width:450px; + + .mix-box { + float:left; + width:100%; + min-width:400px; + @include border_box_sizing; + } + + @include mix-state-box; + .mix-state { + width:100%; + margin:5px 0 10px; + font-size:16px; + //background-color:black ! important; + } + + .mix-state-info { + text-align:center; + font-size:12px; + } + + .mix-state-definition { + margin-bottom:20px; + } + + .summary { + margin-bottom:10px; + + .title { + text-align:center; + font-size:16px; + margin:10px 0 5px; + } + } + } } } } \ No newline at end of file diff --git a/web/app/assets/stylesheets/client/recordingManager.css.scss b/web/app/assets/stylesheets/client/recordingManager.css.scss index 5aa630d64..729e19b1e 100644 --- a/web/app/assets/stylesheets/client/recordingManager.css.scss +++ b/web/app/assets/stylesheets/client/recordingManager.css.scss @@ -2,12 +2,34 @@ color: #CCCCCC; font-size: 11px; - margin: 0 auto; + margin: -5px auto 0 ; position: absolute; text-align: center; left: 17%; width: 50%; - z-index:-1; + + // if it's the native client, then show the File Manager span. if it's not (normal browser) hide it. + // even if it's the native client, once a command is running, hide File Manager + &.native-client { + .recording-manager-launcher { + display:inline-block; + } + &.running { + .recording-manager-launcher { + display:none; + } + .recording-manager-command { + display:inline; + } + } + } + + .recording-manager-launcher { + width:100%; + margin:5px 10px; + display:none; + cursor:pointer; + } .recording-manager-command { -webkit-box-sizing: border-box; @@ -16,7 +38,9 @@ box-sizing: border-box; width:33%; margin:5px 10px; - visibility: hidden; + display:none; + visible:hidden; + cursor:pointer; .percent { margin-left:3px; diff --git a/web/app/assets/stylesheets/client/screen_common.css.scss b/web/app/assets/stylesheets/client/screen_common.css.scss index f36438ea4..39f49a7e8 100644 --- a/web/app/assets/stylesheets/client/screen_common.css.scss +++ b/web/app/assets/stylesheets/client/screen_common.css.scss @@ -428,6 +428,16 @@ small, .small {font-size:11px;} margin:0; } } + +.help-launcher { + font-weight:bold; + font-size:12px; + line-height:12px; + position:absolute; + top:3px; + right:3px; + cursor:pointer; +} .white {color:#fff;} .lightgrey {color:#ccc;} .grey {color:#999;} diff --git a/web/app/assets/stylesheets/client/session.css.scss b/web/app/assets/stylesheets/client/session.css.scss index 90b2f3d97..0a7e00e86 100644 --- a/web/app/assets/stylesheets/client/session.css.scss +++ b/web/app/assets/stylesheets/client/session.css.scss @@ -49,10 +49,11 @@ font-size:11px; height:18px; vertical-align:top; - margin-left:15px; } + .recording-controls { + display:none; position: absolute; bottom: 0; height: 25px; @@ -60,6 +61,9 @@ .play-button { top:2px; } + .recording-current { + top:3px ! important; + } } .playback-mode-buttons { @@ -675,17 +679,6 @@ table.vu td { font-size:18px; } -.recording-controls { - display:none; - - .play-button { - outline:none; - } - - .play-button img.pausebutton { - display:none; - } -} .playback-mode-buttons { display:none; @@ -762,3 +755,15 @@ table.vu td { .rate-thumbsdown.selected { background-image:url('/assets/content/icon_thumbsdown_big_on.png'); } + + +.recording-controls { + + .play-button { + outline:none; + } + + .play-button img.pausebutton { + display:none; + } +} \ No newline at end of file diff --git a/web/app/assets/stylesheets/dialogs/gettingStartDialog.css.scss b/web/app/assets/stylesheets/dialogs/gettingStartDialog.css.scss index 9e768b3e2..a3ada5d95 100644 --- a/web/app/assets/stylesheets/dialogs/gettingStartDialog.css.scss +++ b/web/app/assets/stylesheets/dialogs/gettingStartDialog.css.scss @@ -1,6 +1,6 @@ @import "client/common"; -#whatsnext-dialog, #getting-started-dialog { +#getting-started-dialog { height:auto; width:auto; diff --git a/web/app/assets/stylesheets/dialogs/syncViewerDialog.css.scss b/web/app/assets/stylesheets/dialogs/syncViewerDialog.css.scss new file mode 100644 index 000000000..4c51332e9 --- /dev/null +++ b/web/app/assets/stylesheets/dialogs/syncViewerDialog.css.scss @@ -0,0 +1,3 @@ +#all-syncs-dialog { + height:660px; +} \ No newline at end of file diff --git a/web/app/assets/stylesheets/users/syncViewer.css.scss b/web/app/assets/stylesheets/users/syncViewer.css.scss new file mode 100644 index 000000000..cbc060c1c --- /dev/null +++ b/web/app/assets/stylesheets/users/syncViewer.css.scss @@ -0,0 +1,281 @@ +@import "client/common"; + +.sync-viewer { + width: 100%; + + .list { + height:400px; + overflow:auto; + border-style:solid; + border-width:0 0 1px 0; + border-color:#AAAAAA; + } + + .knobs { + + } + .headline { + height:40px; + @include border_box_sizing; + @include vertical-align-row; + + .download-progress, .upload-progress { + width:200px; + line-height:30px; + .paused { + display:none; + color:#777; + font-size:11px; + } + .quiet { + display:none; + color:#777; + font-size:11px; + } + &.paused { + .paused { + display:inline-block; + } + } + + &.quiet { + .quiet{ + display:inline-block; + } + } + &.busy { + .busy { + display:inline-block; + width:100%; + } + } + } + + .in-progress { + @include border_box_sizing; + @include vertical-align-row; + text-align:center; + + .sync { + line-height:20px; + height:30px; + + .type { + position:relative; + width:100%; + font-size: 10px; + } + .text { + z-index:1; + } + .avatar-tiny { + margin-top:-4px; + z-index:1; + } + .instrument-icon { + margin-top:-3px; + z-index:1; + } + } + } + } + + + .list-header { + width:100%; + height:16px; + font-size:12px; + .type-info { + float:left; + width:26%; + @include border_box_sizing; + @include vertical-align-column; + } + .client-state-info { + float:left; + padding: 0 4px; + width:37%; + @include border_box_sizing; + @include vertical-align-column; + } + .upload-state-info { + float:left; + width:37%; + @include border_box_sizing; + @include vertical-align-column; + } + + .special { + font-size:12px; + border-width:1px 1px 0 1px; + border-style:solid; + border-color:#cccccc; + width:100%; + text-align:center; + @include border_box_sizing; + } + + .special-text { + width:100%; + text-align:center; + @include border_box_sizing; + } + } + + .sync { + margin:3px 0; + display:inline-block; + @include border_box_sizing; + padding:3px; + background-color:black; + width:100%; + line-height:28px; + @include border-radius(2px); + + @include vertical-align-row; + } + + .type { + float:left; + width:26%; + color:white; + padding:3px; + @include border_box_sizing; + @include vertical-align-row; + + .instrument-icon { + width:24px; + height:24px; + } + } + + .msg { + z-index:1; + } + + .progress { + position:absolute; + background-color:$ColorScreenPrimary; + height:100%; + top:0px; + left:0; + } + + @include mix-state-box; + .mix-state { + position:relative; + float:left; + width:74%; + @include vertical-align-row; + } + + @include client-state-box; + .client-state { + position:relative; + float:left; + width:37%; + @include vertical-align-row; + } + + @include upload-state-box; + .upload-state { + position:relative; + float:left; + width: 37%; + @include vertical-align-row; + } + + .recording-holder { + margin-top:10px; + padding: 5px 0 0 0; + width:100%; + @include border_box_sizing; + @include vertical-align-column; + + .timeago { + float:right; + } + } + + .log-list { + height:392px; + overflow:auto; + border-style:solid; + border-width:0 0 1px 0; + border-color:#AAAAAA; + } + + .log { + + @include border_box_sizing; + @include border-radius(2px); + width:100%; + margin:3px 0; + display:inline-block; + padding:3px; + background-color:black; + height:20px; + font-size:12px; + color:white; + + &.success-false { + background-color:$error; + } + + .command { + float:left; + width:33%; + text-align:center; + position:relative; + + img { + position:absolute; + left:0; + top:-1px; + } + } + + .success { + float:left; + width:33%; + text-align:center; + } + + .detail { + float:left; + width:33%; + text-align:center; + } + } + + .badge { + display:none; + margin-left:5px; + height:11px; + width:12px; + color:white; + font-size:10px; + padding-top:2px; + background-color:$error; + -webkit-border-radius:6px; + -moz-border-radius:6px; + border-radius:6px; + vertical-align: top; + } + + .tab-content[purpose="log"] { + display:none; + + .list-header { + .list-column { + float:left; + width:33%; + text-align:center; + } + } + } + + .paginator-holder { + text-align:center; + } +} \ No newline at end of file diff --git a/web/app/assets/stylesheets/web/audioWidgets.css.scss b/web/app/assets/stylesheets/web/audioWidgets.css.scss index 1fcd93f85..55871e7ba 100644 --- a/web/app/assets/stylesheets/web/audioWidgets.css.scss +++ b/web/app/assets/stylesheets/web/audioWidgets.css.scss @@ -25,6 +25,8 @@ text-align: center; @include border_box_sizing; height: 36px; + display:inline-block; + white-space:nowrap; .recording-status { font-size:15px; @@ -34,8 +36,8 @@ font-family:Arial, Helvetica, sans-serif; display:inline-block; font-size:18px; - float:right; - top:3px; + position:absolute; + //top:3px; right:4px; } @@ -49,7 +51,10 @@ display:none; } } - &.no-mix { + + &:not(.has-mix) { + + border-width: 0; // override screen_common's .error .play-button { display:none; @@ -109,6 +114,7 @@ } } + .feed-entry { position:relative; display:block; @@ -172,7 +178,8 @@ .recording-controls { .recording-position { width:70%; - margin-left:0; + margin-left:-29px; + } .recording-playback { diff --git a/web/app/assets/stylesheets/web/recordings.css.scss b/web/app/assets/stylesheets/web/recordings.css.scss index bf15d9ce6..b843a9df2 100644 --- a/web/app/assets/stylesheets/web/recordings.css.scss +++ b/web/app/assets/stylesheets/web/recordings.css.scss @@ -6,7 +6,6 @@ padding:8px 5px 8px 10px; width:98%; position:relative; - text-align:center; } .landing-details .recording-controls, .landing-details .recording-controls { @@ -64,11 +63,11 @@ .recording-current { font-family:Arial, Helvetica, sans-serif; - display:inline-block; + display:inline; font-size:18px; - float:right; top:3px; right:4px; + position:absolute; } #btnPlayPause { diff --git a/web/app/controllers/api_claimed_recordings_controller.rb b/web/app/controllers/api_claimed_recordings_controller.rb index 04cef8d3f..725209843 100644 --- a/web/app/controllers/api_claimed_recordings_controller.rb +++ b/web/app/controllers/api_claimed_recordings_controller.rb @@ -14,8 +14,6 @@ class ApiClaimedRecordingsController < ApiController if !@claimed_recording.is_public && @claimed_recording.user_id != current_user.id raise PermissionError, 'this claimed recording is not public' end - - @claimed_recording end def update @@ -40,10 +38,21 @@ class ApiClaimedRecordingsController < ApiController raise PermissionError, 'this claimed recording is not public' end - mix = @claimed_recording.recording.mixes.first! - params[:type] ||= 'ogg' - redirect_to mix.sign_url(120, params[:type]) + + mix = @claimed_recording.recording.mix + + if mix && mix.completed + redirect_to mix.sign_url(120, params[:type]) + else + quick_mix = @claimed_recording.recording.stream_mix + + if quick_mix + redirect_to quick_mix.sign_url(120, params[:type]) + else + render :json => {}, :status => 404 + end + end end private diff --git a/web/app/controllers/api_recordings_controller.rb b/web/app/controllers/api_recordings_controller.rb index e7c67b29d..ce6cf9d42 100644 --- a/web/app/controllers/api_recordings_controller.rb +++ b/web/app/controllers/api_recordings_controller.rb @@ -3,6 +3,7 @@ class ApiRecordingsController < ApiController before_filter :api_signed_in_user, :except => [ :add_like ] before_filter :look_up_recording, :only => [ :show, :stop, :claim, :discard, :keep ] before_filter :parse_filename, :only => [ :download, :upload_next_part, :upload_sign, :upload_part_complete, :upload_complete ] + before_filter :lookup_stream_mix, :only => [ :upload_next_part_stream_mix, :upload_sign_stream_mix, :upload_part_complete_stream_mix, :upload_complete_stream_mix ] respond_to :json @@ -35,6 +36,10 @@ class ApiRecordingsController < ApiController def show end + def show_recorded_track + @recorded_track = RecordedTrack.find_by_recording_id_and_client_track_id(params[:id], params[:track_id]) + end + def download raise PermissionError, ValidationMessages::PERMISSION_VALIDATION_ERROR unless @recorded_track.can_download?(current_user) @@ -150,7 +155,7 @@ class ApiRecordingsController < ApiController render :json => {}, :status => 200 end - def upload_next_part + def upload_next_part # track length = params[:length] md5 = params[:md5] @@ -174,11 +179,11 @@ class ApiRecordingsController < ApiController end - def upload_sign + def upload_sign # track render :json => @recorded_track.upload_sign(params[:md5]), :status => 200 end - def upload_part_complete + def upload_part_complete # track part = params[:part] offset = params[:offset] @@ -192,7 +197,7 @@ class ApiRecordingsController < ApiController end end - def upload_complete + def upload_complete # track @recorded_track.upload_complete @recorded_track.recording.upload_complete @@ -206,12 +211,72 @@ class ApiRecordingsController < ApiController end + + def upload_next_part_stream_mix + length = params[:length] + md5 = params[:md5] + + @quick_mix.upload_next_part(length, md5) + + if @quick_mix.errors.any? + + response.status = :unprocessable_entity + # this is not typical, but please don't change this line unless you are sure it won't break anything + # this is needed because after_rollback in the RecordedTrackObserver touches the model and something about it's + # state doesn't cause errors to shoot out like normal. + render :json => { :errors => @quick_mix.errors }, :status => 422 + else + result = { + :part => @quick_mix.next_part_to_upload, + :offset => @quick_mix.file_offset.to_s + } + + render :json => result, :status => 200 + end + + end + + def upload_sign_stream_mix + render :json => @quick_mix.upload_sign(params[:md5]), :status => 200 + end + + def upload_part_complete_stream_mix + part = params[:part] + offset = params[:offset] + + @quick_mix.upload_part_complete(part, offset) + + if @quick_mix.errors.any? + response.status = :unprocessable_entity + respond_with @quick_mix + else + render :json => {}, :status => 200 + end + end + + def upload_complete_stream_mix + @quick_mix.upload_complete + + if @quick_mix.errors.any? + response.status = :unprocessable_entity + respond_with @quick_mix + return + else + render :json => {}, :status => 200 + end + end + private def parse_filename @recorded_track = RecordedTrack.find_by_recording_id_and_client_track_id!(params[:id], params[:track_id]) raise PermissionError, ValidationMessages::PERMISSION_VALIDATION_ERROR unless @recorded_track.recording.has_access?(current_user) end + def lookup_stream_mix + @quick_mix = QuickMix.find_by_recording_id_and_user_id!(params[:id], current_user.id) + raise PermissionError, ValidationMessages::PERMISSION_VALIDATION_ERROR unless @quick_mix.recording.has_access?(current_user) + end + def look_up_recording @recording = Recording.find(params[:id]) raise PermissionError, ValidationMessages::PERMISSION_VALIDATION_ERROR unless @recording.has_access?(current_user) diff --git a/web/app/controllers/api_user_syncs_controller.rb b/web/app/controllers/api_user_syncs_controller.rb new file mode 100644 index 000000000..32fbbe395 --- /dev/null +++ b/web/app/controllers/api_user_syncs_controller.rb @@ -0,0 +1,41 @@ +# abstracts the idea of downloading / uploading files to JamKazam. +# Recordings, JamTracks, and Video are this + +class ApiUserSyncsController < ApiController + + before_filter :api_signed_in_user, :except => [ ] + before_filter :auth_user + + respond_to :json + + @@log = Logging.logger[ApiUserSyncsController] + + + def show + @user_sync = UserSync.show(params[:user_sync_id], current_user.id) + + if @user_sync.nil? + raise ActiveRecord::RecordNotFound + end + + render "api_user_syncs/show", :layout => nil + end + + # returns all downloads and uploads for a user, meaning it should return: + # all recorded_tracks (audio and video/soon) + # all mixes + # all jamtracks (soon) + def index + data = UserSync.index( + {user_id:current_user.id, + recording_id: params[:recording_id], + offset: params[:since], + limit: params[:limit]}) + + + + @user_syncs = data[:query] + @next = data[:next] + render "api_user_syncs/index", :layout => nil + end +end diff --git a/web/app/controllers/recordings_controller.rb b/web/app/controllers/recordings_controller.rb index 650c6971f..2f92c64c0 100644 --- a/web/app/controllers/recordings_controller.rb +++ b/web/app/controllers/recordings_controller.rb @@ -3,7 +3,11 @@ class RecordingsController < ApplicationController respond_to :html def show - @claimed_recording = ClaimedRecording.find(params[:id]) + @claimed_recording = ClaimedRecording.find_by_id(params[:id]) + if @claimed_recording.nil? + recording = Recording.find(params[:id]) + @claimed_recording = recording.candidate_claimed_recording + end render :layout => "web" end diff --git a/web/app/helpers/feeds_helper.rb b/web/app/helpers/feeds_helper.rb index fb128dc7b..38abfff85 100644 --- a/web/app/helpers/feeds_helper.rb +++ b/web/app/helpers/feeds_helper.rb @@ -119,5 +119,4 @@ module FeedsHelper def recording_genre(recording) recording.candidate_claimed_recording.genre.description end - end diff --git a/web/app/helpers/recording_helper.rb b/web/app/helpers/recording_helper.rb index 0c7b4030d..cbad1805c 100644 --- a/web/app/helpers/recording_helper.rb +++ b/web/app/helpers/recording_helper.rb @@ -45,4 +45,11 @@ module RecordingHelper def description_for_claimed_recording(claimed_recording) truncate(claimed_recording.name, length:250) end + + def listen_mix_url(recording) + { + mp3_url: claimed_recording_download_url(recording.candidate_claimed_recording.id, 'mp3'), + ogg_url: claimed_recording_download_url(recording.candidate_claimed_recording.id, 'ogg') + } + end end diff --git a/web/app/views/api_claimed_recordings/show.rabl b/web/app/views/api_claimed_recordings/show.rabl index 5210fcbc9..6be19dee3 100644 --- a/web/app/views/api_claimed_recordings/show.rabl +++ b/web/app/views/api_claimed_recordings/show.rabl @@ -12,6 +12,11 @@ node :share_url do |claimed_recording| end end + +node :mix do |claimed_recording| + listen_mix_url(claimed_recording.recording) if claimed_recording.has_mix? +end + child(:recording => :recording) { attributes :id, :created_at, :duration, :comment_count, :like_count, :play_count @@ -39,14 +44,3 @@ child(:recording => :recording) { } } } - -if :has_mix? - node do |claimed_recording| - { - mix: { - mp3_url: claimed_recording_download_url(claimed_recording.id, 'mp3'), - ogg_url: claimed_recording_download_url(claimed_recording.id, 'ogg') - } - } - end -end \ No newline at end of file diff --git a/web/app/views/api_feeds/show.rabl b/web/app/views/api_feeds/show.rabl index bdf21d1c7..06a6df897 100644 --- a/web/app/views/api_feeds/show.rabl +++ b/web/app/views/api_feeds/show.rabl @@ -78,7 +78,7 @@ glue :recording do 'recording' end - attributes :id, :band, :created_at, :duration, :comment_count, :like_count, :play_count, :has_mix? + attributes :id, :band, :created_at, :duration, :comment_count, :like_count, :play_count, :has_mix?, :mix_state node do |recording| { @@ -96,6 +96,13 @@ glue :recording do } end + node :mix do |recording| + { + state: recording.mix_state, + error: recording.mix_error + } + end unless @object.mix.nil? + child(:owner => :owner) { attributes :id, :name, :location, :photo_url } @@ -153,18 +160,9 @@ glue :recording do end end - if :has_mix? - node do |claimed_recording| - { - mix: { - mp3_url: claimed_recording_download_url(claimed_recording.id, 'mp3'), - ogg_url: claimed_recording_download_url(claimed_recording.id, 'ogg') - } - } - end + node :mix do |claimed_recording| + listen_mix_url(claimed_recording.recording) if claimed_recording.has_mix? end - - } end diff --git a/web/app/views/api_recordings/show.rabl b/web/app/views/api_recordings/show.rabl index 8b75f0384..78392c0b1 100644 --- a/web/app/views/api_recordings/show.rabl +++ b/web/app/views/api_recordings/show.rabl @@ -2,6 +2,17 @@ object @recording attributes :id, :band, :created_at, :duration, :comment_count, :like_count, :play_count +node :mix do |recording| + if recording.mix + { + id: recording.mix.id + } + else + nil + end +end + + child(:band => :band) { attributes :id, :name, :location, :photo_url } @@ -11,11 +22,9 @@ child(:owner => :owner) { } child(:recorded_tracks => :recorded_tracks) { - attributes :id, :fully_uploaded, :client_track_id, :client_id, :instrument_id - - child(:user => :user) { - attributes :id, :first_name, :last_name, :city, :state, :country, :location, :photo_url - } + node do |recorded_track| + partial("api_recordings/show_recorded_track", :object => recorded_track) + end } child(:comments => :comments) { @@ -26,6 +35,16 @@ child(:comments => :comments) { } } +# returns the current_user's version of the recording, i.e., their associated claimed_recording if present +node :my do |recording| + claim = recording.claim_for_user(current_user) || recording.candidate_claimed_recording + if claim + { name: claim.name, description: claim.description, genre: claim.genre.id, genre_name: claim.genre.description } + else + nil + end +end + child(:claimed_recordings => :claimed_recordings) { attributes :id, :name, :description, :is_public, :genre_id, :has_mix?, :user_id @@ -36,14 +55,7 @@ child(:claimed_recordings => :claimed_recordings) { end end - if :has_mix? - node do |claimed_recording| - { - mix: { - mp3_url: claimed_recording_download_url(claimed_recording.id, 'mp3'), - ogg_url: claimed_recording_download_url(claimed_recording.id, 'ogg') - } - } - end + node :mix do |claimed_recording| + listen_mix_url(claimed_recording.recording) if claimed_recording.has_mix? end } diff --git a/web/app/views/api_recordings/show_recorded_track.rabl b/web/app/views/api_recordings/show_recorded_track.rabl new file mode 100644 index 000000000..f5f73cc90 --- /dev/null +++ b/web/app/views/api_recordings/show_recorded_track.rabl @@ -0,0 +1,7 @@ +object @recorded_track + +attributes :id, :fully_uploaded, :client_track_id, :client_id, :instrument_id, :recording_id + +child(:user => :user) { + attributes :id, :first_name, :last_name, :city, :state, :country, :location, :photo_url +} \ No newline at end of file diff --git a/web/app/views/api_user_syncs/index.rabl b/web/app/views/api_user_syncs/index.rabl new file mode 100644 index 000000000..097e4f96f --- /dev/null +++ b/web/app/views/api_user_syncs/index.rabl @@ -0,0 +1,15 @@ + + +node :next do |page| + @next +end + +node :entries do |page| + partial "api_user_syncs/show", object: @user_syncs +end + +node :total_entries do |page| + @user_syncs.total_entries +end + + diff --git a/web/app/views/api_user_syncs/show.rabl b/web/app/views/api_user_syncs/show.rabl new file mode 100644 index 000000000..0c63ba010 --- /dev/null +++ b/web/app/views/api_user_syncs/show.rabl @@ -0,0 +1,103 @@ +object @user_sync + +glue :recorded_track do + + @object.current_user = current_user + + node :type do |i| + 'recorded_track' + end + + attributes :id, :recording_id, :client_id, :track_id, :client_track_id, :md5, :length, :download_count, :instrument_id, :fully_uploaded, :upload_failures, :part_failures, :created_at + + node :user do |recorded_track| + partial("api_users/show_minimal", :object => recorded_track.user) + end + + node :recording do |recorded_track| + partial("api_recordings/show", :object => recorded_track.recording) + end + + + node :upload do |recorded_track| + { + should_upload: true, + too_many_upload_failures: recorded_track.too_many_upload_failures? + } + end + + node :download do |recorded_track| + { + should_download: recorded_track.can_download?(current_user), + too_many_downloads: recorded_track.too_many_downloads? + } + end + +end + +glue :mix do + + @object.current_user = current_user + + node :type do |i| + 'mix' + end + + attributes :id, :recording_id, :started_at, :completed_at, :completed, :should_retry, :download_count, :state + + node :recording do |mix| + partial("api_recordings/show", :object => mix.recording) + end + + node do |mix| + { + duration: mix.recording.duration, + error: mix.error + } + end + + node :download do |mix| + { + should_download: mix.can_download?(current_user), + too_many_downloads: mix.too_many_downloads? + } + end + +end + + +glue :quick_mix do + + @object.current_user = current_user + + node :type do |i| + 'stream_mix' + end + + attributes :id, :recording_id, :fully_uploaded, :upload_failures, :part_failures, :created_at + + node :user do |quick_mix| + partial("api_users/show_minimal", :object => quick_mix.user) + end + + node :recording do |quick_mix| + partial("api_recordings/show", :object => quick_mix.recording) + end + + + node :upload do |quick_mix| + { + should_upload: true, + too_many_upload_failures: quick_mix.too_many_upload_failures? + } + end + + node :download do |quick_mix| + { + should_download: false, + too_many_downloads: false + } + end + +end + diff --git a/web/app/views/api_users/show_minimal.rabl b/web/app/views/api_users/show_minimal.rabl new file mode 100644 index 000000000..3846a746a --- /dev/null +++ b/web/app/views/api_users/show_minimal.rabl @@ -0,0 +1,3 @@ +object @user + +attributes :id, :first_name, :last_name, :photo_url, :name diff --git a/web/app/views/clients/_help.html.erb b/web/app/views/clients/_help.html.erb index 66d852a09..767a09794 100644 --- a/web/app/views/clients/_help.html.erb +++ b/web/app/views/clients/_help.html.erb @@ -119,3 +119,25 @@ + + + + + + + + diff --git a/web/app/views/clients/_help_launcher.html.slim b/web/app/views/clients/_help_launcher.html.slim new file mode 100644 index 000000000..09c428b72 --- /dev/null +++ b/web/app/views/clients/_help_launcher.html.slim @@ -0,0 +1,2 @@ +.help-launcher + | ? \ No newline at end of file diff --git a/web/app/views/clients/_play_controls.html.erb b/web/app/views/clients/_play_controls.html.erb index 7ee2042d4..88e088018 100644 --- a/web/app/views/clients/_play_controls.html.erb +++ b/web/app/views/clients/_play_controls.html.erb @@ -1,5 +1,5 @@ -
+
diff --git a/web/app/views/clients/_recordingManager.html.erb b/web/app/views/clients/_recordingManager.html.erb index 6a1f7f2b4..8553d47b7 100644 --- a/web/app/views/clients/_recordingManager.html.erb +++ b/web/app/views/clients/_recordingManager.html.erb @@ -1,4 +1,7 @@ + + File Manager + converting0 diff --git a/web/app/views/clients/_sync_viewer_templates.html.slim b/web/app/views/clients/_sync_viewer_templates.html.slim new file mode 100644 index 000000000..24e333b4f --- /dev/null +++ b/web/app/views/clients/_sync_viewer_templates.html.slim @@ -0,0 +1,206 @@ +script type="text/template" id='template-sync-viewer' + .sync-viewer + .headline + .knobs + | + .in-progress + .download-progress + .paused Downloads paused. + .quiet No download work. + .busy + .upload-progress + .paused Uploads paused. + .quiet No upload work. + .busy + .dialog-tabs + a.tab.selected purpose="recordings" Recordings + a.tab purpose="log" + | Log + span.badge ! + .tab.tab-content purpose="recordings" + .list-header + .type-info + span.special-text file details + .client-state-info + span.special-text is the file on your system? + .upload-state-info + span.special-text is it uploaded? + .list + + .tab.tab-content purpose="log" + .list-header + .list-column + span.special-text file details + .list-column + span.special-text did it succeed? + .list-column + span.special-text more detail + .log-list + + .paginator-holder + + + +script type="text/template" id='template-sync-viewer-recorded-track' + .recorded-track.sync data-id="{{data.id}}" data-recording-id="{{data.recording_id}}" data-client-id="{{data.client_id}}" data-client-track-id="{{data.client_track_id}}" data-track-id="{{data.track_id}}" data-fully-uploaded="{{data.fully_uploaded}}" data-instrument-id="{{data.instrument_id}}" + .type + span.text TRACK + a.avatar-tiny href="#" user-id="{{data.user.id}}" hoveraction="musician" + img src="{{JK.resolveAvatarUrl(data.user.photo_url)}}" + img.instrument-icon data-instrument-id="{{data.instrument_id}}" hoveraction="instrument" src="{{JK.getInstrumentIconMap24()[data.instrument_id].asset}}" + .client-state.bar + .progress + span.msg + a.retry href='#' + = image_tag('content/icon_resync.png', width:12, height: 14) + .upload-state.bar + .progress + span.msg + a.retry href='#' + = image_tag('content/icon_resync.png', width:12, height: 14) + + +script type="text/template" id='template-sync-viewer-stream-mix' + .stream-mix.sync data-id="{{data.id}}" data-recording-id="{{data.recording_id}}" + .type + span.text STREAM MIX + .client-state.bar + .progress + span.msg + .upload-state.bar + .progress + span.msg + a.retry href='#' + = image_tag('content/icon_resync.png', width:12, height: 14) + + +script type="text/template" id='template-sync-viewer-mix' + .mix.sync.virtual data-id="{{data.id}}" data-recording-id="{{data.recording_id}}" + .type + span.text MIX + span.duration + | {{window.JK.prettyPrintSeconds(data.duration)}} + .mix-state.bar + span.msg + +script type="text/template" id='template-sync-viewer-no-syncs' + .no-syncs + | You have no recordings. + +script type="text/template" id="template-sync-viewer-recording-wrapper-details" + .details + a.session-detail-page href="{{data.recording_landing_url}}" rel="external" data-session-id="{{data.recording_id}}" + span.name + | {{data.my ? data.my.name : 'Unknown Name'}} + span.export + span.timeago + | {{$.timeago(data.created_at)}} + +script type="text/template" id="template-sync-viewer-hover-recorded-track" + .help-hover-recorded-tracks + + .client-box + .client-state-info + span.special-text is the file on your system? + .client-state class="{{data.clientStateClass}}" + span.msg + | {{data.clientStateMsg}} + .client-state-definition.sync-definition + | {{data.clientStateDefinition}} + .upload-box + .upload-state-info + span.special-text is it uploaded? + .upload-state class="{{data.uploadStateClass}}" + span.msg + | {{data.uploadStateMsg}} + .upload-state-definition.sync-definition + | {{data.uploadStateDefinition}} + br clear="both" + + | {% if(data.summary) { %} + .summary + .title what's next? + | {{data.summary}} + | {% } %} + +script type="text/template" id="template-sync-viewer-hover-stream-mix" + .help-hover-stream-mix + + .client-box + .client-state-info + span.special-text is the file on your system? + .client-state class="{{data.clientStateClass}}" + span.msg + | {{data.clientStateMsg}} + .client-state-definition.sync-definition + | {{data.clientStateDefinition}} + .upload-box + .upload-state-info + span.special-text is it uploaded? + .upload-state class="{{data.uploadStateClass}}" + span.msg + | {{data.uploadStateMsg}} + .upload-state-definition.sync-definition + | {{data.uploadStateDefinition}} + br clear="both" + + | {% if(data.summary) { %} + .summary + .title what's next? + | {{data.summary}} + | {% } %} + + +script type="text/template" id="template-sync-viewer-hover-mix" + .help-hover-mix + .mix-box + .mix-state-info + span.special-text is it mixed? + .mix-state class="{{data.mixStateClass}} {{data.context}}" + span.msg + | {{data.mixStateMsg}} + .mix-state-definition.sync-definition + | {{data.mixStateDefinition}} + br clear="both" + | {% if(data.summary) { %} + .summary + .title what's next? + | {{data.summary}} + | {% } %} + + +script type="text/template" id="template-sync-viewer-download-progress-reset" + span.notice + | No download in progress. + +script type="text/template" id="template-sync-viewer-download-progress-reset" + span.notice + | No download in progress. + +script type="text/template" id="template-sync-viewer-generic-command" + .generic.sync.command + .type + span.text + | {{data.displayType}} + +script type="text/template" id="template-sync-viewer-recorded-track-command" + .recorded-track.sync + .type + .progress + span.text + | {{data.action}} TRACK + a.avatar-tiny href="#" user-id="{{data.user.id}}" hoveraction="musician" + img src="{{JK.resolveAvatarUrl(data.user.photo_url)}}" + img.instrument-icon data-instrument-id="{{data.instrument_id}}" hoveraction="instrument" src="{{JK.getInstrumentIconMap24()[data.instrument_id].asset}}" + +script type="text/template" id="template-sync-viewer-log-item" + .log class="success-{{data.success}}" + .command + | {% if(data.isAsap) { %} + = image_tag('content/icon_resync.png', width:12, height: 14, title: 'This means you initiated this') + | {% } %} + | {{data.command}} + .success + | {{data.displaySuccess}} + .detail + | {{data.detail}} \ No newline at end of file diff --git a/web/app/views/clients/index.html.erb b/web/app/views/clients/index.html.erb index f310fa583..82abf884c 100644 --- a/web/app/views/clients/index.html.erb +++ b/web/app/views/clients/index.html.erb @@ -57,6 +57,7 @@ <%= render "notify" %> <%= render "client_update" %> <%= render "overlay_small" %> +<%= render "sync_viewer_templates" %> <%= render "help" %> <%= render 'dialogs/dialogs' %>
@@ -124,7 +125,7 @@ // This is a helper class with a singleton. No need to instantiate. JK.GenreSelectorHelper.initialize(JK.app); - var recordingManager = new JK.RecordingManager(); + var recordingManager = new JK.RecordingManager(JK.app); var acceptFriendRequestDialog = new JK.AcceptFriendRequestDialog(JK.app); @@ -250,6 +251,9 @@ var changeSearchLocationDialog = new JK.ChangeSearchLocationDialog(JK.app); changeSearchLocationDialog.initialize(); + var allSyncsDialog = new JK.AllSyncsDialog(JK.app); + allSyncsDialog.initialize(); + // do a client update early check upon initialization JK.ClientUpdateInstance.check() @@ -267,6 +271,8 @@ return; } + JK.RecordingUtils.init(); + // Let's get things rolling... if (JK.currentUserId) { diff --git a/web/app/views/dialogs/_allSyncsDialog.html.slim b/web/app/views/dialogs/_allSyncsDialog.html.slim new file mode 100644 index 000000000..e880c48f7 --- /dev/null +++ b/web/app/views/dialogs/_allSyncsDialog.html.slim @@ -0,0 +1,14 @@ +.dialog.dialog-overlay-sm layout='dialog' layout-id='all-syncs-dialog' id='all-syncs-dialog' + .content-head + = image_tag "content/icon_findsession.png", {:width => 19, :height => 19, :class => 'content-icon' } + h1 Your Files + .dialog-inner + p.instructions-header Files that belong to you are shown below. You can see if they are currently on your system, and in the case of Recordings, if they have been uploaded to the server yet. + + + .syncs + + .buttons + .right + a.button-grey.btnClose layout-action="close" CLOSE + diff --git a/web/app/views/dialogs/_dialogs.html.haml b/web/app/views/dialogs/_dialogs.html.haml index 28a85f986..69c332a5c 100644 --- a/web/app/views/dialogs/_dialogs.html.haml +++ b/web/app/views/dialogs/_dialogs.html.haml @@ -6,7 +6,6 @@ = render 'dialogs/configure_tracks_dialog' = render 'dialogs/edit_recording_dialog' = render 'dialogs/invitationDialog' -= render 'dialogs/whatsNextDialog' = render 'dialogs/shareDialog' = render 'dialogs/networkTestDialog' = render 'dialogs/recordingFinishedDialog' @@ -28,3 +27,4 @@ = render 'dialogs/gettingStartedDialog' = render 'dialogs/joinTestSessionDialog' = render 'dialogs/changeSearchLocationDialog' += render 'dialogs/allSyncsDialog' diff --git a/web/app/views/users/_feed_recording.html.haml b/web/app/views/users/_feed_recording.html.haml index e81203183..4753b7af2 100644 --- a/web/app/views/users/_feed_recording.html.haml +++ b/web/app/views/users/_feed_recording.html.haml @@ -21,16 +21,16 @@ / timeline and controls .recording-controls-holder / recording play controls - .recording-controls{ class: feed_item.candidate_claimed_recording.has_mix? ? 'has-mix' : 'no-mix'} + .recording-controls.mix / play button %a.left.play-button{:href => "#"} = image_tag 'content/icon_playbutton.png', width:20, height:20, class:'play-icon' - if feed_item.has_mix? %audio{preload: 'none'} - %source{src: claimed_recording_download_url(feed_item.candidate_claimed_recording.id, 'mp3'), type:'audio/mpeg'} - %source{src: claimed_recording_download_url(feed_item.candidate_claimed_recording.id, 'ogg'), type:'audio/ogg'} + %source{src: listen_mix_url(feed_item)[:mp3_url], type:'audio/mpeg'} + %source{src: listen_mix_url(feed_item)[:ogg_url], type:'audio/ogg'} .recording-status - %span.status-text STILL MIXING + %span.status-text .recording-duration = recording_duration(feed_item) / playback position @@ -94,5 +94,5 @@ %br/ :javascript $(function () { - new window.JK.FeedItemRecording($('.feed-entry[data-claimed-recording-id="#{feed_item.candidate_claimed_recording.id}"]')); + new window.JK.FeedItemRecording($('.feed-entry[data-claimed-recording-id="#{feed_item.candidate_claimed_recording.id}"]'), {mix_state: #{feed_item.mix_state.to_json}}); }) diff --git a/web/app/views/users/_feed_recording_ajax.html.haml b/web/app/views/users/_feed_recording_ajax.html.haml index 6e3eb140d..11438e92f 100644 --- a/web/app/views/users/_feed_recording_ajax.html.haml +++ b/web/app/views/users/_feed_recording_ajax.html.haml @@ -24,7 +24,7 @@ / timeline and controls .right.w40 / recording play controls - .recording-controls{ class: '{{data.mix_class}}'} + .recording-controls.mix{ class: '{{data.feed_item.mix_info.mixStateClass}} {{data.mix_class}}'} / play button %a.left.play-button{:href => "#"} = image_tag 'content/icon_playbutton.png', width:20, height:20, class:'play-icon' @@ -34,7 +34,9 @@ %source{src: '{{data.candidate_claimed_recording.mix.ogg_url}}', type:'audio/ogg'} = "{% } %}" .recording-status - %span.status-text STILL MIXING + %span.status-text + = '{{data.feed_item.mix_info.mixStateMsg}}' + = render 'help_launcher' .recording-duration %time{class: 'tick-duration duration', duration: '{{data.feed_item.duration}}'} = '{{data.feed_item.duration}}' @@ -101,5 +103,6 @@ = '{% } %}' = '{% }) %}' + %br{:clear => "all"}/ %br/ diff --git a/web/app/views/users/_latest.html.haml b/web/app/views/users/_latest.html.haml index ffbecc30e..470f3a1f3 100644 --- a/web/app/views/users/_latest.html.haml +++ b/web/app/views/users/_latest.html.haml @@ -3,4 +3,4 @@ %h2.latest-head Latest Sessions & Recordings .latest-body .session-list-wrapper.content-scroller - = render :partial => "feed_item", :collection => @promo_latest + = render :partial => "feed_item", :collection => @promo_latest \ No newline at end of file diff --git a/web/config/application.rb b/web/config/application.rb index 6947079af..ed7798fbf 100644 --- a/web/config/application.rb +++ b/web/config/application.rb @@ -39,7 +39,7 @@ if defined?(Bundler) # Activate observers that should always be running. # config.active_record.observers = :cacher, :garbage_collector, :forum_observer - config.active_record.observers = "JamRuby::InvitedUserObserver", "JamRuby::UserObserver", "JamRuby::FeedbackObserver", "JamRuby::RecordedTrackObserver" + config.active_record.observers = "JamRuby::InvitedUserObserver", "JamRuby::UserObserver", "JamRuby::FeedbackObserver", "JamRuby::RecordedTrackObserver", "JamRuby::QuickMixObserver" # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone. # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC. @@ -247,7 +247,7 @@ if defined?(Bundler) # packet size (bytes) of test config.ftue_network_test_packet_size = 60 # number of times that the backend retries before giving up - config.ftue_network_test_backend_retries = 10 + config.ftue_network_test_backend_retries = 3 # amount of time that we want passed until we run the next network test config.ftue_network_test_min_wait_since_last_score = 5 # the maximum amount of allowable latency diff --git a/web/config/routes.rb b/web/config/routes.rb index 5bdab1017..bd557572d 100644 --- a/web/config/routes.rb +++ b/web/config/routes.rb @@ -317,6 +317,11 @@ SampleApp::Application.routes.draw do match '/users/:id/plays' => 'api_users#add_play', :via => :post, :as => 'api_users_add_play' match '/users/:id/affiliate' => 'api_users#affiliate_report', :via => :get, :as => 'api_users_affiliate' + # downloads/uploads + match '/users/:id/syncs' => 'api_user_syncs#index', :via => :get + match '/users/:id/syncs/:user_sync_id' => 'api_user_syncs#show', :via => :get + + # bands match '/bands' => 'api_bands#index', :via => :get match '/bands/validate' => 'api_bands#validate', :via => :post @@ -396,11 +401,16 @@ SampleApp::Application.routes.draw do match '/recordings/:id/comments' => 'api_recordings#add_comment', :via => :post, :as => 'api_recordings_add_comment' match '/recordings/:id/likes' => 'api_recordings#add_like', :via => :post, :as => 'api_recordings_add_like' match '/recordings/:id/discard' => 'api_recordings#discard', :via => :post, :as => 'api_recordings_discard' + match '/recordings/:id/tracks/:track_id' => 'api_recordings#show_recorded_track', :via => :get, :as => 'api_recordings_show_recorded_track' match '/recordings/:id/tracks/:track_id/download' => 'api_recordings#download', :via => :get, :as => 'api_recordings_download' match '/recordings/:id/tracks/:track_id/upload_next_part' => 'api_recordings#upload_next_part', :via => :get match '/recordings/:id/tracks/:track_id/upload_sign' => 'api_recordings#upload_sign', :via => :get match '/recordings/:id/tracks/:track_id/upload_part_complete' => 'api_recordings#upload_part_complete', :via => :post match '/recordings/:id/tracks/:track_id/upload_complete' => 'api_recordings#upload_complete', :via => :post + match '/recordings/:id/stream_mix/upload_next_part' => 'api_recordings#upload_next_part_stream_mix', :via => :get + match '/recordings/:id/stream_mix/upload_sign' => 'api_recordings#upload_sign_stream_mix', :via => :get + match '/recordings/:id/stream_mix/upload_part_complete' => 'api_recordings#upload_part_complete_stream_mix', :via => :post + match '/recordings/:id/stream_mix/upload_complete' => 'api_recordings#upload_complete_stream_mix', :via => :post # Claimed Recordings match '/claimed_recordings' => 'api_claimed_recordings#index', :via => :get diff --git a/web/spec/controllers/api_claimed_recordings_spec.rb b/web/spec/controllers/api_claimed_recordings_spec.rb index 540ff2cf5..b52d6e4a2 100644 --- a/web/spec/controllers/api_claimed_recordings_spec.rb +++ b/web/spec/controllers/api_claimed_recordings_spec.rb @@ -18,15 +18,16 @@ describe ApiClaimedRecordingsController do @genre = FactoryGirl.create(:genre) @recording.claim(@user, "name", "description", @genre, true) @recording.reload - @claimed_recording = @recording.claimed_recordings.first + @claimed_recording = @recording.claimed_recordings.first! + @recording.has_mix?.should be_false + end - describe "GET 'show'" do + describe "show" do it "should show the right thing when one recording just finished" do - pending controller.current_user = @user - get :show, :id => @claimed_recording.id + get :show, {format:'json', id: @claimed_recording.id} response.should be_success json = JSON.parse(response.body) json.should_not be_nil @@ -38,8 +39,9 @@ describe ApiClaimedRecordingsController do json["recording"]["recorded_tracks"].first["id"].should == @recording.recorded_tracks.first.id json["recording"]["recorded_tracks"].first["instrument_id"].should == @instrument.id json["recording"]["recorded_tracks"].first["user"]["id"].should == @user.id + json["mix"].should be_nil end - + it "should show the right thing when one recording was just uploaded" do pending @recording.recorded_tracks.first.upload_complete @@ -71,7 +73,7 @@ describe ApiClaimedRecordingsController do end end - describe "GET 'index'" do + describe "index" do it "should generate a single output" do pending controller.current_user = @user @@ -84,6 +86,83 @@ describe ApiClaimedRecordingsController do end end + + describe "download" do + it "no mix" do + controller.current_user = @user + get :download, id: @claimed_recording.id + response.status.should == 404 + end + + it "quick mix, not completed" do + quick_mix = FactoryGirl.create(:quick_mix) + controller.current_user = @user + puts quick_mix.recording.candidate_claimed_recording + get :download, id: quick_mix.recording.candidate_claimed_recording.id + response.status.should == 404 + end + + it "quick mix, completed" do + quick_mix = FactoryGirl.create(:quick_mix_completed) + controller.current_user = @user + get :download, id: quick_mix.recording.candidate_claimed_recording.id + response.status.should == 302 + response.headers['Location'].include?('recordings/ogg').should be_true + + get :download, id: quick_mix.recording.candidate_claimed_recording.id, type:'mp3' + response.status.should == 302 + response.headers['Location'].include?('recordings/mp3').should be_true + end + + it "final mix, not completed" do + mix = FactoryGirl.create(:mix, completed:false) + controller.current_user = @user + get :download, id: mix.recording.candidate_claimed_recording.id + response.status.should == 404 + end + + it "final mix, completed" do + mix = FactoryGirl.create(:mix, completed:true) + controller.current_user = @user + get :download, id: mix.recording.candidate_claimed_recording.id + response.status.should == 302 + response.headers['Location'].include?('recordings/mixed/ogg').should be_true + + get :download, id: mix.recording.candidate_claimed_recording.id, type:'mp3' + response.status.should == 302 + response.headers['Location'].include?('recordings/mixed/mp3').should be_true + end + + it "both completed final mix and stream mix" do + # when both quick mix and mix are present, this should let mix win + mix = FactoryGirl.create(:mix, completed:true) + quick_mix = FactoryGirl.create(:quick_mix_completed, recording: mix.recording, user: mix.recording.owner) + controller.current_user = @user + get :download, id: mix.recording.candidate_claimed_recording.id + response.status.should == 302 + response.headers['Location'].include?('recordings/mixed/ogg').should be_true + + get :download, id: mix.recording.candidate_claimed_recording.id, type:'mp3' + response.status.should == 302 + response.headers['Location'].include?('recordings/mixed/mp3').should be_true + end + + it "completed stream mix, incomplete final mix" do + # when both quick mix and mix are present, this should let mix win + mix = FactoryGirl.create(:mix, completed:false) + quick_mix = FactoryGirl.create(:quick_mix_completed, recording: mix.recording, user: mix.recording.owner) + controller.current_user = @user + get :download, id: mix.recording.candidate_claimed_recording.id + response.status.should == 302 + response.headers['Location'].include?('recordings/ogg').should be_true + + get :download, id: quick_mix.recording.candidate_claimed_recording.id, type:'mp3' + response.status.should == 302 + response.headers['Location'].include?('recordings/mp3').should be_true + end + + + end =begin We can't test these because rspec doesn't like that we return 204. It causes rails to return a 406. diff --git a/web/spec/controllers/api_recordings_controller_spec.rb b/web/spec/controllers/api_recordings_controller_spec.rb index b500193a3..e0691cb2d 100644 --- a/web/spec/controllers/api_recordings_controller_spec.rb +++ b/web/spec/controllers/api_recordings_controller_spec.rb @@ -15,7 +15,7 @@ describe ApiRecordingsController do describe "start" do it "should work" do - post :start, { :format => 'json', :music_session_id => @music_session.id } + post :start, { :format => 'json', :music_session_id => @music_session.id } response.should be_success response_body = JSON.parse(response.body) response_body['id'].should_not be_nil diff --git a/web/spec/controllers/api_user_syncs_controller_spec.rb b/web/spec/controllers/api_user_syncs_controller_spec.rb new file mode 100644 index 000000000..5e88703a3 --- /dev/null +++ b/web/spec/controllers/api_user_syncs_controller_spec.rb @@ -0,0 +1,163 @@ +require 'spec_helper' + +describe ApiUserSyncsController do + render_views + + + let(:user1) {FactoryGirl.create(:user)} + let(:user2) {FactoryGirl.create(:user)} + + before(:each) { + controller.current_user = user1 + } + + it "requires logged in" do + controller.current_user = nil + get :index + json = JSON.parse(response.body, :symbolize_names => true) + json.should == {message: "not logged in"} + end + + it "can return empty results" do + get :index, { :format => 'json', :id => user1.id } + json = JSON.parse(response.body, :symbolize_names => true) + json[:next].should be_nil + json[:entries].length.should == 0 + end + + describe "one recording with two users" do + let!(:recording1) { + recording = FactoryGirl.create(:recording, owner: user1, band: nil, duration:1) + recording.recorded_tracks << FactoryGirl.create(:recorded_track, recording: recording, user: recording.owner) + recording.recorded_tracks << FactoryGirl.create(:recorded_track, recording: recording, user: user2) + recording.save! + recording.reload + recording + } + + let(:sorted_tracks) { + Array.new(recording1.recorded_tracks).sort_by! {|rt| -rt.id} + } + + it "no claimed_recordings" do + # every is supposed to upload immediately, but no downloads until you try claim it. The assertions below validate this + get :index, { :format => 'json', :id => user1.id } + json = JSON.parse(response.body, :symbolize_names => true) + json[:next].should be_nil + json[:entries].length.should == 2 + + recorded_track1 = json[:entries][0] + recorded_track1[:upload][:should_upload].should be_true + recorded_track1[:upload][:too_many_upload_failures].should be_false + recorded_track1[:download][:should_download].should be_false + recorded_track1[:download][:too_many_downloads].should be_false + + recorded_track2 = json[:entries][1] + recorded_track2[:upload][:should_upload].should be_true + recorded_track2[:upload][:too_many_upload_failures].should be_false + recorded_track2[:download][:should_download].should be_false + recorded_track2[:download][:too_many_downloads].should be_false + end + + it "recording isn't over" do + recording1.duration = nil + recording1.save! + + get :index, { :format => 'json', :id => user1.id } + json = JSON.parse(response.body, :symbolize_names => true) + json[:next].should be_nil + json[:entries].length.should == 0 + end + + it "one user decides to keep the recording" do + + FactoryGirl.create(:claimed_recording, user: user1, recording: recording1, discarded:false) + + get :index, { :format => 'json', :id => user1.id } + json = JSON.parse(response.body, :symbolize_names => true) + json[:next].should be_nil + json[:entries].length.should == 2 + + recorded_track1 = json[:entries][0] + recorded_track1[:upload][:should_upload].should be_true + recorded_track1[:upload][:too_many_upload_failures].should be_false + recorded_track1[:download][:should_download].should be_true + recorded_track1[:download][:too_many_downloads].should be_false + + recorded_track2 = json[:entries][1] + recorded_track2[:upload][:should_upload].should be_true + recorded_track2[:upload][:too_many_upload_failures].should be_false + recorded_track2[:download][:should_download].should be_true + recorded_track2[:download][:too_many_downloads].should be_false + + controller.current_user = user2 + get :index, { :format => 'json', :id => user2.id } + json = JSON.parse(response.body, :symbolize_names => true) + json[:next].should be_nil + json[:entries].length.should == 2 + + recorded_track1 = json[:entries][0] + recorded_track1[:upload][:should_upload].should be_true + recorded_track1[:upload][:too_many_upload_failures].should be_false + recorded_track1[:download][:should_download].should be_false + recorded_track1[:download][:too_many_downloads].should be_false + + recorded_track2 = json[:entries][1] + recorded_track2[:upload][:should_upload].should be_true + recorded_track2[:upload][:too_many_upload_failures].should be_false + recorded_track2[:download][:should_download].should be_false + recorded_track2[:download][:too_many_downloads].should be_false + end + + it "one user decides to discard the recording" do + FactoryGirl.create(:claimed_recording, user: user1, recording: recording1, discarded:true) + + get :index, { :format => 'json', :id => user1.id } + json = JSON.parse(response.body, :symbolize_names => true) + json[:next].should be_nil + json[:entries].length.should == 2 + + recorded_track1 = json[:entries][0] + recorded_track1[:upload][:should_upload].should be_true + recorded_track1[:upload][:too_many_upload_failures].should be_false + recorded_track1[:download][:should_download].should be_false + recorded_track1[:download][:too_many_downloads].should be_false + + recorded_track2 = json[:entries][1] + recorded_track2[:upload][:should_upload].should be_true + recorded_track2[:upload][:too_many_upload_failures].should be_false + recorded_track2[:download][:should_download].should be_false + recorded_track2[:download][:too_many_downloads].should be_false + + controller.current_user = user2 + get :index, { :format => 'json', :id => user2.id } + json = JSON.parse(response.body, :symbolize_names => true) + json[:next].should be_nil + json[:entries].length.should == 2 + + recorded_track1 = json[:entries][0] + recorded_track1[:upload][:should_upload].should be_true + recorded_track1[:upload][:too_many_upload_failures].should be_false + recorded_track1[:download][:should_download].should be_false + recorded_track1[:download][:too_many_downloads].should be_false + + recorded_track2 = json[:entries][1] + recorded_track2[:upload][:should_upload].should be_true + recorded_track2[:upload][:too_many_upload_failures].should be_false + recorded_track2[:download][:should_download].should be_false + recorded_track2[:download][:too_many_downloads].should be_false + end + + it "both users decide to discard the recording" do + recording1.all_discarded = true + recording1.save! + + get :index, { :format => 'json', :id => user1.id } + json = JSON.parse(response.body, :symbolize_names => true) + json[:next].should be_nil + json[:entries].length.should == 0 + + end + end + +end diff --git a/web/spec/factories.rb b/web/spec/factories.rb index d82706fe3..bf81273e7 100644 --- a/web/spec/factories.rb +++ b/web/spec/factories.rb @@ -309,9 +309,41 @@ FactoryGirl.define do association :user, factory: :user before(:create) { |claimed_recording| - claimed_recording.recording = FactoryGirl.create(:recording_with_track, owner: claimed_recording.user) + claimed_recording.recording = FactoryGirl.create(:recording_with_track, owner: claimed_recording.user) unless claimed_recording.recording + } + end + + factory :quick_mix, :class => JamRuby::QuickMix do + started_at nil + completed_at nil + ogg_md5 nil + ogg_length 0 + ogg_url nil + mp3_md5 nil + mp3_length 0 + mp3_url nil + completed false + + before(:create) {|mix, evaluator| + user = FactoryGirl.create(:user) + mix.user = user + if evaluator.recording.nil? + mix.recording = FactoryGirl.create(:recording_with_track, owner: user) + mix.recording.claimed_recordings << FactoryGirl.create(:claimed_recording, user: user, recording: mix.recording) + end } + factory :quick_mix_completed do + started_at 1.minute.ago + completed_at Time.now + ogg_md5 'a' + ogg_length 1 + ogg_url 'recordings/ogg' + mp3_md5 'a' + mp3_length 1 + mp3_url 'recordings/mp3' + completed true + end end factory :icecast_limit, :class => JamRuby::IcecastLimit do @@ -508,16 +540,20 @@ FactoryGirl.define do completed_at Time.now ogg_md5 'abc' ogg_length 1 - sequence(:ogg_url) { |n| "recordings/ogg/#{n}" } mp3_md5 'abc' mp3_length 1 - sequence(:mp3_url) { |n| "recordings/mp3/#{n}" } + sequence(:mp3_url) { |n| "recordings/mixed/mp3/#{n}" } completed true before(:create) {|mix| user = FactoryGirl.create(:user) mix.recording = FactoryGirl.create(:recording_with_track, owner: user) mix.recording.claimed_recordings << FactoryGirl.create(:claimed_recording, user: user, recording: mix.recording) + mix.recording.has_final_mix = true + mix.recording.save! + } + after(:create) {|mix| + Mix.where(:id => mix.id).update_all(:ogg_url => 'recordings/mixed/ogg' ) # the uploader on the model prevents using typical FactoryGirl mechanisms for this } end diff --git a/web/spec/features/alt_landing_spec.rb b/web/spec/features/alt_landing_spec.rb index 813975ebb..f8663886c 100644 --- a/web/spec/features/alt_landing_spec.rb +++ b/web/spec/features/alt_landing_spec.rb @@ -12,7 +12,7 @@ describe "Alternate Landing Pages", :js => true, :type => :feature, :capybara_fe find('a.landing_wb', text: 'Watch a Video to See How to Get Started').trigger(:click) find('h1', text: 'See How to Get Started') find('div.fb-like') - find('iframe.twitter-follow-button') + find('a.twitter-follow-button') find('.g-follow-btn iframe') end @@ -24,7 +24,7 @@ describe "Alternate Landing Pages", :js => true, :type => :feature, :capybara_fe find('a.landing_wb', text: 'See How to Get Started Using JamKazam').trigger(:click) find('h1', text: 'See How to Get Started Using JamKazam') find('div.fb-like') - find('iframe.twitter-follow-button') + find('a.twitter-follow-button') find('.g-follow-btn iframe') end @@ -34,7 +34,7 @@ describe "Alternate Landing Pages", :js => true, :type => :feature, :capybara_fe find('h2', text: "It's FREE! Ready to Go?") find('h2', text: 'Not a Good Time to Sign Up?') find('div.fb-like') - find('iframe.twitter-follow-button') + find('a.twitter-follow-button') find('.g-follow-btn iframe') end @@ -44,7 +44,7 @@ describe "Alternate Landing Pages", :js => true, :type => :feature, :capybara_fe find('h2', text: "It's FREE! Ready to Go?") find('h2', text: 'Not a Good Time to Sign Up?') find('div.fb-like') - find('iframe.twitter-follow-button') + find('a.twitter-follow-button') find('.g-follow-btn iframe') end diff --git a/web/spec/javascripts/fixtures/syncViewer.html.slim b/web/spec/javascripts/fixtures/syncViewer.html.slim new file mode 100644 index 000000000..9642180dc --- /dev/null +++ b/web/spec/javascripts/fixtures/syncViewer.html.slim @@ -0,0 +1,2 @@ += render "clients/sync_viewer_templates" += render "clients/paginator" \ No newline at end of file diff --git a/web/spec/javascripts/fixtures/user_sync_track1.json b/web/spec/javascripts/fixtures/user_sync_track1.json new file mode 100644 index 000000000..83c40bc11 --- /dev/null +++ b/web/spec/javascripts/fixtures/user_sync_track1.json @@ -0,0 +1,174 @@ +{ + "next": null, + "entries": [ + { + "id": 20, + "recording_id": "52480b2a-046d-4be3-bfc0-8fd8bbb527b8", + "client_id": "7987a826-803a-4cb8-9c74-85ffba25d6d3", + "track_id": "1ac48188-a84f-4607-b074-37d252828a2e", + "client_track_id": "37bf89de-4f11-469a-a061-67e4e5451eb4", + "md5": "Nx29Yo0ku8MclPu5rmYsPg==", + "length": 159976, + "download_count": 0, + "instrument_id": "bass guitar", + "fully_uploaded": false, + "upload_failures": 0, + "part_failures": 0, + "created_at": "2014-10-15T02:50:44Z", + "type": "recorded_track", + "user": { + "id": "eab2713f-8631-4baa-b5d5-cc4eb8b2d86a", + "first_name": "Seth", + "last_name": "Call", + "photo_url": null, + "name": "Seth Call" + }, + "recording": { + "id": "52480b2a-046d-4be3-bfc0-8fd8bbb527b8", + "band": null, + "created_at": "2014-10-15T02:50:44Z", + "duration": 3, + "comment_count": 0, + "like_count": 0, + "play_count": 0, + "my": { + "name": "Cheerful4", + "description": "", + "genre": "pop", + "genre_name": "Pop" + }, + "owner": { + "id": "eab2713f-8631-4baa-b5d5-cc4eb8b2d86a", + "name": "Seth Call", + "location": "Austin, TX", + "photo_url": null + }, + "recorded_tracks": [ + { + "id": 20, + "fully_uploaded": true, + "client_track_id": "37bf89de-4f11-469a-a061-67e4e5451eb4", + "client_id": "7987a826-803a-4cb8-9c74-85ffba25d6d3", + "instrument_id": "bass guitar", + "recording_id": "52480b2a-046d-4be3-bfc0-8fd8bbb527b8", + "user": { + "id": "eab2713f-8631-4baa-b5d5-cc4eb8b2d86a", + "first_name": "Seth", + "last_name": "Call", + "city": "Austin", + "state": "TX", + "country": "US", + "location": "Austin, TX", + "photo_url": null + } + } + ], + "comments": [ + + ], + "claimed_recordings": [ + { + "id": "4fa1ac49-169a-49f2-9ae5-dd008fec121b", + "name": "Cheerful4", + "description": "", + "is_public": true, + "genre_id": "pop", + "has_mix?": false, + "user_id": "eab2713f-8631-4baa-b5d5-cc4eb8b2d86a", + "share_url": "http:\/\/localhost:3000\/s\/NJEHSKOMME", + "mix": { + "mp3_url": "http:\/\/localhost:3000\/api\/claimed_recordings\/4fa1ac49-169a-49f2-9ae5-dd008fec121b\/download\/mp3", + "ogg_url": "http:\/\/localhost:3000\/api\/claimed_recordings\/4fa1ac49-169a-49f2-9ae5-dd008fec121b\/download\/ogg" + } + } + ] + }, + "upload": { + "should_upload": true, + "too_many_upload_failures": false + }, + "download": { + "should_download": true, + "too_many_downloads": false + } + }, + { + "id": 19, + "recording_id": "3aefac5f-b960-4196-90aa-1446825b4612", + "started_at": "2014-10-15T02:47:15Z", + "completed_at": "2014-10-15T02:47:17Z", + "completed": true, + "should_retry": false, + "download_count": 1, + "state": "mixed", + "type": "mix", + "recording": { + "id": "3aefac5f-b960-4196-90aa-1446825b4612", + "band": null, + "created_at": "2014-10-15T02:46:54Z", + "duration": 3, + "comment_count": 0, + "like_count": 0, + "play_count": 0, + "my": { + "name": "Cheerful3", + "description": "", + "genre": "pop", + "genre_name": "Pop" + }, + "owner": { + "id": "eab2713f-8631-4baa-b5d5-cc4eb8b2d86a", + "name": "Seth Call", + "location": "Austin, TX", + "photo_url": null + }, + "recorded_tracks": [ + { + "id": 18, + "fully_uploaded": true, + "client_track_id": "ac651da5-7609-494a-aa2c-17287772441b", + "client_id": "7987a826-803a-4cb8-9c74-85ffba25d6d3", + "instrument_id": "bass guitar", + "recording_id": "3aefac5f-b960-4196-90aa-1446825b4612", + "user": { + "id": "eab2713f-8631-4baa-b5d5-cc4eb8b2d86a", + "first_name": "Seth", + "last_name": "Call", + "city": "Austin", + "state": "TX", + "country": "US", + "location": "Austin, TX", + "photo_url": null + } + } + ], + "comments": [ + + ], + "claimed_recordings": [ + { + "id": "870fba4c-59de-4f57-a9d2-8200bdbffba9", + "name": "Cheerful3", + "description": "", + "is_public": true, + "genre_id": "pop", + "has_mix?": true, + "user_id": "eab2713f-8631-4baa-b5d5-cc4eb8b2d86a", + "share_url": "http:\/\/localhost:3000\/s\/IBB8ACWSLOA", + "mix": { + "mp3_url": "http:\/\/localhost:3000\/api\/claimed_recordings\/870fba4c-59de-4f57-a9d2-8200bdbffba9\/download\/mp3", + "ogg_url": "http:\/\/localhost:3000\/api\/claimed_recordings\/870fba4c-59de-4f57-a9d2-8200bdbffba9\/download\/ogg" + } + } + ] + }, + "duration": 3, + "error": null, + "download": { + "should_download": true, + "too_many_downloads": false + } + } + ], + "total_entries": 2 +} \ No newline at end of file diff --git a/web/spec/javascripts/helpers/jasmine-jquery.js b/web/spec/javascripts/helpers/jasmine-jquery.js deleted file mode 100644 index 597512e7c..000000000 --- a/web/spec/javascripts/helpers/jasmine-jquery.js +++ /dev/null @@ -1,700 +0,0 @@ -/*! - Jasmine-jQuery: a set of jQuery helpers for Jasmine tests. - - Version 1.5.92 - - https://github.com/velesin/jasmine-jquery - - Copyright (c) 2010-2013 Wojciech Zawistowski, Travis Jeffery - - Permission is hereby granted, free of charge, to any person obtaining - a copy of this software and associated documentation files (the - "Software"), to deal in the Software without restriction, including - without limitation the rights to use, copy, modify, merge, publish, - distribute, sublicense, and/or sell copies of the Software, and to - permit persons to whom the Software is furnished to do so, subject to - the following conditions: - - The above copyright notice and this permission notice shall be - included in all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE - LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION - OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION - WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -+function (jasmine, $) { "use strict"; - - jasmine.spiedEventsKey = function (selector, eventName) { - return [$(selector).selector, eventName].toString() - } - - jasmine.getFixtures = function () { - return jasmine.currentFixtures_ = jasmine.currentFixtures_ || new jasmine.Fixtures() - } - - jasmine.getStyleFixtures = function () { - return jasmine.currentStyleFixtures_ = jasmine.currentStyleFixtures_ || new jasmine.StyleFixtures() - } - - jasmine.Fixtures = function () { - this.containerId = 'jasmine-fixtures' - this.fixturesCache_ = {} - this.fixturesPath = 'spec/javascripts/fixtures' - } - - jasmine.Fixtures.prototype.set = function (html) { - this.cleanUp() - return this.createContainer_(html) - } - - jasmine.Fixtures.prototype.appendSet= function (html) { - this.addToContainer_(html) - } - - jasmine.Fixtures.prototype.preload = function () { - this.read.apply(this, arguments) - } - - jasmine.Fixtures.prototype.load = function () { - this.cleanUp() - this.createContainer_(this.read.apply(this, arguments)) - } - - jasmine.Fixtures.prototype.appendLoad = function () { - this.addToContainer_(this.read.apply(this, arguments)) - } - - jasmine.Fixtures.prototype.read = function () { - var htmlChunks = [] - , fixtureUrls = arguments - - for(var urlCount = fixtureUrls.length, urlIndex = 0; urlIndex < urlCount; urlIndex++) { - htmlChunks.push(this.getFixtureHtml_(fixtureUrls[urlIndex])) - } - - return htmlChunks.join('') - } - - jasmine.Fixtures.prototype.clearCache = function () { - this.fixturesCache_ = {} - } - - jasmine.Fixtures.prototype.cleanUp = function () { - $('#' + this.containerId).remove() - } - - jasmine.Fixtures.prototype.sandbox = function (attributes) { - var attributesToSet = attributes || {} - return $('
').attr(attributesToSet) - } - - jasmine.Fixtures.prototype.createContainer_ = function (html) { - var container = $('
') - .attr('id', this.containerId) - .html(html) - - $(document.body).append(container) - return container - } - - jasmine.Fixtures.prototype.addToContainer_ = function (html){ - var container = $(document.body).find('#'+this.containerId).append(html) - if(!container.length){ - this.createContainer_(html) - } - } - - jasmine.Fixtures.prototype.getFixtureHtml_ = function (url) { - if (typeof this.fixturesCache_[url] === 'undefined') { - this.loadFixtureIntoCache_(url) - } - return this.fixturesCache_[url] - } - - jasmine.Fixtures.prototype.loadFixtureIntoCache_ = function (relativeUrl) { - var self = this - , url = this.makeFixtureUrl_(relativeUrl) - , request = $.ajax({ - async: false, // must be synchronous to guarantee that no tests are run before fixture is loaded - cache: false, - url: url, - success: function (data, status, $xhr) { - self.fixturesCache_[relativeUrl] = $xhr.responseText - }, - error: function (jqXHR, status, errorThrown) { - throw new Error('Fixture could not be loaded: ' + url + ' (status: ' + status + ', message: ' + errorThrown.message + ')') - } - }) - } - - jasmine.Fixtures.prototype.makeFixtureUrl_ = function (relativeUrl){ - return this.fixturesPath.match('/$') ? this.fixturesPath + relativeUrl : this.fixturesPath + '/' + relativeUrl - } - - jasmine.Fixtures.prototype.proxyCallTo_ = function (methodName, passedArguments) { - return this[methodName].apply(this, passedArguments) - } - - - jasmine.StyleFixtures = function () { - this.fixturesCache_ = {} - this.fixturesNodes_ = [] - this.fixturesPath = 'spec/javascripts/fixtures' - } - - jasmine.StyleFixtures.prototype.set = function (css) { - this.cleanUp() - this.createStyle_(css) - } - - jasmine.StyleFixtures.prototype.appendSet = function (css) { - this.createStyle_(css) - } - - jasmine.StyleFixtures.prototype.preload = function () { - this.read_.apply(this, arguments) - } - - jasmine.StyleFixtures.prototype.load = function () { - this.cleanUp() - this.createStyle_(this.read_.apply(this, arguments)) - } - - jasmine.StyleFixtures.prototype.appendLoad = function () { - this.createStyle_(this.read_.apply(this, arguments)) - } - - jasmine.StyleFixtures.prototype.cleanUp = function () { - while(this.fixturesNodes_.length) { - this.fixturesNodes_.pop().remove() - } - } - - jasmine.StyleFixtures.prototype.createStyle_ = function (html) { - var styleText = $('
').html(html).text() - , style = $('') - - this.fixturesNodes_.push(style) - $('head').append(style) - } - - jasmine.StyleFixtures.prototype.clearCache = jasmine.Fixtures.prototype.clearCache - jasmine.StyleFixtures.prototype.read_ = jasmine.Fixtures.prototype.read - jasmine.StyleFixtures.prototype.getFixtureHtml_ = jasmine.Fixtures.prototype.getFixtureHtml_ - jasmine.StyleFixtures.prototype.loadFixtureIntoCache_ = jasmine.Fixtures.prototype.loadFixtureIntoCache_ - jasmine.StyleFixtures.prototype.makeFixtureUrl_ = jasmine.Fixtures.prototype.makeFixtureUrl_ - jasmine.StyleFixtures.prototype.proxyCallTo_ = jasmine.Fixtures.prototype.proxyCallTo_ - - jasmine.getJSONFixtures = function () { - return jasmine.currentJSONFixtures_ = jasmine.currentJSONFixtures_ || new jasmine.JSONFixtures() - } - - jasmine.JSONFixtures = function () { - this.fixturesCache_ = {} - this.fixturesPath = 'spec/javascripts/fixtures/json' - } - - jasmine.JSONFixtures.prototype.load = function () { - this.read.apply(this, arguments) - return this.fixturesCache_ - } - - jasmine.JSONFixtures.prototype.read = function () { - var fixtureUrls = arguments - - for(var urlCount = fixtureUrls.length, urlIndex = 0; urlIndex < urlCount; urlIndex++) { - this.getFixtureData_(fixtureUrls[urlIndex]) - } - - return this.fixturesCache_ - } - - jasmine.JSONFixtures.prototype.clearCache = function () { - this.fixturesCache_ = {} - } - - jasmine.JSONFixtures.prototype.getFixtureData_ = function (url) { - if (!this.fixturesCache_[url]) this.loadFixtureIntoCache_(url) - return this.fixturesCache_[url] - } - - jasmine.JSONFixtures.prototype.loadFixtureIntoCache_ = function (relativeUrl) { - var self = this - , url = this.fixturesPath.match('/$') ? this.fixturesPath + relativeUrl : this.fixturesPath + '/' + relativeUrl - - $.ajax({ - async: false, // must be synchronous to guarantee that no tests are run before fixture is loaded - cache: false, - dataType: 'json', - url: url, - success: function (data) { - self.fixturesCache_[relativeUrl] = data - }, - error: function (jqXHR, status, errorThrown) { - throw new Error('JSONFixture could not be loaded: ' + url + ' (status: ' + status + ', message: ' + errorThrown.message + ')') - } - }) - } - - jasmine.JSONFixtures.prototype.proxyCallTo_ = function (methodName, passedArguments) { - return this[methodName].apply(this, passedArguments) - } - - jasmine.JQuery = function () {} - - jasmine.JQuery.browserTagCaseIndependentHtml = function (html) { - return $('
').append(html).html() - } - - jasmine.JQuery.elementToString = function (element) { - return $(element).map(function () { return this.outerHTML; }).toArray().join(', ') - } - - jasmine.JQuery.matchersClass = {} - - !function (namespace) { - var data = { - spiedEvents: {} - , handlers: [] - } - - namespace.events = { - spyOn: function (selector, eventName) { - var handler = function (e) { - data.spiedEvents[jasmine.spiedEventsKey(selector, eventName)] = jasmine.util.argsToArray(arguments) - } - - $(selector).on(eventName, handler) - data.handlers.push(handler) - - return { - selector: selector, - eventName: eventName, - handler: handler, - reset: function (){ - delete data.spiedEvents[jasmine.spiedEventsKey(selector, eventName)] - } - } - }, - - args: function (selector, eventName) { - var actualArgs = data.spiedEvents[jasmine.spiedEventsKey(selector, eventName)] - - if (!actualArgs) { - throw "There is no spy for " + eventName + " on " + selector.toString() + ". Make sure to create a spy using spyOnEvent." - } - - return actualArgs - }, - - wasTriggered: function (selector, eventName) { - return !!(data.spiedEvents[jasmine.spiedEventsKey(selector, eventName)]) - }, - - wasTriggeredWith: function (selector, eventName, expectedArgs, env) { - var actualArgs = jasmine.JQuery.events.args(selector, eventName).slice(1) - if (Object.prototype.toString.call(expectedArgs) !== '[object Array]') { - actualArgs = actualArgs[0] - } - return env.equals_(expectedArgs, actualArgs) - }, - - wasPrevented: function (selector, eventName) { - var args = data.spiedEvents[jasmine.spiedEventsKey(selector, eventName)] - , e = args ? args[0] : undefined - - return e && e.isDefaultPrevented() - }, - - wasStopped: function (selector, eventName) { - var args = data.spiedEvents[jasmine.spiedEventsKey(selector, eventName)] - , e = args ? args[0] : undefined - return e && e.isPropagationStopped() - }, - - cleanUp: function () { - data.spiedEvents = {} - data.handlers = [] - } - } - }(jasmine.JQuery) - - !function (){ - var jQueryMatchers = { - toHaveClass: function (className) { - return this.actual.hasClass(className) - }, - - toHaveCss: function (css){ - for (var prop in css){ - var value = css[prop] - // see issue #147 on gh - ;if (value === 'auto' && this.actual.get(0).style[prop] === 'auto') continue - if (this.actual.css(prop) !== value) return false - } - return true - }, - - toBeVisible: function () { - return this.actual.is(':visible') - }, - - toBeHidden: function () { - return this.actual.is(':hidden') - }, - - toBeSelected: function () { - return this.actual.is(':selected') - }, - - toBeChecked: function () { - return this.actual.is(':checked') - }, - - toBeEmpty: function () { - return this.actual.is(':empty') - }, - - toExist: function () { - return this.actual.length - }, - - toHaveLength: function (length) { - return this.actual.length === length - }, - - toHaveAttr: function (attributeName, expectedAttributeValue) { - return hasProperty(this.actual.attr(attributeName), expectedAttributeValue) - }, - - toHaveProp: function (propertyName, expectedPropertyValue) { - return hasProperty(this.actual.prop(propertyName), expectedPropertyValue) - }, - - toHaveId: function (id) { - return this.actual.attr('id') == id - }, - - toHaveHtml: function (html) { - return this.actual.html() == jasmine.JQuery.browserTagCaseIndependentHtml(html) - }, - - toContainHtml: function (html){ - var actualHtml = this.actual.html() - , expectedHtml = jasmine.JQuery.browserTagCaseIndependentHtml(html) - - return (actualHtml.indexOf(expectedHtml) >= 0) - }, - - toHaveText: function (text) { - var trimmedText = $.trim(this.actual.text()) - - if (text && $.isFunction(text.test)) { - return text.test(trimmedText) - } else { - return trimmedText == text - } - }, - - toContainText: function (text) { - var trimmedText = $.trim(this.actual.text()) - - if (text && $.isFunction(text.test)) { - return text.test(trimmedText) - } else { - return trimmedText.indexOf(text) != -1 - } - }, - - toHaveValue: function (value) { - return this.actual.val() === value - }, - - toHaveData: function (key, expectedValue) { - return hasProperty(this.actual.data(key), expectedValue) - }, - - toBe: function (selector) { - return this.actual.is(selector) - }, - - toContain: function (selector) { - return this.actual.find(selector).length - }, - - toBeMatchedBy: function (selector) { - return this.actual.filter(selector).length - }, - - toBeDisabled: function (selector){ - return this.actual.is(':disabled') - }, - - toBeFocused: function (selector) { - return this.actual[0] === this.actual[0].ownerDocument.activeElement - }, - - toHandle: function (event) { - var events = $._data(this.actual.get(0), "events") - - if(!events || !event || typeof event !== "string") { - return false - } - - var namespaces = event.split(".") - , eventType = namespaces.shift() - , sortedNamespaces = namespaces.slice(0).sort() - , namespaceRegExp = new RegExp("(^|\\.)" + sortedNamespaces.join("\\.(?:.*\\.)?") + "(\\.|$)") - - if(events[eventType] && namespaces.length) { - for(var i = 0; i < events[eventType].length; i++) { - var namespace = events[eventType][i].namespace - - if(namespaceRegExp.test(namespace)) { - return true - } - } - } else { - return events[eventType] && events[eventType].length > 0 - } - }, - - toHandleWith: function (eventName, eventHandler) { - var normalizedEventName = eventName.split('.')[0] - , stack = $._data(this.actual.get(0), "events")[normalizedEventName] - - for (var i = 0; i < stack.length; i++) { - if (stack[i].handler == eventHandler) return true - } - - return false - } - } - - var hasProperty = function (actualValue, expectedValue) { - if (expectedValue === undefined) return actualValue !== undefined - - return actualValue == expectedValue - } - - var bindMatcher = function (methodName) { - var builtInMatcher = jasmine.Matchers.prototype[methodName] - - jasmine.JQuery.matchersClass[methodName] = function () { - if (this.actual - && (this.actual instanceof $ - || jasmine.isDomNode(this.actual))) { - this.actual = $(this.actual) - var result = jQueryMatchers[methodName].apply(this, arguments) - , element - - if (this.actual.get && (element = this.actual.get()[0]) && !$.isWindow(element) && element.tagName !== "HTML") - this.actual = jasmine.JQuery.elementToString(this.actual) - - return result - } - - if (builtInMatcher) { - return builtInMatcher.apply(this, arguments) - } - - return false - } - } - - for(var methodName in jQueryMatchers) { - bindMatcher(methodName) - } - }() - - beforeEach(function () { - this.addMatchers(jasmine.JQuery.matchersClass) - this.addMatchers({ - toHaveBeenTriggeredOn: function (selector) { - this.message = function () { - return [ - "Expected event " + this.actual + " to have been triggered on " + selector, - "Expected event " + this.actual + " not to have been triggered on " + selector - ] - } - return jasmine.JQuery.events.wasTriggered(selector, this.actual) - } - }) - this.addMatchers({ - toHaveBeenTriggered: function (){ - var eventName = this.actual.eventName - , selector = this.actual.selector - - this.message = function () { - return [ - "Expected event " + eventName + " to have been triggered on " + selector, - "Expected event " + eventName + " not to have been triggered on " + selector - ] - } - - return jasmine.JQuery.events.wasTriggered(selector, eventName) - } - }) - this.addMatchers({ - toHaveBeenTriggeredOnAndWith: function () { - var selector = arguments[0] - , expectedArgs = arguments[1] - , wasTriggered = jasmine.JQuery.events.wasTriggered(selector, this.actual) - - this.message = function () { - if (wasTriggered) { - var actualArgs = jasmine.JQuery.events.args(selector, this.actual, expectedArgs)[1] - return [ - "Expected event " + this.actual + " to have been triggered with " + jasmine.pp(expectedArgs) + " but it was triggered with " + jasmine.pp(actualArgs), - "Expected event " + this.actual + " not to have been triggered with " + jasmine.pp(expectedArgs) + " but it was triggered with " + jasmine.pp(actualArgs) - ] - } else { - return [ - "Expected event " + this.actual + " to have been triggered on " + selector, - "Expected event " + this.actual + " not to have been triggered on " + selector - ] - } - } - - return wasTriggered && jasmine.JQuery.events.wasTriggeredWith(selector, this.actual, expectedArgs, this.env) - } - }) - this.addMatchers({ - toHaveBeenPreventedOn: function (selector) { - this.message = function () { - return [ - "Expected event " + this.actual + " to have been prevented on " + selector, - "Expected event " + this.actual + " not to have been prevented on " + selector - ] - } - - return jasmine.JQuery.events.wasPrevented(selector, this.actual) - } - }) - this.addMatchers({ - toHaveBeenPrevented: function () { - var eventName = this.actual.eventName - , selector = this.actual.selector - this.message = function () { - return [ - "Expected event " + eventName + " to have been prevented on " + selector, - "Expected event " + eventName + " not to have been prevented on " + selector - ] - } - - return jasmine.JQuery.events.wasPrevented(selector, eventName) - } - }) - this.addMatchers({ - toHaveBeenStoppedOn: function (selector) { - this.message = function () { - return [ - "Expected event " + this.actual + " to have been stopped on " + selector, - "Expected event " + this.actual + " not to have been stopped on " + selector - ] - } - - return jasmine.JQuery.events.wasStopped(selector, this.actual) - } - }) - this.addMatchers({ - toHaveBeenStopped: function () { - var eventName = this.actual.eventName - , selector = this.actual.selector - this.message = function () { - return [ - "Expected event " + eventName + " to have been stopped on " + selector, - "Expected event " + eventName + " not to have been stopped on " + selector - ] - } - return jasmine.JQuery.events.wasStopped(selector, eventName) - } - }) - jasmine.getEnv().addEqualityTester(function (a, b) { - if(a instanceof jQuery && b instanceof jQuery) { - if(a.size() != b.size()) { - return jasmine.undefined - } - else if(a.is(b)) { - return true - } - } - - return jasmine.undefined - }) - }) - - afterEach(function () { - jasmine.getFixtures().cleanUp() - jasmine.getStyleFixtures().cleanUp() - jasmine.JQuery.events.cleanUp() - }) -}(window.jasmine, window.jQuery) - -+function (jasmine, global) { "use strict"; - - global.readFixtures = function () { - return jasmine.getFixtures().proxyCallTo_('read', arguments) - } - - global.preloadFixtures = function () { - jasmine.getFixtures().proxyCallTo_('preload', arguments) - } - - global.loadFixtures = function () { - jasmine.getFixtures().proxyCallTo_('load', arguments) - } - - global.appendLoadFixtures = function () { - jasmine.getFixtures().proxyCallTo_('appendLoad', arguments) - } - - global.setFixtures = function (html) { - return jasmine.getFixtures().proxyCallTo_('set', arguments) - } - - global.appendSetFixtures = function () { - jasmine.getFixtures().proxyCallTo_('appendSet', arguments) - } - - global.sandbox = function (attributes) { - return jasmine.getFixtures().sandbox(attributes) - } - - global.spyOnEvent = function (selector, eventName) { - return jasmine.JQuery.events.spyOn(selector, eventName) - } - - global.preloadStyleFixtures = function () { - jasmine.getStyleFixtures().proxyCallTo_('preload', arguments) - } - - global.loadStyleFixtures = function () { - jasmine.getStyleFixtures().proxyCallTo_('load', arguments) - } - - global.appendLoadStyleFixtures = function () { - jasmine.getStyleFixtures().proxyCallTo_('appendLoad', arguments) - } - - global.setStyleFixtures = function (html) { - jasmine.getStyleFixtures().proxyCallTo_('set', arguments) - } - - global.appendSetStyleFixtures = function (html) { - jasmine.getStyleFixtures().proxyCallTo_('appendSet', arguments) - } - - global.loadJSONFixtures = function () { - return jasmine.getJSONFixtures().proxyCallTo_('load', arguments) - } - - global.getJSONFixture = function (url) { - return jasmine.getJSONFixtures().proxyCallTo_('read', arguments)[url] - } -}(jasmine, window); \ No newline at end of file diff --git a/web/spec/javascripts/spec_helper.js b/web/spec/javascripts/spec_helper.js index e3a81ec65..bb20037dd 100644 --- a/web/spec/javascripts/spec_helper.js +++ b/web/spec/javascripts/spec_helper.js @@ -1,8 +1,9 @@ // Teaspoon includes some support files, but you can use anything from your own support path too. +// require support/sinon +// require support/chai +// require support/expect // require support/jasmine-jquery-1.7.0 // require support/jasmine-jquery-2.0.0 -// require support/sinon -// require support/your-support-file // // PhantomJS (Teaspoons default driver) doesn't have support for Function.prototype.bind, which has caused confusion. // Use this polyfill to avoid the confusion. @@ -29,3 +30,6 @@ // You can require your own javascript files here. By default this will include everything in application, however you // may get better load performance if you require the specific files that are being used in the spec that tests them. //= require application +//= require support/sinon +// require support/expect +//= require support/jasmine-jquery-1.7.0 diff --git a/web/spec/javascripts/support/jasmine.yml b/web/spec/javascripts/support/jasmine.yml index 9bfa261a3..e69de29bb 100644 --- a/web/spec/javascripts/support/jasmine.yml +++ b/web/spec/javascripts/support/jasmine.yml @@ -1,76 +0,0 @@ -# src_files -# -# Return an array of filepaths relative to src_dir to include before jasmine specs. -# Default: [] -# -# EXAMPLE: -# -# src_files: -# - lib/source1.js -# - lib/source2.js -# - dist/**/*.js -# -src_files: - - assets/application.js - -# stylesheets -# -# Return an array of stylesheet filepaths relative to src_dir to include before jasmine specs. -# Default: [] -# -# EXAMPLE: -# -# stylesheets: -# - css/style.css -# - stylesheets/*.css -# -stylesheets: - - stylesheets/**/*.css - -# helpers -# -# Return an array of filepaths relative to spec_dir to include before jasmine specs. -# Default: ["helpers/**/*.js"] -# -# EXAMPLE: -# -# helpers: -# - helpers/**/*.js -# -helpers: - - helpers/**/*.js - -# spec_files -# -# Return an array of filepaths relative to spec_dir to include. -# Default: ["**/*[sS]pec.js"] -# -# EXAMPLE: -# -# spec_files: -# - **/*[sS]pec.js -# -spec_files: - - '**/*[sS]pec.js' - -# src_dir -# -# Source directory path. Your src_files must be returned relative to this path. Will use root if left blank. -# Default: project root -# -# EXAMPLE: -# -# src_dir: public -# -src_dir: - -# spec_dir -# -# Spec directory path. Your spec_files must be returned relative to this path. -# Default: spec/javascripts -# -# EXAMPLE: -# -# spec_dir: spec/javascripts -# -spec_dir: spec/javascripts diff --git a/web/spec/javascripts/sync_viewer_spec.js.coffee b/web/spec/javascripts/sync_viewer_spec.js.coffee new file mode 100644 index 000000000..6d04a4a39 --- /dev/null +++ b/web/spec/javascripts/sync_viewer_spec.js.coffee @@ -0,0 +1,228 @@ +describe "SyncViewer", -> + + beforeEach -> + this.fixtures = fixture.load("syncViewer.html", "user_sync_track1.json"); # append these fixtures which were already cached + this.server = sinon.fakeServer.create(); + window.jamClient = sinon.stub() + this.syncViewer = new JK.SyncViewer() + this.syncViewer.init() + $('body').append(this.syncViewer.root) + this.track1 = this.fixtures[1]["entries"][0] + + callback = sinon.spy() + window.jamClient.GetLocalRecordingState = sinon.stub().returns({recordings: []}) + window.jamClient.GetRecordingManagerState = sinon.stub().returns({running: false}) + window.jamClient.IsNativeClient = sinon.stub().returns(true) + + + afterEach -> + this.server.restore(); + + it "display state correctly", -> + $track = this.syncViewer.createTrack(this.track1) + this.syncViewer.updateTrackState($track) + + expect($track.find('.client-state .msg')).toContainText('MISSING') + expect($track.find('.upload-state .msg')).toContainText('PENDING UPLOAD') + + this.track1.fully_uploaded = true + + this.syncViewer.updateTrackState($track) + + expect($track.find('.upload-state .msg')).toContainText('UPLOADED') + + + describe "no track", -> + + beforeEach -> + this.syncViewer.load() + + this.server.requests[0].respond( + 200, + { "Content-Type": "application/json" }, + JSON.stringify({ next: null, entries: []}) + ); + + + it "loads empty", -> + + expect(this.syncViewer.list.find('.no-syncs')).toExist(); + + + describe "one track", -> + beforeEach -> + this.syncViewer.load() + + this.server.requests[0].respond( + 200, + { "Content-Type": "application/json" }, + JSON.stringify({ next: null, entries: [this.track1]}) + ); + + + it "loads single track", -> + + expect(this.syncViewer.list.find('.no-syncs')).not.toExist(); + $track = this.syncViewer.list.find('.recorded-track') + expect($track).toHaveLength(1); + expect($track.find('.client-state .msg')).toContainText('MISSING') + expect($track.find('.upload-state .msg')).toContainText('PENDING UPLOAD') + + it "handles recorded_track upload progress events correctly", -> + + commandMetadata = {queue: 'upload', type: 'recorded_track', action: 'upload', recording_id: this.track1.recording_id, track_id: this.track1.client_track_id} + cmd = {commandId: '1', commandType: 'upload', commandMetadata: commandMetadata } + + # first we should see that recording manager is paused + expect(this.syncViewer.downloadProgress).toHaveClass('paused') + expect(this.syncViewer.uploadProgress).toHaveClass('paused') + + # send in a start command + this.syncViewer.fileManagerCmdStart(null, cmd) + + expect(this.syncViewer.downloadProgress).toHaveClass('paused') + expect(this.syncViewer.uploadProgress).toHaveClass('busy') + + $recordedTrackProgress = this.syncViewer.uploadProgress.find('.recorded-track.sync') + expect($recordedTrackProgress).toHaveLength(1) + expect($recordedTrackProgress.find('.progress')).toBeHidden() + + cmd.percentage = 50 # half way done + this.syncViewer.fileManagerCmdProgress(null, cmd) + + expect($recordedTrackProgress.find('.progress')).toBeVisible() + + # command is over; progress element should be removed now + this.syncViewer.fileManagerCmdStop(null, cmd) + + expect(this.syncViewer.downloadProgress).toHaveClass('paused') + expect(this.syncViewer.uploadProgress).toHaveClass('paused') + expect($recordedTrackProgress).not.toBeInDOM() # got removed + + it "handles mix download progress events correctly", -> + + commandMetadata = {queue: 'download', type: 'mix', action: 'download'} + cmd = {commandId: '1', commandType: 'download', commandMetadata: commandMetadata } + + # first we should see that recording manager is paused + expect(this.syncViewer.downloadProgress).toHaveClass('paused') + expect(this.syncViewer.uploadProgress).toHaveClass('paused') + + # send in a start command + this.syncViewer.fileManagerCmdStart(null, cmd) + + expect(this.syncViewer.downloadProgress).toHaveClass('busy') + expect(this.syncViewer.uploadProgress).toHaveClass('paused') + + $progress = this.syncViewer.downloadProgress.find('.generic.sync') + expect($progress).toHaveLength(1) + expect($progress.find('span.text')).toHaveText('DOWNLOADING MIX') + + cmd.percentage = 50 # half way done + this.syncViewer.fileManagerCmdProgress(null, cmd) + + # nothing happens in percentage update + + # command is over; progress element should be removed now + this.syncViewer.fileManagerCmdStop(null, cmd) + + expect(this.syncViewer.downloadProgress).toHaveClass('paused') + expect(this.syncViewer.uploadProgress).toHaveClass('paused') + expect($progress).not.toBeInDOM() # got removed + + it "handles recorded track convert progress events correctly", -> + + commandMetadata = {queue: 'upload', type: 'recorded_track', action: 'convert'} + cmd = {commandId: '1', commandType: 'upload', commandMetadata: commandMetadata } + + # first we should see that recording manager is paused + expect(this.syncViewer.downloadProgress).toHaveClass('paused') + expect(this.syncViewer.uploadProgress).toHaveClass('paused') + + # send in a start command + this.syncViewer.fileManagerCmdStart(null, cmd) + + expect(this.syncViewer.uploadProgress).toHaveClass('busy') + expect(this.syncViewer.downloadProgress).toHaveClass('paused') + + $progress = this.syncViewer.uploadProgress.find('.generic.sync') + expect($progress).toHaveLength(1) + expect($progress.find('span.text')).toHaveText('COMPRESSING TRACK') + + cmd.percentage = 50 # half way done + this.syncViewer.fileManagerCmdProgress(null, cmd) + + # nothing happens in percentage update + + # command is over; progress element should be removed now + this.syncViewer.fileManagerCmdStop(null, cmd) + + expect(this.syncViewer.downloadProgress).toHaveClass('paused') + expect(this.syncViewer.uploadProgress).toHaveClass('paused') + expect($progress).not.toBeInDOM() # got removed + + it "handles stream mix upload progress events correctly", -> + + commandMetadata = {queue: 'upload', type: 'stream_mix', action: 'upload'} + cmd = {commandId: '1', commandType: 'upload', commandMetadata: commandMetadata } + + # first we should see that recording manager is paused + expect(this.syncViewer.downloadProgress).toHaveClass('paused') + expect(this.syncViewer.uploadProgress).toHaveClass('paused') + + # send in a start command + this.syncViewer.fileManagerCmdStart(null, cmd) + + expect(this.syncViewer.uploadProgress).toHaveClass('busy') + expect(this.syncViewer.downloadProgress).toHaveClass('paused') + + $progress = this.syncViewer.uploadProgress.find('.generic.sync') + expect($progress).toHaveLength(1) + expect($progress.find('span.text')).toHaveText('UPLOADING STREAM MIX') + + cmd.percentage = 50 # half way done + this.syncViewer.fileManagerCmdProgress(null, cmd) + + # nothing happens in percentage update + + # command is over; progress element should be removed now + this.syncViewer.fileManagerCmdStop(null, cmd) + + expect(this.syncViewer.downloadProgress).toHaveClass('paused') + expect(this.syncViewer.uploadProgress).toHaveClass('paused') + expect($progress).not.toBeInDOM() # got removed + + + + + + + it "displays hover info over recorded-track", -> + + $track = this.syncViewer.list.find('.recorded-track') + + # should show over client state + $clientState = $track.find('.client-state') + expect($clientState).toHaveLength(1); + $clientState.btOn() + $hoverHelp = $('.help-hover-recorded-tracks') + expect($hoverHelp).toHaveLength(1); + + # also verify that the message is correct about client state and upload state + expect($hoverHelp.find('.client-state .msg')).toContainText('MISSING') + expect($hoverHelp.find('.upload-state .msg')).toContainText('PENDING UPLOAD') + + $clientState.btOff() + + # as well as show over upload state + $uploadState = $track.find('.upload-state') + expect($uploadState).toHaveLength(1); + $uploadState.btOn() + $hoverHelp = $('.help-hover-recorded-tracks') + expect($hoverHelp).toHaveLength(1); + # also verify that the message is correct about client state and upload state + expect($hoverHelp.find('.client-state .msg')).toContainText('MISSING') + expect($hoverHelp.find('.upload-state .msg')).toContainText('PENDING UPLOAD') + + $uploadState.btOff() + diff --git a/web/spec/javascripts/vendor/jquery.js b/web/spec/javascripts/vendor/jquery.js index 7343eba6f..e69de29bb 100644 --- a/web/spec/javascripts/vendor/jquery.js +++ b/web/spec/javascripts/vendor/jquery.js @@ -1,9405 +0,0 @@ -/*! - * jQuery JavaScript Library v1.7.2 - * http://jquery.com/ - * - * Copyright 2011, John Resig - * Dual licensed under the MIT or GPL Version 2 licenses. - * http://jquery.org/license - * - * Includes Sizzle.js - * http://sizzlejs.com/ - * Copyright 2011, The Dojo Foundation - * Released under the MIT, BSD, and GPL Licenses. - * - * Date: Wed Mar 21 12:46:34 2012 -0700 - */ - -(function( window, undefined ) { - -// Use the correct document accordingly with window argument (sandbox) -var document = window.document, - navigator = window.navigator, - location = window.location; -var jQuery = (function() { - -// Define a local copy of jQuery -var jQuery = function( selector, context ) { - // The jQuery object is actually just the init constructor 'enhanced' - return new jQuery.fn.init( selector, context, rootjQuery ); - }, - - // Map over jQuery in case of overwrite - _jQuery = window.jQuery, - - // Map over the $ in case of overwrite - _$ = window.$, - - // A central reference to the root jQuery(document) - rootjQuery, - - // A simple way to check for HTML strings or ID strings - // Prioritize #id over to avoid XSS via location.hash (#9521) - quickExpr = /^(?:[^#<]*(<[\w\W]+>)[^>]*$|#([\w\-]*)$)/, - - // Check if a string has a non-whitespace character in it - rnotwhite = /\S/, - - // Used for trimming whitespace - trimLeft = /^\s+/, - trimRight = /\s+$/, - - // Match a standalone tag - rsingleTag = /^<(\w+)\s*\/?>(?:<\/\1>)?$/, - - // JSON RegExp - rvalidchars = /^[\],:{}\s]*$/, - rvalidescape = /\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, - rvalidtokens = /"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, - rvalidbraces = /(?:^|:|,)(?:\s*\[)+/g, - - // Useragent RegExp - rwebkit = /(webkit)[ \/]([\w.]+)/, - ropera = /(opera)(?:.*version)?[ \/]([\w.]+)/, - rmsie = /(msie) ([\w.]+)/, - rmozilla = /(mozilla)(?:.*? rv:([\w.]+))?/, - - // Matches dashed string for camelizing - rdashAlpha = /-([a-z]|[0-9])/ig, - rmsPrefix = /^-ms-/, - - // Used by jQuery.camelCase as callback to replace() - fcamelCase = function( all, letter ) { - return ( letter + "" ).toUpperCase(); - }, - - // Keep a UserAgent string for use with jQuery.browser - userAgent = navigator.userAgent, - - // For matching the engine and version of the browser - browserMatch, - - // The deferred used on DOM ready - readyList, - - // The ready event handler - DOMContentLoaded, - - // Save a reference to some core methods - toString = Object.prototype.toString, - hasOwn = Object.prototype.hasOwnProperty, - push = Array.prototype.push, - slice = Array.prototype.slice, - trim = String.prototype.trim, - indexOf = Array.prototype.indexOf, - - // [[Class]] -> type pairs - class2type = {}; - -jQuery.fn = jQuery.prototype = { - constructor: jQuery, - init: function( selector, context, rootjQuery ) { - var match, elem, ret, doc; - - // Handle $(""), $(null), or $(undefined) - if ( !selector ) { - return this; - } - - // Handle $(DOMElement) - if ( selector.nodeType ) { - this.context = this[0] = selector; - this.length = 1; - return this; - } - - // The body element only exists once, optimize finding it - if ( selector === "body" && !context && document.body ) { - this.context = document; - this[0] = document.body; - this.selector = selector; - this.length = 1; - return this; - } - - // Handle HTML strings - if ( typeof selector === "string" ) { - // Are we dealing with HTML string or an ID? - if ( selector.charAt(0) === "<" && selector.charAt( selector.length - 1 ) === ">" && selector.length >= 3 ) { - // Assume that strings that start and end with <> are HTML and skip the regex check - match = [ null, selector, null ]; - - } else { - match = quickExpr.exec( selector ); - } - - // Verify a match, and that no context was specified for #id - if ( match && (match[1] || !context) ) { - - // HANDLE: $(html) -> $(array) - if ( match[1] ) { - context = context instanceof jQuery ? context[0] : context; - doc = ( context ? context.ownerDocument || context : document ); - - // If a single string is passed in and it's a single tag - // just do a createElement and skip the rest - ret = rsingleTag.exec( selector ); - - if ( ret ) { - if ( jQuery.isPlainObject( context ) ) { - selector = [ document.createElement( ret[1] ) ]; - jQuery.fn.attr.call( selector, context, true ); - - } else { - selector = [ doc.createElement( ret[1] ) ]; - } - - } else { - ret = jQuery.buildFragment( [ match[1] ], [ doc ] ); - selector = ( ret.cacheable ? jQuery.clone(ret.fragment) : ret.fragment ).childNodes; - } - - return jQuery.merge( this, selector ); - - // HANDLE: $("#id") - } else { - elem = document.getElementById( match[2] ); - - // Check parentNode to catch when Blackberry 4.6 returns - // nodes that are no longer in the document #6963 - if ( elem && elem.parentNode ) { - // Handle the case where IE and Opera return items - // by name instead of ID - if ( elem.id !== match[2] ) { - return rootjQuery.find( selector ); - } - - // Otherwise, we inject the element directly into the jQuery object - this.length = 1; - this[0] = elem; - } - - this.context = document; - this.selector = selector; - return this; - } - - // HANDLE: $(expr, $(...)) - } else if ( !context || context.jquery ) { - return ( context || rootjQuery ).find( selector ); - - // HANDLE: $(expr, context) - // (which is just equivalent to: $(context).find(expr) - } else { - return this.constructor( context ).find( selector ); - } - - // HANDLE: $(function) - // Shortcut for document ready - } else if ( jQuery.isFunction( selector ) ) { - return rootjQuery.ready( selector ); - } - - if ( selector.selector !== undefined ) { - this.selector = selector.selector; - this.context = selector.context; - } - - return jQuery.makeArray( selector, this ); - }, - - // Start with an empty selector - selector: "", - - // The current version of jQuery being used - jquery: "1.7.2", - - // The default length of a jQuery object is 0 - length: 0, - - // The number of elements contained in the matched element set - size: function() { - return this.length; - }, - - toArray: function() { - return slice.call( this, 0 ); - }, - - // Get the Nth element in the matched element set OR - // Get the whole matched element set as a clean array - get: function( num ) { - return num == null ? - - // Return a 'clean' array - this.toArray() : - - // Return just the object - ( num < 0 ? this[ this.length + num ] : this[ num ] ); - }, - - // Take an array of elements and push it onto the stack - // (returning the new matched element set) - pushStack: function( elems, name, selector ) { - // Build a new jQuery matched element set - var ret = this.constructor(); - - if ( jQuery.isArray( elems ) ) { - push.apply( ret, elems ); - - } else { - jQuery.merge( ret, elems ); - } - - // Add the old object onto the stack (as a reference) - ret.prevObject = this; - - ret.context = this.context; - - if ( name === "find" ) { - ret.selector = this.selector + ( this.selector ? " " : "" ) + selector; - } else if ( name ) { - ret.selector = this.selector + "." + name + "(" + selector + ")"; - } - - // Return the newly-formed element set - return ret; - }, - - // Execute a callback for every element in the matched set. - // (You can seed the arguments with an array of args, but this is - // only used internally.) - each: function( callback, args ) { - return jQuery.each( this, callback, args ); - }, - - ready: function( fn ) { - // Attach the listeners - jQuery.bindReady(); - - // Add the callback - readyList.add( fn ); - - return this; - }, - - eq: function( i ) { - i = +i; - return i === -1 ? - this.slice( i ) : - this.slice( i, i + 1 ); - }, - - first: function() { - return this.eq( 0 ); - }, - - last: function() { - return this.eq( -1 ); - }, - - slice: function() { - return this.pushStack( slice.apply( this, arguments ), - "slice", slice.call(arguments).join(",") ); - }, - - map: function( callback ) { - return this.pushStack( jQuery.map(this, function( elem, i ) { - return callback.call( elem, i, elem ); - })); - }, - - end: function() { - return this.prevObject || this.constructor(null); - }, - - // For internal use only. - // Behaves like an Array's method, not like a jQuery method. - push: push, - sort: [].sort, - splice: [].splice -}; - -// Give the init function the jQuery prototype for later instantiation -jQuery.fn.init.prototype = jQuery.fn; - -jQuery.extend = jQuery.fn.extend = function() { - var options, name, src, copy, copyIsArray, clone, - target = arguments[0] || {}, - i = 1, - length = arguments.length, - deep = false; - - // Handle a deep copy situation - if ( typeof target === "boolean" ) { - deep = target; - target = arguments[1] || {}; - // skip the boolean and the target - i = 2; - } - - // Handle case when target is a string or something (possible in deep copy) - if ( typeof target !== "object" && !jQuery.isFunction(target) ) { - target = {}; - } - - // extend jQuery itself if only one argument is passed - if ( length === i ) { - target = this; - --i; - } - - for ( ; i < length; i++ ) { - // Only deal with non-null/undefined values - if ( (options = arguments[ i ]) != null ) { - // Extend the base object - for ( name in options ) { - src = target[ name ]; - copy = options[ name ]; - - // Prevent never-ending loop - if ( target === copy ) { - continue; - } - - // Recurse if we're merging plain objects or arrays - if ( deep && copy && ( jQuery.isPlainObject(copy) || (copyIsArray = jQuery.isArray(copy)) ) ) { - if ( copyIsArray ) { - copyIsArray = false; - clone = src && jQuery.isArray(src) ? src : []; - - } else { - clone = src && jQuery.isPlainObject(src) ? src : {}; - } - - // Never move original objects, clone them - target[ name ] = jQuery.extend( deep, clone, copy ); - - // Don't bring in undefined values - } else if ( copy !== undefined ) { - target[ name ] = copy; - } - } - } - } - - // Return the modified object - return target; -}; - -jQuery.extend({ - noConflict: function( deep ) { - if ( window.$ === jQuery ) { - window.$ = _$; - } - - if ( deep && window.jQuery === jQuery ) { - window.jQuery = _jQuery; - } - - return jQuery; - }, - - // Is the DOM ready to be used? Set to true once it occurs. - isReady: false, - - // A counter to track how many items to wait for before - // the ready event fires. See #6781 - readyWait: 1, - - // Hold (or release) the ready event - holdReady: function( hold ) { - if ( hold ) { - jQuery.readyWait++; - } else { - jQuery.ready( true ); - } - }, - - // Handle when the DOM is ready - ready: function( wait ) { - // Either a released hold or an DOMready/load event and not yet ready - if ( (wait === true && !--jQuery.readyWait) || (wait !== true && !jQuery.isReady) ) { - // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443). - if ( !document.body ) { - return setTimeout( jQuery.ready, 1 ); - } - - // Remember that the DOM is ready - jQuery.isReady = true; - - // If a normal DOM Ready event fired, decrement, and wait if need be - if ( wait !== true && --jQuery.readyWait > 0 ) { - return; - } - - // If there are functions bound, to execute - readyList.fireWith( document, [ jQuery ] ); - - // Trigger any bound ready events - if ( jQuery.fn.trigger ) { - jQuery( document ).trigger( "ready" ).off( "ready" ); - } - } - }, - - bindReady: function() { - if ( readyList ) { - return; - } - - readyList = jQuery.Callbacks( "once memory" ); - - // Catch cases where $(document).ready() is called after the - // browser event has already occurred. - if ( document.readyState === "complete" ) { - // Handle it asynchronously to allow scripts the opportunity to delay ready - return setTimeout( jQuery.ready, 1 ); - } - - // Mozilla, Opera and webkit nightlies currently support this event - if ( document.addEventListener ) { - // Use the handy event callback - document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false ); - - // A fallback to window.onload, that will always work - window.addEventListener( "load", jQuery.ready, false ); - - // If IE event model is used - } else if ( document.attachEvent ) { - // ensure firing before onload, - // maybe late but safe also for iframes - document.attachEvent( "onreadystatechange", DOMContentLoaded ); - - // A fallback to window.onload, that will always work - window.attachEvent( "onload", jQuery.ready ); - - // If IE and not a frame - // continually check to see if the document is ready - var toplevel = false; - - try { - toplevel = window.frameElement == null; - } catch(e) {} - - if ( document.documentElement.doScroll && toplevel ) { - doScrollCheck(); - } - } - }, - - // See test/unit/core.js for details concerning isFunction. - // Since version 1.3, DOM methods and functions like alert - // aren't supported. They return false on IE (#2968). - isFunction: function( obj ) { - return jQuery.type(obj) === "function"; - }, - - isArray: Array.isArray || function( obj ) { - return jQuery.type(obj) === "array"; - }, - - isWindow: function( obj ) { - return obj != null && obj == obj.window; - }, - - isNumeric: function( obj ) { - return !isNaN( parseFloat(obj) ) && isFinite( obj ); - }, - - type: function( obj ) { - return obj == null ? - String( obj ) : - class2type[ toString.call(obj) ] || "object"; - }, - - isPlainObject: function( obj ) { - // Must be an Object. - // Because of IE, we also have to check the presence of the constructor property. - // Make sure that DOM nodes and window objects don't pass through, as well - if ( !obj || jQuery.type(obj) !== "object" || obj.nodeType || jQuery.isWindow( obj ) ) { - return false; - } - - try { - // Not own constructor property must be Object - if ( obj.constructor && - !hasOwn.call(obj, "constructor") && - !hasOwn.call(obj.constructor.prototype, "isPrototypeOf") ) { - return false; - } - } catch ( e ) { - // IE8,9 Will throw exceptions on certain host objects #9897 - return false; - } - - // Own properties are enumerated firstly, so to speed up, - // if last one is own, then all properties are own. - - var key; - for ( key in obj ) {} - - return key === undefined || hasOwn.call( obj, key ); - }, - - isEmptyObject: function( obj ) { - for ( var name in obj ) { - return false; - } - return true; - }, - - error: function( msg ) { - throw new Error( msg ); - }, - - parseJSON: function( data ) { - if ( typeof data !== "string" || !data ) { - return null; - } - - // Make sure leading/trailing whitespace is removed (IE can't handle it) - data = jQuery.trim( data ); - - // Attempt to parse using the native JSON parser first - if ( window.JSON && window.JSON.parse ) { - return window.JSON.parse( data ); - } - - // Make sure the incoming data is actual JSON - // Logic borrowed from http://json.org/json2.js - if ( rvalidchars.test( data.replace( rvalidescape, "@" ) - .replace( rvalidtokens, "]" ) - .replace( rvalidbraces, "")) ) { - - return ( new Function( "return " + data ) )(); - - } - jQuery.error( "Invalid JSON: " + data ); - }, - - // Cross-browser xml parsing - parseXML: function( data ) { - if ( typeof data !== "string" || !data ) { - return null; - } - var xml, tmp; - try { - if ( window.DOMParser ) { // Standard - tmp = new DOMParser(); - xml = tmp.parseFromString( data , "text/xml" ); - } else { // IE - xml = new ActiveXObject( "Microsoft.XMLDOM" ); - xml.async = "false"; - xml.loadXML( data ); - } - } catch( e ) { - xml = undefined; - } - if ( !xml || !xml.documentElement || xml.getElementsByTagName( "parsererror" ).length ) { - jQuery.error( "Invalid XML: " + data ); - } - return xml; - }, - - noop: function() {}, - - // Evaluates a script in a global context - // Workarounds based on findings by Jim Driscoll - // http://weblogs.java.net/blog/driscoll/archive/2009/09/08/eval-javascript-global-context - globalEval: function( data ) { - if ( data && rnotwhite.test( data ) ) { - // We use execScript on Internet Explorer - // We use an anonymous function so that context is window - // rather than jQuery in Firefox - ( window.execScript || function( data ) { - window[ "eval" ].call( window, data ); - } )( data ); - } - }, - - // Convert dashed to camelCase; used by the css and data modules - // Microsoft forgot to hump their vendor prefix (#9572) - camelCase: function( string ) { - return string.replace( rmsPrefix, "ms-" ).replace( rdashAlpha, fcamelCase ); - }, - - nodeName: function( elem, name ) { - return elem.nodeName && elem.nodeName.toUpperCase() === name.toUpperCase(); - }, - - // args is for internal usage only - each: function( object, callback, args ) { - var name, i = 0, - length = object.length, - isObj = length === undefined || jQuery.isFunction( object ); - - if ( args ) { - if ( isObj ) { - for ( name in object ) { - if ( callback.apply( object[ name ], args ) === false ) { - break; - } - } - } else { - for ( ; i < length; ) { - if ( callback.apply( object[ i++ ], args ) === false ) { - break; - } - } - } - - // A special, fast, case for the most common use of each - } else { - if ( isObj ) { - for ( name in object ) { - if ( callback.call( object[ name ], name, object[ name ] ) === false ) { - break; - } - } - } else { - for ( ; i < length; ) { - if ( callback.call( object[ i ], i, object[ i++ ] ) === false ) { - break; - } - } - } - } - - return object; - }, - - // Use native String.trim function wherever possible - trim: trim ? - function( text ) { - return text == null ? - "" : - trim.call( text ); - } : - - // Otherwise use our own trimming functionality - function( text ) { - return text == null ? - "" : - text.toString().replace( trimLeft, "" ).replace( trimRight, "" ); - }, - - // results is for internal usage only - makeArray: function( array, results ) { - var ret = results || []; - - if ( array != null ) { - // The window, strings (and functions) also have 'length' - // Tweaked logic slightly to handle Blackberry 4.7 RegExp issues #6930 - var type = jQuery.type( array ); - - if ( array.length == null || type === "string" || type === "function" || type === "regexp" || jQuery.isWindow( array ) ) { - push.call( ret, array ); - } else { - jQuery.merge( ret, array ); - } - } - - return ret; - }, - - inArray: function( elem, array, i ) { - var len; - - if ( array ) { - if ( indexOf ) { - return indexOf.call( array, elem, i ); - } - - len = array.length; - i = i ? i < 0 ? Math.max( 0, len + i ) : i : 0; - - for ( ; i < len; i++ ) { - // Skip accessing in sparse arrays - if ( i in array && array[ i ] === elem ) { - return i; - } - } - } - - return -1; - }, - - merge: function( first, second ) { - var i = first.length, - j = 0; - - if ( typeof second.length === "number" ) { - for ( var l = second.length; j < l; j++ ) { - first[ i++ ] = second[ j ]; - } - - } else { - while ( second[j] !== undefined ) { - first[ i++ ] = second[ j++ ]; - } - } - - first.length = i; - - return first; - }, - - grep: function( elems, callback, inv ) { - var ret = [], retVal; - inv = !!inv; - - // Go through the array, only saving the items - // that pass the validator function - for ( var i = 0, length = elems.length; i < length; i++ ) { - retVal = !!callback( elems[ i ], i ); - if ( inv !== retVal ) { - ret.push( elems[ i ] ); - } - } - - return ret; - }, - - // arg is for internal usage only - map: function( elems, callback, arg ) { - var value, key, ret = [], - i = 0, - length = elems.length, - // jquery objects are treated as arrays - isArray = elems instanceof jQuery || length !== undefined && typeof length === "number" && ( ( length > 0 && elems[ 0 ] && elems[ length -1 ] ) || length === 0 || jQuery.isArray( elems ) ) ; - - // Go through the array, translating each of the items to their - if ( isArray ) { - for ( ; i < length; i++ ) { - value = callback( elems[ i ], i, arg ); - - if ( value != null ) { - ret[ ret.length ] = value; - } - } - - // Go through every key on the object, - } else { - for ( key in elems ) { - value = callback( elems[ key ], key, arg ); - - if ( value != null ) { - ret[ ret.length ] = value; - } - } - } - - // Flatten any nested arrays - return ret.concat.apply( [], ret ); - }, - - // A global GUID counter for objects - guid: 1, - - // Bind a function to a context, optionally partially applying any - // arguments. - proxy: function( fn, context ) { - if ( typeof context === "string" ) { - var tmp = fn[ context ]; - context = fn; - fn = tmp; - } - - // Quick check to determine if target is callable, in the spec - // this throws a TypeError, but we will just return undefined. - if ( !jQuery.isFunction( fn ) ) { - return undefined; - } - - // Simulated bind - var args = slice.call( arguments, 2 ), - proxy = function() { - return fn.apply( context, args.concat( slice.call( arguments ) ) ); - }; - - // Set the guid of unique handler to the same of original handler, so it can be removed - proxy.guid = fn.guid = fn.guid || proxy.guid || jQuery.guid++; - - return proxy; - }, - - // Mutifunctional method to get and set values to a collection - // The value/s can optionally be executed if it's a function - access: function( elems, fn, key, value, chainable, emptyGet, pass ) { - var exec, - bulk = key == null, - i = 0, - length = elems.length; - - // Sets many values - if ( key && typeof key === "object" ) { - for ( i in key ) { - jQuery.access( elems, fn, i, key[i], 1, emptyGet, value ); - } - chainable = 1; - - // Sets one value - } else if ( value !== undefined ) { - // Optionally, function values get executed if exec is true - exec = pass === undefined && jQuery.isFunction( value ); - - if ( bulk ) { - // Bulk operations only iterate when executing function values - if ( exec ) { - exec = fn; - fn = function( elem, key, value ) { - return exec.call( jQuery( elem ), value ); - }; - - // Otherwise they run against the entire set - } else { - fn.call( elems, value ); - fn = null; - } - } - - if ( fn ) { - for (; i < length; i++ ) { - fn( elems[i], key, exec ? value.call( elems[i], i, fn( elems[i], key ) ) : value, pass ); - } - } - - chainable = 1; - } - - return chainable ? - elems : - - // Gets - bulk ? - fn.call( elems ) : - length ? fn( elems[0], key ) : emptyGet; - }, - - now: function() { - return ( new Date() ).getTime(); - }, - - // Use of jQuery.browser is frowned upon. - // More details: http://docs.jquery.com/Utilities/jQuery.browser - uaMatch: function( ua ) { - ua = ua.toLowerCase(); - - var match = rwebkit.exec( ua ) || - ropera.exec( ua ) || - rmsie.exec( ua ) || - ua.indexOf("compatible") < 0 && rmozilla.exec( ua ) || - []; - - return { browser: match[1] || "", version: match[2] || "0" }; - }, - - sub: function() { - function jQuerySub( selector, context ) { - return new jQuerySub.fn.init( selector, context ); - } - jQuery.extend( true, jQuerySub, this ); - jQuerySub.superclass = this; - jQuerySub.fn = jQuerySub.prototype = this(); - jQuerySub.fn.constructor = jQuerySub; - jQuerySub.sub = this.sub; - jQuerySub.fn.init = function init( selector, context ) { - if ( context && context instanceof jQuery && !(context instanceof jQuerySub) ) { - context = jQuerySub( context ); - } - - return jQuery.fn.init.call( this, selector, context, rootjQuerySub ); - }; - jQuerySub.fn.init.prototype = jQuerySub.fn; - var rootjQuerySub = jQuerySub(document); - return jQuerySub; - }, - - browser: {} -}); - -// Populate the class2type map -jQuery.each("Boolean Number String Function Array Date RegExp Object".split(" "), function(i, name) { - class2type[ "[object " + name + "]" ] = name.toLowerCase(); -}); - -browserMatch = jQuery.uaMatch( userAgent ); -if ( browserMatch.browser ) { - jQuery.browser[ browserMatch.browser ] = true; - jQuery.browser.version = browserMatch.version; -} - -// Deprecated, use jQuery.browser.webkit instead -if ( jQuery.browser.webkit ) { - jQuery.browser.safari = true; -} - -// IE doesn't match non-breaking spaces with \s -if ( rnotwhite.test( "\xA0" ) ) { - trimLeft = /^[\s\xA0]+/; - trimRight = /[\s\xA0]+$/; -} - -// All jQuery objects should point back to these -rootjQuery = jQuery(document); - -// Cleanup functions for the document ready method -if ( document.addEventListener ) { - DOMContentLoaded = function() { - document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false ); - jQuery.ready(); - }; - -} else if ( document.attachEvent ) { - DOMContentLoaded = function() { - // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443). - if ( document.readyState === "complete" ) { - document.detachEvent( "onreadystatechange", DOMContentLoaded ); - jQuery.ready(); - } - }; -} - -// The DOM ready check for Internet Explorer -function doScrollCheck() { - if ( jQuery.isReady ) { - return; - } - - try { - // If IE is used, use the trick by Diego Perini - // http://javascript.nwbox.com/IEContentLoaded/ - document.documentElement.doScroll("left"); - } catch(e) { - setTimeout( doScrollCheck, 1 ); - return; - } - - // and execute any waiting functions - jQuery.ready(); -} - -return jQuery; - -})(); - - -// String to Object flags format cache -var flagsCache = {}; - -// Convert String-formatted flags into Object-formatted ones and store in cache -function createFlags( flags ) { - var object = flagsCache[ flags ] = {}, - i, length; - flags = flags.split( /\s+/ ); - for ( i = 0, length = flags.length; i < length; i++ ) { - object[ flags[i] ] = true; - } - return object; -} - -/* - * Create a callback list using the following parameters: - * - * flags: an optional list of space-separated flags that will change how - * the callback list behaves - * - * By default a callback list will act like an event callback list and can be - * "fired" multiple times. - * - * Possible flags: - * - * once: will ensure the callback list can only be fired once (like a Deferred) - * - * memory: will keep track of previous values and will call any callback added - * after the list has been fired right away with the latest "memorized" - * values (like a Deferred) - * - * unique: will ensure a callback can only be added once (no duplicate in the list) - * - * stopOnFalse: interrupt callings when a callback returns false - * - */ -jQuery.Callbacks = function( flags ) { - - // Convert flags from String-formatted to Object-formatted - // (we check in cache first) - flags = flags ? ( flagsCache[ flags ] || createFlags( flags ) ) : {}; - - var // Actual callback list - list = [], - // Stack of fire calls for repeatable lists - stack = [], - // Last fire value (for non-forgettable lists) - memory, - // Flag to know if list was already fired - fired, - // Flag to know if list is currently firing - firing, - // First callback to fire (used internally by add and fireWith) - firingStart, - // End of the loop when firing - firingLength, - // Index of currently firing callback (modified by remove if needed) - firingIndex, - // Add one or several callbacks to the list - add = function( args ) { - var i, - length, - elem, - type, - actual; - for ( i = 0, length = args.length; i < length; i++ ) { - elem = args[ i ]; - type = jQuery.type( elem ); - if ( type === "array" ) { - // Inspect recursively - add( elem ); - } else if ( type === "function" ) { - // Add if not in unique mode and callback is not in - if ( !flags.unique || !self.has( elem ) ) { - list.push( elem ); - } - } - } - }, - // Fire callbacks - fire = function( context, args ) { - args = args || []; - memory = !flags.memory || [ context, args ]; - fired = true; - firing = true; - firingIndex = firingStart || 0; - firingStart = 0; - firingLength = list.length; - for ( ; list && firingIndex < firingLength; firingIndex++ ) { - if ( list[ firingIndex ].apply( context, args ) === false && flags.stopOnFalse ) { - memory = true; // Mark as halted - break; - } - } - firing = false; - if ( list ) { - if ( !flags.once ) { - if ( stack && stack.length ) { - memory = stack.shift(); - self.fireWith( memory[ 0 ], memory[ 1 ] ); - } - } else if ( memory === true ) { - self.disable(); - } else { - list = []; - } - } - }, - // Actual Callbacks object - self = { - // Add a callback or a collection of callbacks to the list - add: function() { - if ( list ) { - var length = list.length; - add( arguments ); - // Do we need to add the callbacks to the - // current firing batch? - if ( firing ) { - firingLength = list.length; - // With memory, if we're not firing then - // we should call right away, unless previous - // firing was halted (stopOnFalse) - } else if ( memory && memory !== true ) { - firingStart = length; - fire( memory[ 0 ], memory[ 1 ] ); - } - } - return this; - }, - // Remove a callback from the list - remove: function() { - if ( list ) { - var args = arguments, - argIndex = 0, - argLength = args.length; - for ( ; argIndex < argLength ; argIndex++ ) { - for ( var i = 0; i < list.length; i++ ) { - if ( args[ argIndex ] === list[ i ] ) { - // Handle firingIndex and firingLength - if ( firing ) { - if ( i <= firingLength ) { - firingLength--; - if ( i <= firingIndex ) { - firingIndex--; - } - } - } - // Remove the element - list.splice( i--, 1 ); - // If we have some unicity property then - // we only need to do this once - if ( flags.unique ) { - break; - } - } - } - } - } - return this; - }, - // Control if a given callback is in the list - has: function( fn ) { - if ( list ) { - var i = 0, - length = list.length; - for ( ; i < length; i++ ) { - if ( fn === list[ i ] ) { - return true; - } - } - } - return false; - }, - // Remove all callbacks from the list - empty: function() { - list = []; - return this; - }, - // Have the list do nothing anymore - disable: function() { - list = stack = memory = undefined; - return this; - }, - // Is it disabled? - disabled: function() { - return !list; - }, - // Lock the list in its current state - lock: function() { - stack = undefined; - if ( !memory || memory === true ) { - self.disable(); - } - return this; - }, - // Is it locked? - locked: function() { - return !stack; - }, - // Call all callbacks with the given context and arguments - fireWith: function( context, args ) { - if ( stack ) { - if ( firing ) { - if ( !flags.once ) { - stack.push( [ context, args ] ); - } - } else if ( !( flags.once && memory ) ) { - fire( context, args ); - } - } - return this; - }, - // Call all the callbacks with the given arguments - fire: function() { - self.fireWith( this, arguments ); - return this; - }, - // To know if the callbacks have already been called at least once - fired: function() { - return !!fired; - } - }; - - return self; -}; - - - - -var // Static reference to slice - sliceDeferred = [].slice; - -jQuery.extend({ - - Deferred: function( func ) { - var doneList = jQuery.Callbacks( "once memory" ), - failList = jQuery.Callbacks( "once memory" ), - progressList = jQuery.Callbacks( "memory" ), - state = "pending", - lists = { - resolve: doneList, - reject: failList, - notify: progressList - }, - promise = { - done: doneList.add, - fail: failList.add, - progress: progressList.add, - - state: function() { - return state; - }, - - // Deprecated - isResolved: doneList.fired, - isRejected: failList.fired, - - then: function( doneCallbacks, failCallbacks, progressCallbacks ) { - deferred.done( doneCallbacks ).fail( failCallbacks ).progress( progressCallbacks ); - return this; - }, - always: function() { - deferred.done.apply( deferred, arguments ).fail.apply( deferred, arguments ); - return this; - }, - pipe: function( fnDone, fnFail, fnProgress ) { - return jQuery.Deferred(function( newDefer ) { - jQuery.each( { - done: [ fnDone, "resolve" ], - fail: [ fnFail, "reject" ], - progress: [ fnProgress, "notify" ] - }, function( handler, data ) { - var fn = data[ 0 ], - action = data[ 1 ], - returned; - if ( jQuery.isFunction( fn ) ) { - deferred[ handler ](function() { - returned = fn.apply( this, arguments ); - if ( returned && jQuery.isFunction( returned.promise ) ) { - returned.promise().then( newDefer.resolve, newDefer.reject, newDefer.notify ); - } else { - newDefer[ action + "With" ]( this === deferred ? newDefer : this, [ returned ] ); - } - }); - } else { - deferred[ handler ]( newDefer[ action ] ); - } - }); - }).promise(); - }, - // Get a promise for this deferred - // If obj is provided, the promise aspect is added to the object - promise: function( obj ) { - if ( obj == null ) { - obj = promise; - } else { - for ( var key in promise ) { - obj[ key ] = promise[ key ]; - } - } - return obj; - } - }, - deferred = promise.promise({}), - key; - - for ( key in lists ) { - deferred[ key ] = lists[ key ].fire; - deferred[ key + "With" ] = lists[ key ].fireWith; - } - - // Handle state - deferred.done( function() { - state = "resolved"; - }, failList.disable, progressList.lock ).fail( function() { - state = "rejected"; - }, doneList.disable, progressList.lock ); - - // Call given func if any - if ( func ) { - func.call( deferred, deferred ); - } - - // All done! - return deferred; - }, - - // Deferred helper - when: function( firstParam ) { - var args = sliceDeferred.call( arguments, 0 ), - i = 0, - length = args.length, - pValues = new Array( length ), - count = length, - pCount = length, - deferred = length <= 1 && firstParam && jQuery.isFunction( firstParam.promise ) ? - firstParam : - jQuery.Deferred(), - promise = deferred.promise(); - function resolveFunc( i ) { - return function( value ) { - args[ i ] = arguments.length > 1 ? sliceDeferred.call( arguments, 0 ) : value; - if ( !( --count ) ) { - deferred.resolveWith( deferred, args ); - } - }; - } - function progressFunc( i ) { - return function( value ) { - pValues[ i ] = arguments.length > 1 ? sliceDeferred.call( arguments, 0 ) : value; - deferred.notifyWith( promise, pValues ); - }; - } - if ( length > 1 ) { - for ( ; i < length; i++ ) { - if ( args[ i ] && args[ i ].promise && jQuery.isFunction( args[ i ].promise ) ) { - args[ i ].promise().then( resolveFunc(i), deferred.reject, progressFunc(i) ); - } else { - --count; - } - } - if ( !count ) { - deferred.resolveWith( deferred, args ); - } - } else if ( deferred !== firstParam ) { - deferred.resolveWith( deferred, length ? [ firstParam ] : [] ); - } - return promise; - } -}); - - - - -jQuery.support = (function() { - - var support, - all, - a, - select, - opt, - input, - fragment, - tds, - events, - eventName, - i, - isSupported, - div = document.createElement( "div" ), - documentElement = document.documentElement; - - // Preliminary tests - div.setAttribute("className", "t"); - div.innerHTML = "
a"; - - all = div.getElementsByTagName( "*" ); - a = div.getElementsByTagName( "a" )[ 0 ]; - - // Can't get basic test support - if ( !all || !all.length || !a ) { - return {}; - } - - // First batch of supports tests - select = document.createElement( "select" ); - opt = select.appendChild( document.createElement("option") ); - input = div.getElementsByTagName( "input" )[ 0 ]; - - support = { - // IE strips leading whitespace when .innerHTML is used - leadingWhitespace: ( div.firstChild.nodeType === 3 ), - - // Make sure that tbody elements aren't automatically inserted - // IE will insert them into empty tables - tbody: !div.getElementsByTagName("tbody").length, - - // Make sure that link elements get serialized correctly by innerHTML - // This requires a wrapper element in IE - htmlSerialize: !!div.getElementsByTagName("link").length, - - // Get the style information from getAttribute - // (IE uses .cssText instead) - style: /top/.test( a.getAttribute("style") ), - - // Make sure that URLs aren't manipulated - // (IE normalizes it by default) - hrefNormalized: ( a.getAttribute("href") === "/a" ), - - // Make sure that element opacity exists - // (IE uses filter instead) - // Use a regex to work around a WebKit issue. See #5145 - opacity: /^0.55/.test( a.style.opacity ), - - // Verify style float existence - // (IE uses styleFloat instead of cssFloat) - cssFloat: !!a.style.cssFloat, - - // Make sure that if no value is specified for a checkbox - // that it defaults to "on". - // (WebKit defaults to "" instead) - checkOn: ( input.value === "on" ), - - // Make sure that a selected-by-default option has a working selected property. - // (WebKit defaults to false instead of true, IE too, if it's in an optgroup) - optSelected: opt.selected, - - // Test setAttribute on camelCase class. If it works, we need attrFixes when doing get/setAttribute (ie6/7) - getSetAttribute: div.className !== "t", - - // Tests for enctype support on a form(#6743) - enctype: !!document.createElement("form").enctype, - - // Makes sure cloning an html5 element does not cause problems - // Where outerHTML is undefined, this still works - html5Clone: document.createElement("nav").cloneNode( true ).outerHTML !== "<:nav>", - - // Will be defined later - submitBubbles: true, - changeBubbles: true, - focusinBubbles: false, - deleteExpando: true, - noCloneEvent: true, - inlineBlockNeedsLayout: false, - shrinkWrapBlocks: false, - reliableMarginRight: true, - pixelMargin: true - }; - - // jQuery.boxModel DEPRECATED in 1.3, use jQuery.support.boxModel instead - jQuery.boxModel = support.boxModel = (document.compatMode === "CSS1Compat"); - - // Make sure checked status is properly cloned - input.checked = true; - support.noCloneChecked = input.cloneNode( true ).checked; - - // Make sure that the options inside disabled selects aren't marked as disabled - // (WebKit marks them as disabled) - select.disabled = true; - support.optDisabled = !opt.disabled; - - // Test to see if it's possible to delete an expando from an element - // Fails in Internet Explorer - try { - delete div.test; - } catch( e ) { - support.deleteExpando = false; - } - - if ( !div.addEventListener && div.attachEvent && div.fireEvent ) { - div.attachEvent( "onclick", function() { - // Cloning a node shouldn't copy over any - // bound event handlers (IE does this) - support.noCloneEvent = false; - }); - div.cloneNode( true ).fireEvent( "onclick" ); - } - - // Check if a radio maintains its value - // after being appended to the DOM - input = document.createElement("input"); - input.value = "t"; - input.setAttribute("type", "radio"); - support.radioValue = input.value === "t"; - - input.setAttribute("checked", "checked"); - - // #11217 - WebKit loses check when the name is after the checked attribute - input.setAttribute( "name", "t" ); - - div.appendChild( input ); - fragment = document.createDocumentFragment(); - fragment.appendChild( div.lastChild ); - - // WebKit doesn't clone checked state correctly in fragments - support.checkClone = fragment.cloneNode( true ).cloneNode( true ).lastChild.checked; - - // Check if a disconnected checkbox will retain its checked - // value of true after appended to the DOM (IE6/7) - support.appendChecked = input.checked; - - fragment.removeChild( input ); - fragment.appendChild( div ); - - // Technique from Juriy Zaytsev - // http://perfectionkills.com/detecting-event-support-without-browser-sniffing/ - // We only care about the case where non-standard event systems - // are used, namely in IE. Short-circuiting here helps us to - // avoid an eval call (in setAttribute) which can cause CSP - // to go haywire. See: https://developer.mozilla.org/en/Security/CSP - if ( div.attachEvent ) { - for ( i in { - submit: 1, - change: 1, - focusin: 1 - }) { - eventName = "on" + i; - isSupported = ( eventName in div ); - if ( !isSupported ) { - div.setAttribute( eventName, "return;" ); - isSupported = ( typeof div[ eventName ] === "function" ); - } - support[ i + "Bubbles" ] = isSupported; - } - } - - fragment.removeChild( div ); - - // Null elements to avoid leaks in IE - fragment = select = opt = div = input = null; - - // Run tests that need a body at doc ready - jQuery(function() { - var container, outer, inner, table, td, offsetSupport, - marginDiv, conMarginTop, style, html, positionTopLeftWidthHeight, - paddingMarginBorderVisibility, paddingMarginBorder, - body = document.getElementsByTagName("body")[0]; - - if ( !body ) { - // Return for frameset docs that don't have a body - return; - } - - conMarginTop = 1; - paddingMarginBorder = "padding:0;margin:0;border:"; - positionTopLeftWidthHeight = "position:absolute;top:0;left:0;width:1px;height:1px;"; - paddingMarginBorderVisibility = paddingMarginBorder + "0;visibility:hidden;"; - style = "style='" + positionTopLeftWidthHeight + paddingMarginBorder + "5px solid #000;"; - html = "
" + - "" + - "
"; - - container = document.createElement("div"); - container.style.cssText = paddingMarginBorderVisibility + "width:0;height:0;position:static;top:0;margin-top:" + conMarginTop + "px"; - body.insertBefore( container, body.firstChild ); - - // Construct the test element - div = document.createElement("div"); - container.appendChild( div ); - - // Check if table cells still have offsetWidth/Height when they are set - // to display:none and there are still other visible table cells in a - // table row; if so, offsetWidth/Height are not reliable for use when - // determining if an element has been hidden directly using - // display:none (it is still safe to use offsets if a parent element is - // hidden; don safety goggles and see bug #4512 for more information). - // (only IE 8 fails this test) - div.innerHTML = "
t
"; - tds = div.getElementsByTagName( "td" ); - isSupported = ( tds[ 0 ].offsetHeight === 0 ); - - tds[ 0 ].style.display = ""; - tds[ 1 ].style.display = "none"; - - // Check if empty table cells still have offsetWidth/Height - // (IE <= 8 fail this test) - support.reliableHiddenOffsets = isSupported && ( tds[ 0 ].offsetHeight === 0 ); - - // Check if div with explicit width and no margin-right incorrectly - // gets computed margin-right based on width of container. For more - // info see bug #3333 - // Fails in WebKit before Feb 2011 nightlies - // WebKit Bug 13343 - getComputedStyle returns wrong value for margin-right - if ( window.getComputedStyle ) { - div.innerHTML = ""; - marginDiv = document.createElement( "div" ); - marginDiv.style.width = "0"; - marginDiv.style.marginRight = "0"; - div.style.width = "2px"; - div.appendChild( marginDiv ); - support.reliableMarginRight = - ( parseInt( ( window.getComputedStyle( marginDiv, null ) || { marginRight: 0 } ).marginRight, 10 ) || 0 ) === 0; - } - - if ( typeof div.style.zoom !== "undefined" ) { - // Check if natively block-level elements act like inline-block - // elements when setting their display to 'inline' and giving - // them layout - // (IE < 8 does this) - div.innerHTML = ""; - div.style.width = div.style.padding = "1px"; - div.style.border = 0; - div.style.overflow = "hidden"; - div.style.display = "inline"; - div.style.zoom = 1; - support.inlineBlockNeedsLayout = ( div.offsetWidth === 3 ); - - // Check if elements with layout shrink-wrap their children - // (IE 6 does this) - div.style.display = "block"; - div.style.overflow = "visible"; - div.innerHTML = "
"; - support.shrinkWrapBlocks = ( div.offsetWidth !== 3 ); - } - - div.style.cssText = positionTopLeftWidthHeight + paddingMarginBorderVisibility; - div.innerHTML = html; - - outer = div.firstChild; - inner = outer.firstChild; - td = outer.nextSibling.firstChild.firstChild; - - offsetSupport = { - doesNotAddBorder: ( inner.offsetTop !== 5 ), - doesAddBorderForTableAndCells: ( td.offsetTop === 5 ) - }; - - inner.style.position = "fixed"; - inner.style.top = "20px"; - - // safari subtracts parent border width here which is 5px - offsetSupport.fixedPosition = ( inner.offsetTop === 20 || inner.offsetTop === 15 ); - inner.style.position = inner.style.top = ""; - - outer.style.overflow = "hidden"; - outer.style.position = "relative"; - - offsetSupport.subtractsBorderForOverflowNotVisible = ( inner.offsetTop === -5 ); - offsetSupport.doesNotIncludeMarginInBodyOffset = ( body.offsetTop !== conMarginTop ); - - if ( window.getComputedStyle ) { - div.style.marginTop = "1%"; - support.pixelMargin = ( window.getComputedStyle( div, null ) || { marginTop: 0 } ).marginTop !== "1%"; - } - - if ( typeof container.style.zoom !== "undefined" ) { - container.style.zoom = 1; - } - - body.removeChild( container ); - marginDiv = div = container = null; - - jQuery.extend( support, offsetSupport ); - }); - - return support; -})(); - - - - -var rbrace = /^(?:\{.*\}|\[.*\])$/, - rmultiDash = /([A-Z])/g; - -jQuery.extend({ - cache: {}, - - // Please use with caution - uuid: 0, - - // Unique for each copy of jQuery on the page - // Non-digits removed to match rinlinejQuery - expando: "jQuery" + ( jQuery.fn.jquery + Math.random() ).replace( /\D/g, "" ), - - // The following elements throw uncatchable exceptions if you - // attempt to add expando properties to them. - noData: { - "embed": true, - // Ban all objects except for Flash (which handle expandos) - "object": "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000", - "applet": true - }, - - hasData: function( elem ) { - elem = elem.nodeType ? jQuery.cache[ elem[jQuery.expando] ] : elem[ jQuery.expando ]; - return !!elem && !isEmptyDataObject( elem ); - }, - - data: function( elem, name, data, pvt /* Internal Use Only */ ) { - if ( !jQuery.acceptData( elem ) ) { - return; - } - - var privateCache, thisCache, ret, - internalKey = jQuery.expando, - getByName = typeof name === "string", - - // We have to handle DOM nodes and JS objects differently because IE6-7 - // can't GC object references properly across the DOM-JS boundary - isNode = elem.nodeType, - - // Only DOM nodes need the global jQuery cache; JS object data is - // attached directly to the object so GC can occur automatically - cache = isNode ? jQuery.cache : elem, - - // Only defining an ID for JS objects if its cache already exists allows - // the code to shortcut on the same path as a DOM node with no cache - id = isNode ? elem[ internalKey ] : elem[ internalKey ] && internalKey, - isEvents = name === "events"; - - // Avoid doing any more work than we need to when trying to get data on an - // object that has no data at all - if ( (!id || !cache[id] || (!isEvents && !pvt && !cache[id].data)) && getByName && data === undefined ) { - return; - } - - if ( !id ) { - // Only DOM nodes need a new unique ID for each element since their data - // ends up in the global cache - if ( isNode ) { - elem[ internalKey ] = id = ++jQuery.uuid; - } else { - id = internalKey; - } - } - - if ( !cache[ id ] ) { - cache[ id ] = {}; - - // Avoids exposing jQuery metadata on plain JS objects when the object - // is serialized using JSON.stringify - if ( !isNode ) { - cache[ id ].toJSON = jQuery.noop; - } - } - - // An object can be passed to jQuery.data instead of a key/value pair; this gets - // shallow copied over onto the existing cache - if ( typeof name === "object" || typeof name === "function" ) { - if ( pvt ) { - cache[ id ] = jQuery.extend( cache[ id ], name ); - } else { - cache[ id ].data = jQuery.extend( cache[ id ].data, name ); - } - } - - privateCache = thisCache = cache[ id ]; - - // jQuery data() is stored in a separate object inside the object's internal data - // cache in order to avoid key collisions between internal data and user-defined - // data. - if ( !pvt ) { - if ( !thisCache.data ) { - thisCache.data = {}; - } - - thisCache = thisCache.data; - } - - if ( data !== undefined ) { - thisCache[ jQuery.camelCase( name ) ] = data; - } - - // Users should not attempt to inspect the internal events object using jQuery.data, - // it is undocumented and subject to change. But does anyone listen? No. - if ( isEvents && !thisCache[ name ] ) { - return privateCache.events; - } - - // Check for both converted-to-camel and non-converted data property names - // If a data property was specified - if ( getByName ) { - - // First Try to find as-is property data - ret = thisCache[ name ]; - - // Test for null|undefined property data - if ( ret == null ) { - - // Try to find the camelCased property - ret = thisCache[ jQuery.camelCase( name ) ]; - } - } else { - ret = thisCache; - } - - return ret; - }, - - removeData: function( elem, name, pvt /* Internal Use Only */ ) { - if ( !jQuery.acceptData( elem ) ) { - return; - } - - var thisCache, i, l, - - // Reference to internal data cache key - internalKey = jQuery.expando, - - isNode = elem.nodeType, - - // See jQuery.data for more information - cache = isNode ? jQuery.cache : elem, - - // See jQuery.data for more information - id = isNode ? elem[ internalKey ] : internalKey; - - // If there is already no cache entry for this object, there is no - // purpose in continuing - if ( !cache[ id ] ) { - return; - } - - if ( name ) { - - thisCache = pvt ? cache[ id ] : cache[ id ].data; - - if ( thisCache ) { - - // Support array or space separated string names for data keys - if ( !jQuery.isArray( name ) ) { - - // try the string as a key before any manipulation - if ( name in thisCache ) { - name = [ name ]; - } else { - - // split the camel cased version by spaces unless a key with the spaces exists - name = jQuery.camelCase( name ); - if ( name in thisCache ) { - name = [ name ]; - } else { - name = name.split( " " ); - } - } - } - - for ( i = 0, l = name.length; i < l; i++ ) { - delete thisCache[ name[i] ]; - } - - // If there is no data left in the cache, we want to continue - // and let the cache object itself get destroyed - if ( !( pvt ? isEmptyDataObject : jQuery.isEmptyObject )( thisCache ) ) { - return; - } - } - } - - // See jQuery.data for more information - if ( !pvt ) { - delete cache[ id ].data; - - // Don't destroy the parent cache unless the internal data object - // had been the only thing left in it - if ( !isEmptyDataObject(cache[ id ]) ) { - return; - } - } - - // Browsers that fail expando deletion also refuse to delete expandos on - // the window, but it will allow it on all other JS objects; other browsers - // don't care - // Ensure that `cache` is not a window object #10080 - if ( jQuery.support.deleteExpando || !cache.setInterval ) { - delete cache[ id ]; - } else { - cache[ id ] = null; - } - - // We destroyed the cache and need to eliminate the expando on the node to avoid - // false lookups in the cache for entries that no longer exist - if ( isNode ) { - // IE does not allow us to delete expando properties from nodes, - // nor does it have a removeAttribute function on Document nodes; - // we must handle all of these cases - if ( jQuery.support.deleteExpando ) { - delete elem[ internalKey ]; - } else if ( elem.removeAttribute ) { - elem.removeAttribute( internalKey ); - } else { - elem[ internalKey ] = null; - } - } - }, - - // For internal use only. - _data: function( elem, name, data ) { - return jQuery.data( elem, name, data, true ); - }, - - // A method for determining if a DOM node can handle the data expando - acceptData: function( elem ) { - if ( elem.nodeName ) { - var match = jQuery.noData[ elem.nodeName.toLowerCase() ]; - - if ( match ) { - return !(match === true || elem.getAttribute("classid") !== match); - } - } - - return true; - } -}); - -jQuery.fn.extend({ - data: function( key, value ) { - var parts, part, attr, name, l, - elem = this[0], - i = 0, - data = null; - - // Gets all values - if ( key === undefined ) { - if ( this.length ) { - data = jQuery.data( elem ); - - if ( elem.nodeType === 1 && !jQuery._data( elem, "parsedAttrs" ) ) { - attr = elem.attributes; - for ( l = attr.length; i < l; i++ ) { - name = attr[i].name; - - if ( name.indexOf( "data-" ) === 0 ) { - name = jQuery.camelCase( name.substring(5) ); - - dataAttr( elem, name, data[ name ] ); - } - } - jQuery._data( elem, "parsedAttrs", true ); - } - } - - return data; - } - - // Sets multiple values - if ( typeof key === "object" ) { - return this.each(function() { - jQuery.data( this, key ); - }); - } - - parts = key.split( ".", 2 ); - parts[1] = parts[1] ? "." + parts[1] : ""; - part = parts[1] + "!"; - - return jQuery.access( this, function( value ) { - - if ( value === undefined ) { - data = this.triggerHandler( "getData" + part, [ parts[0] ] ); - - // Try to fetch any internally stored data first - if ( data === undefined && elem ) { - data = jQuery.data( elem, key ); - data = dataAttr( elem, key, data ); - } - - return data === undefined && parts[1] ? - this.data( parts[0] ) : - data; - } - - parts[1] = value; - this.each(function() { - var self = jQuery( this ); - - self.triggerHandler( "setData" + part, parts ); - jQuery.data( this, key, value ); - self.triggerHandler( "changeData" + part, parts ); - }); - }, null, value, arguments.length > 1, null, false ); - }, - - removeData: function( key ) { - return this.each(function() { - jQuery.removeData( this, key ); - }); - } -}); - -function dataAttr( elem, key, data ) { - // If nothing was found internally, try to fetch any - // data from the HTML5 data-* attribute - if ( data === undefined && elem.nodeType === 1 ) { - - var name = "data-" + key.replace( rmultiDash, "-$1" ).toLowerCase(); - - data = elem.getAttribute( name ); - - if ( typeof data === "string" ) { - try { - data = data === "true" ? true : - data === "false" ? false : - data === "null" ? null : - jQuery.isNumeric( data ) ? +data : - rbrace.test( data ) ? jQuery.parseJSON( data ) : - data; - } catch( e ) {} - - // Make sure we set the data so it isn't changed later - jQuery.data( elem, key, data ); - - } else { - data = undefined; - } - } - - return data; -} - -// checks a cache object for emptiness -function isEmptyDataObject( obj ) { - for ( var name in obj ) { - - // if the public data object is empty, the private is still empty - if ( name === "data" && jQuery.isEmptyObject( obj[name] ) ) { - continue; - } - if ( name !== "toJSON" ) { - return false; - } - } - - return true; -} - - - - -function handleQueueMarkDefer( elem, type, src ) { - var deferDataKey = type + "defer", - queueDataKey = type + "queue", - markDataKey = type + "mark", - defer = jQuery._data( elem, deferDataKey ); - if ( defer && - ( src === "queue" || !jQuery._data(elem, queueDataKey) ) && - ( src === "mark" || !jQuery._data(elem, markDataKey) ) ) { - // Give room for hard-coded callbacks to fire first - // and eventually mark/queue something else on the element - setTimeout( function() { - if ( !jQuery._data( elem, queueDataKey ) && - !jQuery._data( elem, markDataKey ) ) { - jQuery.removeData( elem, deferDataKey, true ); - defer.fire(); - } - }, 0 ); - } -} - -jQuery.extend({ - - _mark: function( elem, type ) { - if ( elem ) { - type = ( type || "fx" ) + "mark"; - jQuery._data( elem, type, (jQuery._data( elem, type ) || 0) + 1 ); - } - }, - - _unmark: function( force, elem, type ) { - if ( force !== true ) { - type = elem; - elem = force; - force = false; - } - if ( elem ) { - type = type || "fx"; - var key = type + "mark", - count = force ? 0 : ( (jQuery._data( elem, key ) || 1) - 1 ); - if ( count ) { - jQuery._data( elem, key, count ); - } else { - jQuery.removeData( elem, key, true ); - handleQueueMarkDefer( elem, type, "mark" ); - } - } - }, - - queue: function( elem, type, data ) { - var q; - if ( elem ) { - type = ( type || "fx" ) + "queue"; - q = jQuery._data( elem, type ); - - // Speed up dequeue by getting out quickly if this is just a lookup - if ( data ) { - if ( !q || jQuery.isArray(data) ) { - q = jQuery._data( elem, type, jQuery.makeArray(data) ); - } else { - q.push( data ); - } - } - return q || []; - } - }, - - dequeue: function( elem, type ) { - type = type || "fx"; - - var queue = jQuery.queue( elem, type ), - fn = queue.shift(), - hooks = {}; - - // If the fx queue is dequeued, always remove the progress sentinel - if ( fn === "inprogress" ) { - fn = queue.shift(); - } - - if ( fn ) { - // Add a progress sentinel to prevent the fx queue from being - // automatically dequeued - if ( type === "fx" ) { - queue.unshift( "inprogress" ); - } - - jQuery._data( elem, type + ".run", hooks ); - fn.call( elem, function() { - jQuery.dequeue( elem, type ); - }, hooks ); - } - - if ( !queue.length ) { - jQuery.removeData( elem, type + "queue " + type + ".run", true ); - handleQueueMarkDefer( elem, type, "queue" ); - } - } -}); - -jQuery.fn.extend({ - queue: function( type, data ) { - var setter = 2; - - if ( typeof type !== "string" ) { - data = type; - type = "fx"; - setter--; - } - - if ( arguments.length < setter ) { - return jQuery.queue( this[0], type ); - } - - return data === undefined ? - this : - this.each(function() { - var queue = jQuery.queue( this, type, data ); - - if ( type === "fx" && queue[0] !== "inprogress" ) { - jQuery.dequeue( this, type ); - } - }); - }, - dequeue: function( type ) { - return this.each(function() { - jQuery.dequeue( this, type ); - }); - }, - // Based off of the plugin by Clint Helfers, with permission. - // http://blindsignals.com/index.php/2009/07/jquery-delay/ - delay: function( time, type ) { - time = jQuery.fx ? jQuery.fx.speeds[ time ] || time : time; - type = type || "fx"; - - return this.queue( type, function( next, hooks ) { - var timeout = setTimeout( next, time ); - hooks.stop = function() { - clearTimeout( timeout ); - }; - }); - }, - clearQueue: function( type ) { - return this.queue( type || "fx", [] ); - }, - // Get a promise resolved when queues of a certain type - // are emptied (fx is the type by default) - promise: function( type, object ) { - if ( typeof type !== "string" ) { - object = type; - type = undefined; - } - type = type || "fx"; - var defer = jQuery.Deferred(), - elements = this, - i = elements.length, - count = 1, - deferDataKey = type + "defer", - queueDataKey = type + "queue", - markDataKey = type + "mark", - tmp; - function resolve() { - if ( !( --count ) ) { - defer.resolveWith( elements, [ elements ] ); - } - } - while( i-- ) { - if (( tmp = jQuery.data( elements[ i ], deferDataKey, undefined, true ) || - ( jQuery.data( elements[ i ], queueDataKey, undefined, true ) || - jQuery.data( elements[ i ], markDataKey, undefined, true ) ) && - jQuery.data( elements[ i ], deferDataKey, jQuery.Callbacks( "once memory" ), true ) )) { - count++; - tmp.add( resolve ); - } - } - resolve(); - return defer.promise( object ); - } -}); - - - - -var rclass = /[\n\t\r]/g, - rspace = /\s+/, - rreturn = /\r/g, - rtype = /^(?:button|input)$/i, - rfocusable = /^(?:button|input|object|select|textarea)$/i, - rclickable = /^a(?:rea)?$/i, - rboolean = /^(?:autofocus|autoplay|async|checked|controls|defer|disabled|hidden|loop|multiple|open|readonly|required|scoped|selected)$/i, - getSetAttribute = jQuery.support.getSetAttribute, - nodeHook, boolHook, fixSpecified; - -jQuery.fn.extend({ - attr: function( name, value ) { - return jQuery.access( this, jQuery.attr, name, value, arguments.length > 1 ); - }, - - removeAttr: function( name ) { - return this.each(function() { - jQuery.removeAttr( this, name ); - }); - }, - - prop: function( name, value ) { - return jQuery.access( this, jQuery.prop, name, value, arguments.length > 1 ); - }, - - removeProp: function( name ) { - name = jQuery.propFix[ name ] || name; - return this.each(function() { - // try/catch handles cases where IE balks (such as removing a property on window) - try { - this[ name ] = undefined; - delete this[ name ]; - } catch( e ) {} - }); - }, - - addClass: function( value ) { - var classNames, i, l, elem, - setClass, c, cl; - - if ( jQuery.isFunction( value ) ) { - return this.each(function( j ) { - jQuery( this ).addClass( value.call(this, j, this.className) ); - }); - } - - if ( value && typeof value === "string" ) { - classNames = value.split( rspace ); - - for ( i = 0, l = this.length; i < l; i++ ) { - elem = this[ i ]; - - if ( elem.nodeType === 1 ) { - if ( !elem.className && classNames.length === 1 ) { - elem.className = value; - - } else { - setClass = " " + elem.className + " "; - - for ( c = 0, cl = classNames.length; c < cl; c++ ) { - if ( !~setClass.indexOf( " " + classNames[ c ] + " " ) ) { - setClass += classNames[ c ] + " "; - } - } - elem.className = jQuery.trim( setClass ); - } - } - } - } - - return this; - }, - - removeClass: function( value ) { - var classNames, i, l, elem, className, c, cl; - - if ( jQuery.isFunction( value ) ) { - return this.each(function( j ) { - jQuery( this ).removeClass( value.call(this, j, this.className) ); - }); - } - - if ( (value && typeof value === "string") || value === undefined ) { - classNames = ( value || "" ).split( rspace ); - - for ( i = 0, l = this.length; i < l; i++ ) { - elem = this[ i ]; - - if ( elem.nodeType === 1 && elem.className ) { - if ( value ) { - className = (" " + elem.className + " ").replace( rclass, " " ); - for ( c = 0, cl = classNames.length; c < cl; c++ ) { - className = className.replace(" " + classNames[ c ] + " ", " "); - } - elem.className = jQuery.trim( className ); - - } else { - elem.className = ""; - } - } - } - } - - return this; - }, - - toggleClass: function( value, stateVal ) { - var type = typeof value, - isBool = typeof stateVal === "boolean"; - - if ( jQuery.isFunction( value ) ) { - return this.each(function( i ) { - jQuery( this ).toggleClass( value.call(this, i, this.className, stateVal), stateVal ); - }); - } - - return this.each(function() { - if ( type === "string" ) { - // toggle individual class names - var className, - i = 0, - self = jQuery( this ), - state = stateVal, - classNames = value.split( rspace ); - - while ( (className = classNames[ i++ ]) ) { - // check each className given, space seperated list - state = isBool ? state : !self.hasClass( className ); - self[ state ? "addClass" : "removeClass" ]( className ); - } - - } else if ( type === "undefined" || type === "boolean" ) { - if ( this.className ) { - // store className if set - jQuery._data( this, "__className__", this.className ); - } - - // toggle whole className - this.className = this.className || value === false ? "" : jQuery._data( this, "__className__" ) || ""; - } - }); - }, - - hasClass: function( selector ) { - var className = " " + selector + " ", - i = 0, - l = this.length; - for ( ; i < l; i++ ) { - if ( this[i].nodeType === 1 && (" " + this[i].className + " ").replace(rclass, " ").indexOf( className ) > -1 ) { - return true; - } - } - - return false; - }, - - val: function( value ) { - var hooks, ret, isFunction, - elem = this[0]; - - if ( !arguments.length ) { - if ( elem ) { - hooks = jQuery.valHooks[ elem.type ] || jQuery.valHooks[ elem.nodeName.toLowerCase() ]; - - if ( hooks && "get" in hooks && (ret = hooks.get( elem, "value" )) !== undefined ) { - return ret; - } - - ret = elem.value; - - return typeof ret === "string" ? - // handle most common string cases - ret.replace(rreturn, "") : - // handle cases where value is null/undef or number - ret == null ? "" : ret; - } - - return; - } - - isFunction = jQuery.isFunction( value ); - - return this.each(function( i ) { - var self = jQuery(this), val; - - if ( this.nodeType !== 1 ) { - return; - } - - if ( isFunction ) { - val = value.call( this, i, self.val() ); - } else { - val = value; - } - - // Treat null/undefined as ""; convert numbers to string - if ( val == null ) { - val = ""; - } else if ( typeof val === "number" ) { - val += ""; - } else if ( jQuery.isArray( val ) ) { - val = jQuery.map(val, function ( value ) { - return value == null ? "" : value + ""; - }); - } - - hooks = jQuery.valHooks[ this.type ] || jQuery.valHooks[ this.nodeName.toLowerCase() ]; - - // If set returns undefined, fall back to normal setting - if ( !hooks || !("set" in hooks) || hooks.set( this, val, "value" ) === undefined ) { - this.value = val; - } - }); - } -}); - -jQuery.extend({ - valHooks: { - option: { - get: function( elem ) { - // attributes.value is undefined in Blackberry 4.7 but - // uses .value. See #6932 - var val = elem.attributes.value; - return !val || val.specified ? elem.value : elem.text; - } - }, - select: { - get: function( elem ) { - var value, i, max, option, - index = elem.selectedIndex, - values = [], - options = elem.options, - one = elem.type === "select-one"; - - // Nothing was selected - if ( index < 0 ) { - return null; - } - - // Loop through all the selected options - i = one ? index : 0; - max = one ? index + 1 : options.length; - for ( ; i < max; i++ ) { - option = options[ i ]; - - // Don't return options that are disabled or in a disabled optgroup - if ( option.selected && (jQuery.support.optDisabled ? !option.disabled : option.getAttribute("disabled") === null) && - (!option.parentNode.disabled || !jQuery.nodeName( option.parentNode, "optgroup" )) ) { - - // Get the specific value for the option - value = jQuery( option ).val(); - - // We don't need an array for one selects - if ( one ) { - return value; - } - - // Multi-Selects return an array - values.push( value ); - } - } - - // Fixes Bug #2551 -- select.val() broken in IE after form.reset() - if ( one && !values.length && options.length ) { - return jQuery( options[ index ] ).val(); - } - - return values; - }, - - set: function( elem, value ) { - var values = jQuery.makeArray( value ); - - jQuery(elem).find("option").each(function() { - this.selected = jQuery.inArray( jQuery(this).val(), values ) >= 0; - }); - - if ( !values.length ) { - elem.selectedIndex = -1; - } - return values; - } - } - }, - - attrFn: { - val: true, - css: true, - html: true, - text: true, - data: true, - width: true, - height: true, - offset: true - }, - - attr: function( elem, name, value, pass ) { - var ret, hooks, notxml, - nType = elem.nodeType; - - // don't get/set attributes on text, comment and attribute nodes - if ( !elem || nType === 3 || nType === 8 || nType === 2 ) { - return; - } - - if ( pass && name in jQuery.attrFn ) { - return jQuery( elem )[ name ]( value ); - } - - // Fallback to prop when attributes are not supported - if ( typeof elem.getAttribute === "undefined" ) { - return jQuery.prop( elem, name, value ); - } - - notxml = nType !== 1 || !jQuery.isXMLDoc( elem ); - - // All attributes are lowercase - // Grab necessary hook if one is defined - if ( notxml ) { - name = name.toLowerCase(); - hooks = jQuery.attrHooks[ name ] || ( rboolean.test( name ) ? boolHook : nodeHook ); - } - - if ( value !== undefined ) { - - if ( value === null ) { - jQuery.removeAttr( elem, name ); - return; - - } else if ( hooks && "set" in hooks && notxml && (ret = hooks.set( elem, value, name )) !== undefined ) { - return ret; - - } else { - elem.setAttribute( name, "" + value ); - return value; - } - - } else if ( hooks && "get" in hooks && notxml && (ret = hooks.get( elem, name )) !== null ) { - return ret; - - } else { - - ret = elem.getAttribute( name ); - - // Non-existent attributes return null, we normalize to undefined - return ret === null ? - undefined : - ret; - } - }, - - removeAttr: function( elem, value ) { - var propName, attrNames, name, l, isBool, - i = 0; - - if ( value && elem.nodeType === 1 ) { - attrNames = value.toLowerCase().split( rspace ); - l = attrNames.length; - - for ( ; i < l; i++ ) { - name = attrNames[ i ]; - - if ( name ) { - propName = jQuery.propFix[ name ] || name; - isBool = rboolean.test( name ); - - // See #9699 for explanation of this approach (setting first, then removal) - // Do not do this for boolean attributes (see #10870) - if ( !isBool ) { - jQuery.attr( elem, name, "" ); - } - elem.removeAttribute( getSetAttribute ? name : propName ); - - // Set corresponding property to false for boolean attributes - if ( isBool && propName in elem ) { - elem[ propName ] = false; - } - } - } - } - }, - - attrHooks: { - type: { - set: function( elem, value ) { - // We can't allow the type property to be changed (since it causes problems in IE) - if ( rtype.test( elem.nodeName ) && elem.parentNode ) { - jQuery.error( "type property can't be changed" ); - } else if ( !jQuery.support.radioValue && value === "radio" && jQuery.nodeName(elem, "input") ) { - // Setting the type on a radio button after the value resets the value in IE6-9 - // Reset value to it's default in case type is set after value - // This is for element creation - var val = elem.value; - elem.setAttribute( "type", value ); - if ( val ) { - elem.value = val; - } - return value; - } - } - }, - // Use the value property for back compat - // Use the nodeHook for button elements in IE6/7 (#1954) - value: { - get: function( elem, name ) { - if ( nodeHook && jQuery.nodeName( elem, "button" ) ) { - return nodeHook.get( elem, name ); - } - return name in elem ? - elem.value : - null; - }, - set: function( elem, value, name ) { - if ( nodeHook && jQuery.nodeName( elem, "button" ) ) { - return nodeHook.set( elem, value, name ); - } - // Does not return so that setAttribute is also used - elem.value = value; - } - } - }, - - propFix: { - tabindex: "tabIndex", - readonly: "readOnly", - "for": "htmlFor", - "class": "className", - maxlength: "maxLength", - cellspacing: "cellSpacing", - cellpadding: "cellPadding", - rowspan: "rowSpan", - colspan: "colSpan", - usemap: "useMap", - frameborder: "frameBorder", - contenteditable: "contentEditable" - }, - - prop: function( elem, name, value ) { - var ret, hooks, notxml, - nType = elem.nodeType; - - // don't get/set properties on text, comment and attribute nodes - if ( !elem || nType === 3 || nType === 8 || nType === 2 ) { - return; - } - - notxml = nType !== 1 || !jQuery.isXMLDoc( elem ); - - if ( notxml ) { - // Fix name and attach hooks - name = jQuery.propFix[ name ] || name; - hooks = jQuery.propHooks[ name ]; - } - - if ( value !== undefined ) { - if ( hooks && "set" in hooks && (ret = hooks.set( elem, value, name )) !== undefined ) { - return ret; - - } else { - return ( elem[ name ] = value ); - } - - } else { - if ( hooks && "get" in hooks && (ret = hooks.get( elem, name )) !== null ) { - return ret; - - } else { - return elem[ name ]; - } - } - }, - - propHooks: { - tabIndex: { - get: function( elem ) { - // elem.tabIndex doesn't always return the correct value when it hasn't been explicitly set - // http://fluidproject.org/blog/2008/01/09/getting-setting-and-removing-tabindex-values-with-javascript/ - var attributeNode = elem.getAttributeNode("tabindex"); - - return attributeNode && attributeNode.specified ? - parseInt( attributeNode.value, 10 ) : - rfocusable.test( elem.nodeName ) || rclickable.test( elem.nodeName ) && elem.href ? - 0 : - undefined; - } - } - } -}); - -// Add the tabIndex propHook to attrHooks for back-compat (different case is intentional) -jQuery.attrHooks.tabindex = jQuery.propHooks.tabIndex; - -// Hook for boolean attributes -boolHook = { - get: function( elem, name ) { - // Align boolean attributes with corresponding properties - // Fall back to attribute presence where some booleans are not supported - var attrNode, - property = jQuery.prop( elem, name ); - return property === true || typeof property !== "boolean" && ( attrNode = elem.getAttributeNode(name) ) && attrNode.nodeValue !== false ? - name.toLowerCase() : - undefined; - }, - set: function( elem, value, name ) { - var propName; - if ( value === false ) { - // Remove boolean attributes when set to false - jQuery.removeAttr( elem, name ); - } else { - // value is true since we know at this point it's type boolean and not false - // Set boolean attributes to the same name and set the DOM property - propName = jQuery.propFix[ name ] || name; - if ( propName in elem ) { - // Only set the IDL specifically if it already exists on the element - elem[ propName ] = true; - } - - elem.setAttribute( name, name.toLowerCase() ); - } - return name; - } -}; - -// IE6/7 do not support getting/setting some attributes with get/setAttribute -if ( !getSetAttribute ) { - - fixSpecified = { - name: true, - id: true, - coords: true - }; - - // Use this for any attribute in IE6/7 - // This fixes almost every IE6/7 issue - nodeHook = jQuery.valHooks.button = { - get: function( elem, name ) { - var ret; - ret = elem.getAttributeNode( name ); - return ret && ( fixSpecified[ name ] ? ret.nodeValue !== "" : ret.specified ) ? - ret.nodeValue : - undefined; - }, - set: function( elem, value, name ) { - // Set the existing or create a new attribute node - var ret = elem.getAttributeNode( name ); - if ( !ret ) { - ret = document.createAttribute( name ); - elem.setAttributeNode( ret ); - } - return ( ret.nodeValue = value + "" ); - } - }; - - // Apply the nodeHook to tabindex - jQuery.attrHooks.tabindex.set = nodeHook.set; - - // Set width and height to auto instead of 0 on empty string( Bug #8150 ) - // This is for removals - jQuery.each([ "width", "height" ], function( i, name ) { - jQuery.attrHooks[ name ] = jQuery.extend( jQuery.attrHooks[ name ], { - set: function( elem, value ) { - if ( value === "" ) { - elem.setAttribute( name, "auto" ); - return value; - } - } - }); - }); - - // Set contenteditable to false on removals(#10429) - // Setting to empty string throws an error as an invalid value - jQuery.attrHooks.contenteditable = { - get: nodeHook.get, - set: function( elem, value, name ) { - if ( value === "" ) { - value = "false"; - } - nodeHook.set( elem, value, name ); - } - }; -} - - -// Some attributes require a special call on IE -if ( !jQuery.support.hrefNormalized ) { - jQuery.each([ "href", "src", "width", "height" ], function( i, name ) { - jQuery.attrHooks[ name ] = jQuery.extend( jQuery.attrHooks[ name ], { - get: function( elem ) { - var ret = elem.getAttribute( name, 2 ); - return ret === null ? undefined : ret; - } - }); - }); -} - -if ( !jQuery.support.style ) { - jQuery.attrHooks.style = { - get: function( elem ) { - // Return undefined in the case of empty string - // Normalize to lowercase since IE uppercases css property names - return elem.style.cssText.toLowerCase() || undefined; - }, - set: function( elem, value ) { - return ( elem.style.cssText = "" + value ); - } - }; -} - -// Safari mis-reports the default selected property of an option -// Accessing the parent's selectedIndex property fixes it -if ( !jQuery.support.optSelected ) { - jQuery.propHooks.selected = jQuery.extend( jQuery.propHooks.selected, { - get: function( elem ) { - var parent = elem.parentNode; - - if ( parent ) { - parent.selectedIndex; - - // Make sure that it also works with optgroups, see #5701 - if ( parent.parentNode ) { - parent.parentNode.selectedIndex; - } - } - return null; - } - }); -} - -// IE6/7 call enctype encoding -if ( !jQuery.support.enctype ) { - jQuery.propFix.enctype = "encoding"; -} - -// Radios and checkboxes getter/setter -if ( !jQuery.support.checkOn ) { - jQuery.each([ "radio", "checkbox" ], function() { - jQuery.valHooks[ this ] = { - get: function( elem ) { - // Handle the case where in Webkit "" is returned instead of "on" if a value isn't specified - return elem.getAttribute("value") === null ? "on" : elem.value; - } - }; - }); -} -jQuery.each([ "radio", "checkbox" ], function() { - jQuery.valHooks[ this ] = jQuery.extend( jQuery.valHooks[ this ], { - set: function( elem, value ) { - if ( jQuery.isArray( value ) ) { - return ( elem.checked = jQuery.inArray( jQuery(elem).val(), value ) >= 0 ); - } - } - }); -}); - - - - -var rformElems = /^(?:textarea|input|select)$/i, - rtypenamespace = /^([^\.]*)?(?:\.(.+))?$/, - rhoverHack = /(?:^|\s)hover(\.\S+)?\b/, - rkeyEvent = /^key/, - rmouseEvent = /^(?:mouse|contextmenu)|click/, - rfocusMorph = /^(?:focusinfocus|focusoutblur)$/, - rquickIs = /^(\w*)(?:#([\w\-]+))?(?:\.([\w\-]+))?$/, - quickParse = function( selector ) { - var quick = rquickIs.exec( selector ); - if ( quick ) { - // 0 1 2 3 - // [ _, tag, id, class ] - quick[1] = ( quick[1] || "" ).toLowerCase(); - quick[3] = quick[3] && new RegExp( "(?:^|\\s)" + quick[3] + "(?:\\s|$)" ); - } - return quick; - }, - quickIs = function( elem, m ) { - var attrs = elem.attributes || {}; - return ( - (!m[1] || elem.nodeName.toLowerCase() === m[1]) && - (!m[2] || (attrs.id || {}).value === m[2]) && - (!m[3] || m[3].test( (attrs[ "class" ] || {}).value )) - ); - }, - hoverHack = function( events ) { - return jQuery.event.special.hover ? events : events.replace( rhoverHack, "mouseenter$1 mouseleave$1" ); - }; - -/* - * Helper functions for managing events -- not part of the public interface. - * Props to Dean Edwards' addEvent library for many of the ideas. - */ -jQuery.event = { - - add: function( elem, types, handler, data, selector ) { - - var elemData, eventHandle, events, - t, tns, type, namespaces, handleObj, - handleObjIn, quick, handlers, special; - - // Don't attach events to noData or text/comment nodes (allow plain objects tho) - if ( elem.nodeType === 3 || elem.nodeType === 8 || !types || !handler || !(elemData = jQuery._data( elem )) ) { - return; - } - - // Caller can pass in an object of custom data in lieu of the handler - if ( handler.handler ) { - handleObjIn = handler; - handler = handleObjIn.handler; - selector = handleObjIn.selector; - } - - // Make sure that the handler has a unique ID, used to find/remove it later - if ( !handler.guid ) { - handler.guid = jQuery.guid++; - } - - // Init the element's event structure and main handler, if this is the first - events = elemData.events; - if ( !events ) { - elemData.events = events = {}; - } - eventHandle = elemData.handle; - if ( !eventHandle ) { - elemData.handle = eventHandle = function( e ) { - // Discard the second event of a jQuery.event.trigger() and - // when an event is called after a page has unloaded - return typeof jQuery !== "undefined" && (!e || jQuery.event.triggered !== e.type) ? - jQuery.event.dispatch.apply( eventHandle.elem, arguments ) : - undefined; - }; - // Add elem as a property of the handle fn to prevent a memory leak with IE non-native events - eventHandle.elem = elem; - } - - // Handle multiple events separated by a space - // jQuery(...).bind("mouseover mouseout", fn); - types = jQuery.trim( hoverHack(types) ).split( " " ); - for ( t = 0; t < types.length; t++ ) { - - tns = rtypenamespace.exec( types[t] ) || []; - type = tns[1]; - namespaces = ( tns[2] || "" ).split( "." ).sort(); - - // If event changes its type, use the special event handlers for the changed type - special = jQuery.event.special[ type ] || {}; - - // If selector defined, determine special event api type, otherwise given type - type = ( selector ? special.delegateType : special.bindType ) || type; - - // Update special based on newly reset type - special = jQuery.event.special[ type ] || {}; - - // handleObj is passed to all event handlers - handleObj = jQuery.extend({ - type: type, - origType: tns[1], - data: data, - handler: handler, - guid: handler.guid, - selector: selector, - quick: selector && quickParse( selector ), - namespace: namespaces.join(".") - }, handleObjIn ); - - // Init the event handler queue if we're the first - handlers = events[ type ]; - if ( !handlers ) { - handlers = events[ type ] = []; - handlers.delegateCount = 0; - - // Only use addEventListener/attachEvent if the special events handler returns false - if ( !special.setup || special.setup.call( elem, data, namespaces, eventHandle ) === false ) { - // Bind the global event handler to the element - if ( elem.addEventListener ) { - elem.addEventListener( type, eventHandle, false ); - - } else if ( elem.attachEvent ) { - elem.attachEvent( "on" + type, eventHandle ); - } - } - } - - if ( special.add ) { - special.add.call( elem, handleObj ); - - if ( !handleObj.handler.guid ) { - handleObj.handler.guid = handler.guid; - } - } - - // Add to the element's handler list, delegates in front - if ( selector ) { - handlers.splice( handlers.delegateCount++, 0, handleObj ); - } else { - handlers.push( handleObj ); - } - - // Keep track of which events have ever been used, for event optimization - jQuery.event.global[ type ] = true; - } - - // Nullify elem to prevent memory leaks in IE - elem = null; - }, - - global: {}, - - // Detach an event or set of events from an element - remove: function( elem, types, handler, selector, mappedTypes ) { - - var elemData = jQuery.hasData( elem ) && jQuery._data( elem ), - t, tns, type, origType, namespaces, origCount, - j, events, special, handle, eventType, handleObj; - - if ( !elemData || !(events = elemData.events) ) { - return; - } - - // Once for each type.namespace in types; type may be omitted - types = jQuery.trim( hoverHack( types || "" ) ).split(" "); - for ( t = 0; t < types.length; t++ ) { - tns = rtypenamespace.exec( types[t] ) || []; - type = origType = tns[1]; - namespaces = tns[2]; - - // Unbind all events (on this namespace, if provided) for the element - if ( !type ) { - for ( type in events ) { - jQuery.event.remove( elem, type + types[ t ], handler, selector, true ); - } - continue; - } - - special = jQuery.event.special[ type ] || {}; - type = ( selector? special.delegateType : special.bindType ) || type; - eventType = events[ type ] || []; - origCount = eventType.length; - namespaces = namespaces ? new RegExp("(^|\\.)" + namespaces.split(".").sort().join("\\.(?:.*\\.)?") + "(\\.|$)") : null; - - // Remove matching events - for ( j = 0; j < eventType.length; j++ ) { - handleObj = eventType[ j ]; - - if ( ( mappedTypes || origType === handleObj.origType ) && - ( !handler || handler.guid === handleObj.guid ) && - ( !namespaces || namespaces.test( handleObj.namespace ) ) && - ( !selector || selector === handleObj.selector || selector === "**" && handleObj.selector ) ) { - eventType.splice( j--, 1 ); - - if ( handleObj.selector ) { - eventType.delegateCount--; - } - if ( special.remove ) { - special.remove.call( elem, handleObj ); - } - } - } - - // Remove generic event handler if we removed something and no more handlers exist - // (avoids potential for endless recursion during removal of special event handlers) - if ( eventType.length === 0 && origCount !== eventType.length ) { - if ( !special.teardown || special.teardown.call( elem, namespaces ) === false ) { - jQuery.removeEvent( elem, type, elemData.handle ); - } - - delete events[ type ]; - } - } - - // Remove the expando if it's no longer used - if ( jQuery.isEmptyObject( events ) ) { - handle = elemData.handle; - if ( handle ) { - handle.elem = null; - } - - // removeData also checks for emptiness and clears the expando if empty - // so use it instead of delete - jQuery.removeData( elem, [ "events", "handle" ], true ); - } - }, - - // Events that are safe to short-circuit if no handlers are attached. - // Native DOM events should not be added, they may have inline handlers. - customEvent: { - "getData": true, - "setData": true, - "changeData": true - }, - - trigger: function( event, data, elem, onlyHandlers ) { - // Don't do events on text and comment nodes - if ( elem && (elem.nodeType === 3 || elem.nodeType === 8) ) { - return; - } - - // Event object or event type - var type = event.type || event, - namespaces = [], - cache, exclusive, i, cur, old, ontype, special, handle, eventPath, bubbleType; - - // focus/blur morphs to focusin/out; ensure we're not firing them right now - if ( rfocusMorph.test( type + jQuery.event.triggered ) ) { - return; - } - - if ( type.indexOf( "!" ) >= 0 ) { - // Exclusive events trigger only for the exact event (no namespaces) - type = type.slice(0, -1); - exclusive = true; - } - - if ( type.indexOf( "." ) >= 0 ) { - // Namespaced trigger; create a regexp to match event type in handle() - namespaces = type.split("."); - type = namespaces.shift(); - namespaces.sort(); - } - - if ( (!elem || jQuery.event.customEvent[ type ]) && !jQuery.event.global[ type ] ) { - // No jQuery handlers for this event type, and it can't have inline handlers - return; - } - - // Caller can pass in an Event, Object, or just an event type string - event = typeof event === "object" ? - // jQuery.Event object - event[ jQuery.expando ] ? event : - // Object literal - new jQuery.Event( type, event ) : - // Just the event type (string) - new jQuery.Event( type ); - - event.type = type; - event.isTrigger = true; - event.exclusive = exclusive; - event.namespace = namespaces.join( "." ); - event.namespace_re = event.namespace? new RegExp("(^|\\.)" + namespaces.join("\\.(?:.*\\.)?") + "(\\.|$)") : null; - ontype = type.indexOf( ":" ) < 0 ? "on" + type : ""; - - // Handle a global trigger - if ( !elem ) { - - // TODO: Stop taunting the data cache; remove global events and always attach to document - cache = jQuery.cache; - for ( i in cache ) { - if ( cache[ i ].events && cache[ i ].events[ type ] ) { - jQuery.event.trigger( event, data, cache[ i ].handle.elem, true ); - } - } - return; - } - - // Clean up the event in case it is being reused - event.result = undefined; - if ( !event.target ) { - event.target = elem; - } - - // Clone any incoming data and prepend the event, creating the handler arg list - data = data != null ? jQuery.makeArray( data ) : []; - data.unshift( event ); - - // Allow special events to draw outside the lines - special = jQuery.event.special[ type ] || {}; - if ( special.trigger && special.trigger.apply( elem, data ) === false ) { - return; - } - - // Determine event propagation path in advance, per W3C events spec (#9951) - // Bubble up to document, then to window; watch for a global ownerDocument var (#9724) - eventPath = [[ elem, special.bindType || type ]]; - if ( !onlyHandlers && !special.noBubble && !jQuery.isWindow( elem ) ) { - - bubbleType = special.delegateType || type; - cur = rfocusMorph.test( bubbleType + type ) ? elem : elem.parentNode; - old = null; - for ( ; cur; cur = cur.parentNode ) { - eventPath.push([ cur, bubbleType ]); - old = cur; - } - - // Only add window if we got to document (e.g., not plain obj or detached DOM) - if ( old && old === elem.ownerDocument ) { - eventPath.push([ old.defaultView || old.parentWindow || window, bubbleType ]); - } - } - - // Fire handlers on the event path - for ( i = 0; i < eventPath.length && !event.isPropagationStopped(); i++ ) { - - cur = eventPath[i][0]; - event.type = eventPath[i][1]; - - handle = ( jQuery._data( cur, "events" ) || {} )[ event.type ] && jQuery._data( cur, "handle" ); - if ( handle ) { - handle.apply( cur, data ); - } - // Note that this is a bare JS function and not a jQuery handler - handle = ontype && cur[ ontype ]; - if ( handle && jQuery.acceptData( cur ) && handle.apply( cur, data ) === false ) { - event.preventDefault(); - } - } - event.type = type; - - // If nobody prevented the default action, do it now - if ( !onlyHandlers && !event.isDefaultPrevented() ) { - - if ( (!special._default || special._default.apply( elem.ownerDocument, data ) === false) && - !(type === "click" && jQuery.nodeName( elem, "a" )) && jQuery.acceptData( elem ) ) { - - // Call a native DOM method on the target with the same name name as the event. - // Can't use an .isFunction() check here because IE6/7 fails that test. - // Don't do default actions on window, that's where global variables be (#6170) - // IE<9 dies on focus/blur to hidden element (#1486) - if ( ontype && elem[ type ] && ((type !== "focus" && type !== "blur") || event.target.offsetWidth !== 0) && !jQuery.isWindow( elem ) ) { - - // Don't re-trigger an onFOO event when we call its FOO() method - old = elem[ ontype ]; - - if ( old ) { - elem[ ontype ] = null; - } - - // Prevent re-triggering of the same event, since we already bubbled it above - jQuery.event.triggered = type; - elem[ type ](); - jQuery.event.triggered = undefined; - - if ( old ) { - elem[ ontype ] = old; - } - } - } - } - - return event.result; - }, - - dispatch: function( event ) { - - // Make a writable jQuery.Event from the native event object - event = jQuery.event.fix( event || window.event ); - - var handlers = ( (jQuery._data( this, "events" ) || {} )[ event.type ] || []), - delegateCount = handlers.delegateCount, - args = [].slice.call( arguments, 0 ), - run_all = !event.exclusive && !event.namespace, - special = jQuery.event.special[ event.type ] || {}, - handlerQueue = [], - i, j, cur, jqcur, ret, selMatch, matched, matches, handleObj, sel, related; - - // Use the fix-ed jQuery.Event rather than the (read-only) native event - args[0] = event; - event.delegateTarget = this; - - // Call the preDispatch hook for the mapped type, and let it bail if desired - if ( special.preDispatch && special.preDispatch.call( this, event ) === false ) { - return; - } - - // Determine handlers that should run if there are delegated events - // Avoid non-left-click bubbling in Firefox (#3861) - if ( delegateCount && !(event.button && event.type === "click") ) { - - // Pregenerate a single jQuery object for reuse with .is() - jqcur = jQuery(this); - jqcur.context = this.ownerDocument || this; - - for ( cur = event.target; cur != this; cur = cur.parentNode || this ) { - - // Don't process events on disabled elements (#6911, #8165) - if ( cur.disabled !== true ) { - selMatch = {}; - matches = []; - jqcur[0] = cur; - for ( i = 0; i < delegateCount; i++ ) { - handleObj = handlers[ i ]; - sel = handleObj.selector; - - if ( selMatch[ sel ] === undefined ) { - selMatch[ sel ] = ( - handleObj.quick ? quickIs( cur, handleObj.quick ) : jqcur.is( sel ) - ); - } - if ( selMatch[ sel ] ) { - matches.push( handleObj ); - } - } - if ( matches.length ) { - handlerQueue.push({ elem: cur, matches: matches }); - } - } - } - } - - // Add the remaining (directly-bound) handlers - if ( handlers.length > delegateCount ) { - handlerQueue.push({ elem: this, matches: handlers.slice( delegateCount ) }); - } - - // Run delegates first; they may want to stop propagation beneath us - for ( i = 0; i < handlerQueue.length && !event.isPropagationStopped(); i++ ) { - matched = handlerQueue[ i ]; - event.currentTarget = matched.elem; - - for ( j = 0; j < matched.matches.length && !event.isImmediatePropagationStopped(); j++ ) { - handleObj = matched.matches[ j ]; - - // Triggered event must either 1) be non-exclusive and have no namespace, or - // 2) have namespace(s) a subset or equal to those in the bound event (both can have no namespace). - if ( run_all || (!event.namespace && !handleObj.namespace) || event.namespace_re && event.namespace_re.test( handleObj.namespace ) ) { - - event.data = handleObj.data; - event.handleObj = handleObj; - - ret = ( (jQuery.event.special[ handleObj.origType ] || {}).handle || handleObj.handler ) - .apply( matched.elem, args ); - - if ( ret !== undefined ) { - event.result = ret; - if ( ret === false ) { - event.preventDefault(); - event.stopPropagation(); - } - } - } - } - } - - // Call the postDispatch hook for the mapped type - if ( special.postDispatch ) { - special.postDispatch.call( this, event ); - } - - return event.result; - }, - - // Includes some event props shared by KeyEvent and MouseEvent - // *** attrChange attrName relatedNode srcElement are not normalized, non-W3C, deprecated, will be removed in 1.8 *** - props: "attrChange attrName relatedNode srcElement altKey bubbles cancelable ctrlKey currentTarget eventPhase metaKey relatedTarget shiftKey target timeStamp view which".split(" "), - - fixHooks: {}, - - keyHooks: { - props: "char charCode key keyCode".split(" "), - filter: function( event, original ) { - - // Add which for key events - if ( event.which == null ) { - event.which = original.charCode != null ? original.charCode : original.keyCode; - } - - return event; - } - }, - - mouseHooks: { - props: "button buttons clientX clientY fromElement offsetX offsetY pageX pageY screenX screenY toElement".split(" "), - filter: function( event, original ) { - var eventDoc, doc, body, - button = original.button, - fromElement = original.fromElement; - - // Calculate pageX/Y if missing and clientX/Y available - if ( event.pageX == null && original.clientX != null ) { - eventDoc = event.target.ownerDocument || document; - doc = eventDoc.documentElement; - body = eventDoc.body; - - event.pageX = original.clientX + ( doc && doc.scrollLeft || body && body.scrollLeft || 0 ) - ( doc && doc.clientLeft || body && body.clientLeft || 0 ); - event.pageY = original.clientY + ( doc && doc.scrollTop || body && body.scrollTop || 0 ) - ( doc && doc.clientTop || body && body.clientTop || 0 ); - } - - // Add relatedTarget, if necessary - if ( !event.relatedTarget && fromElement ) { - event.relatedTarget = fromElement === event.target ? original.toElement : fromElement; - } - - // Add which for click: 1 === left; 2 === middle; 3 === right - // Note: button is not normalized, so don't use it - if ( !event.which && button !== undefined ) { - event.which = ( button & 1 ? 1 : ( button & 2 ? 3 : ( button & 4 ? 2 : 0 ) ) ); - } - - return event; - } - }, - - fix: function( event ) { - if ( event[ jQuery.expando ] ) { - return event; - } - - // Create a writable copy of the event object and normalize some properties - var i, prop, - originalEvent = event, - fixHook = jQuery.event.fixHooks[ event.type ] || {}, - copy = fixHook.props ? this.props.concat( fixHook.props ) : this.props; - - event = jQuery.Event( originalEvent ); - - for ( i = copy.length; i; ) { - prop = copy[ --i ]; - event[ prop ] = originalEvent[ prop ]; - } - - // Fix target property, if necessary (#1925, IE 6/7/8 & Safari2) - if ( !event.target ) { - event.target = originalEvent.srcElement || document; - } - - // Target should not be a text node (#504, Safari) - if ( event.target.nodeType === 3 ) { - event.target = event.target.parentNode; - } - - // For mouse/key events; add metaKey if it's not there (#3368, IE6/7/8) - if ( event.metaKey === undefined ) { - event.metaKey = event.ctrlKey; - } - - return fixHook.filter? fixHook.filter( event, originalEvent ) : event; - }, - - special: { - ready: { - // Make sure the ready event is setup - setup: jQuery.bindReady - }, - - load: { - // Prevent triggered image.load events from bubbling to window.load - noBubble: true - }, - - focus: { - delegateType: "focusin" - }, - blur: { - delegateType: "focusout" - }, - - beforeunload: { - setup: function( data, namespaces, eventHandle ) { - // We only want to do this special case on windows - if ( jQuery.isWindow( this ) ) { - this.onbeforeunload = eventHandle; - } - }, - - teardown: function( namespaces, eventHandle ) { - if ( this.onbeforeunload === eventHandle ) { - this.onbeforeunload = null; - } - } - } - }, - - simulate: function( type, elem, event, bubble ) { - // Piggyback on a donor event to simulate a different one. - // Fake originalEvent to avoid donor's stopPropagation, but if the - // simulated event prevents default then we do the same on the donor. - var e = jQuery.extend( - new jQuery.Event(), - event, - { type: type, - isSimulated: true, - originalEvent: {} - } - ); - if ( bubble ) { - jQuery.event.trigger( e, null, elem ); - } else { - jQuery.event.dispatch.call( elem, e ); - } - if ( e.isDefaultPrevented() ) { - event.preventDefault(); - } - } -}; - -// Some plugins are using, but it's undocumented/deprecated and will be removed. -// The 1.7 special event interface should provide all the hooks needed now. -jQuery.event.handle = jQuery.event.dispatch; - -jQuery.removeEvent = document.removeEventListener ? - function( elem, type, handle ) { - if ( elem.removeEventListener ) { - elem.removeEventListener( type, handle, false ); - } - } : - function( elem, type, handle ) { - if ( elem.detachEvent ) { - elem.detachEvent( "on" + type, handle ); - } - }; - -jQuery.Event = function( src, props ) { - // Allow instantiation without the 'new' keyword - if ( !(this instanceof jQuery.Event) ) { - return new jQuery.Event( src, props ); - } - - // Event object - if ( src && src.type ) { - this.originalEvent = src; - this.type = src.type; - - // Events bubbling up the document may have been marked as prevented - // by a handler lower down the tree; reflect the correct value. - this.isDefaultPrevented = ( src.defaultPrevented || src.returnValue === false || - src.getPreventDefault && src.getPreventDefault() ) ? returnTrue : returnFalse; - - // Event type - } else { - this.type = src; - } - - // Put explicitly provided properties onto the event object - if ( props ) { - jQuery.extend( this, props ); - } - - // Create a timestamp if incoming event doesn't have one - this.timeStamp = src && src.timeStamp || jQuery.now(); - - // Mark it as fixed - this[ jQuery.expando ] = true; -}; - -function returnFalse() { - return false; -} -function returnTrue() { - return true; -} - -// jQuery.Event is based on DOM3 Events as specified by the ECMAScript Language Binding -// http://www.w3.org/TR/2003/WD-DOM-Level-3-Events-20030331/ecma-script-binding.html -jQuery.Event.prototype = { - preventDefault: function() { - this.isDefaultPrevented = returnTrue; - - var e = this.originalEvent; - if ( !e ) { - return; - } - - // if preventDefault exists run it on the original event - if ( e.preventDefault ) { - e.preventDefault(); - - // otherwise set the returnValue property of the original event to false (IE) - } else { - e.returnValue = false; - } - }, - stopPropagation: function() { - this.isPropagationStopped = returnTrue; - - var e = this.originalEvent; - if ( !e ) { - return; - } - // if stopPropagation exists run it on the original event - if ( e.stopPropagation ) { - e.stopPropagation(); - } - // otherwise set the cancelBubble property of the original event to true (IE) - e.cancelBubble = true; - }, - stopImmediatePropagation: function() { - this.isImmediatePropagationStopped = returnTrue; - this.stopPropagation(); - }, - isDefaultPrevented: returnFalse, - isPropagationStopped: returnFalse, - isImmediatePropagationStopped: returnFalse -}; - -// Create mouseenter/leave events using mouseover/out and event-time checks -jQuery.each({ - mouseenter: "mouseover", - mouseleave: "mouseout" -}, function( orig, fix ) { - jQuery.event.special[ orig ] = { - delegateType: fix, - bindType: fix, - - handle: function( event ) { - var target = this, - related = event.relatedTarget, - handleObj = event.handleObj, - selector = handleObj.selector, - ret; - - // For mousenter/leave call the handler if related is outside the target. - // NB: No relatedTarget if the mouse left/entered the browser window - if ( !related || (related !== target && !jQuery.contains( target, related )) ) { - event.type = handleObj.origType; - ret = handleObj.handler.apply( this, arguments ); - event.type = fix; - } - return ret; - } - }; -}); - -// IE submit delegation -if ( !jQuery.support.submitBubbles ) { - - jQuery.event.special.submit = { - setup: function() { - // Only need this for delegated form submit events - if ( jQuery.nodeName( this, "form" ) ) { - return false; - } - - // Lazy-add a submit handler when a descendant form may potentially be submitted - jQuery.event.add( this, "click._submit keypress._submit", function( e ) { - // Node name check avoids a VML-related crash in IE (#9807) - var elem = e.target, - form = jQuery.nodeName( elem, "input" ) || jQuery.nodeName( elem, "button" ) ? elem.form : undefined; - if ( form && !form._submit_attached ) { - jQuery.event.add( form, "submit._submit", function( event ) { - event._submit_bubble = true; - }); - form._submit_attached = true; - } - }); - // return undefined since we don't need an event listener - }, - - postDispatch: function( event ) { - // If form was submitted by the user, bubble the event up the tree - if ( event._submit_bubble ) { - delete event._submit_bubble; - if ( this.parentNode && !event.isTrigger ) { - jQuery.event.simulate( "submit", this.parentNode, event, true ); - } - } - }, - - teardown: function() { - // Only need this for delegated form submit events - if ( jQuery.nodeName( this, "form" ) ) { - return false; - } - - // Remove delegated handlers; cleanData eventually reaps submit handlers attached above - jQuery.event.remove( this, "._submit" ); - } - }; -} - -// IE change delegation and checkbox/radio fix -if ( !jQuery.support.changeBubbles ) { - - jQuery.event.special.change = { - - setup: function() { - - if ( rformElems.test( this.nodeName ) ) { - // IE doesn't fire change on a check/radio until blur; trigger it on click - // after a propertychange. Eat the blur-change in special.change.handle. - // This still fires onchange a second time for check/radio after blur. - if ( this.type === "checkbox" || this.type === "radio" ) { - jQuery.event.add( this, "propertychange._change", function( event ) { - if ( event.originalEvent.propertyName === "checked" ) { - this._just_changed = true; - } - }); - jQuery.event.add( this, "click._change", function( event ) { - if ( this._just_changed && !event.isTrigger ) { - this._just_changed = false; - jQuery.event.simulate( "change", this, event, true ); - } - }); - } - return false; - } - // Delegated event; lazy-add a change handler on descendant inputs - jQuery.event.add( this, "beforeactivate._change", function( e ) { - var elem = e.target; - - if ( rformElems.test( elem.nodeName ) && !elem._change_attached ) { - jQuery.event.add( elem, "change._change", function( event ) { - if ( this.parentNode && !event.isSimulated && !event.isTrigger ) { - jQuery.event.simulate( "change", this.parentNode, event, true ); - } - }); - elem._change_attached = true; - } - }); - }, - - handle: function( event ) { - var elem = event.target; - - // Swallow native change events from checkbox/radio, we already triggered them above - if ( this !== elem || event.isSimulated || event.isTrigger || (elem.type !== "radio" && elem.type !== "checkbox") ) { - return event.handleObj.handler.apply( this, arguments ); - } - }, - - teardown: function() { - jQuery.event.remove( this, "._change" ); - - return rformElems.test( this.nodeName ); - } - }; -} - -// Create "bubbling" focus and blur events -if ( !jQuery.support.focusinBubbles ) { - jQuery.each({ focus: "focusin", blur: "focusout" }, function( orig, fix ) { - - // Attach a single capturing handler while someone wants focusin/focusout - var attaches = 0, - handler = function( event ) { - jQuery.event.simulate( fix, event.target, jQuery.event.fix( event ), true ); - }; - - jQuery.event.special[ fix ] = { - setup: function() { - if ( attaches++ === 0 ) { - document.addEventListener( orig, handler, true ); - } - }, - teardown: function() { - if ( --attaches === 0 ) { - document.removeEventListener( orig, handler, true ); - } - } - }; - }); -} - -jQuery.fn.extend({ - - on: function( types, selector, data, fn, /*INTERNAL*/ one ) { - var origFn, type; - - // Types can be a map of types/handlers - if ( typeof types === "object" ) { - // ( types-Object, selector, data ) - if ( typeof selector !== "string" ) { // && selector != null - // ( types-Object, data ) - data = data || selector; - selector = undefined; - } - for ( type in types ) { - this.on( type, selector, data, types[ type ], one ); - } - return this; - } - - if ( data == null && fn == null ) { - // ( types, fn ) - fn = selector; - data = selector = undefined; - } else if ( fn == null ) { - if ( typeof selector === "string" ) { - // ( types, selector, fn ) - fn = data; - data = undefined; - } else { - // ( types, data, fn ) - fn = data; - data = selector; - selector = undefined; - } - } - if ( fn === false ) { - fn = returnFalse; - } else if ( !fn ) { - return this; - } - - if ( one === 1 ) { - origFn = fn; - fn = function( event ) { - // Can use an empty set, since event contains the info - jQuery().off( event ); - return origFn.apply( this, arguments ); - }; - // Use same guid so caller can remove using origFn - fn.guid = origFn.guid || ( origFn.guid = jQuery.guid++ ); - } - return this.each( function() { - jQuery.event.add( this, types, fn, data, selector ); - }); - }, - one: function( types, selector, data, fn ) { - return this.on( types, selector, data, fn, 1 ); - }, - off: function( types, selector, fn ) { - if ( types && types.preventDefault && types.handleObj ) { - // ( event ) dispatched jQuery.Event - var handleObj = types.handleObj; - jQuery( types.delegateTarget ).off( - handleObj.namespace ? handleObj.origType + "." + handleObj.namespace : handleObj.origType, - handleObj.selector, - handleObj.handler - ); - return this; - } - if ( typeof types === "object" ) { - // ( types-object [, selector] ) - for ( var type in types ) { - this.off( type, selector, types[ type ] ); - } - return this; - } - if ( selector === false || typeof selector === "function" ) { - // ( types [, fn] ) - fn = selector; - selector = undefined; - } - if ( fn === false ) { - fn = returnFalse; - } - return this.each(function() { - jQuery.event.remove( this, types, fn, selector ); - }); - }, - - bind: function( types, data, fn ) { - return this.on( types, null, data, fn ); - }, - unbind: function( types, fn ) { - return this.off( types, null, fn ); - }, - - live: function( types, data, fn ) { - jQuery( this.context ).on( types, this.selector, data, fn ); - return this; - }, - die: function( types, fn ) { - jQuery( this.context ).off( types, this.selector || "**", fn ); - return this; - }, - - delegate: function( selector, types, data, fn ) { - return this.on( types, selector, data, fn ); - }, - undelegate: function( selector, types, fn ) { - // ( namespace ) or ( selector, types [, fn] ) - return arguments.length == 1? this.off( selector, "**" ) : this.off( types, selector, fn ); - }, - - trigger: function( type, data ) { - return this.each(function() { - jQuery.event.trigger( type, data, this ); - }); - }, - triggerHandler: function( type, data ) { - if ( this[0] ) { - return jQuery.event.trigger( type, data, this[0], true ); - } - }, - - toggle: function( fn ) { - // Save reference to arguments for access in closure - var args = arguments, - guid = fn.guid || jQuery.guid++, - i = 0, - toggler = function( event ) { - // Figure out which function to execute - var lastToggle = ( jQuery._data( this, "lastToggle" + fn.guid ) || 0 ) % i; - jQuery._data( this, "lastToggle" + fn.guid, lastToggle + 1 ); - - // Make sure that clicks stop - event.preventDefault(); - - // and execute the function - return args[ lastToggle ].apply( this, arguments ) || false; - }; - - // link all the functions, so any of them can unbind this click handler - toggler.guid = guid; - while ( i < args.length ) { - args[ i++ ].guid = guid; - } - - return this.click( toggler ); - }, - - hover: function( fnOver, fnOut ) { - return this.mouseenter( fnOver ).mouseleave( fnOut || fnOver ); - } -}); - -jQuery.each( ("blur focus focusin focusout load resize scroll unload click dblclick " + - "mousedown mouseup mousemove mouseover mouseout mouseenter mouseleave " + - "change select submit keydown keypress keyup error contextmenu").split(" "), function( i, name ) { - - // Handle event binding - jQuery.fn[ name ] = function( data, fn ) { - if ( fn == null ) { - fn = data; - data = null; - } - - return arguments.length > 0 ? - this.on( name, null, data, fn ) : - this.trigger( name ); - }; - - if ( jQuery.attrFn ) { - jQuery.attrFn[ name ] = true; - } - - if ( rkeyEvent.test( name ) ) { - jQuery.event.fixHooks[ name ] = jQuery.event.keyHooks; - } - - if ( rmouseEvent.test( name ) ) { - jQuery.event.fixHooks[ name ] = jQuery.event.mouseHooks; - } -}); - - - -/*! - * Sizzle CSS Selector Engine - * Copyright 2011, The Dojo Foundation - * Released under the MIT, BSD, and GPL Licenses. - * More information: http://sizzlejs.com/ - */ -(function(){ - -var chunker = /((?:\((?:\([^()]+\)|[^()]+)+\)|\[(?:\[[^\[\]]*\]|['"][^'"]*['"]|[^\[\]'"]+)+\]|\\.|[^ >+~,(\[\\]+)+|[>+~])(\s*,\s*)?((?:.|\r|\n)*)/g, - expando = "sizcache" + (Math.random() + '').replace('.', ''), - done = 0, - toString = Object.prototype.toString, - hasDuplicate = false, - baseHasDuplicate = true, - rBackslash = /\\/g, - rReturn = /\r\n/g, - rNonWord = /\W/; - -// Here we check if the JavaScript engine is using some sort of -// optimization where it does not always call our comparision -// function. If that is the case, discard the hasDuplicate value. -// Thus far that includes Google Chrome. -[0, 0].sort(function() { - baseHasDuplicate = false; - return 0; -}); - -var Sizzle = function( selector, context, results, seed ) { - results = results || []; - context = context || document; - - var origContext = context; - - if ( context.nodeType !== 1 && context.nodeType !== 9 ) { - return []; - } - - if ( !selector || typeof selector !== "string" ) { - return results; - } - - var m, set, checkSet, extra, ret, cur, pop, i, - prune = true, - contextXML = Sizzle.isXML( context ), - parts = [], - soFar = selector; - - // Reset the position of the chunker regexp (start from head) - do { - chunker.exec( "" ); - m = chunker.exec( soFar ); - - if ( m ) { - soFar = m[3]; - - parts.push( m[1] ); - - if ( m[2] ) { - extra = m[3]; - break; - } - } - } while ( m ); - - if ( parts.length > 1 && origPOS.exec( selector ) ) { - - if ( parts.length === 2 && Expr.relative[ parts[0] ] ) { - set = posProcess( parts[0] + parts[1], context, seed ); - - } else { - set = Expr.relative[ parts[0] ] ? - [ context ] : - Sizzle( parts.shift(), context ); - - while ( parts.length ) { - selector = parts.shift(); - - if ( Expr.relative[ selector ] ) { - selector += parts.shift(); - } - - set = posProcess( selector, set, seed ); - } - } - - } else { - // Take a shortcut and set the context if the root selector is an ID - // (but not if it'll be faster if the inner selector is an ID) - if ( !seed && parts.length > 1 && context.nodeType === 9 && !contextXML && - Expr.match.ID.test(parts[0]) && !Expr.match.ID.test(parts[parts.length - 1]) ) { - - ret = Sizzle.find( parts.shift(), context, contextXML ); - context = ret.expr ? - Sizzle.filter( ret.expr, ret.set )[0] : - ret.set[0]; - } - - if ( context ) { - ret = seed ? - { expr: parts.pop(), set: makeArray(seed) } : - Sizzle.find( parts.pop(), parts.length === 1 && (parts[0] === "~" || parts[0] === "+") && context.parentNode ? context.parentNode : context, contextXML ); - - set = ret.expr ? - Sizzle.filter( ret.expr, ret.set ) : - ret.set; - - if ( parts.length > 0 ) { - checkSet = makeArray( set ); - - } else { - prune = false; - } - - while ( parts.length ) { - cur = parts.pop(); - pop = cur; - - if ( !Expr.relative[ cur ] ) { - cur = ""; - } else { - pop = parts.pop(); - } - - if ( pop == null ) { - pop = context; - } - - Expr.relative[ cur ]( checkSet, pop, contextXML ); - } - - } else { - checkSet = parts = []; - } - } - - if ( !checkSet ) { - checkSet = set; - } - - if ( !checkSet ) { - Sizzle.error( cur || selector ); - } - - if ( toString.call(checkSet) === "[object Array]" ) { - if ( !prune ) { - results.push.apply( results, checkSet ); - - } else if ( context && context.nodeType === 1 ) { - for ( i = 0; checkSet[i] != null; i++ ) { - if ( checkSet[i] && (checkSet[i] === true || checkSet[i].nodeType === 1 && Sizzle.contains(context, checkSet[i])) ) { - results.push( set[i] ); - } - } - - } else { - for ( i = 0; checkSet[i] != null; i++ ) { - if ( checkSet[i] && checkSet[i].nodeType === 1 ) { - results.push( set[i] ); - } - } - } - - } else { - makeArray( checkSet, results ); - } - - if ( extra ) { - Sizzle( extra, origContext, results, seed ); - Sizzle.uniqueSort( results ); - } - - return results; -}; - -Sizzle.uniqueSort = function( results ) { - if ( sortOrder ) { - hasDuplicate = baseHasDuplicate; - results.sort( sortOrder ); - - if ( hasDuplicate ) { - for ( var i = 1; i < results.length; i++ ) { - if ( results[i] === results[ i - 1 ] ) { - results.splice( i--, 1 ); - } - } - } - } - - return results; -}; - -Sizzle.matches = function( expr, set ) { - return Sizzle( expr, null, null, set ); -}; - -Sizzle.matchesSelector = function( node, expr ) { - return Sizzle( expr, null, null, [node] ).length > 0; -}; - -Sizzle.find = function( expr, context, isXML ) { - var set, i, len, match, type, left; - - if ( !expr ) { - return []; - } - - for ( i = 0, len = Expr.order.length; i < len; i++ ) { - type = Expr.order[i]; - - if ( (match = Expr.leftMatch[ type ].exec( expr )) ) { - left = match[1]; - match.splice( 1, 1 ); - - if ( left.substr( left.length - 1 ) !== "\\" ) { - match[1] = (match[1] || "").replace( rBackslash, "" ); - set = Expr.find[ type ]( match, context, isXML ); - - if ( set != null ) { - expr = expr.replace( Expr.match[ type ], "" ); - break; - } - } - } - } - - if ( !set ) { - set = typeof context.getElementsByTagName !== "undefined" ? - context.getElementsByTagName( "*" ) : - []; - } - - return { set: set, expr: expr }; -}; - -Sizzle.filter = function( expr, set, inplace, not ) { - var match, anyFound, - type, found, item, filter, left, - i, pass, - old = expr, - result = [], - curLoop = set, - isXMLFilter = set && set[0] && Sizzle.isXML( set[0] ); - - while ( expr && set.length ) { - for ( type in Expr.filter ) { - if ( (match = Expr.leftMatch[ type ].exec( expr )) != null && match[2] ) { - filter = Expr.filter[ type ]; - left = match[1]; - - anyFound = false; - - match.splice(1,1); - - if ( left.substr( left.length - 1 ) === "\\" ) { - continue; - } - - if ( curLoop === result ) { - result = []; - } - - if ( Expr.preFilter[ type ] ) { - match = Expr.preFilter[ type ]( match, curLoop, inplace, result, not, isXMLFilter ); - - if ( !match ) { - anyFound = found = true; - - } else if ( match === true ) { - continue; - } - } - - if ( match ) { - for ( i = 0; (item = curLoop[i]) != null; i++ ) { - if ( item ) { - found = filter( item, match, i, curLoop ); - pass = not ^ found; - - if ( inplace && found != null ) { - if ( pass ) { - anyFound = true; - - } else { - curLoop[i] = false; - } - - } else if ( pass ) { - result.push( item ); - anyFound = true; - } - } - } - } - - if ( found !== undefined ) { - if ( !inplace ) { - curLoop = result; - } - - expr = expr.replace( Expr.match[ type ], "" ); - - if ( !anyFound ) { - return []; - } - - break; - } - } - } - - // Improper expression - if ( expr === old ) { - if ( anyFound == null ) { - Sizzle.error( expr ); - - } else { - break; - } - } - - old = expr; - } - - return curLoop; -}; - -Sizzle.error = function( msg ) { - throw new Error( "Syntax error, unrecognized expression: " + msg ); -}; - -/** - * Utility function for retreiving the text value of an array of DOM nodes - * @param {Array|Element} elem - */ -var getText = Sizzle.getText = function( elem ) { - var i, node, - nodeType = elem.nodeType, - ret = ""; - - if ( nodeType ) { - if ( nodeType === 1 || nodeType === 9 || nodeType === 11 ) { - // Use textContent || innerText for elements - if ( typeof elem.textContent === 'string' ) { - return elem.textContent; - } else if ( typeof elem.innerText === 'string' ) { - // Replace IE's carriage returns - return elem.innerText.replace( rReturn, '' ); - } else { - // Traverse it's children - for ( elem = elem.firstChild; elem; elem = elem.nextSibling) { - ret += getText( elem ); - } - } - } else if ( nodeType === 3 || nodeType === 4 ) { - return elem.nodeValue; - } - } else { - - // If no nodeType, this is expected to be an array - for ( i = 0; (node = elem[i]); i++ ) { - // Do not traverse comment nodes - if ( node.nodeType !== 8 ) { - ret += getText( node ); - } - } - } - return ret; -}; - -var Expr = Sizzle.selectors = { - order: [ "ID", "NAME", "TAG" ], - - match: { - ID: /#((?:[\w\u00c0-\uFFFF\-]|\\.)+)/, - CLASS: /\.((?:[\w\u00c0-\uFFFF\-]|\\.)+)/, - NAME: /\[name=['"]*((?:[\w\u00c0-\uFFFF\-]|\\.)+)['"]*\]/, - ATTR: /\[\s*((?:[\w\u00c0-\uFFFF\-]|\\.)+)\s*(?:(\S?=)\s*(?:(['"])(.*?)\3|(#?(?:[\w\u00c0-\uFFFF\-]|\\.)*)|)|)\s*\]/, - TAG: /^((?:[\w\u00c0-\uFFFF\*\-]|\\.)+)/, - CHILD: /:(only|nth|last|first)-child(?:\(\s*(even|odd|(?:[+\-]?\d+|(?:[+\-]?\d*)?n\s*(?:[+\-]\s*\d+)?))\s*\))?/, - POS: /:(nth|eq|gt|lt|first|last|even|odd)(?:\((\d*)\))?(?=[^\-]|$)/, - PSEUDO: /:((?:[\w\u00c0-\uFFFF\-]|\\.)+)(?:\((['"]?)((?:\([^\)]+\)|[^\(\)]*)+)\2\))?/ - }, - - leftMatch: {}, - - attrMap: { - "class": "className", - "for": "htmlFor" - }, - - attrHandle: { - href: function( elem ) { - return elem.getAttribute( "href" ); - }, - type: function( elem ) { - return elem.getAttribute( "type" ); - } - }, - - relative: { - "+": function(checkSet, part){ - var isPartStr = typeof part === "string", - isTag = isPartStr && !rNonWord.test( part ), - isPartStrNotTag = isPartStr && !isTag; - - if ( isTag ) { - part = part.toLowerCase(); - } - - for ( var i = 0, l = checkSet.length, elem; i < l; i++ ) { - if ( (elem = checkSet[i]) ) { - while ( (elem = elem.previousSibling) && elem.nodeType !== 1 ) {} - - checkSet[i] = isPartStrNotTag || elem && elem.nodeName.toLowerCase() === part ? - elem || false : - elem === part; - } - } - - if ( isPartStrNotTag ) { - Sizzle.filter( part, checkSet, true ); - } - }, - - ">": function( checkSet, part ) { - var elem, - isPartStr = typeof part === "string", - i = 0, - l = checkSet.length; - - if ( isPartStr && !rNonWord.test( part ) ) { - part = part.toLowerCase(); - - for ( ; i < l; i++ ) { - elem = checkSet[i]; - - if ( elem ) { - var parent = elem.parentNode; - checkSet[i] = parent.nodeName.toLowerCase() === part ? parent : false; - } - } - - } else { - for ( ; i < l; i++ ) { - elem = checkSet[i]; - - if ( elem ) { - checkSet[i] = isPartStr ? - elem.parentNode : - elem.parentNode === part; - } - } - - if ( isPartStr ) { - Sizzle.filter( part, checkSet, true ); - } - } - }, - - "": function(checkSet, part, isXML){ - var nodeCheck, - doneName = done++, - checkFn = dirCheck; - - if ( typeof part === "string" && !rNonWord.test( part ) ) { - part = part.toLowerCase(); - nodeCheck = part; - checkFn = dirNodeCheck; - } - - checkFn( "parentNode", part, doneName, checkSet, nodeCheck, isXML ); - }, - - "~": function( checkSet, part, isXML ) { - var nodeCheck, - doneName = done++, - checkFn = dirCheck; - - if ( typeof part === "string" && !rNonWord.test( part ) ) { - part = part.toLowerCase(); - nodeCheck = part; - checkFn = dirNodeCheck; - } - - checkFn( "previousSibling", part, doneName, checkSet, nodeCheck, isXML ); - } - }, - - find: { - ID: function( match, context, isXML ) { - if ( typeof context.getElementById !== "undefined" && !isXML ) { - var m = context.getElementById(match[1]); - // Check parentNode to catch when Blackberry 4.6 returns - // nodes that are no longer in the document #6963 - return m && m.parentNode ? [m] : []; - } - }, - - NAME: function( match, context ) { - if ( typeof context.getElementsByName !== "undefined" ) { - var ret = [], - results = context.getElementsByName( match[1] ); - - for ( var i = 0, l = results.length; i < l; i++ ) { - if ( results[i].getAttribute("name") === match[1] ) { - ret.push( results[i] ); - } - } - - return ret.length === 0 ? null : ret; - } - }, - - TAG: function( match, context ) { - if ( typeof context.getElementsByTagName !== "undefined" ) { - return context.getElementsByTagName( match[1] ); - } - } - }, - preFilter: { - CLASS: function( match, curLoop, inplace, result, not, isXML ) { - match = " " + match[1].replace( rBackslash, "" ) + " "; - - if ( isXML ) { - return match; - } - - for ( var i = 0, elem; (elem = curLoop[i]) != null; i++ ) { - if ( elem ) { - if ( not ^ (elem.className && (" " + elem.className + " ").replace(/[\t\n\r]/g, " ").indexOf(match) >= 0) ) { - if ( !inplace ) { - result.push( elem ); - } - - } else if ( inplace ) { - curLoop[i] = false; - } - } - } - - return false; - }, - - ID: function( match ) { - return match[1].replace( rBackslash, "" ); - }, - - TAG: function( match, curLoop ) { - return match[1].replace( rBackslash, "" ).toLowerCase(); - }, - - CHILD: function( match ) { - if ( match[1] === "nth" ) { - if ( !match[2] ) { - Sizzle.error( match[0] ); - } - - match[2] = match[2].replace(/^\+|\s*/g, ''); - - // parse equations like 'even', 'odd', '5', '2n', '3n+2', '4n-1', '-n+6' - var test = /(-?)(\d*)(?:n([+\-]?\d*))?/.exec( - match[2] === "even" && "2n" || match[2] === "odd" && "2n+1" || - !/\D/.test( match[2] ) && "0n+" + match[2] || match[2]); - - // calculate the numbers (first)n+(last) including if they are negative - match[2] = (test[1] + (test[2] || 1)) - 0; - match[3] = test[3] - 0; - } - else if ( match[2] ) { - Sizzle.error( match[0] ); - } - - // TODO: Move to normal caching system - match[0] = done++; - - return match; - }, - - ATTR: function( match, curLoop, inplace, result, not, isXML ) { - var name = match[1] = match[1].replace( rBackslash, "" ); - - if ( !isXML && Expr.attrMap[name] ) { - match[1] = Expr.attrMap[name]; - } - - // Handle if an un-quoted value was used - match[4] = ( match[4] || match[5] || "" ).replace( rBackslash, "" ); - - if ( match[2] === "~=" ) { - match[4] = " " + match[4] + " "; - } - - return match; - }, - - PSEUDO: function( match, curLoop, inplace, result, not ) { - if ( match[1] === "not" ) { - // If we're dealing with a complex expression, or a simple one - if ( ( chunker.exec(match[3]) || "" ).length > 1 || /^\w/.test(match[3]) ) { - match[3] = Sizzle(match[3], null, null, curLoop); - - } else { - var ret = Sizzle.filter(match[3], curLoop, inplace, true ^ not); - - if ( !inplace ) { - result.push.apply( result, ret ); - } - - return false; - } - - } else if ( Expr.match.POS.test( match[0] ) || Expr.match.CHILD.test( match[0] ) ) { - return true; - } - - return match; - }, - - POS: function( match ) { - match.unshift( true ); - - return match; - } - }, - - filters: { - enabled: function( elem ) { - return elem.disabled === false && elem.type !== "hidden"; - }, - - disabled: function( elem ) { - return elem.disabled === true; - }, - - checked: function( elem ) { - return elem.checked === true; - }, - - selected: function( elem ) { - // Accessing this property makes selected-by-default - // options in Safari work properly - if ( elem.parentNode ) { - elem.parentNode.selectedIndex; - } - - return elem.selected === true; - }, - - parent: function( elem ) { - return !!elem.firstChild; - }, - - empty: function( elem ) { - return !elem.firstChild; - }, - - has: function( elem, i, match ) { - return !!Sizzle( match[3], elem ).length; - }, - - header: function( elem ) { - return (/h\d/i).test( elem.nodeName ); - }, - - text: function( elem ) { - var attr = elem.getAttribute( "type" ), type = elem.type; - // IE6 and 7 will map elem.type to 'text' for new HTML5 types (search, etc) - // use getAttribute instead to test this case - return elem.nodeName.toLowerCase() === "input" && "text" === type && ( attr === type || attr === null ); - }, - - radio: function( elem ) { - return elem.nodeName.toLowerCase() === "input" && "radio" === elem.type; - }, - - checkbox: function( elem ) { - return elem.nodeName.toLowerCase() === "input" && "checkbox" === elem.type; - }, - - file: function( elem ) { - return elem.nodeName.toLowerCase() === "input" && "file" === elem.type; - }, - - password: function( elem ) { - return elem.nodeName.toLowerCase() === "input" && "password" === elem.type; - }, - - submit: function( elem ) { - var name = elem.nodeName.toLowerCase(); - return (name === "input" || name === "button") && "submit" === elem.type; - }, - - image: function( elem ) { - return elem.nodeName.toLowerCase() === "input" && "image" === elem.type; - }, - - reset: function( elem ) { - var name = elem.nodeName.toLowerCase(); - return (name === "input" || name === "button") && "reset" === elem.type; - }, - - button: function( elem ) { - var name = elem.nodeName.toLowerCase(); - return name === "input" && "button" === elem.type || name === "button"; - }, - - input: function( elem ) { - return (/input|select|textarea|button/i).test( elem.nodeName ); - }, - - focus: function( elem ) { - return elem === elem.ownerDocument.activeElement; - } - }, - setFilters: { - first: function( elem, i ) { - return i === 0; - }, - - last: function( elem, i, match, array ) { - return i === array.length - 1; - }, - - even: function( elem, i ) { - return i % 2 === 0; - }, - - odd: function( elem, i ) { - return i % 2 === 1; - }, - - lt: function( elem, i, match ) { - return i < match[3] - 0; - }, - - gt: function( elem, i, match ) { - return i > match[3] - 0; - }, - - nth: function( elem, i, match ) { - return match[3] - 0 === i; - }, - - eq: function( elem, i, match ) { - return match[3] - 0 === i; - } - }, - filter: { - PSEUDO: function( elem, match, i, array ) { - var name = match[1], - filter = Expr.filters[ name ]; - - if ( filter ) { - return filter( elem, i, match, array ); - - } else if ( name === "contains" ) { - return (elem.textContent || elem.innerText || getText([ elem ]) || "").indexOf(match[3]) >= 0; - - } else if ( name === "not" ) { - var not = match[3]; - - for ( var j = 0, l = not.length; j < l; j++ ) { - if ( not[j] === elem ) { - return false; - } - } - - return true; - - } else { - Sizzle.error( name ); - } - }, - - CHILD: function( elem, match ) { - var first, last, - doneName, parent, cache, - count, diff, - type = match[1], - node = elem; - - switch ( type ) { - case "only": - case "first": - while ( (node = node.previousSibling) ) { - if ( node.nodeType === 1 ) { - return false; - } - } - - if ( type === "first" ) { - return true; - } - - node = elem; - - /* falls through */ - case "last": - while ( (node = node.nextSibling) ) { - if ( node.nodeType === 1 ) { - return false; - } - } - - return true; - - case "nth": - first = match[2]; - last = match[3]; - - if ( first === 1 && last === 0 ) { - return true; - } - - doneName = match[0]; - parent = elem.parentNode; - - if ( parent && (parent[ expando ] !== doneName || !elem.nodeIndex) ) { - count = 0; - - for ( node = parent.firstChild; node; node = node.nextSibling ) { - if ( node.nodeType === 1 ) { - node.nodeIndex = ++count; - } - } - - parent[ expando ] = doneName; - } - - diff = elem.nodeIndex - last; - - if ( first === 0 ) { - return diff === 0; - - } else { - return ( diff % first === 0 && diff / first >= 0 ); - } - } - }, - - ID: function( elem, match ) { - return elem.nodeType === 1 && elem.getAttribute("id") === match; - }, - - TAG: function( elem, match ) { - return (match === "*" && elem.nodeType === 1) || !!elem.nodeName && elem.nodeName.toLowerCase() === match; - }, - - CLASS: function( elem, match ) { - return (" " + (elem.className || elem.getAttribute("class")) + " ") - .indexOf( match ) > -1; - }, - - ATTR: function( elem, match ) { - var name = match[1], - result = Sizzle.attr ? - Sizzle.attr( elem, name ) : - Expr.attrHandle[ name ] ? - Expr.attrHandle[ name ]( elem ) : - elem[ name ] != null ? - elem[ name ] : - elem.getAttribute( name ), - value = result + "", - type = match[2], - check = match[4]; - - return result == null ? - type === "!=" : - !type && Sizzle.attr ? - result != null : - type === "=" ? - value === check : - type === "*=" ? - value.indexOf(check) >= 0 : - type === "~=" ? - (" " + value + " ").indexOf(check) >= 0 : - !check ? - value && result !== false : - type === "!=" ? - value !== check : - type === "^=" ? - value.indexOf(check) === 0 : - type === "$=" ? - value.substr(value.length - check.length) === check : - type === "|=" ? - value === check || value.substr(0, check.length + 1) === check + "-" : - false; - }, - - POS: function( elem, match, i, array ) { - var name = match[2], - filter = Expr.setFilters[ name ]; - - if ( filter ) { - return filter( elem, i, match, array ); - } - } - } -}; - -var origPOS = Expr.match.POS, - fescape = function(all, num){ - return "\\" + (num - 0 + 1); - }; - -for ( var type in Expr.match ) { - Expr.match[ type ] = new RegExp( Expr.match[ type ].source + (/(?![^\[]*\])(?![^\(]*\))/.source) ); - Expr.leftMatch[ type ] = new RegExp( /(^(?:.|\r|\n)*?)/.source + Expr.match[ type ].source.replace(/\\(\d+)/g, fescape) ); -} -// Expose origPOS -// "global" as in regardless of relation to brackets/parens -Expr.match.globalPOS = origPOS; - -var makeArray = function( array, results ) { - array = Array.prototype.slice.call( array, 0 ); - - if ( results ) { - results.push.apply( results, array ); - return results; - } - - return array; -}; - -// Perform a simple check to determine if the browser is capable of -// converting a NodeList to an array using builtin methods. -// Also verifies that the returned array holds DOM nodes -// (which is not the case in the Blackberry browser) -try { - Array.prototype.slice.call( document.documentElement.childNodes, 0 )[0].nodeType; - -// Provide a fallback method if it does not work -} catch( e ) { - makeArray = function( array, results ) { - var i = 0, - ret = results || []; - - if ( toString.call(array) === "[object Array]" ) { - Array.prototype.push.apply( ret, array ); - - } else { - if ( typeof array.length === "number" ) { - for ( var l = array.length; i < l; i++ ) { - ret.push( array[i] ); - } - - } else { - for ( ; array[i]; i++ ) { - ret.push( array[i] ); - } - } - } - - return ret; - }; -} - -var sortOrder, siblingCheck; - -if ( document.documentElement.compareDocumentPosition ) { - sortOrder = function( a, b ) { - if ( a === b ) { - hasDuplicate = true; - return 0; - } - - if ( !a.compareDocumentPosition || !b.compareDocumentPosition ) { - return a.compareDocumentPosition ? -1 : 1; - } - - return a.compareDocumentPosition(b) & 4 ? -1 : 1; - }; - -} else { - sortOrder = function( a, b ) { - // The nodes are identical, we can exit early - if ( a === b ) { - hasDuplicate = true; - return 0; - - // Fallback to using sourceIndex (in IE) if it's available on both nodes - } else if ( a.sourceIndex && b.sourceIndex ) { - return a.sourceIndex - b.sourceIndex; - } - - var al, bl, - ap = [], - bp = [], - aup = a.parentNode, - bup = b.parentNode, - cur = aup; - - // If the nodes are siblings (or identical) we can do a quick check - if ( aup === bup ) { - return siblingCheck( a, b ); - - // If no parents were found then the nodes are disconnected - } else if ( !aup ) { - return -1; - - } else if ( !bup ) { - return 1; - } - - // Otherwise they're somewhere else in the tree so we need - // to build up a full list of the parentNodes for comparison - while ( cur ) { - ap.unshift( cur ); - cur = cur.parentNode; - } - - cur = bup; - - while ( cur ) { - bp.unshift( cur ); - cur = cur.parentNode; - } - - al = ap.length; - bl = bp.length; - - // Start walking down the tree looking for a discrepancy - for ( var i = 0; i < al && i < bl; i++ ) { - if ( ap[i] !== bp[i] ) { - return siblingCheck( ap[i], bp[i] ); - } - } - - // We ended someplace up the tree so do a sibling check - return i === al ? - siblingCheck( a, bp[i], -1 ) : - siblingCheck( ap[i], b, 1 ); - }; - - siblingCheck = function( a, b, ret ) { - if ( a === b ) { - return ret; - } - - var cur = a.nextSibling; - - while ( cur ) { - if ( cur === b ) { - return -1; - } - - cur = cur.nextSibling; - } - - return 1; - }; -} - -// Check to see if the browser returns elements by name when -// querying by getElementById (and provide a workaround) -(function(){ - // We're going to inject a fake input element with a specified name - var form = document.createElement("div"), - id = "script" + (new Date()).getTime(), - root = document.documentElement; - - form.innerHTML = ""; - - // Inject it into the root element, check its status, and remove it quickly - root.insertBefore( form, root.firstChild ); - - // The workaround has to do additional checks after a getElementById - // Which slows things down for other browsers (hence the branching) - if ( document.getElementById( id ) ) { - Expr.find.ID = function( match, context, isXML ) { - if ( typeof context.getElementById !== "undefined" && !isXML ) { - var m = context.getElementById(match[1]); - - return m ? - m.id === match[1] || typeof m.getAttributeNode !== "undefined" && m.getAttributeNode("id").nodeValue === match[1] ? - [m] : - undefined : - []; - } - }; - - Expr.filter.ID = function( elem, match ) { - var node = typeof elem.getAttributeNode !== "undefined" && elem.getAttributeNode("id"); - - return elem.nodeType === 1 && node && node.nodeValue === match; - }; - } - - root.removeChild( form ); - - // release memory in IE - root = form = null; -})(); - -(function(){ - // Check to see if the browser returns only elements - // when doing getElementsByTagName("*") - - // Create a fake element - var div = document.createElement("div"); - div.appendChild( document.createComment("") ); - - // Make sure no comments are found - if ( div.getElementsByTagName("*").length > 0 ) { - Expr.find.TAG = function( match, context ) { - var results = context.getElementsByTagName( match[1] ); - - // Filter out possible comments - if ( match[1] === "*" ) { - var tmp = []; - - for ( var i = 0; results[i]; i++ ) { - if ( results[i].nodeType === 1 ) { - tmp.push( results[i] ); - } - } - - results = tmp; - } - - return results; - }; - } - - // Check to see if an attribute returns normalized href attributes - div.innerHTML = ""; - - if ( div.firstChild && typeof div.firstChild.getAttribute !== "undefined" && - div.firstChild.getAttribute("href") !== "#" ) { - - Expr.attrHandle.href = function( elem ) { - return elem.getAttribute( "href", 2 ); - }; - } - - // release memory in IE - div = null; -})(); - -if ( document.querySelectorAll ) { - (function(){ - var oldSizzle = Sizzle, - div = document.createElement("div"), - id = "__sizzle__"; - - div.innerHTML = "

"; - - // Safari can't handle uppercase or unicode characters when - // in quirks mode. - if ( div.querySelectorAll && div.querySelectorAll(".TEST").length === 0 ) { - return; - } - - Sizzle = function( query, context, extra, seed ) { - context = context || document; - - // Only use querySelectorAll on non-XML documents - // (ID selectors don't work in non-HTML documents) - if ( !seed && !Sizzle.isXML(context) ) { - // See if we find a selector to speed up - var match = /^(\w+$)|^\.([\w\-]+$)|^#([\w\-]+$)/.exec( query ); - - if ( match && (context.nodeType === 1 || context.nodeType === 9) ) { - // Speed-up: Sizzle("TAG") - if ( match[1] ) { - return makeArray( context.getElementsByTagName( query ), extra ); - - // Speed-up: Sizzle(".CLASS") - } else if ( match[2] && Expr.find.CLASS && context.getElementsByClassName ) { - return makeArray( context.getElementsByClassName( match[2] ), extra ); - } - } - - if ( context.nodeType === 9 ) { - // Speed-up: Sizzle("body") - // The body element only exists once, optimize finding it - if ( query === "body" && context.body ) { - return makeArray( [ context.body ], extra ); - - // Speed-up: Sizzle("#ID") - } else if ( match && match[3] ) { - var elem = context.getElementById( match[3] ); - - // Check parentNode to catch when Blackberry 4.6 returns - // nodes that are no longer in the document #6963 - if ( elem && elem.parentNode ) { - // Handle the case where IE and Opera return items - // by name instead of ID - if ( elem.id === match[3] ) { - return makeArray( [ elem ], extra ); - } - - } else { - return makeArray( [], extra ); - } - } - - try { - return makeArray( context.querySelectorAll(query), extra ); - } catch(qsaError) {} - - // qSA works strangely on Element-rooted queries - // We can work around this by specifying an extra ID on the root - // and working up from there (Thanks to Andrew Dupont for the technique) - // IE 8 doesn't work on object elements - } else if ( context.nodeType === 1 && context.nodeName.toLowerCase() !== "object" ) { - var oldContext = context, - old = context.getAttribute( "id" ), - nid = old || id, - hasParent = context.parentNode, - relativeHierarchySelector = /^\s*[+~]/.test( query ); - - if ( !old ) { - context.setAttribute( "id", nid ); - } else { - nid = nid.replace( /'/g, "\\$&" ); - } - if ( relativeHierarchySelector && hasParent ) { - context = context.parentNode; - } - - try { - if ( !relativeHierarchySelector || hasParent ) { - return makeArray( context.querySelectorAll( "[id='" + nid + "'] " + query ), extra ); - } - - } catch(pseudoError) { - } finally { - if ( !old ) { - oldContext.removeAttribute( "id" ); - } - } - } - } - - return oldSizzle(query, context, extra, seed); - }; - - for ( var prop in oldSizzle ) { - Sizzle[ prop ] = oldSizzle[ prop ]; - } - - // release memory in IE - div = null; - })(); -} - -(function(){ - var html = document.documentElement, - matches = html.matchesSelector || html.mozMatchesSelector || html.webkitMatchesSelector || html.msMatchesSelector; - - if ( matches ) { - // Check to see if it's possible to do matchesSelector - // on a disconnected node (IE 9 fails this) - var disconnectedMatch = !matches.call( document.createElement( "div" ), "div" ), - pseudoWorks = false; - - try { - // This should fail with an exception - // Gecko does not error, returns false instead - matches.call( document.documentElement, "[test!='']:sizzle" ); - - } catch( pseudoError ) { - pseudoWorks = true; - } - - Sizzle.matchesSelector = function( node, expr ) { - // Make sure that attribute selectors are quoted - expr = expr.replace(/\=\s*([^'"\]]*)\s*\]/g, "='$1']"); - - if ( !Sizzle.isXML( node ) ) { - try { - if ( pseudoWorks || !Expr.match.PSEUDO.test( expr ) && !/!=/.test( expr ) ) { - var ret = matches.call( node, expr ); - - // IE 9's matchesSelector returns false on disconnected nodes - if ( ret || !disconnectedMatch || - // As well, disconnected nodes are said to be in a document - // fragment in IE 9, so check for that - node.document && node.document.nodeType !== 11 ) { - return ret; - } - } - } catch(e) {} - } - - return Sizzle(expr, null, null, [node]).length > 0; - }; - } -})(); - -(function(){ - var div = document.createElement("div"); - - div.innerHTML = "
"; - - // Opera can't find a second classname (in 9.6) - // Also, make sure that getElementsByClassName actually exists - if ( !div.getElementsByClassName || div.getElementsByClassName("e").length === 0 ) { - return; - } - - // Safari caches class attributes, doesn't catch changes (in 3.2) - div.lastChild.className = "e"; - - if ( div.getElementsByClassName("e").length === 1 ) { - return; - } - - Expr.order.splice(1, 0, "CLASS"); - Expr.find.CLASS = function( match, context, isXML ) { - if ( typeof context.getElementsByClassName !== "undefined" && !isXML ) { - return context.getElementsByClassName(match[1]); - } - }; - - // release memory in IE - div = null; -})(); - -function dirNodeCheck( dir, cur, doneName, checkSet, nodeCheck, isXML ) { - for ( var i = 0, l = checkSet.length; i < l; i++ ) { - var elem = checkSet[i]; - - if ( elem ) { - var match = false; - - elem = elem[dir]; - - while ( elem ) { - if ( elem[ expando ] === doneName ) { - match = checkSet[elem.sizset]; - break; - } - - if ( elem.nodeType === 1 && !isXML ){ - elem[ expando ] = doneName; - elem.sizset = i; - } - - if ( elem.nodeName.toLowerCase() === cur ) { - match = elem; - break; - } - - elem = elem[dir]; - } - - checkSet[i] = match; - } - } -} - -function dirCheck( dir, cur, doneName, checkSet, nodeCheck, isXML ) { - for ( var i = 0, l = checkSet.length; i < l; i++ ) { - var elem = checkSet[i]; - - if ( elem ) { - var match = false; - - elem = elem[dir]; - - while ( elem ) { - if ( elem[ expando ] === doneName ) { - match = checkSet[elem.sizset]; - break; - } - - if ( elem.nodeType === 1 ) { - if ( !isXML ) { - elem[ expando ] = doneName; - elem.sizset = i; - } - - if ( typeof cur !== "string" ) { - if ( elem === cur ) { - match = true; - break; - } - - } else if ( Sizzle.filter( cur, [elem] ).length > 0 ) { - match = elem; - break; - } - } - - elem = elem[dir]; - } - - checkSet[i] = match; - } - } -} - -if ( document.documentElement.contains ) { - Sizzle.contains = function( a, b ) { - return a !== b && (a.contains ? a.contains(b) : true); - }; - -} else if ( document.documentElement.compareDocumentPosition ) { - Sizzle.contains = function( a, b ) { - return !!(a.compareDocumentPosition(b) & 16); - }; - -} else { - Sizzle.contains = function() { - return false; - }; -} - -Sizzle.isXML = function( elem ) { - // documentElement is verified for cases where it doesn't yet exist - // (such as loading iframes in IE - #4833) - var documentElement = (elem ? elem.ownerDocument || elem : 0).documentElement; - - return documentElement ? documentElement.nodeName !== "HTML" : false; -}; - -var posProcess = function( selector, context, seed ) { - var match, - tmpSet = [], - later = "", - root = context.nodeType ? [context] : context; - - // Position selectors must be done after the filter - // And so must :not(positional) so we move all PSEUDOs to the end - while ( (match = Expr.match.PSEUDO.exec( selector )) ) { - later += match[0]; - selector = selector.replace( Expr.match.PSEUDO, "" ); - } - - selector = Expr.relative[selector] ? selector + "*" : selector; - - for ( var i = 0, l = root.length; i < l; i++ ) { - Sizzle( selector, root[i], tmpSet, seed ); - } - - return Sizzle.filter( later, tmpSet ); -}; - -// EXPOSE -// Override sizzle attribute retrieval -Sizzle.attr = jQuery.attr; -Sizzle.selectors.attrMap = {}; -jQuery.find = Sizzle; -jQuery.expr = Sizzle.selectors; -jQuery.expr[":"] = jQuery.expr.filters; -jQuery.unique = Sizzle.uniqueSort; -jQuery.text = Sizzle.getText; -jQuery.isXMLDoc = Sizzle.isXML; -jQuery.contains = Sizzle.contains; - - -})(); - - -var runtil = /Until$/, - rparentsprev = /^(?:parents|prevUntil|prevAll)/, - // Note: This RegExp should be improved, or likely pulled from Sizzle - rmultiselector = /,/, - isSimple = /^.[^:#\[\.,]*$/, - slice = Array.prototype.slice, - POS = jQuery.expr.match.globalPOS, - // methods guaranteed to produce a unique set when starting from a unique set - guaranteedUnique = { - children: true, - contents: true, - next: true, - prev: true - }; - -jQuery.fn.extend({ - find: function( selector ) { - var self = this, - i, l; - - if ( typeof selector !== "string" ) { - return jQuery( selector ).filter(function() { - for ( i = 0, l = self.length; i < l; i++ ) { - if ( jQuery.contains( self[ i ], this ) ) { - return true; - } - } - }); - } - - var ret = this.pushStack( "", "find", selector ), - length, n, r; - - for ( i = 0, l = this.length; i < l; i++ ) { - length = ret.length; - jQuery.find( selector, this[i], ret ); - - if ( i > 0 ) { - // Make sure that the results are unique - for ( n = length; n < ret.length; n++ ) { - for ( r = 0; r < length; r++ ) { - if ( ret[r] === ret[n] ) { - ret.splice(n--, 1); - break; - } - } - } - } - } - - return ret; - }, - - has: function( target ) { - var targets = jQuery( target ); - return this.filter(function() { - for ( var i = 0, l = targets.length; i < l; i++ ) { - if ( jQuery.contains( this, targets[i] ) ) { - return true; - } - } - }); - }, - - not: function( selector ) { - return this.pushStack( winnow(this, selector, false), "not", selector); - }, - - filter: function( selector ) { - return this.pushStack( winnow(this, selector, true), "filter", selector ); - }, - - is: function( selector ) { - return !!selector && ( - typeof selector === "string" ? - // If this is a positional selector, check membership in the returned set - // so $("p:first").is("p:last") won't return true for a doc with two "p". - POS.test( selector ) ? - jQuery( selector, this.context ).index( this[0] ) >= 0 : - jQuery.filter( selector, this ).length > 0 : - this.filter( selector ).length > 0 ); - }, - - closest: function( selectors, context ) { - var ret = [], i, l, cur = this[0]; - - // Array (deprecated as of jQuery 1.7) - if ( jQuery.isArray( selectors ) ) { - var level = 1; - - while ( cur && cur.ownerDocument && cur !== context ) { - for ( i = 0; i < selectors.length; i++ ) { - - if ( jQuery( cur ).is( selectors[ i ] ) ) { - ret.push({ selector: selectors[ i ], elem: cur, level: level }); - } - } - - cur = cur.parentNode; - level++; - } - - return ret; - } - - // String - var pos = POS.test( selectors ) || typeof selectors !== "string" ? - jQuery( selectors, context || this.context ) : - 0; - - for ( i = 0, l = this.length; i < l; i++ ) { - cur = this[i]; - - while ( cur ) { - if ( pos ? pos.index(cur) > -1 : jQuery.find.matchesSelector(cur, selectors) ) { - ret.push( cur ); - break; - - } else { - cur = cur.parentNode; - if ( !cur || !cur.ownerDocument || cur === context || cur.nodeType === 11 ) { - break; - } - } - } - } - - ret = ret.length > 1 ? jQuery.unique( ret ) : ret; - - return this.pushStack( ret, "closest", selectors ); - }, - - // Determine the position of an element within - // the matched set of elements - index: function( elem ) { - - // No argument, return index in parent - if ( !elem ) { - return ( this[0] && this[0].parentNode ) ? this.prevAll().length : -1; - } - - // index in selector - if ( typeof elem === "string" ) { - return jQuery.inArray( this[0], jQuery( elem ) ); - } - - // Locate the position of the desired element - return jQuery.inArray( - // If it receives a jQuery object, the first element is used - elem.jquery ? elem[0] : elem, this ); - }, - - add: function( selector, context ) { - var set = typeof selector === "string" ? - jQuery( selector, context ) : - jQuery.makeArray( selector && selector.nodeType ? [ selector ] : selector ), - all = jQuery.merge( this.get(), set ); - - return this.pushStack( isDisconnected( set[0] ) || isDisconnected( all[0] ) ? - all : - jQuery.unique( all ) ); - }, - - andSelf: function() { - return this.add( this.prevObject ); - } -}); - -// A painfully simple check to see if an element is disconnected -// from a document (should be improved, where feasible). -function isDisconnected( node ) { - return !node || !node.parentNode || node.parentNode.nodeType === 11; -} - -jQuery.each({ - parent: function( elem ) { - var parent = elem.parentNode; - return parent && parent.nodeType !== 11 ? parent : null; - }, - parents: function( elem ) { - return jQuery.dir( elem, "parentNode" ); - }, - parentsUntil: function( elem, i, until ) { - return jQuery.dir( elem, "parentNode", until ); - }, - next: function( elem ) { - return jQuery.nth( elem, 2, "nextSibling" ); - }, - prev: function( elem ) { - return jQuery.nth( elem, 2, "previousSibling" ); - }, - nextAll: function( elem ) { - return jQuery.dir( elem, "nextSibling" ); - }, - prevAll: function( elem ) { - return jQuery.dir( elem, "previousSibling" ); - }, - nextUntil: function( elem, i, until ) { - return jQuery.dir( elem, "nextSibling", until ); - }, - prevUntil: function( elem, i, until ) { - return jQuery.dir( elem, "previousSibling", until ); - }, - siblings: function( elem ) { - return jQuery.sibling( ( elem.parentNode || {} ).firstChild, elem ); - }, - children: function( elem ) { - return jQuery.sibling( elem.firstChild ); - }, - contents: function( elem ) { - return jQuery.nodeName( elem, "iframe" ) ? - elem.contentDocument || elem.contentWindow.document : - jQuery.makeArray( elem.childNodes ); - } -}, function( name, fn ) { - jQuery.fn[ name ] = function( until, selector ) { - var ret = jQuery.map( this, fn, until ); - - if ( !runtil.test( name ) ) { - selector = until; - } - - if ( selector && typeof selector === "string" ) { - ret = jQuery.filter( selector, ret ); - } - - ret = this.length > 1 && !guaranteedUnique[ name ] ? jQuery.unique( ret ) : ret; - - if ( (this.length > 1 || rmultiselector.test( selector )) && rparentsprev.test( name ) ) { - ret = ret.reverse(); - } - - return this.pushStack( ret, name, slice.call( arguments ).join(",") ); - }; -}); - -jQuery.extend({ - filter: function( expr, elems, not ) { - if ( not ) { - expr = ":not(" + expr + ")"; - } - - return elems.length === 1 ? - jQuery.find.matchesSelector(elems[0], expr) ? [ elems[0] ] : [] : - jQuery.find.matches(expr, elems); - }, - - dir: function( elem, dir, until ) { - var matched = [], - cur = elem[ dir ]; - - while ( cur && cur.nodeType !== 9 && (until === undefined || cur.nodeType !== 1 || !jQuery( cur ).is( until )) ) { - if ( cur.nodeType === 1 ) { - matched.push( cur ); - } - cur = cur[dir]; - } - return matched; - }, - - nth: function( cur, result, dir, elem ) { - result = result || 1; - var num = 0; - - for ( ; cur; cur = cur[dir] ) { - if ( cur.nodeType === 1 && ++num === result ) { - break; - } - } - - return cur; - }, - - sibling: function( n, elem ) { - var r = []; - - for ( ; n; n = n.nextSibling ) { - if ( n.nodeType === 1 && n !== elem ) { - r.push( n ); - } - } - - return r; - } -}); - -// Implement the identical functionality for filter and not -function winnow( elements, qualifier, keep ) { - - // Can't pass null or undefined to indexOf in Firefox 4 - // Set to 0 to skip string check - qualifier = qualifier || 0; - - if ( jQuery.isFunction( qualifier ) ) { - return jQuery.grep(elements, function( elem, i ) { - var retVal = !!qualifier.call( elem, i, elem ); - return retVal === keep; - }); - - } else if ( qualifier.nodeType ) { - return jQuery.grep(elements, function( elem, i ) { - return ( elem === qualifier ) === keep; - }); - - } else if ( typeof qualifier === "string" ) { - var filtered = jQuery.grep(elements, function( elem ) { - return elem.nodeType === 1; - }); - - if ( isSimple.test( qualifier ) ) { - return jQuery.filter(qualifier, filtered, !keep); - } else { - qualifier = jQuery.filter( qualifier, filtered ); - } - } - - return jQuery.grep(elements, function( elem, i ) { - return ( jQuery.inArray( elem, qualifier ) >= 0 ) === keep; - }); -} - - - - -function createSafeFragment( document ) { - var list = nodeNames.split( "|" ), - safeFrag = document.createDocumentFragment(); - - if ( safeFrag.createElement ) { - while ( list.length ) { - safeFrag.createElement( - list.pop() - ); - } - } - return safeFrag; -} - -var nodeNames = "abbr|article|aside|audio|bdi|canvas|data|datalist|details|figcaption|figure|footer|" + - "header|hgroup|mark|meter|nav|output|progress|section|summary|time|video", - rinlinejQuery = / jQuery\d+="(?:\d+|null)"/g, - rleadingWhitespace = /^\s+/, - rxhtmlTag = /<(?!area|br|col|embed|hr|img|input|link|meta|param)(([\w:]+)[^>]*)\/>/ig, - rtagName = /<([\w:]+)/, - rtbody = /]", "i"), - // checked="checked" or checked - rchecked = /checked\s*(?:[^=]|=\s*.checked.)/i, - rscriptType = /\/(java|ecma)script/i, - rcleanScript = /^\s*", "" ], - legend: [ 1, "
", "
" ], - thead: [ 1, "", "
" ], - tr: [ 2, "", "
" ], - td: [ 3, "", "
" ], - col: [ 2, "", "
" ], - area: [ 1, "", "" ], - _default: [ 0, "", "" ] - }, - safeFragment = createSafeFragment( document ); - -wrapMap.optgroup = wrapMap.option; -wrapMap.tbody = wrapMap.tfoot = wrapMap.colgroup = wrapMap.caption = wrapMap.thead; -wrapMap.th = wrapMap.td; - -// IE can't serialize and