forked from msolters/Tiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
global.coffee
126 lines (119 loc) · 3.42 KB
/
global.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
@Tiles = new Mongo.Collection 'Tiles'
# Only server can create a user!
Accounts.config
forbidClientAccountCreation: true
# Create a handler for the tileEditModal.
# Called on modal render.
@instantiateTileEditModal = (template) =>
@tileEditModal = new TileEditModal template
@_sort =
sort:
'pos.category': 1
'pos.tile': 1
@renderTrigger = new Deps.Dependency # used to force tiles and categories to rearrange
# Materialize Toast:
@toast = (message, displayLength, className, completeCallback) ->
createToast = (html) ->
`var container`
###
switch className
when "danger"
html = "<i class='mdi-alert-error left'></i> #{html}"
###
toast = $('<div class=\'toast\'></div>').addClass(className).html(html)
# Bind hammer
toast.hammer(prevent_default: false).bind('pan', (e) ->
deltaX = e.gesture.deltaX
activationDistance = 80
# change toast state
if !toast.hasClass('panning')
toast.addClass 'panning'
opacityPercent = 1 - Math.abs(deltaX / activationDistance)
if opacityPercent < 0
opacityPercent = 0
toast.velocity {
left: deltaX
opacity: opacityPercent
},
duration: 50
queue: false
easing: 'easeOutQuad'
return
).bind('panend', (e) ->
deltaX = e.gesture.deltaX
activationDistance = 80
# If toast dragged past activation point
if Math.abs(deltaX) > activationDistance
toast.velocity { marginTop: '-40px' },
duration: 375
easing: 'easeOutExpo'
queue: false
complete: ->
if typeof completeCallback == 'function'
completeCallback()
toast.remove()
return
else
toast.removeClass 'panning'
# Put toast back into original position
toast.velocity {
left: 0
opacity: 1
},
duration: 300
easing: 'easeOutExpo'
queue: false
return
).on 'click', ->
toast.fadeOut 'fast', toast.remove
return
toast
className = className or ''
if $('#toast-container').length == 0
# create notification container
container = $('<div></div>').attr('id', 'toast-container')
$('body').append container
# Select and append toast
container = $('#toast-container')
newToast = createToast(message)
container.append newToast
# Limit # of toasts on screen to at most 3:
toasts = container.find('.toast')
if toasts.length >= 4
for toast in [0..(toasts.length-4)]
$(toasts[toast]).fadeOut 700, ->
$(@).remove()
newToast.css
'top': parseFloat(newToast.css('top')) + 35 + 'px'
'opacity': 0
newToast.velocity {
'top': '0px'
opacity: 1
},
duration: 300
easing: 'easeOutCubic'
queue: false
# Allows timer to be pause while being panned
timeLeft = displayLength
counterInterval = setInterval((->
if newToast.parent().length == 0
window.clearInterval counterInterval
if !newToast.hasClass('panning')
timeLeft -= 100
if timeLeft <= 0
newToast.velocity {
'opacity': 0
marginTop: '-40px'
},
duration: 375
easing: 'easeOutExpo'
queue: false
complete: ->
if typeof completeCallback == 'function'
completeCallback()
$(this).remove()
return
window.clearInterval counterInterval
return
), 100)
return