define('application/formatters/epgasset', [
'rofl/lib/l10n',
'antie/formatter',
'antie/iterator',
'application/widgets/asset/asset',
'application/utils',
'application/managers/recording',
'application/managers/feature',
'antie/runtimecontext',
'rofl/lib/utils'
], function (
L10N,
Formatter,
Iterator,
Asset,
AppUtils,
RecordingManager,
FeatureManager,
RuntimeContext,
Utils
) {
'use strict';
var featureManager = FeatureManager.getInstance(),
recordingManager = RecordingManager.getInstance(),
episodeRecording = 'icon-record-new',
seriesRecording = 'icon-record-all',
unableToPlayIcon = 'icon-unable-play',
l10n = L10N.getInstance(),
app = RuntimeContext.getCurrentApplication(),
PROGRAM_BROADCAST = {
PAST: -1,
LIVE: 0,
FUTURE: 1
};
return Formatter.extend({
/**
* Formats the widget.
*
* @param {Object|Iterator} item - The item.
* @param {Object} formatParams - Contains format params passed by child class.
* @returns {Object} - The formatted widget.
*/
format: function (item, formatParams) {
var button = formatParams.asset;
if (!button) {
// Creates the new asset.
button = new Asset(this._getFormattedData(item, formatParams));
button.addClass('epg');
} else {
// Updated the Asset.
button.setDataItem(this._getFormattedData(item, formatParams));
button.setData();
}
return button;
},
/**
* Creates the object needed to build or update the asset.
*
* @param {Object} item - The epg content's data.
* @param {Object} params - Any params from the child class to be used.
* @returns {Object} - The params to build or update the asset.
* @private
*/
_getFormattedData: function (item, params) {
var formatParams = params || {},
isRecording = formatParams.isRecording,
imageDimension = formatParams.imageDimension || {},
start = isRecording ? null : new Date(item.getStartTime() * 1000),
end = isRecording ? null : new Date(item.getEndTime() * 1000),
assetWidth = Math.round(imageDimension.width || formatParams.width),
assetHeight = Math.round(imageDimension.height || formatParams.height),
progressbar = formatParams.progressbar,
topLeftText = formatParams.topLeftText,
title = item.getTitleBrief && item.getTitleBrief() || item.getTitle(),
subTitle = formatParams.subTitle || l10n.get('asset.epgDurationText', {
date: AppUtils.getDate(start, end),
duration: AppUtils.getHourMinutesText(item.getDuration())
}),
channelLogo = formatParams.channelLogo || item.getChannelLogo(),
bottomRightIcon = formatParams.bottomRightIcon,
topRightIcon = isRecording ? null : this._getTopRightIcon(item),
programBroadcast,
recordEvHandler = Utils.bind(this._onRecordingChange, this, item, params),
bottomRightIconClassnames = [];
if (bottomRightIcon) {
programBroadcast = this._getProgramBroadcast(start, end);
bottomRightIcon = '';
if (programBroadcast === PROGRAM_BROADCAST.LIVE) {
bottomRightIcon = {
textIcon: l10n.get('asset.live')
};
}
if (programBroadcast === PROGRAM_BROADCAST.PAST) {
bottomRightIcon = {};
if (this._canReplay(item)) {
bottomRightIcon.textIcon = l10n.get('asset.replay');
bottomRightIconClassnames.push('replay');
} else {
bottomRightIcon.classIcon = unableToPlayIcon;
}
}
}
return {
title: title,
subTitle: subTitle,
lockedTitle: l10n.get('asset.locked'),
logo: channelLogo,
background: item.getImageUrl(assetWidth + 'x' + assetHeight),
gradient: false,
progressbar: progressbar,
topLeftText: topLeftText,
topRightIcon: topRightIcon,
bottomRightIcon: bottomRightIcon,
startTime: start,
endTime: end,
classNames: {
widget: 'epg',
topLeftText: ['channel-number'],
bottomRightIcon: bottomRightIconClassnames
},
listeners: [
{
evName: '$record',
evHandler: recordEvHandler
}
],
item: item
};
},
/**
*
* @param {Object} item - The epg content's data.
* @returns {string} - The top right icon classname.
* @private
*/
_getTopRightIcon: function (item) {
var recordingResult = recordingManager.hasRecording(item.getId(), item.getSeriesId()),
topRightIcon;
if (recordingResult.series) {
topRightIcon = seriesRecording;
} else if (recordingResult.episode) {
topRightIcon = episodeRecording;
}
return topRightIcon;
},
/**
* Determines program's broadcast time.
*
* @param {Date} startTime - Program's start time.
* @param {Date} endTime - Program's end time.
* @returns {number} Return -1 for past program, 0 for live program, 1 for future program.
* @private
*/
_getProgramBroadcast: function (startTime, endTime) {
var now = app.getDate();
if (now > endTime) {
return PROGRAM_BROADCAST.PAST;
} else if (now < startTime) {
return PROGRAM_BROADCAST.FUTURE;
}
return PROGRAM_BROADCAST.LIVE;
},
/**
* Returns true if the video can be played.
*
* @param {Object} item - Item to check catchup availability.
* @returns {boolean} - True if the video can be played.
*/
_canReplay: function (item) {
return item.canCatchup() && featureManager.isReplayEnabled();
},
/**
* Gets executed when the recording state changes.
*
* @param {Object} item - The epg content's data.
* @param {Object} params - Any params from the child class to be used.
* @param {Object} asset - The asset instance to update.
* @param {Object} e - The event data.
* @private
*/
_onRecordingChange: function (item, params, asset, e) {
var data = item;
if (e.target === 'series' && e.id === data.getSeriesId() || e.target === 'episode' && e.id === data.getId()) {
params.asset = asset;
this.format(item, asset);
}
}
});
});