2011-11-21 01:27:05 +00:00
|
|
|
Locomotive.Views.EditableElements ||= {}
|
|
|
|
|
|
|
|
class Locomotive.Views.EditableElements.FileView extends Backbone.View
|
|
|
|
|
|
|
|
tagName: 'li'
|
|
|
|
|
|
|
|
className: 'file input'
|
|
|
|
|
2011-11-21 11:12:06 +00:00
|
|
|
states:
|
|
|
|
change: false
|
|
|
|
delete: false
|
|
|
|
|
|
|
|
events:
|
|
|
|
'click a.change': 'toggle_change'
|
|
|
|
'click a.delete': 'toggle_delete'
|
|
|
|
|
2011-11-21 01:27:05 +00:00
|
|
|
render: ->
|
|
|
|
$(@el).html(ich.editable_file_input(@model.toJSON()))
|
|
|
|
|
2011-11-25 01:04:42 +00:00
|
|
|
# only in HTML 5
|
|
|
|
@$('input[type=file]').bind 'change', (event) =>
|
|
|
|
input = $(event.target)[0]
|
|
|
|
if input.files?
|
|
|
|
@model.set(source: input.files[0])
|
|
|
|
|
2011-11-21 11:12:06 +00:00
|
|
|
return @
|
|
|
|
|
2011-11-25 01:04:42 +00:00
|
|
|
after_render: ->
|
|
|
|
# do nothing
|
|
|
|
|
|
|
|
refresh: ->
|
|
|
|
@$('input[type=file]').unbind 'change'
|
|
|
|
@states = { 'change': false, 'delete': false }
|
|
|
|
@render()
|
|
|
|
|
2011-11-21 11:12:06 +00:00
|
|
|
toggle_change: (event) ->
|
|
|
|
@_toggle event, 'change',
|
|
|
|
on_change: =>
|
|
|
|
@$('a:first').hide() & @$('input[type=file]').show() & @$('a.delete').hide()
|
|
|
|
on_cancel: =>
|
|
|
|
@$('a:first').show() & @$('input[type=file]').hide() & @$('a.delete').show()
|
|
|
|
|
|
|
|
toggle_delete: (event) ->
|
|
|
|
@_toggle event, 'delete',
|
|
|
|
on_change: =>
|
|
|
|
@$('a:first').addClass('deleted') & @$('a.change').hide()
|
|
|
|
@$('input[type=hidden].remove-flag').val('1')
|
2011-11-25 01:04:42 +00:00
|
|
|
@model.set('remove_source': true)
|
2011-11-21 11:12:06 +00:00
|
|
|
on_cancel: =>
|
|
|
|
@$('a:first').removeClass('deleted') & @$('a.change').show()
|
|
|
|
@$('input[type=hidden].remove-flag').val('0')
|
2011-11-25 01:04:42 +00:00
|
|
|
@model.set('remove_source': false)
|
2011-11-21 11:12:06 +00:00
|
|
|
|
|
|
|
_toggle: (event, state, options) ->
|
|
|
|
event.stopPropagation() & event.preventDefault()
|
|
|
|
|
|
|
|
button = $(event.target)
|
|
|
|
label = button.attr('data-alt-label')
|
|
|
|
|
|
|
|
unless @states[state]
|
|
|
|
options.on_change()
|
|
|
|
else
|
|
|
|
options.on_cancel()
|
|
|
|
|
|
|
|
button.attr('data-alt-label', button.html())
|
|
|
|
|
|
|
|
button.html(label)
|
|
|
|
|
|
|
|
@states[state] = !@states[state]
|
|
|
|
|
2011-11-25 01:04:42 +00:00
|
|
|
remove: ->
|
|
|
|
@$('input[type=file]').unbind 'change'
|
|
|
|
super
|