sonarr-repo-only/UI/SeasonPass/SeriesLayout.js

124 lines
3.7 KiB
JavaScript
Raw Normal View History

'use strict';
define(
[
'marionette',
2013-09-12 00:42:15 +00:00
'backgrid',
'Series/SeasonCollection'
], function (Marionette, Backgrid, SeasonCollection) {
return Marionette.Layout.extend({
template: 'SeasonPass/SeriesLayoutTemplate',
ui: {
seasonSelect: '.x-season-select',
expander : '.x-expander',
seasonGrid : '.x-season-grid'
},
events: {
'change .x-season-select': '_seasonSelected',
2013-08-06 06:17:46 +00:00
'click .x-expander' : '_expand',
2013-09-10 05:22:38 +00:00
'click .x-latest' : '_latest',
'click .x-monitored' : '_toggleSeasonMonitored'
},
regions: {
seasonGrid: '.x-season-grid'
},
2013-09-10 05:22:38 +00:00
initialize: function () {
this.seasonCollection = new SeasonCollection(this.model.get('seasons'));
this.expanded = false;
},
onRender: function () {
if (!this.expanded) {
2013-09-10 05:22:38 +00:00
this.ui.seasonGrid.hide();
}
this._setExpanderIcon();
},
_seasonSelected: function () {
var seasonNumber = parseInt(this.ui.seasonSelect.val());
2013-08-06 06:17:46 +00:00
if (seasonNumber == -1 || isNaN(seasonNumber)) {
return;
}
2013-08-06 06:17:46 +00:00
this._setMonitored(seasonNumber)
},
_expand: function () {
if (this.expanded) {
this.ui.seasonGrid.slideUp();
this.expanded = false;
}
else {
this.ui.seasonGrid.slideDown();
this.expanded = true;
}
this._setExpanderIcon();
},
_setExpanderIcon: function () {
if (this.expanded) {
this.ui.expander.removeClass('icon-chevron-right');
this.ui.expander.addClass('icon-chevron-down');
}
else {
this.ui.expander.removeClass('icon-chevron-down');
this.ui.expander.addClass('icon-chevron-right');
}
2013-08-06 06:17:46 +00:00
},
_latest: function () {
2013-09-10 05:22:38 +00:00
var season = _.max(this.model.get('seasons'), function (s) {
return s.seasonNumber;
2013-08-06 06:17:46 +00:00
});
2013-09-10 05:22:38 +00:00
this._setMonitored(season.seasonNumber);
2013-08-06 06:17:46 +00:00
},
_setMonitored: function (seasonNumber) {
var self = this;
2013-09-10 05:22:38 +00:00
this.model.setSeasonPass(seasonNumber);
var promise = this.model.save();
2013-08-06 06:17:46 +00:00
promise.done(function (data) {
self.seasonCollection = new SeasonCollection(data);
self.render();
});
2013-09-10 05:22:38 +00:00
},
_toggleSeasonMonitored: function (e) {
var seasonNumber = 0;
var element;
if (e.target.localName === 'i') {
seasonNumber = parseInt($(e.target).parent('td').attr('data-season-number'));
element = $(e.target);
}
else {
seasonNumber = parseInt($(e.target).attr('data-season-number'));
element = $(e.target).children('i');
}
this.model.setSeasonMonitored(seasonNumber);
2013-09-12 00:42:15 +00:00
var savePromise =this.model.save()
.always(this.render.bind(this));
element.spinForPromise(savePromise);
2013-09-10 05:22:38 +00:00
},
_afterToggleSeasonMonitored: function () {
this.render();
}
});
});