From 93b846f07acf33ab080e9c9952f30318af1bd705 Mon Sep 17 00:00:00 2001 From: Mohamed Feddad Date: Thu, 2 Apr 2020 13:21:20 +0300 Subject: [PATCH] Add autoplay URL parameter trigger. (#764) Resolves #720 --- js/impress.js | 19 ++++++++++++++++--- src/lib/util.js | 14 +++++++++++++- src/plugins/README.md | 3 ++- src/plugins/autoplay/autoplay.js | 5 +++-- 4 files changed, 34 insertions(+), 7 deletions(-) diff --git a/js/impress.js b/js/impress.js index 1bab6d5..5274b4e 100644 --- a/js/impress.js +++ b/js/impress.js @@ -1207,6 +1207,17 @@ return byId( window.location.hash.replace( /^#\/?/, "" ) ); }; + // `getUrlParamValue` return a given URL parameter value if it exists + // `undefined` if it doesn't exist + var getUrlParamValue = function( parameter ) { + var chunk = window.location.search.split( parameter + "=" )[ 1 ]; + var value = chunk && chunk.split( "&" )[ 0 ]; + + if ( value !== "" ) { + return value; + } + }; + // Throttling function calls, by Remy Sharp // http://remysharp.com/2010/07/21/throttling-function-calls/ var throttle = function( fn, delay ) { @@ -1243,7 +1254,8 @@ getElementFromHash: getElementFromHash, throttle: throttle, toNumber: toNumber, - triggerEvent: triggerEvent + triggerEvent: triggerEvent, + getUrlParamValue: getUrlParamValue }; roots[ rootId ] = lib; return lib; @@ -1287,9 +1299,10 @@ // Element attributes starting with "data-", become available under // element.dataset. In addition hyphenized words become camelCased. var data = root.dataset; + var autoplay = util.getUrlParamValue( "impress-autoplay" ) || data.autoplay; - if ( data.autoplay ) { - autoplayDefault = util.toNumber( data.autoplay, 0 ); + if ( autoplay ) { + autoplayDefault = util.toNumber( autoplay, 0 ); } var toolbar = document.querySelector( "#impress-toolbar" ); diff --git a/src/lib/util.js b/src/lib/util.js index bd7dcbb..b0d3a93 100644 --- a/src/lib/util.js +++ b/src/lib/util.js @@ -49,6 +49,17 @@ return byId( window.location.hash.replace( /^#\/?/, "" ) ); }; + // `getUrlParamValue` return a given URL parameter value if it exists + // `undefined` if it doesn't exist + var getUrlParamValue = function( parameter ) { + var chunk = window.location.search.split( parameter + "=" )[ 1 ]; + var value = chunk && chunk.split( "&" )[ 0 ]; + + if ( value !== "" ) { + return value; + } + }; + // Throttling function calls, by Remy Sharp // http://remysharp.com/2010/07/21/throttling-function-calls/ var throttle = function( fn, delay ) { @@ -85,7 +96,8 @@ getElementFromHash: getElementFromHash, throttle: throttle, toNumber: toNumber, - triggerEvent: triggerEvent + triggerEvent: triggerEvent, + getUrlParamValue: getUrlParamValue }; roots[ rootId ] = lib; return lib; diff --git a/src/plugins/README.md b/src/plugins/README.md index 0f9b1f0..31d3908 100644 --- a/src/plugins/README.md +++ b/src/plugins/README.md @@ -14,7 +14,8 @@ rather require the user to invoke them somehow. For example: * The *navigation* plugin waits for the user to press some keys, arrows, page down, page up, space or tab. * The *autoplay* plugin looks for the HTML attribute `data-autoplay` to see - whether it should do its thing. + whether it should do its thing. It can also be triggered with a URL GET parameter + `?impress-autoplay=5` *5 is the waiting duration*. * The *toolbar* plugin looks for a `
` element to become visible. Extra addons diff --git a/src/plugins/autoplay/autoplay.js b/src/plugins/autoplay/autoplay.js index 1d4eadb..54f3631 100644 --- a/src/plugins/autoplay/autoplay.js +++ b/src/plugins/autoplay/autoplay.js @@ -31,9 +31,10 @@ // Element attributes starting with "data-", become available under // element.dataset. In addition hyphenized words become camelCased. var data = root.dataset; + var autoplay = util.getUrlParamValue( "impress-autoplay" ) || data.autoplay; - if ( data.autoplay ) { - autoplayDefault = util.toNumber( data.autoplay, 0 ); + if ( autoplay ) { + autoplayDefault = util.toNumber( autoplay, 0 ); } var toolbar = document.querySelector( "#impress-toolbar" );