Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add full help screen to help plugin #714

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 81 additions & 37 deletions js/impress.js
Original file line number Diff line number Diff line change
Expand Up @@ -1836,11 +1836,6 @@
/**
* Help popup plugin
*
* Example:
*
* <!-- Show a help popup at start, or if user presses "H" -->
* <div id="impress-help"></div>
*
* For developers:
*
* Typical use for this plugin, is for plugins that support some keypress, to add a line
Expand All @@ -1856,46 +1851,88 @@
var rows = [];
var timeoutHandle;

var triggerEvent = function( el, eventName, detail ) {
var event = document.createEvent( "CustomEvent" );
event.initCustomEvent( eventName, true, true, detail );
el.dispatchEvent( event );
var body = document.querySelector( "body" );

// This querySelector is used to keep compatibility with templates using div#impress-help
var helpDiv = document.querySelector( "#impress-help" ) || document.createElement( "div" );
body.appendChild( helpDiv );

var fullHelpDiv = document.createElement( "div" );
var fullHelpStyle = {
display: "none",
position: "fixed",
top: 0,
bottom: 0,
left: 0,
right: 0,
backgroundColor: "#000",
color: "#FFF",
opacity: 0.8,
textAlign: "center",
padding: "3em"
};

var renderHelpDiv = function() {
var helpDiv = document.getElementById( "impress-help" );
if ( helpDiv ) {
body.appendChild( fullHelpDiv );

var css = function( elem, style ) {
for ( var p in style ) {
elem.style[ p ] = style[ p ];
}
};
css( fullHelpDiv, fullHelpStyle );

var renderDiv = function( elem, shortOnly ) {
if ( elem ) {
var html = [];
for ( var row in rows ) {
for ( var arrayItem in row ) {
html.push( rows[ row ][ arrayItem ] );
var item = rows[ row ][ arrayItem ];
if ( item ) {
if ( ( shortOnly && item.short ) || !shortOnly ) {
html.push( item.row );
}
}
}
}
if ( html ) {
helpDiv.innerHTML = "<table>\n" + html.join( "\n" ) + "</table>\n";
elem.innerHTML = "<table style='width: 100%'>\n" + html.join( "\n" ) + "</table>\n";
}
}
};

var toggleHelp = function() {
var helpDiv = document.getElementById( "impress-help" );
if ( !helpDiv ) {
var renderHelpDiv = function() {
renderDiv( helpDiv, true );
renderDiv( fullHelpDiv, false );
};

var toggle = function( elem ) {
if ( !elem ) {
return;
}

if ( helpDiv.style.display === "block" ) {
helpDiv.style.display = "none";
if ( elem.style.display === "block" ) {
elem.style.display = "none";
} else {
helpDiv.style.display = "block";
window.clearTimeout( timeoutHandle );
elem.style.display = "block";
if ( elem === helpDiv ) {
window.clearTimeout( timeoutHandle );
} else {

// Hides helpDiv when fullHelpDiv is shown
helpDiv.style.display = "none";
window.clearTimeout( timeoutHandle );
}
}
};

document.addEventListener( "keyup", function( event ) {

if ( event.keyCode === 72 || event.keyCode === 191 ) { // "h" || "?"
event.preventDefault();
toggleHelp();
toggle( helpDiv );
} else if ( event.key === "F1" ) {
event.preventDefault();
toggle( fullHelpDiv );
}
}, false );

Expand All @@ -1907,6 +1944,7 @@
* :param: e.detail.command Example: "H"
* :param: e.detail.text Example: "Show this help."
* :param: e.detail.row Row index from 0 to 9 where to place this help text. Example: 0
* :param: e.detail.short If this help must appear in the short help. Example: true
*/
document.addEventListener( "impress:help:add", function( e ) {

Expand All @@ -1915,39 +1953,44 @@
// its own array. If there are more than one entry for the same index, they are shown in
// first come, first serve ordering.
var rowIndex = e.detail.row;
if ( typeof rows[ rowIndex ] !== "object" || !rows[ rowIndex ].isArray ) {
rows[ rowIndex ] = [];
}
rows[ e.detail.row ].push( "<tr><td><strong>" + e.detail.command + "</strong></td><td>" +
e.detail.text + "</td></tr>" );
var short = e.detail.short;
rows[ rowIndex ] = rows[ rowIndex ] || [];
rows[ e.detail.row ].push( {
row: "<tr><td><strong>" + e.detail.command + "</strong></td><td>" +
e.detail.text + "</td></tr>",
short: !!short
} );
renderHelpDiv();
} );

