Tab Code Box

TabCodeBox offers ability to switch between code samples using tabs.

Usage Example

The following example demonstrates how to create a TabCodeBox. Simply instantiate the TabCodeBox class by passing a reference to the root element of code box. You can read more about elements of code box here. You can also provide additional options as a second parameter.

<div id="MyTabCodeBox">
    <pre data-cb-name="addition" data-cb-active><code>let x = 1;
let y = 2;

console.log("result: " + (x + y));</code></pre>
    <pre data-cb-name="connecting strings"><code>let string1 = "This is";
let string2 = " string.";

console.log(string1 + string2);</code></pre>

    <div data-cb-name="Image.png" data-cb-file="./img/Image.png"></div>
</div>
import { TabCodeBox } from "@jirkasa/code-box";

new TabCodeBox(document.getElementById("MyTabCodeBox"), {
    svgSpritePath: "./img/icon-sprite.svg",
    svgSpriteIcons: {
        codeFile: "file",
        file: "file-2",
        download: "download"
    }
});
let x = 1;
let y = 2;

console.log("result: " + (x + y));
let string1 = "This is";
let string2 = " string.";

console.log(string1 + string2);

If you want to create multiple tab code boxes, you can use the TabCodeBoxCreator. For more information, refer to the chapter on creators.

Options

TabCodeBox accepts the following options and all options from the base CodeBox class.

...all options from CodeBox class
svgSpritePathSpecifies path to the SVG sprite that contains icons.
svgSpriteIconsSpecifies names of the icons in the SVG sprite.

svgSpritePath : string

Default value: undefined

Specifies path to the SVG sprite that contains icons.

svgSpriteIcons : object

Default value: {}

Specifies names of the icons in the SVG sprite in a form of object with the following properties:

  • codeFile - name of icon for code view tab button
  • file - name of icon for file tab button
  • download - name of icon that is displayed for tab button of downloadable file
// ...
svgSpriteIcons: {
    codeFile: "file",
    file: "file-2",
    download: "download"
}
// ...

Methods

TabCodeBox has the following methods. Most of these methods are also described in the chapter about the base CodeBox class. However, some of them have a return type that is a subclass of CodeBoxCodeView or CodeBoxFile - specifically, TabCodeBoxCodeView or TabCodeBoxFile. For clarity, these methods are also listed in this chapter.

appendToAppends code box to element.
detachDetaches code box from its parent element.
initInitializes code box if it hasn't been initialized yet.
isInitializedChecks whether the code box is initialized.
addOnInitListenerRegisteres function to be called when code box is initialized.
addCodeViewAdds new code view to code box.
getCodeViewsReturns all code views of code box.
getCodeViewReturns code view based on identifier.
removeCodeViewRemoves code view from code box.
removeAllCodeViewsRemoves all code views from code box.
changeCodeViewIdentifierChanges identifier of code view in code box.
getCodeViewButtonPositionReturns position of code view button.
setCodeViewButtonPositionChanges position of code view button.
setActiveCodeViewSets code view as active (displays it in code box).
setNoActiveCodeViewDisplays no code view in code box.
getActiveCodeViewReturns currently active code view.
addFileAdds new file to code box.
getFilesReturns all files of code box.
getFileReturns file based on identifier.
removeFileRemoves file from code box.
removeAllFilesRemoves all files from code box.
changeFileIdentifierChanges identifier of file in code box.
changeFileDownloadLinkChanges download link of file.
getFileButtonPositionReturns position of file button.
setFileButtonPositionChanges position of file button.
resetResets code box to its post-initialization state.
createMementoCreates memento.
applyMementoApplies memento.

appendTo

appendTo(element: HTMLElement): void

Appends code box to element.

Params

element : HTMLElementElement to append code box to.

detach

detach(): void

Detaches code box from its parent element.

init

init(): void

Initializes code box if it hasn't been initialized yet. When lazy initialization is disabled, the code box is initialized immediately after it is created.

isInitialized

isInitialized(): boolean

Checks whether the code box is initialized.

Returns

booleanIndicates whether the code box is initialized.

addOnInitListener

addOnInitListener(callback: () => void): void

Registeres function to be called when code box is initialized.

Params

callback : () => voidFunction to be called after initialization of code box.

addCodeView

addCodeView(identifier: string, codeView: CodeView): boolean

Adds new code view to code box (copy of passed code view is made).

Params

identifier : stringIdentifier under which the code view should be added to code box.
codeView : CodeViewCode view.

Returns

booleanIndicates whether code view has been successfully added.

getCodeViews

getCodeViews(): TabCodeBoxCodeView[]

