You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
COSMOS allows you to build custom widgets which can be deployed with your [plugin](../configuration/plugins.md) and used in [Telemetry Viewer](../tools/tlm-viewer.md). Building custom widgets can utilize any javascript frameworks but since COSMOS is written with Vue.js, we will use that framework in this tutorial. Please see the [Widget Generator](../getting-started/generators#widget-generator) guide for information about generating the scaffolding for a custom widget.
8
+
# Custom Widgets
9
9
10
-
## Custom Widgets
10
+
This guide will walk you through the process of building custom widgets for use in COSMOS [Telemetry Viewer](../tools/tlm-viewer.md). While you can use any JavaScript framework, we'll use Vue.js since COSMOS is built with it. Before starting, you may want to check out the [Widget Generator](../getting-started/generators#widget-generator) guide to create the initial scaffolding.
11
11
12
-
We're basically going to follow the COSMOS [Demo](https://github.com/OpenC3/cosmos/tree/main/openc3-cosmos-init/plugins/packages/openc3-cosmos-demo) and explain how that custom widget was created.
12
+
## Step 1: Set Up Your Plugin Structure
13
13
14
-
If you look at the bottom of the Demo's [plugin.txt](https://github.com/OpenC3/cosmos/blob/main/openc3-cosmos-init/plugins/packages/openc3-cosmos-demo/plugin.txt) file you'll see we declare the widgets:
14
+
If you have an existing plugin, start in the root directory for that plugin. If you do not yet have a plugin, start by using the [Plugin Generator](../getting-started/generators#plugin-generator) to create one.
15
+
16
+
:::warning Use separate plugins for tools and widgets
17
+
If your existing plugin contains a custom tool, you may run into build issues. In this instance, we recommend having one plugin for your custom tool, and a second plugin for you custom custom widgets.
18
+
:::
19
+
20
+
In your plugin's root directory, use the [Widget Generator](../getting-started/generators#widget-generator) to scaffold the widget.
21
+
22
+
Ensure your plugin has the correct directory structure:
23
+
24
+
```
25
+
your-plugin/
26
+
├── LICENSE.txt
27
+
├── your-plugin.gemspec
28
+
├── package.json
29
+
├── plugin.txt
30
+
├── Rakefile
31
+
├── README.md
32
+
├── src/
33
+
│ └── YourcustomWidget.vue
34
+
└── vite.config.js
35
+
```
36
+
37
+
## Step 2: Declare Your Widget in plugin.txt
38
+
39
+
In your plugin's `plugin.txt` file, declare each custom widget you want to create:
40
+
41
+
```ruby
42
+
WIDGETYOURCUSTOM
43
+
```
44
+
45
+
For example, in the COSMOS Demo plugin, two widgets are declared:
15
46
16
47
```ruby
17
48
WIDGETBIG
18
49
WIDGETHELLOWORLD
19
50
```
20
51
21
-
When the plugin is deployed this causes COSMOS to look for the as-built widgets. For the BIG widget it will look for the widget at `tools/widgets/BigWidget/BigWidget.umd.min.js`. Similarly it looks for HELLOWORLD at `tools/widgets/HelloworldWidget/HelloworldWidget.umd.min.js`. These directories and file names may seem mysterious but it's all about how the widgets get built.
52
+
## Step 3: Configure Your Build Process
22
53
23
-
### Helloworld Widget
54
+
### Set Up package.json
24
55
25
-
The Helloworld Widget source code is found in the plugin's src directory and is called [HelloworldWidget.vue](https://github.com/OpenC3/cosmos/blob/main/openc3-cosmos-init/plugins/packages/openc3-cosmos-demo/src/HelloworldWidget.vue). The basic structure is as follows:
56
+
Ensure your `package.json` includes the necessary build script:
57
+
58
+
```json
59
+
{
60
+
"scripts": {
61
+
"build": "vite build"
62
+
},
63
+
"dependencies": {
64
+
"@openc3/vue-common": "latest"
65
+
},
66
+
"devDependencies": {
67
+
"vite": "latest"
68
+
}
69
+
}
70
+
```
71
+
72
+
### Update Your Rakefile
73
+
74
+
Ensure your `Rakefile` is configured to run the build script in its `:build` task:
75
+
76
+
*(This should happen automatically if you use our code generators mentioned above.)*
77
+
78
+
```ruby
79
+
task :builddo
80
+
# ...
81
+
82
+
# Build the widget and gem using sh built into Rake:
If it doesn't exist already, create a Vue component file in the `src` directory, following this naming convention: `YourcustomWidget.vue`.
93
+
94
+
For example, to create a widget called "HELLOWORLD", you would create `HelloworldWidget.vue`:
26
95
27
96
```vue
28
97
<template>
29
-
<!-- Implement widget here -->
98
+
<!-- Your widget's HTML structure goes here -->
30
99
</template>
31
100
32
101
<script>
@@ -41,35 +110,140 @@ export default {
41
110
};
42
111
</script>
43
112
<style scoped>
44
-
/* widget specific style */
113
+
/* Widget-specific styles */
45
114
</style>
46
115
```
47
116
117
+
## Step 5: Develop Your Widget
118
+
119
+
This is where you'll design the actual layout and functionality of your widget. Let's expand on this using the Helloworld Widget as an example:
120
+
121
+
### Designing Your Widget Layout
122
+
123
+
In the `<template>` section, you'll define your widget's visual structure. For a simple Hello World widget:
124
+
125
+
```vue
126
+
<template>
127
+
<div class="hello-world-container">
128
+
<h3>{{ greeting }}</h3>
129
+
<p>This is a custom COSMOS widget</p>
130
+
<v-btn @click="updateGreeting" color="primary">
131
+
Change Greeting
132
+
</v-btn>
133
+
</div>
134
+
</template>
135
+
```
136
+
48
137
:::info Vue & Vuetify
49
138
For more information about how the COSMOS frontend is built (including all the Widgets) please check out [Vue.js](https://vuejs.org) and [Vuetify](https://vuetifyjs.com).
50
139
:::
51
140
52
-
To build this custom widget we changed the Demo [Rakefile](https://github.com/OpenC3/cosmos/blob/main/openc3-cosmos-init/plugins/packages/openc3-cosmos-demo/Rakefile) to call `yarn run build` when the plugin is built. `yarn run XXX` looks for 'scripts' to run in the [package.json](https://github.com/OpenC3/cosmos/blob/main/openc3-cosmos-init/plugins/packages/openc3-cosmos-demo/package.json) file. If we open package.json we find the following:
This uses the `vue-cli-service` to build the code found at `src/HelloworldWidget.vue` and formats as `umd-min` and puts it in the `tools/widgets/HelloworldWidget` directory. So this is why the plugin looks for the plugin at `tools/widgets/HelloworldWidget/HelloworldWidget.umd.min.js`. Click [here](https://cli.vuejs.org/guide/cli-service.html#vue-cli-service-build) for the `vue-cli-service build` documentation.
168
+
### Styling Your Widget
169
+
170
+
Add custom styles in the `<style>` section:
61
171
62
-
If you look at the Demo plugin's [simple.txt](https://github.com/OpenC3/cosmos/blob/main/openc3-cosmos-init/plugins/packages/openc3-cosmos-demo/targets/INST/screens/simple.txt) screen you'll see we're using the widgets:
172
+
```vue
173
+
<style scoped>
174
+
.hello-world-container {
175
+
padding: 15px;
176
+
border: 1px solid #ddd;
177
+
border-radius: 4px;
178
+
text-align: center;
179
+
background-color: #f9f9f9;
180
+
}
181
+
</style>
182
+
```
183
+
184
+
## Step 6: Configure Your Build Output
185
+
186
+
Ensure your `vite.config.js` file is configured to properly build your widgets:
Opening this screen in Telemetry Viewer results in the following:
227
+
In this example, we're using the HELLOWORLD widget from the demo, which will result in a screen that looks like this:
72
228
73
229

74
230
75
-
While this is a simple example the possibilities with custom widgets are limitless!
231
+
The widget name follows the convention from `plugin.txt` file. The screen definition for a screen that has only your custom widget created here, ensure your screen definition looks like this:
232
+
```ruby
233
+
SCREEN AUTO AUTO 0.5
234
+
YOURCUSTOM
235
+
```
236
+
237
+
If your widget requires telemetry data, make sure you include the target and telemetry information:
238
+
239
+
```ruby
240
+
YOURCUSTOM <%= target_name %>HEALTH_STATUSTEMP1
241
+
```
242
+
243
+
## Step 8: Build and Deploy Your Plugin
244
+
245
+
Follow the instructions [here](../getting-started/gettingstarted#building-your-plugin) to build and install your plugin containing your custom widget.
246
+
247
+
Now open Telemetry Viewer and select your screen to see your custom widget in action!
248
+
249
+
While this example is simple, the possibilities with custom widgets are limitless!
0 commit comments