sonarr-repo-only/UI/Series/Details/SeasonLayout.js

243 lines
8.5 KiB
JavaScript
Raw Normal View History

2013-06-25 04:43:16 +00:00
'use strict';
define(
[
'app',
'marionette',
'backgrid',
'Cells/ToggleCell',
'Cells/EpisodeTitleCell',
'Cells/RelativeDateCell',
'Cells/EpisodeStatusCell',
'Cells/EpisodeActionsCell',
2013-09-12 00:42:15 +00:00
'Commands/CommandController',
'moment'
], function (App, Marionette, Backgrid, ToggleCell, EpisodeTitleCell, RelativeDateCell, EpisodeStatusCell, EpisodeActionsCell, CommandController, Moment) {
return Marionette.Layout.extend({
template: 'Series/Details/SeasonLayoutTemplate',
2013-04-23 02:07:21 +00:00
2013-06-24 04:37:55 +00:00
ui: {
2013-07-10 02:11:00 +00:00
seasonSearch : '.x-season-search',
2013-07-19 05:23:04 +00:00
seasonMonitored: '.x-season-monitored',
seasonRename : '.x-season-rename'
2013-06-24 04:37:55 +00:00
},
events: {
2013-09-12 00:42:15 +00:00
'click .x-season-monitored' : '_seasonMonitored',
'click .x-season-search' : '_seasonSearch',
'click .x-season-rename' : '_seasonRename',
'click .x-show-hide-episodes': '_showHideEpisodes',
'dblclick .series-season h2' : '_showHideEpisodes'
2013-06-24 04:37:55 +00:00
},
regions: {
episodeGrid: '.x-episode-grid'
},
2013-04-23 02:07:21 +00:00
columns:
[
{
name : 'monitored',
label : '',
cell : ToggleCell,
trueClass : 'icon-bookmark',
falseClass: 'icon-bookmark-empty',
2013-07-21 23:46:45 +00:00
tooltip : 'Toggle monitored status',
sortable : false
},
{
name : 'episodeNumber',
label: '#',
cell : Backgrid.IntegerCell.extend({
className: 'episode-number-cell'
})
},
{
2013-09-12 00:42:15 +00:00
name : 'this',
label : 'Title',
hideSeriesLink: true,
cell : EpisodeTitleCell,
sortable : false
},
{
2013-07-24 05:28:06 +00:00
name : 'airDateUtc',
label: 'Air Date',
cell : RelativeDateCell
} ,
{
2013-07-21 23:46:45 +00:00
name : 'status',
label : 'Status',
cell : EpisodeStatusCell,
sortable: false
},
{
name : 'this',
label : '',
cell : EpisodeActionsCell,
sortable: false
}
],
2013-04-23 02:07:21 +00:00
initialize: function (options) {
if (!options.episodeCollection) {
throw 'episodeCollection is needed';
}
2013-09-12 02:31:45 +00:00
this.episodeCollection = options.episodeCollection.bySeason(this.model.get('seasonNumber'));
this.series = options.series;
this.showingEpisodes = this._shouldShowEpisodes();
2013-09-12 00:42:15 +00:00
this.listenTo(this.model, 'sync', this._afterSeasonMonitored);
this.listenTo(this.episodeCollection, 'sync', this.render);
},
2013-04-23 02:07:21 +00:00
2013-07-10 02:11:00 +00:00
onRender: function () {
2013-09-12 00:42:15 +00:00
if (this.showingEpisodes) {
this._showEpisodes();
}
2013-07-10 02:11:00 +00:00
this._setSeasonMonitoredState();
2013-09-12 00:42:15 +00:00
CommandController.bindToCommand({
element: this.ui.seasonSearch,
command: {
2013-09-12 02:31:45 +00:00
name : 'seasonSearch',
2013-09-12 00:42:15 +00:00
seriesId : this.series.id,
seasonNumber: this.model.get('seasonNumber')
}
});
CommandController.bindToCommand({
element: this.ui.seasonRename,
command: {
2013-09-12 02:31:45 +00:00
name : 'renameSeason',
2013-09-12 00:42:15 +00:00
seriesId : this.series.id,
seasonNumber: this.model.get('seasonNumber')
}
});
2013-06-24 04:37:55 +00:00
},
_seasonSearch: function () {
2013-09-12 00:42:15 +00:00
CommandController.Execute('seasonSearch', {
2013-09-12 02:31:45 +00:00
name : 'seasonSearch',
2013-09-12 00:42:15 +00:00
seriesId : this.series.id,
seasonNumber: this.model.get('seasonNumber')
});
},
_seasonRename: function () {
CommandController.Execute('renameSeason', {
2013-09-12 02:31:45 +00:00
name : 'renameSeason',
2013-09-12 00:42:15 +00:00
seriesId : this.series.id,
seasonNumber: this.model.get('seasonNumber')
2013-06-24 04:37:55 +00:00
});
2013-07-10 02:11:00 +00:00
},
_seasonMonitored: function () {
var name = 'monitored';
this.model.set(name, !this.model.get(name));
2013-09-10 05:22:38 +00:00
this.series.setSeasonMonitored(this.model.get('seasonNumber'));
2013-07-10 02:11:00 +00:00
2013-09-12 02:31:45 +00:00
var savePromise = this.series.save().always(this._afterSeasonMonitored.bind(this));
2013-09-12 00:42:15 +00:00
this.ui.seasonMonitored.spinForPromise(savePromise);
},
2013-07-10 02:11:00 +00:00
_afterSeasonMonitored: function () {
var self = this;
2013-07-10 02:11:00 +00:00
_.each(this.episodeCollection.models, function (episode) {
episode.set({ monitored: self.model.get('monitored') });
2013-07-10 02:11:00 +00:00
});
this.render();
2013-07-10 02:11:00 +00:00
},
_setSeasonMonitoredState: function () {
this.ui.seasonMonitored.removeClass('icon-spinner icon-spin');
if (this.model.get('monitored')) {
this.ui.seasonMonitored.addClass('icon-bookmark');
this.ui.seasonMonitored.removeClass('icon-bookmark-empty');
}
else {
this.ui.seasonMonitored.addClass('icon-bookmark-empty');
this.ui.seasonMonitored.removeClass('icon-bookmark');
}
2013-07-19 05:23:04 +00:00
},
2013-09-12 00:42:15 +00:00
_afterRename: function () {
App.vent.trigger(App.Events.SeasonRenamed, { series: this.series, seasonNumber: this.model.get('seasonNumber') });
},
_showEpisodes: function () {
this.episodeGrid.show(new Backgrid.Grid({
columns : this.columns,
collection: this.episodeCollection,
className : 'table table-hover season-grid'
}));
},
_shouldShowEpisodes: function () {
var startDate = Moment().add('month', -1);
var endDate = Moment().add('year', 1);
2013-09-12 02:31:45 +00:00
return this.episodeCollection.some(function (episode) {
var airDate = episode.get('airDateUtc');
2013-09-12 00:42:15 +00:00
if (airDate) {
var airDateMoment = Moment(airDate);
if (airDateMoment.isAfter(startDate) && airDateMoment.isBefore(endDate)) {
return true;
}
}
return false;
});
},
2013-09-12 02:31:45 +00:00
templateHelpers: function () {
var episodeCount = this.episodeCollection.filter(function (episode) {
2013-09-12 02:31:45 +00:00
return episode.get('hasFile') || (episode.get('monitored') && Moment(episode.get('airDateUtc')).isBefore(Moment()));
}).length;
var episodeFileCount = this.episodeCollection.where({ hasFile: true }).length;
var percentOfEpisodes = 100;
if (episodeCount > 0) {
percentOfEpisodes = episodeFileCount / episodeCount * 100;
}
2013-09-12 02:31:45 +00:00
return {
showingEpisodes : this.showingEpisodes,
episodeCount : episodeCount,
episodeFileCount : episodeFileCount,
percentOfEpisodes: percentOfEpisodes
};
},
_showHideEpisodes: function () {
if (this.showingEpisodes) {
this.showingEpisodes = false;
this.episodeGrid.close();
}
else {
this.showingEpisodes = true;
this._showEpisodes();
}
this.templateHelpers.showingEpisodes = this.showingEpisodes;
this.render();
}
});
2013-04-23 02:07:21 +00:00
});