@@ -63,6 +63,7 @@ function cdsRecordController($scope, $sce, $http, $timeout, $filter) {
6363 $scope . activeTab = "chapters" ; // Default to chapters tab
6464 $scope . shortDescription = "" ;
6565 $scope . fullDescription = "" ;
66+ $scope . chapterFrames = { } ;
6667
6768 const REQUEST_HEADERS = {
6869 "Content-Type" : "application/json" ,
@@ -191,6 +192,7 @@ function cdsRecordController($scope, $sce, $http, $timeout, $filter) {
191192 if ( newVal ) {
192193 $scope . initVttLoad ( newVal ) ;
193194 $scope . prepareDescriptions ( newVal . metadata . description ) ;
195+ $scope . buildChapterFrames ( ) ;
194196 }
195197 } ) ;
196198
@@ -202,10 +204,14 @@ function cdsRecordController($scope, $sce, $http, $timeout, $filter) {
202204 }
203205
204206 const lines = description . split ( / \r ? \n / ) ;
205- const firstTen = lines . slice ( 0 , $scope . DESCRIPTION_PREVIEW_LINES ) . join ( "\n" ) ;
206-
207- $scope . shortDescription = $scope . processDescriptionWithClickableTimestamps ( firstTen ) ;
208- $scope . fullDescription = $scope . processDescriptionWithClickableTimestamps ( description ) ;
207+ const firstTen = lines
208+ . slice ( 0 , $scope . DESCRIPTION_PREVIEW_LINES )
209+ . join ( "\n" ) ;
210+
211+ $scope . shortDescription =
212+ $scope . processDescriptionWithClickableTimestamps ( firstTen ) ;
213+ $scope . fullDescription =
214+ $scope . processDescriptionWithClickableTimestamps ( description ) ;
209215 } ;
210216
211217 $scope . initVttLoad = function ( record ) {
@@ -405,43 +411,55 @@ function cdsRecordController($scope, $sce, $http, $timeout, $filter) {
405411 } ) ;
406412 } ;
407413
408- $scope . getChapterFrame = function ( chapter ) {
409- if ( ! $scope . record || ! chapter ) return null ;
414+ $scope . buildChapterFrames = function ( ) {
415+ if ( ! $scope . record || ! $scope . chapters ) return ;
410416
411- // Use the findMaster filter to get the master file (this filter is defined in cds/module.js)
412417 const master = $filter ( "findMaster" ) ( $scope . record ) ;
418+ if ( ! master || ! master . frame ) return ;
413419
414- if ( ! master || ! master . frame ) return null ;
415-
416- // Look for a frame with filename that matches chapter timestamp
417- // Chapter frames are named like "chapter-{seconds}.jpg"
418- const expectedFrameName = `chapter-${ chapter . seconds } .jpg` ;
420+ const frames = master . frame ;
421+ let matches = { } ;
419422
420- let chapterFrame = master . frame . find (
421- ( frame ) => frame . key === expectedFrameName
423+ // --- Exact filename match ---
424+ $scope . chapters . forEach ( ( chapter ) => {
425+ const expectedName = `chapter-${ chapter . seconds } .jpg` ;
426+ const frame = frames . find ( ( f ) => f . key === expectedName ) ;
427+ if ( frame ) {
428+ matches [ chapter . seconds ] = frame ;
429+ }
430+ } ) ;
431+ console . log ( "Exact matches found:" , matches ) ;
432+ // Collect chapters still missing
433+ let missing = $scope . chapters . filter (
434+ ( c ) => ! matches . hasOwnProperty ( c . seconds )
422435 ) ;
423- if ( ! chapterFrame ) {
424- // Find the frame with closest timestamp
436+
437+ // If none missing, return
438+ if ( missing . length === 0 ) {
439+ $scope . chapterFrames = matches ;
440+ return ;
441+ }
442+
443+ // --- Fallback closest timestamp ---
444+ missing . forEach ( ( chapter ) => {
425445 const target = Number ( chapter . seconds ) ;
426446 let closest = null ;
427447 let minDiff = Infinity ;
428448
429- master . frame . forEach ( ( frame ) => {
449+ frames . forEach ( ( frame ) => {
430450 if ( ! frame . tags || frame . tags . timestamp == null ) return ;
431-
432451 const ts = Number ( frame . tags . timestamp ) ;
433-
434452 const diff = Math . abs ( ts - target ) ;
435453 if ( diff < minDiff ) {
436454 minDiff = diff ;
437455 closest = frame ;
438456 }
439457 } ) ;
440458
441- chapterFrame = closest ;
442- }
459+ matches [ chapter . seconds ] = closest || null ;
460+ } ) ;
443461
444- return chapterFrame || null ;
462+ $scope . chapterFrames = matches ;
445463 } ;
446464
447465 $scope . cleanHtmlFromTitle = function ( title ) {
0 commit comments