Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add updatePlaylist method, docs and tests. #518

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/configuration/playlists.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,8 @@ The `key` parameter is the key of the playlist such as `ancient_astronauts` up a
The `data` is any of the meta data that works for the playlist. Anything you want to pass as meta data for the playlist can be passed in a JSON object.

the `songs` is an array of song objects for your playlist. These songs will only get added to the new playlist. Each one of these can be either an index of the song in the songs array, or an entirely new song object that only gets added to the playlist.


## Updating an existing playlist

If you want to update an existing (possibly even active) playlist completely, without interrupting the state, you can use the `Amplitude.updatePlaylist( key, data, songs, active_index )` method. This takes the same parameters as the `addPlaylist` method, as well as an integer that can be used to indicate which song is currently playing.
9 changes: 9 additions & 0 deletions docs/functions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,15 @@ The second argument is all of the data describing the playlist such as `name`, `

Finally, the third argument is an array of song objects. These are the songs that will be added to the playlist.

## Update Playlist

This method allows you to update an existing playlist in AmplitudeJS. To do this, you need a unique key for your playlist, the data describing your playlist such as `title`, `author`, etc., an array of song objects for your playlist and optionally the index of the song that should be marked as active (currently playing).
This is required when mass-updating the playlist when reordering with drag&drop, for example.

```javascript
Amplitude.updatePlaylist( key, data, songs, active_index );
```

## Register Visualization

The other way to register a visualization is through the public `Amplitude.registerVisualization( visualization, preferences )` method. The first parameter being the object included with the visualization file and the second parameter being a JSON object containing any of the parameters needed to overwrite defaults provided by the visualization.
Expand Down
33 changes: 32 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,37 @@ let Amplitude = (function() {

return config.playlists[key];
} else {
Debug.writeMessage("A playlist already exists with that key!");
Debug.writeMessage("A playlist already exists with that key, try updating instead!");
return null;
}
}

/**
* Update an existing playlist.
*
* @param {string} key - The key of the playlist we are adding.
* @param {object} data - The data relating to the playlist
* @param {array} songs - The songs to add to the playlist
* @param {int|null} active_index - The active song in the updated list
*/
function updatePlaylist(key, data, songs, active_index=null) {
/*
Ensures the playlist is defined.
*/
if (config.playlists[key] !== undefined) {
/*
Recreate the playlist object.
*/
config.playlists[key] = undefined;
this.addPlaylist(key, data, songs);

if (active_index !== null) {
config.playlists[key].active_index = active_index;
}

return config.playlists[key];
} else {
Debug.writeMessage("No playlist exists with that key.");
return null;
}
}
Expand Down Expand Up @@ -1401,6 +1431,7 @@ let Amplitude = (function() {
getDelay: getDelay,
getPlayerState: getPlayerState,
addPlaylist: addPlaylist,
updatePlaylist: updatePlaylist,
registerVisualization: registerVisualization,
setPlaylistVisualization: setPlaylistVisualization,
setSongVisualization: setSongVisualization,
Expand Down
31 changes: 31 additions & 0 deletions tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,37 @@ test("AmplitudeJS can not add a playlist that already exists", () => {
expect(newPlaylist).toBeNull();
});

/**
* Ensure we can update an existing playlist in AmplitudeJS.
*/
test("Amplitude can update a playlist that already exists", () => {
let song = {
artist: "Emancipator",
name: "Test New Song",
album: "Test New Album"
};

let data = {
foo: "bar"
};

let existingPlaylist = Amplitude.addPlaylist("existing_playlist",[data], [song]);
let updatedPlaylist = Amplitude.updatePlaylist("existing_playlist",[data, data], [song, song], 1);

expect(updatedPlaylist).toBeDefined();
expect(config.playlists["test_playlist"].songs.length).toBe(2);
expect(config.playlists["test_playlist"].songs.active_index).toBe(1);
});

/**
* Ensure we can't update a playlist that doesn't exist.
*/
test("Amplitude can not update a playlist that doesn't exists", () => {
let newPlaylist = Amplitude.updatePlaylist("FooBar123456", [{}], [{}]);

expect(newPlaylist).toBeNull();
});

/**
* Ensures we can remove a song from the song array.
*/
Expand Down