This repository has been archived by the owner on Aug 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
otto.client.coffee
1483 lines (1261 loc) · 52.6 KB
/
otto.client.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
####
#### client side (body of global.otto.client served by zappa as /otto.client.js)
####
global.otto.client = ->
window.otto = window.otto || {}
otto.socketconnected = false
otto.serverproceed = false
otto.clientstarted = false
otto.clientready = false
otto.salutations = false
@on 'connect': ->
console.log 'sio connect'
otto.socketconnected = true
# now we wait for the server to say 'proceed' or ask us to 'resession'
@on 'proceed': ->
console.log 'sio proceed'
otto.serverproceed = true
otto.sayhello()
# now we wait for the server to say 'welcome' and give us data
@on 'resession': ->
console.log 'sio resession, sir, yes sir!'
$.get '/resession', =>
console.log '/resession, sir, done sir!'
otto.serverproceed = true
otto.sayhello()
@on 'disconnect': ->
console.log 'sio disconnect'
$('body').addClass 'disconnected'
otto.socketconnected = false
otto.serverproceed = false
#otto.clientstarted = false
otto.clientready = false
otto.salutations = false
otto.saygoodbye()
@on 'error': ->
console.log 'sio error, reloading'
window.location.reload()
# note: connect doesn't work when moved under $ or nextTick!
# it appears you have to call @connect inside zappa.run's initial call
# or else the context.socket isn't created inside zappa.run() in
# time for it to be used internally. i think this also means it's going
# to be very difficut to rig things so we can call @connect again to connect
# to a different server. -jon
# first arg is the url to connect to, undefined connects back to where we were served from
@connect undefined, 'reconnection limit': 3000, 'max reconnection attempts': Infinity
# this might be in a race condition with the rest of this file being parsed (move it to end?)
# i think i fixed ^^ this with the added otto.clientstarted logic
# using nextTick here so the function, and all the functions it calls, are finished being defined
nextTick -> otto.start_client()
otto.start_client = =>
console.log 'start_client'
otto.clientstate = {}
otto.myusername = no
otto.mychannel = no
otto.current_track_qid = no
otto.channel_list = []
otto.current_channel = no
otto.play_state = 'unknown'
otto.connect_state = 'disconnected'
otto.ignore_reload = false
otto.cache = { queue: [], list: [], stars: [] }
otto.current_volume = 80
otto.soundfx = no
otto.notifications = no
# no notifications in the app
if /Otto_OSX/.test(navigator.userAgent)
delete Notification
otto.touch_init()
otto.showdown_converter = new Showdown.converter()
$(window).on 'scrollstop', otto.results_lazyload_handler
$(window).smartresize otto.results_lazyload_handler
$(window).smartresize otto.autosize_adjust
$(window).on 'focus blur', otto.window_focus_handler
otto.ouroboros_ajax_hooks()
otto.window_idle_handler_init()
$('body').on 'click', otto.button_click_handler
$('body').on 'click', otto.logo_click_handler
$('body').on 'click', otto.letterbar_click_handler
$('body').on 'change', otto.checkbox_click_handler
$('body').on 'submit', otto.form_submit_handler
$(window).on 'unload', ->
otto.lastnotification.close() if otto.lastnotification
$(window).on 'beforeunload', ->
otto.lastnotification.close() if otto.lastnotification
# hmmm, was hoping beforeunload would help with refresh, but alas
if /^#/.test location.hash
params = location.hash.slice(1).split('&')
$.each params, ->
if @ != ''
kv = @.split '='
k = kv[0]
v = kv[1]
switch k
when 'connect' then if v == '1' then otto.connect_player()
when 'chat' then if v == '1' then $('.chattoggle').click()
when 'ignorereload' then if v = '1' then otto.ignore_reload = true
otto.clientstarted = true
otto.sayhello()
otto.sayhello = =>
if otto.socketconnected and otto.serverproceed and otto.clientstarted and not otto.salutations
otto.salutations = true
console.log 'well, hello server!'
@emit 'hello', otto.clientstate # causes the server to welcome us and tell us our state
otto.saygoodbye = =>
console.log 'ok lady, goodbye!'
otto.salutations = false
otto.myusername = false
otto.mychannel = false
otto.current_track_qid = false
@on 'welcome': ->
console.log 'welcome data', @data
$('body').removeClass 'disconnected'
otto.localhost = @data.localhost
otto.emptydatabase = @data.emptydatabase
otto.largedatabase = @data.largedatabase
otto.haslineout = @data.haslineout
otto.musicroot = @data.musicroot
if otto.emptydatabase
otto.create_hellopage()
otto.channel_list = @data.channellist
otto.myusername = @data.myusername
otto.mychannel = @data.mychannel
else
if $('.mmenu-page').length
otto.templates.body_reset()
else
$(document.body).html otto.templates.body()
$('.channellist-container').on 'click', otto.channellist_click_handler
otto.process_channellist @data.channellist, true #process_mychannel will do the final html
otto.process_myusername.call @, @data.myusername
otto.process_mychannel.call @, @data.mychannel
@on 'begun': ->
otto.emptydatabase = false
$(document.body).html otto.templates.body()
$('.channellist-container').on 'click', otto.channellist_click_handler
otto.process_channellist otto.channel_list, true #process_mychannel will do the final html
otto.process_myusername.call @, otto.myusername
otto.process_mychannel.call @, otto.mychannel
$('.output').append navigator.userAgent
$('.output').append otto.app
@emit 'updateme'
otto.create_mainpage = ->
#$(document).attr 'title', otto.current_channel.fullname + ' ▪ ' + otto.myurl + ' ▪ otto' #FIXME
#$(document).attr 'title', otto.current_channel.fullname + ' ▪ otto' #FIXME
if Modernizr.localstorage
size = localStorage.getItem('size') || 'size1'
$('#mainpage').html otto.templates.mainpage channel: otto.current_channel, largedatabase: otto.largedatabase, size: size
$('.playing-container').on 'click', otto.results_click_handler
$('.browseresults-container').on 'click', otto.results_click_handler
$('.console-container').on 'click', otto.console_click_handler
$('.volume').slider().slider value: otto.current_volume, range: 'min', slide: otto.adjust_volume_handler
$('.volumelineout').slider().slider value: otto.current_volume, range: 'min', slide: otto.adjust_volumelineout_handler
$('.scrollkiller').on 'mousewheel', otto.scroll_bubble_stop_handler
$('.console-container').resizable handles: 's', alsoResize: $('.output'), minHeight: 45, autoHide: true
$('.channellist-container').mmenu( { slidingSubmenus: false } )
#$('.cursor-hider').hover (e) -> e.stopPropagation() # don't suppress the mouseleave event FIXME
$('.cursor-hider').on 'mouseenter', (e) -> e.stopPropagation()
otto.chat_init()
# preserve chat window state
if otto.clientstate.inchat
otto.clientstate.inchat = false # to convince enable_chat to act
otto.enable_chat true
otto.create_hellopage = ->
otto.load_module 'cubes' # all the welcome css is in otto.cubes.css
$(document.body).html otto.templates.body_welcome musicroot: otto.musicroot
$('.folder .path').keydown (e) ->
if e.keyCode is 13
e.preventDefault()
$('.folder .path').blur()
@on 'queue': ->
console.log 'queue', @data
if @data.length
n = 0
console.log @data
for song, i in @data
if song.nowplaying
n = i
break
if not otto.current_track_qid == false || otto.current_track_qid != @data[n].mpdqueueid
len = Math.ceil(@data[n].length) # we want to round up so it matches what mpd does
# take 'len' if you don't want an initial progress bar
time = otto.current_song_time || { current: 0, total: len || 0 }
active = $('.currentcover-container .thumb').is '.active'
$('.currenttrack-container').html otto.templates.currenttrack song: @data[n], current: time.current, total: time.total
otto.autosize_adjust()
if active
top = $(window).scrollTop()
$('.currentcover-container .thumb').click()
$(window).scrollTop(top)
# that isn't cutting it, still scrolls if track changes
# and a minimum we should check if the album changed and only repaint if so
otto.current_track_qid = @data[n].mpdqueueid
$target = if otto.noscroll_click_event then $(otto.noscroll_click_event.target) else false
otto.noscroll_click_event = false
otto.render_without_scrolling $target, =>
if otto.current_channel.layout is 'featured'
$('.ondeck-container').html otto.templates.featured songs: @data[0..@data.length]
else
$('.ondeck-container').html otto.templates.ondeck songs: @data[1..@data.length]
if otto.notifications # add notification for enter/leaving chat room #FIXME <- might be ok
song = @data[n]
if song._id isnt otto.lastnotificationid
otto.lastnotificationid = song._id
album = song.album || ''
#album += ' • ' + song.year if song.year
album += ' ' + song.year if song.year
artist = song.artist || ''
body = "#{album}\n#{artist}"
body += "\n#{song.owners[0].owner}" if song.owners?[0].owner?
otto.lastnotification.close() if otto.lastnotification # we should (also?) close on 'finished' event
otto.lastnotification = new Notification song.song, body: body, icon: "/image/120?id=#{song.cover}"
#n.onshow = ->
# timeoutSet 10000, -> n.close()
else
$('.currenttrack-container').html otto.templates.currenttrack {}
otto.cache.queue = @data
otto.mark_allthethings()
@on 'state': ->
console.log 'state', @data
otto.play_state = @data
if @data is 'play'
$play = $('#play')
$play.html otto.templates.icon 'pause'
$play.addClass 'shy'
else
$play = $('#play')
$play.html otto.templates.icon 'play'
$play.removeClass 'shy'
@on 'time': ->
if @data
parts = @data.split(':')
[current, total] = parts[0..1]
otto.current_song_time = { current: current, total: total }
$('.timeprogress-container').html otto.templates.timeprogress_widgets total: total, current: current
@on 'loader': ->
console.log 'loader says:', @data
otto.load_module 'cubes', =>
otto.client.cubes.loader_event @data
if @data is 'started'
if otto.emptydatabase
$(document.body).html otto.templates.initialload folder: $('.folder .path').text()
$('.loadingstatus').addClass('searching');
$('.loadingcubes').html otto.client.cubes.show()
else if otto.localhost and $('.loadingstatus').length is 0
# switch to cubes view (for when scan is triggered from app menu)
$('.browseresults-container').html otto.templates.cubeswithload()
$('.browseresults-container').append otto.templates.footer()
$('.loadingstatus').addClass('begin')
$('.loadingcubes').html otto.call_module 'cubes', 'show'
if $('.loadmusic2').text() is 'scan'
$('.loadmusic2').html otto.templates.ouroboros size: 'small', direction: 'cw', speed: 'slow'
else if @data.stdout and @data.stdout isnt 'new' and @data.stdout isnt ' ' and @data.stdout isnt ''
$output = $('.output')
if $output.length
amountscrolledup = $output.prop('scrollHeight') - $output.height() - $output.scrollTop()
$output.append otto.templates.loader event: @data.stdout
if amountscrolledup < 500 # was 80, but i think we need a different detection mech. for autoscroll
$output.scrollToBottom()
@on 'myusername': ->
console.log 'myusername'
otto.process_myusername.call @, @data
otto.process_myusername = (username) ->
otto.myusername = username
if username
$('body').addClass 'loggedin'
$('body').removeClass 'loggedout'
else
otto.enable_chat false
$('.browseresults-container').empty()
$('body').addClass 'loggedout'
$('body').removeClass 'loggedin'
@on 'myurl': ->
console.log 'myurl'
otto.myurl = @data.name
$('#channelname').text @data.fullname
@on 'mychannel': ->
console.log 'changing channel to', @data.name
otto.process_mychannel.call @, @data.name
otto.process_mychannel = (name) ->
otto.mychannel = name
otto.current_track_qid = false
otto.current_channel = false
for channel in otto.channel_list
if channel.name is otto.mychannel
otto.current_channel = channel
if not otto.current_channel then otto.current_channel = otto.channel_list[0]
if not otto.emptydatabase
otto.create_mainpage()
$('.channellist-container').html otto.templates.channellist channellist: otto.channel_list
if otto.connect_state isnt 'disconnected'
otto.connect_player()
otto.clientready = true
@emit 'updateme'
@on 'reload': ->
if otto.ignore_reload
console.log 'ignoring reload event'
return
if otto.connect_state isnt 'disconnected'
window.location.hash = '#connect=1'
else
window.location.hash = '' # hash still appears, rumour is you can't get rid of it
if otto.clientstate.inchat
if window.location.hash isnt ''
window.location.hash += '&chat=1'
else
window.location.hash += '#chat=1'
window.location.reload()
@on 'flushstream': ->
console.log 'flushstream'
if otto.connect_state isnt 'disconnected'
otto.reconnect_player() # make it stop playing instantly (flush buffer)
@on 'chat': ->
console.log @data.name
return if @data.name is 'finished'
otto.play_soundfx @data.name
otto.play_notification @data
$output = $('.output')
amountscrolledup = $output.prop('scrollHeight') - $output.height() - $output.scrollTop()
$output.append otto.templates.event event: @data
if amountscrolledup < 80
$output.scrollToBottom()
@on 'lists': ->
console.log 'lists'
if @data
otto.process_lists @data
otto.process_lists = (lists) ->
if lists
for user in lists
if user == otto.myusername
otto.cache.list = user.list
otto.mark_listed_items()
$('.browsersults-container').trigger('scrollstop')
@on 'stars': ->
console.log 'on stars', @data
if @data
otto.process_stars @data
otto.process_stars = (stars) ->
console.log 'process_stars'
for own username of stars
console.log 'username', username
console.log 'myusername', otto.myusername
if username == otto.myusername
console.log 'matched'
otto.cache.stars = stars[username]
otto.mark_starred_items()
$('.browseresults-container').trigger('scrollstop')
@on 'reloadmodule': ->
modulename = @data.match(/otto[.]client[.](.*)[.]coffee/)[1]
console.log 'reloading module ' + modulename
if modulename is 'templates' # templates module exception
delete otto.templates
$('head').append '<script src="/otto.client.templates.js">'
else
if modulename of otto.client
delete otto.client[modulename]
otto.load_module modulename
@emit 'updateme'
@on 'restyle': ->
sheetname = @data.sheetname
console.log "restyle time! (#{sheetname})"
original_sheet = undefined
for sheet in document.styleSheets
continue if sheet.disabled
try
if (sheet.ownerNode?.dataset?.href?.indexOf '/'+sheetname) > -1 # is this Chrome specific?
console.log 'disabling old style sheet'
sheet.disabled = true
else if sheet.data is sheetname
console.log 'disabling old reloaded style sheet'
sheet.disabled = true
catch err
console.error 'failed to disable style sheet'
#$new_sheet = $('<style id="#'+@data.sheetname+'css">').html @data.css
$new_sheet = $('<style>').html @data.css
#$new_sheet[0].data = sheetname
$('head').append $new_sheet
document.styleSheets[document.styleSheets.length-1].data = sheetname
@on 'listeners': ->
#console.log 'listeners', @data
socketid = @socket?.socket?.sessionid
$('.listeners-container').html otto.templates.listeners(listeners: @data, socketid: socketid)
#$('.channellist-container').html otto.templates.channellist channellist: otto.channel_list, listeners: @data, socketid: socketid
# let's try to insert the listeners without rebuilding the entire channel list
# so that open channel setting don't go away every time a listener changes state
for channel in otto.channel_list
html = otto.templates.format_listeners_for_channel_in_channelbar listeners: @data, channelname: channel.name
$('.channellist-container [data-channelname="'+channel.name+'"] .channellisteners').html html
@on 'channellist': ->
console.log 'channellist'
otto.process_channellist @data
@on 'outputs': ->
@on 'lineout': ->
if @data
for channelname,lineout of @data
$el = $('.channellist-container .changechannel[data-channelname="'+channelname+'"]')
if $el
if lineout == '1'
$el.addClass 'lineout'
else
$el.removeClass 'lineout'
@on 'status': ->
return if not otto.clientready or not @data
for channelname,status of @data
$el = $('.channellist-container .changechannel[data-channelname="'+channelname+'"]')
if $el.length
if status.state == 'play'
$el.addClass 'playing'
else
$el.removeClass 'playing'
if channelname == otto.mychannel
# should prob. do this here too: if parseInt($vol.slider('value')) != parseInt(status.volume)
$('#mainpage .volumelineout').slider 'option', 'value', status.volume
$el.find('.volumelineout').each ->
$vol = $(@)
# when i do a log based volume formula, i'll need to reverse it here
if parseInt($vol.slider('value')) != parseInt(status.volume)
#console.log '$vol.value', $vol.slider('value'), 'status.volume', status.volume
$vol.slider 'option', 'value', status.volume
$el.find('.crossfade').each ->
if status.xfade is '0'
$(@).removeClass 'enabled'
else
$(@).addClass 'enabled'
$el.find('.channelerrata-container').html otto.templates.channel_status_errata_widget status: status
@on 'replaygain': ->
if @data
for channelname,replaygain of @data
$el = $('.channellist-container .changechannel[data-channelname="'+channelname+'"]')
$el.find('.replaygain').each ->
if replaygain is 'off'
$(@).removeClass 'enabled'
else
$(@).addClass 'enabled'
otto.process_channellist = (channellist, skiphtml) =>
otto.channel_list = channellist
unless skiphtml
$('.channellist-container').html otto.templates.channellist channellist: otto.channel_list
$('.volumelineout').slider value: otto.current_volume, range: 'min', slide: otto.adjust_volumelineout_handler
####
#### handlers
####
otto.checkbox_click_handler = (e) =>
$checkbox = $(e.target)
if not $checkbox.is 'input[type="checkbox"]'
return
e.stopPropagation()
# no longer used
if $checkbox.is '#fxtoggle'
otto.soundfx = $checkbox.is ':checked'
if otto.soundfx
otto.play_soundfx 'fxenabled'
# not used anymore
if $checkbox.is '#lineouttoggle'
otto.lineout = $checkbox.is ':checked'
if otto.lineout
$('.volumelineout').show()
else
$('.volumelineout').hide()
for channel in otto.channel_list
if channel.name is otto.mychannel
@emit 'lineout', otto.lineout
break
otto.button_click_handler = (e) =>
$button = $(e.target)
if not $button.is 'button'
$button = $(e.target).parents('button').first()
if not $button.is 'button'
#console.log 'this did not look like a button to me'
#console.log $button
return
e.stopPropagation()
find_id = ($el, ancestorlimit=2) ->
id = $el.data('id')
if id then return id
for oneclass in $el[0].className.split /\s/
found = oneclass.match /^id(.{24})$/
if found then return found[1]
$el.find("*").each ->
id = find_id $(this), 0
if id then return false # stops .each
if id then return id
if ancestorlimit > 0
return find_id $el.parent(), ancestorlimit - 1
return 0
find_qid = ($el, ancestorlimit=2) ->
qid = $el.data('mpdqueueid')
if qid then return qid
$el.find("*").each ->
qid = find_qid $(this), 0
if qid then return false # stop .each
if qid then return qid
if ancestorlimit > 0
return find_qid $el.parent(), ancestorlimit - 1
return 0
# check for unqueue class before enqueue since the button will be .enqueue.unqueue
if $button.is '.unqueue'
qid = find_qid $button
console.log 'deleteid', qid
otto.noscroll_click_event = e
@emit 'deleteid', qid
else if $button.is '.enqueue'
id = find_id $button
console.log 'enqueue', id
otto.noscroll_click_event = e
@emit 'enqueue', id
else if $button.is '.stars'
#console.log '.stars', e
console.log $button
#console.log 'e.pageX', e.pageX
console.log '$button.offset().left', $button.offset().left
if $('html').is '.doubler'
clickpoint = e.pageX - ($button.offset().left * 2) - 8
clickpoint = clickpoint / 2
else
clickpoint = e.pageX - $button.offset().left - 4
#console.log 'clickpoint', clickpoint
if clickpoint < 2
halfstars = 0
else if clickpoint < 11
halfstars = 1
else if clickpoint < 19
halfstars = 2
else if clickpoint < 26
halfstars = 3
else if clickpoint < 35
halfstars = 4
else if clickpoint < 42
halfstars = 5
else
halfstars = 6
#console.log halfstars
id = find_id $button
console.log "stars #{halfstars} " + id
$button.removeClass('n0 n1 n2 n3 n4 n5 n6').addClass('n' + halfstars)
@emit 'stars', id: id, rank: halfstars
else if $button.is '#connect'
console.log otto.connect_state
if otto.connect_state is 'disconnected'
otto.connect_player()
else
otto.disconnect_player()
else if $button.is '#play'
toggle_play.call @
else if $button.is '#next'
next_track.call @
else if $button.is '.smaller'
#if $('html').is '.doubler'
# console.log 'undoubler'
# $('html').removeClass('doubler')
#else
# $('.currenttrack-container').addClass('size1').removeClass('size2')
# $('.next-container').addClass('size1').removeClass('size2')
$('.currenttrack-container,.next-container,.size-container').addClass('size1').removeClass('size2')
otto.autosize_adjust()
if Modernizr.localstorage
localStorage.setItem 'size', 'size1'
else if $button.is '.bigger'
#window.resizeTo(1920, 1080) # just for debugging tv mode
#if $('.currenttrack-container').is '.size2'
# $('html').addClass('doubler')
#else
# $('.currenttrack-container').addClass('size2').removeClass('size1')
# $('.next-container').addClass('size2').removeClass('size1')
$('.currenttrack-container,.next-container,.size-container').addClass('size2').removeClass('size1')
otto.autosize_adjust()
if Modernizr.localstorage
localStorage.setItem 'size', 'size2'
else if $button.is '.close'
container_top = $button.parent().parent().parent().offset().top
$button.parent().remove()
if $('.browseresults-container').parent().scrollTop() > container_top
$('.browseresults-container').parent().scrollTop(container_top)
else if $button.is '.runself'
run = $button.data('run')
run()
else if $button.is '.download'
id = $button.data('id')
$iframe = $("<iframe class='download' id='#{id}' style='display:none'>")
$(document.body).append $iframe
$iframe.attr 'src', "/download/#{id}"
$iframe.load ->
console.log "iframe #{id} loaded"
#$iframe.remove() # this seems to cut off the download FIXME
else if $button.is '.chattoggle'
#$('.console-container').toggle(200)
if not otto.clientstate.inchat
otto.enable_chat true
else
otto.enable_chat false
else if $button.is '.channeltoggle'
toggle_channellist $button
else if $button.is '.logout'
@emit 'logout'
else if $button.is '.play'
@emit 'play', $button.data('position')
else if $button.is '.notificationstoggle'
if otto.notifications
otto.notifications = false
$button.removeClass 'enabled'
otto.lastnotification.close() if otto.lastnotification
else if Notification?
Notification.requestPermission (status) ->
console.log 'notifications permission', status # looking for "granted"
if status isnt "granted"
otto.notifications = false
$button.removeClass 'enabled'
else
otto.notifications = true
$button.addClass 'enabled'
n = new Notification "Notifications Enabled", {body: ""} # this also shows the notification
n.onshow = ->
timeoutSet 4000, -> n.close()
otto.lastnotification = n
else if $button.is '.soundfxtoggle'
if otto.soundfx
otto.soundfx = false
$button.removeClass 'enabled'
else
otto.soundfx = true
$button.addClass 'enabled'
otto.play_soundfx 'fxenabled'
else if $button.is '.selectfolder'
if otto.localhost and /Otto$/.test navigator.userAgent
#@emit('selectfolder') # uneven message processing in Otto.py make this unusable
# instead i use a UIDelegate on the webview to override the file selection input
# so the rest of this if should never be run
$('#selectFolder').click()
$('#selectFolder').change ->
alert 'sorry, you can\'t use the folder selection dialog from a web browser'
#$('.folder .path').text $('#selectFolder').val()
return false
else
$path = $('.folder .path')
$path.focus()
#$path.text $path.text() # move cursor to end? nope.
#len = $path.val().length
#$path[0].setSelectionRange(len, len) # nope (setSelectionRange not defined)
else if $button.is '.loadmusic'
@emit 'loadmusic', $('.folder .path').text()
else if $button.is '.loadmusic2'
if $button.text() is 'scan'
$button.html otto.templates.ouroboros size: 'small', direction: 'cw', speed: 'slow'
@emit 'loadmusic' # scan.py defaults to last directory scanned
else
$button.html('scan')
@emit 'loadmusiccancel'
else if $button.is '.begin'
console.log 'begin!'
@emit 'begin'
else if $button.is '.restartload'
console.log 'restartload'
otto.create_hellopage()
else
console.log 'did not know what action to do with button'
console.log $button
return
e.cancelBubble = true
if e.stopPropagation
e.stopPropagation()
otto.results_click_handler = (e) =>
$target = $(e.target)
if $target.is '.expand'
$expand = $target
else if $target.parent().is '.expand'
$expand = $target.parent()
if $target.is '.gotothere'
$gotothere = $target
else if $target.parent().is '.gotothere'
$gotothere = $target.parent()
else if $target.parent().parent().is '.gotothere'
$gotothere = $target.parent().parent()
if $expand
id = $expand.data('id')
containerid = $expand.data('container') || id
#$container = $(".id#{containerid}")
$parent = $expand.parent()
$container = $parent
if $parent.is '.thumbnails'
$container = $parent.parent()
$expanded = $('.expanded')
if $expanded.length and $expanded.data('id') == id
# same item, close it instead of redisplaying it (i.e. do nothing)
otto.render_without_scrolling $(e.target), ->
$expanded.remove()
$('.active').removeClass('active')
else
$.getJSON '/album_details', {'id': id}, (data) ->
if data.albums
data=data.albums
$.getJSON '/load_object', {'id': containerid}, (fileunder) ->
otto.render_without_scrolling $(e.target), ->
$expanded.remove()
$('.active').removeClass('active')
$element = $ "<div class='expanded cf' data-id=#{id}>"
$element.html otto.templates.albums_details data: [].concat(data), fileunder: fileunder, id: id
if $parent.is '.albumall'
# search forward to find where the line breaks and put it there
$eol = false
$last = $parent
while not $eol
$next = $last.next()
if $next.length is 0
$eol = $last
else if $next.offset().top > $last.offset().top
$eol = $last
else
$last = $next
$eol.after $element
else
$container.append $element
$expand.addClass('active')
otto.mark_allthethings()
else if $gotothere
id = $gotothere.data('id')
if $gotothere.is '.active'
$('.browseresults-container').empty()
$gotothere.removeClass('active')
console.log 'removing active'
else
$('.active').removeClass('active')
$gotothere.addClass('active')
console.log 'adding active'
if not $('.browseresults-container').children().first().is '.albumdetailscontainer'
$('.browseresults-container').empty()
$.getJSON '/load_object', {id: id, load_parents: 20}, (object) ->
if object.otype is 10
id = object.albums[0]._id
else if object.otype in [20, 30]
id = object._id
if object.otype in [10, 20]
$.getJSON '/album_details', {'id': id}, (data) ->
displayed_id = $('.browseresults-container').children().first().data('id')
if not displayed_id or displayed_id != id
if data.albums
data=data.albums
$('.browseresults-container').html otto.templates.albums_details data: [].concat(data), id: id
otto.mark_allthethings()
else if object.otype in [30]
# database is broken. artist->fileunder isn't recorded in collections! FIXME
# hack around this
id = $gotothere.data('albumid')
$.getJSON '/load_fileunder', {'artistid': id}, (fileunder) ->
id = 0
for fu in fileunder
if fu.key != 'various'
id = fu._id
if id
$.getJSON '/album_details', {'id': id}, (data) ->
displayed_id = $('.browseresults-container').children().first().data('id')
if not displayed_id or displayed_id != id
if data.albums
data=data.albums
$('.browseresults-container').html otto.templates.albums_details data: [].concat(data), id: id
otto.mark_allthethings()
else if $target.is('.progress') or $target.is('.progress-indicator')
if $target.is('.progress-indicator')
width = $target.parent().innerWidth()
adjust = 0
else
width = $target.innerWidth()
adjust = -2
seconds = Math.round( (e.offsetX+adjust) / (width-1) * otto.current_song_time.total)
console.log 'seconds', seconds
@emit 'seek', seconds
#if otto.connect_state isnt 'disconnected'
# otto.reconnect_player() # make it stop playing instantly (flush buffer)
#else if $target.is '.loadmusic' # i don't understand why this is needed here, why button_click_handler doesn't see it
# #@emit 'loadmusic', $('.folder .path').text()
# console.log 'this one'
# @emit 'loadmusic', '/Users/jon/Music'
else
console.log 'do not know what to do with clicks on this element:'
console.dir $target
console.dir e
toggle_play = ->
if otto.play_state is 'play'
@emit 'pause'
else
@emit 'play'
next_track = ->
qid = otto.current_track_qid
console.log 'deleteid', qid
@emit 'deleteid', qid
otto.current_song_time = false
#if otto.connect_state isnt 'disconnected'
# otto.reconnect_player() # make it stop playing instantly (flush buffer)
toggle_channellist = ($button) ->
$channellist = $('.channellist-container')
if $channellist.is '.mmenu-opened'
$channellist.trigger('close')
else
$channellist.trigger('open')
$button.trigger 'mousemove'
# webkit bug leaves the div hovered when it is moved from under cursor
#$('.channelbar').trigger 'mouseleave' # doesn't work
otto.channellist_click_handler = (e) =>
$target = $(e.target)
$button = $(e.target)
if $target.is 'button'
$button = $target
else
$button = $target.parents('button').first()
if $button.is 'button'
e.stopPropagation()
else
$button = false
find_channelname = ($el, ancestorlimit=4) ->
channelname = $el.data('channelname')
if channelname then return channelname
if ancestorlimit > 0
return find_channelname $el.parent(), ancestorlimit - 1
return false
if $button
if $button.is '.channeltoggle'
toggle_channellist $button
else if $button.is '.channeloutput'
channelname = find_channelname($target)
alt = e.altKey
@emit 'togglelineout', channelname: channelname, alt: e.altKey
else if $button.is '.crossfade'
channelname = find_channelname($target)
@emit 'togglecrossfade', channelname: channelname
else if $button.is '.replaygain'
channelname = find_channelname($target)
@emit 'togglereplaygain', channelname: channelname
else if $button.is '.channelplay'
channelname = find_channelname($target)
@emit 'toggleplay', channelname
else if $button.is '.channelsettings'
if $button.parent().is '.open'
$button.parent().parent().parent().find('.open').removeClass 'open'
else
$button.parent().parent().parent().find('.open').removeClass 'open'
$button.parent().addClass 'open'
else if $target.is '.channelselect, .channelname, .channellisteners, .listener'
newchannelname = find_channelname($target)
console.log 'change channel to', newchannelname
@emit 'changechannel', newchannelname
$('.channellist-container').trigger('close')
else
console.log 'do not know what to do about a click on this here element:'
console.dir $target
otto.console_click_handler = (e) ->
$target = $(e.target)
if $target.is '.console-container'
$target.focus()
return true