Share Gaia assets and adapters, keep private profiles and development documents local, and update both public READMEs.
11 lines
No EOL
5.3 KiB
JavaScript
11 lines
No EOL
5.3 KiB
JavaScript
;(function(exports){'use strict';var currentLayout='tiny';var dom={};var elementIDs=['tutorial','tutorial-step-title','tutorial-step-media','tutorial-step-image','tutorial-step-video','forward-tutorial','back-tutorial'];var Tutorial={config:null,_stepsConfig:{},_stepsKey:'',_currentStep:1,_initialized:false,init:function(stepsKey,onLoaded){if(this._initialized||this._initialization){return;}
|
|
var initTasks=this._initialization=new TutorialUtils.Sequence(this.loadConfig.bind(this),this._initWithConfig.bind(this,stepsKey));initTasks.onabort=this._onAbortInitialization.bind(this);initTasks.oncomplete=this._onCompleteInitialization.bind(this,onLoaded);elementIDs.forEach(function(name){dom[Utils.camelCase(name)]=document.getElementById(name);if(!dom[Utils.camelCase(name)]){console.error('Cache DOM elements: couldnt cache: '+name);}},this);initTasks.next();var mediaEvents=this._mediaEvents=['abort','canplay','canplaythrough','durationchange','emptied','ended','error','interruptbegin','interruptend','loadeddata','loadedmetadata','loadstart','mozaudioavailable','pause','play','playing','stalled','suspend','waiting'];this._debugEventHandler={handleEvent:function(evt){var errCodes={'1':'MEDIA_ERR_ABORTED','2':'MEDIA_ERR_NETWORK','3':'MEDIA_ERR_DECODE','4':'MEDIA_ERR_SRC_NOT_SUPPORTED'};console.log('TUTORIAL: media-event: '+evt.type);if(evt.type=='error'){var errtype=errCodes[evt.target.error.code]||'unknown';console.log('TUTORIAL: media-event, '+errtype+' error');}}};mediaEvents.forEach(name=>{dom.tutorialStepVideo.addEventListener(name,this._debugEventHandler);});},start:function(onReady){var sequence;var initInProgress=false;if(this._initialization){sequence=this._initialization;initInProgress=true;}else{this._initialization=sequence=new TutorialUtils.Sequence();sequence.onabort=this._onAbortInitialization.bind(this);sequence.oncomplete=this._onCompleteInitialization.bind(this);}
|
|
sequence.push(function setInitialStep(){console.log('TUTORIAL: setInitialStep');return this._setStep();}.bind(this));sequence.push(function showTutorialAndFinishInit(){dom.tutorial.classList.add('show');console.log('TUTORIAL: dispatching tutorialinitialized');window.dispatchEvent(new CustomEvent('tutorialinitialized'));});if(typeof onReady==='function'){sequence.push(onReady);}
|
|
if(!initInProgress){sequence.next();}},_initWithConfig:function(stepsKey){stepsKey=stepsKey||'default';this._stepsKey=stepsKey;this._stepsConfig=this.config[stepsKey]||this.config['default'];dom.forwardTutorial.addEventListener('click',this);dom.backTutorial.addEventListener('click',this);this._currentStep=1;},_onAbortInitialization:function(){this._initialization=null;},_onCompleteInitialization:function(onReady){this._initialization=null;this._initialized=true;if(typeof onReady==='function'){onReady();}},_setStep:function(value){value=(typeof value==='number')?value:this._currentStep;var stepIndex=value-1;if(stepIndex>=this._stepsConfig.steps.length){return Promise.resolve().then(function(){Tutorial.done();});}
|
|
var stepData=this._stepsConfig.steps[stepIndex];if(!stepData){return Promise.reject('No data for step: '+value);}
|
|
dom.tutorial.dataset.step=this._currentStep;document.l10n.setAttributes(dom.tutorialStepTitle,stepData.l10nKey);var imgElement=dom.tutorialStepImage,videoElement=dom.tutorialStepVideo;var stepPromise;if(stepData.video){stepPromise=TutorialUtils.getBestAssetForDirection(stepData.video).then((bestSrc)=>{return TutorialUtils.loadAndPlayMedia(videoElement,bestSrc);});videoElement.hidden=false;imgElement.hidden=true;}else{imgElement.hidden=false;stepPromise=TutorialUtils.loadMedia(imgElement,stepData.image);imgElement.hidden=false;videoElement.hidden=true;}
|
|
return stepPromise;},handleEvent:function(evt){if(evt.type==='click'){switch(evt.target){case dom.forwardTutorial:this.next(evt);break;case dom.backTutorial:this.back(evt);break;}}},next:function(){return this._setStep(++this._currentStep);},back:function(){return this._setStep(--this._currentStep);},done:function(){var isUpgrade=this._stepsKey&&this._stepsKey!=='default';FinishScreen.init(isUpgrade);dom.tutorial.classList.remove('show');dom.tutorialStepVideo.removeAttribute('src');dom.tutorialStepImage.removeAttribute('src');},loadConfig:function(){if(!this._configPromise){currentLayout=ScreenLayout.getCurrentLayout()==='tiny'?'tiny':'large';var configUrl='/config/'+currentLayout+'.json';this._configPromise=LazyLoader.getJSON(configUrl).then(function(json){Tutorial.config=json;return Tutorial.config;},function(){return new Error('Tutorial config failed to load from: '+configUrl);});}
|
|
return this._configPromise;},reset:function(){var resetPromise=Promise.resolve();if(dom.tutorialStepVideo){this._mediaEvents.forEach(name=>{dom.tutorialStepVideo.removeEventListener(name,this._debugEventHandler);});dom.tutorialStepVideo.hidden=true;if(dom.tutorialStepVideo.src){resetPromise=new Promise((resolve,reject)=>{dom.tutorialStepVideo.addEventListener('emptied',()=>{resolve();});dom.tutorialStepVideo.removeAttribute('src');dom.tutorialStepVideo.load();});}}
|
|
if(this._initialization){this._initialization.abort();this._initialization=null;}
|
|
this._configPromise=null;this._currentStep=1;this._stepsConfig=this.config=null;if(this._initialized){dom.tutorial.classList.remove('show');this._initialized=false;}
|
|
document.getElementById('tutorial').classList.remove('show');return resetPromise;}};exports.Tutorial=Tutorial;})(this); |