helper.js (2331B)
1 import {__} from '@wordpress/i18n' 2 import CONFIG from './config'; 3 export default { 4 5 /** 6 * Get number of seconds left to complete the Challenge. 7 */ 8 getSecondsLeft: function() { 9 var secondsLeft = localStorage.getItem( 'reduxChallengeSecondsLeft' ); 10 11 secondsLeft = isNaN(secondsLeft) || secondsLeft == null ? CONFIG.initialSecondsLeft : parseInt( secondsLeft, 10 ); 12 13 return secondsLeft; 14 }, 15 16 /** 17 * Save number of seconds left to complete the Challenge. 18 */ 19 saveSecondsLeft: function( secondsLeft ) { 20 21 localStorage.setItem( 'reduxChallengeSecondsLeft', secondsLeft ); 22 }, 23 24 /** 25 * Get 'minutes' part of timer display. 26 */ 27 getMinutesFormatted: function( secondsLeft ) { 28 return Math.floor( secondsLeft / 60 ); 29 }, 30 31 /** 32 * Get 'seconds' part of timer display. 33 */ 34 getSecondsFormatted: function( secondsLeft ) { 35 return secondsLeft % 60; 36 }, 37 38 /** 39 * Get formatted timer for display. 40 */ 41 getFormatted: function( secondsLeft ) { 42 43 if (secondsLeft < 0) return '0:00'; 44 45 var timerMinutes = this.getMinutesFormatted( secondsLeft ); 46 var timerSeconds = this.getSecondsFormatted( secondsLeft ); 47 48 return timerMinutes + ( 9 < timerSeconds ? ':' : ':0' ) + timerSeconds; 49 }, 50 51 /** 52 * Get Localized time string for display 53 */ 54 getLocalizedDuration: function() { 55 let secondsLeft = this.getSecondsLeft(); 56 secondsLeft = CONFIG.initialSecondsLeft - secondsLeft; 57 58 var timerMinutes = this.getMinutesFormatted( secondsLeft ); 59 var timerSeconds = this.getSecondsFormatted( secondsLeft ); 60 61 const minutesString = timerMinutes ? timerMinutes + ' ' + __( 'minutes', redux_templates.i18n ) + ' ' : ''; 62 const secondsString = timerSeconds ? timerSeconds + ' ' + __( 'seconds', redux_templates.i18n ) : ''; 63 return minutesString + secondsString; 64 }, 65 66 /** 67 * Get last saved step. 68 */ 69 loadStep: function() { 70 71 var step = localStorage.getItem( 'reduxChallengeStep' ); 72 step = isNaN(step) ? -1 : parseInt( step, 10 ); 73 74 return step; 75 }, 76 77 /** 78 * Save Challenge step. 79 */ 80 saveStep: function( step ) { 81 localStorage.setItem( 'reduxChallengeStep', step ); 82 }, 83 };