123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- <script>
- import { mapState } from "vuex";
- import Toast from "toasters";
- import draggable from "vuedraggable";
- export default {
- components: { draggable },
- data() {
- return {
- orderOfPlaylists: [],
- drag: false
- };
- },
- computed: {
- ...mapState({
- station: state => state.station.station,
- myUserId: state => state.user.auth.userId
- }),
- playlists: {
- get() {
- return this.$store.state.user.playlists.playlists;
- },
- set(playlists) {
- this.$store.commit("user/playlists/setPlaylists", playlists);
- }
- },
- dragOptions() {
- return {
- animation: 200,
- group: "playlists",
- disabled: this.myUserId !== this.userId,
- ghostClass: "draggable-list-ghost"
- };
- }
- },
- mounted() {
- this.socket.on(
- "event:playlist.created",
- res => this.playlists.push(res.data.playlist),
- { replaceable: true }
- );
- this.socket.on(
- "event:playlist.deleted",
- res => {
- this.playlists.forEach((playlist, index) => {
- if (playlist._id === res.data.playlistId) {
- this.playlists.splice(index, 1);
- }
- });
- },
- { replaceable: true }
- );
- this.socket.on(
- "event:playlist.song.added",
- res => {
- this.playlists.forEach((playlist, index) => {
- if (playlist._id === res.data.playlistId) {
- this.playlists[index].songs.push(res.data.song);
- }
- });
- },
- { replaceable: true }
- );
- this.socket.on(
- "event:playlist.song.removed",
- res => {
- this.playlists.forEach((playlist, index) => {
- if (playlist._id === res.data.playlistId) {
- this.playlists[index].songs.forEach((song, index2) => {
- if (song.youtubeId === res.data.youtubeId) {
- this.playlists[index].songs.splice(index2, 1);
- }
- });
- }
- });
- },
- { replaceable: true }
- );
- this.socket.on(
- "event:playlist.displayName.updated",
- res => {
- this.playlists.forEach((playlist, index) => {
- if (playlist._id === res.data.playlistId) {
- this.playlists[index].displayName =
- res.data.displayName;
- }
- });
- },
- { replaceable: true }
- );
- this.socket.on(
- "event:playlist.privacy.updated",
- res => {
- this.playlists.forEach((playlist, index) => {
- if (playlist._id === res.data.playlist._id) {
- this.playlists[index].privacy =
- res.data.playlist.privacy;
- }
- });
- },
- { replaceable: true }
- );
- this.socket.on(
- "event:user.orderOfPlaylists.updated",
- res => {
- const sortedPlaylists = [];
- this.playlists.forEach(playlist => {
- sortedPlaylists[
- res.data.order.indexOf(playlist._id)
- ] = playlist;
- });
- this.playlists = sortedPlaylists;
- this.orderOfPlaylists = this.calculatePlaylistOrder();
- },
- { replaceable: true }
- );
- },
- methods: {
- calculatePlaylistOrder() {
- const calculatedOrder = [];
- this.playlists.forEach(playlist =>
- calculatedOrder.push(playlist._id)
- );
- return calculatedOrder;
- },
- savePlaylistOrder() {
- const recalculatedOrder = this.calculatePlaylistOrder();
- if (
- JSON.stringify(this.orderOfPlaylists) ===
- JSON.stringify(recalculatedOrder)
- )
- return; // nothing has changed
- this.socket.dispatch(
- "users.updateOrderOfPlaylists",
- recalculatedOrder,
- res => {
- if (res.status === "error") return new Toast(res.message);
- this.orderOfPlaylists = this.calculatePlaylistOrder(); // new order in regards to the database
- return new Toast(res.message);
- }
- );
- }
- }
- };
- </script>
|