/
application.coffee
145 lines (120 loc) · 4.61 KB
/
application.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
new_ringer = (data) ->
$("#campaign-ringers .total_count").text(
data.ring_count + " Total Ring" + (if data.ring_count != 1 then "s" else ""))
$("#campaign-ringers .unique_count").text(
data.ringer_count + " Unique Ringer" + (if data.ringer_count != 1 then "s" else ""))
$("#campaign-ringers .counts").effect("highlight", {color: '#63DB00'}, 500)
$(".all-label .ui-button-text").text('All ' + data.ringer_count)
delete_last = ->
if $('#ringers-numbers li').length > 10
$('#ringers-numbers li').last().remove()
$("<li>#{data.number}</li>").hide()
.css('opacity', 0.0)
.prependTo("ul.ringers")
.slideDown(250)
.animate({opacity: 1.0}, 250, delete_last)
$('#progress-inner').css('width', "#{5 + (data.ringer_count / 200 )* 100}%")
$('#progress-inner .count').html(data.ringer_count)
setupBroadcastTextArea = ->
character_limit = 160
$('#broadcast-text-area').bind('input', ->
if $.trim($(this).val()) == "" || $(this).val().length > character_limit
$('#broadcastbutton').attr('disabled', 'disabled')
else
$('#broadcastbutton').removeAttr('disabled'))
$('#broadcast-text-area').charCount({
allowed: character_limit,
warning: 20,
})
setupTabs = ->
$( "#tabs" ).tabs()
setupFilters = (buttons) ->
$(buttons).buttonset()
$("#{buttons} :radio").change ->
context = $(buttons).parent().parent()
$("#filter-options", context).slideUp()
clicked = $(this)
id = $(this).attr('id')
id = id.substr(0, id.length-1)
if $("##{id}-options").length == 1
$("#filter-options", context).html($("##{id}-options").html()).slideDown()
$("#filter-options :checkbox", context).change(->
str = 'country:' + $("#filter-options :checked", context).map((_, c) -> c.value).toArray().join('|')
clicked.val(str)
)
loadCampaign = (pusher, campaign, prev_channel) ->
if prev_channel?
pusher.unsubscribe(prev_channel)
$("#campaign").empty()
$("select.campaign-select").val(campaign)
$("select").trigger("liszt:updated")
channel_name = null
if campaign != ""
$.get("/campaign/#{campaign}",
(data) ->
$("#campaign").hide()
.html(data)
.slideDown(200)
setupBroadcastTextArea()
setupFilters('#broadcast-filter')
setupFilters('#export-filter')
setupTabs()
).error(-> window.location.replace '/')
channel_name = campaign
channel = pusher.subscribe(channel_name)
channel.bind 'new', new_ringer
window.onhashchange = -> loadCampaign(pusher, document.location.hash[1..], channel_name)
tagFor = (tagname, id) ->
$("<div>#{tagname} <input type='hidden' name='campaign[message][filtered_messages][#{id}]tags[]' value='#{tagname}' /> <button type='button'>Remove</button></div>")
removeFilter = (btn) ->
btn.parent().remove()
removeTag = (btn) ->
btn.parent().remove()
addTag = (parent, item, id) ->
fullTagName = item.label
newTag = tagFor(fullTagName, id)
$('button', newTag).click ->
removeTag($(this))
newTag.appendTo($('#tag-filters', parent))
$('.tag-name', parent).val('')
newFilterMessage = ->
id = $('.filtered-message-template').length
newDiv = $('#original-filtered-message-template-container div:first-child').clone()
$('textarea[name="MESSAGE"]', newDiv).attr('name', "campaign[message][filtered_messages][#{id}][message_text]")
$('#filtered-messages').append(newDiv)
$('#add-tag-button', newDiv).click ->
addTag(newDiv, id)
$('#remove-filter-button', newDiv).click ->
removeFilter($(this))
$.getJSON('/tags/tags.json', (data) ->
$('.tag-name', newDiv).catcomplete({
delay: 0,
source: data,
autoFocus: true,
minLength: 0,
select: (evt, ui) ->
addTag($(this).parent(), ui.item, id)
return false
}))
$('.counter', newDiv).remove()
$('.msg-text-area', newDiv).charCount({
allowed: 160,
warning: 20,
})
$ ->
$('select.campaign-select').chosen()
setTimeout((->$('.notice').slideUp('medium')), 3000)
pusher = new Pusher(window.pusher_key)
$("#campaign").empty()
window.onhashchange = -> loadCampaign(pusher, document.location.hash[1..], null)
window.onhashchange()
$("select.campaign-select").change (evt) ->
document.location.hash = $(this).val()
$('.msg-text-area').charCount({
allowed: 160,
warning: 20,
})
window.removeTag = (btn) -> removeTag(btn)
window.addTag = (div, id) -> addTag(div, id)
window.removeFilter = (btn) -> removeFilter(btn)
window.addFilter = -> newFilterMessage()