(function () { "use strict"; angular.module("SportlogiqWebApp.services") .service("usersPlaylists", UsersPlaylists); UsersPlaylists.$inject = ["$timeout", "$rootScope", "$http", "apiEndpoint", "userCompany", "mixpanelAPI"]; function UsersPlaylists($timeout, $rootScope, $http, apiEndpoint, userCompany, mixpanelAPI) { var that = this; // Private Members var ENDPOINT = apiEndpoint + "playlists"; // Public Members this.playlists = []; this.playlistsSharedWithUser = []; this.playbackContext = {}; this.playbackHandlers = undefined; this.eventPlayerOpen = false; this.shareWithEveryone = false; this.currentlyPlaying = undefined; this.loadingPlaylists = false; this.loadingPlaylistsShares = false; // Public Methods this.getPlaylists = getPlaylists; this.getPlaylistDetails = getPlaylistDetails; this.getPlaylistsSharedWithUser = getPlaylistsSharedWithUser; this.playEventsInPlaylist = playEventsInPlaylist; this.restartPlaylist = restartPlaylist; this.revokeShareFromUser = revokeShareFromUser; this.deletePlaylist = deletePlaylist; //////////////////// /** * Gets all playlists the current user has created * @return {number} - The status of the request if failed */ function getPlaylists() { that.loadingPlaylists = true; return $http.get(ENDPOINT) .success(function (data) { data.map(function (value, index, array) { if (value.creationTime && typeof value.creationTime === 'string') { // Set value.creationTime to an actual Date object so that angular // can use the builtin 'date' filter. return value.creationTime = new Date(value.creationTime); } }); that.playlists = data; }) .error(function (data, status) { return status; }) .then(function() { that.loadingPlaylists = false; }) } /** * Gets specific details of playlist passed in by view * @param {object} playlist - The playlist the user is actioning on (passed by view) * @returns {object} - Returns shares, items and status in an object */ function getPlaylistDetails(playlist) { return $http.get(ENDPOINT + "/" + playlist.id) .success(function (data) { that.playbackContext = data.items; return { shares: data.shares, items: data.items, status: status }; }) .error(function (data, status) { return { data: data, status: status }; }); } function getPlaylistsSharedWithUser() { that.loadingPlaylistsShares = true; return $http.get(apiEndpoint + "playlistshares") .success(function (data) { that.playlistsSharedWithUser = data.map(function (value, index, array) { if (value.shareTime && typeof value.shareTime === 'string') { // Set value.creationTime to an actual Date object so that angular // can use the builtin 'date' filter. value.shareTime = new Date(value.shareTime); return value; } }).filter(function (value) { // Don't show results that were shared with everyone... // which includes the current user return value.target.isEveryone === false; }); return status; }) .error(function (data, status) { console.error(data); return status; }) .then(function() { that.loadingPlaylistsShares = false; }); } function playEventsInPlaylist(playlist) { if (that.eventPlayerOpen) { that.eventPlayerOpen = false; that.currentlyPlaying = undefined; that.playbackHandlers.deactivateVideo(); $rootScope.$broadcast('eventPlayer::Closed'); return; } that.eventPlayerOpen = true; $rootScope.$broadcast('eventSelection::Reset'); return that.getPlaylistDetails(playlist).then(function (data) { that.playbackContext.list = data.data.items .filter(function (value) { //only take items with an event (v2 introduced the concept of an item with no event) return !!value.legacyV1 && !!value.legacyV1.event && !!value.legacyV1.event.id; }).map(function (value) { var video = data.data.videoRefs.filter(function (vid) { return vid.id === value.videoId })[0] || {}; var legacyEvent = value.legacyV1; return { id: legacyEvent.event.id, name: legacyEvent.event.name, frame: legacyEvent.event.frameNum, period: legacyEvent.event.period, periodTime: legacyEvent.event.periodTime, shorthand: legacyEvent.event.shorthand, gameTime: legacyEvent.event.gameTime, date: legacyEvent.game.date, playerInfo: { id: legacyEvent.player.id, firstName: legacyEvent.player.firstName, lastName: legacyEvent.player.lastName, playerPosition: legacyEvent.player.position, team: { id: legacyEvent.team.id, location: legacyEvent.team.location, name: legacyEvent.team.name } }, vidparam: { fileName: video.filename, frameRate: video.frameRate, rootURL: video.rootUrl } }; }); // Hack to make sure that the playbackHandlers are // instantiated properly... $timeout(function () { if (that.playbackContext.list.length) { that.playbackHandlers.playbackEventList(that.playbackContext.list); } else { alert("Oops! Couldn't play that playlist"); that.eventPlayerOpen = false; that.playbackHandlers.deactivateVideo(); } }, 1000); }) .catch(function (error) { alert("Oops! Couldn't load that playlist"); that.eventPlayerOpen = false; that.playbackHandlers.deactivateVideo(); }); } function restartPlaylist(playlist) { that.playbackHandlers.deactivateVideo(); that.eventPlayerOpen = false; that.playEventsInPlaylist(playlist); } function revokeShareFromUser(share, playlist, context) { context.playlist = playlist; context.sharedWith = share.target; return $http.delete(apiEndpoint + "playlistshares/" + share.id) .then(function (response) { if (playlist.shareCount == 1) { playlist.isExpanded = false; } playlist.shares = playlist.shares.filter(function (value) { return value.id !== share.id; }); playlist.shareCount = playlist.shares.length; mixpanelAPI.send('revokePlaylistShareFromUser', context); }) .catch(function (error) { console.error(error); }); } function deletePlaylist(playlist, context) { context.playlist = playlist; return $http.delete(ENDPOINT + "/" + playlist.id) .success(function (data, status) { that.playlists = that.playlists.filter(function (value) { return value.id !== playlist.id; }); mixpanelAPI.send('deleteEventPlaylist', context); return status; }) .error(function (data, status) { return status; }); } } })();