import { App } from "../../index" ; import { AppStore , AppStoreReplay } from "../../store" ; import { createDraft } from "immer" ; export class QuickAlgoLibraries { libraries : { [ name ] : { [ mode ] : QuickAlgoLibrary ; } ; } = { } ; addLibrary ( library , name , environment ) { if ( ! ( name in this . libraries ) ) { this . libraries [ name ] = { } ; } this . libraries [ name ] [ environment ] = library ; } getContext ( name = null , environment ) { if ( name in this . libraries ) { return this . libraries [ name ] [ environment ] ; } return Object . keys ( this . libraries ) . length ? this . libraries [ Object . keys ( this . libraries ) [ 0 ] ] [ environment ] : null ; } reset ( taskInfos = null , appState = null ) { this . applyOnLibraries ( 'reset' , [ taskInfos , appState ] ) ; } redrawDisplay ( taskInfos = null ) { this . applyOnLibraries ( 'redrawDisplay' , [ taskInfos ] ) ; } applyOnLibraries ( method , args ) { for ( let library of this . getAllLibraries ( ) ) { library [ method ] . apply ( library , args ) ; } } getVisualization ( ) { for ( let library of this . getAllLibraries ( ) ) { if ( library . getComponent ( ) ) { return library . getComponent ( ) ; } } return null ; } getSagas ( app ) { const sagas = [ ] ; for ( let library of this . getAllLibraries ( ) ) { const librarySagas = library . getSaga ( app ) ; if ( librarySagas ) { sagas . push ( librarySagas ) ; } } return sagas ; } getEventListeners ( ) { let listeners = { } as { [ key ] : { module : string ; method : string ; } ; } ; for ( let [ module , libraries ] of Object . entries ( this . libraries ) ) { for ( let library of Object . values ( libraries ) ) { const libraryListeners = library . getEventListeners ( ) ; if ( libraryListeners && Object . keys ( libraryListeners ) . length ) { for ( let [ eventName , method ] of Object . entries ( libraryListeners ) ) { listeners [ eventName ] = { module , method } ; } } } } return listeners ; } getAllLibraries ( ) { return Object . values ( this . libraries ) . reduce ( ( prev , libs ) => [ ... prev , ... Object . values ( libs ) ] , [ ] ) ; } } export const quickAlgoLibraries = new QuickAlgoLibraries ( ) ; window . quickAlgoLoadedLibraries = quickAlgoLibraries ; export class QuickAlgoLibrary { display : boolean ; infos : any ; nbCodes : number ; nbNodes : number ; strings : any ; customBlocks : any ; customConstants : any ; conceptList : any [ ] ; runner : any ; curNode : any ; lost : boolean = false ; aceEditor : any ; messagePrefixFailure : string ; messagePrefixSuccess : string ; linkBack : boolean ; delayFactory : any ; raphaelFactory : any ; constructor ( display , infos ) { this . display = display ; this . infos = infos ; this . nbCodes = 1 ; this . nbNodes = 1 ; this . strings = { } ; this . customBlocks = { } ; this . customConstants = { } ; this . conceptList = [ ] ; this . aceEditor = null ; this . messagePrefixFailure = '' ; this . messagePrefixSuccess = '' ; this . linkBack = false ; this . delayFactory = new window . DelayFactory ( ) ; this . raphaelFactory = new window . RaphaelFactory ( ) ; } setLocalLanguageStrings ( localLanguageStrings ) { window . stringsLanguage = window . stringsLanguage && window . stringsLanguage in localLanguageStrings ? window . stringsLanguage : "fr" ; window . languageStrings = window . languageStrings || { } ; if ( typeof window . languageStrings != "object" ) { console . error ( "window.languageStrings is not an object" ) ; } else { window . languageStrings = { ... window . languageStrings , ... localLanguageStrings [ window . stringsLanguage ] , } ; } this . strings = window . languageStrings ; return this . strings ; } ; importLanguageStrings ( source , dest ) { if ( ( typeof source != "object" ) || ( typeof dest != "object" ) ) { return ; } for ( let key1 of Object . keys ( source ) ) { if ( dest [ key1 ] != undefined && typeof dest [ key1 ] == "object" ) { this . importLanguageStrings ( source [ key1 ] , dest [ key1 ] ) ; } else { dest [ key1 ] = source [ key1 ] ; } } } ; getConceptList ( ) { return this . conceptList || [ ] ; } ; changeDelay ( newDelay ) { this . infos . actionDelay = newDelay ; } ; waitDelay ( callback , value = null ) { if ( this . runner ) { this . runner . returnCallback ( callback , value ) ; } else { callback ( value ) ; } } ; callCallback ( callback , value ) { if ( this . runner ) { this . runner . noDelay ( callback , value ) ; } else { callback ( value ) ; } } ; setCurNode ( curNode ) { this . curNode = curNode ; } ; reset ( taskInfos = null , appState = null ) { if ( this . display ) { this . redrawDisplay ( ) ; } } ; resetAndReloadState ( taskInfos = null , appState = null ) { this . reset ( taskInfos , appState ) ; if ( this . reloadInnerState ) { this . reloadInnerState ( createDraft ( this . getInnerState ( ) ) ) ; } } ; redrawDisplay ( ) { } ; updateScale ( ) { } ; unload ( ) { } ; provideBlocklyColours ( ) { return { } ; } ; getComponent ( ) { return null ; } ; getInnerState ( ) { return { } ; } ; reloadInnerState ( state ) { } getSaga ( app ) { return null ; } getEventListeners ( ) { return null ; } ; onError ( diagnostics ) { } onSuccess ( message ) { } onInput ( ) { } } window . quickAlgoResponsive = true ; window . quickAlgoContext = function ( display , infos ) { return new QuickAlgoLibrary ( display , infos ) ; } ;