2013-11-26 11:06:28 +03:00
|
|
|
'use strict';
|
|
|
|
define(
|
|
|
|
[
|
|
|
|
'underscore',
|
|
|
|
'vent',
|
|
|
|
'marionette',
|
|
|
|
'Rename/RenamePreviewCollection',
|
|
|
|
'Rename/RenamePreviewCollectionView',
|
|
|
|
'Rename/RenamePreviewEmptyCollectionView',
|
|
|
|
'Shared/LoadingView',
|
|
|
|
'Commands/CommandController'
|
|
|
|
], function (_, vent, Marionette, RenamePreviewCollection, RenamePreviewCollectionView, EmptyCollectionView, LoadingView, CommandController) {
|
|
|
|
|
|
|
|
return Marionette.Layout.extend({
|
|
|
|
template: 'Rename/RenamePreviewLayoutTemplate',
|
|
|
|
|
|
|
|
regions: {
|
|
|
|
renamePreviews : '#rename-previews'
|
|
|
|
},
|
|
|
|
|
|
|
|
ui: {
|
2013-11-27 12:19:44 +03:00
|
|
|
pathInfo : '.x-path-info',
|
|
|
|
renameAll : '.x-rename-all',
|
|
|
|
checkboxIcon: '.x-rename-all-button i'
|
2013-11-26 11:06:28 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
events: {
|
2013-11-27 12:19:44 +03:00
|
|
|
'click .x-organize' : '_organizeFiles',
|
|
|
|
'change .x-rename-all': '_toggleAll'
|
2013-11-26 11:06:28 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
initialize: function (options) {
|
|
|
|
this.model = options.series;
|
|
|
|
this.seasonNumber = options.seasonNumber;
|
|
|
|
|
|
|
|
var viewOptions = {};
|
|
|
|
viewOptions.seriesId = this.model.id;
|
|
|
|
viewOptions.seasonNumber = this.seasonNumber;
|
|
|
|
|
|
|
|
this.collection = new RenamePreviewCollection(viewOptions);
|
|
|
|
this.listenTo(this.collection, 'sync', this._showPreviews);
|
2013-11-27 12:19:44 +03:00
|
|
|
this.listenTo(this.collection, 'rename:select', this._itemRenameChanged);
|
2013-11-26 11:06:28 +03:00
|
|
|
|
|
|
|
this.collection.fetch();
|
|
|
|
},
|
|
|
|
|
|
|
|
onRender: function() {
|
|
|
|
this.renamePreviews.show(new LoadingView());
|
|
|
|
},
|
|
|
|
|
|
|
|
_showPreviews: function () {
|
|
|
|
if (this.collection.length === 0) {
|
|
|
|
this.ui.pathInfo.hide();
|
|
|
|
this.renamePreviews.show(new EmptyCollectionView());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-27 12:19:44 +03:00
|
|
|
this.ui.pathInfo.show();
|
2013-11-26 11:06:28 +03:00
|
|
|
this.collection.invoke('set', { rename: true });
|
|
|
|
this.renamePreviews.show(new RenamePreviewCollectionView({ collection: this.collection }));
|
|
|
|
},
|
|
|
|
|
|
|
|
_organizeFiles: function () {
|
|
|
|
if (this.collection.length === 0) {
|
|
|
|
vent.trigger(vent.Commands.CloseModalCommand);
|
|
|
|
}
|
|
|
|
|
|
|
|
var files = _.map(this.collection.where({ rename: true }), function (model) {
|
|
|
|
return model.get('episodeFileId');
|
|
|
|
});
|
|
|
|
|
|
|
|
if (files.length === 0) {
|
|
|
|
vent.trigger(vent.Commands.CloseModalCommand);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.seasonNumber) {
|
|
|
|
CommandController.Execute('renameFiles', {
|
|
|
|
name : 'renameFiles',
|
|
|
|
seriesId : this.model.id,
|
|
|
|
seasonNumber: this.seasonNumber,
|
|
|
|
files : files
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
CommandController.Execute('renameFiles', {
|
|
|
|
name : 'renameFiles',
|
|
|
|
seriesId : this.model.id,
|
|
|
|
seasonNumber: -1,
|
|
|
|
files : files
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
vent.trigger(vent.Commands.CloseModalCommand);
|
2013-11-27 12:19:44 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
_setCheckedState: function (checked) {
|
|
|
|
if (checked) {
|
|
|
|
this.ui.checkboxIcon.addClass('icon-check');
|
|
|
|
this.ui.checkboxIcon.removeClass('icon-check-empty');
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
this.ui.checkboxIcon.addClass('icon-check-empty');
|
|
|
|
this.ui.checkboxIcon.removeClass('icon-check');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_toggleAll: function () {
|
|
|
|
var checked = this.ui.renameAll.prop('checked');
|
|
|
|
this._setCheckedState(checked);
|
|
|
|
|
|
|
|
this.collection.each(function (model) {
|
|
|
|
model.trigger('rename:select', model, checked);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
_itemRenameChanged: function (model, checked) {
|
|
|
|
var allChecked = this.collection.all(function (m) {
|
|
|
|
return m.get('rename');
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!checked || allChecked) {
|
|
|
|
this._setCheckedState(checked);
|
|
|
|
}
|
2013-11-26 11:06:28 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|