Skip to content

Commit

Permalink
"goto now accepts more types of parameters: numbers, id strings and…
Browse files Browse the repository at this point in the history
… DOM elements"
  • Loading branch information
bartaz committed Mar 14, 2012
1 parent 1a21865 commit 9d99c03
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
5 changes: 3 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,9 @@ <h1>impress.js<sup>*</sup></h1>
`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.
Expand Down
14 changes: 11 additions & 3 deletions js/impress.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 9d99c03

Please sign in to comment.