From 6480e6ad1ebf61d9daa07cec8dbc2598861f9a89 Mon Sep 17 00:00:00 2001 From: vapormusic Date: Tue, 28 Dec 2021 09:49:38 +0700 Subject: [PATCH] add contextmenu to the square --- .../views/components/mediaitem-square-sp.ejs | 44 ++++++++++++++++++- .../views/components/mediaitem-square.ejs | 43 +++++++++++++++++- 2 files changed, 85 insertions(+), 2 deletions(-) diff --git a/src/renderer/views/components/mediaitem-square-sp.ejs b/src/renderer/views/components/mediaitem-square-sp.ejs index 07058151..ff8bd402 100644 --- a/src/renderer/views/components/mediaitem-square-sp.ejs +++ b/src/renderer/views/components/mediaitem-square-sp.ejs @@ -85,9 +85,34 @@ return { app: this.$root, isVisible: true, + addedToLibrary : false, } }, methods: { + revisedRandId() { + return Math.random().toString(36).replace(/[^a-z]+/g, '').substr(2, 10); + }, + async isInLibrary() { + if (this.item.type && !this.item.type.includes("library")) { + var params = {"fields[playlists]": "inLibrary", "fields[albums]": "inLibrary", "relate": "library", "extend": this.revisedRandId()} + var res = await app.mkapi(this.item.attributes.playParams.kind ?? this.item.type, this.item.attributes.playParams.isLibrary ?? false, this.item.attributes.playParams.id ?? this.item.id, params); + this.addedToLibrary = (res && res.attributes && res.attributes.inLibrary) ? res.attributes.inLibrary : false + } else { + this.addedToLibrary = true + } + }, + async removeFromLibrary(id) { + var params = {"fields[playlists]": "inLibrary","fields[songs]": "inLibrary", "fields[albums]": "inLibrary", "relate": "library", "extend": this.revisedRandId()} + var id = this.item.id ?? this.item.attributes.playParams.id + var res = await app.mkapi(this.item.attributes.playParams.kind ?? this.item.type, this.item.attributes.playParams.isLibrary ?? false, this.item.attributes.playParams.id ?? this.item.id, params); + if (res && res.relationships && res.relationships.library && res.relationships.library.data && res.relationships.library.data.length > 0) { + id = res.relationships.library.data[0].id + } + let kind = this.item.attributes.playParams.kind ?? this.item.type ?? ''; + var truekind = (!kind.endsWith("s")) ? (kind + "s") : kind; + app.mk.api.library.remove({[truekind]: id}) + this.addedToLibrary = true + }, subtitleSearchNavigate(item) { if((item.attributes.editorialNotes == null) && item.attributes.artistName)app.searchAndNavigate(item,'artist') }, @@ -108,7 +133,7 @@ visibilityChanged: function (isVisible, entry) { this.isVisible = isVisible }, - contextMenu(event) { + async contextMenu(event) { if (!event) { event = this.$refs.main } else { @@ -116,6 +141,7 @@ } let self = this let useMenu = "normal" + await this.isInLibrary() if (app.selectedMediaItems.length <= 1) { app.selectedMediaItems = [] app.select_selectMediaItem(this.item.attributes.playParams.id ?? this.item.id, this.item.attributes.playParams.kind ?? this.item.type, this.index, this.guid) @@ -187,10 +213,26 @@ app.selectedMediaItems = [] } }, + { + "name": "Add to Playlist...", + "action": function () { + app.promptAddToPlaylist() + } + }, + { + "name": (this.addedToLibrary) ? "Remove from Library..." : "Add to Library...", + "action": async function () { + let item_id = self.item.attributes.playParams.id ?? self.item.id; + let data_type = self.item.attributes.playParams.kind ?? self.item.type; + if (self.addedToLibrary != true) { console.log("add"); app.addToLibrary(item_id); self.addedToLibrary = true} + else { console.log("remove"); await self.removeFromLibrary(item_id); self.addedToLibrary = false}; + } + }, ] } } + if ((self.item.attributes.playParams.kind ?? self.item.type).includes("playlist")) { menus.normal.items.splice(2, 1);} CiderContextMenu.Create(event, menus[useMenu]) }, } diff --git a/src/renderer/views/components/mediaitem-square.ejs b/src/renderer/views/components/mediaitem-square.ejs index 664dc57e..879ab049 100644 --- a/src/renderer/views/components/mediaitem-square.ejs +++ b/src/renderer/views/components/mediaitem-square.ejs @@ -57,6 +57,30 @@ } }, methods: { + revisedRandId() { + return Math.random().toString(36).replace(/[^a-z]+/g, '').substr(2, 10); + }, + async isInLibrary() { + if (this.item.type && !this.item.type.includes("library")) { + var params = {"fields[playlists]": "inLibrary", "fields[albums]": "inLibrary", "relate": "library", "extend": this.revisedRandId()} + var res = await app.mkapi(this.item.attributes.playParams.kind ?? this.item.type, this.item.attributes.playParams.isLibrary ?? false, this.item.attributes.playParams.id ?? this.item.id, params); + this.addedToLibrary = (res && res.attributes && res.attributes.inLibrary) ? res.attributes.inLibrary : false + } else { + this.addedToLibrary = true + } + }, + async removeFromLibrary(id) { + var params = {"fields[playlists]": "inLibrary","fields[songs]": "inLibrary", "fields[albums]": "inLibrary", "relate": "library", "extend": this.revisedRandId()} + var id = this.item.id ?? this.item.attributes.playParams.id + var res = await app.mkapi(this.item.attributes.playParams.kind ?? this.item.type, this.item.attributes.playParams.isLibrary ?? false, this.item.attributes.playParams.id ?? this.item.id, params); + if (res && res.relationships && res.relationships.library && res.relationships.library.data && res.relationships.library.data.length > 0) { + id = res.relationships.library.data[0].id + } + let kind = this.item.attributes.playParams.kind ?? this.item.type ?? ''; + var truekind = (!kind.endsWith("s")) ? (kind + "s") : kind; + app.mk.api.library.remove({[truekind]: id}) + this.addedToLibrary = true + }, uuidv4() { return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c => (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16) @@ -93,7 +117,7 @@ visibilityChanged: function (isVisible, entry) { this.isVisible = isVisible }, - contextMenu(event) { + async contextMenu(event) { if (this.nomenu.includes(this.item.type)) { return } @@ -102,6 +126,7 @@ } else { console.log(event) } + await this.isInLibrary(); let self = this let useMenu = "normal" if (app.selectedMediaItems.length <= 1) { @@ -174,9 +199,25 @@ app.selectedMediaItems = [] } }, + { + "name": "Add to Playlist...", + "action": function () { + app.promptAddToPlaylist() + } + }, + { + "name": (this.addedToLibrary) ? "Remove from Library..." : "Add to Library...", + "action": async function () { + let item_id = self.item.attributes.playParams.id ?? self.item.id; + let data_type = self.item.attributes.playParams.kind ?? self.item.type; + if (self.addedToLibrary != true) { console.log("add"); app.addToLibrary(item_id); self.addedToLibrary = true} + else { console.log("remove"); await self.removeFromLibrary(item_id); self.addedToLibrary = false}; + } + }, ] } } + if ((self.item.attributes.playParams.kind ?? self.item.type).includes("playlist")) { menus.normal.items.splice(2, 1);} CiderContextMenu.Create(event, menus[useMenu]) }, },