Skip to content

Commit

Permalink
User correct track insert position when invoking 'PLAY_NEXT' from dia…
Browse files Browse the repository at this point in the history
…log.
  • Loading branch information
jcass77 committed Jan 7, 2017
1 parent 7892218 commit 200559e
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 92 deletions.
5 changes: 5 additions & 0 deletions mopidy_musicbox_webclient/static/css/webclient.css
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ span.hostInfo {
.ui-icon-playAll:after,
.ui-icon-play:after,
.ui-icon-playNext:after,
.ui-icon-insert:after,
.ui-icon-add:after,
.ui-icon-addAll:after,
.ui-icon-remove:after {
Expand All @@ -435,6 +436,10 @@ span.hostInfo {
content: '\f149';
}

.ui-icon-insert:after {
content: '\f177';
}

.ui-icon-add:after {
content: '\f196';
}
Expand Down
8 changes: 4 additions & 4 deletions mopidy_musicbox_webclient/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,11 @@ <h2>Artists</h2>
<li data-icon="play">
<a href="#" onclick="return controls.playQueueTrack();">Play <span class="popupTrackName"></span></a>
</li>
<li data-icon="playNext">
<a href="#" onclick="return controls.showAddTrackPopup();">Insert Track After This One</a>
<li data-icon="insert">
<a href="#" onclick="return controls.showAddTrackPopup();">Insert a Track Here</a>
</li>
<li data-icon="remove">
<a href="#" onclick="return controls.removeTrack();">Remove from Queue</a>
<a href="#" onclick="return controls.removeTrack('', mopidy);">Remove from Queue</a>
</li>
<li class="popupAlbumLi">
<a href="#" onclick="showAlbumPopup('#popupQueue')">Show Album <span class="popupAlbumName"></span></a>
Expand Down Expand Up @@ -196,7 +196,7 @@ <h2>Artists</h2>
<button class="btn" type="button" onclick="return $('#popupAddTrack').popup('close');">
Cancel
</button>
<button class="btn" type="button" data-default-btn="true" onclick="return controls.addTrack($('#addTrackInput').val());">
<button class="btn" type="button" data-default-btn="true" onclick="return controls.addTrack($('#addTrackInput').val(), mopidy);">
Add
</button>
</div>
Expand Down
114 changes: 66 additions & 48 deletions mopidy_musicbox_webclient/static/js/controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,16 @@
switch (action) {
case PLAY_NOW:
case PLAY_NEXT:
if (action === PLAY_NOW || typeof index === 'undefined' || index === '') {
// Use current track as reference point for insertion.
mopidy.tracklist.index().then(function (currentIndex) {
controls._addTrackAtIndex(action, mopidy, trackUris, currentIndex)
})
} else {
// Use provided index for insertion.
controls._addTrackAtIndex(action, mopidy, trackUris, index)
}
mopidy.tracklist.index().then(function (currentIndex) {
if (currentIndex === null && action === PLAY_NEXT) {
// Tracklist is empty, start playing new tracks immediately.
action = PLAY_NOW
}
controls._addTrackAtIndex(action, mopidy, trackUris, currentIndex)
})
break
case INSERT_AT_INDEX:
controls._addTrackAtIndex(action, mopidy, trackUris, index)
break
case ADD_THIS_BOTTOM:
case ADD_ALL_BOTTOM:
Expand All @@ -89,12 +90,14 @@
throw new Error('Unexpected tracklist action identifier: ' + action)
}

if (window[$(document.body).data('on-track-click')] === DYNAMIC) {
// Save last 'action' - will become default for future 'onClick' events
var previousAction = $.cookie('onTrackClick')
if (typeof previousAction === 'undefined' || action !== previousAction) {
$.cookie('onTrackClick', action, { expires: 365 })
updatePlayIcons('', '', controls.getIconForAction(action))
if (action !== INSERT_AT_INDEX) { // TODO: Add support for 'INSERT_AT_INDEX' to allow user to insert tracks in any playlist.
if (window[$(document.body).data('on-track-click')] === DYNAMIC) {
// Save last 'action' - will become default for future 'onClick' events
var previousAction = $.cookie('onTrackClick')
if (typeof previousAction === 'undefined' || action !== previousAction) {
$.cookie('onTrackClick', action, { expires: 365 })
updatePlayIcons('', '', controls.getIconForAction(action))
}
}
}

Expand Down Expand Up @@ -125,6 +128,8 @@
return 'fa fa-play-circle'
case PLAY_NOW:
return 'fa fa-play-circle-o'
case INSERT_AT_INDEX:
return 'fa fa-long-arrow-left'
case PLAY_NEXT:
return 'fa fa-level-down'
case ADD_THIS_BOTTOM:
Expand All @@ -143,6 +148,7 @@
switch (parseInt(action)) {
case PLAY_NOW:
case PLAY_NEXT:
case INSERT_AT_INDEX:
case ADD_THIS_BOTTOM:
// Process single track
trackUris.push(trackUri)
Expand All @@ -159,19 +165,19 @@
},

_addTrackAtIndex: function (action, mopidy, trackUris, index) {
var pos
if (index !== null) {
if (action === PLAY_NOW) {
// Insert at provided index.
pos = index
} else if (action === PLAY_NEXT) {
// Insert after provided index.
pos = index + 1
}
} else {
// Insert at top of queue
if (typeof index === 'undefined' || index === '') {
throw new Error('No index provided for insertion.')
}

var pos = index
if (pos === null) {
pos = 0
}

if (action === PLAY_NEXT) {
pos = pos + 1
}

mopidy.tracklist.add({at_position: pos, uris: trackUris}).then(function (tlTracks) {
if (action === PLAY_NOW) { // Start playback immediately.
mopidy.playback.stop().then(function () {
Expand Down Expand Up @@ -203,7 +209,7 @@
/** *********************************
* remove a track from the queue *
***********************************/
removeTrack: function (tlid) {
removeTrack: function (tlid, mopidy) {
toast('Deleting...')

tlid = tlid || $('#popupQueue').data('tlid')
Expand Down Expand Up @@ -236,7 +242,7 @@
$(this).removeData('tlid')
})
var trackName = popupData[$('#popupQueue').data('track')].name
$('#select-add').append('<option value="0" selected="selected">Insert track after \'' + trackName + '\'</option>')
$('#select-add').append('<option value="6" selected="selected">Insert track before \'' + trackName + '\'</option>')
}
if (typeof songdata.track.uri !== 'undefined' && songdata.track.uri !== '') {
$('#getPlayingBtn').button('enable')
Expand All @@ -252,35 +258,47 @@
$('#popupAddTrack').popup('open')
},

addTrack: function (trackUri) {
if (parseInt($('#select-add').val()) === ADD_THIS_BOTTOM) {
controls.addTrackToBottom(trackUri)
addTrack: function (trackUri, mopidy) {
var selection = parseInt($('#select-add').val())

if (selection === ADD_THIS_BOTTOM) {
controls.addTrackToBottom(trackUri, mopidy)

} else if (selection === PLAY_NEXT) {
controls.insertTrack(trackUri, mopidy)

} else if (selection === INSERT_AT_INDEX) {
var tlid = $('#popupAddTrack').data('tlid')
controls.insertTrack(trackUri, mopidy, tlid)
} else {
controls.insertTrack(trackUri)
throw new Error('Unkown tracklist action selection option: ' + selection)
}
},

insertTrack: function (trackUri) {
if (typeof trackUri !== 'undefined' && trackUri !== '') {
var tlid = $('#popupAddTrack').data('tlid')
if (typeof tlid !== 'undefined' && tlid !== '') {
mopidy.tracklist.index({tlid: parseInt(tlid)}).then(function (index) {
controls.playTracks(PLAY_NEXT, mopidy, trackUri, 'undefined', index)
})
} else {
// No tlid provided, insert after current track.
controls.playTracks(PLAY_NEXT, mopidy, trackUri, 'undefined')
}
$('#popupAddTrack').popup('close')
insertTrack: function (trackUri, mopidy, tlid) {
if (typeof trackUri === 'undefined' || trackUri === '') {
throw new Error('No track URI provided to insert.')
}

if (typeof tlid !== 'undefined' && tlid !== '') {
mopidy.tracklist.index({tlid: parseInt(tlid)}).then(function (index) {
controls.playTracks(INSERT_AT_INDEX, mopidy, trackUri, CURRENT_PLAYLIST_TABLE, index)
})
} else {
// No tlid provided, insert after current track.
controls.playTracks(PLAY_NEXT, mopidy, trackUri, CURRENT_PLAYLIST_TABLE)
}
$('#popupAddTrack').popup('close')
return false
},

addTrackToBottom: function (trackUri) {
if (typeof trackUri !== 'undefined' && trackUri !== '') {
controls.playTracks(ADD_THIS_BOTTOM, mopidy, trackUri, 'undefined')
$('#popupAddTrack').popup('close')
addTrackToBottom: function (trackUri, mopidy) {
if (typeof trackUri === 'undefined' || trackUri === '') {
throw new Error('No track URI provided to add.')
}

controls.playTracks(ADD_THIS_BOTTOM, mopidy, trackUri, CURRENT_PLAYLIST_TABLE)
$('#popupAddTrack').popup('close')
return false
},

Expand Down
1 change: 1 addition & 0 deletions mopidy_musicbox_webclient/static/js/functionsvars.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ ADD_THIS_BOTTOM = 2
ADD_ALL_BOTTOM = 3
PLAY_ALL = 4
DYNAMIC = 5
INSERT_AT_INDEX = 6

// the first part of Mopidy extensions which serve radio streams
var radioExtensionsList = ['somafm', 'tunein', 'dirble', 'audioaddict']
Expand Down
2 changes: 1 addition & 1 deletion mopidy_musicbox_webclient/static/mb.appcache
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CACHE MANIFEST

# 2017-01-06:v2
# 2017-01-07:v1

NETWORK:
*
Expand Down
45 changes: 36 additions & 9 deletions tests/js/dummy_tracklist.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,46 @@
throw new Error('DummyTracklist.add does not support deprecated "tracks" and "uri" parameters.')
}

var position = params.at_position
// Add tracks to end of tracklist if no position is provided
params.at_position = params.at_position || this._tlTracks.length
if (typeof position === 'undefined') {
position = Math.max(0, this._tlTracks.length)
}

var tlTrack
var tlTracks = []
for (var i = 0; i < params.uris.length; i++) {
tlTrack = {
tlid: this._nextTlid++,
track: {
uri: params.uris[i]
}
}
tlTracks.push(tlTrack)
this._tlTracks.splice(params.at_position + i, 0, tlTrack)
this._tlTracks.splice(position++, 0, tlTrack)
}

return $.when(tlTracks)
return $.when(this._tlTracks)
}

/* Clears the tracklist */
DummyTracklist.prototype.clear = function () {
this._tlTracks = []
}

/* Remove the matching tracks from the tracklist */
DummyTracklist.prototype.remove = function (criteria) {
this.filter(criteria).then( function (matches) {
for (var i = 0; i < matches.length; i++) {
for (var j = 0; j < this._tlTracks.length; j++) {
if (this._tlTracks[j].track.uri === matches[i].track.uri) {
this._tlTracks.splice(j, 1)
}
}
}
}.bind(this))

return $.when(this._tlTracks)
}

/**
* Retuns a list containing tlTracks that contain the provided
* criteria.uri or has ID criteria.tlid.
Expand Down Expand Up @@ -89,9 +106,9 @@
/* Retuns index of the currently 'playing' track. */
DummyTracklist.prototype.index = function (params) {
if (!params) {
if (this._tlTracks.length > 1) {
// Always just assume that the second track is playing
return $.when(1)
if (this._tlTracks.length > 0) {
// Always just assume that the first track is playing
return $.when(0)
} else {
return $.when(null)
}
Expand All @@ -101,7 +118,17 @@
return $.when(i)
}
}
return $.when(0)
return $.when(null)
}

/* Returns the tracks in the tracklist */
DummyTracklist.prototype.get_tl_tracks = function () {
return $.when(this._tlTracks)
}

/* Returns the length of the tracklist */
DummyTracklist.prototype.get_length = function () {
return $.when(this._tlTracks.length)
}

return DummyTracklist
Expand Down
Loading

0 comments on commit 200559e

Please sign in to comment.