24 KiB
id, title
| id | title |
|---|---|
| api-reference | API Reference |
@nebula.js/stardust
version: 0.6.0-alpha.0
Table of contents
- function: embed(app[, instanceConfig])
- function: useState(initialState)
- function: useEffect(effect[, deps])
- function: useMemo(factory, deps)
- function: usePromise(factory[, deps])
- function: useElement()
- function: useRect()
- function: useLayout()
- function: useStaleLayout()
- function: useAppLayout()
- function: useModel()
- function: useApp()
- function: useGlobal()
- function: useSelections()
- function: useTheme()
- function: useTranslator()
- function: useAction(factory[, deps])
- function: useConstraints()
- function: useOptions()
- function: onTakeSnapshot(snapshotCallback)
- interface: Context
- interface: Configuration
- undefined: Galaxy.translator
- undefined: Galaxy.flags
- undefined: Galaxy.anything
- class: Embed
- interface: ThemeInfo
- class: SupernovaController
- interface: Flags
- interface: CreateConfig
- interface: BaseConfig
- interface: GetConfig
- [type Field = <string|
qae.NxDimension|qae.NxMeasure|LibraryField>](#type-field-stringqaenxdimensionqaenxmeasurelibraryfield) - interface: LibraryField
- class: AppSelections
- class: ObjectSelections
- objectSelections.begin(paths)
- objectSelections.clear()
- objectSelections.confirm()
- objectSelections.cancel()
- objectSelections.select(s)
- objectSelections.canClear()
- objectSelections.canConfirm()
- objectSelections.canCancel()
- objectSelections.isActive()
- objectSelections.isModal()
- objectSelections.goModal(paths)
- objectSelections.noModal([accept])
- objectSelections.abortModal()
- interface: LoadType(type)
- interface: TypeInfo
- interface: Supernova(galaxy)
- interface: SupernovaDefinition
- interface: SetStateFn(newState)
- [type EffectCallback = <Function>](#type-effectcallback-function)
- interface: Rect
- interface: ActionDefinition
- interface: Constraints
- interface: QAEDefinition
- interface: DataTarget
- interface: FieldTarget
- class: Translator
- class: Theme
- theme.getDataColorScales()
- theme.getDataColorPalettes()
- theme.getDataColorPickerPalettes()
- theme.getDataColorSpecials()
- theme.getColorPickerColor(c)
- theme.getContrastingColorTo(color)
- theme.getStyle(basePath, path, attribute)
- interface: ScalePalette
- interface: DataPalette
- interface: ColorPickerPalette
- interface: DataColorSpecials
API
function: embed(app[, instanceConfig])
app<enigma.Doc>instanceConfig<Configuration>returns:<Embed>
Initiates a new embed instance using the specified app.
import { embed } from '@nebula.js/stardust';
const n = embed(app);
n.render({ id: 'abc' });
embed.createConfiguration(configuration)
configuration<Configuration> The configuration objectreturns:<Embed>
Creates a new embed scope bound to the specified configuration.
The configuration is merged with all previous scopes.
import { embed } from '@nebula.js/stardust';
// create a 'master' config which registers all types
const m = embed.createConfiguration({
types: [
{
name: 'mekko',
version: '1.0.0',
load: () => Promise.resolve(mekko),
},
],
});
// create an alternate config with dark theme
// and inherit the config from the previous
const d = m.createConfiguration({
theme: 'dark',
});
m(app).render({ type: 'mekko' }); // will render the object with default theme
d(app).render({ type: 'mekko' }); // will render the object with 'dark' theme
embed(app).render({ type: 'mekko' }); // will throw error since 'mekko' is not a register type on the default instance
function: useState(initialState)
initialState<S|Function> The initial state.returns:<Array> The value and a function to update it.
Creates a stateful value.
import { useState } from '@nebula.js/stardust';
// ...
// initiate with simple primitive value
const [zoomed, setZoomed] = useState(false);
// update
setZoomed(true);
// lazy initiation
const [value, setValue] = useState(() => heavy());
function: useEffect(effect[, deps])
effect<EffectCallback> The callback.deps<Array<any>> The dependencies which should trigger the callback.
Triggers a callback function when a dependant value changes.
import { useEffect } from '@nebula.js/stardust';
// ...
useEffect(() => {
console.log('mounted');
return () => {
console.log('unmounted');
};
}, []);
function: useMemo(factory, deps)
factory<Function> The factory function.deps<Array<any>> The dependencies.returns:<T> The value returned from the factory function.
Creates a stateful value when a dependant changes.
import { useMemo } from '@nebula.js/stardust';
// ...
const v = useMemo(() => {
return doSomeHeavyCalculation();
}), []);
function: usePromise(factory[, deps])
factory<Function> The factory function that calls the promise.deps<Array<any>> The dependencies.returns:<Array> The resolved value.
Runs a callback function when a dependant changes.
import { usePromise } from '@nebula.js/stardust';
import { useModel } from '@nebula.js/stardust';
// ...
const model = useModel();
const [resolved, rejected] = usePromise(() => model.getLayout(), []);
function: useElement()
returns:<HTMLElement>
Gets the HTMLElement this supernova is rendered into.
import { useElement } from '@nebula.js/stardust';
// ...
const el = useElement();
el.innerHTML = 'Hello!';
function: useRect()
returns:<Rect> The size of the element.
Gets the size of the HTMLElement the supernova is rendered into.
import { useRect } from '@nebula.js/stardust';
// ...
const rect = useRect();
useEffect(() => {
console.log('resize');
}, [rect.width, rect.height]);
function: useLayout()
returns:<qae.GenericObjectLayout>
Gets the layout of the generic object associated with this supernova.
import { useLayout } from '@nebula.js/stardust';
// ...
const layout = useLayout();
console.log(layout);
function: useStaleLayout()
returns:<qae.GenericObjectLayout>
Gets the layout of the generic object associated with this supernova.
Unlike the regular layout, a stale layout is not changed when a generic object enters
the modal state. This is mostly notable in that qSelectionInfo.qInSelections in the layout is
always false.
The returned value from useStaleLayout() and useLayout() are identical when the object
is not in a modal state.
import { useStaleLayout } from '@nebula.js/stardust';
// ...
const staleLayout = useStaleLayout();
console.log(staleLayout);
function: useAppLayout()
returns:<qae.NxAppLayout> The app layout
Gets the layout of the app associated with this supernova.
import { useAppLayout } from '@nebula.js/stardust';
// ...
const appLayout = useAppLayout();
console.log(appLayout.qLocaleInfo);
function: useModel()
returns:<enigma.GenericObject|undefined>
Gets the generic object API of the generic object connected to this supernova.
import { useModel } from '@nebula.js/stardust';
// ...
const model = useModel();
useEffect(() => {
model.getInfo().then((info) => {
console.log(info);
});
}, []);
function: useApp()
returns:<enigma.Doc|undefined> The doc API.
Gets the doc API.
import { useApp } from '@nebula.js/stardust';
// ...
const app = useApp();
useEffect(() => {
app.getAllInfos().then((infos) => {
console.log(infos);
});
}, []);
function: useGlobal()
returns:<enigma.Global|undefined> The global API.
Gets the global API.
import { useGlobal } from '@nebula.js/stardust';
// ...
const g = useGlobal();
useEffect(() => {
g.engineVersion().then((version) => {
console.log(version);
});
}, []);
function: useSelections()
returns:<ObjectSelections> The object selections.
Gets the object selections.
import { useSelections } from '@nebula.js/stardust';
import { useElement } from '@nebula.js/stardust';
import { useEffect } from '@nebula.js/stardust';
// ...
const selections = useSelections();
const element = useElement();
useEffect(() => {
const onClick = () => {
selections.begin('/qHyperCubeDef');
};
element.addEventListener('click', onClick);
return () => {
element.removeEventListener('click', onClick);
};
}, []);
function: useTheme()
returns:<Theme> The theme.
Gets the theme.
import { useTheme } from '@nebula.js/stardust';
const theme = useTheme();
console.log(theme.getContrastinColorTo('#ff0000'));
function: useTranslator()
returns:<Translator> The translator.
Gets the translator.
import { useTranslator } from '@nebula.js/stardust';
// ...
const translator = useTranslator();
console.log(translator.get('SomeString'));
function: useAction(factory[, deps])
Registers a custom action.
import { useAction } from '@nebula.js/stardust';
// ...
const [zoomed, setZoomed] = useState(false);
const act = useAction(
() => ({
hidden: false,
disabled: zoomed,
action() {
setZoomed((prev) => !prev);
},
icon: {},
}),
[zoomed]
);
function: useConstraints()
returns:<Constraints>
Gets the desired constraints that should be applied when rendering the supernova.
The constraints are set on the nuclues configuration before the supernova is rendered and should respected by you when implementing the supernova.
// configure embed to disallow active interactions when rendering
embed(app, {
constraints: {
active: true, // do not allow interactions
},
}).render({ element, id: 'sdfsdf' });
import { useConstraints } from '@nebula.js/stardust';
// ...
const constraints = useConstraints();
useEffect(() => {
if (constraints.active) {
// do not add any event listener if active constraint is set
return undefined;
}
const listener = () => {};
element.addEventListener('click', listener);
return () => {
element.removeEventListener('click', listener);
};
}, [constraints]);
function: useOptions()
returns:<Object>
Gets the options object provided when rendering the supernova.
This is an empty object by default but enables customization of the supernova through this object. Options are different from setting properties on the generic object in that options are only temporary settings applied to the supernova when rendered.
You have the responsibility to provide documentation of the options you support, if any.
// when embedding the supernova, anything can be set in options
embed(app).render({
element,
type: 'my-chart',
options: {
showNavigation: true,
},
});
// it is up to you use and implement the provided options
import { useOptions } from '@nebula.js/stardust';
import { useEffect } from '@nebula.js/stardust';
// ...
const options = useOptions();
useEffect(() => {
if (!options.showNavigation) {
// hide navigation
} else {
// show navigation
}
}, [options.showNavigation]);
function: onTakeSnapshot(snapshotCallback)
snapshotCallback<Function>
Registers a callback that is called when a snapshot is taken.
import { onTakeSnapshot } from '@nebula.js/stardust';
import { useState } from '@nebula.js/stardust';
import { useLayout } from '@nebula.js/stardust';
const layout = useLayout();
const [zoomed] = useState(layout.isZoomed || false);
onTakeSnapshot((copyOfLayout) => {
copyOfLayout.isZoomed = zoomed;
return Promise.resolve(copyOfLayout);
});
interface: Context
interface: Configuration
undefined: Galaxy.translator
undefined: Galaxy.flags
undefined: Galaxy.anything
class: Embed
embed.render(cfg)
cfg<CreateConfig|GetConfig> The render configuration.returns:<Promise<SupernovaController>> A controller to the rendered visualization
Renders a visualization into an HTMLElement.
// render from existing object
n.render({
element: el,
id: 'abcdef',
});
// render on the fly
n.render({
type: 'barchart',
fields: ['Product', { qLibraryId: 'u378hn', type: 'measure' }],
});
embed.context(ctx)
Updates the current context of this embed instance. Use this when you want to change some part of the current context, like theme.
// change theme
n.context({ theme: 'dark' });
// limit constraints
n.context({ constraints: { active: true } });
embed.selections()
returns:<Promise<AppSelections>>
Gets the app selections of this instance.
const selections = await n.selections();
selections.mount(element);
interface: ThemeInfo
id<string> Theme identifierload<Function> A function that should return a Promise that resolve to a raw JSON theme
class: SupernovaController
A controller to further modify a supernova after it has been rendered.
const ctl = await embed(app).render({
element,
type: 'barchart',
});
ctl.destroy();
supernovaController.destroy()
Destroys the supernova and removes if from the the DOM.
const ctl = ctl.destroy();
interface: Flags
isEnabled<Function> Checks whether the specified flag is enabled.
interface: CreateConfig
- extends: <BaseConfig>
interface: BaseConfig
element<HTMLElement>options<Object>
interface: GetConfig
- extends: <BaseConfig>
id<string>
type Field = <string|qae.NxDimension|qae.NxMeasure|LibraryField>
interface: LibraryField
qLibraryId<string>type<'dimension'|'measure'>
class: AppSelections
appSelections.mount(element)
element<HTMLElement>
Mounts the app selection UI into the provided HTMLElement
selections.mount(element);
appSelections.unmount()
Unmounts the app selection UI from the DOM
selections.unmount();
class: ObjectSelections
objectSelections.begin(paths)
objectSelections.clear()
returns:<Promise<undefined>>
objectSelections.confirm()
returns:<Promise<undefined>>
objectSelections.cancel()
returns:<Promise<undefined>>
objectSelections.select(s)
objectSelections.canClear()
returns:<boolean>
objectSelections.canConfirm()
returns:<boolean>
objectSelections.canCancel()
returns:<boolean>
objectSelections.isActive()
returns:<boolean>
objectSelections.isModal()
returns:<boolean>
objectSelections.goModal(paths)
objectSelections.noModal([accept])
objectSelections.abortModal()
returns:<Promise<undefined>>
interface: LoadType(type)
interface: TypeInfo
interface: Supernova(galaxy)
galaxy<Galaxy>returns:<SupernovaDefinition>
The entry point for defining a supernova.
import { useElement, useLayout } from '@nebula.js/stardust';
export default function () {
return {
qae: {
properties: {
dude: 'Heisenberg',
},
},
component() {
const el = useElement();
const layout = useLayout();
el.innerHTML = `What's my name? ${layout.dude}!!!`;
},
};
}
interface: SupernovaDefinition
qae<QAEDefinition>component<Function>
interface: SetStateFn(newState)
newState<S|Function> The new state
type EffectCallback = <Function>
interface: Rect
interface: ActionDefinition
interface: Constraints
interface: QAEDefinition
properties<qae.GenericObjectProperties>data<Object>targets<Array<DataTarget>>
interface: DataTarget
path<string>dimensions<FieldTarget<qae.NxDimension>>measures<FieldTarget<qae.NxMeasure>>
interface: FieldTarget
class: Translator
translator.add(item)
Registers a string in multiple locales
translator.add({
id: 'company.hello_user',
locale: {
'en-US': 'Hello {0}',
'sv-SE': 'Hej {0}
}
});
translator.get('company.hello_user', ['John']); // Hello John
translator.get(str[, args])
str<string> Id of the registered stringargs<Array<string>> Values passed down for string interpolationreturns:<string> The translated string
Translates a string for current locale
class: Theme
theme.getDataColorScales()
returns:<Array<ScalePalette>>
theme.getDataColorPalettes()
returns:<Array<DataPalette>>
theme.getDataColorPickerPalettes()
returns:<Array<ColorPickerPalette>>
theme.getDataColorSpecials()
returns:<DataColorSpecials>
theme.getColorPickerColor(c)
Resolve a color object using the color picker palette from the provided JSON theme.
theme.getColorPickerColor({ index: 1 });
theme.getColorPickerColor({ color: 'red' });
theme.getContrastingColorTo(color)
color<string> A color to measure the contrast againstreturns:<string> - The color that has the best contrast against the specifiedcolor.
Get the best contrasting color against the specified color.
This is typically used to find a suitable text color for a label placed on an arbitrarily colored background.
The returned colors are derived from the theme.
theme.getContrastingColorTo('#400');
theme.getStyle(basePath, path, attribute)
basePath<string> Base path in the theme's json structure to start the search in (specified as a name path separated by dots)path<string> Expected path for the attribute (specified as a name path separated by dots)attribute<string> Name of the style attributereturns:<string> The style value
Get the value of a style attribute in the theme by searching in the theme's json structure. The search starts at the specified base path and continue upwards until the value is found. If possible it will get the attribute's value using the given path.
theme.getStyle('object', 'title.main', 'fontSize'));
theme.getStyle('', '', 'fontSize'));