Skip to main content

Class: Backpack

Defined in: plugins/workspace-backpack/src/backpack.ts:27

Class for backpack that can be used save blocks from the workspace for future use.

Extends​

  • DragTarget

Implements​

  • IComponent
  • IContextMenu
  • IAutoHideable
  • IPositionable
  • IFocusableNode

Properties index​

PropertyDescription
contents_A list of JSON (stored as strings) representing blocks in the backpack.
flyout_The backpack flyout. Initialized during init.
HEIGHT_Height of the backpack in pixels. Used for clip path.
HOTSPOT_MARGIN_Extent of hotspot on all sides beyond the size of the image in pixels.
idThe unique id for this component.
initialized_Whether or not the plugin has been initialized.
left_Left coordinate of the backpack in pixels.
MARGIN_HORIZONTAL_Distance between backpack and right/left edge of workspace in pixels.
MARGIN_VERTICAL_Distance between backpack and bottom/top edge of workspace in pixels.
svgGroup_The SVG group containing the backpack.
svgImg_The SVG image of the backpack.
top_Top coordinate of the backpack in pixels.
WIDTH_Width of the backpack in pixels. Used for clip path.
workspace_The target workspace that the backpack will be added to.

Methods index​

MethodDescription
addBackpackable-
addBackpackables-
addBlockAdds the specified block to backpack.
addBlocksAdds the provided blocks to backpack.
addItemAdds item to backpack.
addItemsAdds multiple items to the backpack.
attachListenersAttaches event listeners.
autoHideHides the component. Called in Blockly.hideChaff.
blockMouseDownWhenOpenablePrevents a workspace scroll and click event if the backpack is openable.
canBeFocusedIndicates whether this node allows focus. If this returns false then none of the other IFocusableNode methods will be called.
closeCloses the backpack flyout.
containsBackpackableReturns whether the backpack contains a duplicate of the provided backpackable.
containsBlockReturns whether the backpack contains a duplicate of the provided Block.
createDomCreates DOM for UI element.
disposeDisposes of workspace search. Unlink from all DOM elements and remove all event listeners to prevent memory leaks.
emptyEmpties the backpack's contents. If the contents-flyout is currently open it will be closed.
getBoundingRectangleReturns the bounding rectangle of the UI element in pixel units relative to the Blockly injection div.
getClientRectReturns the bounding rectangle of the drag target area in pixel units relative to viewport.
getContentsReturns backpack contents.
getCountReturns the count of items in the backpack.
getFlyoutReturns the backpack flyout.
getFocusableElementReturns the DOM element that can be explicitly requested to receive focus.
getFocusableTreeReturns the closest parent tree of this node (in cases where a tree has distinct trees underneath it), which represents the tree to which this node belongs.
initInitializes the backpack.
initFlyoutCreates and initializes the flyout and inserts it into the dom.
isOpenReturns whether the backpack is open.
isOpenableReturns whether the backpack is open-able.
maybeRefreshFlyoutContentsRefreshes backpack flyout contents if the flyout is open.
onClickHandle click event.
onContentChangeHandles content change.
onDragEnterHandles when a cursor with a block or bubble enters this drag target.
onDragExitHandles when a cursor with a block or bubble exits this drag target.
onDragOverHandles when a cursor with a block or bubble is dragged over this drag target.
onDropHandles when a block or bubble is dropped on this component. Should not handle delete here.
onNodeBlurCalled when this node loses active focus. It may still have passive focus.
onNodeFocusCalled when this node receives active focus.
openOpens the backpack flyout.
performActionOptional method invoked when this node has focus and the user acts on it by pressing Enter or Space. Behavior should generally be similar to the node being clicked on.
positionPositions the backpack. It is positioned in the opposite corner to the corner the categories/toolbox starts at.
removeBackpackable-
removeBlockRemoves the specified block from the backpack.
removeItemRemoves item from the backpack.
setContentsSets backpack contents.
shouldPreventMoveReturns whether the provided block or bubble should not be moved after being dropped on this component. If true, the element will return to where it was when the drag started.
showContextMenuShow the context menu for the backpack.
updateHoverStyingAdds or removes styling to darken the backpack to show it is interactable.

Constructors​

Constructor​

new Backpack(workspace_, backpackOptions?): Backpack;

Defined in: plugins/workspace-backpack/src/backpack.ts:102

Constructor for a backpack.

Parameters​

