@@ -777,8 +777,8 @@ class PlayModel extends BoundedIntModel {
777
777
return _ . extend ( super . defaults ( ) , {
778
778
_model_name : 'PlayModel' ,
779
779
_view_name : 'PlayView' ,
780
- _playing : false ,
781
780
_repeat : false ,
781
+ playing : false ,
782
782
show_repeat : true ,
783
783
interval : 100 ,
784
784
step : 1 ,
@@ -790,41 +790,40 @@ class PlayModel extends BoundedIntModel {
790
790
}
791
791
792
792
loop ( ) {
793
- if ( this . get ( '_playing' ) ) {
794
- let next_value = this . get ( 'value' ) + this . get ( 'step' ) ;
795
- if ( next_value <= this . get ( 'max' ) ) {
796
- this . set ( 'value' , next_value ) ;
793
+ if ( ! this . get ( 'playing' ) ) {
794
+ return ;
795
+ }
796
+ let next_value = this . get ( 'value' ) + this . get ( 'step' ) ;
797
+ if ( next_value <= this . get ( 'max' ) ) {
798
+ this . set ( 'value' , next_value ) ;
799
+ this . schedule_next ( ) ;
800
+ } else {
801
+ if ( this . get ( '_repeat' ) ) {
802
+ this . set ( 'value' , this . get ( 'min' ) ) ;
797
803
this . schedule_next ( ) ;
798
804
} else {
799
- if ( this . get ( '_repeat' ) ) {
800
- this . set ( 'value' , this . get ( 'min' ) ) ;
801
- this . schedule_next ( ) ;
802
- } else {
803
- this . set ( '_playing' , false ) ;
804
- }
805
+ this . set ( 'playing' , false ) ;
805
806
}
806
- this . save_changes ( ) ;
807
807
}
808
+ this . save_changes ( ) ;
808
809
}
809
810
810
811
schedule_next ( ) {
811
812
window . setTimeout ( this . loop . bind ( this ) , this . get ( 'interval' ) ) ;
812
813
}
813
814
814
815
stop ( ) {
815
- this . set ( '_playing' , false ) ;
816
816
this . set ( 'value' , this . get ( 'min' ) ) ;
817
817
this . save_changes ( ) ;
818
818
}
819
819
820
820
pause ( ) {
821
- this . set ( '_playing ' , false ) ;
821
+ this . set ( 'playing ' , false ) ;
822
822
this . save_changes ( ) ;
823
823
}
824
824
825
825
play ( ) {
826
- this . set ( '_playing' , true ) ;
827
- if ( this . get ( 'value' ) == this . get ( 'max' ) ) {
826
+ if ( this . get ( 'value' ) === this . get ( 'max' ) ) {
828
827
// if the value is at the end, reset if first, and then schedule the next
829
828
this . set ( 'value' , this . get ( 'min' ) ) ;
830
829
this . schedule_next ( ) ;
@@ -834,6 +833,7 @@ class PlayModel extends BoundedIntModel {
834
833
// loop will call save_changes in this case
835
834
this . loop ( ) ;
836
835
}
836
+ this . save_changes ( ) ;
837
837
}
838
838
839
839
repeat ( ) {
@@ -878,15 +878,15 @@ class PlayView extends DOMWidgetView {
878
878
repeatIcon . className = 'fa fa-retweet' ;
879
879
this . repeatButton . appendChild ( repeatIcon ) ;
880
880
881
- this . playButton . onclick = this . model . play . bind ( this . model ) ;
881
+ this . playButton . onclick = this . onPlayClicked . bind ( this ) ;
882
882
this . pauseButton . onclick = this . model . pause . bind ( this . model ) ;
883
883
this . stopButton . onclick = this . model . stop . bind ( this . model ) ;
884
884
this . repeatButton . onclick = this . model . repeat . bind ( this . model ) ;
885
885
886
- this . listenTo ( this . model , 'change:_playing ' , this . update_playing ) ;
886
+ this . listenTo ( this . model , 'change:playing ' , this . onPlayingChanged ) ;
887
887
this . listenTo ( this . model , 'change:_repeat' , this . update_repeat ) ;
888
888
this . listenTo ( this . model , 'change:show_repeat' , this . update_repeat ) ;
889
- this . update_playing ( ) ;
889
+ this . updatePlaying ( ) ;
890
890
this . update_repeat ( ) ;
891
891
this . update ( ) ;
892
892
}
@@ -897,11 +897,27 @@ class PlayView extends DOMWidgetView {
897
897
this . pauseButton . disabled = disabled ;
898
898
this . stopButton . disabled = disabled ;
899
899
this . repeatButton . disabled = disabled ;
900
- this . update_playing ( ) ;
900
+ this . updatePlaying ( ) ;
901
+ }
902
+
903
+ onPlayClicked ( ) {
904
+ const playing = this . model . get ( 'playing' ) ;
905
+ this . model . set ( 'playing' , ! playing ) ;
906
+ }
907
+
908
+ onPlayingChanged ( ) {
909
+ this . updatePlaying ( ) ;
910
+ const previous = this . model . previous ( 'playing' ) ;
911
+ const current = this . model . get ( 'playing' ) ;
912
+ if ( ! previous && current ) {
913
+ this . model . play ( ) ;
914
+ } else {
915
+ this . model . pause ( ) ;
916
+ }
901
917
}
902
918
903
- update_playing ( ) {
904
- let playing = this . model . get ( '_playing ' ) ;
919
+ updatePlaying ( ) {
920
+ let playing = this . model . get ( 'playing ' ) ;
905
921
let disabled = this . model . get ( 'disabled' ) ;
906
922
if ( playing ) {
907
923
if ( ! disabled ) {
0 commit comments