koel/resources/assets/js/components/modals/edit-songs-form.vue

348 lines
9.7 KiB
Vue
Raw Normal View History

2016-03-05 09:01:12 +00:00
<template>
2016-11-30 09:23:11 +00:00
<div id="editSongsOverlay" v-if="shown" class="overlay">
2016-06-25 16:05:24 +00:00
<sound-bar v-if="loading"></sound-bar>
<form v-else @submit.prevent="submit">
<header>
2016-10-31 08:15:32 +00:00
<img :src="coverUrl" width="96" height="96">
2016-06-25 16:05:24 +00:00
<hgroup class="meta">
<h1 :class="{ mixed: !editSingle }">{{ displayedTitle }}</h1>
<h2 :class="{ mixed: !bySameArtist && !formData.artistName }">
{{ bySameArtist || formData.artistName ? formData.artistName : 'Mixed Artists' }}
</h2>
<h2 :class="{ mixed: !inSameAlbum && !formData.albumName }">
{{ inSameAlbum || formData.albumName ? formData.albumName : 'Mixed Albums' }}
</h2>
</hgroup>
</header>
<div>
<div class="tabs tabs-white">
<div class="header clear">
<a @click.prevent="currentView = 'details'"
:class="{ active: currentView === 'details' }">Details</a>
<a @click.prevent="currentView = 'lyrics'" v-show="editSingle"
:class="{ active: currentView === 'lyrics' }">Lyrics</a>
</div>
<div class="panes">
<div v-show="currentView === 'details'">
<div class="form-row" v-if="editSingle">
<label>Title</label>
2016-11-15 07:54:41 +00:00
<input name="title" type="text" v-model="formData.title">
2016-06-25 16:05:24 +00:00
</div>
<div class="form-row">
<label>Artist</label>
<typeahead
:items="artistState.artists"
:options="artistTypeaheadOptions"
2016-10-31 04:28:12 +00:00
v-model="formData.artistName"/>
2016-06-25 16:05:24 +00:00
</div>
<div class="form-row">
<label>Album</label>
<typeahead
:items="albumState.albums"
:options="albumTypeaheadOptions"
2016-10-31 04:28:12 +00:00
v-model="formData.albumName"/>
2016-06-25 16:05:24 +00:00
</div>
<div class="form-row">
<label class="small">
<input type="checkbox" @change="changeCompilationState" ref="compilationStateChk" />
Album is a compilation of songs by various artists
</label>
<label class="small warning" v-if="needsReload">
Koel will reload after saving.
</label>
</div>
<div class="form-row" v-show="editSingle">
<label>Track</label>
2017-01-20 02:55:33 +00:00
<input name="track" type="text" pattern="\d*" v-model="formData.track"
title="Empty or a number">
2016-06-25 16:05:24 +00:00
</div>
2016-03-05 09:01:12 +00:00
</div>
2016-06-25 16:05:24 +00:00
<div v-show="currentView === 'lyrics' && editSingle">
<div class="form-row">
2016-10-31 04:28:12 +00:00
<textarea v-model="formData.lyrics"/>
2016-06-25 16:05:24 +00:00
</div>
</div>
</div>
</div>
</div>
<footer>
2016-10-31 04:28:12 +00:00
<input type="submit" value="Update">
2016-06-25 16:05:24 +00:00
<a @click.prevent="close" class="btn btn-white">Cancel</a>
</footer>
</form>
</div>
2016-03-05 09:01:12 +00:00
</template>
<script>
2016-11-28 10:27:59 +00:00
import { every, filter } from 'lodash'
import { br2nl, forceReloadWindow } from '../../utils'
import { songInfo } from '../../services/info'
import { artistStore, albumStore, songStore } from '../../stores'
import soundBar from '../shared/sound-bar.vue'
import typeahead from '../shared/typeahead.vue'
const COMPILATION_STATES = {
NONE: 0, // No songs belong to a compilation album
ALL: 1, // All songs belong to compilation album(s)
SOME: 2 // Some of the songs belong to compilation album(s)
}
export default {
components: { soundBar, typeahead },
data () {
return {
shown: false,
songs: [],
currentView: '',
loading: false,
needsReload: false,
artistState: artistStore.state,
artistTypeaheadOptions: {
displayKey: 'name',
filterKey: 'name'
2016-06-25 16:05:24 +00:00
},
2016-11-28 10:27:59 +00:00
albumState: albumStore.state,
albumTypeaheadOptions: {
displayKey: 'name',
filterKey: 'name'
2016-06-25 16:05:24 +00:00
},
/**
2016-11-28 10:27:59 +00:00
* In order not to mess up the original songs, we manually assign and manipulate
* their attributes.
2016-06-25 16:05:24 +00:00
*
2016-11-28 10:27:59 +00:00
* @type {Object}
2016-06-25 16:05:24 +00:00
*/
2016-11-28 10:27:59 +00:00
formData: {
title: '',
albumName: '',
artistName: '',
lyrics: '',
track: '',
compilationState: null
}
}
},
computed: {
/**
* Determine if we're editing but one song.
*
* @return {boolean}
*/
editSingle () {
return this.songs.length === 1
},
2016-06-25 16:05:24 +00:00
2016-11-28 10:27:59 +00:00
/**
* Determine if all songs we're editing are by the same artist.
*
* @return {boolean}
*/
bySameArtist () {
2016-12-04 12:40:25 +00:00
return every(this.songs, song => song.artist.id === this.songs[0].artist.id)
2016-11-28 10:27:59 +00:00
},
2016-10-31 08:15:32 +00:00
2016-11-28 10:27:59 +00:00
/**
* Determine if all songs we're editing are from the same album.
*
* @return {boolean}
*/
inSameAlbum () {
2016-12-04 12:40:25 +00:00
return every(this.songs, song => song.album.id === this.songs[0].album.id)
2016-11-28 10:27:59 +00:00
},
2016-06-25 16:05:24 +00:00
2016-11-28 10:27:59 +00:00
/**
* URL of the cover to display.
*
* @return {string}
*/
coverUrl () {
return this.inSameAlbum ? this.songs[0].album.cover : '/public/img/covers/unknown-album.png'
},
2016-06-25 16:05:24 +00:00
2016-11-28 10:27:59 +00:00
/**
* Determine the compilation state of the songs.
*
* @return {Number}
*/
compilationState () {
const contributedSongs = filter(this.songs, song => song.contributing_artist_id)
if (!contributedSongs.length) {
this.formData.compilationState = COMPILATION_STATES.NONE
} else if (contributedSongs.length === this.songs.length) {
this.formData.compilationState = COMPILATION_STATES.ALL
} else {
this.formData.compilationState = COMPILATION_STATES.SOME
}
2016-06-25 16:05:24 +00:00
2016-11-28 10:27:59 +00:00
return this.formData.compilationState
},
2016-06-25 16:05:24 +00:00
2016-11-28 10:27:59 +00:00
/**
* The song title to be displayed.
*
* @return {string}
*/
displayedTitle () {
return this.editSingle ? this.formData.title : `${this.songs.length} songs selected`
},
2016-06-25 16:05:24 +00:00
2016-11-28 10:27:59 +00:00
/**
* The album name to be displayed.
*
* @return {string}
*/
displayedAlbum () {
if (this.editSingle) {
return this.formData.albumName
} else {
return this.formData.albumName ? this.formData.albumName : 'Mixed Albums'
2016-11-26 03:25:35 +00:00
}
2016-06-25 16:05:24 +00:00
},
2016-11-28 10:27:59 +00:00
/**
* The artist name to be displayed.
*
* @return {string}
*/
displayedArtist () {
if (this.editSingle) {
return this.formData.artistName
} else {
return this.formData.artistName ? this.formData.artistName : 'Mixed Artists'
}
}
},
methods: {
open (songs) {
this.shown = true
this.songs = songs
this.currentView = 'details'
this.needsReload = false
if (this.editSingle) {
this.formData.title = this.songs[0].title
this.formData.albumName = this.songs[0].album.name
this.formData.artistName = this.songs[0].artist.name
// If we're editing only one song and the song's info (including lyrics)
// hasn't been loaded, load it now.
if (!this.songs[0].infoRetrieved) {
this.loading = true
songInfo.fetch(this.songs[0]).then(r => {
this.loading = false
2016-11-26 03:25:35 +00:00
this.formData.lyrics = br2nl(this.songs[0].lyrics)
2017-01-20 02:55:33 +00:00
this.formData.track = this.songs[0].track || ''
2016-11-26 03:25:35 +00:00
this.initCompilationStateCheckbox()
2016-11-28 10:27:59 +00:00
})
2016-06-25 16:05:24 +00:00
} else {
2016-11-28 10:27:59 +00:00
this.formData.lyrics = br2nl(this.songs[0].lyrics)
2017-01-20 02:55:33 +00:00
this.formData.track = this.songs[0].track || ''
2016-11-26 03:25:35 +00:00
this.initCompilationStateCheckbox()
2016-06-25 16:05:24 +00:00
}
2016-11-28 10:27:59 +00:00
} else {
this.formData.albumName = this.inSameAlbum ? this.songs[0].album.name : ''
this.formData.artistName = this.bySameArtist ? this.songs[0].artist.name : ''
this.loading = false
this.initCompilationStateCheckbox()
}
},
2016-06-25 16:05:24 +00:00
2016-11-28 10:27:59 +00:00
/**
* Initialize the compilation state's checkbox of the editing songs' album(s).
*/
initCompilationStateCheckbox () {
// This must be wrapped in a $nextTick callback, because the form is dynamically
// attached into DOM in conjunction with `this.loading` data binding.
this.$nextTick(() => {
const chk = this.$refs.compilationStateChk
switch (this.compilationState) {
case COMPILATION_STATES.ALL:
chk.checked = true
chk.indeterminate = false
break
case COMPILATION_STATES.NONE:
chk.checked = false
chk.indeterminate = false
break
default:
chk.checked = false
chk.indeterminate = true
break
}
})
},
2016-06-25 16:05:24 +00:00
2016-11-28 10:27:59 +00:00
/**
* Manually set the compilation state.
* We can't use v-model here due to the tri-state nature of the property.
* Also, following iTunes style, we don't support circular switching of the states -
* once the user clicks the checkbox, there's no going back to indeterminate state.
*/
changeCompilationState (e) {
this.formData.compilationState = e.target.checked ? COMPILATION_STATES.ALL : COMPILATION_STATES.NONE
this.needsReload = true
},
2016-06-25 16:05:24 +00:00
2016-11-28 10:27:59 +00:00
/**
* Close the modal.
*/
close () {
this.shown = false
},
2016-06-25 16:05:24 +00:00
2016-11-28 10:27:59 +00:00
/**
* Submit the form.
*/
submit () {
this.loading = true
songStore.update(this.songs, this.formData).then(r => {
this.loading = false
this.close()
this.needsReload && forceReloadWindow()
}).catch(r => {
this.loading = false
})
2016-11-26 03:25:35 +00:00
}
}
2016-11-28 10:27:59 +00:00
}
2016-03-05 09:01:12 +00:00
</script>
2017-02-14 06:53:02 +00:00
<style lang="scss">
2016-11-28 10:27:59 +00:00
@import "../../../sass/partials/_vars.scss";
@import "../../../sass/partials/_mixins.scss";
2016-06-25 16:05:24 +00:00
2016-11-28 10:27:59 +00:00
#editSongsOverlay {
form {
> header {
img {
flex: 0 0 96px;
}
2016-06-25 16:05:24 +00:00
2016-11-28 10:27:59 +00:00
.meta {
flex: 1;
padding-left: 8px;
.mixed {
opacity: .5;
2016-03-05 09:01:12 +00:00
}
2016-06-25 16:05:24 +00:00
}
2016-03-05 09:01:12 +00:00
}
2016-06-25 16:05:24 +00:00
}
2016-11-28 10:27:59 +00:00
}
2016-03-05 09:01:12 +00:00
</style>