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 | |
---|---|
svgSpritePath | Specifies path to the SVG sprite that contains icons. |
svgSpriteIcons | Specifies 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.
appendTo | Appends code box to element. |
---|---|
detach | Detaches code box from its parent element. |
init | Initializes code box if it hasn't been initialized yet. |
isInitialized | Checks whether the code box is initialized. |
addOnInitListener | Registeres function to be called when code box is initialized. |
addCodeView | Adds new code view to code box. |
getCodeViews | Returns all code views of code box. |
getCodeView | Returns code view based on identifier. |
removeCodeView | Removes code view from code box. |
removeAllCodeViews | Removes all code views from code box. |
changeCodeViewIdentifier | Changes identifier of code view in code box. |
getCodeViewButtonPosition | Returns position of code view button. |
setCodeViewButtonPosition | Changes position of code view button. |
setActiveCodeView | Sets code view as active (displays it in code box). |
setNoActiveCodeView | Displays no code view in code box. |
getActiveCodeView | Returns currently active code view. |
addFile | Adds new file to code box. |
getFiles | Returns all files of code box. |
getFile | Returns file based on identifier. |
removeFile | Removes file from code box. |
removeAllFiles | Removes all files from code box. |
changeFileIdentifier | Changes identifier of file in code box. |
changeFileDownloadLink | Changes download link of file. |
getFileButtonPosition | Returns position of file button. |
setFileButtonPosition | Changes position of file button. |
reset | Resets code box to its post-initialization state. |
createMemento | Creates memento. |
applyMemento | Applies memento. |
appendTo
appendTo(element: HTMLElement): void
Appends code box to element.
Params
element : HTMLElement | Element 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
boolean | Indicates whether the code box is initialized. |
---|
addOnInitListener
addOnInitListener(callback: () => void): void
Registeres function to be called when code box is initialized.
Params
callback : () => void | Function 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 : string | Identifier under which the code view should be added to code box. |
---|---|
codeView : CodeView | Code view. |
Returns
boolean | Indicates 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 : string | Identifier of code view. |
---|
Returns
TabCodeBoxCodeView | null | Code view or null if code view wasn't found. |
---|
removeCodeView
removeCodeView(identifier: string): boolean
Removes code view from code box.
Params
identifier : string | Identifier of code view to be removed. |
---|
Returns
boolean | Indicates 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 : string | Identifier of code view whose identifier should be changed. |
---|---|
newIdentifier : string | New identifier. |
Returns
boolean | Indicates 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 : string | Identifier of code view. |
---|
Returns
number | null | Position 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 : string | Identifier of code view. |
---|---|
position : number | Position of code view or file button (starting from 0) with which should be code view button swapped. |
Returns
boolean | Indicates whether the position has been successfully changed. |
---|
setActiveCodeView
setActiveCodeView(identifier: string): boolean
Sets code view as active (displays it in code box).
Params
identifier : string | Identifier of code view which should be set as active. |
---|
Returns
boolean | Indicates 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 | null | Active 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 : string | Identifier under which the file should be added to code box. |
---|---|
downloadLink : string | null | Download link or null if file should not be downloadable. |
Returns
boolean | Indicates whether file has been successfully added. |
---|
getFiles
getFiles(): TabCodeBoxFile[]
Returns all files of code box.
Returns
TabCodeBoxFile[] | Files. |
---|
getFile
getFile(identifier: string): TabCodeBoxFile | null
Returns file based on identifier.
Params
identifier : string | Identifier of file. |
---|
Returns
TabCodeBoxFile | null | File or null if file wasn't found. |
---|
removeFile
removeFile(identifier: string): boolean
Removes file from code box.
Params
identifier : string | Identifier of file to be removed. |
---|
Returns
boolean | Indicates 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 : string | Indentifier of file whose identifier should be changed. |
---|---|
newIdentifier : string | New identifier. |
Returns
boolean | Indicates whether change has been successfully completed (if passed new identifier already belongs to some other file in code box, it returns false). |
---|
changeFileDownloadLink
changeFileDownloadLink(identifier: string, newDownloadLink: string | null): boolean
Changes download link of file.
Params
identifier : string | Identifier of file whose download link should be changed. |
---|---|
newDownloadLink : string | null | Download link (or null if file should not be downloadable). |
Returns
boolean | Indicates 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 : string | Identifier of file. |
---|
Returns
number | null | Position 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 : string | Identifier of file. |
---|---|
position : number | Position of code view or file button (starting from 0) with which should be file button swapped. |
Returns
boolean | Indicates 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
CodeBoxMemento | Memento (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 : CodeBoxMemento | Memento (saved state of code box). |
---|