From 9d99c03392eda04e35a50a18dad9a7e121dedcac Mon Sep 17 00:00:00 2001 From: Bartek Szopka Date: Wed, 14 Mar 2012 22:06:57 +0000 Subject: [PATCH] "`goto` now accepts more types of parameters: numbers, id strings and DOM elements" --- README.md | 4 ++++ index.html | 5 +++-- js/impress.js | 14 +++++++++++--- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 5117459..060bfd6 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,10 @@ VERSION HISTORY - `past` class is added to already visited steps (when the step is left) * and good news, `goto()` API method is back! it seems that `goto` **was** a future reserved word but isn't anymore, so we can use this short and pretty name instead of camelCassy `stepTo` - and yes, that means API changed again... +* additionally `goto()` method now supports new types of parameters: + - you can give it a number of step you want to go to: `impress().goto(7)` + - or its id: `impress().goto("the-best-slide-ever")` + - of course DOM element is still acceptable: `impress().goto( document.getElementById("overview") )` ### 0.4.1 ([browse](http://github.com/bartaz/impress.js/tree/0.4.1), [zip](http://github.com/bartaz/impress.js/zipball/0.4.1), [tar](http://github.com/bartaz/impress.js/tarball/0.4.1)) diff --git a/index.html b/index.html index 72e8e5d..e81557e 100644 --- a/index.html +++ b/index.html @@ -339,8 +339,9 @@ if ("ontouchstart" in document.documentElement) { `api.init()` - initializes the presentation, `api.next()` - moves to next step of the presentation, - `api.prev()` - moves to previous step of the presentation - `api.goto( stepElement ) - moves the presentation to given step element (the DOM element of the step). + `api.prev()` - moves to previous step of the presentation, + `api.goto( idx | id | element )` - moves the presentation to the step given by its index number, + id or the DOM element. You can also simply call `impress()` again to get the API, so `impress().next()` is also allowed. Don't worry, it wont initialize the presentation again. diff --git a/js/impress.js b/js/impress.js index 6e8d7c0..e06edbc 100644 --- a/js/impress.js +++ b/js/impress.js @@ -356,10 +356,18 @@ triggerEvent(root, "impress-init", { api: roots[ "impress-root-" + rootId ] }); }; + var getStep = function ( step ) { + if (typeof step === "number") { + step = step < 0 ? steps[ steps.length + step] : steps[ step ]; + } else if (typeof step === "string") { + step = byId(step); + } + return (step && step.id && stepsData["impress-" + step.id]) ? step : null; + }; + var goto = function ( el, force ) { - if ( !initialized || - !(el && el.id && stepsData["impress-" + el.id]) || // element is not a step - (el === activeStep && !force) ) { + + if ( !initialized || !(el = getStep(el)) || (el === activeStep && !force) ) { // selected element is not defined as step or is already active return false; }