ParameterTypeDescription
workspace_WorkspaceSvgThe target workspace that the backpack will be added to.
backpackOptions?BackpackOptionsThe backpack options to use.

Returns​

Backpack

Overrides​

Blockly.DragTarget.constructor

Properties​

contents_​

protected contents_: string[] = [];

Defined in: plugins/workspace-backpack/src/backpack.ts:51

A list of JSON (stored as strings) representing blocks in the backpack.


flyout_​

protected flyout_: IFlyout | null = null;

Defined in: plugins/workspace-backpack/src/backpack.ts:48

The backpack flyout. Initialized during init.


HEIGHT_​

protected readonly HEIGHT_: 60 = 60;

Defined in: plugins/workspace-backpack/src/backpack.ts:66

Height of the backpack in pixels. Used for clip path.


HOTSPOT_MARGIN_​

protected readonly HOTSPOT_MARGIN_: 10 = 10;

Defined in: plugins/workspace-backpack/src/backpack.ts:75

Extent of hotspot on all sides beyond the size of the image in pixels.


id​

id: string = 'backpack';

Defined in: plugins/workspace-backpack/src/backpack.ts:38

The unique id for this component.

Implementation of​

Blockly.IComponent.id

Overrides​

Blockly.DragTarget.id


initialized_​

protected initialized_: boolean = false;

Defined in: plugins/workspace-backpack/src/backpack.ts:93

Whether or not the plugin has been initialized.


left_​

protected left_: number = 0;

Defined in: plugins/workspace-backpack/src/backpack.ts:57

Left coordinate of the backpack in pixels.


MARGIN_HORIZONTAL_​

protected readonly MARGIN_HORIZONTAL_: 20 = 20;

Defined in: plugins/workspace-backpack/src/backpack.ts:72

Distance between backpack and right/left edge of workspace in pixels.


MARGIN_VERTICAL_​

protected readonly MARGIN_VERTICAL_: 20 = 20;

Defined in: plugins/workspace-backpack/src/backpack.ts:69

Distance between backpack and bottom/top edge of workspace in pixels.


svgGroup_​

protected svgGroup_: SVGElement | null = null;

Defined in: plugins/workspace-backpack/src/backpack.ts:78

The SVG group containing the backpack.


svgImg_​

protected svgImg_: SVGImageElement | null = null;

Defined in: plugins/workspace-backpack/src/backpack.ts:81

The SVG image of the backpack.


top_​

protected top_: number = 0;

Defined in: plugins/workspace-backpack/src/backpack.ts:60

Top coordinate of the backpack in pixels.


WIDTH_​

protected readonly WIDTH_: 40 = 40;

Defined in: plugins/workspace-backpack/src/backpack.ts:63

Width of the backpack in pixels. Used for clip path.


workspace_​

protected workspace_: WorkspaceSvg;

Defined in: plugins/workspace-backpack/src/backpack.ts:103

The target workspace that the backpack will be added to.

Methods​

addBackpackable()​

addBackpackable(backpackable): void;

Defined in: plugins/workspace-backpack/src/backpack.ts:650

Parameters​

ParameterTypeDescription
backpackableBackpackableThe backpackable to add to the backpack.

Returns​

void


addBackpackables()​

addBackpackables(backpackables): void;

Defined in: plugins/workspace-backpack/src/backpack.ts:655

Parameters​

ParameterTypeDescription
backpackablesBackpackable[]The backpackables to add to the backpack.

Returns​

void


addBlock()​

addBlock(block): void;

Defined in: plugins/workspace-backpack/src/backpack.ts:598

Adds the specified block to backpack.

Parameters​

ParameterTypeDescription
blockBlockThe block to be added to the backpack.

Returns​

void


addBlocks()​

addBlocks(blocks): void;

Defined in: plugins/workspace-backpack/src/backpack.ts:612

Adds the provided blocks to backpack.

Parameters​

ParameterTypeDescription
blocksBlock[]The blocks to be added to the backpack.

Returns​

void


addItem()​

addItem(item): void;

Defined in: plugins/workspace-backpack/src/backpack.ts:680

Adds item to backpack.

Parameters​

ParameterTypeDescription
itemstringText representing the JSON of a block to add, cleaned of all unnecessary attributes.

Returns​

void


addItems()​

addItems(items): void;

Defined in: plugins/workspace-backpack/src/backpack.ts:689

Adds multiple items to the backpack.

Parameters​

ParameterTypeDescription
itemsstring[]The backpack contents to add.

Returns​

void


