<commit_before>var BaseURL = "https://linode.shellcode.in/"
var DatasetURL = document.baseURI.substring(0, document.baseURI.lastIndexOf("/") + 1) + "assets/";

function LoadList(dataset, idName, callback, attribute){
  var idName = idName === undefined ? "list" : idName;
  var template = "<li><img class='cover' src='COVER' alt='TRACK'/><div class='meta'><p class='track'>TRACK - ALBUM</p><p class='artist'>ARTIST</p></div><div class='control'><span class='count'>DOWNVOTES</span><img class='vote downvote' src='./assets/downvote.svg' alt='Downvote' data-track='TRACKID' onclick='VoteAction(this, false)' /><span class='count'>UPVOTES</span><img class='vote upvote' src='./assets/upvote.svg' alt='Upvote' data-track='TRACKID' onclick='VoteAction(this, true)' /></div></li>";
  
  document.getElementById(idName).innerHTML = "";
  
  for (var i = 0; i < dataset.length; ++i){
    var itemTemp = template;
    var itemID = dataset[i]["ID"];
    var itemUpvote = dataset[i]["upvotes"];
    var itemDownvote = dataset[i]["downvotes"];
    var itemCover = dataset[i]["images"][2]["url"];
    var itemTrack = dataset[i]["name"];
    var itemAlbum = dataset[i]["albumname"];
    var itemArtists = dataset[i]["artists"];
    var itemDuration = dataset[i]["Duration"]
    
    itemTemp = itemTemp.replace(/TRACKID/g, itemID).replace(/UPVOTES/g, itemUpvote).replace(/DOWNVOTES/g, itemDownvote).replace(/COVER/g, itemCover).replace(/TRACK/g, itemTrack).replace(/ALBUM/g, itemAlbum).replace(/ARTIST/g, itemArtists[0]["name"]);
    
    document.getElementById(idName).innerHTML += itemTemp;
  }
}

function LoadJSON(queryURL, queryParam, queryMethod, storageKey, callback, attribute){
  var httpRequest = new XMLHttpRequest();
  var queryMethod = queryMethod === undefined ? "GET" : queryMethod;
  var storageKey = storageKey === undefined ? "temp" : storageKey;
  
  httpRequest.open(queryMethod, queryURL + (queryMethod === "GET" ? "?" + $.param(queryParam) : "") , true);
  //httpRequest.setRequestHeader("Access-Control-Allow-Origin", "*");
  httpRequest.onload = function(e){
    if (httpRequest.readyState === 4 && httpRequest.status === 200){
      localStorage.setItem(storageKey, httpRequest.responseText);
      if (callback !== undefined && callback !== null)
        callback(httpRequest.responseText === "" ? undefined : JSON.parse(httpRequest.responseText), attribute);
    }
    else
      console.error(httpRequest.statusText);
  }
  // if (!queryURL.includes("file:///")) httpRequest.send(queryMethod === "GET" ? null : $.param(queryParam));
  if (!queryURL.includes("file:///"))
    if (queryMethod === "GET") httpRequest.send(null);
    else {
      var formData = new FormData();
      for (var key in queryParam) formData.append(key, queryParam[key]);
      httpRequest.send(formData);
    }
}

function VoteAction(el){
  console.log(el.getAttribute("track"));
}

function Initialize(dataset, attribute){
  var localUUID = localStorage.getItem("uuid");
  if (localUUID === null || localUUID === ""){
    identity().get(function(UUID){
      document.cookie = "uuid=" + UUID + "; max-age=86400;";
      localStorage.setItem("uuid", UUID);
    });
  }
  
  var localPoolID = localStorage.getItem("poolid");
  if (localPoolID !== null && localPoolID !== ""){
    $("#join").addClass("inactive");
    $("#pool").html("Pool: " + localPoolID);
    $("#quit, #container, #play").removeClass("inactive");
    
    var localDataset = localStorage.getItem("dataset");
    if ((dataset === undefined || dataset === null || dataset === "") 
      && localDataset !== null && localDataset !== ""){
      LoadList(JSON.parse(localDataset)["songheap"]);
    }
  }
  
  if (dataset !== undefined && dataset !== null && dataset !== ""){
    LoadList(dataset["songheap"]);
  }

}

