2013-08-05 12:09:41 +03:00
|
|
|
'use strict';
|
|
|
|
define(
|
|
|
|
[
|
|
|
|
'marionette',
|
2013-09-12 03:42:15 +03:00
|
|
|
'backgrid',
|
|
|
|
'Series/SeasonCollection'
|
|
|
|
], function (Marionette, Backgrid, SeasonCollection) {
|
2013-08-05 12:09:41 +03:00
|
|
|
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 09:17:46 +03:00
|
|
|
'click .x-expander' : '_expand',
|
2013-09-10 08:22:38 +03:00
|
|
|
'click .x-latest' : '_latest',
|
|
|
|
'click .x-monitored' : '_toggleSeasonMonitored'
|
2013-08-05 12:09:41 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
regions: {
|
|
|
|
seasonGrid: '.x-season-grid'
|
|
|
|
},
|
|
|
|
|
2013-09-10 08:22:38 +03:00
|
|
|
initialize: function () {
|
|
|
|
this.seasonCollection = new SeasonCollection(this.model.get('seasons'));
|
2013-08-05 12:09:41 +03:00
|
|
|
this.expanded = false;
|
|
|
|
},
|
|
|
|
|
|
|
|
onRender: function () {
|
|
|
|
if (!this.expanded) {
|
2013-09-10 08:22:38 +03:00
|
|
|
this.ui.seasonGrid.hide();
|
2013-08-05 12:09:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
this._setExpanderIcon();
|
|
|
|
},
|
|
|
|
|
|
|
|
_seasonSelected: function () {
|
|
|
|
var seasonNumber = parseInt(this.ui.seasonSelect.val());
|
|
|
|
|
2013-08-06 09:17:46 +03:00
|
|
|
if (seasonNumber == -1 || isNaN(seasonNumber)) {
|
2013-08-05 12:09:41 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-08-06 09:17:46 +03:00
|
|
|
this._setMonitored(seasonNumber)
|
2013-08-05 12:09:41 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
_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 09:17:46 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
_latest: function () {
|
2013-09-10 08:22:38 +03:00
|
|
|
var season = _.max(this.model.get('seasons'), function (s) {
|
|
|
|
return s.seasonNumber;
|
2013-08-06 09:17:46 +03:00
|
|
|
});
|
|
|
|
|
2013-09-10 08:22:38 +03:00
|
|
|
this._setMonitored(season.seasonNumber);
|
2013-08-06 09:17:46 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
_setMonitored: function (seasonNumber) {
|
|
|
|
var self = this;
|
|
|
|
|
2013-09-10 08:22:38 +03:00
|
|
|
this.model.setSeasonPass(seasonNumber);
|
|
|
|
|
|
|
|
var promise = this.model.save();
|
2013-08-06 09:17:46 +03:00
|
|
|
|
|
|
|
promise.done(function (data) {
|
|
|
|
self.seasonCollection = new SeasonCollection(data);
|
|
|
|
self.render();
|
|
|
|
});
|
2013-09-10 08:22:38 +03: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 03:42:15 +03:00
|
|
|
var savePromise =this.model.save()
|
|
|
|
.always(this.render.bind(this));
|
|
|
|
element.spinForPromise(savePromise);
|
2013-09-10 08:22:38 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
_afterToggleSeasonMonitored: function () {
|
|
|
|
this.render();
|
2013-08-05 12:09:41 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|