Skip to content
This repository has been archived by the owner on Jan 19, 2023. It is now read-only.

Commit

Permalink
Add property CustomSvg to icons and navigation
Browse files Browse the repository at this point in the history
Signed-off-by: ftovaro <f.tovar12@gmail.com>
  • Loading branch information
ftovaro committed Aug 23, 2021
1 parent 5163d0f commit 3701e53
Show file tree
Hide file tree
Showing 17 changed files with 224 additions and 11 deletions.
1 change: 1 addition & 0 deletions changelogs/unreleased/1422-ftovaro
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added support of SVGs for Icons and Navigation
2 changes: 1 addition & 1 deletion cmd/octant-sample-plugin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func handleNavigation(request *service.NavigationRequest) (navigation.Navigation
},
},
},
IconName: "cloud",
IconName: "test-custom-svg",
}, nil
}

Expand Down
18 changes: 17 additions & 1 deletion pkg/navigation/navigation.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type Navigation struct {
Children []Navigation `json:"children,omitempty"`
IconName string `json:"iconName,omitempty"`
Loading bool `json:"isLoading"`
CustomSvg string `json:"customSvg,omitempty"`
}

// New creates a Navigation.
Expand Down Expand Up @@ -215,6 +216,7 @@ type navConfig struct {
suffix string
iconName string
isLoading bool
customSvg string
}

// EntriesHelper generates navigation entries.
Expand All @@ -229,14 +231,28 @@ func (neh *EntriesHelper) Add(title, suffix string, isLoading bool) {
})
}

// SetCustomSvg sets the icon for the navigation entry.
func SetCustomSvg(customSvg string) Option {
return func(n *Navigation) error {
if customSvg == "" {
return nil
}

n.CustomSvg = customSvg

return nil
}
}

// Generate generates navigation entries.
func (neh *EntriesHelper) Generate(prefix, namespace, name string) ([]Navigation, error) {
var navigations []Navigation

for _, nc := range neh.navConfigs {
navigation, err := New(nc.title, path.Join(prefix, nc.suffix),
SetNavigationIcon(nc.iconName),
SetLoading(nc.isLoading))
SetLoading(nc.isLoading),
SetCustomSvg(nc.customSvg))
if err != nil {
return nil, err
}
Expand Down
14 changes: 8 additions & 6 deletions pkg/plugin/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,10 @@ func convertToNavigation(in *dashboard.NavigationResponse_Navigation) navigation
}

out := navigation.Navigation{
Title: in.Title,
Path: in.Path,
IconName: in.IconName,
Title: in.Title,
Path: in.Path,
IconName: in.IconName,
CustomSvg: in.CustomSvg,
}

for _, child := range in.Children {
Expand All @@ -104,9 +105,10 @@ func convertToNavigation(in *dashboard.NavigationResponse_Navigation) navigation

func convertFromNavigation(in navigation.Navigation) *dashboard.NavigationResponse_Navigation {
out := dashboard.NavigationResponse_Navigation{
Title: in.Title,
Path: in.Path,
IconName: in.IconName,
Title: in.Title,
Path: in.Path,
IconName: in.IconName,
CustomSvg: in.CustomSvg,
}

for _, child := range in.Children {
Expand Down
8 changes: 8 additions & 0 deletions pkg/plugin/dashboard/dashboard.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions pkg/view/component/icon.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type IconConfig struct {
BadgeColor string `json:"badgeColor"`
Label string `json:"label"`
Tooltip *TooltipConfig `json:"tooltip,omitempty"`
CustomSvg string `json:"customSvg"`
}

type Direction string
Expand Down Expand Up @@ -146,3 +147,8 @@ func (i *Icon) SetBadgeColor(color string) {
func (i *Icon) SetSize(size string) {
i.Config.Size = size
}

// SetCustomSvg sets an SVG for an Icon.
func (i *Icon) SetCustomSvg(customSvg string) {
i.Config.CustomSvg = customSvg
}
45 changes: 45 additions & 0 deletions pkg/view/component/icon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,51 @@ func Test_Icon_Marshal(t *testing.T) {
}
}

func Test_Icon_With_SVG(t *testing.T) {
test := []struct {
name string
input *Icon
expectedPath string
isErr bool
}{
{
name: "in general with svg",
input: &Icon{
Base: newBase(TypeIcon, nil),
Config: IconConfig{
Shape: "user",
Size: "16",
Direction: DirectionDown,
Flip: FlipHorizontal,
Solid: true,
Status: StatusDanger,
Inverse: false,
Badge: BadgeDanger,
Color: "#add8e6",
BadgeColor: "purple",
Label: "example icon",
CustomSvg: "<svg width=\"9px\" height=\"9px\" viewBox=\"0 0 9 9\"><g stroke-width=\"1\"><g transform=\"translate(-496.000000, -443.000000)\" stroke=\"#111111\"></g></g></svg>",
},
},
expectedPath: "icon_with_svg.json",
isErr: false,
},
}
for _, tc := range test {
t.Run(tc.name, func(t *testing.T) {
actual, err := json.Marshal(tc.input)
isErr := err != nil
if isErr != tc.isErr {
t.Fatalf("UnExpected error: %v", err)
}

expected, err := ioutil.ReadFile(path.Join("testdata", tc.expectedPath))
require.NoError(t, err)
assert.JSONEq(t, string(expected), string(actual))
})
}
}

func Test_Icon_Options(t *testing.T) {
test := []struct {
name string
Expand Down
3 changes: 2 additions & 1 deletion pkg/view/component/testdata/icon.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"badge": "danger",
"color": "#add8e6",
"badgeColor": "purple",
"label": "example icon"
"label": "example icon",
"customSvg": ""
}
}
19 changes: 19 additions & 0 deletions pkg/view/component/testdata/icon_with_svg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"metadata": {
"type": "icon"
},
"config": {
"shape": "user",
"size": "16",
"direction": "down",
"flip": "horizontal",
"solid": true,
"status": "danger",
"inverse": false,
"badge": "danger",
"color": "#add8e6",
"badgeColor": "purple",
"label": "example icon",
"customSvg": "<svg width=\"9px\" height=\"9px\" viewBox=\"0 0 9 9\"><g stroke-width=\"1\"><g transform=\"translate(-496.000000, -443.000000)\" stroke=\"#111111\"></g></g></svg>"
}
}
3 changes: 2 additions & 1 deletion pkg/view/component/testdata/signpost.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"badge": "",
"color": "",
"badgeColor": "",
"label": ""
"label": "",
"customSvg": ""
}
},
"message": "Message",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
loadTextEditIconSet,
loadTechnologyIconSet,
loadChartIconSet,
ClarityIcons,
} from '@cds/core/icon';
import { IconView, Tooltip } from '../../../models/content';

