1
0
Fork 0
mirror of https://github.com/jellyfin/jellyfin-web synced 2025-03-30 19:56:21 +00:00
jellyfin-web/src/components/playback/playqueuemanager.js

278 lines
7.4 KiB
JavaScript
Raw Normal View History

2019-01-23 11:33:34 +00:00
define([], function () {
'use strict';
2018-10-23 01:05:09 +03:00
var currentId = 0;
2018-10-23 01:05:09 +03:00
function addUniquePlaylistItemId(item) {
if (!item.PlaylistItemId) {
2020-05-04 12:44:12 +02:00
item.PlaylistItemId = 'playlistItem' + currentId;
currentId++;
}
2018-10-23 01:05:09 +03:00
}
function findPlaylistIndex(playlistItemId, list) {
for (var i = 0, length = list.length; i < length; i++) {
if (list[i].PlaylistItemId === playlistItemId) {
return i;
}
}
return -1;
2018-10-23 01:05:09 +03:00
}
function PlayQueueManager() {
this._sortedPlaylist = [];
this._playlist = [];
this._repeatMode = 'RepeatNone';
this._shuffleMode = 'Sorted';
2018-10-23 01:05:09 +03:00
}
PlayQueueManager.prototype.getPlaylist = function () {
return this._playlist.slice(0);
};
PlayQueueManager.prototype.setPlaylist = function (items) {
items = items.slice(0);
for (var i = 0, length = items.length; i < length; i++) {
addUniquePlaylistItemId(items[i]);
}
this._currentPlaylistItemId = null;
this._playlist = items;
this._repeatMode = 'RepeatNone';
};
PlayQueueManager.prototype.queue = function (items) {
for (var i = 0, length = items.length; i < length; i++) {
addUniquePlaylistItemId(items[i]);
this._playlist.push(items[i]);
}
};
PlayQueueManager.prototype.shufflePlaylist = function () {
this._sortedPlaylist = [];
for (let item of this._playlist) {
this._sortedPlaylist.push(item);
}
for (let i = this._playlist.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * i);
const temp = this._playlist[i];
this._playlist[i] = this._playlist[j];
this._playlist[j] = temp;
}
this._shuffleMode = 'Shuffle';
};
PlayQueueManager.prototype.sortShuffledPlaylist = function () {
this._playlist = [];
for (let item of this._sortedPlaylist) {
this._playlist.push(item);
}
this._sortedPlaylist = [];
this._shuffleMode = 'Sorted';
};
2018-10-23 01:05:09 +03:00
function arrayInsertAt(destArray, pos, arrayToInsert) {
var args = [];
args.push(pos); // where to insert
args.push(0); // nothing to remove
args = args.concat(arrayToInsert); // add on array to insert
destArray.splice.apply(destArray, args); // splice it in
2018-10-23 01:05:09 +03:00
}
PlayQueueManager.prototype.queueNext = function (items) {
var i;
var length;
for (i = 0, length = items.length; i < length; i++) {
addUniquePlaylistItemId(items[i]);
}
var currentIndex = this.getCurrentPlaylistIndex();
if (currentIndex === -1) {
currentIndex = this._playlist.length;
} else {
currentIndex++;
}
arrayInsertAt(this._playlist, currentIndex, items);
};
PlayQueueManager.prototype.getCurrentPlaylistIndex = function () {
return findPlaylistIndex(this.getCurrentPlaylistItemId(), this._playlist);
};
PlayQueueManager.prototype.getCurrentItem = function () {
2018-10-23 01:05:09 +03:00
var index = findPlaylistIndex(this.getCurrentPlaylistItemId(), this._playlist);
return index === -1 ? null : this._playlist[index];
};
PlayQueueManager.prototype.getCurrentPlaylistItemId = function () {
return this._currentPlaylistItemId;
};
PlayQueueManager.prototype.setPlaylistState = function (playlistItemId, playlistIndex) {
this._currentPlaylistItemId = playlistItemId;
};
PlayQueueManager.prototype.setPlaylistIndex = function (playlistIndex) {
if (playlistIndex < 0) {
this.setPlaylistState(null);
} else {
this.setPlaylistState(this._playlist[playlistIndex].PlaylistItemId);
}
};
PlayQueueManager.prototype.removeFromPlaylist = function (playlistItemIds) {
2018-10-23 01:05:09 +03:00
var playlist = this.getPlaylist();
if (playlist.length <= playlistItemIds.length) {
return {
result: 'empty'
};
2018-10-23 01:05:09 +03:00
}
var currentPlaylistItemId = this.getCurrentPlaylistItemId();
var isCurrentIndex = playlistItemIds.indexOf(currentPlaylistItemId) !== -1;
this._playlist = playlist.filter(function (item) {
return playlistItemIds.indexOf(item.PlaylistItemId) === -1;
});
return {
result: 'removed',
isCurrentIndex: isCurrentIndex
};
};
function moveInArray(array, from, to) {
array.splice(to, 0, array.splice(from, 1)[0]);
}
PlayQueueManager.prototype.movePlaylistItem = function (playlistItemId, newIndex) {
var playlist = this.getPlaylist();
var oldIndex;
for (var i = 0, length = playlist.length; i < length; i++) {
2018-10-23 01:05:09 +03:00
if (playlist[i].PlaylistItemId === playlistItemId) {
oldIndex = i;
break;
}
}
if (oldIndex === -1 || oldIndex === newIndex) {
return {
result: 'noop'
};
}
if (newIndex >= playlist.length) {
throw new Error('newIndex out of bounds');
}
moveInArray(playlist, oldIndex, newIndex);
this._playlist = playlist;
return {
result: 'moved',
2018-10-23 01:05:09 +03:00
playlistItemId: playlistItemId,
newIndex: newIndex
};
};
PlayQueueManager.prototype.reset = function () {
this._sortedPlaylist = [];
this._playlist = [];
this._currentPlaylistItemId = null;
this._repeatMode = 'RepeatNone';
this._shuffleMode = 'Sorted';
};
PlayQueueManager.prototype.setRepeatMode = function (value) {
if (value === 'RepeatOne' || value === 'RepeatAll' || value === 'RepeatNone') {
this._repeatMode = value;
} else {
throw new TypeError('invalid value provided for setRepeatMode');
}
};
PlayQueueManager.prototype.getRepeatMode = function () {
return this._repeatMode;
};
PlayQueueManager.prototype.setShuffleMode = function (value) {
if (value === 'Sorted') {
this.sortShuffledPlaylist();
} else if (value === 'Shuffle') {
this.shufflePlaylist();
} else {
throw new TypeError('invalid value provided for setShuffleMode');
}
};
PlayQueueManager.prototype.getShuffleMode = function () {
return this._shuffleMode;
};
PlayQueueManager.prototype.getNextItemInfo = function () {
var newIndex;
var playlist = this.getPlaylist();
var playlistLength = playlist.length;
2018-10-23 01:05:09 +03:00
switch (this.getRepeatMode()) {
case 'RepeatOne':
2018-10-23 01:05:09 +03:00
newIndex = this.getCurrentPlaylistIndex();
break;
case 'RepeatAll':
newIndex = this.getCurrentPlaylistIndex() + 1;
if (newIndex >= playlistLength) {
newIndex = 0;
}
2018-10-23 01:05:09 +03:00
break;
default:
newIndex = this.getCurrentPlaylistIndex() + 1;
break;
2018-10-23 01:05:09 +03:00
}
if (newIndex < 0 || newIndex >= playlistLength) {
return null;
}
2018-10-23 01:05:09 +03:00
var item = playlist[newIndex];
if (!item) {
return null;
}
return {
2018-10-23 01:05:09 +03:00
item: item,
index: newIndex
};
};
return PlayQueueManager;
});