Skip to content

Commit

Permalink
Merge pull request #31 from fleetbase/dev-v0.2.4
Browse files Browse the repository at this point in the history
critical patch: register of widgets with `widgetId` not name
  • Loading branch information
roncodes authored Feb 7, 2024
2 parents 238d68c + da37038 commit e9f4dc4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
45 changes: 38 additions & 7 deletions addon/services/universe.js
Original file line number Diff line number Diff line change
Expand Up @@ -769,31 +769,39 @@ export default class UniverseService extends Service.extend(Evented) {
}

/**
* Creates a dashboard widget object
* Creates a dashboard widget object from the given widget configuration.
*
* @param {Object} widget
* @return {Widgetobject}
* @param {Object} widget - The widget configuration object.
* @param {string} widget.widgetId - The unique identifier for the widget.
* @param {string} widget.name - The name of the widget.
* @param {string} widget.description - The description of the widget.
* @param {string} widget.icon - The icon for the widget.
* @param {(Function|string)} widget.component - The component class or name for the widget.
* @param {Object} widget.grid_options - Grid options for the widget layout.
* @param {Object} widget.options - Additional options for the widget.
* @returns {Object} A new widget object with properties derived from the input configuration.
* @memberof UniverseService
*/
_createDashboardWidget(widget) {
// Extract properties from the widget object
let { did, name, description, icon, component, grid_options, options } = widget;
let { widgetId, name, description, icon, component, grid_options, options } = widget;

// If component is a definition register to host application
if (typeof component === 'function') {
const owner = getOwner(this);
const widgetId = component.widgetId || widgetId || this._createUniqueWidgetHashFromDefinition(component);

if (owner) {
owner.register(`component:${component.name}`, component);
owner.register(`component:${widgetId}`, component);

// Update component name
component = component.name;
component = widgetId;
}
}

// Create a new widget object with the extracted properties
const newWidget = {
did,
widgetId,
name,
description,
icon,
Expand All @@ -805,6 +813,29 @@ export default class UniverseService extends Service.extend(Evented) {
return newWidget;
}

/**
* Creates a unique hash from a component's definition. This hash is used as an identifier
* for the component when a direct identifier (widgetId) or a name is not available.
*
* @param {Function} component - The component class or constructor function.
* @returns {string} A unique hash string representing the component's definition.
* @memberof UniverseService
*/
_createUniqueWidgetHashFromDefinition(component) {
if (typeof component.toString === 'function') {
let definition = component.toString();
let hash = 0;
for (let i = 0; i < definition.length; i++) {
const char = definition.charCodeAt(i);
hash = (hash << 5) - hash + char;
hash |= 0;
}
return hash.toString(16);
}

return component.name;
}

/**
* Registers a new settings menu item.
*
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@fleetbase/ember-core",
"version": "0.2.3",
"version": "0.2.4",
"description": "Provides all the core services, decorators and utilities for building a Fleetbase extension for the Console.",
"keywords": [
"fleetbase-core",
Expand Down

0 comments on commit e9f4dc4

Please sign in to comment.