11import videojs from 'video.js' ;
22import 'assets/styles/components/title-bar.scss' ;
33import componentUtils from '../component-utils' ;
4+ import { getCloudinaryUrlPrefix } from 'plugins/cloudinary/common' ;
45
56// support VJS5 & VJS6 at the same time
67const dom = videojs . dom || videojs ;
@@ -18,34 +19,72 @@ class TitleBar extends Component {
1819 setItem ( source ) {
1920 if ( ! source ) {
2021 this . setTitle ( '' ) ;
21- this . setSubtitle ( '' ) ;
22-
22+ this . setDescription ( '' ) ;
2323 return ;
2424 }
2525
2626 const info = source . info ( ) ;
27-
2827 this . setTitle ( info . title ) ;
29- this . setSubtitle ( info . subtitle ) ;
28+ this . setDescription ( info . subtitle ) ;
29+
30+ // auto-fetch title/description if `true`
31+ const shouldFetchTitle = source . title && source . title ( ) === true ;
32+ const shouldFetchDescription = source . description && source . description ( ) === true ;
33+
34+ if ( shouldFetchTitle || shouldFetchDescription ) {
35+ this . fetchAutoMetadata ( source , shouldFetchTitle , shouldFetchDescription ) ;
36+ }
37+ }
38+
39+ fetchAutoMetadata ( source , fetchTitle , fetchDescription ) {
40+ if ( source . isRawUrl ) return ;
41+
42+ const config = source . cloudinaryConfig ( ) ;
43+ const publicId = source . publicId ( ) ;
44+
45+ if ( ! config ?. cloud_name || ! publicId ) return ;
46+
47+ const urlPrefix = getCloudinaryUrlPrefix ( config ) ;
48+ const deliveryType = source . getInitOptions ( ) . type || 'upload' ;
49+ const metadataUrl = `${ urlPrefix } /_applet_/video_service/video_metadata/${ deliveryType } /${ publicId } .json` ;
50+
51+ fetch ( metadataUrl )
52+ . then ( response => {
53+ if ( ! response . ok ) throw new Error ( `HTTP ${ response . status } ` ) ;
54+ return response . json ( ) ;
55+ } )
56+ . then ( metadata => {
57+ if ( fetchTitle && metadata . title ) {
58+ this . setTitle ( metadata . title ) ;
59+ }
60+ if ( fetchDescription && metadata . description ) {
61+ this . setDescription ( metadata . description ) ;
62+ }
63+ } )
64+ . catch ( error => {
65+ console . warn ( `Failed to fetch metadata for ${ publicId } :` , error ) ;
66+ } ) ;
3067 }
3168
3269 setTitle ( text ) {
33- componentUtils . setText ( this . titleEl , text ) ;
70+ const displayText = typeof text === 'string' ? text : '' ;
71+ componentUtils . setText ( this . titleEl , displayText ) ;
3472 this . refresh ( ) ;
35- return text ;
73+ return displayText ;
3674 }
3775
38- setSubtitle ( text ) {
39- componentUtils . setText ( this . subtitleEl , text ) ;
76+ setDescription ( text ) {
77+ const displayText = typeof text === 'string' ? text : '' ;
78+ componentUtils . setText ( this . descriptionEl , displayText ) ;
4079 this . refresh ( ) ;
41- return text ;
80+ return displayText ;
4281 }
4382
4483 refresh ( ) {
4584 const titleValue = ( ) => this . titleEl . innerText ;
46- const subtitleValue = ( ) => this . subtitleEl . innerText ;
85+ const descriptionValue = ( ) => this . descriptionEl . innerText ;
4786
48- if ( ! titleValue ( ) && ! subtitleValue ( ) ) {
87+ if ( ! titleValue ( ) && ! descriptionValue ( ) ) {
4988 this . hide ( ) ;
5089 return ;
5190 }
@@ -58,7 +97,7 @@ class TitleBar extends Component {
5897 className : 'vjs-title-bar-title'
5998 } ) ;
6099
61- this . subtitleEl = dom . createEl ( 'div' , {
100+ this . descriptionEl = dom . createEl ( 'div' , {
62101 className : 'vjs-title-bar-subtitle'
63102 } ) ;
64103
@@ -68,7 +107,7 @@ class TitleBar extends Component {
68107 } ) ;
69108
70109 el . appendChild ( this . titleEl ) ;
71- el . appendChild ( this . subtitleEl ) ;
110+ el . appendChild ( this . descriptionEl ) ;
72111
73112 return el ;
74113 }
0 commit comments