diff options
author | Bartek Szopka <bartek.szopka+github@gmail.com> | 2012-03-14 22:06:57 +0000 |
---|---|---|
committer | Bartek Szopka <bartek.szopka+github@gmail.com> | 2012-03-14 22:06:57 +0000 |
commit | 9d99c03392eda04e35a50a18dad9a7e121dedcac (patch) | |
tree | 7b53f13f3769ecf2df3b976ad23d1924235826f1 | |
parent | 1a21865ea693f5a59ea95a429731c833f525893d (diff) | |
download | impress.js-9d99c03392eda04e35a50a18dad9a7e121dedcac.tar.gz |
"`goto` now accepts more types of parameters: numbers, id strings and DOM elements"
-rw-r--r-- | README.md | 4 | ||||
-rw-r--r-- | index.html | 5 | ||||
-rw-r--r-- | js/impress.js | 14 |
3 files changed, 18 insertions, 5 deletions
@@ -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)) @@ -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; } |