2011-12-19 13:15:11 +00:00
|
|
|
Locomotive.Views.ContentTypes ||= {}
|
|
|
|
|
|
|
|
class Locomotive.Views.ContentTypes.CustomFieldEntryView extends Backbone.View
|
|
|
|
|
|
|
|
tagName: 'li'
|
|
|
|
|
|
|
|
className: 'custom-field'
|
|
|
|
|
|
|
|
events:
|
2011-12-21 14:24:39 +00:00
|
|
|
'click a.toggle': 'toggle'
|
2011-12-19 13:15:11 +00:00
|
|
|
'click a.remove': 'remove'
|
|
|
|
|
2011-12-22 15:45:42 +00:00
|
|
|
initialize: ->
|
2012-02-01 01:01:42 +00:00
|
|
|
@inverse_of_list = @options.parent_view.options.inverse_of_list
|
|
|
|
|
|
|
|
console.log(@model.get('inverse_of'))
|
|
|
|
|
2011-12-22 15:45:42 +00:00
|
|
|
@model.bind 'change', (custom_field) =>
|
|
|
|
@switch_to_type() if @model.hasChanged('type')
|
2012-02-01 01:01:42 +00:00
|
|
|
@fetch_inverse_of_list() if @model.hasChanged('class_name') && @model.get('class_name')?
|
2011-12-22 15:45:42 +00:00
|
|
|
|
2011-12-19 13:15:11 +00:00
|
|
|
render: ->
|
|
|
|
$(@el).html(ich.custom_field_entry(@model.toJSON()))
|
|
|
|
|
2012-02-01 01:01:42 +00:00
|
|
|
@fetch_inverse_of_list() unless @model.isNew()
|
|
|
|
|
2011-12-19 13:15:11 +00:00
|
|
|
Backbone.ModelBinding.bind @, all: 'class'
|
|
|
|
|
2011-12-22 15:45:42 +00:00
|
|
|
@make_fields_editable()
|
|
|
|
|
|
|
|
@enable_behaviours()
|
|
|
|
|
|
|
|
@switch_to_type()
|
2011-12-21 14:24:39 +00:00
|
|
|
|
2011-12-19 13:15:11 +00:00
|
|
|
return @
|
|
|
|
|
2011-12-22 15:45:42 +00:00
|
|
|
enable_behaviours: ->
|
2012-01-18 00:43:01 +00:00
|
|
|
required_input = @$('.required-input input[type=checkbox]')
|
|
|
|
required_input.checkToggle(on_label: required_input.attr('data-on-label'), off_label: required_input.attr('data-off-label'))
|
2012-01-14 00:38:09 +00:00
|
|
|
@$('li.input.toggle input[type=checkbox]').checkToggle()
|
2011-12-22 15:45:42 +00:00
|
|
|
@render_select_options_view()
|
|
|
|
|
|
|
|
switch_to_type: ->
|
|
|
|
@$('li.input.extra').hide() # reset
|
|
|
|
|
|
|
|
switch @model.get('type')
|
|
|
|
when 'select'
|
|
|
|
@$('li.input.select-options').show()
|
|
|
|
when 'text'
|
|
|
|
@$('li.input.text-formatting').show()
|
2012-01-31 00:50:09 +00:00
|
|
|
when 'belongs_to'
|
|
|
|
@$('li.input.localized').hide()
|
|
|
|
@$('li.input.class-name').show()
|
2012-02-01 01:01:42 +00:00
|
|
|
when 'has_many'
|
|
|
|
@$('li.input.localized').hide()
|
|
|
|
@$('li.input.class-name').show()
|
|
|
|
@$('li.input.inverse-of').show()
|
|
|
|
@$('li.input.ui-enabled').show()
|
|
|
|
|
|
|
|
fetch_inverse_of_list: ->
|
|
|
|
@$('li.input.inverse-of select option').remove()
|
|
|
|
|
|
|
|
_.each @inverse_of_list, (data) =>
|
|
|
|
if data.class_name == @model.get('class_name')
|
|
|
|
option = new Option(data.label, data.name, data.class_name == @model.get('inverse_of') || @inverse_of_list.length == 1)
|
|
|
|
@$('li.input.inverse-of select').append(option)
|
2011-12-22 15:45:42 +00:00
|
|
|
|
|
|
|
render_select_options_view: ->
|
|
|
|
@select_options_view = new Locomotive.Views.ContentTypes.SelectOptionsView
|
|
|
|
model: @model
|
|
|
|
collection: @model.get('select_options')
|
|
|
|
|
|
|
|
@$('#content_type_contents_custom_field_select_options_input').append(@select_options_view.render().el)
|
|
|
|
|
|
|
|
make_fields_editable: ->
|
2012-01-18 00:43:01 +00:00
|
|
|
@$('.label-input input[type=text], .type-input select').editableField()
|
2011-12-22 15:45:42 +00:00
|
|
|
|
2011-12-21 14:24:39 +00:00
|
|
|
toggle: (event) ->
|
2011-12-19 13:15:11 +00:00
|
|
|
event.stopPropagation() & event.preventDefault()
|
|
|
|
form = @$('ol')
|
|
|
|
|
|
|
|
if form.is(':hidden')
|
|
|
|
form.slideDown()
|
|
|
|
else
|
|
|
|
form.slideUp()
|
|
|
|
|
2011-12-21 14:24:39 +00:00
|
|
|
show_error: (message) ->
|
2012-01-18 00:43:01 +00:00
|
|
|
html = $("<span class=\"inline-errors\">#{message}</span>")
|
|
|
|
@$('.required-input').after(html)
|
2011-12-21 14:24:39 +00:00
|
|
|
|
2011-12-19 13:15:11 +00:00
|
|
|
remove: (event) ->
|
|
|
|
event.stopPropagation() & event.preventDefault()
|
|
|
|
|
|
|
|
if confirm($(event.target).attr('data-confirm'))
|
2012-01-18 00:43:01 +00:00
|
|
|
@$('.label-input input[type=text], .type-input select').editableField('destroy')
|
2011-12-19 13:15:11 +00:00
|
|
|
super()
|
|
|
|
@options.parent_view.remove_entry(@model, @)
|