<commit_before>var alerts = require('alerts')
var CommuterLocation = require('commuter-location')
var csvToArray = require('csv-to-array')
var each = require('each')
var file = require('file')
var filePicker = require('file-picker')
var log = require('log')('location-page')
var map = require('map')
var spin = require('spinner')
var value = require('value')
var view = require('view')

var View = view(require('./template.html'))

module.exports = function (ctx, next) {
  log('render')

  ctx.location.commuterLocations = ctx.commuterLocations
  ctx.view = new View(ctx.location)
  ctx.view.on('rendered', function (view) {
    var m = map(view.find('.map'), {
      center: ctx.location.coordinate(),
      zoom: 13
    })
    m.addLayer(ctx.location.mapMarker())

    if (ctx.location.commuterLocations.length > 0) {
      var cluster = new window.L.MarkerClusterGroup()
      ctx.location.commuterLocations.forEach(function (cl) {
        if (cl._commuter.validCoordinate()) cluster.addLayer(cl._commuter.mapMarker())
      })

      if (cluster.getBounds()._northEast) {
        m.addLayer(cluster)
      }
    }
  })

  next()
}

var CommuterRow = view(require('./commuter.html'))

CommuterRow.prototype.status = function () {
  var commuter = this.model._commuter
  var status = commuter.status() || ' '
  var label = commuter.statusLabel() || 'default'
  return '<span class="label label-' + label + '">' + status + '</span>'
}

CommuterRow.prototype.name = function () {
  var user = this.model._commuter._user()
  if (user && user.email) {
    return user.email
  } else {
    return this.model.name()
  }
}

CommuterRow.prototype.remove = function () {
  var self = this
  CommuterLocation.remove(this.model._id, function (err) {
    if (err) {
      console.error(err)
      window.alert(err)
    } else {
      self.el.remove()
    }
  })
}

View.prototype.commuterCount = function () {
  return this.model.commuterLocations.length
}

View.prototype['commuterLocations-view'] = function () {
  return CommuterRow
}

/**
 * Upload CSV
 */

View.prototype.parseCSV = function (e) {
  var view = this
  var spinner = spin()
  filePicker({
    accept: ['.csv']
  }, function (files) {
    var csv = file(files[0])
    csv.toText(function (err, text) {
      if (err) log.error(err)
      spinner.remove()
      var commuters = csvToArray(text)
      view.showConfirmUpload(commuters.filter(function (commuter) {
        return commuter.email && commuter.email.length >= 5
      }))
    })
  })
}

/**
 * Modal, Input
 */

var Input = view(require('./commuter-confirm-input.html'))
var Modal = view(require('./commuter-confirm-modal.html'))

/**
 * Confirm Upload
 */

View.prototype.showConfirmUpload = function (commuters) {
  var modal = new Modal({
    commuters: commuters,
    location: this.model
  })
  document.body.appendChild(modal.el)
}

View.prototype.profileAndMatch = function () {
  this.model.profileAndMatch(function (err) {
    if (err) {
      window.alert('Failed to profile commuters.')
    } else {
      window.alert('Profiling commuters.')
    }
  })
}

/**
 * Inputs
 */

Modal.prototype['commuters-view'] = function () {<commit_msg>Add more verbose alert message<commit_after>var alerts = require('alerts')
var CommuterLocation = require('commuter-location')
var csvToArray = require('csv-to-array')
var each = require('each')
var file = require('file')
var filePicker = require('file-picker')
var log = require('log')('location-page')
var map = require('map')
var spin = require('spinner')
var value = require('value')
var view = require('view')

var View = view(require('./template.html'))

module.exports = function (ctx, next) {
  log('render')

  ctx.location.commuterLocations = ctx.commuterLocations
  ctx.view = new View(ctx.location)
  ctx.view.on('rendered', function (view) {
    var m = map(view.find('.map'), {
      center: ctx.location.coordinate(),
      zoom: 13
    })
    m.addLayer(ctx.location.mapMarker())

    if (ctx.location.commuterLocations.length > 0) {
      var cluster = new window.L.MarkerClusterGroup()
      ctx.location.commuterLocations.forEach(function (cl) {
        if (cl._commuter.validCoordinate()) cluster.addLayer(cl._commuter.mapMarker())
      })

      if (cluster.getBounds()._northEast) {
        m.addLayer(cluster)
      }
    }
  })

  next()
}

var CommuterRow = view(require('./commuter.html'))

CommuterRow.prototype.status = function () {
  var commuter = this.model._commuter
  var status = commuter.status() || ' '
  var label = commuter.statusLabel() || 'default'
  return '<span class="label label-' + label + '">' + status + '</span>'
}

CommuterRow.prototype.name = function () {
  var user = this.model._commuter._user()
  if (user && user.email) {
    return user.email
  } else {
    return this.model.name()
  }
}

CommuterRow.prototype.remove = function () {
  var self = this
  CommuterLocation.remove(this.model._id, function (err) {
    if (err) {
      console.error(err)
      window.alert(err)
    } else {
      self.el.remove()
    }
  })
}

View.prototype.commuterCount = function () {
  return this.model.commuterLocations.length
}

View.prototype['commuterLocations-view'] = function () {
  return CommuterRow
}

/**
 * Upload CSV
 */

View.prototype.parseCSV = function (e) {
  var view = this
  var spinner = spin()
  filePicker({
    accept: ['.csv']
  }, function (files) {
    var csv = file(files[0])
    csv.toText(function (err, text) {
      if (err) log.error(err)
      spinner.remove()
      var commuters = csvToArray(text)
      view.showConfirmUpload(commuters.filter(function (commuter) {
        return commuter.email && commuter.email.length >= 5
      }))
    })
  })
}

/**
 * Modal, Input
 */

var Input = view(require('./commuter-confirm-input.html'))
var Modal = view(require('./commuter-confirm-modal.html'))

/**
 * Confirm Upload
 */

View.prototype.showConfirmUpload = function (commuters) {
  var modal = new Modal({
    commuters: commuters,
    location: this.model
  })
  document.body.appendChild(modal.el)
}

View.prototype.profileAndMatch = function () {
  this.model.profileAndMatch(function (err) {
    if (err) {
      window.alert('Failed to profile commuters.')
    } else {
      window.alert('Profiling commuters. Please come back in a few minutes to see the results.')
    }
  })
}

/**
 * Inputs
 */

Modal.prototype['commuters-view'] = function () {
{'fraction_non_alphanumeric': 0.155923208990167, 'fraction_numerical': 0.0015607928827844546, 'mean_word_length': 3.309347679892401, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, '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': '6174651', 'n_tokens_mistral': 2114, 'n_tokens_neox': 2072, 'n_words': 421}