attachListeners()​

protected attachListeners(): void;

Defined in: plugins/workspace-backpack/src/backpack.ts:301

Attaches event listeners.

Returns​

void


autoHide()​

autoHide(onlyClosePopups): void;

Defined in: plugins/workspace-backpack/src/backpack.ts:861

Hides the component. Called in Blockly.hideChaff.

Parameters​

ParameterTypeDescription
onlyClosePopupsbooleanWhether only popups should be closed. Flyouts should not be closed if this is true.

Returns​

void

Implementation of​

Blockly.IAutoHideable.autoHide


blockMouseDownWhenOpenable()​

protected blockMouseDownWhenOpenable(e): void;

Defined in: plugins/workspace-backpack/src/backpack.ts:960

Prevents a workspace scroll and click event if the backpack is openable.

Parameters​

ParameterTypeDescription
eEventA mouse down event.

Returns​

void


canBeFocused()​

canBeFocused(): boolean;

Defined in: plugins/workspace-backpack/src/backpack.ts:990

Indicates whether this node allows focus. If this returns false then none of the other IFocusableNode methods will be called.

Note that special care must be taken if implementations of this function dynamically change their return value value over the lifetime of the node as certain environment conditions could affect the focusability of this node's DOM element (such as whether the element has a positive or zero tabindex). Also, changing from a true to a false value while the node holds focus will not immediately change the current focus of the node nor FocusManager's internal state, and thus may result in some of the node's functions being called later on when defocused (since it was previously considered focusable at the time of being focused).

Implementations should generally always return true here unless there are circumstances under which this node should be skipped for focus considerations. Examples may include being disabled, read-only, a purely visual decoration, or a node with no visual representation that must implement this interface (e.g. due to a parent interface extending it). Keep in mind accessibility best practices when determining whether a node should be focusable since even disabled and read-only elements are still often relevant to providing organizational context to users (particularly when using a screen reader).

Returns​

boolean

Whether this node can be focused by FocusManager.

Implementation of​

Blockly.IFocusableNode.canBeFocused


close()​

close(): void;

Defined in: plugins/workspace-backpack/src/backpack.ts:840

Closes the backpack flyout.

Returns​

void


containsBackpackable()​

containsBackpackable(backpackable): boolean;

Defined in: plugins/workspace-backpack/src/backpack.ts:641

Parameters​

ParameterTypeDescription
backpackableBackpackableThe backpackable we want to check for existence within the backpack.

Returns​

boolean

whether the backpack contains a duplicate of the provided backpackable.


containsBlock()​

containsBlock(block): boolean;

Defined in: plugins/workspace-backpack/src/backpack.ts:585

Returns whether the backpack contains a duplicate of the provided Block.

Parameters​

ParameterTypeDescription
blockBlockThe block to check.

Returns​

boolean

Whether the backpack contains a duplicate of the provided block.


createDom()​

protected createDom(): void;

Defined in: plugins/workspace-backpack/src/backpack.ts:228

Creates DOM for UI element.

Returns​

void


dispose()​

dispose(): void;

Defined in: plugins/workspace-backpack/src/backpack.ts:161

Disposes of workspace search. Unlink from all DOM elements and remove all event listeners to prevent memory leaks.

Returns​

void


empty()​

empty(): void;

Defined in: plugins/workspace-backpack/src/backpack.ts:734

Empties the backpack's contents. If the contents-flyout is currently open it will be closed.

Returns​

void


getBoundingRectangle()​

getBoundingRectangle(): Rect;

Defined in: plugins/workspace-backpack/src/backpack.ts:367

Returns the bounding rectangle of the UI element in pixel units relative to the Blockly injection div.

Returns​

Rect

The component’s bounding box.

Implementation of​

Blockly.IPositionable.getBoundingRectangle


getClientRect()​

getClientRect(): Rect | null;

Defined in: plugins/workspace-backpack/src/backpack.ts:348

Returns the bounding rectangle of the drag target area in pixel units relative to viewport.

Returns​

Rect | null

The component's bounding box. Null if drag target area should be ignored.

Overrides​

Blockly.DragTarget.getClientRect


getContents()​

getContents(): string[];

Defined in: plugins/workspace-backpack/src/backpack.ts:479

Returns backpack contents.

Returns​

string[]

The backpack contents.


getCount()​

getCount(): number;

Defined in: plugins/workspace-backpack/src/backpack.ts:470

Returns the count of items in the backpack.

Returns​

number