Expand Down Expand Up @@ -58,6 +59,10 @@ export class IconComponent extends AbstractViewComponent<IconView> {
this.generateTooltipClassStyles();
}

if (view.config.customSvg) {
ClarityIcons.addIcons([view.config.shape, view.config.customSvg]);
}

this.shape = view.config.shape;
this.flip = view.config.flip;
this.size = view.config.size;
Expand Down
1 change: 1 addition & 0 deletions web/src/app/modules/shared/models/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,7 @@ export interface IconView extends View {
badgeColor?: string;
label?: string;
tooltip?: Tooltip;
customSvg?: string;
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { ContentService } from '../content/content.service';
import { NavigationEnd, Router, RouterEvent } from '@angular/router';
import { filter } from 'rxjs/operators';
import { LoadingService } from '../loading/loading.service';
import { ClarityIcons } from '@cds/core/icon';

export type Selection = {
module: number;
Expand All @@ -26,6 +27,7 @@ export type Module = {
endIndex?: number;
icon: string;
children?: any[];
customSvg?: string;
};

const emptyNavigation: Navigation = {
Expand Down Expand Up @@ -166,7 +168,11 @@ export class NavigationService {
description: section.description,
path: section.path,
title: section.title,
customSvg: section.customSvg,
});
if (section.customSvg) {
ClarityIcons.addIcons([section.iconName, section.customSvg]);
}
}
});

Expand All @@ -186,7 +192,11 @@ export class NavigationService {
path: module.path,
icon: module.icon,
title: module.title,
customSvg: module.customSvg,
};
if (module.customSvg) {
ClarityIcons.addIcons([module.icon, module.customSvg]);
}
module.children = [
...[first],
...sections[module.startIndex].children,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,14 @@
'button-tool-labels': showLabels
}">
<div *ngIf="showLabels" class="button-wrapper">
<clr-icon class="icon-nav" [attr.shape]="tab.icon"></clr-icon>
<div *ngIf="tab.customSvg; else regularIcon">
<cds-icon class="icon-nav" [attr.shape]="tab.icon"></cds-icon>
</div>
<ng-template #regularIcon>
<div>
<clr-icon class="icon-nav" [attr.shape]="tab.icon"></clr-icon>
</div>
</ng-template>
<div class="button-text-wrapper">
<div class="button-text">{{tab.title}}</div>
</div>
Expand Down
1 change: 1 addition & 0 deletions web/src/app/modules/sugarloaf/models/navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface NavigationChild {
children?: NavigationChild[];
iconName?: string;
isLoading: boolean;
customSvg?: string;
}

export interface Navigation {
Expand Down
42 changes: 42 additions & 0 deletions web/src/stories/docs/plugins/GoPluginsIntro.story.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -327,3 +327,45 @@ func handlePrint(request *service.PrintRequest) (plugin.PrintResponse, error) {
ctx = metadata.AppendToOutgoingContext(request.Context(), "x-octant-foo", "bar")
u, err := request.DashboardClient.Get(ctx, key)
```
## Custom Icon
### Custom Icon for plugins
Is possible to add a custom SVG for plugins.
First to do is define you SVG:
```go
svg := "<svg width=\"10px\" height=\"10px\">" +
"<g>" +
"<ellipse fill=\"#000000\" stroke=\"#000000\" cx=\"5\" cy=\"5.5\" rx=\"3\" ry=\"3\"/>" +
"</g>" +
"</svg>"
```
Then, where you are defining your navigation, add the SVG and set a custom name for it:
```go
navigation.Navigation{
Title: "Sample Plugin",
Path: request.GeneratePath(),
Children: []navigation.Navigation{
{
Title: "Nested Once",
Path: request.GeneratePath("nested-once"),
IconName: "folder",
Children: []navigation.Navigation{
{
Title: "Nested Twice",
Path: request.GeneratePath("nested-once", "nested-twice"),
IconName: "folder",
},
},
},
},
IconName: "test-custom-svg",
CustomSvg: svg,
}, nil
```
Loading

0 comments on commit 3701e53

Please sign in to comment.