<commit_before>var dragDrop = require('drag-drop')
var localforage = require('localforage')
var createTorrent = require('create-torrent')
var parseTorrent = require('parse-torrent')

//modules
var TagReader = require('../modules/tag-reader')
var SongDuration = require('../modules/song-duration')

module.exports = QueueController

function QueueController (view, model, config) {
  var self = this

  this.view = view
  this.model = model

  this.model.restore(this.view.render.bind(this.view))

  this.queueItem = config.queueItem

  var DOM = this.view.getDOM()

  this.model.on('add-song', this.view.appendSong.bind(this.view))
  this.model.on('cycle', this.view.cycle.bind(this.view))
  this.model.on('remove-song', this.view.removeSong.bind(this.view))
  this.model.on('move-to-top', this.view.moveToTop.bind(this.view))

  this.tagReader = new TagReader()

  // TODO: use config selectors

  DOM.$songQueue.on('click', '.song-remove', function (e) {
    // remove item from model
    var index = $(this).closest(self.queueItem).index()
    self.model.removeSongAtPosition(index)
  })

  DOM.$songQueue.on('click', '.song-top-control', function (e) {
    // move item to top of model
    // remove item from model
    var index = $(this).closest(self.queueItem).index()
    self.model.moveToTop(index)
  })

  dragDrop('#my-queue', function (files) {
    // TODO: accept multiple files
    var file = files[0]
    var key = file.name

    console.log('Reading tags')

    // TODO: use create-torrent and parse-torrent to get infohash & save
    // when user's turn to dj, seed file, infohash should be the same
    
    // TODO: clean up callbacks
    self.tagReader.tagsFromFile(file, function (tags) {
      SongDuration.get(file, function (duration) {
        self._calculateInfoHash(file, function (infoHash, err) {
          if (err) {
            console.log('Error calculating infoHash: ', err)
            return
          }
          var song = {title: tags.combinedTitle, source: 'MP3', id: key, infoHash: infoHash, duration: duration}
          self.model.addSong(song)
          console.log('Added song to queue model: ', song)
        })
      })
    })

    // store files in localstorage so they can be seeded in future
    // TODO: add loading indicator while song saved to localstorage
    self._saveFileWithKey(file, key)
  })

  DOM.$songQueue.sortable({
    scrollSpeed: 14,
    start: function (event, ui) {
      $(this).data('original-index', ui.item.index())
    },
    update: function (event, ui) {
      var newIndex = ui.item.index()
      var oldIndex = $(this).data('original-index')
      $(this).removeData('original-index')
      self.model.move(oldIndex, newIndex)
    }
  })<commit_msg>added support for dropping multiple files at once<commit_after>var dragDrop = require('drag-drop')
var localforage = require('localforage')
var createTorrent = require('create-torrent')
var parseTorrent = require('parse-torrent')

//modules
var TagReader = require('../modules/tag-reader')
var SongDuration = require('../modules/song-duration')

module.exports = QueueController

function QueueController (view, model, config) {
  var self = this

  this.view = view
  this.model = model

  this.model.restore(this.view.render.bind(this.view))

  this.queueItem = config.queueItem

  var DOM = this.view.getDOM()

  this.model.on('add-song', this.view.appendSong.bind(this.view))
  this.model.on('cycle', this.view.cycle.bind(this.view))
  this.model.on('remove-song', this.view.removeSong.bind(this.view))
  this.model.on('move-to-top', this.view.moveToTop.bind(this.view))

  this.tagReader = new TagReader()

  // TODO: use config selectors

  DOM.$songQueue.on('click', '.song-remove', function (e) {
    // remove item from model
    var index = $(this).closest(self.queueItem).index()
    self.model.removeSongAtPosition(index)
  })

  DOM.$songQueue.on('click', '.song-top-control', function (e) {
    // move item to top of model
    // remove item from model
    var index = $(this).closest(self.queueItem).index()
    self.model.moveToTop(index)
  })

  dragDrop('#my-queue', function (files) {
    files.forEach(processFile)  
  })

  function processFile (file) {
    var key = file.name

    console.log('Reading tags')
    
    // TODO: clean up callbacks
    self.tagReader.tagsFromFile(file, function (tags) {
      SongDuration.get(file, function (duration) {
        self._calculateInfoHash(file, function (infoHash, err) {
          if (err) {
            console.log('Error calculating infoHash: ', err)
            return
          }
          var song = {title: tags.combinedTitle, source: 'MP3', id: key, infoHash: infoHash, duration: duration}
          self.model.addSong(song)
          console.log('Added song to queue model: ', song)
        })
      })
    })

    // store files in localstorage so they can be seeded in future
    // TODO: add loading indicator while song saved to localstorage
    self._saveFileWithKey(file, key)
  }

  DOM.$songQueue.sortable({
    scrollSpeed: 14,
    start: function (event, ui) {
      $(this).data('original-index', ui.item.index())
    },
    update: function (event, ui) {
      var newIndex = ui.item.index()
      var oldIndex = $(this).data('original-index')
      $(this).removeData('original-index')
      self.model.move(oldIndex, newIndex)
    }
  })
{'fraction_non_alphanumeric': 0.1426171364569722, 'fraction_numerical': 0.0013067015120403211, 'mean_word_length': 3.0900763358778627, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3500860', 'n_tokens_mistral': 1713, 'n_tokens_neox': 1649, 'n_words': 391}