Introduce plugin framework
* Source files are under src/ * js/impress.js is now generated, but remains part of the repo (so it just works) * npm run build * build.js uses buildify node module * Break out navigation and resize plugins from core src/impress.js file
This commit is contained in:
407
src/plugins/README.md
Normal file
407
src/plugins/README.md
Normal file
@@ -0,0 +1,407 @@
|
||||
Impress.js Plugins documentation
|
||||
================================
|
||||
|
||||
The default set of plugins
|
||||
--------------------------
|
||||
|
||||
A lot of impress.js features are and will be implemented as plugins. Each plugin
|
||||
has user documentation in a README.md file in [its own directory](./).
|
||||
|
||||
The plugins in this directory are called default plugins, and - unsurprisingly -
|
||||
are enabled by default. However, most of them won't do anything by default,
|
||||
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.
|
||||
* The *toolbar* plugin looks for a `<div>` element to become visible.
|
||||
|
||||
Extra addons
|
||||
------------
|
||||
|
||||
Yet more features are available in presentations that enable
|
||||
[extra addons](../../extras/). Extra addons are 3rd party plugins integrated
|
||||
into impress.js to provide convenient and standardized access to them. However,
|
||||
they are not activated by default, rather must be included with a `<script>`
|
||||
tag.
|
||||
|
||||
Note: The enabled extra addons are automatically initialized by the *extras*
|
||||
plugin.
|
||||
|
||||
Example HTML and CSS
|
||||
--------------------
|
||||
|
||||
Generally plugins will do something sane, or nothing, by default. Hence, no
|
||||
particular HTML or CSS is required. The README file of each plugin documents the
|
||||
HTML and CSS that you can use with that plugin.
|
||||
|
||||
For your convenience, below is some sample HTML and CSS code covering all the
|
||||
plugins that you may want to use or adapt.
|
||||
|
||||
### Sample HTML to enable plugins and extra addons
|
||||
|
||||
<head>
|
||||
<!-- CSS files if using Highlight.js or Mermaid.js extras. -->
|
||||
<link rel="stylesheet" href="../../extras/highlight/styles/github.css">
|
||||
<link rel="stylesheet" href="../../extras/mermaid/mermaid.forest.css">
|
||||
</head>
|
||||
<body>
|
||||
<div id="impress" data-autoplay="10">
|
||||
<div class="step"
|
||||
data-autoplay="15"
|
||||
data-rel-x="1000"
|
||||
data-rel-y="1000">
|
||||
|
||||
<h1>Slide content</h1>
|
||||
|
||||
<ul>
|
||||
<li class="substep">Point 1</li>
|
||||
<li class="substep">Point 2</li>
|
||||
</ul>
|
||||
|
||||
<div class="notes">
|
||||
Speaker notes are shown in the impressConsole.
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div id="impress-toolbar"></div>
|
||||
<div class="impress-progressbar"><div></div></div>
|
||||
<div class="impress-progress"></div>
|
||||
<div id="impress-help"></div>
|
||||
|
||||
<script type="text/javascript" src="../../extras/highlight/highlight.pack.js"></script>
|
||||
<script type="text/javascript" src="../../extras/mermaid/mermaid.min.js"></script>
|
||||
<script type="text/javascript" src="../../extras/markdown/markdown.js"></script>
|
||||
<script type="text/javascript" src="../../extras/mathjax/MathJax.js?config=TeX-AMS_CHTML"></script>
|
||||
</body>
|
||||
|
||||
### Sample CSS related to plugins and extra addons
|
||||
|
||||
/* Using the substep plugin, hide bullet points at first, then show them one by one. */
|
||||
#impress .step .substep {
|
||||
opacity: 0;
|
||||
}
|
||||
|
||||
#impress .step .substep.substep-visible {
|
||||
opacity: 1;
|
||||
transition: opacity 1s;
|
||||
}
|
||||
/*
|
||||
Speaker notes allow you to write comments within the steps, that will not
|
||||
be displayed as part of the presentation. However, they will be picked up
|
||||
and displayed by impressConsole.js when you press P.
|
||||
*/
|
||||
.notes {
|
||||
display: none;
|
||||
}
|
||||
|
||||
/* Toolbar plugin */
|
||||
.impress-enabled div#impress-toolbar {
|
||||
position: fixed;
|
||||
right: 1px;
|
||||
bottom: 1px;
|
||||
opacity: 0.6;
|
||||
z-index: 10;
|
||||
}
|
||||
.impress-enabled div#impress-toolbar > span {
|
||||
margin-right: 10px;
|
||||
}
|
||||
.impress-enabled div#impress-toolbar.impress-toolbar-show {
|
||||
display: block;
|
||||
}
|
||||
.impress-enabled div#impress-toolbar.impress-toolbar-hide {
|
||||
display: none;
|
||||
}
|
||||
/* If you disable pointer-events (like in the impress.js official demo), you need to re-enable them for the toolbar. */
|
||||
.impress-enabled #impress-toolbar { pointer-events: auto }
|
||||
/* Progress bar */
|
||||
.impress-enabled .impress-progressbar {
|
||||
position: absolute;
|
||||
right: 318px;
|
||||
bottom: 1px;
|
||||
left: 118px;
|
||||
border-radius: 7px;
|
||||
border: 2px solid rgba(100, 100, 100, 0.2);
|
||||
}
|
||||
.impress-enabled .impress-progressbar DIV {
|
||||
width: 0;
|
||||
height: 2px;
|
||||
border-radius: 5px;
|
||||
background: rgba(75, 75, 75, 0.4);
|
||||
transition: width 1s linear;
|
||||
}
|
||||
.impress-enabled .impress-progress {
|
||||
position: absolute;
|
||||
left: 59px;
|
||||
bottom: 1px;
|
||||
text-align: left;
|
||||
opacity: 0.6;
|
||||
}
|
||||
.impress-enabled #impress-help {
|
||||
background: none repeat scroll 0 0 rgba(0, 0, 0, 0.5);
|
||||
color: #EEEEEE;
|
||||
font-size: 80%;
|
||||
position: fixed;
|
||||
left: 2em;
|
||||
bottom: 2em;
|
||||
width: 24em;
|
||||
border-radius: 1em;
|
||||
padding: 1em;
|
||||
text-align: center;
|
||||
z-index: 100;
|
||||
font-family: Verdana, Arial, Sans;
|
||||
}
|
||||
.impress-enabled #impress-help td {
|
||||
padding-left: 1em;
|
||||
padding-right: 1em;
|
||||
}
|
||||
|
||||
|
||||
For developers
|
||||
==============
|
||||
|
||||
The vision for impress.js is to provide a compact core library doing the
|
||||
actual presentations, with a collection of plugins that provide additional
|
||||
functionality. A default set of plugins are distributed together with the core
|
||||
impress.js, and are located in this directory. They are called *default plugins*
|
||||
because they are distributed and active when users use the [js/impress.js](../../js/impress.js)
|
||||
in their presentations.
|
||||
|
||||
Building js/impress.js
|
||||
-----------------------
|
||||
|
||||
The common way to use impress.js is to link to the file
|
||||
[js/impress.js](../../js/impress.js). This is a simple concatenation of the
|
||||
core impress.js and all plugins in this directory. If you edit or add code
|
||||
under [src/](../), you can run `node build.js` to recreate the distributable
|
||||
`js/impress.js` file. The build script also creates a minified file, but this
|
||||
is not included in the git repository.
|
||||
|
||||
### Tip: Build errors
|
||||
|
||||
If your code has parse errors, the `build.js` will print a rather unhelpful
|
||||
exception like
|
||||
|
||||
/home/hingo/hacking/impress.js/js/impress.js
|
||||
|
||||
/home/hingo/hacking/impress.js/node_modules/uglify-js/lib/parse-js.js:271
|
||||
throw new JS_Parse_Error(message, line, col, pos);
|
||||
^
|
||||
Error
|
||||
at new JS_Parse_Error (/home/hingo/hacking/impress.js/node_modules/uglify-js/lib/parse-js.js:263:18)
|
||||
at js_error (/home/hingo/hacking/impress.js/node_modules/uglify-js/lib/parse-js.js:271:11)
|
||||
at croak (/home/hingo/hacking/impress.js/node_modules/uglify-js/lib/parse-js.js:733:9)
|
||||
at token_error (/home/hingo/hacking/impress.js/node_modules/uglify-js/lib/parse-js.js:740:9)
|
||||
at unexpected (/home/hingo/hacking/impress.js/node_modules/uglify-js/lib/parse-js.js:746:9)
|
||||
at Object.semicolon [as 1] (/home/hingo/hacking/impress.js/node_modules/uglify-js/lib/parse-js.js:766:43)
|
||||
at prog1 (/home/hingo/hacking/impress.js/node_modules/uglify-js/lib/parse-js.js:1314:21)
|
||||
at simple_statement (/home/hingo/hacking/impress.js/node_modules/uglify-js/lib/parse-js.js:906:27)
|
||||
at /home/hingo/hacking/impress.js/node_modules/uglify-js/lib/parse-js.js:814:19
|
||||
at block_ (/home/hingo/hacking/impress.js/node_modules/uglify-js/lib/parse-js.js:1003:20)
|
||||
|
||||
You will be pleased to know, that the concatenation of the unminified file
|
||||
[js/impress.js](../../js/impress.js) has already succeeded at this point. Just
|
||||
open a test in your browser, and the browser will show you the line and error.
|
||||
|
||||
|
||||
### Structure, naming and policy
|
||||
|
||||
Each plugin is contained within its own directory. The name of the directory
|
||||
is the name of the plugin. For example, imagine a plugin called *pluginA*:
|
||||
|
||||
src/plugins/plugina/
|
||||
|
||||
The main javascript file should use the directory name as its root name:
|
||||
|
||||
src/plugins/plugina/plugina.js
|
||||
|
||||
For most plugins, a single `.js` file is enough.
|
||||
|
||||
Note that the plugin name is also used as a namespace for various things. For
|
||||
example, the *autoplay* plugin can be configured by setting the `data-autoplay="5"`
|
||||
attribute on a `div`.
|
||||
|
||||
As a general rule ids, classes and attributes within the `div#impress` root
|
||||
element, may use the plugin name directly (e.g. `data-autoplay="5"`). However,
|
||||
outside of the root element, you should use `impress-pluginname` (e.g.
|
||||
`<div id="impress-toolbar">`. The latter (longer) form also applies to all
|
||||
events, they should be prefixed with `impress:pluginname`.
|
||||
|
||||
You should use crisp and descriptive names for your plugins. But
|
||||
sometimes you might optimize for a short namespace. Hence, the
|
||||
[Relative Positioning Plugin](rel/rel.js) is called `rel` to keep html attributes
|
||||
short. You should not overuse this idea!
|
||||
|
||||
Note that for default plugins, which is all plugins in this directory,
|
||||
**NO css, html or image files** are allowed.
|
||||
|
||||
Default plugins must not add any global variables.
|
||||
|
||||
### Testing
|
||||
|
||||
The plugin directory should also include tests, which should use the *QUnit* and
|
||||
*Syn* libraries under [test/](../../test). You can have as many tests as you like,
|
||||
but it is suggested your first and main test file is called `plugina_tests.html`
|
||||
and `plugina_tests.js` respectively. You need to add your test `.js` file into
|
||||
[/qunit_test_runner.html](../../qunit_test_runner.html), and the `.js` file
|
||||
should start by loading the test `.html` file into the
|
||||
`iframe#presentation-iframe`. See [navigation-ui](navigation-ui) plugin for an
|
||||
example.
|
||||
|
||||
You are allowed to test your plugin whatever way you like, but the general
|
||||
approach is for the test to load the [js/impress.js](../../js/impress.js) file
|
||||
produced by build.js. This way you are testing what users will actually be
|
||||
using, rather than the uncompiled source code.
|
||||
|
||||
HowTo write a plugin
|
||||
--------------------
|
||||
|
||||
### Encapsulation
|
||||
|
||||
To avoid polluting the global namespace, plugins must encapsulate them in the
|
||||
standard javascript anonymous function:
|
||||
|
||||
/**
|
||||
* Plugin A - An example plugin
|
||||
*
|
||||
* Description...
|
||||
*
|
||||
* Copyright 2016 Firstname Lastname, email or github handle
|
||||
* Released under the MIT license.
|
||||
*/
|
||||
(function ( document, window ) {
|
||||
|
||||
// Plugin implementation...
|
||||
|
||||
})(document, window);
|
||||
|
||||
|
||||
### Init plugins
|
||||
|
||||
We categorize plugins into various categories, based on how and when they are
|
||||
called, and what they do.
|
||||
|
||||
An init plugin is the simplest kind of plugin. It simply listens for the
|
||||
`impress().init()` method to send the `impress:init` event, at which point
|
||||
the plugin can initialize itself and start doing whatever it does, for example
|
||||
by calling methods in the public api returned by `impress()`.
|
||||
|
||||
Both [Navigation](navigation/navigation.js) and [Autoplay](autoplay/autoplay.js)
|
||||
are init plugins.
|
||||
|
||||
To provide end user configurability in your plugin, a good idea might be to
|
||||
read html attributes from the impress presentation. The
|
||||
[Autoplay](autoplay/autoplay.js) plugin does exactly this, you can provide
|
||||
a default value in the `div#impress` element, or in each `div.step`.
|
||||
|
||||
A plugin must only use html attributes in its designated namespace, which is
|
||||
|
||||
data-pluginName-*="value"
|
||||
|
||||
For example, if *pluginA* offers config options `foo` and `bar`, it would look
|
||||
like this:
|
||||
|
||||
<div id="impress" data-plugina-foo="5" data-plugina-bar="auto" >
|
||||
|
||||
|
||||
### Pre-init plugins
|
||||
|
||||
Some plugins need to run before even impress().init() does anything. These
|
||||
are typically *filters*: they want to modify the html via DOM calls, before
|
||||
impress.js core parses the presentation. We call these *pre-init plugins*.
|
||||
|
||||
A pre-init plugin must be called synchronously, before `impress().init()` is
|
||||
executed. Plugins can register themselves to be called in the pre-init phase
|
||||
by calling:
|
||||
|
||||
impress.addPreInitPlugin( plugin [, weight] );
|
||||
|
||||
The argument `plugin` must be a function. `weight` is optional and defaults to
|
||||
`10`. Plugins are ordered by weight when they are executed, with lower weight
|
||||
first.
|
||||
|
||||
The [Relative Positioning Plugin](rel/rel.js) is an example of a pre-init plugin.
|
||||
|
||||
### Pre-StepLeave plugins
|
||||
|
||||
A *pre-stepleave plugin* is called synchronously from impress.js core at the
|
||||
beginning of `impress().goto()`.
|
||||
|
||||
To register a plugin, call
|
||||
|
||||
impress.addPreStepLeavePlugin( plugin [, weight] );
|
||||
|
||||
When the plugin function is executed, it will be passed an argument
|
||||
that resembles the `event` object from DOM event handlers:
|
||||
|
||||
`event.target` contains the current step, which we are about to leave.
|
||||
|
||||
`event.detail.next` contains the element we are about to transition to.
|
||||
|
||||
`event.detail.reason` contains a string, one of "next", "prev" or "goto",
|
||||
which tells you which API function was called to initiate the transition.
|
||||
|
||||
`event.detail.transitionDuration` contains the transitionDuration for the
|
||||
upcoming transition.
|
||||
|
||||
A pre-stepleave plugin may alter the values in `event.detail` (except for
|
||||
`reason`), and this can change the behavior of the upcoming transition.
|
||||
For example, the `goto` plugin will set the `event.detail.next` to point to
|
||||
some other element, causing the presentation to jump to that step instead.
|
||||
|
||||
|
||||
### GUI plugins
|
||||
|
||||
A *GUI plugin* is actually just an init plugin, but is a special category that
|
||||
exposes visible widgets or effects in the presentation. For example, it might
|
||||
provide clickable buttons to go to the next and previous slide.
|
||||
|
||||
Note that all plugins shipped in the default set **must not** produce any visible
|
||||
html elements unless the user asks for it. A recommended best practice is to let
|
||||
the user add a div element, with an id equaling the plugin's namespace, in the
|
||||
place where he wants to see whatever visual UI elements the plugin is providing:
|
||||
|
||||
<div id="impress-plugina"></div>
|
||||
|
||||
Another way to show the elements of a UI plugin might be by allowing the user
|
||||
to explicitly press a key, like "H" for a help dialog.
|
||||
|
||||
[Toolbar plugin](toolbar/README.md) is an example of a GUI plugin. It presents
|
||||
a toolbar where other plugins can add their buttons in a centralized fashion.
|
||||
|
||||
Remember that for default plugins, even GUI plugins, no html files, css files
|
||||
or images are allowed. Everything must be generated from javascript. The idea
|
||||
is that users can theme widgets with their own CSS. (A plugin is of course welcome
|
||||
to provide example CSS that can be copypasted :-)
|
||||
|
||||
Dependencies
|
||||
------------
|
||||
|
||||
If *pluginB* depends on the existence of *pluginA*, and also *pluginA* must run
|
||||
before *pluginB*, then *pluginB* should not listen to the `impress:init` event,
|
||||
rather *pluginA* should send its own init event, which *pluginB* listens to.
|
||||
|
||||
Example:
|
||||
|
||||
// pluginA
|
||||
document.addEventListener("impress:init", function (event) {
|
||||
// plugin A does it's own initialization first...
|
||||
|
||||
// Signal other plugins that plugin A is now initialized
|
||||
var root = document.querySelector( "div#impress" );
|
||||
var event = document.createEvent("CustomEvent");
|
||||
event.initCustomEvent("impress:plugina:init', true, true, { "plugina" : "data..." });
|
||||
root.dispatchEvent(event);
|
||||
}, false);
|
||||
|
||||
// pluginB
|
||||
document.addEventListener("impress:init", function (event) {
|
||||
// plugin B implementation
|
||||
}, false);
|
||||
|
||||
A plugin should use the namespace `impress:pluginname:*` for any events it sends.
|
||||
|
||||
In theory all plugins could always send an `init` and other events, but in
|
||||
practice we're adding them on an as needed basis.
|
||||
174
src/plugins/navigation/navigation.js
Normal file
174
src/plugins/navigation/navigation.js
Normal file
@@ -0,0 +1,174 @@
|
||||
/**
|
||||
* Navigation events plugin
|
||||
*
|
||||
* As you can see this part is separate from the impress.js core code.
|
||||
* It's because these navigation actions only need what impress.js provides with
|
||||
* its simple API.
|
||||
*
|
||||
* This plugin is what we call an _init plugin_. It's a simple kind of
|
||||
* impress.js plugin. When loaded, it starts listening to the `impress:init`
|
||||
* event. That event listener initializes the plugin functionality - in this
|
||||
* case we listen to some keypress and mouse events. The only dependencies on
|
||||
* core impress.js functionality is the `impress:init` method, as well as using
|
||||
* the public api `next(), prev(),` etc when keys are pressed.
|
||||
*
|
||||
* Copyright 2011-2012 Bartek Szopka (@bartaz)
|
||||
* Released under the MIT license.
|
||||
* ------------------------------------------------
|
||||
* author: Bartek Szopka
|
||||
* version: 0.5.3
|
||||
* url: http://bartaz.github.com/impress.js/
|
||||
* source: http://github.com/bartaz/impress.js/
|
||||
*
|
||||
*/
|
||||
/* global document */
|
||||
( function( document ) {
|
||||
"use strict";
|
||||
|
||||
var triggerEvent = function( el, eventName, detail ) {
|
||||
var event = document.createEvent( "CustomEvent" );
|
||||
event.initCustomEvent( eventName, true, true, detail );
|
||||
el.dispatchEvent( event );
|
||||
};
|
||||
|
||||
// Wait for impress.js to be initialized
|
||||
document.addEventListener( "impress:init", function( event ) {
|
||||
|
||||
// Getting API from event data.
|
||||
// So you don't event need to know what is the id of the root element
|
||||
// or anything. `impress:init` event data gives you everything you
|
||||
// need to control the presentation that was just initialized.
|
||||
var api = event.detail.api;
|
||||
|
||||
// Supported keys are:
|
||||
// [space] - quite common in presentation software to move forward
|
||||
// [up] [right] / [down] [left] - again common and natural addition,
|
||||
// [pgdown] / [pgup] - often triggered by remote controllers,
|
||||
// [tab] - this one is quite controversial, but the reason it ended up on
|
||||
// this list is quite an interesting story... Remember that strange part
|
||||
// in the impress.js code where window is scrolled to 0,0 on every presentation
|
||||
// step, because sometimes browser scrolls viewport because of the focused element?
|
||||
// Well, the [tab] key by default navigates around focusable elements, so clicking
|
||||
// it very often caused scrolling to focused element and breaking impress.js
|
||||
// positioning. I didn't want to just prevent this default action, so I used [tab]
|
||||
// as another way to moving to next step... And yes, I know that for the sake of
|
||||
// consistency I should add [shift+tab] as opposite action...
|
||||
var isNavigationEvent = function( event ) {
|
||||
|
||||
// Don't trigger navigation for example when user returns to browser window with ALT+TAB
|
||||
if ( event.altKey || event.ctrlKey || event.metaKey ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// In the case of TAB, we force step navigation always, overriding the browser
|
||||
// navigation between input elements, buttons and links.
|
||||
if ( event.keyCode === 9 ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// With the sole exception of TAB, we also ignore keys pressed if shift is down.
|
||||
if ( event.shiftKey ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// For arrows, etc, check that event target is html or body element. This is to allow
|
||||
// presentations to have, for example, forms with input elements where user can type
|
||||
// text, including space, and not move to next step.
|
||||
if ( event.target.nodeName !== "BODY" && event.target.nodeName !== "HTML" ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( ( event.keyCode >= 32 && event.keyCode <= 34 ) ||
|
||||
( event.keyCode >= 37 && event.keyCode <= 40 ) ) {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
// KEYBOARD NAVIGATION HANDLERS
|
||||
|
||||
// Prevent default keydown action when one of supported key is pressed.
|
||||
document.addEventListener( "keydown", function( event ) {
|
||||
if ( isNavigationEvent( event ) ) {
|
||||
event.preventDefault();
|
||||
}
|
||||
}, false );
|
||||
|
||||
// Trigger impress action (next or prev) on keyup.
|
||||
document.addEventListener( "keyup", function( event ) {
|
||||
if ( isNavigationEvent( event ) ) {
|
||||
if ( event.shiftKey ) {
|
||||
switch ( event.keyCode ) {
|
||||
case 9: // Shift+tab
|
||||
api.prev();
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
switch ( event.keyCode ) {
|
||||
case 33: // Pg up
|
||||
case 37: // Left
|
||||
case 38: // Up
|
||||
api.prev( event );
|
||||
break;
|
||||
case 9: // Tab
|
||||
case 32: // Space
|
||||
case 34: // Pg down
|
||||
case 39: // Right
|
||||
case 40: // Down
|
||||
api.next( event );
|
||||
break;
|
||||
}
|
||||
}
|
||||
event.preventDefault();
|
||||
}
|
||||
}, false );
|
||||
|
||||
// Delegated handler for clicking on the links to presentation steps
|
||||
document.addEventListener( "click", function( event ) {
|
||||
|
||||
// Event delegation with "bubbling"
|
||||
// check if event target (or any of its parents is a link)
|
||||
var target = event.target;
|
||||
while ( ( target.tagName !== "A" ) &&
|
||||
( target !== document.documentElement ) ) {
|
||||
target = target.parentNode;
|
||||
}
|
||||
|
||||
if ( target.tagName === "A" ) {
|
||||
var href = target.getAttribute( "href" );
|
||||
|
||||
// If it's a link to presentation step, target this step
|
||||
if ( href && href[ 0 ] === "#" ) {
|
||||
target = document.getElementById( href.slice( 1 ) );
|
||||
}
|
||||
}
|
||||
|
||||
if ( api.goto( target ) ) {
|
||||
event.stopImmediatePropagation();
|
||||
event.preventDefault();
|
||||
}
|
||||
}, false );
|
||||
|
||||
// Delegated handler for clicking on step elements
|
||||
document.addEventListener( "click", function( event ) {
|
||||
var target = event.target;
|
||||
|
||||
// Find closest step element that is not active
|
||||
while ( !( target.classList.contains( "step" ) &&
|
||||
!target.classList.contains( "active" ) ) &&
|
||||
( target !== document.documentElement ) ) {
|
||||
target = target.parentNode;
|
||||
}
|
||||
|
||||
if ( api.goto( target ) ) {
|
||||
event.preventDefault();
|
||||
}
|
||||
}, false );
|
||||
|
||||
// Add a line to the help popup
|
||||
triggerEvent( document, "impress:help:add",
|
||||
{ command: "Left & Right", text: "Previous & Next step", row: 1 } );
|
||||
|
||||
}, false );
|
||||
|
||||
} )( document );
|
||||
|
||||
169
src/plugins/navigation/navigation_tests.js
Normal file
169
src/plugins/navigation/navigation_tests.js
Normal file
@@ -0,0 +1,169 @@
|
||||
/*
|
||||
* Copyright 2016 Henrik Ingo (@henrikingo)
|
||||
*
|
||||
* Released under the MIT license. See LICENSE file.
|
||||
*/
|
||||
/* global QUnit, loadIframe, initPresentation, document, window */
|
||||
|
||||
QUnit.module( "Navigation plugin" );
|
||||
|
||||
QUnit.test( "Navigation Plugin", function( assert ) {
|
||||
window.console.log( "Begin navigation plugin" );
|
||||
var done = assert.async();
|
||||
|
||||
loadIframe( "test/core_tests_presentation.html", assert, function() {
|
||||
initPresentation( assert, function() {
|
||||
var iframe = document.getElementById( "presentation-iframe" );
|
||||
var iframeDoc = iframe.contentDocument;
|
||||
var iframeWin = iframe.contentWindow;
|
||||
|
||||
var wait = 5; // Milliseconds
|
||||
|
||||
var step1 = iframeDoc.querySelector( "div#step-1" );
|
||||
var step2 = iframeDoc.querySelector( "div#step-2" );
|
||||
var step3 = iframeDoc.querySelector( "div#step-3" );
|
||||
var step4 = iframeDoc.querySelector( "div#fourth" );
|
||||
var root = iframeDoc.querySelector( "div#impress" );
|
||||
|
||||
var i = 0;
|
||||
var sequence = [ { left: step1,
|
||||
entered: step2,
|
||||
next: function() { return iframeWin.syn.type( "bodyid", " " ); },
|
||||
text: "space (2->3)" },
|
||||
{ left: step2,
|
||||
entered: step3,
|
||||
next: function() { return iframeWin.syn.type( "bodyid", "[right]" ); },
|
||||
text: "[right] (3->4)" },
|
||||
{ left: step3,
|
||||
entered: step4,
|
||||
next: function() { return iframeWin.syn.type( "bodyid", "\t" ); },
|
||||
text: "tab (4->1)" },
|
||||
{ left: step4,
|
||||
entered: step1,
|
||||
next: function() { return iframeWin.syn.type( "bodyid", "[down]" ); },
|
||||
text: "[down] (1->2)" },
|
||||
{ left: step1,
|
||||
entered: step2,
|
||||
next: function() { return iframeWin.syn.type( "bodyid", "[page-down]" ); },
|
||||
text: "[page-down] (2->3)" },
|
||||
{ left: step2,
|
||||
entered: step3,
|
||||
next: function() { return iframeWin.syn.type( "bodyid", "[page-up]" ); },
|
||||
text: "[page-up] (3->2)" },
|
||||
{ left: step3,
|
||||
entered: step2,
|
||||
next: function() { return iframeWin.syn.type( "bodyid", "[left]" ); },
|
||||
text: "[left] (2->1)" },
|
||||
{ left: step2,
|
||||
entered: step1,
|
||||
next: function() { return iframeWin.syn.type( "bodyid", "[up]" ); },
|
||||
text: "[up] (1->4)" },
|
||||
{ left: step1,
|
||||
entered: step4,
|
||||
next: function() { return iframeWin.syn.click( "step-2", {} ); },
|
||||
text: "click on 2 (4->2)" },
|
||||
{ left: step4,
|
||||
entered: step2,
|
||||
next: function() { return iframeWin.syn.click( "linktofourth", {} ); },
|
||||
text: "click on link with href to id=fourth (2->4)" },
|
||||
{ left: step2,
|
||||
entered: step4,
|
||||
next: function() { return iframeWin.impress().goto( 0 ); },
|
||||
text: "Return to first step with goto(0)." },
|
||||
{ left: step4,
|
||||
entered: step1,
|
||||
next: false }
|
||||
];
|
||||
|
||||
var readyCount = 0;
|
||||
var readyForNext = function() {
|
||||
readyCount++;
|
||||
if ( readyCount % 2 === 0 ) {
|
||||
if ( sequence[ i ].next ) {
|
||||
assert.ok( sequence[ i ].next(), sequence[ i ].text );
|
||||
i++;
|
||||
} else {
|
||||
window.console.log( "End navigation plugin" );
|
||||
done();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Things to check on impress:stepenter event -----------------------------//
|
||||
var assertStepEnter = function( event ) {
|
||||
assert.equal( event.target, sequence[ i ].entered,
|
||||
event.target.id + " triggered impress:stepenter event." );
|
||||
readyForNext();
|
||||
};
|
||||
|
||||
var assertStepEnterWrapper = function( event ) {
|
||||
window.setTimeout( function() { assertStepEnter( event ); }, wait );
|
||||
};
|
||||
root.addEventListener( "impress:stepenter", assertStepEnterWrapper );
|
||||
|
||||
// Things to check on impress:stepleave event -----------------------------//
|
||||
var assertStepLeave = function( event ) {
|
||||
assert.equal( event.target, sequence[ i ].left,
|
||||
event.target.id + " triggered impress:stepleave event." );
|
||||
readyForNext();
|
||||
};
|
||||
|
||||
var assertStepLeaveWrapper = function( event ) {
|
||||
window.setTimeout( function() { assertStepLeave( event ); }, wait );
|
||||
};
|
||||
root.addEventListener( "impress:stepleave", assertStepLeaveWrapper );
|
||||
|
||||
assert.ok( iframeWin.impress().next(), "next() called and returns ok (1->2)" );
|
||||
} ); // InitPresentation()
|
||||
} ); // LoadIframe()
|
||||
} );
|
||||
|
||||
QUnit.test( "Navigation Plugin - No-op tests", function( assert ) {
|
||||
window.console.log( "Begin navigation no-op" );
|
||||
var done = assert.async();
|
||||
|
||||
loadIframe( "test/core_tests_presentation.html", assert, function() {
|
||||
initPresentation( assert, function() {
|
||||
var iframe = document.getElementById( "presentation-iframe" );
|
||||
var iframeDoc = iframe.contentDocument;
|
||||
var iframeWin = iframe.contentWindow;
|
||||
|
||||
var wait = 5; // Milliseconds
|
||||
|
||||
var root = iframeDoc.querySelector( "div#impress" );
|
||||
|
||||
// This should never happen -----------------------------//
|
||||
var assertStepEnter = function( event ) {
|
||||
assert.ok( false,
|
||||
event.target.id + " triggered impress:stepenter event." );
|
||||
};
|
||||
|
||||
var assertStepEnterWrapper = function( event ) {
|
||||
window.setTimeout( function() { assertStepEnter( event ); }, wait );
|
||||
};
|
||||
root.addEventListener( "impress:stepenter", assertStepEnterWrapper );
|
||||
|
||||
// This should never happen -----------------------------//
|
||||
var assertStepLeave = function( event ) {
|
||||
assert.ok( false,
|
||||
event.target.id + " triggered impress:stepleave event." );
|
||||
};
|
||||
|
||||
var assertStepLeaveWrapper = function( event ) {
|
||||
window.setTimeout( function() { assertStepLeave( event ); }, wait );
|
||||
};
|
||||
root.addEventListener( "impress:stepleave", assertStepLeaveWrapper );
|
||||
|
||||
// These are no-op actions, we're already in step-1 -----------------------//
|
||||
assert.ok( iframeWin.syn.click( "step-1", {} ),
|
||||
"Click on step that is currently active, should do nothing." );
|
||||
assert.ok( iframeWin.syn.click( "linktofirst", {} ),
|
||||
"Click on link pointing to step that is currently active, should do nothing." );
|
||||
|
||||
// After delay, if no event triggers are called. We're done.
|
||||
window.setTimeout( function() {
|
||||
window.console.log( "End navigation no-op" ); done();
|
||||
}, 3000 );
|
||||
} ); // InitPresentation()
|
||||
} ); // LoadIframe()
|
||||
} );
|
||||
47
src/plugins/resize/resize.js
Normal file
47
src/plugins/resize/resize.js
Normal file
@@ -0,0 +1,47 @@
|
||||
/**
|
||||
* Resize plugin
|
||||
*
|
||||
* Rescale the presentation after a window resize.
|
||||
*
|
||||
* Copyright 2011-2012 Bartek Szopka (@bartaz)
|
||||
* Released under the MIT license.
|
||||
* ------------------------------------------------
|
||||
* author: Bartek Szopka
|
||||
* version: 0.5.3
|
||||
* url: http://bartaz.github.com/impress.js/
|
||||
* source: http://github.com/bartaz/impress.js/
|
||||
*
|
||||
*/
|
||||
|
||||
/* global document, window */
|
||||
|
||||
( function( document, window ) {
|
||||
"use strict";
|
||||
|
||||
// Throttling function calls, by Remy Sharp
|
||||
// http://remysharp.com/2010/07/21/throttling-function-calls/
|
||||
var throttle = function( fn, delay ) {
|
||||
var timer = null;
|
||||
return function() {
|
||||
var context = this, args = arguments;
|
||||
window.clearTimeout( timer );
|
||||
timer = window.setTimeout( function() {
|
||||
fn.apply( context, args );
|
||||
}, delay );
|
||||
};
|
||||
};
|
||||
|
||||
// Wait for impress.js to be initialized
|
||||
document.addEventListener( "impress:init", function( event ) {
|
||||
var api = event.detail.api;
|
||||
|
||||
// Rescale presentation when window is resized
|
||||
window.addEventListener( "resize", throttle( function() {
|
||||
|
||||
// Force going to active step again, to trigger rescaling
|
||||
api.goto( document.querySelector( ".step.active" ), 500 );
|
||||
}, 250 ), false );
|
||||
}, false );
|
||||
|
||||
} )( document, window );
|
||||
|
||||
Reference in New Issue
Block a user