<commit_before>module.exports = class SoundNewView extends Backbone.View

  className: 'sound-new tab'

  template: require './templates/sound_new'

  apiUrls:
    resolve:  'http://api.soundcloud.com/resolve.json'
    waveform: 'http://waveformjs.org/w'

  events:
    'submit form': 'new'

  render: =>
    @$el.html @template
    this

  hide: ->
    @$('form')[0].reset()
    @$el.hide()

  new: (e) =>
    e.preventDefault()
    url = @$('input[name="url"]').val()
    if url.match(/soundcloud.com/)?
      req = $.getJSON "#{@apiUrls.resolve}?url=#{url}&client_id=#{app.apiKey}"
      req.success @create

  # TODO: refactor, views should bind to add event
  create: (res) =>
    return unless res.kind is 'track'
    playlist = app.playlists.current()
    $.when(@getWaveformData(res.waveform_url))
     .then (waveformData) =>
        extraData =
          track_id: res.id
          playlist_id: playlist.id
          waveform_data: waveformData
        sound = playlist.sounds.create(_.extend res, extraData)
        app.soundsView.addOne(sound)
        app.bannerView.deactivateTabs()
        @hide()

  getWaveformData: (waveform_url) ->
    dfd = $.Deferred()
    req = $.getJSON "#{@apiUrls.waveform}?url=#{waveform_url}&callback=?"
    req.success (res) => dfd.resolve(res)
    dfd.promise()<commit_msg>use jsonp for resolve call, fixes adding sounds in safari<commit_after>module.exports = class SoundNewView extends Backbone.View

  className: 'sound-new tab'

  template: require './templates/sound_new'

  apiUrls:
    resolve:  'http://api.soundcloud.com/resolve.json'
    waveform: 'http://waveformjs.org/w'

  events:
    'submit form': 'new'

  render: =>
    @$el.html @template
    this

  hide: ->
    @$('form')[0].reset()
    @$el.hide()

  new: (e) =>
    e.preventDefault()
    url = @$('input[name="url"]').val()
    if url.match(/soundcloud.com/)?
      req = $.getJSON "#{@apiUrls.resolve}?url=#{url}&client_id=#{app.apiKey}&callback=?"
      req.success @create

  # TODO: refactor, views should bind to add event
  create: (res) =>
    return unless res.kind is 'track'
    playlist = app.playlists.current()
    $.when(@getWaveformData(res.waveform_url))
     .then (waveformData) =>
        extraData =
          track_id: res.id
          playlist_id: playlist.id
          waveform_data: waveformData
        sound = playlist.sounds.create(_.extend res, extraData)
        app.soundsView.addOne(sound)
        app.bannerView.deactivateTabs()
        @hide()

  getWaveformData: (waveform_url) ->
    dfd = $.Deferred()
    req = $.getJSON "#{@apiUrls.waveform}?url=#{waveform_url}&callback=?"
    req.success (res) => dfd.resolve(res)
    dfd.promise()
{'fraction_non_alphanumeric': 0.15904868078781123, 'fraction_numerical': 0.0007432181345224824, 'mean_word_length': 2.9472140762463344, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, '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': '6320938', 'n_tokens_mistral': 947, 'n_tokens_neox': 921, 'n_words': 176}