function Finalize(dataset, attribute){
  Initialize();
  if (dataset === undefined || dataset === null || dataset === ""){
    var queryResult = LoadJSON(BaseURL + "getpool", {}, "GET", "dataset", Initialize, {});
  }
}

function JoinPool(){
  var inputPoolID = document.getElementById("poolid").value;
  if (inputPoolID !== undefined && inputPoolID !== null && inputPoolID !== ""){
    localStorage.setItem("poolid", inputPoolID);
    var queryResult = LoadJSON(BaseURL + "join_pool", {"poolShortId": inputPoolID, "userId": localStorage.getItem("uuid")}, "POST", "dataset", Finalize, {});
    // var queryResult = LoadJSON(DatasetURL + "dataset.json", {"poolShortId": inputPoolID, "userId": localStorage.getItem("uuid")}, "GET", "dataset", Initialize, {});
    //console.log(queryResult);
  }
}

function QuitPool(){
  localStorage.removeItem("poolid");
  localStorage.removeItem("dataset");
  window.location.reload(true);
}

function VoteAction(elem, upvote){
  console.log(elem.getAttribute("data-track"));
  console.log(upvote);
  //var queryResult = LoadJSON(BaseURL + "join_pool", {"poolShortId": inputPoolID, "userId": localStorage.getItem("uuid")}, "POST", "dataset", Finalize, {});
}

Initialize();

$("#quit").click(QuitPool);
$("#quit").singletap(QuitPool);<commit_msg>added upvote downvote function<commit_after>var BaseURL = "https://linode.shellcode.in/"
var DatasetURL = document.baseURI.substring(0, document.baseURI.lastIndexOf("/") + 1) + "assets/";

function LoadList(dataset, idName, callback, attribute){
  var idName = idName === undefined ? "list" : idName;
  var template = "<li><img class='cover' src='COVER' alt='TRACK'/><div class='meta'><p class='track'>TRACK - ALBUM</p><p class='artist'>ARTIST</p></div><div class='control'><span class='count'>DOWNVOTES</span><img class='vote downvote' src='./assets/downvote.svg' alt='Downvote' data-track='TRACKID' onclick='VoteAction(this, false)' /><span class='count'>UPVOTES</span><img class='vote upvote' src='./assets/upvote.svg' alt='Upvote' data-track='TRACKID' onclick='VoteAction(this, true)' /></div></li>";
  
  document.getElementById(idName).innerHTML = "";
  
  for (var i = 0; i < dataset.length; ++i){
    var itemTemp = template;
    var itemID = dataset[i]["ID"];
    var itemUpvote = dataset[i]["upvotes"];
    var itemDownvote = dataset[i]["downvotes"];
    var itemCover = dataset[i]["images"][2]["url"];
    var itemTrack = dataset[i]["name"];
    var itemAlbum = dataset[i]["albumname"];
    var itemArtists = dataset[i]["artists"];
    var itemDuration = dataset[i]["Duration"]
    
    itemTemp = itemTemp.replace(/TRACKID/g, itemID).replace(/UPVOTES/g, itemUpvote).replace(/DOWNVOTES/g, itemDownvote).replace(/COVER/g, itemCover).replace(/TRACK/g, itemTrack).replace(/ALBUM/g, itemAlbum).replace(/ARTIST/g, itemArtists[0]["name"]);
    
    document.getElementById(idName).innerHTML += itemTemp;
  }
}

