JavaScript Elements
With Universal Dashboard 2.3 and later you can now define JavaScript elements without having to use New-UDElement
. You can define your own objects and cmdlets without writing any C# code.
You can use the UD Custom Component Template project to get started quickly with custom components. For a full example project, check out ud-leaflet.
Universal Dashboard uses several web development technologies that may be helpful when developing your own JavaScript-based elements.
Webpack
Babel
React
NPM
To get started, you will need to define a NPM package.json file. This final defines the package's dependencies and any tooling that is required to bundle the package into a final JS file. UDSparklines uses react-sparklines.
The build
and dev
tasks define how the JS file is compiled. In this case, we are using webpack.
To define how webpack packages the JS file, we need to use a webpack.config.js. This file defines all the settings for webpack. This is a very minimal file and you can expand it will all kinds of plugins and configuration options.
The filename
option defines the output value of webpack. The entry
option defines the input file. In this case, we are using Babel JSX to define our React component. You can also define React components with Typescript and JavaScript.
Next, we are including a UniversalDashboard
external. This variable is available to all custom components and defines functions for calling back to the server.
Finally, we use the provide plugin for React. This ensures that you do not include all over React with each of your components as UD will provide this dependency.
The actual implementation of our React component is very minimal. It imports the react-sparklines
components and passes props to them. We need to ensure that when this JavaScript file is loaded by UniversalDashboard, the component is registered.
You will need to package your JavaScript before including it with your module. You can do this by running the npm build command. From the same directory where your package.json is.
Finally, we can write the PowerShell script to create these components. The first step is to register the JavaScript with Universal Dashboard. This will return an ID that you need to include with your components. This ensures the JavaScript is loaded when you use a component. JavaScript is only loaded when the component is used. It won't be loaded more than once.
Now you can create a function that users will call. The function should return a hashtable. Make sure to set isPlugin
to $true
and assetId
to the value you received when registering your script.
Distribution
At a minimum you will need to include the bundled JavaScript file and PSM1 file in a module. Users can then load this module into UD for use.
Last updated