The count of items.


getFlyout()​

getFlyout(): IFlyout | null;

Defined in: plugins/workspace-backpack/src/backpack.ts:337

Returns the backpack flyout.

Returns​

IFlyout | null

The backpack flyout.


getFocusableElement()​

getFocusableElement(): SVGElement | HTMLElement;

Defined in: plugins/workspace-backpack/src/backpack.ts:974

Returns the DOM element that can be explicitly requested to receive focus.

IMPORTANT: Please note that this element is expected to have a visual presence on the page as it will both be explicitly focused and have its style changed depending on its current focus state (i.e. blurred, actively focused, and passively focused). The element will have one of two styles attached (where no style indicates blurred/not focused):

  • blocklyActiveFocus
  • blocklyPassiveFocus

The returned element must also have a valid ID specified, and this ID should be unique across the entire page. Failing to have a properly unique ID could result in trying to focus one node (such as via a mouse click) leading to another node with the same ID actually becoming focused by FocusManager.

The returned element must be visible if the node is ever focused via FocusManager.focusNode() or FocusManager.focusTree(). It's allowed for an element to be hidden until onNodeFocus() is called, or become hidden with a call to onNodeBlur().

It's expected the actual returned element will not change for the lifetime of the node (that is, its properties can change but a new element should never be returned). Also, the returned element will have its tabindex overwritten throughout the lifecycle of this node and FocusManager.

If a node requires the ability to be focused directly without first being focused via FocusManager then it must set its own tab index.

Returns​

SVGElement | HTMLElement

The HTMLElement or SVGElement which can both receive focus and be visually represented as actively or passively focused for this node.

Implementation of​

Blockly.IFocusableNode.getFocusableElement


getFocusableTree()​

getFocusableTree(): IFocusableTree;

Defined in: plugins/workspace-backpack/src/backpack.ts:982

Returns the closest parent tree of this node (in cases where a tree has distinct trees underneath it), which represents the tree to which this node belongs.

Returns​

IFocusableTree

The node's IFocusableTree.

Implementation of​

Blockly.IFocusableNode.getFocusableTree


init()​

init(): void;

Defined in: plugins/workspace-backpack/src/backpack.ts:135

Initializes the backpack.

Returns​

void


initFlyout()​

protected initFlyout(): void;

Defined in: plugins/workspace-backpack/src/backpack.ts:174

Creates and initializes the flyout and inserts it into the dom.

Returns​

void


isOpen()​

isOpen(): boolean;

Defined in: plugins/workspace-backpack/src/backpack.ts:797

Returns whether the backpack is open.

Returns​

boolean

Whether the backpack is open.


isOpenable()​

protected isOpenable(): boolean;

Defined in: plugins/workspace-backpack/src/backpack.ts:786

Returns whether the backpack is open-able.

Returns​

boolean

Whether the backpack is open-able.


maybeRefreshFlyoutContents()​

protected maybeRefreshFlyoutContents(): void;

Defined in: plugins/workspace-backpack/src/backpack.ts:829

Refreshes backpack flyout contents if the flyout is open.

Returns​

void


onClick()​

protected onClick(e?): void;

Defined in: plugins/workspace-backpack/src/backpack.ts:874

Handle click event.

Parameters​

ParameterTypeDescription
e?EventMouse event.

Returns​

void


onContentChange()​

protected onContentChange(): void;

Defined in: plugins/workspace-backpack/src/backpack.ts:748

Handles content change.

Returns​

void


onDragEnter()​

onDragEnter(dragElement): void;

Defined in: plugins/workspace-backpack/src/backpack.ts:893

Handles when a cursor with a block or bubble enters this drag target.

Parameters​

ParameterTypeDescription
dragElementIDraggableThe block or bubble currently being dragged.

Returns​

void

Overrides​

Blockly.DragTarget.onDragEnter


onDragExit()​

onDragExit(dragElement): void;

Defined in: plugins/workspace-backpack/src/backpack.ts:905

Handles when a cursor with a block or bubble exits this drag target.

Parameters​

ParameterTypeDescription
dragElementIDraggableThe block or bubble currently being dragged.

Returns​

void

Overrides​

Blockly.DragTarget.onDragExit


onDragOver()​

onDragOver(_dragElement): void;

Handles when a cursor with a block or bubble is dragged over this drag target.

Parameters​

ParameterTypeDescription
_dragElementIDraggableThe block or bubble currently being dragged.

Returns​

void

Inherited from​

