-
Notifications
You must be signed in to change notification settings - Fork 408
/
attachments.js.coffee
72 lines (59 loc) · 2.4 KB
/
attachments.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
class Kandan.Plugins.Attachments
@widget_title: "Media"
@plugin_namespace: "Kandan.Plugins.Attachments"
@template: _.template('''
<form accept-charset="UTF-8" action="/channels/<%= channel_id %>/attachments.json" data-remote="true" html="{:multipart=>true}" id="file_upload" method="post">
<div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓">
<input name="<%=csrf_param %>" type="hidden" value="<%= csrf_token %>"/>
</div>
<input id="channel_id_<%= channel_id %>" name="channel_id[<%= channel_id %>]" type="hidden"/>
<input id="file" name="file" type="file"/>
<input name="commit" type="submit" value="Upload"/>
<div class="dropzone" style="height: 100px;background-color: #000;"></div>
</form>
''')
@supports_drop_upload: ()->
!!(window.File && window.FileList && window.FileReader)
@channel_id: ()->
Kandan.Data.Channels.activeChannelId()
@csrf_param: ->
$('meta[name=csrf-param]').attr('content')
@csrf_token: ->
$('meta[name=csrf-token]').attr('content')
@file_item_template: _.template('''
<li><a href="<%= url %>"><%= file_name %></a></li>
''')
# TODO this part is very bad for APIs! shoudnt be exposing a backbone collection in a plugin.
@render: ($widget_el)->
$upload_form = @template({
channel_id: @channel_id(),
csrf_param: @csrf_param(),
csrf_token: @csrf_token()
})
$widget_el.html($upload_form)
@init_dropzone @channel_id()
$file_list = $("<ul></ul>")
attachments = new Kandan.Collections.Attachments([], {channel_id: @channel_id()})
attachments.fetch({success: (collection)=>
for model in collection.models
$file_list.append(@file_item_template({
file_name: model.get('file_file_name'),
url: model.get('url')
}))
$widget_el.append($file_list)
})
@init_dropzone: (channel_id)->
$(".dropzone").filedrop({
fallback_id: "file",
url: "/channels/#{channel_id}/attachments.json",
paramname: 'file',
uploadFinished: (i, file, response, time)->
console.log "upload complete"
dragOver: ->
console.log "reached dropzone!"
})
@init: ()->
Kandan.Widgets.register @plugin_namespace
Kandan.Data.Channels.register_callback "change", ()=>
Kandan.Widgets.render @plugin_namespace
# Kandan.Plugins.register "Kandan.Plugins.Attachments"