@@ -776,8 +776,8 @@ class PlayModel extends BoundedIntModel {
776
776
return _ . extend ( super . defaults ( ) , {
777
777
_model_name : 'PlayModel' ,
778
778
_view_name : 'PlayView' ,
779
- _playing : false ,
780
- _repeat : false ,
779
+ repeat : false ,
780
+ playing : false ,
781
781
show_repeat : true ,
782
782
interval : 100 ,
783
783
step : 1 ,
@@ -789,42 +789,47 @@ class PlayModel extends BoundedIntModel {
789
789
}
790
790
791
791
loop ( ) : void {
792
- if ( this . get ( '_playing' ) ) {
793
- const next_value = this . get ( 'value' ) + this . get ( 'step' ) ;
794
- if ( next_value <= this . get ( 'max' ) ) {
795
- this . set ( 'value' , next_value ) ;
792
+ if ( ! this . get ( 'playing' ) ) {
793
+ return ;
794
+ }
795
+ const next_value = this . get ( 'value' ) + this . get ( 'step' ) ;
796
+ if ( next_value <= this . get ( 'max' ) ) {
797
+ this . set ( 'value' , next_value ) ;
798
+ this . schedule_next ( ) ;
799
+ } else {
800
+ if ( this . get ( 'repeat' ) ) {
801
+ this . set ( 'value' , this . get ( 'min' ) ) ;
796
802
this . schedule_next ( ) ;
797
803
} else {
798
- if ( this . get ( '_repeat' ) ) {
799
- this . set ( 'value' , this . get ( 'min' ) ) ;
800
- this . schedule_next ( ) ;
801
- } else {
802
- this . set ( '_playing' , false ) ;
803
- }
804
+ this . pause ( ) ;
804
805
}
805
- this . save_changes ( ) ;
806
806
}
807
+ this . save_changes ( ) ;
807
808
}
808
809
809
810
schedule_next ( ) : void {
810
- window . setTimeout ( this . loop . bind ( this ) , this . get ( 'interval' ) ) ;
811
+ this . _timerId = window . setTimeout ( this . loop . bind ( this ) , this . get ( 'interval' ) ) ;
811
812
}
812
813
813
814
stop ( ) : void {
814
- this . set ( '_playing' , false ) ;
815
+ this . pause ( ) ;
815
816
this . set ( 'value' , this . get ( 'min' ) ) ;
816
817
this . save_changes ( ) ;
817
818
}
818
819
819
820
pause ( ) : void {
820
- this . set ( '_playing' , false ) ;
821
+ window . clearTimeout ( this . _timerId ) ;
822
+ this . _timerId = null ;
823
+ this . set ( 'playing' , false ) ;
821
824
this . save_changes ( ) ;
822
825
}
823
826
824
- play ( ) : void {
825
- this . set ( '_playing' , true ) ;
826
- if ( this . get ( 'value' ) == this . get ( 'max' ) ) {
827
- // if the value is at the end, reset if first, and then schedule the next
827
+ animate ( ) : void {
828
+ if ( this . _timerId !== null ) {
829
+ return ;
830
+ }
831
+ if ( this . get ( 'value' ) === this . get ( 'max' ) ) {
832
+ // if the value is at the end, reset it first, and then schedule the next
828
833
this . set ( 'value' , this . get ( 'min' ) ) ;
829
834
this . schedule_next ( ) ;
830
835
this . save_changes ( ) ;
@@ -833,12 +838,20 @@ class PlayModel extends BoundedIntModel {
833
838
// loop will call save_changes in this case
834
839
this . loop ( ) ;
835
840
}
841
+ this . save_changes ( ) ;
842
+ }
843
+
844
+ play ( ) : void {
845
+ this . set ( 'playing' , ! this . get ( 'playing' ) ) ;
846
+ this . save_changes ( ) ;
836
847
}
837
848
838
849
repeat ( ) : void {
839
- this . set ( '_repeat ' , ! this . get ( '_repeat ' ) ) ;
850
+ this . set ( 'repeat ' , ! this . get ( 'repeat ' ) ) ;
840
851
this . save_changes ( ) ;
841
852
}
853
+
854
+ private _timerId : number | null = null ;
842
855
}
843
856
844
857
export
@@ -882,11 +895,11 @@ class PlayView extends DOMWidgetView {
882
895
this . stopButton . onclick = this . model . stop . bind ( this . model ) ;
883
896
this . repeatButton . onclick = this . model . repeat . bind ( this . model ) ;
884
897
885
- this . listenTo ( this . model , 'change:_playing ' , this . update_playing ) ;
886
- this . listenTo ( this . model , 'change:_repeat ' , this . update_repeat ) ;
887
- this . listenTo ( this . model , 'change:show_repeat' , this . update_repeat ) ;
888
- this . update_playing ( ) ;
889
- this . update_repeat ( ) ;
898
+ this . listenTo ( this . model , 'change:playing ' , this . onPlayingChanged ) ;
899
+ this . listenTo ( this . model , 'change:repeat ' , this . updateRepeat ) ;
900
+ this . listenTo ( this . model , 'change:show_repeat' , this . updateRepeat ) ;
901
+ this . updatePlaying ( ) ;
902
+ this . updateRepeat ( ) ;
890
903
this . update ( ) ;
891
904
}
892
905
@@ -896,11 +909,22 @@ class PlayView extends DOMWidgetView {
896
909
this . pauseButton . disabled = disabled ;
897
910
this . stopButton . disabled = disabled ;
898
911
this . repeatButton . disabled = disabled ;
899
- this . update_playing ( ) ;
912
+ this . updatePlaying ( ) ;
913
+ }
914
+
915
+ onPlayingChanged ( ) {
916
+ this . updatePlaying ( ) ;
917
+ const previous = this . model . previous ( 'playing' ) ;
918
+ const current = this . model . get ( 'playing' ) ;
919
+ if ( ! previous && current ) {
920
+ this . model . animate ( ) ;
921
+ } else {
922
+ this . model . pause ( ) ;
923
+ }
900
924
}
901
925
902
- update_playing ( ) : void {
903
- const playing = this . model . get ( '_playing ' ) ;
926
+ updatePlaying ( ) : void {
927
+ const playing = this . model . get ( 'playing ' ) ;
904
928
const disabled = this . model . get ( 'disabled' ) ;
905
929
if ( playing ) {
906
930
if ( ! disabled ) {
@@ -915,8 +939,8 @@ class PlayView extends DOMWidgetView {
915
939
}
916
940
}
917
941
918
- update_repeat ( ) : void {
919
- const repeat = this . model . get ( '_repeat ' ) ;
942
+ updateRepeat ( ) : void {
943
+ const repeat = this . model . get ( 'repeat ' ) ;
920
944
this . repeatButton . style . display = this . model . get ( 'show_repeat' ) ? this . playButton . style . display : 'none' ;
921
945
if ( repeat ) {
922
946
this . repeatButton . classList . add ( 'mod-active' ) ;
0 commit comments