Skip to content

Commit

Permalink
[mainui] support staticIcon parameter in sitemap configuration (#1999)
Browse files Browse the repository at this point in the history
Related to openhab/openhab-core#3735

---------

Signed-off-by: Mark Herwege <mark.herwege@telenet.be>
  • Loading branch information
mherwege authored Sep 30, 2023
1 parent f32ef1f commit 87e68aa
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 0 deletions.
11 changes: 11 additions & 0 deletions bundles/org.openhab.ui/web/src/assets/sitemap-lexer.nearley
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
name: 'name=',
label: 'label=',
item: 'item=',
staticIcon: 'staticIcon=',
icon: 'icon=',
widgetattr: ['url=', 'refresh=', 'service=', 'period=', 'height=', 'minValue=', 'maxValue=', 'step=', 'encoding=', 'yAxisDecimalPattern=', 'inputHint='],
widgetboolattr: ['legend='],
Expand Down Expand Up @@ -71,6 +72,15 @@
}
}

// if icon exists remove staticIcon, if not set icon to staticIcon and make saticIcon=true
if (widget.config.icon) {
delete widget.config.staticIcon
}
if (widget.config.staticIcon) {
widget.config.icon = widget.config.staticIcon
widget.config.staticIcon = true
}

// reject widgets with missing parameters
if (requiresItem.includes(widget.component) && !widget.config.item) return reject
if ((widget.component === 'Video' || widget.component === 'Webview') && !widget.config.url) return reject
Expand Down Expand Up @@ -103,6 +113,7 @@ WidgetAttr -> %widgetswitchattr
| %widgetboolattr _ WidgetBooleanAttrValue {% (d) => [d[0].value, d[2]] %}
| %widgetfreqattr _ WidgetAttrValue {% (d) => ['frequency', d[2]] %}
| %icon _ WidgetIconAttrValue {% (d) => [d[0].value, d[2].join("")] %}
| %staticIcon_ WidgetIconAttrValue {% (d) => [d[0].value, d[2].join("")] %}
| WidgetAttrName _ WidgetAttrValue {% (d) => [d[0][0].value, d[2]] %}
| WidgetMappingsAttrName WidgetMappingsAttrValue {% (d) => [d[0][0].value, d[1]] %}
| WidgetVisibilityAttrName WidgetVisibilityAttrValue {% (d) => [d[0][0].value, d[1]] %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,43 @@ describe('dslUtil', () => {
])
})

it('renders a widget with icon correctly', () => {
const component = createSitemapComponent('test', 'Test')
const widget = {
}
addWidget(component, 'Switch', {
item: 'TestItem',
label: 'Test Switch',
icon: 'lightbulb'
})
const sitemap = dslUtil.toDsl(component).split('\n')
expect(sitemap).toEqual([
'sitemap test label="Test" {',
' Switch item=TestItem label="Test Switch" icon=lightbulb',
'}',
''
])
})

it('renders a widget with static icon correctly', () => {
const component = createSitemapComponent('test', 'Test')
const widget = {
}
addWidget(component, 'Switch', {
item: 'TestItem',
label: 'Test Switch',
icon: 'lightbulb',
staticIcon: true
})
const sitemap = dslUtil.toDsl(component).split('\n')
expect(sitemap).toEqual([
'sitemap test label="Test" {',
' Switch item=TestItem label="Test Switch" staticIcon=lightbulb',
'}',
''
])
})

it('renders a sitemap with a frame container widget correctly', () => {
const component = createSitemapComponent('test', 'Test')
const frame = addWidget(component, 'Frame', {})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,56 @@ describe('SitemapCode', () => {
})
})

it('parses a staticIcon definition', async () => {
expect(wrapper.vm.sitemapDsl).toBeDefined()
// simulate updating the sitemap in code
const sitemap = [
'sitemap test label="Test" {',
' Switch item=Item_Icon icon=lightbulb',
' Switch item=Item_Icon staticIcon=lightbulb_static',
' Switch item=Item_Icon icon=lightbulb staticIcon=lightbulb_static',
'}',
''
].join('\n')
wrapper.vm.updateSitemap(sitemap)
expect(wrapper.vm.sitemapDsl).toMatch(/^sitemap test label="Test"/)
expect(wrapper.vm.parsedSitemap.error).toBeFalsy()

await wrapper.vm.$nextTick()

// check whether an 'updated' event was emitted and its payload
// (should contain the parsing result for the new sitemap definition)
const events = wrapper.emitted().updated
expect(events).toBeTruthy()
expect(events.length).toBe(1)
const payload = events[0][0]
expect(payload.slots).toBeDefined()
expect(payload.slots.widgets).toBeDefined()
expect(payload.slots.widgets.length).toBe(3)
expect(payload.slots.widgets[0]).toEqual({
component: 'Switch',
config: {
item: 'Item_Icon',
icon: 'lightbulb'
}
})
expect(payload.slots.widgets[1]).toEqual({
component: 'Switch',
config: {
item: 'Item_Icon',
icon: 'lightbulb_static',
staticIcon: true
}
})
expect(payload.slots.widgets[2]).toEqual({
component: 'Switch',
config: {
item: 'Item_Icon',
icon: 'lightbulb'
}
})
})

it('parses a segmented icon name with hyphens', async () => {
expect(wrapper.vm.sitemapDsl).toBeDefined()
// simulate updating the sitemap in code
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ function writeWidget (widget, indent) {
dsl += ' sendFrequency=' + widget.config[key]
} else if (key === 'forceAsItem') {
dsl += ' forceasitem=' + widget.config[key]
} else if (['icon', 'staticIcon'].includes(key)) {
if (widget.config.staticIcon) {
dsl += ' staticIcon=' + widget.config[key]
} else {
dsl += ' icon=' + widget.config[key]
}
} else {
dsl += ` ${key}=`
if (key === 'item' || key === 'period' || key === 'legend' || Number.isFinite(widget.config[key])) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
<oh-icon :icon="widget.config.icon" height="32" width="32" />
</div>
</f7-list-input>
<f7-list-item title="Static icon">
<f7-toggle slot="after" :checked="widget.config.staticIcon" @toggle:change="widget.config.staticIcon = $event" />
</f7-list-item>
</ul>
<ul>
<!-- additional controls -->
Expand Down

0 comments on commit 87e68aa

Please sign in to comment.