<commit_before>(function ( mw, $ ) {
	"use strict";

	mw.PluginManager.add( 'mediaList', mw.KBaseComponent.extend( {

			defaultConfig: {
				'parent': 'sideBarContainer',
				'containerPosition': null,//'after',
				'order': 2,
				'showTooltip': false,
				"displayImportance": 'high',
				'templatePath': 'components/mediaList/mediaList.tmpl.html',
				'cuePointType': ['annotation.Annotation', 'codeCuePoint.Code', 'thumbCuePoint.Thumb'],
				'oneSecRotatorSlidesLimit': 61,
				'twoSecRotatorSlidesLimit': 250,
				'maxRotatorSlides': 125,
				'mediaItemWidth': 290,
				'titleLimit': 25,
				'descriptionLimit': 70,
				'thumbnailWidth' : 100,
				'horizontalChapterBoxWidth': 290,
				'overflow': false,
				'includeThumbnail': true,
				'includeChapterStartTime': true,
				'includeChapterNumberPattern': false,
				'includeChapterDuration': true
			},
			mediaList: [],

			isDisabled: true,

			setup: function ( embedPlayer ) {
				this.baseThumbSettings = {
					'partner_id': this.getPlayer().kpartnerid,
					'uiconf_id': this.getPlayer().kuiconfid,
					'entry_id': this.getPlayer().kentryid,
					'width': this.getConfig( "thumbWidth" )
				};
				if (this.getConfig('containerPosition')){
					this.getListContainer();
				}
				this.addBindings();
			},
			addBindings: function () {
				var _this = this;

				this.bind( 'KalturaSupport_ThumbCuePointsReady', function () {
					_this.mediaList.meta = {};
					//Init data provider
					var cuePoints = _this.getPlayer().kCuePoints.getCuePoints();
					//Generate data transfer object
					var filteredCuePoints = $.grep(cuePoints, function(cuePoint){
						var found = false;
						$.each(_this.getConfig('cuePointType'), function(i, cuePointType){
							if (cuePointType == cuePoint.cuePointType) {
								found = true;
								return false;
							}
						});
						return found;
					});

					filteredCuePoints.sort( function ( a, b ) {
						return a.startTime - b.startTime;
					} );

					$.each(filteredCuePoints, function(i, filteredCuePoint){
						_this.addMediaItem(filteredCuePoint, i);
					});

					//Need to recalc all durations after we have all the items startTime values
					_this.setMediaItemTime();

					_this.getComponent().append(
						_this.getTemplateHTML( {meta: _this.getMetaData(), mediaList: _this.getTemplateData()})
					);

					if (_this.getConfig('containerPosition')) {
						_this.$chaptersContainer.append(_this.getTemplateHTML( {meta: _this.getMetaData(), mediaList: _this.getTemplateData()} ));
					}
					_this.dataIntialized = true;
					_this.shouldAddScroll(_this.addScroll);
				} );

				this.bind( 'playerReady', function ( e, newState ) {
					if (_this.dataIntialized) {
						_this.updateActiveItem();
					}
				});

				this.bind('updateLayout', function(){
					if (_this.dataIntialized) {
						_this.getComponent().empty().append(
							_this.getTemplateHTML( {meta: _this.getMetaData(), mediaList: _this.getTemplateData()})
						);
						_this.shouldAddScroll(_this.addScroll);
						//_this.shouldAddScroll(_this.initScroll);
					}
				});

				this.bind( 'onChangeMedia', function(){
					_this.destroy();
					// redraw the list
					_this.shouldAddScroll(_this.addScroll);
				});

				this.bind('updatePlayHeadPercent', function( ct ){
					if (_this.dataIntialized) {
						_this.updateActiveItem();
					}
				});
			},
			getComponent: function(){
				if( ! this.$el ){
					this.$el = $( '<div />' )
						.addClass(this.pluginName + " k-chapters-container k-" + this.getLayout() /*+ this.getCssClass()*/);
						/*.append(
							this.getTemplateHTML( {meta: this.getMetaData(), mediaList: this.getTemplateData()})
					);*/
				}
				return this.$el;
			},
			destroy: function(){
				this.unbind();
				this.getComponent.empty();
			},
			//General
			getLayout: function(){
				return  this.getConfig( 'layout' ) || 'horizontal';
			},
			getTemplateData: function(){
				return this.mediaList;
			},
			getMetaData: function(){
				return {
					includeThumbnail: this.getConfig('includeThumbnail'),
					includeChapterStartTime: this.getConfig('includeChapterStartTime'),
					includeChapterNumberPattern: this.getConfig('includeChapterNumberPattern'),
					includeChapterDuration:this.getConfig('includeChapterDuration'),
					titleLimit: this.getConfig('titleLimit'),
					descLimit: this.getConfig('descriptionLimit'),
					layout: this.getLayout()
				}
			},
			getListContainer: function(){
				// remove any existing k-chapters-container for this player
				$('.k-player-' + this.getPlayer().id + '.k-chapters-container').remove();
				// Build new chapters container
				var $chaptersContainer = this.$chaptersContainer = $('<div>').addClass( 'k-player-' + this.getPlayer().id + ' k-chapters-container');
				// check for where it should be appended:
				var targetRef = $('#'+this.getPlayer().id, parent.document.body );//$( this.getPlayer().getInterface() );
				switch( this.getConfig('containerPosition') ){
					case 'before':
						$chaptersContainer.css('clear', 'both');
						targetRef
							.css( 'float', 'left')
							.before( $chaptersContainer );
						break;
					case 'left':
						$chaptersContainer.css('float', 'left').insertBefore( targetRef );
						$( this.getPlayer() ).css('float', 'left');
						break;
					case 'right':
						$chaptersContainer.css('float', 'left').insertAfter( targetRef );
						$( targetRef ).css('float', 'left' );
						break;
					case 'after':
					default:
						targetRef
							.css( 'float', 'none')
							.after( $chaptersContainer );
						break;
				};
				// set size based on layout
				// set sizes:
				if( this.getConfig('overflow') != true ){
					$chaptersContainer.css('width', targetRef.width() )
					if( this.getLayout() == 'vertical' ){
						$chaptersContainer.css( 'height', targetRef.height() )
					}
				} else {
					if( this.getLayout() == 'horizontal' ){
						$chaptersContainer.css('width', '100%' );
					} else if( this.getLayout() == 'vertical' ){
						$chaptersContainer.css( 'width', targetRef.width() );
					}
				}
				// special conditional for vertical chapter width
				if(
					this.getLayout() == 'vertical'
					&&
					this.getConfig('horizontalChapterBoxWidth')
					&&
					(
						this.getConfig('containerPosition') == 'right'
						||
						this.getConfig('containerPosition') == 'left'
						)
					){
					$chaptersContainer.css('width', this.getConfig('horizontalChapterBoxWidth') );
				}
				return $chaptersContainer;
			},
			//Media Item
			addMediaItem: function(obj ,index){
				var mediaItem;
				var customData = obj.partnerData ? JSON.parse(obj.partnerData) :  {};
				var title = obj.title ? obj.title : customData['title'];
				var description = obj.description ? obj.description : customData['desc'];
				var thumbnailUrl = /*customData['thumbUrl'] ||*/ obj.thumbnailUrl || this.getThumbUrl(obj);
				var thumbnailRotatorUrl = this.getConfig( 'thumbnailRotator' ) ? this.getThumRotatorUrl() : '';

				mediaItem = {
					order: index,
					id: obj.id,
					title: title,
					description: description,
					width: this.getConfig( 'mediaItemWidth' ),
					thumbnail: {
						url: thumbnailUrl,
						thumbAssetId: obj.assetId,
						rotatorUrl: thumbnailRotatorUrl,
						width: this.getThumbWidth(),
						height: this.getThumbHeight()
					},
					startTime: obj.startTime / 1000,
					startTimeDisplay: this.formatTimeDisplayValue(kWidget.seconds2npt( obj.startTime / 1000 )),
					endTime: null,
					durationDisplay: null,
					chapterNumber: this.getItemNumber(index)

				}
				this.mediaList.push(mediaItem);
			},
			getMediaItemThumbs: function(callback){
				var _this = this;
				var requestArray = [];
				var response = [];
				$.each(this.mediaList, function(index, item) {
					requestArray.push(
						{
							'service': 'thumbAsset',
							'action': 'getUrl',
							'id': item.thumbnail.thumbAssetId
						}
					);
					response[index] = { id: item.id, url: null};
				});

				// do the api request
				this.getKalturaClient().doRequest( requestArray, function ( data ) {
					// Validate result
					if ( !_this.isValidResult( data ) ) {
						return;
					}
					$.each(data, function(index, url) {
						response[index]['url'] = url;

					});
					callback.apply(_this, [response]);
				} );
			},
			setMediaItemTime: function(){
				var _this = this;
				$.each(this.mediaList, function(index, item){
					if (_this.mediaList[index + 1]){
						item.endTime = _this.mediaList[index + 1].startTime;
					} else {
						item.endTime = _this.getPlayer().duration;
					}

					item.durationDisplay = kWidget.seconds2npt((item.endTime - item.startTime) );
				});
			},
			formatTimeDisplayValue: function(time){
				// Add 0 padding to start time min
				var timeParts = time.split(':');
				if( timeParts.length == 2 && timeParts[0].length == 1 ) {
					time = '0' + time;
				}
				return time;
			},
			getItemNumber: function(index){
				var chapterVal = ( index + 1 ).toString();
				if( typeof this.getConfig('includeChapterNumberPattern' ) == 'string' ){
					chapterVal =  this.getConfig('includeChapterNumberPattern' ).replace( '$1', chapterVal );
				}
				// replace spaces with '&nbsp;'
				chapterVal = chapterVal.replace(/\s/g, '&nbsp;' );
				return chapterVal;
			},
			getMediaItemById: function(id){
				var mediaItem = null;
				mediaItem = $.grep(this.mediaList, function(mediaItem){
					return (mediaItem.id == id);
				});
				return mediaItem;
			},
			getThumbUrl: function(item) {
				var thumbUrl = kWidget.getKalturaThumbUrl(
					$.extend( {}, this.baseThumbSettings, {
						'vid_sec': parseInt( item.startTime / 1000 )<commit_msg>Support cue point thumbnail from offset or start time<commit_after>(function ( mw, $ ) {
	"use strict";

	mw.PluginManager.add( 'mediaList', mw.KBaseComponent.extend( {

			defaultConfig: {
				'parent': 'sideBarContainer',
				'containerPosition': null,//'after',
				'order': 2,
				'showTooltip': false,
				"displayImportance": 'high',
				'templatePath': 'components/mediaList/mediaList.tmpl.html',
				'cuePointType': ['annotation.Annotation', 'codeCuePoint.Code', 'thumbCuePoint.Thumb'],
				'oneSecRotatorSlidesLimit': 61,
				'twoSecRotatorSlidesLimit': 250,
				'maxRotatorSlides': 125,
				'mediaItemWidth': 290,
				'titleLimit': 25,
				'descriptionLimit': 70,
				'thumbnailWidth' : 100,
				'horizontalChapterBoxWidth': 290,
				'overflow': false,
				'includeThumbnail': true,
				'includeChapterStartTime': true,
				'includeChapterNumberPattern': false,
				'includeChapterDuration': true
			},
			mediaList: [],

			isDisabled: true,

			setup: function ( embedPlayer ) {
				this.baseThumbSettings = {
					'partner_id': this.getPlayer().kpartnerid,
					'uiconf_id': this.getPlayer().kuiconfid,
					'entry_id': this.getPlayer().kentryid,
					'width': this.getConfig( "thumbWidth" )
				};
				if (this.getConfig('containerPosition')){
					this.getListContainer();
				}
				this.addBindings();
			},
			addBindings: function () {
				var _this = this;

				this.bind( 'KalturaSupport_ThumbCuePointsReady', function () {
					_this.mediaList.meta = {};
					//Init data provider
					var cuePoints = _this.getPlayer().kCuePoints.getCuePoints();
					//Generate data transfer object
					var filteredCuePoints = $.grep(cuePoints, function(cuePoint){
						var found = false;
						$.each(_this.getConfig('cuePointType'), function(i, cuePointType){
							if (cuePointType == cuePoint.cuePointType) {
								found = true;
								return false;
							}
						});
						return found;
					});

					filteredCuePoints.sort( function ( a, b ) {
						return a.startTime - b.startTime;
					} );

					$.each(filteredCuePoints, function(i, filteredCuePoint){
						_this.addMediaItem(filteredCuePoint, i);
					});

					//Need to recalc all durations after we have all the items startTime values
					_this.setMediaItemTime();

					_this.getComponent().append(
						_this.getTemplateHTML( {meta: _this.getMetaData(), mediaList: _this.getTemplateData()})
					);

					if (_this.getConfig('containerPosition')) {
						_this.$chaptersContainer.append(_this.getTemplateHTML( {meta: _this.getMetaData(), mediaList: _this.getTemplateData()} ));
					}
					_this.dataIntialized = true;
					_this.shouldAddScroll(_this.addScroll);
				} );

				this.bind( 'playerReady', function ( e, newState ) {
					if (_this.dataIntialized) {
						_this.updateActiveItem();
					}
				});

				this.bind('updateLayout', function(){
					if (_this.dataIntialized) {
						_this.getComponent().empty().append(
							_this.getTemplateHTML( {meta: _this.getMetaData(), mediaList: _this.getTemplateData()})
						);
						_this.shouldAddScroll(_this.addScroll);
						//_this.shouldAddScroll(_this.initScroll);
					}
				});

				this.bind( 'onChangeMedia', function(){
					_this.destroy();
					// redraw the list
					_this.shouldAddScroll(_this.addScroll);
				});

				this.bind('updatePlayHeadPercent', function( ct ){
					if (_this.dataIntialized) {
						_this.updateActiveItem();
					}
				});
			},
			getComponent: function(){
				if( ! this.$el ){
					this.$el = $( '<div />' )
						.addClass(this.pluginName + " k-chapters-container k-" + this.getLayout() /*+ this.getCssClass()*/);
						/*.append(
							this.getTemplateHTML( {meta: this.getMetaData(), mediaList: this.getTemplateData()})
					);*/
				}
				return this.$el;
			},
			destroy: function(){
				this.unbind();
				this.getComponent.empty();
			},
			//General
			getLayout: function(){
				return  this.getConfig( 'layout' ) || 'horizontal';
			},
			getTemplateData: function(){
				return this.mediaList;
			},
			getMetaData: function(){
				return {
					includeThumbnail: this.getConfig('includeThumbnail'),
					includeChapterStartTime: this.getConfig('includeChapterStartTime'),
					includeChapterNumberPattern: this.getConfig('includeChapterNumberPattern'),
					includeChapterDuration:this.getConfig('includeChapterDuration'),
					titleLimit: this.getConfig('titleLimit'),
					descLimit: this.getConfig('descriptionLimit'),
					layout: this.getLayout()
				}
			},
			getListContainer: function(){
				// remove any existing k-chapters-container for this player
				$('.k-player-' + this.getPlayer().id + '.k-chapters-container').remove();
				// Build new chapters container
				var $chaptersContainer = this.$chaptersContainer = $('<div>').addClass( 'k-player-' + this.getPlayer().id + ' k-chapters-container');
				// check for where it should be appended:
				var targetRef = $('#'+this.getPlayer().id, parent.document.body );//$( this.getPlayer().getInterface() );
				switch( this.getConfig('containerPosition') ){
					case 'before':
						$chaptersContainer.css('clear', 'both');
						targetRef
							.css( 'float', 'left')
							.before( $chaptersContainer );
						break;
					case 'left':
						$chaptersContainer.css('float', 'left').insertBefore( targetRef );
						$( this.getPlayer() ).css('float', 'left');
						break;
					case 'right':
						$chaptersContainer.css('float', 'left').insertAfter( targetRef );
						$( targetRef ).css('float', 'left' );
						break;
					case 'after':
					default:
						targetRef
							.css( 'float', 'none')
							.after( $chaptersContainer );
						break;
				};
				// set size based on layout
				// set sizes:
				if( this.getConfig('overflow') != true ){
					$chaptersContainer.css('width', targetRef.width() )
					if( this.getLayout() == 'vertical' ){
						$chaptersContainer.css( 'height', targetRef.height() )
					}
				} else {
					if( this.getLayout() == 'horizontal' ){
						$chaptersContainer.css('width', '100%' );
					} else if( this.getLayout() == 'vertical' ){
						$chaptersContainer.css( 'width', targetRef.width() );
					}
				}
				// special conditional for vertical chapter width
				if(
					this.getLayout() == 'vertical'
					&&
					this.getConfig('horizontalChapterBoxWidth')
					&&
					(
						this.getConfig('containerPosition') == 'right'
						||
						this.getConfig('containerPosition') == 'left'
						)
					){
					$chaptersContainer.css('width', this.getConfig('horizontalChapterBoxWidth') );
				}
				return $chaptersContainer;
			},
			//Media Item
			addMediaItem: function(obj ,index){
				var mediaItem;
				var customData = obj.partnerData ? JSON.parse(obj.partnerData) :  {};
				var title = obj.title || customData.title;
				var description = obj.description || customData.desc;
				debugger;
				var thumbnailUrl = obj.thumbnailUrl || customData.thumbUrl || this.getThumbUrl(obj);
				var thumbnailRotatorUrl = this.getConfig( 'thumbnailRotator' ) ? this.getThumRotatorUrl() : '';

				mediaItem = {
					order: index,
					id: obj.id,
					title: title,
					description: description,
					width: this.getConfig( 'mediaItemWidth' ),
					thumbnail: {
						url: thumbnailUrl,
						thumbAssetId: obj.assetId,
						rotatorUrl: thumbnailRotatorUrl,
						width: this.getThumbWidth(),
						height: this.getThumbHeight()
					},
					startTime: obj.startTime / 1000,
					startTimeDisplay: this.formatTimeDisplayValue(kWidget.seconds2npt( obj.startTime / 1000 )),
					endTime: null,
					durationDisplay: null,
					chapterNumber: this.getItemNumber(index)

				}
				this.mediaList.push(mediaItem);
			},
			getMediaItemThumbs: function(callback){
				var _this = this;
				var requestArray = [];
				var response = [];
				$.each(this.mediaList, function(index, item) {
					requestArray.push(
						{
							'service': 'thumbAsset',
							'action': 'getUrl',
							'id': item.thumbnail.thumbAssetId
						}
					);
					response[index] = { id: item.id, url: null};
				});

				// do the api request
				this.getKalturaClient().doRequest( requestArray, function ( data ) {
					// Validate result
					if ( !_this.isValidResult( data ) ) {
						return;
					}
					$.each(data, function(index, url) {
						response[index]['url'] = url;

					});
					callback.apply(_this, [response]);
				} );
			},
			setMediaItemTime: function(){
				var _this = this;
				$.each(this.mediaList, function(index, item){
					if (_this.mediaList[index + 1]){
						item.endTime = _this.mediaList[index + 1].startTime;
					} else {
						item.endTime = _this.getPlayer().duration;
					}

					item.durationDisplay = kWidget.seconds2npt((item.endTime - item.startTime) );
				});
			},
			formatTimeDisplayValue: function(time){
				// Add 0 padding to start time min
				var timeParts = time.split(':');
				if( timeParts.length == 2 && timeParts[0].length == 1 ) {
					time = '0' + time;
				}
				return time;
			},
			getItemNumber: function(index){
				var chapterVal = ( index + 1 ).toString();
				if( typeof this.getConfig('includeChapterNumberPattern' ) == 'string' ){
					chapterVal =  this.getConfig('includeChapterNumberPattern' ).replace( '$1', chapterVal );
				}
				// replace spaces with '&nbsp;'
				chapterVal = chapterVal.replace(/\s/g, '&nbsp;' );
				return chapterVal;
			},
			getMediaItemById: function(id){
				var mediaItem = null;
				mediaItem = $.grep(this.mediaList, function(mediaItem){
					return (mediaItem.id == id);
				});
				return mediaItem;
			},
			getThumbUrl: function(item) {
				var time = item.thumbOffset || item.startTime;
				var thumbUrl = kWidget.getKalturaThumbUrl(
					$.extend( {}, this.baseThumbSettings, {
						'vid_sec': parseInt( time / 1000 )
{'fraction_non_alphanumeric': 0.15122278402641626, 'fraction_numerical': 0.004953049220926633, 'mean_word_length': 3.009722796855606, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3136082', 'n_tokens_mistral': 8173, 'n_tokens_neox': 5957, 'n_words': 1137}