-
Notifications
You must be signed in to change notification settings - Fork 42
/
paintings.js.coffee
34 lines (31 loc) · 1.31 KB
/
paintings.js.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
jQuery ->
$('#fileupload').fileupload
add: (e, data) ->
types = /(\.|\/)(gif|jpe?g|png)$/i
file = data.files[0]
if types.test(file.type) || types.test(file.name)
data.context = $(tmpl("template-upload", file))
$('#fileupload').append(data.context)
data.submit()
else
alert("#{file.name} is not a gif, jpeg, or png image file")
progress: (e, data) ->
if data.context
progress = parseInt(data.loaded / data.total * 100, 10)
data.context.find('.bar').css('width', progress + '%')
done: (e, data) ->
file = data.files[0]
domain = $('#fileupload').attr('action')
path = $('#fileupload input[name=key]').val().replace('${filename}', file.name)
to = $('#fileupload').data('post')
content = {}
content[$('#fileupload').data('as')] = domain + path
$.post(to, content)
data.context.remove() if data.context # remove progress bar
fail: (e, data) ->
alert("#{data.files[0].name} failed to upload.")
console.log("Upload failed:")
console.log(data)