2013-06-25 07:43:16 +03:00
|
|
|
'use strict';
|
2013-06-25 02:41:59 +03:00
|
|
|
define(
|
|
|
|
[
|
2013-09-03 08:02:47 +03:00
|
|
|
'app',
|
2013-06-25 02:41:59 +03:00
|
|
|
'marionette',
|
|
|
|
'backgrid',
|
|
|
|
'Cells/ToggleCell',
|
|
|
|
'Cells/EpisodeTitleCell',
|
2013-07-26 05:09:17 +03:00
|
|
|
'Cells/RelativeDateCell',
|
2013-06-25 02:41:59 +03:00
|
|
|
'Cells/EpisodeStatusCell',
|
2013-09-05 08:01:22 +03:00
|
|
|
'Shared/Actioneer',
|
|
|
|
'moment'
|
|
|
|
], function (App, Marionette, Backgrid, ToggleCell, EpisodeTitleCell, RelativeDateCell, EpisodeStatusCell, Actioneer, Moment) {
|
2013-06-25 02:41:59 +03:00
|
|
|
return Marionette.Layout.extend({
|
2013-06-09 23:51:32 +03:00
|
|
|
template: 'Series/Details/SeasonLayoutTemplate',
|
2013-04-23 05:07:21 +03:00
|
|
|
|
2013-06-24 07:37:55 +03:00
|
|
|
ui: {
|
2013-07-10 05:11:00 +03:00
|
|
|
seasonSearch : '.x-season-search',
|
2013-07-19 08:23:04 +03:00
|
|
|
seasonMonitored: '.x-season-monitored',
|
|
|
|
seasonRename : '.x-season-rename'
|
2013-06-24 07:37:55 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
events: {
|
2013-09-05 08:01:22 +03:00
|
|
|
'click .x-season-search' : '_seasonSearch',
|
|
|
|
'click .x-season-monitored' : '_seasonMonitored',
|
|
|
|
'click .x-season-rename' : '_seasonRename',
|
|
|
|
'click .x-show-hide-episodes' : '_showHideEpisodes',
|
|
|
|
'dblclick .series-season h2' : '_showHideEpisodes'
|
2013-06-24 07:37:55 +03:00
|
|
|
},
|
|
|
|
|
2013-06-09 23:51:32 +03:00
|
|
|
regions: {
|
2013-09-05 08:01:22 +03:00
|
|
|
episodeGrid: '.x-episode-grid'
|
2013-06-09 23:51:32 +03:00
|
|
|
},
|
2013-04-23 05:07:21 +03:00
|
|
|
|
2013-06-25 02:41:59 +03:00
|
|
|
columns:
|
|
|
|
[
|
|
|
|
{
|
2013-07-09 18:41:42 +03:00
|
|
|
name : 'monitored',
|
2013-06-25 02:41:59 +03:00
|
|
|
label : '',
|
|
|
|
cell : ToggleCell,
|
2013-07-09 18:41:42 +03:00
|
|
|
trueClass : 'icon-bookmark',
|
|
|
|
falseClass: 'icon-bookmark-empty',
|
2013-07-22 02:46:45 +03:00
|
|
|
tooltip : 'Toggle monitored status',
|
|
|
|
sortable : false
|
2013-06-25 02:41:59 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name : 'episodeNumber',
|
|
|
|
label: '#',
|
|
|
|
cell : Backgrid.IntegerCell.extend({
|
|
|
|
className: 'episode-number-cell'
|
|
|
|
})
|
|
|
|
},
|
|
|
|
{
|
2013-07-22 02:46:45 +03:00
|
|
|
name : 'this',
|
|
|
|
label : 'Title',
|
2013-08-22 00:47:39 +03:00
|
|
|
hideSeriesLink : true,
|
2013-07-22 02:46:45 +03:00
|
|
|
cell : EpisodeTitleCell,
|
|
|
|
sortable: false
|
2013-06-25 02:41:59 +03:00
|
|
|
},
|
|
|
|
{
|
2013-07-24 08:28:06 +03:00
|
|
|
name : 'airDateUtc',
|
2013-06-25 02:41:59 +03:00
|
|
|
label: 'Air Date',
|
2013-07-26 05:09:17 +03:00
|
|
|
cell : RelativeDateCell
|
2013-06-25 02:41:59 +03:00
|
|
|
} ,
|
|
|
|
{
|
2013-07-22 02:46:45 +03:00
|
|
|
name : 'status',
|
|
|
|
label : 'Status',
|
|
|
|
cell : EpisodeStatusCell,
|
|
|
|
sortable: false
|
2013-06-25 02:41:59 +03:00
|
|
|
}
|
|
|
|
],
|
2013-04-23 05:07:21 +03:00
|
|
|
|
2013-06-09 23:51:32 +03:00
|
|
|
initialize: function (options) {
|
2013-06-01 22:31:39 +03:00
|
|
|
|
2013-06-09 23:51:32 +03:00
|
|
|
if (!options.episodeCollection) {
|
|
|
|
throw 'episodeCollection is needed';
|
|
|
|
}
|
2013-06-01 22:31:39 +03:00
|
|
|
|
2013-09-06 09:18:25 +03:00
|
|
|
this.templateHelpers = {};
|
2013-06-09 23:51:32 +03:00
|
|
|
this.episodeCollection = options.episodeCollection.bySeason(this.model.get('seasonNumber'));
|
2013-09-03 08:02:47 +03:00
|
|
|
this.series = options.series;
|
2013-06-29 01:12:57 +03:00
|
|
|
|
2013-09-05 08:01:22 +03:00
|
|
|
this.showingEpisodes = this._shouldShowEpisodes();
|
2013-09-05 09:33:06 +03:00
|
|
|
this._generateTemplateHelpers();
|
2013-09-05 08:01:22 +03:00
|
|
|
|
2013-08-19 18:12:35 +03:00
|
|
|
this.listenTo(this.model, 'sync', function () {
|
2013-08-01 09:32:36 +03:00
|
|
|
this._afterSeasonMonitored();
|
|
|
|
}, this);
|
|
|
|
|
2013-08-19 18:12:35 +03:00
|
|
|
this.listenTo(this.episodeCollection, 'sync', function () {
|
2013-07-19 04:49:04 +03:00
|
|
|
this.render();
|
|
|
|
}, this);
|
2013-06-09 23:51:32 +03:00
|
|
|
},
|
2013-04-23 05:07:21 +03:00
|
|
|
|
2013-07-10 05:11:00 +03:00
|
|
|
onRender: function () {
|
2013-09-05 08:01:22 +03:00
|
|
|
if (this.showingEpisodes) {
|
|
|
|
this._showEpisodes();
|
|
|
|
}
|
2013-07-10 05:11:00 +03:00
|
|
|
|
|
|
|
this._setSeasonMonitoredState();
|
2013-06-24 07:37:55 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
_seasonSearch: function () {
|
2013-07-22 05:23:17 +03:00
|
|
|
Actioneer.ExecuteCommand({
|
2013-07-22 05:52:53 +03:00
|
|
|
command : 'seasonSearch',
|
|
|
|
properties : {
|
2013-07-22 05:23:17 +03:00
|
|
|
seriesId : this.model.get('seriesId'),
|
|
|
|
seasonNumber: this.model.get('seasonNumber')
|
|
|
|
},
|
2013-08-31 06:08:19 +03:00
|
|
|
element : this.ui.seasonSearch,
|
|
|
|
errorMessage : 'Search for season {0} failed'.format(this.model.get('seasonNumber')),
|
|
|
|
startMessage : 'Search for season {0} started'.format(this.model.get('seasonNumber')),
|
|
|
|
successMessage: 'Search for season {0} completed'.format(this.model.get('seasonNumber'))
|
2013-06-24 07:37:55 +03:00
|
|
|
});
|
2013-07-10 05:11:00 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
_seasonMonitored: function () {
|
|
|
|
var name = 'monitored';
|
|
|
|
this.model.set(name, !this.model.get(name));
|
|
|
|
|
2013-07-22 05:23:17 +03:00
|
|
|
Actioneer.SaveModel({
|
2013-08-08 10:38:14 +03:00
|
|
|
context: this,
|
|
|
|
element: this.ui.seasonMonitored,
|
|
|
|
always : this._afterSeasonMonitored
|
2013-07-22 05:23:17 +03:00
|
|
|
});
|
|
|
|
},
|
2013-07-10 05:11:00 +03:00
|
|
|
|
2013-07-22 05:23:17 +03:00
|
|
|
_afterSeasonMonitored: function () {
|
|
|
|
var self = this;
|
2013-07-10 05:11:00 +03:00
|
|
|
|
2013-07-22 05:23:17 +03:00
|
|
|
_.each(this.episodeCollection.models, function (episode) {
|
|
|
|
episode.set({ monitored: self.model.get('monitored') });
|
2013-07-10 05:11:00 +03:00
|
|
|
});
|
2013-07-22 05:23:17 +03:00
|
|
|
|
|
|
|
this.render();
|
2013-07-10 05:11:00 +03: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 08:23:04 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
_seasonRename: function () {
|
2013-07-22 05:23:17 +03:00
|
|
|
Actioneer.ExecuteCommand({
|
2013-08-31 06:08:19 +03:00
|
|
|
command : 'renameSeason',
|
|
|
|
properties : {
|
2013-07-22 05:23:17 +03:00
|
|
|
seriesId : this.model.get('seriesId'),
|
|
|
|
seasonNumber: this.model.get('seasonNumber')
|
|
|
|
},
|
2013-08-31 06:08:19 +03:00
|
|
|
element : this.ui.seasonRename,
|
2013-09-03 17:18:04 +03:00
|
|
|
errorMessage: 'Season rename failed',
|
|
|
|
context : this,
|
|
|
|
onSuccess : this._afterRename
|
2013-07-19 08:23:04 +03:00
|
|
|
});
|
2013-09-03 08:02:47 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
_afterRename: function () {
|
|
|
|
App.vent.trigger(App.Events.SeasonRenamed, { series: this.series, seasonNumber: this.model.get('seasonNumber') });
|
2013-09-05 08:01:22 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
_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);
|
|
|
|
|
|
|
|
var result = this.episodeCollection.some(function(episode) {
|
|
|
|
|
|
|
|
var airDate = episode.get('airDateUtc');
|
|
|
|
|
|
|
|
if (airDate)
|
|
|
|
{
|
|
|
|
var airDateMoment = Moment(airDate);
|
|
|
|
|
|
|
|
if (airDateMoment.isAfter(startDate) && airDateMoment.isBefore(endDate)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
|
2013-09-05 09:33:06 +03:00
|
|
|
_generateTemplateHelpers: function () {
|
|
|
|
this.templateHelpers.showingEpisodes = this.showingEpisodes;
|
|
|
|
|
|
|
|
var episodeCount = this.episodeCollection.filter(function (episode) {
|
|
|
|
return (episode.get('monitored') && Moment(episode.get('airDateUtc')).isBefore(Moment())) || episode.get('hasFile');
|
|
|
|
}).length;
|
|
|
|
|
|
|
|
var episodeFileCount = this.episodeCollection.where({ hasFile: true }).length;
|
|
|
|
var percentOfEpisodes = 100;
|
|
|
|
|
|
|
|
if (episodeCount > 0) {
|
|
|
|
percentOfEpisodes = episodeFileCount / episodeCount * 100;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.templateHelpers.episodeCount = episodeCount;
|
|
|
|
this.templateHelpers.episodeFileCount = episodeFileCount;
|
|
|
|
this.templateHelpers.percentOfEpisodes = percentOfEpisodes;
|
|
|
|
},
|
|
|
|
|
2013-09-05 08:01:22 +03:00
|
|
|
_showHideEpisodes: function () {
|
|
|
|
if (this.showingEpisodes) {
|
|
|
|
this.showingEpisodes = false;
|
|
|
|
this.episodeGrid.$el.slideUp();
|
|
|
|
this.episodeGrid.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
this.showingEpisodes = true;
|
|
|
|
this._showEpisodes();
|
|
|
|
this.episodeGrid.$el.slideDown();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.templateHelpers.showingEpisodes = this.showingEpisodes;
|
|
|
|
this.render();
|
2013-06-09 23:51:32 +03:00
|
|
|
}
|
|
|
|
});
|
2013-04-23 05:07:21 +03:00
|
|
|
});
|