<commit_before>"use strict";

/*
  Constructor
  @param Object params
    - Function@Promise onBeforeAdded
    - HTMLElement element
*/
function Playlist(params) {
  this.element = params.element;
  this.shuffle = false;
  this.element.scrollTo = function(y, t) {
    t = t > 0 ? Math.floor(t / 4) : 40;
    let step = (y - this.scrollTop) / t * 40;
    let that = this;
    function aux() {
      t -= 40;
      that.scrollTop += step;
      if (t > 0) {
        setTimeout(aux, 40);
      } else {
        that.scrollTop = y;
      }
    }
    aux();
  };
  this.params = params;

  this.onItemSelected = this.onItemSelected.bind(this);
  this.onItemRemoved = this.onItemRemoved.bind(this);
  this.onItemCleared = this.onItemCleared.bind(this);
  this.selectNext = this.selectNext.bind(this);

  // Hash Map
  this.list = new Map();
  return this;
}

Playlist.prototype = {
  addAll(media) {
    return Promise.all(media.map(m => this.add(m)));
  },

  /*
    Adds an media file to the playlist
    @param File media: The file to add
  */
  add(media) {
    if (this.list.has(createHash(media))) {
      return Promise.resolve();
    }
    let adding = new PlaylistItem({
      type: media.type.match("audio") == "audio" ? "audio" : "video",
      media,
      playlist: this
    });
    return adding.then((item) => {
      this.list.set(item.hash, item);
      if (!this.selectedItem) {
        this.onItemSelected(item.hash);
      }
      return item;
    });
  },

  selectNext(hash) {
    if (!hash) {
      hash = this.selectedItem;
    }
    var nextItemIndex;
    if (this.shuffle) {
      nextItemIndex = Math.floor(Math.random() * this.list.size);
    } else {
      var itemIndex = [...this.list.keys()].findIndex(h => h === hash);
      nextItemIndex = itemIndex === this.list.size - 1 ? 0 : itemIndex + 1;
    }

    var nextItem = [...this.list.keys()][nextItemIndex];
    this.list.get(hash).unselect();
    this.onItemSelected(nextItem);
  },

  onItemSelected(hash) {
    if (this.selectedItem) {
      this.list.get(this.selectedItem).unselect();
    }
    this.list.get(hash).select();<commit_msg>Fix a little bug with the shuffle button<commit_after>"use strict";

/*
  Constructor
  @param Object params
    - Function@Promise onBeforeAdded
    - HTMLElement element
*/
function Playlist(params) {
  this.element = params.element;
  this.shuffle = false;
  this.element.scrollTo = function(y, t) {
    t = t > 0 ? Math.floor(t / 4) : 40;
    let step = (y - this.scrollTop) / t * 40;
    let that = this;
    function aux() {
      t -= 40;
      that.scrollTop += step;
      if (t > 0) {
        setTimeout(aux, 40);
      } else {
        that.scrollTop = y;
      }
    }
    aux();
  };
  this.params = params;

  this.onItemSelected = this.onItemSelected.bind(this);
  this.onItemRemoved = this.onItemRemoved.bind(this);
  this.onItemCleared = this.onItemCleared.bind(this);
  this.selectNext = this.selectNext.bind(this);

  // Hash Map
  this.list = new Map();
  return this;
}

Playlist.prototype = {
  addAll(media) {
    return Promise.all(media.map(m => this.add(m)));
  },

  /*
    Adds an media file to the playlist
    @param File media: The file to add
  */
  add(media) {
    if (this.list.has(createHash(media))) {
      return Promise.resolve();
    }
    let adding = new PlaylistItem({
      type: media.type.match("audio") == "audio" ? "audio" : "video",
      media,
      playlist: this
    });
    return adding.then((item) => {
      this.list.set(item.hash, item);
      if (!this.selectedItem) {
        this.onItemSelected(item.hash);
      }
      return item;
    });
  },

  selectNext(hash) {
    if (!hash) {
      hash = this.selectedItem;
    }
    var nextItemIndex;
    var itemIndex = [...this.list.keys()].findIndex(h => h === hash);
    if (this.shuffle && this.list.size > 1) {
      var oth = [...Array(this.list.size).keys()].filter(e => e != itemIndex);
      console.log(oth);
      nextItemIndex = oth[Math.floor(Math.random() * oth.length)];
    } else {
      nextItemIndex = itemIndex === this.list.size - 1 ? 0 : itemIndex + 1;
    }

    var nextItem = [...this.list.keys()][nextItemIndex];
    this.list.get(hash).unselect();
    this.onItemSelected(nextItem);
  },

  onItemSelected(hash) {
    if (this.selectedItem) {
      this.list.get(this.selectedItem).unselect();
    }
    this.list.get(hash).select();
{'fraction_non_alphanumeric': 0.15835042150831624, 'fraction_numerical': 0.006607427660059239, 'mean_word_length': 2.4676145339652447, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, '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': '4842481', 'n_tokens_mistral': 1538, 'n_tokens_neox': 1497, 'n_words': 306}