Skip to content

Commit

Permalink
Port widgets/help.js to ES6 class
Browse files Browse the repository at this point in the history
* adding classes to help.js

* Fix error due to mistimed asynchronous call

* Format code

Co-authored-by: Anindya Kundu <[email protected]>
  • Loading branch information
kushal-khare-official and meganindya authored Dec 10, 2020
1 parent 8c59919 commit 2b08fc1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 38 deletions.
9 changes: 4 additions & 5 deletions js/activity.js
Original file line number Diff line number Diff line change
Expand Up @@ -3979,17 +3979,16 @@ function Activity() {
* Shows help page
*/
_showHelp = function () {
let helpWidget = new HelpWidget();
helpWidget.init(null);
// Will show welcome page by default.
new HelpWidget(null);
};

/*
* Shows about page
*/
_showAboutPage = function () {
let helpWidget = new HelpWidget();
helpWidget.init(null);
helpWidget.showPageByName(_('About'));
// Will show welcome page by default.
new HelpWidget(null);
};

/*
Expand Down
3 changes: 1 addition & 2 deletions js/piemenus.js
Original file line number Diff line number Diff line change
Expand Up @@ -3218,8 +3218,7 @@ piemenuBlockContext = function(block) {
if (helpButton !== null) {
wheel.navItems[helpButton].navigateFunction = function() {
that.blocks.activeBlock = blockBlock;
let helpWidget = new HelpWidget();
helpWidget.init(blocks);
let helpWidget = new HelpWidget(blocks);
docById("contextWheelDiv").style.display = "none";
};
}
Expand Down
61 changes: 30 additions & 31 deletions js/widgets/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@

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

function HelpWidget() {
const ICONSIZE = 32;
let beginnerBlocks = [];
let advancedBlocks = [];
let appendedBlockList = [];
let index = 0;

this.init = (blocks) => {
const ICONSIZE = 32;

class HelpWidget {
constructor(blocks) {
this.beginnerBlocks = [];
this.advancedBlocks = [];
this.appendedBlockList = [];
this.index = 0;
this.isOpen = true;

let widgetWindow = window.widgetWindows.windowFor(this, "help", "help");
Expand All @@ -27,7 +27,6 @@ function HelpWidget() {
this.widgetWindow = widgetWindow;
widgetWindow.clear();
widgetWindow.show();

widgetWindow.onClose = () => {
this.isOpen = false;
this.destroy();
Expand All @@ -40,9 +39,9 @@ function HelpWidget() {

// Position center
setTimeout(this.widgetWindow.sendToCenter, 50);
};
}

this._setup = (blocks) => {
_setup(blocks) {
let iconSize = ICONSIZE;
// Which help page are we on?
let page = 0;
Expand Down Expand Up @@ -243,9 +242,9 @@ function HelpWidget() {
}

this.widgetWindow.takeFocus();
};
}

this._showPage = (page) => {
_showPage(page) {
let helpBody = docById("helpBodyDiv");
let body = "";
if (
Expand Down Expand Up @@ -292,18 +291,18 @@ function HelpWidget() {
helpBody.innerHTML = body;

this.widgetWindow.takeFocus();
};
}

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

this._prepareBlockList = (blocks) => {
_prepareBlockList(blocks) {
for (let key in blocks.protoBlockDict) {
if (
blocks.protoBlockDict[key].beginnerModeBlock === true &&
blocks.protoBlockDict[key].helpString !== undefined &&
blocks.protoBlockDict[key].helpString.length !== 0
) {
beginnerBlocks.push(key);
this.beginnerBlocks.push(key);
}
}

Expand All @@ -313,22 +312,22 @@ function HelpWidget() {
blocks.protoBlockDict[key].helpString !== undefined &&
blocks.protoBlockDict[key].helpString.length !== 0
) {
advancedBlocks.push(key);
this.advancedBlocks.push(key);
}
}

// Array containing list of all blocks (Beginner blocks first)

appendedBlockList.push(...beginnerBlocks);
appendedBlockList.push(...advancedBlocks);
this.appendedBlockList.push(...this.beginnerBlocks);
this.appendedBlockList.push(...this.advancedBlocks);

this._blockHelp(blocks.protoBlockDict[appendedBlockList[0]], blocks);
};
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)

this._blockHelp = (block, blocks) => {
_blockHelp(block, blocks) {
let iconSize = ICONSIZE;

let widgetWindow = window.widgetWindows.windowFor(this, "help", "help");
Expand All @@ -346,20 +345,20 @@ function HelpWidget() {
this.widgetWindow.sendToCenter();
let cell = docById("right-arrow");
cell.onclick = () => {
if (index !== appendedBlockList.length - 1) {
index += 1;
if (this.index !== this.appendedBlockList.length - 1) {
this.index += 1;
}
this._blockHelp(blocks.protoBlockDict[appendedBlockList[index]], blocks);
this._blockHelp(blocks.protoBlockDict[this.appendedBlockList[this.index]], blocks);
};

cell = docById("left-arrow");

cell.onclick = () => {
if (index !== 0) {
index -= 1;
if (this.index !== 0) {
this.index -= 1;
}

this._blockHelp(blocks.protoBlockDict[appendedBlockList[index]], blocks);
this._blockHelp(blocks.protoBlockDict[this.appendedBlockList[this.index]], blocks);
};
if (block.name !== null) {
let label = block.staticLabels[0];
Expand Down Expand Up @@ -496,13 +495,13 @@ function HelpWidget() {
}

this.widgetWindow.takeFocus();
};
}

this.showPageByName = (pageName) => {
showPageByName(pageName) {
for (let i = 0; i < HELPCONTENT.length; i++) {
if (HELPCONTENT[i].includes(pageName)) {
this._showPage(i);
}
}
};
}
}

0 comments on commit 2b08fc1

Please sign in to comment.