document.addEventListener( "impress:init", function( e ) {
var api = e.detail.api;
var gc = api.lib.gc;
var util = api.lib.util;

renderHelpDiv();

// At start, show the help for 7 seconds.
var helpDiv = document.getElementById( "impress-help" );
if ( helpDiv ) {
helpDiv.style.display = "block";
timeoutHandle = window.setTimeout( function() {
var helpDiv = document.getElementById( "impress-help" );
helpDiv.style.display = "none";
}, 7000 );

// Regster callback to empty the help div on teardown
var api = e.detail.api;
api.lib.gc.pushCallback( function() {
gc.pushCallback( function() {
window.clearTimeout( timeoutHandle );
helpDiv.style.display = "";
helpDiv.innerHTML = "";
helpDiv.remove();
fullHelpDiv.remove();
rows = [];
} );
}

// Use our own API to register the help text for "h"
triggerEvent( document, "impress:help:add",
{ command: "H", text: "Show this help", row: 0 } );
util.triggerEvent( document, "impress:help:add",
{ command: "H", text: "Show/hide this help", row: 0, short: true } );
util.triggerEvent( document, "impress:help:add",
{ command: "F1", text: "Show/hide full help", row: 999, short: true } );
} );

} )( document, window );
Expand Down Expand Up @@ -2555,7 +2598,7 @@

// Add 'P' to the help popup
triggerEvent( document, 'impress:help:add',
{ command: 'P', text: 'Presenter console', row: 10 } );
{ command: 'P', text: 'Presenter console', row: 10, short: true } );
} );

// Returns a string to be used inline as a css <style> element in the console window.
Expand Down Expand Up @@ -3297,7 +3340,8 @@
// Add a line to the help popup
util.triggerEvent( document, "impress:help:add", { command: "Left &amp; Right",
text: "Previous &amp; Next step",
row: 1 } );
row: 1,
short: true } );

}, false );

