2016-07-06 12:25:58 -07:00
|
|
|
|
define(['connectionManager', 'playbackManager', 'globalize'], function (connectionManager, playbackManager, globalize) {
|
2016-10-17 22:06:48 -07:00
|
|
|
|
'use strict';
|
2016-02-16 23:58:07 -07:00
|
|
|
|
|
|
|
|
|
/// <summary> Play items. </summary>
|
|
|
|
|
/// <param name="items"> The items. </param>
|
|
|
|
|
/// <param name="shuffle"> The shuffle. </param>
|
|
|
|
|
/// <returns> . </returns>
|
|
|
|
|
function playItems(items, shuffle) {
|
|
|
|
|
|
|
|
|
|
if (shuffle) {
|
|
|
|
|
items = shuffleArray(items);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (items.length) {
|
2016-07-20 06:56:24 -07:00
|
|
|
|
var serverId = items[0].ServerId;
|
|
|
|
|
items = items.map(function (i) {
|
|
|
|
|
return i.Id;
|
|
|
|
|
});
|
|
|
|
|
|
2016-07-06 12:25:58 -07:00
|
|
|
|
playbackManager.play({
|
2016-07-20 06:56:24 -07:00
|
|
|
|
ids: items,
|
|
|
|
|
serverId: serverId
|
2016-02-16 23:58:07 -07:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else {
|
2016-02-24 23:38:12 -07:00
|
|
|
|
require(['toast'], function (toast) {
|
2016-07-06 12:25:58 -07:00
|
|
|
|
toast(globalize.translate('sharedcomponents#NoItemsFound'));
|
2016-02-16 23:58:07 -07:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary> Shuffle array. </summary>
|
|
|
|
|
/// <param name="array"> The array. </param>
|
|
|
|
|
/// <returns> . </returns>
|
|
|
|
|
function shuffleArray(array) {
|
|
|
|
|
var currentIndex = array.length, temporaryValue, randomIndex;
|
|
|
|
|
|
|
|
|
|
// While there remain elements to shuffle...
|
|
|
|
|
while (0 !== currentIndex) {
|
|
|
|
|
|
|
|
|
|
// Pick a remaining element...
|
|
|
|
|
randomIndex = Math.floor(Math.random() * currentIndex);
|
|
|
|
|
currentIndex -= 1;
|
|
|
|
|
|
|
|
|
|
// And swap it with the current element.
|
|
|
|
|
temporaryValue = array[currentIndex];
|
|
|
|
|
array[currentIndex] = array[randomIndex];
|
|
|
|
|
array[randomIndex] = temporaryValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return array;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return function (result) {
|
|
|
|
|
|
2016-07-06 13:16:56 -07:00
|
|
|
|
return function () {
|
|
|
|
|
var query = {
|
|
|
|
|
Limit: result.item.limit || 100,
|
|
|
|
|
UserId: result.userId,
|
|
|
|
|
ExcludeLocationTypes: "Virtual"
|
|
|
|
|
};
|
2016-02-16 23:58:07 -07:00
|
|
|
|
|
2016-07-06 13:16:56 -07:00
|
|
|
|
if (result.item.itemType) {
|
|
|
|
|
query.IncludeItemTypes = result.item.itemType;
|
|
|
|
|
}
|
2016-02-16 23:58:07 -07:00
|
|
|
|
|
2016-07-06 13:16:56 -07:00
|
|
|
|
var apiClient = connectionManager.currentApiClient();
|
2016-07-20 06:56:24 -07:00
|
|
|
|
|
2016-07-06 13:16:56 -07:00
|
|
|
|
if (result.item.sourceid === 'nextup') {
|
2016-02-16 23:58:07 -07:00
|
|
|
|
|
2016-07-06 13:16:56 -07:00
|
|
|
|
apiClient.getNextUpEpisodes(query).then(function (queryResult) {
|
2016-02-16 23:58:07 -07:00
|
|
|
|
|
2016-07-06 13:16:56 -07:00
|
|
|
|
playItems(queryResult.Items, result.item.shuffle);
|
2016-02-16 23:58:07 -07:00
|
|
|
|
|
2016-07-06 13:16:56 -07:00
|
|
|
|
});
|
|
|
|
|
}
|
2016-02-16 23:58:07 -07:00
|
|
|
|
|
2016-07-06 13:16:56 -07:00
|
|
|
|
if (result.item.shuffle) {
|
|
|
|
|
result.item.sortBy = result.sortBy ? 'Random,' + result.item.sortBy : 'Random';
|
|
|
|
|
}
|
2016-02-16 23:58:07 -07:00
|
|
|
|
|
2016-07-06 13:16:56 -07:00
|
|
|
|
query.SortBy = result.item.sortBy;
|
|
|
|
|
query.SortOrder = result.item.sortOrder;
|
|
|
|
|
query.Recursive = true;
|
2016-02-16 23:58:07 -07:00
|
|
|
|
|
2016-07-06 13:16:56 -07:00
|
|
|
|
if (result.item.filters.indexOf('unplayed') !== -1) {
|
|
|
|
|
query.IsPlayed = false;
|
|
|
|
|
}
|
|
|
|
|
if (result.item.filters.indexOf('played') !== -1) {
|
|
|
|
|
query.IsPlayed = true;
|
|
|
|
|
}
|
|
|
|
|
if (result.item.filters.indexOf('favorite') !== -1) {
|
|
|
|
|
query.Filters = 'IsFavorite';
|
|
|
|
|
}
|
2016-02-16 23:58:07 -07:00
|
|
|
|
|
2016-07-06 13:16:56 -07:00
|
|
|
|
apiClient.getItems(apiClient.getCurrentUserId(), query).then(function (queryResult) {
|
2016-02-16 23:58:07 -07:00
|
|
|
|
|
2016-07-06 13:16:56 -07:00
|
|
|
|
playItems(queryResult.Items, result.item.shuffle);
|
|
|
|
|
});
|
|
|
|
|
};
|
2016-10-17 22:06:48 -07:00
|
|
|
|
};
|
2016-02-16 23:58:07 -07:00
|
|
|
|
});
|