2011-12-02 14:59:41 +00:00
|
|
|
Locomotive.Views.Sites ||= {}
|
2011-11-28 21:50:24 +00:00
|
|
|
|
2011-12-02 14:59:41 +00:00
|
|
|
class Locomotive.Views.Sites.DomainsView extends Backbone.View
|
2011-11-28 21:50:24 +00:00
|
|
|
|
|
|
|
tagName: 'div'
|
|
|
|
|
|
|
|
className: 'list'
|
|
|
|
|
|
|
|
_entry_views = []
|
|
|
|
|
|
|
|
events:
|
|
|
|
'click .new-entry a.add': 'add_entry'
|
2011-11-29 01:24:02 +00:00
|
|
|
'keypress .new-entry input[type=text]': 'add_on_entry_from_enter'
|
2011-11-28 21:50:24 +00:00
|
|
|
|
|
|
|
render: ->
|
|
|
|
$(@el).html(ich.domains_list(@model.toJSON()))
|
|
|
|
|
|
|
|
@render_entries()
|
|
|
|
|
2011-11-29 13:58:19 +00:00
|
|
|
@enable_ui_effects()
|
|
|
|
|
2011-11-28 21:50:24 +00:00
|
|
|
return @
|
|
|
|
|
|
|
|
add_entry: (event) ->
|
|
|
|
event.stopPropagation() & event.preventDefault()
|
|
|
|
|
|
|
|
input = @$('.new-entry input[name=domain]')
|
|
|
|
|
|
|
|
if input.val() != ''
|
|
|
|
domain = new Locomotive.Models.Domain name: input.val()
|
|
|
|
|
|
|
|
@model.get('domains').push(domain)
|
|
|
|
|
|
|
|
@_insert_entry(domain)
|
|
|
|
|
2011-11-29 01:24:02 +00:00
|
|
|
@$('ul li.domain:last input[type=text]').editableField()
|
|
|
|
@$('.empty').hide()
|
|
|
|
|
2011-11-28 21:50:24 +00:00
|
|
|
input.val('') # reset for further entries
|
|
|
|
|
2011-11-29 01:24:02 +00:00
|
|
|
add_on_entry_from_enter: (event) ->
|
|
|
|
return if event.keyCode != 13
|
|
|
|
@add_entry(event)
|
|
|
|
|
2011-11-28 21:50:24 +00:00
|
|
|
change_entry: (domain, value) ->
|
|
|
|
domain.set name: value
|
|
|
|
|
|
|
|
remove_entry: (domain) ->
|
|
|
|
list = _.reject @model.get('domains'), (_domain) => _domain == domain
|
|
|
|
@model.set domains: list
|
2011-11-29 01:24:02 +00:00
|
|
|
@$('.empty').show() if @model.get('domains').length == 0
|
2011-11-28 21:50:24 +00:00
|
|
|
|
|
|
|
render_entries: ->
|
2011-11-29 01:24:02 +00:00
|
|
|
if @model.get('domains').length == 0
|
|
|
|
@$('.empty').show()
|
|
|
|
else
|
|
|
|
_.each @model.get('domains'), (domain) =>
|
|
|
|
@_insert_entry(domain)
|
2011-11-28 21:50:24 +00:00
|
|
|
|
2011-11-29 13:58:19 +00:00
|
|
|
@show_errors()
|
|
|
|
|
|
|
|
show_errors: ->
|
|
|
|
_.each @options.errors.domain || [], (message) => @show_error(message)
|
|
|
|
|
|
|
|
show_error: (message) ->
|
|
|
|
_.each (@_entry_views || []), (view) ->
|
|
|
|
if new RegExp("^#{view.model.get('name')}").test message
|
|
|
|
html = $('<span></span>').addClass('inline-errors').html(message)
|
|
|
|
view.$('input[type=text].path').after(html)
|
|
|
|
|
|
|
|
enable_ui_effects: ->
|
|
|
|
@$('.domain input[type=text]').editableField()
|
|
|
|
|
2011-11-28 21:50:24 +00:00
|
|
|
_insert_entry: (domain) ->
|
2011-12-02 14:59:41 +00:00
|
|
|
view = new Locomotive.Views.Sites.DomainEntryView model: domain, parent_view: @
|
2011-11-28 21:50:24 +00:00
|
|
|
|
|
|
|
(@_entry_views ||= []).push(view)
|
|
|
|
|
|
|
|
@$('ul').append(view.render().el)
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-11-29 01:24:02 +00:00
|
|
|
|
|
|
|
|