Blockly.DragTarget.onDragOver


onDrop()​

onDrop(dragElement): void;

Defined in: plugins/workspace-backpack/src/backpack.ts:491

Handles when a block or bubble is dropped on this component. Should not handle delete here.

Parameters​

ParameterTypeDescription
dragElementIDraggableThe block or bubble currently being dragged.

Returns​

void

Overrides​

Blockly.DragTarget.onDrop


onNodeBlur()​

onNodeBlur(): void;

Defined in: plugins/workspace-backpack/src/backpack.ts:988

Called when this node loses active focus. It may still have passive focus.

This has the same implementation restrictions as onNodeFocus().

Returns​

void

Implementation of​

Blockly.IFocusableNode.onNodeBlur


onNodeFocus()​

onNodeFocus(): void;

Defined in: plugins/workspace-backpack/src/backpack.ts:986

Called when this node receives active focus.

Note that it's fine for implementations to change visibility modifiers, but they should avoid the following:

  • Creating or removing DOM elements (including via the renderer or drawer).
  • Affecting focus via DOM focus() calls or the FocusManager.

Implementations may consider scrolling themselves into view here; that is not handled by the focus manager.

Returns​

void

Implementation of​

Blockly.IFocusableNode.onNodeFocus


open()​

open(): void;

Defined in: plugins/workspace-backpack/src/backpack.ts:804

Opens the backpack flyout.

Returns​

void


performAction()​

performAction(e?): void;

Defined in: plugins/workspace-backpack/src/backpack.ts:994

Optional method invoked when this node has focus and the user acts on it by pressing Enter or Space. Behavior should generally be similar to the node being clicked on.

Parameters​

ParameterTypeDescription
e?EventThe event that triggered this action, if any.

Returns​

void

Implementation of​

Blockly.IFocusableNode.performAction


position()​

position(metrics, savedPositions): void;

Defined in: plugins/workspace-backpack/src/backpack.ts:385

Positions the backpack. It is positioned in the opposite corner to the corner the categories/toolbox starts at.

Parameters​

ParameterTypeDescription
metricsUiMetricsThe workspace metrics.
savedPositionsRect[]List of rectangles that are already on the workspace.

Returns​

void

Implementation of​

Blockly.IPositionable.position


removeBackpackable()​

removeBackpackable(backpackable): void;

Defined in: plugins/workspace-backpack/src/backpack.ts:668

Parameters​

ParameterTypeDescription
backpackableBackpackableThe backpackable to remove from the backpack.

Returns​

void


removeBlock()​

removeBlock(block): void;

Defined in: plugins/workspace-backpack/src/backpack.ts:627

Removes the specified block from the backpack.

Parameters​

ParameterTypeDescription
blockBlockThe block to be removed from the backpack.

Returns​

void


removeItem()​

removeItem(item): void;

Defined in: plugins/workspace-backpack/src/backpack.ts:703

Removes item from the backpack.

Parameters​

ParameterTypeDescription
itemstringText representing the JSON of a block to remove, cleaned of all unnecessary attributes.

Returns​

void


setContents()​

setContents(contents): void;

Defined in: plugins/workspace-backpack/src/backpack.ts:716

Sets backpack contents.

Parameters​

ParameterTypeDescription
contentsstring[]The new backpack contents.

Returns​

void


shouldPreventMove()​

shouldPreventMove(dragElement): boolean;

Defined in: plugins/workspace-backpack/src/backpack.ts:951

Returns whether the provided block or bubble should not be moved after being dropped on this component. If true, the element will return to where it was when the drag started.

Parameters​

ParameterTypeDescription
dragElementIDraggableThe block or bubble currently being dragged.

Returns​

boolean

Whether the block or bubble provided should be returned to drag start.

Overrides​

Blockly.DragTarget.shouldPreventMove


showContextMenu()​

showContextMenu(e): void;

Defined in: plugins/workspace-backpack/src/backpack.ts:1003

Show the context menu for the backpack.

Parameters​

ParameterTypeDescription
eEventEvent that triggered the display of the context menu.

Returns​

void

Implementation of​

Blockly.IContextMenu.showContextMenu


updateHoverStying()​

protected updateHoverStying(addClass): void;

Defined in: plugins/workspace-backpack/src/backpack.ts:930

Adds or removes styling to darken the backpack to show it is interactable.

Parameters​

ParameterTypeDescription
addClassbooleanTrue to add styling, false to remove.

Returns​

void