mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-09 10:34:17 +00:00
Support for add items in custom position
This commit is contained in:
parent
8aa6913b72
commit
c3df679449
@ -1,122 +1,128 @@
|
||||
export default class QueueManager {
|
||||
constructor(params = {}) {
|
||||
this.params = params
|
||||
constructor(params = {}) {
|
||||
this.params = params
|
||||
|
||||
return this
|
||||
}
|
||||
return this
|
||||
}
|
||||
|
||||
prevItems = []
|
||||
nextItems = []
|
||||
prevItems = []
|
||||
nextItems = []
|
||||
|
||||
currentItem = null
|
||||
currentItem = null
|
||||
|
||||
next = ({ random = false } = {}) => {
|
||||
if (this.nextItems.length === 0) {
|
||||
return null
|
||||
}
|
||||
next = ({ random = false } = {}) => {
|
||||
if (this.nextItems.length === 0) {
|
||||
return null
|
||||
}
|
||||
|
||||
if (this.currentItem) {
|
||||
this.prevItems.push(this.currentItem)
|
||||
}
|
||||
if (this.currentItem) {
|
||||
this.prevItems.push(this.currentItem)
|
||||
}
|
||||
|
||||
if (random) {
|
||||
const randomIndex = Math.floor(Math.random() * this.nextItems.length)
|
||||
if (random) {
|
||||
const randomIndex = Math.floor(
|
||||
Math.random() * this.nextItems.length,
|
||||
)
|
||||
|
||||
this.currentItem = this.nextItems.splice(randomIndex, 1)[0]
|
||||
} else {
|
||||
this.currentItem = this.nextItems.shift()
|
||||
}
|
||||
this.currentItem = this.nextItems.splice(randomIndex, 1)[0]
|
||||
} else {
|
||||
this.currentItem = this.nextItems.shift()
|
||||
}
|
||||
|
||||
return this.currentItem
|
||||
}
|
||||
return this.currentItem
|
||||
}
|
||||
|
||||
set = (item) => {
|
||||
if (typeof item === "number") {
|
||||
item = this.nextItems[item]
|
||||
}
|
||||
set = (item) => {
|
||||
if (typeof item === "number") {
|
||||
item = this.nextItems[item]
|
||||
}
|
||||
|
||||
if (this.currentItem && this.currentItem.id === item.id) {
|
||||
return this.currentItem
|
||||
}
|
||||
if (this.currentItem && this.currentItem.id === item.id) {
|
||||
return this.currentItem
|
||||
}
|
||||
|
||||
const itemInNext = this.nextItems.findIndex((i) => i.id === item.id)
|
||||
const itemInPrev = this.prevItems.findIndex((i) => i.id === item.id)
|
||||
const itemInNext = this.nextItems.findIndex((i) => i.id === item.id)
|
||||
const itemInPrev = this.prevItems.findIndex((i) => i.id === item.id)
|
||||
|
||||
if (itemInNext === -1 && itemInPrev === -1) {
|
||||
throw new Error("Item not found in the queue")
|
||||
}
|
||||
if (itemInNext === -1 && itemInPrev === -1) {
|
||||
throw new Error("Item not found in the queue")
|
||||
}
|
||||
|
||||
if (itemInNext > -1) {
|
||||
if (this.currentItem) {
|
||||
this.prevItems.push(this.currentItem)
|
||||
}
|
||||
if (itemInNext > -1) {
|
||||
if (this.currentItem) {
|
||||
this.prevItems.push(this.currentItem)
|
||||
}
|
||||
|
||||
this.prevItems.push(...this.nextItems.splice(0, itemInNext))
|
||||
this.prevItems.push(...this.nextItems.splice(0, itemInNext))
|
||||
|
||||
this.currentItem = this.nextItems.shift()
|
||||
}
|
||||
this.currentItem = this.nextItems.shift()
|
||||
}
|
||||
|
||||
if (itemInPrev > -1) {
|
||||
if (this.currentItem) {
|
||||
this.nextItems.unshift(this.currentItem)
|
||||
}
|
||||
if (itemInPrev > -1) {
|
||||
if (this.currentItem) {
|
||||
this.nextItems.unshift(this.currentItem)
|
||||
}
|
||||
|
||||
this.nextItems.unshift(...this.prevItems.splice(itemInPrev + 1))
|
||||
this.nextItems.unshift(...this.prevItems.splice(itemInPrev + 1))
|
||||
|
||||
this.currentItem = this.prevItems.pop()
|
||||
}
|
||||
this.currentItem = this.prevItems.pop()
|
||||
}
|
||||
|
||||
return this.currentItem
|
||||
}
|
||||
return this.currentItem
|
||||
}
|
||||
|
||||
previous = () => {
|
||||
if (this.prevItems.length === 0) {
|
||||
return this.currentItem
|
||||
}
|
||||
previous = () => {
|
||||
if (this.prevItems.length === 0) {
|
||||
return this.currentItem
|
||||
}
|
||||
|
||||
if (this.currentItem) {
|
||||
this.nextItems.unshift(this.currentItem)
|
||||
}
|
||||
if (this.currentItem) {
|
||||
this.nextItems.unshift(this.currentItem)
|
||||
}
|
||||
|
||||
this.currentItem = this.prevItems.pop()
|
||||
this.currentItem = this.prevItems.pop()
|
||||
|
||||
return this.currentItem
|
||||
}
|
||||
return this.currentItem
|
||||
}
|
||||
|
||||
add = (items) => {
|
||||
if (!Array.isArray(items)) {
|
||||
items = [items]
|
||||
}
|
||||
add = (items, position = "end") => {
|
||||
if (!Array.isArray(items)) {
|
||||
items = [items]
|
||||
}
|
||||
|
||||
this.nextItems = [...this.nextItems, ...items]
|
||||
if (position === "start") {
|
||||
this.nextItems = [...items, ...this.nextItems]
|
||||
} else {
|
||||
this.nextItems = [...this.nextItems, ...items]
|
||||
}
|
||||
|
||||
return items
|
||||
}
|
||||
return items
|
||||
}
|
||||
|
||||
remove = (item) => {
|
||||
const indexNext = this.nextItems.findIndex((i) => i.id === item.id)
|
||||
const indexPrev = this.prevItems.findIndex((i) => i.id === item.id)
|
||||
remove = (item) => {
|
||||
const indexNext = this.nextItems.findIndex((i) => i.id === item.id)
|
||||
const indexPrev = this.prevItems.findIndex((i) => i.id === item.id)
|
||||
|
||||
if (indexNext > -1) {
|
||||
this.nextItems.splice(indexNext, 1)
|
||||
}
|
||||
if (indexNext > -1) {
|
||||
this.nextItems.splice(indexNext, 1)
|
||||
}
|
||||
|
||||
if (indexPrev > -1) {
|
||||
this.prevItems.splice(indexPrev, 1)
|
||||
}
|
||||
}
|
||||
if (indexPrev > -1) {
|
||||
this.prevItems.splice(indexPrev, 1)
|
||||
}
|
||||
}
|
||||
|
||||
flush = () => {
|
||||
this.nextItems = []
|
||||
this.prevItems = []
|
||||
this.currentItem = null
|
||||
}
|
||||
flush = () => {
|
||||
this.nextItems = []
|
||||
this.prevItems = []
|
||||
this.currentItem = null
|
||||
}
|
||||
|
||||
async load(item) {
|
||||
if (typeof this.params.loadFunction === "function") {
|
||||
return await this.params.loadFunction(item)
|
||||
}
|
||||
async load(item) {
|
||||
if (typeof this.params.loadFunction === "function") {
|
||||
return await this.params.loadFunction(item)
|
||||
}
|
||||
|
||||
return item
|
||||
}
|
||||
return item
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user