#= require rails_admin_uploader/tmpl
#= require rails_admin_uploader/canvas-to-blob
#= require rails_admin_uploader/load-image.all.min.js
#= require rails_admin_uploader/jquery.fileupload
#= require rails_admin_uploader/jquery.fileupload-process
#= require rails_admin_uploader/jquery.fileupload-image
#= require rails_admin_uploader/jquery.fileupload-audio
#= require rails_admin_uploader/jquery.fileupload-video
#= require rails_admin_uploader/jquery.fileupload-validate
#= require rails_admin_uploader/jquery.iframe-transport
#= require rails_admin_uploader/jquery.fileupload-ui
upload_template = """
"""
e_spin = '
'
e_on = '
'
e_off = '
'
download_template = """
"""
class RaUploader
constructor: ($t)->
@$t = $t
@url = @$t.data('url')
@klass = @$t.data('klass')
@id = @$t.data('id')
@field = @$t.data('field')
@$f = @$t.find('.files')
@flags = @$t.data('flags')
@guid = @$t.find('input[type=hidden]').val()
@$t.disableSelection()
@initUploader()
@loadFiles()
data: =>
r = {
field: @field
klass: @klass
guid: @guid
}
if @id
r.obj_id = @id
r
initSortable: ->
return false unless @flags.sortable
@$t.find('.files tbody').sortable
placeholder: 'attach_item'
cursor: 'move'
handle: ".sort-handle",
update: (event, ui) =>
order = []
@$t.find('.template-download').each ->
$i = $(this)
order.push($i.data('id'))
d = @data()
d.order = order
$.ajax
url: @url
type: 'POST'
dataType: 'json'
data: d
error: (r)->
alert(r)
initEnableable: =>
return false unless @flags.enableable
t = @
@$t.on('click', '.enable-button a', (e)->
$t = $(this)
$i = $t.parents('.template-download')
e.preventDefault()
d = t.data()
d.img = {enabled: !$t.hasClass('label-success')}
$p = $t.parent()
$p.html(e_spin)
id = $i.data('id')
$.ajax
url: "#{t.url}/#{id}"
type: 'PUT'
dataType: 'json'
data: d
success: (r)->
$p.html(if r.enabled then e_on else e_off)
error: (r)->
alert(r)
)
initNameable: =>
return false unless @flags.nameable
t = @
@$t.on('blur', 'input.nameable', (e)->
$t = $(this)
$i = $t.parents('.template-download')
e.preventDefault()
d = t.data()
d.img = {name: $t.val()}
id = $i.data('id')
$t.next('.spinner').show()
$.ajax
url: "#{t.url}/#{id}"
type: 'PUT'
dataType: 'json'
data: d
success: (r)->
$t.next('.spinner').hide()
error: (r)->
alert(r)
)
initUploader: =>
t = @
@$fo = @$t.find('input.fileupload').fileupload(
url: @url
dataType: 'json'
autoUpload: true
previewMaxWidth: 100
previewMaxHeight: 100
formData: (form)=>
formData = []
$.each @data(), (name, value) ->
formData.push
name: name
value: value
formData
uploadTemplateId: "ra_uploader_upload",
downloadTemplateId: "ra_uploader_download"
dropZone: @$t
filesContainer: @$f
previewCrop: true
flags: @flags
destroy: (e, data) ->
$i = data.context
d = t.data()
id = $i.data('id')
if confirm("Точно удалить?")
$.ajax
url: "#{t.url}/#{id}"
type: 'DELETE'
dataType: 'json'
data: d
success: (r)->
$i.remove()
error: (r)->
alert(r)
)
@uploader = @$fo.data('blueimpFileupload')
loadFiles: ->
$.ajax
type: 'GET'
dataType: 'json'
url: @url
data: @data()
success: (r)=>
@$f.html("")
@uploader._renderDownload(r.files).appendTo(@$f)
@initSortable()
@initEnableable()
@initNameable()
error: ->
@$f.html("Ошибка загрузки списка файлов...").css(color: 'red')
init = ->
$('.fileupload-block').each ->
new RaUploader($(this))
$(document).on 'pjax:complete', init
$ ->
$('body').append(upload_template)
$('body').append(download_template)
init()