Browse Source

Improved songs/queue admin page and the edit song modal.

KrisVos130 8 years ago
parent
commit
ab7fba9af4

+ 64 - 28
frontend/components/Admin/QueueSongs.vue

@@ -25,9 +25,9 @@
 					<td>{{ song.genres.join(', ') }}</td>
 					<td>{{ song.requestedBy }}</td>
 					<td>
-						<a class='button is-primary' href='#' @click='edit(song, index)'>Edit</a>
-						<a class='button is-success' href='#' @click='add(song)'>Add</a>
-						<a class='button is-danger' href='#' @click='remove(song._id, index)'>Remove</a>
+						<button class='button is-primary' @click='edit(song, index)'>Edit</button>
+						<button class='button is-success' @click='add(song)'>Add</button>
+						<button class='button is-danger' @click='remove(song._id, index)'>Remove</button>
 					</td>
 				</tr>
 			</tbody>
@@ -55,29 +55,32 @@
 				video: {
 					player: null,
 					paused: false,
-					settings: function (type) {
-						switch(type) {
-							case 'stop':
-								this.player.stopVideo();
-								this.paused = true;
-								break;
-							case 'pause':
-								this.player.pauseVideo();
-								this.paused = true;
-								break;
-							case 'play':
-								this.player.playVideo();
-								this.paused = false;
-								break;
-							case 'skipToLast10Secs':
-								this.player.seekTo(this.player.getDuration() - 10);
-								break;
-						}
-					}
-				}
+					playerReady: false
+				},
+				timeout: 0
 			}
 		},
 		methods: {
+			settings: function (type) {
+				let _this = this;
+				switch(type) {
+					case 'stop':
+						_this.video.player.stopVideo();
+						_this.video.paused = true;
+						break;
+					case 'pause':
+						_this.video.player.pauseVideo();
+						_this.video.paused = true;
+						break;
+					case 'play':
+						_this.video.player.playVideo();
+						_this.video.paused = false;
+						break;
+					case 'skipToLast10Secs':
+						_this.video.player.seekTo((_this.editing.song.duration - 10) + _this.editing.song.skipDuration);
+						break;
+				}
+			},
 			changeVolume: function() {
 				let local = this;
 				let volume = $("#volumeSlider").val();
@@ -87,7 +90,7 @@
 			},
 			toggleModal: function () {
 				this.isEditActive = !this.isEditActive;
-				this.video.settings('stop');
+				this.settings('stop');
 			},
 			addTag: function (type) {
 				if (type == 'genres') {
@@ -112,7 +115,7 @@
 			},
 			edit: function (song, index) {
 				if (this.video.player) {
-					this.video.player.loadVideoById(song._id);
+					this.video.player.loadVideoById(song._id, this.editing.song.skipDuration);
 					let songCopy = {};
 					for (let n in song) {
 						songCopy[n] = song[n];
@@ -121,11 +124,22 @@
 					this.isEditActive = true;
 				}
 			},
-			save: function (song) {
+			save: function (song, close) {
 				let _this = this;
 				this.socket.emit('queueSongs.update', song._id, song, function (res) {
-					if (res.status == 'success' || res.status == 'error') Toast.methods.addToast(res.message, 2000);
-					_this.toggleModal();
+					Toast.methods.addToast(res.message, 4000);
+					if (res.status === 'success') {
+						_this.songs.forEach((lSong) => {
+							if (song._id === lSong._id) {
+								for (let n in song) {
+									lSong[n] = song[n];
+								}
+							}
+						});
+					}
+					if (close) {
+						_this.toggleModal();
+					}
 				});
 			},
 			add: function (song) {
@@ -166,26 +180,48 @@
 				});
 			});
 
+			setInterval(() => {
+				if (_this.video.paused === false && _this.playerReady && _this.video.player.getCurrentTime() - _this.editing.song.skipDuration > _this.editing.song.duration) {
+					_this.video.paused = false;
+					_this.video.player.stopVideo();
+				}
+			}, 200);
+
 			this.video.player = new YT.Player('player', {
 				height: 315,
 				width: 560,
 				videoId: this.editing.song._id,
 				playerVars: { controls: 0, iv_load_policy: 3, rel: 0, showinfo: 0 },
+				startSeconds: _this.editing.song.skipDuration,
 				events: {
 					'onReady': () => {
 						let volume = parseInt(localStorage.getItem("volume"));
 						volume = (typeof volume === "number") ? volume : 20;
+						_this.video.player.seekTo(_this.editing.song.skipDuration);
 						_this.video.player.setVolume(volume);
 						if (volume > 0) _this.video.player.unMute();
+						_this.playerReady = true;
 					},
 					'onStateChange': event => {
 						if (event.data === 1) {
+							_this.video.paused = false;
 							let youtubeDuration = _this.video.player.getDuration();
 							youtubeDuration -= _this.editing.song.skipDuration;
 							if (_this.editing.song.duration > youtubeDuration) {
 								this.video.player.stopVideo();
+								_this.video.paused = true;
 								Toast.methods.addToast("Video can't play. Specified duration is bigger than the YouTube song duration.", 4000);
+							} else if (_this.editing.song.duration <= 0) {
+								this.video.player.stopVideo();
+								_this.video.paused = true;
+								Toast.methods.addToast("Video can't play. Specified duration has to be more than 0 seconds.", 4000);
+							}
+
+							if (_this.video.player.getCurrentTime() < _this.editing.song.skipDuration) {
+								_this.video.player.seekTo(10);
 							}
+						} else if (event.data === 2) {
+							this.video.paused = true;
 						}
 					}
 				}

+ 62 - 27
frontend/components/Admin/Songs.vue

@@ -25,8 +25,8 @@
 					<td>{{ song.genres.join(', ') }}</td>
 					<td>{{ song.requestedBy }}</td>
 					<td>
-						<a class='button is-primary' href='#' @click='edit(song, index)'>Edit</a>
-						<a class='button is-danger' href='#' @click='remove(song._id, index)'>Remove</a>
+						<button class='button is-primary' @click='edit(song, index)'>Edit</button>
+						<button class='button is-danger' @click='remove(song._id, index)'>Remove</button>
 					</td>
 				</tr>
 			</tbody>
@@ -54,29 +54,31 @@
 				video: {
 					player: null,
 					paused: false,
-					settings: function (type) {
-						switch(type) {
-							case 'stop':
-								this.player.stopVideo();
-								this.paused = true;
-								break;
-							case 'pause':
-								this.player.pauseVideo();
-								this.paused = true;
-								break;
-							case 'play':
-								this.player.playVideo();
-								this.paused = false;
-								break;
-							case 'skipToLast10Secs':
-								this.player.seekTo(this.player.getDuration() - 10);
-								break;
-						}
-					}
+					playerReady: false
 				}
 			}
 		},
 		methods: {
+			settings: function (type) {
+				let _this = this;
+				switch(type) {
+					case 'stop':
+						_this.video.player.stopVideo();
+						_this.video.paused = true;
+						break;
+					case 'pause':
+						_this.video.player.pauseVideo();
+						_this.video.paused = true;
+						break;
+					case 'play':
+						_this.video.player.playVideo();
+						_this.video.paused = false;
+						break;
+					case 'skipToLast10Secs':
+						_this.video.player.seekTo((_this.editing.song.duration - 10) + _this.editing.song.skipDuration);
+						break;
+				}
+			},
 			changeVolume: function() {
 				let local = this;
 				let volume = $("#volumeSlider").val();
@@ -86,7 +88,7 @@
 			},
 			toggleModal: function () {
 				this.isEditActive = !this.isEditActive;
-				this.video.settings('stop');
+				this.settings('stop');
 			},
 			addTag: function (type) {
 				if (type == 'genres') {
@@ -111,7 +113,7 @@
 			},
 			edit: function (song, index) {
 				if (this.video.player) {
-					this.video.player.loadVideoById(song._id);
+					this.video.player.loadVideoById(song._id, this.editing.song.skipDuration);
 					let songCopy = {};
 					for (let n in song) {
 						songCopy[n] = song[n];
@@ -120,11 +122,22 @@
 					this.isEditActive = true;
 				}
 			},
-			save: function (song) {
+			save: function (song, close) {
 				let _this = this;
 				this.socket.emit('songs.update', song._id, song, function (res) {
-					if (res.status == 'success' || res.status == 'error') Toast.methods.addToast(res.message, 2000);
-					_this.toggleModal();
+					Toast.methods.addToast(res.message, 4000);
+					if (res.status === 'success') {
+						_this.songs.forEach((lSong) => {
+							if (song._id === lSong._id) {
+								for (let n in song) {
+									lSong[n] = song[n];
+								}
+							}
+						});
+					}
+					if (close) {
+						_this.toggleModal();
+					}
 				});
 			},
 			remove: function (id, index) {
@@ -161,26 +174,48 @@
 				});
 			});
 
+			setInterval(() => {
+				if (_this.video.paused === false && _this.playerReady && _this.video.player.getCurrentTime() - _this.editing.song.skipDuration > _this.editing.song.duration) {
+					_this.video.paused = false;
+					_this.video.player.stopVideo();
+				}
+			}, 200);
+
 			this.video.player = new YT.Player('player', {
 				height: 315,
 				width: 560,
 				videoId: this.editing.song._id,
 				playerVars: { controls: 0, iv_load_policy: 3, rel: 0, showinfo: 0 },
+				startSeconds: _this.editing.song.skipDuration,
 				events: {
 					'onReady': () => {
 						let volume = parseInt(localStorage.getItem("volume"));
 						volume = (typeof volume === "number") ? volume : 20;
+						_this.video.player.seekTo(_this.editing.song.skipDuration);
 						_this.video.player.setVolume(volume);
 						if (volume > 0) _this.video.player.unMute();
+						_this.playerReady = true;
 					},
 					'onStateChange': event => {
-						if (event.data == 1) {
+						if (event.data === 1) {
+							_this.video.paused = false;
 							let youtubeDuration = _this.video.player.getDuration();
 							youtubeDuration -= _this.editing.song.skipDuration;
 							if (_this.editing.song.duration > youtubeDuration) {
 								this.video.player.stopVideo();
+								_this.video.paused = true;
 								Toast.methods.addToast("Video can't play. Specified duration is bigger than the YouTube song duration.", 4000);
+							} else if (_this.editing.song.duration <= 0) {
+								this.video.player.stopVideo();
+								_this.video.paused = true;
+								Toast.methods.addToast("Video can't play. Specified duration has to be more than 0 seconds.", 4000);
+							}
+
+							if (_this.video.player.getCurrentTime() < _this.editing.song.skipDuration) {
+								_this.video.player.seekTo(10);
 							}
+						} else if (event.data === 2) {
+							this.video.paused = true;
 						}
 					}
 				}

+ 19 - 15
frontend/components/Modals/EditSong.vue

@@ -14,18 +14,18 @@
 							</p>
 						</form>
 						<p class='control has-addons'>
-							<a class='button' @click='$parent.video.settings("pause")' v-if='!$parent.video.paused' href='#'>
+							<button class='button' @click='$parent.settings("pause")' v-if='!$parent.video.paused'>
 								<i class='material-icons'>pause</i>
-							</a>
-							<a class='button' @click='$parent.video.settings("play")' v-if='$parent.video.paused' href='#'>
+							</button>
+							<button class='button' @click='$parent.settings("play")' v-if='$parent.video.paused'>
 								<i class='material-icons'>play_arrow</i>
-							</a>
-							<a class='button' @click='$parent.video.settings("stop")' href='#'>
+							</button>
+							<button class='button' @click='$parent.settings("stop")'>
 								<i class='material-icons'>stop</i>
-							</a>
-							<a class='button' @click='$parent.video.settings("skipToLast10Secs")' href='#'>
+							</button>
+							<button class='button' @click='$parent.settings("skipToLast10Secs")'>
 								<i class='material-icons'>fast_forward</i>
-							</a>
+							</button>
 						</p>
 					</div>
 				</div>
@@ -66,7 +66,7 @@
 						<div>
 							<p class='control has-addons'>
 								<input class='input' id='new-artist' type='text' placeholder='Artist'>
-								<a class='button is-info' @click='$parent.addTag("artists")' href='#'>Add Artist</a>
+								<button class='button is-info' @click='$parent.addTag("artists")'>Add Artist</button>
 							</p>
 							<span class='tag is-info' v-for='(index, artist) in $parent.editing.song.artists' track-by='$index'>
 								{{ artist }}
@@ -76,7 +76,7 @@
 						<div>
 							<p class='control has-addons'>
 								<input class='input' id='new-genre' type='text' placeholder='Genre'>
-								<a class='button is-info' @click='$parent.addTag("genres")' href='#'>Add Genre</a>
+								<button class='button is-info' @click='$parent.addTag("genres")'>Add Genre</button>
 							</p>
 							<span class='tag is-info' v-for='(index, genre) in $parent.editing.song.genres' track-by='$index'>
 								{{ genre }}
@@ -96,13 +96,17 @@
 
 			</section>
 			<footer class='modal-card-foot'>
-				<a class='button is-success' @click='$parent.save($parent.editing.song)' href='#'>
+				<button class='button is-success' @click='$parent.save($parent.editing.song, false)'>
 					<i class='material-icons save-changes'>done</i>
 					<span>&nbsp;Save</span>
-				</a>
-				<a class='button is-danger' @click='$parent.toggleModal()' href='#'>
-					<span>&nbsp;Cancel</span>
-				</a>
+				</button>
+				<button class='button is-success' @click='$parent.save($parent.editing.song, true)'>
+					<i class='material-icons save-changes'>done</i>
+					<span>&nbsp;Save and close</span>
+				</button>
+				<button class='button is-danger' @click='$parent.toggleModal()'>
+					<span>&nbsp;Close</span>
+				</button>
 			</footer>
 		</div>
 	</div>