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

Refactor: widgets/help - lint, prettify and documentation #2768

Merged
merged 4 commits into from
Jan 25, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"es6": true,
"browser": true
},
"parser": "babel-eslint",
"extends": ["eslint:recommended", "prettier"],
"rules": {
"no-console": "warn",
Expand Down
154 changes: 97 additions & 57 deletions js/widgets/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,35 @@

// This widget displays help about a block or a button.

/*global _, docById, getMacroExpansion, HELPCONTENT*/

/*
Globals locations

- js/utils/utils.js
_, docById

- js/macros.js
getMacroExpansion

- js/turtledefs.js
HELPCONTENT
*/

class HelpWidget {
static ICONSIZE = 32;

/**
* @param {Blocks} blocks
*/
constructor(blocks) {
this.beginnerBlocks = [];
this.advancedBlocks = [];
this.appendedBlockList = [];
this.index = 0;
this.isOpen = true;

let widgetWindow = window.widgetWindows.windowFor(this, "help", "help");
const widgetWindow = window.widgetWindows.windowFor(this, "help", "help");
widgetWindow.getWidgetBody().style.overflowY = "auto";
// const canvasHeight = docById("myCanvas").getBoundingClientRect().height;
widgetWindow.getWidgetBody().style.maxHeight = "500px";
Expand All @@ -41,8 +59,13 @@ class HelpWidget {
setTimeout(this.widgetWindow.sendToCenter, 50);
}

/**
* @private
* @param {Blocks} blocks
* @returns {void}
*/
_setup(blocks) {
let iconSize = HelpWidget.ICONSIZE;
const iconSize = HelpWidget.ICONSIZE;
// Which help page are we on?
let page = 0;

Expand Down Expand Up @@ -89,7 +112,7 @@ class HelpWidget {
};
} else {
if (blocks.activeBlock.name !== null) {
let label = blocks.blockList[blocks.activeBlock].protoblock.staticLabels[0];
const label = blocks.blockList[blocks.activeBlock].protoblock.staticLabels[0];
this.widgetWindow.updateTitle(_(label));
}

Expand All @@ -110,9 +133,9 @@ class HelpWidget {
} else {
// display help for this block
if (blocks.activeBlock.name !== null) {
let name = blocks.blockList[blocks.activeBlock].name;
const name = blocks.blockList[blocks.activeBlock].name;

let advIcon =
const advIcon =
'<a\
class="tooltipped"\
data-toggle="tooltip"\
Expand All @@ -125,7 +148,7 @@ class HelpWidget {
></a\
>';

let findIcon =
const findIcon =
'<a\
class="tooltipped"\
data-toggle="tooltip"\
Expand All @@ -139,16 +162,14 @@ class HelpWidget {
></a\
>';

let showPaletteParamater =
blocks.blockList[blocks.activeBlock].protoblock.palette.name;
// Each block's help entry contains a help string, the
// path of the help svg, an override name for the help
// svg file, and an optional macro name for generating
// the help output.
let message = blocks.blockList[blocks.activeBlock].protoblock.helpString;
const message = blocks.blockList[blocks.activeBlock].protoblock.helpString;

if (message) {
let helpBody = docById("helpBodyDiv");
const helpBody = docById("helpBodyDiv");
helpBody.style.height = "";

let body = "";
Expand Down Expand Up @@ -195,20 +216,23 @@ class HelpWidget {
helpBody.innerHTML += advIcon;
}

let object = blocks.palettes.getProtoNameAndPalette(name);
const object = blocks.palettes.getProtoNameAndPalette(name);

let loadButton = docById("loadButton");
const loadButton = docById("loadButton");
if (loadButton !== null) {
loadButton.onclick = () => {
if (message.length < 4) {
// If there is nothing specified, just load the block.
console.debug("CLICK: " + name);
// console.debug("CLICK: " + name);

let protoblk = object[0];
let paletteName = object[1];
let protoName = object[2];
const protoblk = object[0];
const paletteName = object[1];
const protoName = object[2];

let protoResult = blocks.protoBlockDict.hasOwnProperty(protoName);
const protoResult = Object.prototype.hasOwnProperty.call(
blocks.protoBlockDict,
protoName
);
if (protoResult) {
blocks.palettes.dict[paletteName].makeBlockFromSearch(
protoblk,
Expand All @@ -221,18 +245,18 @@ class HelpWidget {
} else if (typeof message[3] === "string") {
// If it is a string, load the macro
// assocuated with this block
let blocksToLoad = getMacroExpansion(message[3], 100, 100);
console.debug("CLICK: " + blocksToLoad);
const blocksToLoad = getMacroExpansion(message[3], 100, 100);
// console.debug("CLICK: " + blocksToLoad);
blocks.loadNewBlocks(blocksToLoad);
} else {
// Load the blocks.
let blocksToLoad = message[3];
console.debug("CLICK: " + blocksToLoad);
const blocksToLoad = message[3];
// console.debug("CLICK: " + blocksToLoad);
blocks.loadNewBlocks(blocksToLoad);
}
};
}
let findIconMethod = docById("findIcon");
const findIconMethod = docById("findIcon");

findIconMethod.onclick = () => {
blocks.palettes.showPalette(object[1]);
Expand All @@ -244,8 +268,13 @@ class HelpWidget {
this.widgetWindow.takeFocus();
}

/**
* @private
* @param {number} page
* @returns {void}
*/
_showPage(page) {
let helpBody = docById("helpBodyDiv");
const helpBody = docById("helpBodyDiv");
let body = "";
if (
[
Expand All @@ -268,8 +297,8 @@ class HelpWidget {
body = body + "<p>" + HELPCONTENT[page][1] + "</p>";

if (HELPCONTENT[page].length > 3) {
let link = HELPCONTENT[page][3];
console.debug(page + " " + link);
const link = HELPCONTENT[page][3];
// console.debug(page + " " + link);
body =
body +
'<p><a href="' +
Expand All @@ -280,7 +309,7 @@ class HelpWidget {
}

if ([_("Congratulations.")].indexOf(HELPCONTENT[page][0]) !== -1) {
let cell = docById("right-arrow");
const cell = docById("right-arrow");

cell.onclick = () => {
this._prepareBlockList(blocks);
Expand All @@ -293,10 +322,14 @@ class HelpWidget {
this.widgetWindow.takeFocus();
}

// Prepare a list of beginner and advanced blocks and cycle through their help

/**
* Prepare a list of beginner and advanced blocks and cycle through their help
* @private
* @param {Blocks} blocks
* @returns {void}
*/
_prepareBlockList(blocks) {
for (let key in blocks.protoBlockDict) {
for (const key in blocks.protoBlockDict) {
if (
blocks.protoBlockDict[key].beginnerModeBlock === true &&
blocks.protoBlockDict[key].helpString !== undefined &&
Expand All @@ -306,7 +339,7 @@ class HelpWidget {
}
}

for (let key in blocks.protoBlockDict) {
for (const key in blocks.protoBlockDict) {
if (
blocks.protoBlockDict[key].beginnerModeBlock === false &&
blocks.protoBlockDict[key].helpString !== undefined &&
Expand All @@ -324,11 +357,17 @@ class HelpWidget {
this._blockHelp(blocks.protoBlockDict[this.appendedBlockList[0]], blocks);
}

// Function to display help related to a single block
// called recursively to cycle through help string of all blocks (Beginner Blocks First)

/**
* Function to display help related to a single block
* called recursively to cycle through help string of all blocks (Beginner Blocks First)
* @private
* @param {ProtoBlock} block
* @param {Blocks} blocks
* @returns {void}
*/
_blockHelp(block, blocks) {
let widgetWindow = window.widgetWindows.windowFor(this, "help", "help");
const widgetWindow = window.widgetWindows.windowFor(this, "help", "help");
this.widgetWindow = widgetWindow;
widgetWindow.clear();
this._helpDiv = document.createElement("div");
Expand Down Expand Up @@ -359,18 +398,13 @@ class HelpWidget {
this._blockHelp(blocks.protoBlockDict[this.appendedBlockList[this.index]], blocks);
};
if (block.name !== null) {
let label = block.staticLabels[0];
const label = block.staticLabels[0];
this.widgetWindow.updateTitle(_(label));
}

// display help menu
// docById("helpBodyDiv").style.height = "325px";
// docById("helpBodyDiv").style.width = "400px";
// this._showPage(0);

if (block.name !== null) {
let name = block.name;
let advIcon =
const name = block.name;
const advIcon =
'<a\
class="tooltipped"\
data-toggle="tooltip"\
Expand All @@ -383,7 +417,7 @@ class HelpWidget {
></a\
>';

let findIcon =
const findIcon =
'<a\
class="tooltipped"\
data-toggle="tooltip"\
Expand All @@ -397,9 +431,9 @@ class HelpWidget {
></a\
>';

let message = block.helpString;
const message = block.helpString;

let helpBody = docById("helpBodyDiv");
const helpBody = docById("helpBodyDiv");
helpBody.style.height = "500px";
helpBody.style.backgroundColor = "#e8e8e8";
if (message) {
Expand Down Expand Up @@ -447,25 +481,28 @@ class HelpWidget {
helpBody.innerHTML += advIcon;
}

let findIconMethod = docById("findIcon");
const findIconMethod = docById("findIcon");

findIconMethod.onclick = () => {
block.palette.palettes.showPalette(block.palette.name);
};

let loadButton = docById("loadButton");
const loadButton = docById("loadButton");
if (loadButton !== null) {
loadButton.onclick = () => {
if (message.length < 4) {
// If there is nothing specified, just
// load the block.
console.debug("CLICK: " + name);
let obj = blocks.palettes.getProtoNameAndPalette(name);
let protoblk = obj[0];
let paletteName = obj[1];
let protoName = obj[2];

let protoResult = blocks.protoBlockDict.hasOwnProperty(protoName);
// console.debug("CLICK: " + name);
const obj = blocks.palettes.getProtoNameAndPalette(name);
const protoblk = obj[0];
const paletteName = obj[1];
const protoName = obj[2];

const protoResult = Object.prototype.hasOwnProperty.call(
blocks.protoBlockDict,
protoName
);
if (protoResult) {
blocks.palettes.dict[paletteName].makeBlockFromSearch(
protoblk,
Expand All @@ -478,13 +515,13 @@ class HelpWidget {
} else if (typeof message[3] === "string") {
// If it is a string, load the macro
// assocuated with this block
let blocksToLoad = getMacroExpansion(message[3], 100, 100);
console.debug("CLICK: " + blocksToLoad);
const blocksToLoad = getMacroExpansion(message[3], 100, 100);
// console.debug("CLICK: " + blocksToLoad);
blocks.loadNewBlocks(blocksToLoad);
} else {
// Load the blocks.
let blocksToLoad = message[3];
console.debug("CLICK: " + blocksToLoad);
const blocksToLoad = message[3];
// console.debug("CLICK: " + blocksToLoad);
blocks.loadNewBlocks(blocksToLoad);
}
};
Expand All @@ -495,6 +532,9 @@ class HelpWidget {
this.widgetWindow.takeFocus();
}

/**
* @deprecated
*/
showPageByName(pageName) {
for (let i = 0; i < HELPCONTENT.length; i++) {
if (HELPCONTENT[i].includes(pageName)) {
Expand Down