* SAAS-1161 - refactor backend so episodes are loaded separately from podcast data to speed up loading and improve API readability

* Decouple imported and station podcast behaviour on the frontend
This commit is contained in:
Duncan Sommerville 2015-11-02 18:07:16 -05:00
parent c4be9aebb2
commit def8e7280b
9 changed files with 312 additions and 256 deletions

View file

@ -8,86 +8,168 @@ var AIRTIME = (function (AIRTIME) {
mod = AIRTIME.podcast;
var endpoint = 'rest/podcast/', PodcastTable;
//AngularJS app
var podcastApp = angular.module('podcast', [])
.controller('RestController', function($scope, $http, podcast, tab) {
// We need to pass in the tab object and the episodes table object so we can reference them
//We take a podcast object in as a parameter rather fetching the podcast by ID here because
//when you're creating a new podcast, we already have the object from the result of the POST. We're saving
//a roundtrip by not fetching it again here.
$scope.podcast = podcast;
tab.setName($scope.podcast.title);
$scope.csrf = jQuery("#csrf").val();
tab.contents.find("table").attr("id", "podcast_episodes_" + podcast.id);
// TODO: this solves a race condition, but we should look for the root cause
AIRTIME.tabs.onResize();
var episodeTable = AIRTIME.podcast.initPodcastEpisodeDatatable(podcast, tab);
function PodcastController($scope, $http, podcast, tab) {
// We need to pass in the tab object and the episodes table object so we can reference them
var self = this;
/**
* Override the switchTo function to reload the table when the tab is focused.
* Should help to reduce the number of cases where the frontend doesn't match the state
* of the backend (due to automatic ingestion).
*
* Note that these cases should already be very few and far between.
*
* TODO: make sure this doesn't noticeably slow performance
* XXX: it's entirely possible that this (in the angular app) is not where we want this function...
*/
tab.switchTo = function() {
AIRTIME.tabs.Tab.prototype.switchTo.call(this);
episodeTable.reload($scope.podcast.id);
};
//We take a podcast object in as a parameter rather fetching the podcast by ID here because
//when you're creating a new podcast, we already have the object from the result of the POST. We're saving
//a roundtrip by not fetching it again here.
$scope.podcast = podcast;
$scope.tab = tab;
tab.setName($scope.podcast.title);
$scope.csrf = jQuery("#csrf").val();
tab.contents.find("table").attr("id", "podcast_episodes_" + podcast.id);
/**
* Internal function.
*
* Make a PUT request to the server to update the podcast object
*
* @private
*/
function _updatePodcast() {
$http.put(endpoint + $scope.podcast.id, { csrf_token: $scope.csrf, podcast: $scope.podcast })
.success(function() {
episodeTable.reload($scope.podcast.id);
AIRTIME.library.podcastDataTable.fnDraw();
tab.setName($scope.podcast.title);
});
}
/**
* Override the switchTo function to reload the table when the tab is focused.
* Should help to reduce the number of cases where the frontend doesn't match the state
* of the backend (due to automatic ingestion).
*
* Note that these cases should already be very few and far between.
*
* TODO: make sure this doesn't noticeably slow performance
* XXX: it's entirely possible that this (in the angular app) is not where we want this function...
*/
tab.switchTo = function () {
AIRTIME.tabs.Tab.prototype.switchTo.call(this);
self.reloadEpisodeTable();
};
/**
* For imported podcasts.
*
* Save each of the selected episodes and update the podcast object.
*/
$scope.savePodcast = function() {
var episodes = episodeTable.getSelectedRows();
/**
* Internal function.
*
* Make a PUT request to the server to update the podcast object
*
* @private
*/
function _updatePodcast() {
$http.put(endpoint + $scope.podcast.id, {csrf_token: $scope.csrf, podcast: $scope.podcast})
.success(function () {
// episodeTable.reload($scope.podcast.id);
self.episodeTable.getDatatable().fnDraw();
AIRTIME.library.podcastDataTable.fnDraw();
tab.setName($scope.podcast.title);
});
}
/**
* For imported podcasts.
*
* Save each of the selected episodes and update the podcast object.
*/
$scope.savePodcast = $scope.savePodcast || function () {
var episodes = self.episodeTable.getSelectedRows();
// TODO: Should we implement a batch endpoint for this instead?
jQuery.each(episodes, function() {
$http.post(endpoint + $scope.podcast.id + '/episodes', { csrf_token: $scope.csrf, episode: this });
jQuery.each(episodes, function () {
$http.post(endpoint + $scope.podcast.id + '/episodes', {
csrf_token: $scope.csrf,
episode: this
});
});
_updatePodcast();
};
/**
* For the station podcast.
*
* Update the station podcast object.
*/
$scope.saveStationPodcast = function() {
// TODO: We still need a way to delete episodes from the station podcast
_updatePodcast();
/**
* Close the tab and discard any changes made to the podcast data.
*/
$scope.discard = function () {
tab.close();
$scope.podcast = {};
};
self.$scope = $scope;
self.$http = $http;
self.initialize();
}
PodcastController.prototype._initTable = function() {
var self = this,
$scope = self.$scope;
// We want to fetch the data statically for imported podcasts because we would need to implement sorting
// in a very convoluted way on the backend to accommodate the nonexistent rows for uningested episodes
var params = {
bServerSide : false,
sAjaxSource : null,
// Initialize the table with empty data so we can defer loading
// If we load sequentially there's a delay before the table appears
aaData : {},
aoColumns : [
/* GUID */ { "sTitle" : "" , "mDataProp" : "guid" , "sClass" : "podcast_episodes_guid" , "bVisible" : false },
/* Title */ { "sTitle" : $.i18n._("Title") , "mDataProp" : "title" , "sClass" : "podcast_episodes_title" , "sWidth" : "170px" },
/* Author */ { "sTitle" : $.i18n._("Author") , "mDataProp" : "author" , "sClass" : "podcast_episodes_author" , "sWidth" : "170px" },
/* Description */ { "sTitle" : $.i18n._("Description") , "mDataProp" : "description" , "sClass" : "podcast_episodes_description" , "sWidth" : "300px" },
/* Link */ { "sTitle" : $.i18n._("Link") , "mDataProp" : "link" , "sClass" : "podcast_episodes_link" , "sWidth" : "170px" },
/* Publication Date */ { "sTitle" : $.i18n._("Publication Date") , "mDataProp" : "pub_date" , "sClass" : "podcast_episodes_pub_date" , "sWidth" : "170px" }
]
},
buttons = {};
self.episodeTable = AIRTIME.podcast.initPodcastEpisodeDatatable($scope.podcast, $scope.tab, params, buttons);
self.reloadEpisodeTable();
};
PodcastController.prototype.reloadEpisodeTable = function() {
this.episodeTable.reload(this.$scope.podcast.id);
};
PodcastController.prototype.initialize = function() {
var self = this;
// TODO: this solves a race condition, but we should look for the root cause
AIRTIME.tabs.onResize();
self._initTable();
};
function StationPodcastController($scope, $http, podcast, tab) {
// Super call to parent controller
PodcastController.call(this, $scope, $http, podcast, tab);
/**
* For the station podcast.
*
* Update the station podcast object.
*/
$scope.savePodcast = function () {
console.log("Saving station podcast");
// TODO: We still need a way to delete episodes from the station podcast
_updatePodcast();
};
}
StationPodcastController.prototype = Object.create(PodcastController.prototype);
StationPodcastController.prototype._initTable = function() {
var $scope = this.$scope,
buttons = {
0: {
'title' : $.i18n._('Delete'),
'iconClass' : "icon-trash",
extraBtnClass : "btn-danger",
elementId : '',
eventHandlers : {
click: function (e) {
// TODO: delete function for station podcast episodes
}
}
}
},
params = {
sAjaxSource : endpoint + $scope.podcast.id + '/episodes',
aoColumns: [
/* Title */ { "sTitle" : $.i18n._("Title") , "mDataProp" : "CcFiles.track_title" , "sClass" : "podcast_episodes_title" , "sWidth" : "170px" },
/* Description */ { "sTitle" : $.i18n._("Description") , "mDataProp" : "CcFiles.description" , "sClass" : "podcast_episodes_description" , "sWidth" : "300px" }
]
};
/**
* Close the tab and discard any changes made to the podcast data.
*/
$scope.discard = function() {
tab.close();
$scope.podcast = {};
};
});
this.episodeTable = AIRTIME.podcast.initPodcastEpisodeDatatable($scope.podcast, $scope.tab, params, buttons);
};
StationPodcastController.prototype.reloadEpisodeTable = function() {
self.episodeTable.getDatatable().fnDraw();
};
//AngularJS app
var podcastApp = angular.module('podcast', [])
.controller('Podcast', ['$scope', '$http', 'podcast', 'tab', PodcastController])
.controller('StationPodcast', ['$scope', '$http', 'podcast', 'tab', StationPodcastController]);
/**
* Implement bulk editing of podcasts in order to accommodate the existing selection
@ -129,8 +211,7 @@ var AIRTIME = (function (AIRTIME) {
function _bootstrapAngularApp(podcast, tab) {
podcastApp.value('podcast', podcast);
podcastApp.value('tab', tab);
var wrapper = tab.contents.find(".editor_pane_wrapper");
wrapper.attr("ng-controller", "RestController");
var wrapper = tab.contents.find(".angular_wrapper");
angular.bootstrap(wrapper.get(0), ["podcast"]);
}
@ -178,13 +259,13 @@ var AIRTIME = (function (AIRTIME) {
if (rowData.ingested) return null; // Don't create checkboxes for ingested items
return AIRTIME.widgets.Table.prototype._datatablesCheckboxDataDelegate.call(this, rowData, callType, dataToSave);
};
// Since we're using a static source, define a separate function to fetch and 'reload' the table data
// Since we're sometimes using a static source, define a separate function to fetch and 'reload' the table data
// We use this when we save the Podcast because we need to flag rows the user is ingesting
PodcastTable.prototype.reload = function(id) {
PodcastTable.prototype.reload = function (id) {
var dt = this._datatable;
$.get(endpoint + id, function(json) {
$.get(endpoint + id + '/episodes', function (json) {
dt.fnClearTable();
dt.fnAddData(JSON.parse(json).episodes);
dt.fnAddData(JSON.parse(json));
});
};
}
@ -252,59 +333,40 @@ var AIRTIME = (function (AIRTIME) {
/**
* Initialize the internal datatable for the podcast editor view to hold episode data passed back from the server.
*
* The episode data is taken from the RSS feed XML and contains all episodes in the feed, including but not
* limited to episodes already ingested (downloaded) into Airtime.
*
* Selection for the internal table represents episodes marked for ingest and is disabled for ingested episodes.
*
* @param podcast the podcast data JSON object. Includes episode data
* @param tab Tab object the podcast will be opened in
* @param podcast the podcast data JSON object.
* @param tab Tab object the podcast will be opened in
* @param params JSON object containing datatables parameters to override
* @param buttons JSON object containing datatables button parameters
*
* @returns {*} the created Table object
*/
mod.initPodcastEpisodeDatatable = function(podcast, tab) {
var aoColumns = [
/* GUID */ { "sTitle" : "" , "mDataProp" : "guid" , "sClass" : "podcast_episodes_guid" , "bVisible" : false },
/* Title */ { "sTitle" : $.i18n._("Title") , "mDataProp" : "title" , "sClass" : "podcast_episodes_title" , "sWidth" : "170px" },
/* Author */ { "sTitle" : $.i18n._("Author") , "mDataProp" : "author" , "sClass" : "podcast_episodes_author" , "sWidth" : "170px" },
/* Description */ { "sTitle" : $.i18n._("Description") , "mDataProp" : "description" , "sClass" : "podcast_episodes_description" , "sWidth" : "300px" },
/* Link */ { "sTitle" : $.i18n._("Link") , "mDataProp" : "link" , "sClass" : "podcast_episodes_link" , "sWidth" : "170px" },
/* Publication Date */ { "sTitle" : $.i18n._("Publication Date") , "mDataProp" : "pub_date" , "sClass" : "podcast_episodes_pub_date" , "sWidth" : "170px" }
];
mod.initPodcastEpisodeDatatable = function(podcast, tab, params, buttons) {
params = $.extend(params,
{
oColVis : {
sAlign: "right",
aiExclude: [0, 1],
buttonText: $.i18n._("Columns"),
iOverlayFade: 0,
oColReorder: {
iFixedColumns: 1 // Checkbox
}
}
}
);
if (typeof PodcastTable === 'undefined') {
_initPodcastTable();
}
var podcastToolbarButtons = AIRTIME.widgets.Table.getStandardToolbarButtons();
podcastToolbarButtons[AIRTIME.widgets.Table.TOOLBAR_BUTTON_ROLES.DELETE].eventHandlers.click = function(e) {
// TODO: add {this} reference to event handlers and implement deletion for station podcasts
};
// Set up the div with id "podcast_table" as a datatable.
var podcastEpisodesTableWidget = new PodcastTable(
tab.contents.find('.podcast_episodes'), // DOM node to create the table inside.
true, // Enable item selection
podcastToolbarButtons, // Toolbar buttons
{ // Datatables overrides.
'aoColumns' : aoColumns,
'bServerSide' : false,
// We want to make as few round trips as possible, so we get
// the episode data alongside the Podcast data and pass it in
// as json. Doing this caches all the episode data on the front-end,
// which means we also don't need to go back to the server for pagination
'sAjaxSource' : null,
'aaData' : podcast.episodes,
"oColVis": {
"sAlign": "right",
"aiExclude": [0, 1],
"buttonText": $.i18n._("Columns"),
"iOverlayFade": 0,
'oColReorder': {
'iFixedColumns': 1 // Checkbox
}
}
}
true, // Enable item selection
buttons, // Toolbar buttons
params // Datatables overrides.
);
podcastEpisodesTableWidget.getDatatable().addTitles("td");