define('application/components/modals/record', [
'application/components/modals/abstract',
'rofl/events/keyevent',
'application/widgets/pointerfocusablebutton',
'rofl/widgets/horizontallist',
'rofl/widgets/label',
'antie/runtimecontext',
'rofl/lib/l10n',
'rofl/widgets/container',
'application/managers/recording',
'application/utils',
'rofl/lib/utils',
'application/models/production/series'
], function (
Modal,
KeyEvent,
Button,
HorizontalList,
Label,
RuntimeContext,
L10N,
Container,
RecordingManager,
AppUtils,
Utils,
Series
) {
'use strict';
var application = RuntimeContext.getCurrentApplication(),
recordingManager = RecordingManager.getInstance(),
l10n = L10N.getInstance();
return Modal.extend({
/**
* Initialises the modal.
*/
init: function init () {
init.base.call(this, 'record');
},
/**
* BeforeShow event.
*
* @param {Object} e - The event data.
* @param {Object} e.args - The event arguments.
* @param {Object} e.args.item - The record item.
* @param {Function} e.args.callback - The callback to execute once the modal has executed.
* @param {string} e.args.callingComponent - The component that should receive focus after closing.
*/
onBeforeShow: function (e) {
var args = e.args,
item = args.item,
flow = this._flow = args.flow || 'default';
application.hideLoader();
this._list.removeChildWidgets();
this._callback = args.callback;
this._callingComponent = args.callingComponent || 'player';
this._item = item;
this._setButtons(item);
this._list.setActiveChildIndex(0);
if (flow === 'default') {
this._title.setText(item.getTitle());
this._metadata.setText(this._getMetaData());
} else if (flow === 'delete') {
this._title.setText(l10n.get('recordings.modal.delete.title'));
this._metadata.setText(l10n.get('recordings.modal.delete.subtitle', {
item: item.getTitle()
}));
}
if (this._list.getChildWidgetCount() === 2) {
this._list.setActiveChildIndex(1);
}
this._list.focus();
this.showGoBackButton(false);
},
/**
* Sets the buttons.
*
* @param {Object} item - The current item.
* @private
*/
_setButtons: function (item) {
var list = this._list,
hasRecording,
isFutureRecording;
if (this._flow === 'default') {
if (item instanceof Series) {
hasRecording = recordingManager.hasRecording(null, item.getId());
if (hasRecording.series) {
list.appendChildWidget(this._getRemoveSeriesButton());
} else {
list.appendChildWidget(this._getRecordSeriesButton());
}
} else {
hasRecording = recordingManager.hasRecording(item.getId(), item.getSeriesId());
isFutureRecording = item.getEndTime() * 1000 > application.getDate();
if (hasRecording.episode) {
list.appendChildWidget(this._getRemoveEpisodesButton(isFutureRecording));
} else {
list.appendChildWidget(this._getRecordEpisodesButton());
}
if (item.getSeriesId() !== null && item.getSeriesId() !== undefined && !isNaN(item.getSeriesId())) {
if (hasRecording.series) {
list.appendChildWidget(this._getRemoveSeriesButton());
} else {
list.appendChildWidget(this._getRecordSeriesButton());
}
}
}
} else if (this._flow === 'delete') {
list.appendChildWidget(this._getCancelButton());
if (item instanceof Series) {
list.appendChildWidget(this._getRecordSeriesButton());
} else {
list.appendChildWidget(this._getRemoveEpisodesButton(false));
}
}
},
/**
* Creates the modal.
*
* @private
*/
_createModal: function () {
var title = this._title = new Label({ text: '', classNames: ['title'] }),
metadata = this._metadata = new Label({ text: '', classNames: ['metadata'] }),
buttonList = this._list = new HorizontalList(),
view,
overlay;
view = this._view = new Container();
view.addClass('modal-view');
overlay = this._overlay = new Container();
overlay.addClass('overlay');
view.appendChildWidget(title);
view.appendChildWidget(metadata);
view.appendChildWidget(buttonList);
this.appendChildWidget(overlay);
this.appendChildWidget(view);
},
/**
* KeyDown event.
*
* @param {Object} e - The event data.
* @private
*/
_onKeyDown: function (e) {
e.stopPropagation();
e.preventDefault();
if (e.keyCode === KeyEvent.VK_BACK) {
this.parentWidget.back();
}
},
/**
* Select event.
*
* @param {Object} e - The event data.
* @private
*/
_onSelect: function (e) {
var targetId = e.target.id;
if (targetId === 'record-episode') {
this._addEpisodeRecording();
} else if (targetId === 'record-series') {
this._addSeriesRecording();
} else if (targetId === 'remove-episode') {
this._removeEpisodeRecording();
} else if (targetId === 'remove-series') {
this._removeSeriesRecording();
} else if (targetId === 'cancel') {
this.parentWidget.back();
}
},
/**
* Adds an episode recording.
*
* @private
*/
_addEpisodeRecording: function () {
var self = this;
application.showLoader();
recordingManager.addEpisodeRecording(this._item.getId())
.then(function () {
self._executeCallback('episode');
})
['catch'](function () {
self._onBack();
});
},
/**
* Adds a series recording.
*
* @private
*/
_addSeriesRecording: function () {
var self = this;
application.showLoader();
recordingManager.addSeriesRecording(
this._item.getSeriesId(),
this._item.getChannelId()
)
.then(function () {
self._executeCallback('series');
})
['catch'](function () {
self._onBack();
});
},
/**
* Removes an episode recording.
*
* @private
*/
_removeEpisodeRecording: function () {
var self = this;
application.showLoader();
recordingManager.deleteEpisodeRecording(this._item)
.then(function () {
self._executeCallback('episode', true);
})
['catch'](function () {
self._onBack();
});
},
/**
* Removes a series recording.
*
* @private
*/
_removeSeriesRecording: function () {
var self = this;
application.showLoader();
recordingManager.deleteSeriesRecording(
this._item.getSeriesId()
)
.then(function () {
self._executeCallback('series', true);
})
['catch'](function () {
self._onBack();
});
},
/**
* Goes back to the previous page.
*
* @private
*/
_onBack: function () {
application.hideLoader();
this.parentWidget.back();
application.getComponent(this._callingComponent).focus();
},
/**
* Returns the metadata line.
*
* @returns {string} - The metadata.
* @private
*/
_getMetaData: function () {
var item = this._item;
if (item instanceof Series) {
item = item.getItems()[0];
}
return AppUtils.getRecordDateSubtitle(
new Date(item.getStartTime() * 1000),
new Date(item.getEndTime() * 1000),
item.getDuration());
},
/**
* Executes the callback.
*
* @param {string} type - The record type.
* @param {boolean} removed - True if the recording got removed.
* @private
*/
_executeCallback: function (type, removed) {
this._onBack();
if (Utils.isFunction(this._callback)) {
this._callback({
type: type,
item: this._item,
removed: removed
});
}
},
/**
* Returns the record series button.
*
* @returns {Object} - The button.
* @private
*/
_getRecordSeriesButton: function () {
return this._getButton(
'record-series',
'recordings.modal.series',
'icon-record-all'
);
},
/**
* Returns the record episodes button.
*
* @returns {Object} - The button.
* @private
*/
_getRecordEpisodesButton: function () {
return this._getButton(
'record-episode',
'recordings.modal.episode',
'icon-record-new'
);
},
/**
* Returns the remove series button.
*
* @returns {Object} - The button.
* @private
*/
_getRemoveSeriesButton: function () {
return this._getButton(
'remove-series',
'recordings.modal.cancel_series',
'icon-record-unable'
);
},
/**
* Returns the remove episodes button.
*
* @param {boolean} isFutureRecording - True if the recording is a future recoding.
* @returns {Object} - The button.
* @private
*/
_getRemoveEpisodesButton: function (isFutureRecording) {
var title = isFutureRecording ? 'recordings.modal.cancel_episode'
: 'recordings.modal.remove_episode',
icon = isFutureRecording ? 'icon-record-unable' : 'icon-delete',
button = this._getButton(
'remove-episode',
title,
icon
);
button.addClass(isFutureRecording ? 'future' : 'old');
return button;
},
/**
* Returns the cancel button.
*
* @returns {Object} - The button.
* @private
*/
_getCancelButton: function () {
return this._getButton(
'cancel',
'recordings.modal.cancel'
);
},
/**
* Returns a button.
*
* @param {string} id - The button id.
* @param {string} title - The title.
* @param {string} [icon] - The remove icon.
* @returns {Object} - The button.
* @private
*/
_getButton: function (id, title, icon) {
var button = new Button(id),
label = new Label({ text: l10n.get(title), classNames: ['v-align-target'] }),
iconLabel = new Label({ text: '', classNames: ['icon', 'v-align-target', icon] }),
helper = new Label({ text: '', classNames: ['v-align-helper'] });
button.appendChildWidget(helper);
if (icon) {
button.appendChildWidget(iconLabel);
}
button.appendChildWidget(label);
button.addClass('icon-button');
return button;
}
});
});