summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/views/my/tutorials/new_view.js.coffee
blob: 46918386302002ea5aa140c0e3053debab7eeaf3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#= require service/embedly_service

class csx.Views.My.Tutorials.NewView extends Marionette.ItemView
  template: JST['templates/my/tutorials/new']
  ui:
    url: '#tutorial_url'
    url_group: '#url-group'
    save_button: '#save-button'
    preview: '#preview-panel'
    tags: '#tutorial_tags'

  modelEvents:
    'invalid': 'displayError'
    'change:url': 'render'

  events:
    'change #tutorial_url': 'loadUrl'
    "submit #new-tutorial": "save"

  initialize: ->
    @model = new @collection.model()
    @service = new csx.EmbedlyService()

  loadUrl: ->
    if @model.isValidUrl(@ui.url.val())
      @service.retrieve_info_on(@ui.url.val(), @updateTutorial)
    @validate()

  updateTutorial: (attributes) =>
    @model.set
      url: attributes.url
      heading: attributes.title
      description: attributes.description
      image_url: if _.any(attributes.images) then attributes.images[0].url else ''
      author: attributes.provider_name
      author_url: attributes.provider_url

  onRender: ->
    @ui.tags.tagit({ availableTags: csx.Tags.pluck('name') })

  validate: ->
    @model.isValid()

  displayError: (model, error) ->
    @ui.save_button.attr('disabled', 'disabled')
    @ui.preview.hide()
    @ui.url_group.addClass("error")
    errorTag = $('<span>').addClass('help-inline').text(error)
    @ui.url_group.find('.controls').append(errorTag)

  save: (event) ->
    event.preventDefault()
    event.stopPropagation()
    @model.set('tags', @ui.tags.val())
    @ui.save_button.attr('disabled', 'disabled')
    @collection.create(@model,
      success: @savedSuccessfully
      error: @couldNotSave
    )

  savedSuccessfully: (cake) =>
    window.location.hash = "tutorials"

  couldNotSave: (cake, xhr) =>
    @ui.save_button.removeAttr('disabled')
    error = new csx.Views.ErrorView
      el: @$('form#new-tutorial'),
      attributesWithErrors: $.parseJSON(xhr.responseText)
    error.render()