<commit_before>;window.Plautocompleter.Playlist = (function($) {

  var el;
  var template;

  /* Public methods */

  var initialize = function(elParam) {
    el = elParam;
    template = Handlebars.compile($("#playlist-template").html());

    $('#app').on('click', '.destroy', function() {
      $(this).parents('li').remove();
    });

    $('#app').on('click', '.preview', function() {
      $this = $(this);
      if ($this.hasClass('stopped')) {
        $this.removeClass('stopped').addClass('playing');
      } else {
        $this.removeClass('playing').addClass('stopped');
      }
    });

    $('#app').on('click', '#save', function() {
      var $playlist = $('#playlist');
      var playlistId = $playlist.data('playlist-id');
      var playlistTracks = $.map($playlist.children(), function(item) {
        return $(item).data('track-uri');
      });

      save(playlistId, playlistTracks);
    });

    $('#app').on('click', '#back', function() {
      window.Plautocompleter.Main.showView('playlists');
    });
  };

  var fetch = function(playlistUri, onSuccess) {
    var result = {};

    fetchFromSpotify(playlistUri, function(data) {
      result.id = data.id;
      result.name = data.name;
      result.tracks = $.map(data.tracks.items, function(item) {
        return {
          artist: item.track.artists[0].name,
          name: item.track.name,
          uri: item.track.uri,
          original: true
        };
      });

      fetchFromIntenseScrubland(result, function(tracks) {
        result.tracks = result.tracks.concat(tracks);
        onSuccess(result);
      });
    });
  };

  var generatePlaylist = function(playlist) {
    fetch(playlist, function(result) {
      el.html(template(result));
      window.Plautocompleter.Main.showView('playlist');

      $('ol li.original').last().next().addClass('last-original');
    });
  };

  var save = function(playlistId, playlistTracks) {
    var accessToken = window.Plautocompleter.Login.getToken();

    $.ajax({
      url: 'https://api.spotify.com/v1/users/' + window.Plautocompleter.Login.userId
        + '/playlists/' + playlistId
        + '/tracks?uris=' + playlistTracks.toString(),
      type: 'PUT',
      headers: {
        'Authorization': 'Bearer ' + accessToken
      },
      success: function(response) {
        window.Plautocompleter.Main.showView('playlists');
      }
    });
  }

  /* Private methods */

  var fetchFromSpotify = function(playlistUri, onSuccess) {
    var accessToken = window.Plautocompleter.Login.getToken();
    $.ajax({
      url: playlistUri,
      headers: {
        'Authorization': 'Bearer ' + accessToken
      },
      success: function(response) {
        onSuccess(response);
      }
    });
  };

  var fetchFromIntenseScrubland = function(playlist, onSuccess) {
    $.ajax({
      url: 'http://intense-scrubland-2743.herokuapp.com/generate_playlist/10',
      type: 'POST',
      data: JSON.stringify(playlist),
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: function(response) {
        onSuccess($.map(response.songs, function(item) {
          return {
            artist: item.artist_name,
            name: item.title,
            uri: item.id,
            original: false
          };
        }));
      }
    });
  };

  /* Export public interface */

  return {
    initialize: initialize,
    fetch: fetch,
    generatePlaylist: generatePlaylist,
    save: save
  };

})(jQuery);
<commit_msg>Added preview functionality for tracks.<commit_after>;window.Plautocompleter.Playlist = (function($) {

  var el;
  var template;
  var audioObject = null;


  /* Public methods */

  var initialize = function(elParam) {
    el = elParam;
    template = Handlebars.compile($("#playlist-template").html());

    $('#app').on('click', '.destroy', function() {
      $(this).parents('li').remove();
    });

    $('#app').on('click', '.preview', function(e) {
      e.preventDefault();
      $this = $(this);
      if ($this.hasClass('stopped')) {
        $this.removeClass('stopped').addClass('playing');
        playTrack($this.parents('li').data('track-uri'));
      } else {
        $this.removeClass('playing').addClass('stopped');
        stopTrack();
      }
    });

    $('#app').on('click', '#save', function() {
      var $playlist = $('#playlist');
      var playlistId = $playlist.data('playlist-id');
      var playlistTracks = $.map($playlist.children(), function(item) {
        return $(item).data('track-uri');
      });

      save(playlistId, playlistTracks);
    });

    $('#app').on('click', '#back', function() {
      window.Plautocompleter.Main.showView('playlists');
    });
  };

  var fetch = function(playlistUri, onSuccess) {
    var result = {};

    fetchFromSpotify(playlistUri, function(data) {
      console.log(data);
      result.id = data.id;
      result.name = data.name;
      result.tracks = $.map(data.tracks.items, function(item) {
        return {
          artist: item.track.artists[0].name,
          name: item.track.name,
          uri: item.track.uri,
          original: true
        };
      });

      fetchFromIntenseScrubland(result, function(tracks) {
        result.tracks = result.tracks.concat(tracks);
        onSuccess(result);
      });
    });
  };

  var generatePlaylist = function(playlist) {
    fetch(playlist, function(result) {
      el.html(template(result));
      window.Plautocompleter.Main.showView('playlist');

      $('ol li.original').last().next().addClass('last-original');
    });
  };

  var save = function(playlistId, playlistTracks) {
    var accessToken = window.Plautocompleter.Login.getToken();

    $.ajax({
      url: 'https://api.spotify.com/v1/users/' + window.Plautocompleter.Login.userId
        + '/playlists/' + playlistId
        + '/tracks?uris=' + playlistTracks.toString(),
      type: 'PUT',
      headers: {
        'Authorization': 'Bearer ' + accessToken
      },
      success: function(response) {
        window.Plautocompleter.Main.showView('playlists');
      }
    });
  };

  var playTrack = function(trackUrl) {
    // get the preview track from spotify
    var id = trackUrl.split(':')[2];
    var accessToken = window.Plautocompleter.Login.getToken();
    $.ajax({
      url: 'https://api.spotify.com/v1/tracks/' + id,
      headers: {
        'Authorization': 'Bearer ' + accessToken
      },
      success: function(response) {
        audioObject = new Audio(response.preview_url);
        audioObject.play();
      }
    });

  };

  var stopTrack = function() {
    audioObject.pause();
  };

  /* Private methods */

  var fetchFromSpotify = function(playlistUri, onSuccess) {
    var accessToken = window.Plautocompleter.Login.getToken();
    $.ajax({
      url: playlistUri,
      headers: {
        'Authorization': 'Bearer ' + accessToken
      },
      success: function(response) {
        onSuccess(response);
      }
    });
  };

  var fetchFromIntenseScrubland = function(playlist, onSuccess) {
    $.ajax({
      url: 'http://intense-scrubland-2743.herokuapp.com/generate_playlist/10',
      type: 'POST',
      data: JSON.stringify(playlist),
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: function(response) {
        onSuccess($.map(response.songs, function(item) {
          return {
            artist: item.artist_name,
            name: item.title,
            uri: item.id,
            original: false
          };
        }));
      }
    });
  };

  /* Export public interface */

  return {
    initialize: initialize,
    fetch: fetch,
    generatePlaylist: generatePlaylist,
    save: save
  };

})(jQuery);

{'fraction_non_alphanumeric': 0.16540864133263924, 'fraction_numerical': 0.0026028110359187923, 'mean_word_length': 2.5139460448102424, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6070962', 'n_tokens_mistral': 2400, 'n_tokens_neox': 2341, 'n_words': 390}