Source: formatters/epgasset.js

  1. define('application/formatters/epgasset', [
  2. 'rofl/lib/l10n',
  3. 'antie/formatter',
  4. 'antie/iterator',
  5. 'application/widgets/asset/asset',
  6. 'application/utils',
  7. 'application/managers/recording',
  8. 'application/managers/feature',
  9. 'antie/runtimecontext',
  10. 'rofl/lib/utils'
  11. ], function (
  12. L10N,
  13. Formatter,
  14. Iterator,
  15. Asset,
  16. AppUtils,
  17. RecordingManager,
  18. FeatureManager,
  19. RuntimeContext,
  20. Utils
  21. ) {
  22. 'use strict';
  23. var featureManager = FeatureManager.getInstance(),
  24. recordingManager = RecordingManager.getInstance(),
  25. episodeRecording = 'icon-record-new',
  26. seriesRecording = 'icon-record-all',
  27. unableToPlayIcon = 'icon-unable-play',
  28. l10n = L10N.getInstance(),
  29. app = RuntimeContext.getCurrentApplication(),
  30. PROGRAM_BROADCAST = {
  31. PAST: -1,
  32. LIVE: 0,
  33. FUTURE: 1
  34. };
  35. return Formatter.extend({
  36. /**
  37. * Formats the widget.
  38. *
  39. * @param {Object|Iterator} item - The item.
  40. * @param {Object} formatParams - Contains format params passed by child class.
  41. * @returns {Object} - The formatted widget.
  42. */
  43. format: function (item, formatParams) {
  44. var button = formatParams.asset;
  45. if (!button) {
  46. // Creates the new asset.
  47. button = new Asset(this._getFormattedData(item, formatParams));
  48. button.addClass('epg');
  49. } else {
  50. // Updated the Asset.
  51. button.setDataItem(this._getFormattedData(item, formatParams));
  52. button.setData();
  53. }
  54. return button;
  55. },
  56. /**
  57. * Creates the object needed to build or update the asset.
  58. *
  59. * @param {Object} item - The epg content's data.
  60. * @param {Object} params - Any params from the child class to be used.
  61. * @returns {Object} - The params to build or update the asset.
  62. * @private
  63. */
  64. _getFormattedData: function (item, params) {
  65. var formatParams = params || {},
  66. isRecording = formatParams.isRecording,
  67. imageDimension = formatParams.imageDimension || {},
  68. start = isRecording ? null : new Date(item.getStartTime() * 1000),
  69. end = isRecording ? null : new Date(item.getEndTime() * 1000),
  70. assetWidth = Math.round(imageDimension.width || formatParams.width),
  71. assetHeight = Math.round(imageDimension.height || formatParams.height),
  72. progressbar = formatParams.progressbar,
  73. topLeftText = formatParams.topLeftText,
  74. title = item.getTitleBrief && item.getTitleBrief() || item.getTitle(),
  75. subTitle = formatParams.subTitle || l10n.get('asset.epgDurationText', {
  76. date: AppUtils.getDate(start, end),
  77. duration: AppUtils.getHourMinutesText(item.getDuration())
  78. }),
  79. channelLogo = formatParams.channelLogo || item.getChannelLogo(),
  80. bottomRightIcon = formatParams.bottomRightIcon,
  81. topRightIcon = isRecording ? null : this._getTopRightIcon(item),
  82. programBroadcast,
  83. recordEvHandler = Utils.bind(this._onRecordingChange, this, item, params),
  84. bottomRightIconClassnames = [];
  85. if (bottomRightIcon) {
  86. programBroadcast = this._getProgramBroadcast(start, end);
  87. bottomRightIcon = '';
  88. if (programBroadcast === PROGRAM_BROADCAST.LIVE) {
  89. bottomRightIcon = {
  90. textIcon: l10n.get('asset.live')
  91. };
  92. }
  93. if (programBroadcast === PROGRAM_BROADCAST.PAST) {
  94. bottomRightIcon = {};
  95. if (this._canReplay(item)) {
  96. bottomRightIcon.textIcon = l10n.get('asset.replay');
  97. bottomRightIconClassnames.push('replay');
  98. } else {
  99. bottomRightIcon.classIcon = unableToPlayIcon;
  100. }
  101. }
  102. }
  103. return {
  104. title: title,
  105. subTitle: subTitle,
  106. lockedTitle: l10n.get('asset.locked'),
  107. logo: channelLogo,
  108. background: item.getImageUrl(assetWidth + 'x' + assetHeight),
  109. gradient: false,
  110. progressbar: progressbar,
  111. topLeftText: topLeftText,
  112. topRightIcon: topRightIcon,
  113. bottomRightIcon: bottomRightIcon,
  114. startTime: start,
  115. endTime: end,
  116. classNames: {
  117. widget: 'epg',
  118. topLeftText: ['channel-number'],
  119. bottomRightIcon: bottomRightIconClassnames
  120. },
  121. listeners: [
  122. {
  123. evName: '$record',
  124. evHandler: recordEvHandler
  125. }
  126. ],
  127. item: item
  128. };
  129. },
  130. /**
  131. *
  132. * @param {Object} item - The epg content's data.
  133. * @returns {string} - The top right icon classname.
  134. * @private
  135. */
  136. _getTopRightIcon: function (item) {
  137. var recordingResult = recordingManager.hasRecording(item.getId(), item.getSeriesId()),
  138. topRightIcon;
  139. if (recordingResult.series) {
  140. topRightIcon = seriesRecording;
  141. } else if (recordingResult.episode) {
  142. topRightIcon = episodeRecording;
  143. }
  144. return topRightIcon;
  145. },
  146. /**
  147. * Determines program's broadcast time.
  148. *
  149. * @param {Date} startTime - Program's start time.
  150. * @param {Date} endTime - Program's end time.
  151. * @returns {number} Return -1 for past program, 0 for live program, 1 for future program.
  152. * @private
  153. */
  154. _getProgramBroadcast: function (startTime, endTime) {
  155. var now = app.getDate();
  156. if (now > endTime) {
  157. return PROGRAM_BROADCAST.PAST;
  158. } else if (now < startTime) {
  159. return PROGRAM_BROADCAST.FUTURE;
  160. }
  161. return PROGRAM_BROADCAST.LIVE;
  162. },
  163. /**
  164. * Returns true if the video can be played.
  165. *
  166. * @param {Object} item - Item to check catchup availability.
  167. * @returns {boolean} - True if the video can be played.
  168. */
  169. _canReplay: function (item) {
  170. return item.canCatchup() && featureManager.isReplayEnabled();
  171. },
  172. /**
  173. * Gets executed when the recording state changes.
  174. *
  175. * @param {Object} item - The epg content's data.
  176. * @param {Object} params - Any params from the child class to be used.
  177. * @param {Object} asset - The asset instance to update.
  178. * @param {Object} e - The event data.
  179. * @private
  180. */
  181. _onRecordingChange: function (item, params, asset, e) {
  182. var data = item;
  183. if (e.target === 'series' && e.id === data.getSeriesId() || e.target === 'episode' && e.id === data.getId()) {
  184. params.asset = asset;
  185. this.format(item, asset);
  186. }
  187. }
  188. });
  189. });