function LoadJSON(queryURL, queryParam, queryMethod, storageKey, callback, attribute){
  var httpRequest = new XMLHttpRequest();
  var queryMethod = queryMethod === undefined ? "GET" : queryMethod;
  var storageKey = storageKey === undefined ? "temp" : storageKey;
  
  httpRequest.open(queryMethod, queryURL + (queryMethod === "GET" ? "?" + $.param(queryParam) : "") , true);
  //httpRequest.setRequestHeader("Access-Control-Allow-Origin", "*");
  httpRequest.onload = function(e){
    if (httpRequest.readyState === 4 && httpRequest.status === 200){
      localStorage.setItem(storageKey, httpRequest.responseText);
      if (callback !== undefined && callback !== null)
        callback(httpRequest.responseText === "" ? undefined : JSON.parse(httpRequest.responseText), attribute);
    }
    else
      console.error(httpRequest.statusText);
  }
  // if (!queryURL.includes("file:///")) httpRequest.send(queryMethod === "GET" ? null : $.param(queryParam));
  if (!queryURL.includes("file:///"))
    if (queryMethod === "GET") httpRequest.send(null);
    else {
      var formData = new FormData();
      for (var key in queryParam) formData.append(key, queryParam[key]);
      httpRequest.send(formData);
    }
}

function VoteAction(el){
  console.log(el.getAttribute("track"));
}

function Initialize(dataset, attribute){
  var localUUID = localStorage.getItem("uuid");
  if (localUUID === null || localUUID === ""){
    identity().get(function(UUID){
      document.cookie = "uuid=" + UUID + "; max-age=86400;";
      localStorage.setItem("uuid", UUID);
    });
  }
  
  var localPoolID = localStorage.getItem("poolid");
  if (localPoolID !== null && localPoolID !== ""){
    $("#join").addClass("inactive");
    $("#pool").html("Pool: " + localPoolID);
    $("#quit, #container, #play").removeClass("inactive");
    
    var localDataset = localStorage.getItem("dataset");
    if ((dataset === undefined || dataset === null || dataset === "") 
      && localDataset !== null && localDataset !== ""){
      LoadList(JSON.parse(localDataset)["songheap"]);
    }
  }
  
  if (dataset !== undefined && dataset !== null && dataset !== ""){
    LoadList(dataset["songheap"]);
  }

}

function Finalize(dataset, attribute){
  Initialize();
  if (dataset === undefined || dataset === null || dataset === ""){
    var queryResult = LoadJSON(BaseURL + "getpool", {}, "GET", "dataset", Initialize, {});
  }
}

function JoinPool(){
  var inputPoolID = document.getElementById("poolid").value;
  if (inputPoolID !== undefined && inputPoolID !== null && inputPoolID !== ""){
    localStorage.setItem("poolid", inputPoolID);
    var queryResult = LoadJSON(BaseURL + "join_pool", {"poolShortId": inputPoolID, "userId": localStorage.getItem("uuid")}, "POST", "temp", Finalize, {});
    // var queryResult = LoadJSON(DatasetURL + "dataset.json", {"poolShortId": inputPoolID, "userId": localStorage.getItem("uuid")}, "GET", "dataset", Initialize, {});
    //console.log(queryResult);
  }
}

function QuitPool(){
  localStorage.removeItem("poolid");
  localStorage.removeItem("dataset");
  window.location.reload(true);
}

function VoteAction(elem, upvote){
  console.log(elem.getAttribute("data-track"));
  var queryResult = LoadJSON(BaseURL + upvote ? "upvote" : "downvote", {"songId": elem.getAttribute("data-track"), "userId": localStorage.getItem("uuid")}, "POST", "temp", Finalize, {});
}

Initialize();

$("#quit").click(QuitPool);
$("#quit").singletap(QuitPool);
{'fraction_non_alphanumeric': 0.19391785150078988, 'fraction_numerical': 0.002764612954186414, 'mean_word_length': 4.79462242562929, 'pattern_counts': {'":': 12, '<': 39, '<?xml version=': 0, '>': 37, 'https://': 2, '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': '3908805', 'n_tokens_mistral': 3169, 'n_tokens_neox': 3172, 'n_words': 603}