Expand Down
113 changes: 78 additions & 35 deletions src/plugins/help/help.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
/**
* Help popup plugin
*
* Example:
*
* <!-- Show a help popup at start, or if user presses "H" -->
* <div id="impress-help"></div>
*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to stay. Plugins that display something on the screen are required to be optional. One way to make them optional is that the presentation author must explicitly add an empty div for them to appear.

In the case of the help plugin, note that it is shown automatically when the presentation is loaded. Due to this, the div must stay.

* For developers:
*
* Typical use for this plugin, is for plugins that support some keypress, to add a line
Expand All @@ -21,46 +16,88 @@
var rows = [];
var timeoutHandle;

var triggerEvent = function( el, eventName, detail ) {
var event = document.createEvent( "CustomEvent" );
event.initCustomEvent( eventName, true, true, detail );
el.dispatchEvent( event );
var body = document.querySelector( "body" );

// This querySelector is used to keep compatibility with templates using div#impress-help
var helpDiv = document.querySelector( "#impress-help" ) || document.createElement( "div" );
body.appendChild( helpDiv );

var fullHelpDiv = document.createElement( "div" );
var fullHelpStyle = {
display: "none",
position: "fixed",
top: 0,
bottom: 0,
left: 0,
right: 0,
backgroundColor: "#000",
color: "#FFF",
opacity: 0.8,
textAlign: "center",
padding: "3em"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The philosophy of impress.js is that we don't want to force any colors or other styling for the user. So instead of setting the style here, you should set it in css/impress-demo.css. Unfortunately this means you also need to set this in every other css file under presentations. (Related: #681)

So what is left for the plugin to do? Maybe nothing? Do you need different CSS for the full help?

At most, you could set a class that indicates short help vs full help:

<div id="impress-help" class="help-short">
<div id="impress-help" class="help-full">

Presentation authors can then choose to use this class for styling if they want.

};

var renderHelpDiv = function() {
var helpDiv = document.getElementById( "impress-help" );
if ( helpDiv ) {
body.appendChild( fullHelpDiv );

var css = function( elem, style ) {
for ( var p in style ) {
elem.style[ p ] = style[ p ];
}
};
css( fullHelpDiv, fullHelpStyle );

var renderDiv = function( elem, shortOnly ) {
if ( elem ) {
var html = [];
for ( var row in rows ) {
for ( var arrayItem in row ) {
html.push( rows[ row ][ arrayItem ] );
var item = rows[ row ][ arrayItem ];
if ( item ) {
if ( ( shortOnly && item.short ) || !shortOnly ) {
html.push( item.row );
}
}
}
}
if ( html ) {
helpDiv.innerHTML = "<table>\n" + html.join( "\n" ) + "</table>\n";
elem.innerHTML = "<table style='width: 100%'>\n" + html.join( "\n" ) + "</table>\n";
}
}
};

var toggleHelp = function() {
var helpDiv = document.getElementById( "impress-help" );
if ( !helpDiv ) {
var renderHelpDiv = function() {
renderDiv( helpDiv, true );
renderDiv( fullHelpDiv, false );
};

var toggle = function( elem ) {
if ( !elem ) {
return;
}

if ( helpDiv.style.display === "block" ) {
helpDiv.style.display = "none";
if ( elem.style.display === "block" ) {
elem.style.display = "none";
} else {
helpDiv.style.display = "block";
window.clearTimeout( timeoutHandle );
elem.style.display = "block";
if ( elem === helpDiv ) {
window.clearTimeout( timeoutHandle );
} else {

// Hides helpDiv when fullHelpDiv is shown
helpDiv.style.display = "none";
window.clearTimeout( timeoutHandle );
}
}
};

document.addEventListener( "keyup", function( event ) {

if ( event.keyCode === 72 || event.keyCode === 191 ) { // "h" || "?"
event.preventDefault();
toggleHelp();
toggle( helpDiv );
} else if ( event.key === "F1" ) {
event.preventDefault();
toggle( fullHelpDiv );
}
}, false );

Expand All @@ -72,6 +109,7 @@
* :param: e.detail.command Example: "H"
* :param: e.detail.text Example: "Show this help."
* :param: e.detail.row Row index from 0 to 9 where to place this help text. Example: 0
* :param: e.detail.short If this help must appear in the short help. Example: true
*/
document.addEventListener( "impress:help:add", function( e ) {

Expand All @@ -80,39 +118,44 @@
// its own array. If there are more than one entry for the same index, they are shown in
// first come, first serve ordering.
var rowIndex = e.detail.row;
if ( typeof rows[ rowIndex ] !== "object" || !rows[ rowIndex ].isArray ) {
rows[ rowIndex ] = [];
}
rows[ e.detail.row ].push( "<tr><td><strong>" + e.detail.command + "</strong></td><td>" +
e.detail.text + "</td></tr>" );
var short = e.detail.short;
rows[ rowIndex ] = rows[ rowIndex ] || [];
rows[ e.detail.row ].push( {
row: "<tr><td><strong>" + e.detail.command + "</strong></td><td>" +
e.detail.text + "</td></tr>",
short: !!short
} );
renderHelpDiv();
} );

document.addEventListener( "impress:init", function( e ) {
var api = e.detail.api;
var gc = api.lib.gc;
var util = api.lib.util;

renderHelpDiv();

// At start, show the help for 7 seconds.
var helpDiv = document.getElementById( "impress-help" );
if ( helpDiv ) {
helpDiv.style.display = "block";
timeoutHandle = window.setTimeout( function() {
var helpDiv = document.getElementById( "impress-help" );
helpDiv.style.display = "none";
}, 7000 );

// Regster callback to empty the help div on teardown
var api = e.detail.api;
api.lib.gc.pushCallback( function() {
gc.pushCallback( function() {
window.clearTimeout( timeoutHandle );
helpDiv.style.display = "";
helpDiv.innerHTML = "";
helpDiv.remove();
fullHelpDiv.remove();
rows = [];
} );
}

// Use our own API to register the help text for "h"
triggerEvent( document, "impress:help:add",
{ command: "H", text: "Show this help", row: 0 } );
util.triggerEvent( document, "impress:help:add",
{ command: "H", text: "Show/hide this help", row: 0, short: true } );
util.triggerEvent( document, "impress:help:add",
{ command: "F1", text: "Show/hide full help", row: 999, short: true } );
} );

} )( document, window );
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/impressConsole/impressConsole.js
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@

// Add 'P' to the help popup
triggerEvent( document, 'impress:help:add',
{ command: 'P', text: 'Presenter console', row: 10 } );
{ command: 'P', text: 'Presenter console', row: 10, short: true } );
} );

// Returns a string to be used inline as a css <style> element in the console window.
Expand Down
Loading