Controller = window.require 'spine/controller'
Container  = window.require 'spine/controller/container'

###
Item Render for each content item used in the WebAddress service
###
class WebAdressField extends Controller
	className: 'directinput-field'
	title : 'directinput'

	@include Container

	elements:
		'.field-delete' : 'del'
		'input': 'input'

	events:
		'click .field-delete' : '_delete'
		'focus input': 'onInputFocus'
		'click .input-textarea': 'onInputFocus'

	constructor:->
		super
		@control = $.extend true, {}, @control
		@$addClass('new') if @item.isNew()
		@listenTo @item, 'create', => @$removeClass 'new'
		@listenTo @item, 'destroy', @release

		Control = window.require @control.control
		@control = new Control { @item ,attribute:@control.attribute, options:@control.options}
		@append @control, "<span class='field-delete' data-icon='⏍'></span>"

	_delete: -> @item.destroy()

	enable: =>
		@el.toggleClass 'disabled', @disabled = false

	disable: =>
		@el.toggleClass 'disabled', @disabled = true

	onInputFocus: =>
		@parent().trigger 'canAdd', (->), !@disabled
		@input.blur() if @disabled

module.exports = WebAdressField
