Class: VideoPlayerComponent

Inherits:
ViewComponent::Base
  • Object
show all
Includes:
ApplicationHelper
Defined in:
app/components/video_player_component.rb

Overview

ViewComponent: renders the video player block.

Supports Cloudflare Stream videos (iframe or HTML5/Shaka player) and
self-hosted videos with an attachment.

Constant Summary collapse

PLAYER_TYPES =

Video types.

%w[iframe html5 auto].freeze
PRELOAD_OPTIONS =

Available preload options.

%w[auto metadata none].freeze

Constants included from ApplicationHelper

ApplicationHelper::CF_WEB_ANALYTICS_SKIP_PATHS, ApplicationHelper::MARKDOWN_ALLOWED_ATTRIBUTES, ApplicationHelper::MARKDOWN_ALLOWED_TAGS

Constants included from Www::SeoHelper

Www::SeoHelper::AWARDS, Www::SeoHelper::CA_ADDRESS, Www::SeoHelper::CA_BUSINESS_HOURS, Www::SeoHelper::CA_CONTACT_POINT, Www::SeoHelper::CA_CURRENCIES, Www::SeoHelper::CA_DESCRIPTION, Www::SeoHelper::CA_FOUNDING_DATE, Www::SeoHelper::CA_GLOBAL_LOCATION_NUMBER, Www::SeoHelper::CA_LEGAL_NAME, Www::SeoHelper::CA_LOCAL_BUSINESS, Www::SeoHelper::CA_ONLINE_STORE, Www::SeoHelper::CA_RETURN_POLICY, Www::SeoHelper::CA_SALES_DEPARTMENT, Www::SeoHelper::CA_SERVICE_AREA, Www::SeoHelper::CA_URL, Www::SeoHelper::CA_VAT_ID, Www::SeoHelper::CA_WAREHOUSE_DEPARTMENT, Www::SeoHelper::CA_WAREHOUSE_HOURS, Www::SeoHelper::COMPANY_EMAIL, Www::SeoHelper::COMPANY_LOGO, Www::SeoHelper::COMPANY_NAME, Www::SeoHelper::COMPANY_SLOGAN, Www::SeoHelper::EXPERTISE, Www::SeoHelper::FAX_NUMBER, Www::SeoHelper::GS1_COMPANY_PREFIX, Www::SeoHelper::ISO6523_CODE, Www::SeoHelper::PAYMENT_METHODS, Www::SeoHelper::PHONE_NUMBER, Www::SeoHelper::PRIMARY_NAICS, Www::SeoHelper::REFUND_TYPE, Www::SeoHelper::RETURN_FEES, Www::SeoHelper::RETURN_METHOD, Www::SeoHelper::RETURN_POLICY_CATEGORY, Www::SeoHelper::SECONDARY_NAICS, Www::SeoHelper::SOCIAL_PROFILES, Www::SeoHelper::US_ADDRESS, Www::SeoHelper::US_BUSINESS_HOURS, Www::SeoHelper::US_CONTACT_POINT, Www::SeoHelper::US_CURRENCIES, Www::SeoHelper::US_DESCRIPTION, Www::SeoHelper::US_FOUNDING_DATE, Www::SeoHelper::US_GLOBAL_LOCATION_NUMBER, Www::SeoHelper::US_IMAGE, Www::SeoHelper::US_LEGAL_NAME, Www::SeoHelper::US_LOCAL_BUSINESS, Www::SeoHelper::US_ONLINE_STORE, Www::SeoHelper::US_RETURN_POLICY, Www::SeoHelper::US_SALES_DEPARTMENT, Www::SeoHelper::US_SERVICE_AREA, Www::SeoHelper::US_TAX_ID, Www::SeoHelper::US_URL, Www::SeoHelper::US_WAREHOUSE_DEPARTMENT, Www::SeoHelper::US_WAREHOUSE_HOURS

Constants included from IconHelper

IconHelper::CUSTOM_ICON_MAP, IconHelper::CUSTOM_SVG_DIR, IconHelper::DEFAULT_FAMILY

Instance Method Summary collapse

Methods included from ApplicationHelper

#better_number_to_currency, #cf_web_analytics?, #check_or_cross, #check_or_times, #embedded_tab_frame_id, #error_messages, #general_disclaimer_on_product_installation_and_local_codes, #markdown_to_html, #parent_layout, #pass_or_fail, #render_error_messages_list, #render_video_card, #resolved_auth_form_turbo_frame, #return_path_or, #safe_css_color, #set_return_path_if_present, #set_section_if_present, #tab_frame_id, #to_underscore, #turbo_section_wrapper, #turbo_tabs_request?, #url_on_same_domain_as_request, #working_hours?, #yes_or_no, #yes_or_no_highlighted, #yes_or_no_with_check_or_cross

