2011-11-16 12:42:42 +00:00
|
|
|
Locomotive.Views.Shared ||= {}
|
|
|
|
|
|
|
|
class Locomotive.Views.Shared.FormView extends Backbone.View
|
|
|
|
|
|
|
|
el: '#content'
|
|
|
|
|
|
|
|
render: ->
|
|
|
|
# make title editable (if possible)
|
|
|
|
@make_title_editable()
|
|
|
|
|
|
|
|
@_hide_last_separator()
|
|
|
|
|
|
|
|
# make inputs foldable (if specified)
|
|
|
|
@make_inputs_foldable()
|
|
|
|
|
|
|
|
return @
|
|
|
|
|
|
|
|
save: (event) ->
|
|
|
|
# by default, follow the default behaviour
|
|
|
|
|
2011-11-29 13:58:19 +00:00
|
|
|
save_in_ajax: (event, options) ->
|
|
|
|
event.stopPropagation() & event.preventDefault()
|
|
|
|
|
|
|
|
@clear_errors()
|
|
|
|
|
2012-02-02 15:53:26 +00:00
|
|
|
options ||= { headers: {}, on_success: null, on_error: null }
|
2011-11-29 13:58:19 +00:00
|
|
|
|
|
|
|
previous_attributes = _.clone @model.attributes
|
|
|
|
|
|
|
|
@model.save {},
|
2012-02-02 15:53:26 +00:00
|
|
|
headers: options.headers
|
2011-11-29 13:58:19 +00:00
|
|
|
success: (model, response, xhr) =>
|
|
|
|
model.attributes = previous_attributes
|
|
|
|
|
2011-12-02 17:20:59 +00:00
|
|
|
options.on_success(response, xhr) if options.on_success
|
2011-11-29 13:58:19 +00:00
|
|
|
|
|
|
|
error: (model, xhr) =>
|
|
|
|
errors = JSON.parse(xhr.responseText)
|
|
|
|
|
|
|
|
@show_errors errors
|
|
|
|
|
|
|
|
options.on_error() if options.on_error
|
|
|
|
|
2011-11-16 12:42:42 +00:00
|
|
|
make_title_editable: ->
|
|
|
|
title = @$('h2 a.editable')
|
|
|
|
|
|
|
|
if title.size() > 0
|
2011-11-25 14:10:56 +00:00
|
|
|
target = @$("##{title.attr('rel')}")
|
2011-11-16 12:42:42 +00:00
|
|
|
target.parent().hide()
|
|
|
|
|
|
|
|
title.click (event) =>
|
|
|
|
event.stopPropagation() & event.preventDefault()
|
|
|
|
newValue = prompt(title.attr('title'), title.html());
|
|
|
|
if newValue && newValue != ''
|
|
|
|
title.html(newValue)
|
2011-11-25 01:04:42 +00:00
|
|
|
target.val(newValue).trigger('change')
|
2011-11-16 12:42:42 +00:00
|
|
|
|
|
|
|
make_inputs_foldable: ->
|
|
|
|
self = @
|
|
|
|
@$('.formtastic fieldset.foldable.folded ol').hide()
|
|
|
|
@$('.formtastic fieldset.foldable legend').click ->
|
|
|
|
parent = $(@).parent()
|
|
|
|
content = $(@).next()
|
|
|
|
|
|
|
|
if parent.hasClass 'folded'
|
|
|
|
parent.removeClass 'folded'
|
|
|
|
content.slideDown 100, -> self.after_inputs_fold(parent)
|
|
|
|
else
|
|
|
|
content.slideUp 100, -> parent.addClass('folded')
|
|
|
|
|
|
|
|
after_inputs_fold: ->
|
|
|
|
# overide this method if necessary
|
|
|
|
|
2011-11-25 01:04:42 +00:00
|
|
|
clear_errors: ->
|
2011-11-29 13:58:19 +00:00
|
|
|
@$('.inline-errors').remove()
|
2011-11-25 01:04:42 +00:00
|
|
|
|
|
|
|
show_errors: (errors) ->
|
|
|
|
for attribute, message of errors
|
2012-01-18 00:43:01 +00:00
|
|
|
if _.isString(message[0])
|
2011-12-21 14:24:39 +00:00
|
|
|
html = $("<div class=\"inline-errors\"><p>#{message[0]}</p></div>")
|
|
|
|
@show_error attribute, message[0], html
|
2012-01-18 00:43:01 +00:00
|
|
|
else
|
|
|
|
@show_error attribute, message
|
2011-11-25 01:04:42 +00:00
|
|
|
|
|
|
|
show_error: (attribute, message, html) ->
|
|
|
|
input = @$("##{@model.paramRoot}_#{attribute}")
|
2012-01-31 00:50:09 +00:00
|
|
|
input = @$("##{@model.paramRoot}_#{attribute}_id") if input.size() == 0
|
2011-12-02 17:20:59 +00:00
|
|
|
|
|
|
|
return unless input.size() > 0
|
|
|
|
|
|
|
|
anchor = input.parent().find('.error-anchor')
|
|
|
|
anchor = input if anchor.size() == 0
|
|
|
|
anchor.after(html)
|
2011-11-25 01:04:42 +00:00
|
|
|
|
2011-11-16 12:42:42 +00:00
|
|
|
_enable_checkbox: (name, options) ->
|
2011-12-22 00:31:33 +00:00
|
|
|
options ||= {}
|
|
|
|
model_name = options.model_name || @model.paramRoot
|
|
|
|
|
|
|
|
@$("li##{model_name}_#{name}_input input[type=checkbox]").checkToggle
|
2011-11-16 12:42:42 +00:00
|
|
|
on_callback: =>
|
2011-12-22 00:31:33 +00:00
|
|
|
_.each options.features, (exp) -> @$("li##{model_name}_#{exp}_input").hide()
|
2011-11-16 12:42:42 +00:00
|
|
|
options.on_callback() if options.on_callback?
|
|
|
|
@_hide_last_separator()
|
|
|
|
off_callback: =>
|
2011-12-22 00:31:33 +00:00
|
|
|
_.each options.features, (exp) -> @$("li##{model_name}_#{exp}_input").show()
|
2011-11-16 12:42:42 +00:00
|
|
|
options.off_callback() if options.off_callback?
|
|
|
|
@_hide_last_separator()
|
|
|
|
|
|
|
|
_hide_last_separator: ->
|
|
|
|
_.each @$('fieldset'), (fieldset) =>
|
|
|
|
$(fieldset).find('li.last').removeClass('last')
|
|
|
|
$(_.last($(fieldset).find('li.input:visible'))).addClass('last')
|