Returns all code views of code box.

Returns

TabCodeBoxCodeView[]Code views.

getCodeView

getCodeView(identifier: string): TabCodeBoxCodeView | null

Returns code view based on identifier.

Params

identifier : stringIdentifier of code view.

Returns

TabCodeBoxCodeView | nullCode view or null if code view wasn't found.

removeCodeView

removeCodeView(identifier: string): boolean

Removes code view from code box.

Params

identifier : stringIdentifier of code view to be removed.

Returns

booleanIndicates whether code view has been removed.

removeAllCodeViews

removeAllCodeViews(): void

Removes all code views from code box.

changeCodeViewIdentifier

changeCodeViewIdentifier(identifier: string, newIdentifier: string): boolean

Changes identifier of code view in code box.

Params

identifier : stringIdentifier of code view whose identifier should be changed.
newIdentifier : stringNew identifier.

Returns

booleanIndicates whether change has been successfully completed (if passed new identifier already belongs to some other code view in code box, it returns false).

getCodeViewButtonPosition

getCodeViewButtonPosition(identifier: string): number | null

Returns position of code view button.

Params

identifier : stringIdentifier of code view.

Returns

number | nullPosition of code view button starting from 0 or null if code view does not exist.

setCodeViewButtonPosition

setCodeViewButtonPosition(identifier: string, position: number): boolean

Changes position of code view button by swapping it with different code view or file button.

Params

identifier : stringIdentifier of code view.
position : numberPosition of code view or file button (starting from 0) with which should be code view button swapped.

Returns

booleanIndicates whether the position has been successfully changed.

setActiveCodeView

setActiveCodeView(identifier: string): boolean

Sets code view as active (displays it in code box).

Params

identifier : stringIdentifier of code view which should be set as active.

Returns

booleanIndicates whether code view was found and has been successfully set as active.

setNoActiveCodeView

setNoActiveCodeView(): void

Displays no code view in code box.

getActiveCodeView

getActiveCodeView(): TabCodeBoxCodeView | null

Returns currently active code view.

Returns

TabCodeBoxCodeView | nullActive code view or null if no code view is set as active.

addFile

addFile(identifier: string, downloadLink: string | null): boolean

Adds new file to code box.

Params

identifier : stringIdentifier under which the file should be added to code box.
downloadLink : string | nullDownload link or null if file should not be downloadable.

Returns

booleanIndicates whether file has been successfully added.

getFiles

getFiles(): TabCodeBoxFile[]

Returns all files of code box.

Returns

getFile

getFile(identifier: string): TabCodeBoxFile | null

Returns file based on identifier.

Params

identifier : stringIdentifier of file.

Returns

TabCodeBoxFile | nullFile or null if file wasn't found.

removeFile

removeFile(identifier: string): boolean

Removes file from code box.

Params

identifier : stringIdentifier of file to be removed.

Returns

booleanIndicates whether file has been removed.

removeAllFiles

removeAllFiles(): void

Removes all files from code box.

changeFileIdentifier

changeFileIdentifier(identifier: string, newIdentifier: string): boolean

Changes identifier of file in code box.

Params

identifier : stringIndentifier of file whose identifier should be changed.
newIdentifier : stringNew identifier.

Returns

booleanIndicates whether change has been successfully completed (if passed new identifier already belongs to some other file in code box, it returns false).

changeFileDownloadLink(identifier: string, newDownloadLink: string | null): boolean

Changes download link of file.

Params

identifier : stringIdentifier of file whose download link should be changed.
newDownloadLink : string | nullDownload link (or null if file should not be downloadable).

Returns

booleanIndicates whether file was found and its download link has been successfully changed.

getFileButtonPosition

getFileButtonPosition(identifier: string): number | null

Returns position of file button.

Params

identifier : stringIdentifier of file.

Returns

number | nullPosition of file button starting from 0 or null if file does not exist.

setFileButtonPosition

setFileButtonPosition(identifier: string, position: number): boolean

Changes position of file button by swapping it with different code view or file button.

Params

identifier : stringIdentifier of file.
position : numberPosition of code view or file button (starting from 0) with which should be file button swapped.

Returns

booleanIndicates whether the position has been successfully changed.

reset

reset(): void

Resets code box to its post-initialization state.

createMemento

createMemento(): CodeBoxMemento

Creates memento (saved state of code box).

Returns

CodeBoxMementoMemento (saved state of code box).

applyMemento

applyMemento(memento: CodeBoxMemento): void

Applies memento (sets code box to the state defined by the provided memento).

Params

memento : CodeBoxMementoMemento (saved state of code box).