Methods included from UppyUploaderHelper

#file_uploader, #image_uploader, #large_file_uploader_s3, #lead_sketch_uploader, #public_warranty_card_uploader, #rma_image_uploader, #rma_image_uploader_s3, #uppy_uploader, #video_uploader, #warranty_card_uploader

Methods included from Www::ImagesHelper

#image_asset_tag, #image_asset_url, #picture_asset_tag

Methods included from Www::SeoHelper

#add_page_schema, #add_webpage_schema, #canada?, #collect_schema, #company_social_links, #ensure_context_json, #json_ld_script_tag, #local_business_schema, #online_store_id, #online_store_schema, #page_main_entity, #page_main_entity_json, #render_auto_collection_page_schema, #render_collection_page_schema, #render_local_business_schema, #render_online_store_schema, #render_page_schemas, #render_page_video_schemas, #render_webpage_schema, #render_webpage_schema_with_collections, #usa?

Methods included from UrlsHelper

#catalog_breadcrumb_links, #catalog_link, #catalog_link_for_product_line, #catalog_link_for_sku, #cms_link, #delocalized_path, #path_to_sales_product_sku, #path_to_sales_product_sku_for_product_line, #path_to_sales_product_sku_for_product_line_slug, #product_line_from_catalog_link, #protocol_neutral_url, #sanitize_external_url, #valid_external_url?

Methods included from IconHelper

#account_nav_icon, #fa_icon, #star_rating_html

Constructor Details

#initialize(video:, player_type: 'auto', thumb: nil, slug: nil, autoplay: false, controls: true, preload: 'metadata', class: nil, lazy: false, shaka_ui: false) ⇒ VideoPlayerComponent

Returns a new instance of VideoPlayerComponent.

Parameters:

  • video (Object)

    Video-like record (responds to cloudflare_uid for
    Cloudflare videos, or is_hosted?/attachment for hosted videos)

  • player_type (String) (defaults to: 'auto')

    Player type (iframe, html5, or auto to
    pick based on the request context); invalid values fall back to auto

  • thumb (String, nil) (defaults to: nil)

    Poster/thumbnail URL; defaults to the video's
    own poster image or Cloudflare thumbnail when available

  • slug (String, nil) (defaults to: nil)

    Slug used for engagement tracking; defaults to
    the video's own slug

  • autoplay (Boolean) (defaults to: false)

    Whether the video starts playing automatically

  • controls (Boolean) (defaults to: true)

    Whether to render playback controls

  • preload (String) (defaults to: 'metadata')

    preload attribute (auto, metadata, or
    none); invalid values fall back to metadata

  • class (String, nil) (defaults to: nil)

    Additional CSS classes for the player element

  • lazy (Boolean) (defaults to: false)

    Whether to defer loading the player until it is
    scrolled into view

  • shaka_ui (Boolean) (defaults to: false)

    Whether to render Shaka's UI (its own controls,
    a #t= deep-link seek, and chapter markers) instead of native controls



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/components/video_player_component.rb', line 32

def initialize(video:, player_type: 'auto', thumb: nil, slug: nil, autoplay: false, controls: true, preload: 'metadata', class: nil, lazy: false, shaka_ui: false)
  super()
  @video = video
  @player_type = validate_player_type(player_type)
  @thumb = thumb
  @slug = slug
  @autoplay = autoplay
  @controls = controls
  @preload = validate_preload(preload)
  @additional_class = binding.local_variable_get(:class)
  @lazy = lazy
  # When true, render Shaka's UI (its own controls + a #t= deep-link seek, plus
  # chapter markers when the video has chapters) instead of native controls.
  # Set on the watch page for a consistent player; gallery/PDP players stay
  # native. Opt-in so a stray #t= hash never affects those other players.
  @shaka_ui = shaka_ui

  # Set default thumbnail for Cloudflare videos
  # Prefer our own poster image via Image#image_url when available; fallback to Cloudflare
  if @video.respond_to?(:poster_image) && @video.poster_image.present?
    @thumb ||= @video.poster_image.image_url(width: 1280, height: 720, thumbnail: true)
  elsif @video.respond_to?(:thumbnail_url)
    @thumb ||= @video.thumbnail_url(width: 1280, height: 720)
  end
end

Instance Method Details

#before_rendervoid

This method returns an undefined value.

Resolves auto player type based on the request context.



60
61
62
63
64
65
# File 'app/components/video_player_component.rb', line 60

def before_render
  # Auto-detect player type based on context if not specified
  return unless @player_type == 'auto'

  @player_type = is_crm_request? ? 'iframe' : 'html5'
end