-
Notifications
You must be signed in to change notification settings - Fork 1
/
demo.coffee
86 lines (72 loc) · 2.33 KB
/
demo.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
# @TODO: loading screen (or make it fast!)
ringtones =
for rtttl in $("#data").remove().text().split("\n")
do (rtttl)->
try
RTTTL.parse rtttl
catch error
{error, name: rtttl.split(":")[0], toString: -> rtttl}
for ringtone in ringtones
do (ringtone)->
$ringtone = $("<article class=song>").appendTo(document.body)
$left = $("<div class=left>").appendTo($ringtone)
$right = $("<div class=right>").appendTo($ringtone)
$title = $("<h1 class=title>").appendTo($right).text(ringtone.name)
$rtttl = $("<pre class=rtttl>").appendTo($right).text(ringtone.toString())
$ringtone.attr "name", ringtone.name
if ringtone.error
$error = $("<output class=error>").appendTo($right).text(ringtone.error.message)
else
$rtttl.text("#{ringtone.name}:#{ringtone.controls}:")
$notes = $(
for note, i in ringtone.notes
$rtttl.append(
if note.toString().match /p/
$("<span class='rest'>").appendTo($rtttl).text(note.toString())
else
$("<span class='note'>").appendTo($rtttl).text(note.toString())
unless i is ringtone.notes.length - 1
$("<span class='comma delimeter'>").appendTo($rtttl).text(",")
)
)
iid = -1
update = ->
if isPlaying ringtone
$play.removeClass "play"
$play.addClass "stop"
$ringtone.addClass "playing"
else
$play.removeClass "stop"
$play.addClass "play"
$ringtone.removeClass "playing"
clearInterval iid
ringtone.play = ->
play ringtone
update()
iid = setInterval update, 50
$ringtone[0].scrollIntoViewIfNeeded() # @TODO: account for padding
ringtone.stop = ->
stop ringtone
update()
clearInterval iid
$play = $("<button class=round>").appendTo($left).click ->
if isPlaying ringtone
ringtone.stop()
else
unless (location.hash.indexOf ringtone.name) is 1
history.pushState ringtone.name, ringtone.name, "##{ringtone.name}"
ringtone.play()
update()
unless play? and stop?
$play.attr
disabled: yes
title: "Web Audio API support required"
$(window).on "navigate", (event, data)->
if location.hash.match /#?.+/
for ringtone in ringtones
if (location.hash.indexOf ringtone.name) is 1
ringtone.play?() unless isPlaying ringtone
else
for ringtone in ringtones
ringtone.stop?()
$(window).triggerHandler "navigate"