1
1
// IWSY
2
2
3
- const IWSY = ( playerElement , text ) => {
3
+ const IWSY = ( playerElement , scriptObject ) => {
4
4
5
5
let player = playerElement ;
6
- let script = text ;
6
+ let script = scriptObject ;
7
+ let homeScript ;
8
+ let afterRun ;
9
+ let path ;
7
10
8
11
// Set up all the blocks
9
12
const setupBlocks = ( ) => {
@@ -281,7 +284,7 @@ const IWSY = (playerElement, text) => {
281
284
step . next ( ) ;
282
285
} else {
283
286
const element = document . createElement ( `div` ) ;
284
- block . element . parentElement . appendChild ( element ) ;
287
+ player . appendChild ( element ) ;
285
288
element . style . position = `absolute` ;
286
289
element . style . opacity = `0.0` ;
287
290
element . style . left = block . element . style . left ;
@@ -480,7 +483,12 @@ const IWSY = (playerElement, text) => {
480
483
const interval = setInterval ( ( ) => {
481
484
if ( animStep < animSteps ) {
482
485
const ratio = 0.5 - Math . cos ( Math . PI * animStep / animSteps ) / 2 ;
483
- doTransitionStep ( block , target , ratio ) ;
486
+ try {
487
+ doTransitionStep ( block , target , ratio ) ;
488
+ } catch ( err ) {
489
+ clearInterval ( interval ) ;
490
+ throw Error ( err ) ;
491
+ }
484
492
animStep ++ ;
485
493
} else {
486
494
clearInterval ( interval ) ;
@@ -502,10 +510,12 @@ const IWSY = (playerElement, text) => {
502
510
503
511
// Remove all the blocks from the player
504
512
const removeBlocks = ( ) => {
505
- for ( const block of script . blocks ) {
506
- if ( block . element ) {
507
- removeElement ( block . element ) ;
508
- delete ( block . element ) ;
513
+ if ( Array . isArray ( script . blocks ) ) {
514
+ for ( const block of script . blocks ) {
515
+ if ( block . element ) {
516
+ removeElement ( block . element ) ;
517
+ delete ( block . element ) ;
518
+ }
509
519
}
510
520
}
511
521
} ;
@@ -515,8 +525,6 @@ const IWSY = (playerElement, text) => {
515
525
const parent = element . parentElement ;
516
526
if ( parent ) {
517
527
parent . removeChild ( element ) ;
518
- } else {
519
- throw Error ( `element has no parent` ) ;
520
528
}
521
529
element . remove ( ) ;
522
530
} ;
@@ -545,7 +553,6 @@ const IWSY = (playerElement, text) => {
545
553
document . title = step . title ;
546
554
}
547
555
if ( step . css ) {
548
- console . log ( `Set styles at ${ step . index } ` ) ;
549
556
setHeadStyle ( step . css . split ( `%0a` ) . join ( `\n` ) ) ;
550
557
}
551
558
const aspect = step [ `aspect ratio` ] ;
@@ -589,22 +596,17 @@ const IWSY = (playerElement, text) => {
589
596
}
590
597
} ;
591
598
592
- // Chain to another presentation
593
- const chain = step => {
594
- step . next ( ) ;
595
- } ;
596
-
597
- // Embed another presentation
599
+ // Embed another script
598
600
const embed = step => {
599
601
step . next ( ) ;
600
602
} ;
601
603
602
604
// Restore the cursor
603
605
const restoreCursor = ( ) => {
604
606
player . style . cursor = `pointer` ;
605
- if ( script . then ) {
606
- script . then ( ) ;
607
- script . then = null ;
607
+ script = homeScript ;
608
+ if ( afterRun ) {
609
+ afterRun ( ) ;
608
610
}
609
611
} ;
610
612
@@ -821,7 +823,6 @@ const IWSY = (playerElement, text) => {
821
823
transition,
822
824
goto,
823
825
load,
824
- chain,
825
826
embed
826
827
} ;
827
828
@@ -843,27 +844,68 @@ const IWSY = (playerElement, text) => {
843
844
}
844
845
}
845
846
}
847
+
848
+ const onStepCB = script . onStepCB ;
849
+ if ( step . action === `chain` ) {
850
+ const runMode = script . runMode ;
851
+ fetch ( `${ path } ${ step . script } ` )
852
+ . then ( response => {
853
+ if ( response . status >= 400 ) {
854
+ throw Error ( `Unable to load ${ step . script } : ${ response . status } ` )
855
+ }
856
+ response . json ( ) . then ( data => {
857
+ script = data ;
858
+ if ( onStepCB ) {
859
+ onStepCB ( - 1 ) ;
860
+ }
861
+ initScript ( ) ;
862
+ script . runMode = runMode ;
863
+ doStep ( script . steps [ 1 ] ) ;
864
+ } ) ;
865
+ } )
866
+ . catch ( err => {
867
+ throw Error ( `Fetch Error :${ err } ` ) ;
868
+ } ) ;
869
+ return ;
870
+ }
871
+
846
872
const actionName = step . action . split ( ` ` ) . join ( `` ) ;
847
873
let handler = actions [ actionName ] ;
848
- if ( typeof handler === `undefined` ) {
849
- handler = IWSY . plugins [ actionName ] ;
874
+ if ( script . runMode === `auto` ) {
850
875
if ( typeof handler === `undefined` ) {
851
- throw Error ( `Unknown action: '${ step . action } '` ) ;
876
+ handler = IWSY . plugins [ actionName ] ;
877
+ if ( typeof handler === `undefined` ) {
878
+ throw Error ( `Unknown action: '${ step . action } '` ) ;
879
+ }
880
+ }
881
+ if ( onStepCB ) {
882
+ onStepCB ( step . index ) ;
883
+ }
884
+ try {
885
+ handler ( step ) ;
886
+ } catch ( err ) {
887
+ console . log ( `Step ${ step . index } (${ step . action } ): ${ err } ` ) ;
888
+ alert ( `Step ${ step . index } (${ step . action } ): ${ err } ` ) ;
889
+ }
890
+ } else {
891
+ try {
892
+ handler ( step ) ;
893
+ } catch ( err ) {
894
+ console . log ( JSON . stringify ( step , 0 , 2 ) + `\n` + JSON . stringify ( handler , 0 , 2 ) ) ;
895
+ console . log ( `Step ${ step . index } (${ step . action } ): ${ err } ` ) ;
896
+ alert ( `Step ${ step . index } (${ step . action } ): ${ err } ` ) ;
852
897
}
853
898
}
854
- if ( script . onStepCB && script . runMode === `auto` ) {
855
- script . onStepCB ( step . index ) ;
856
- }
857
- try {
858
- handler ( step ) ;
859
- } catch ( err ) {
860
- console . log ( `Step ${ step . index } (${ step . action } ): ${ err } ` ) ;
861
- alert ( `Step ${ step . index } (${ step . action } ): ${ err } ` ) ;
862
- }
899
+
863
900
} ;
864
901
865
902
///////////////////////////////////////////////////////////////////////////////
866
903
// These are all the exported functions
904
+
905
+ // Get the script
906
+ const getScript = ( ) => {
907
+ return script ;
908
+ } ;
867
909
868
910
// Set the script
869
911
const setScript = newScript => {
@@ -872,12 +914,17 @@ const IWSY = (playerElement, text) => {
872
914
initScript ( ) ;
873
915
} ;
874
916
917
+ // Set the path
918
+ const setPath = p => {
919
+ path = p ;
920
+ } ;
921
+
875
922
// Go to a specified step number
876
- const gotoStep = ( target ) => {
923
+ const gotoStep = target => {
877
924
script . scanTarget = target ;
878
925
script . runMode = `manual` ;
879
926
scan ( ) ;
880
- } ;
927
+ } ;
881
928
882
929
// Show a block
883
930
const block = blockIndex => {
@@ -936,7 +983,8 @@ const IWSY = (playerElement, text) => {
936
983
937
984
// Run the presentation
938
985
const run = ( mode , startMode , then ) => {
939
- script . then = then ;
986
+ homeScript = JSON . parse ( JSON . stringify ( script ) ) ;
987
+ afterRun = then ;
940
988
initScript ( ) ;
941
989
if ( mode === `fullscreen` ) {
942
990
if ( document . fullscreenElement ) {
@@ -996,7 +1044,9 @@ const IWSY = (playerElement, text) => {
996
1044
setupShowdown ( ) ;
997
1045
initScript ( ) ;
998
1046
return {
1047
+ getScript,
999
1048
setScript,
1049
+ setPath,
1000
1050
gotoStep,
1001
1051
block,
1002
1052
run,
0 commit comments