Skip to content

Commit

Permalink
Merge fc8b30e into e2f06a2
Browse files Browse the repository at this point in the history
  • Loading branch information
Hilbrand committed Dec 15, 2022
2 parents e2f06a2 + fc8b30e commit c59b0c8
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@
</style>

<script>
import diacritic from 'diacritic'
import ChannelGroup from './channel-group.vue'
import ChannelLink from './channel-link.vue'
import ItemForm from '@/components/item/item-form.vue'
Expand Down Expand Up @@ -185,13 +183,13 @@ export default {
this.newItems.splice(this.newItems.findIndex((i) => i.channel === channel), 1)
} else {
this.selectedChannels.push(channel)
let newItemName = (this.newItemsPrefix) ? this.newItemsPrefix : diacritic.clean(this.thing.label).replace(/[^0-9a-z]/gi, '')
let newItemName = this.newItemsPrefix || this.$oh.util.normalizeLabel(this.thing.label)
newItemName += '_'
let suffix = channel.label || channelType.label || channel.id
if (this.thing.channels.filter((c) => c.label === suffix || (c.channelTypeUID && this.channelTypesMap[c.channelTypeUID] && this.channelTypesMap[c.channelTypeUID].label === suffix)).length > 1) {
suffix = channel.id.replace('#', '_').replace(/(^\w{1})|(_+\w{1})/g, letter => letter.toUpperCase())
}
newItemName += diacritic.clean(suffix).replace(/[^0-9a-z_]/gi, '')
newItemName += this.$oh.util.normalizeLabel(suffix)
const defaultTags = (channel.defaultTags.length > 0) ? channel.defaultTags : channelType.tags
const newItem = {
channel: channel,
Expand Down
4 changes: 3 additions & 1 deletion bundles/org.openhab.ui/web/src/js/openhab/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import auth from './auth'
import sse from './sse'
import media from './media'
import speech from './speech'
import util from './util'

export default {
api,
auth,
sse,
media,
speech
speech,
util
}
7 changes: 7 additions & 0 deletions bundles/org.openhab.ui/web/src/js/openhab/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import diacritic from 'diacritic'

export default {
normalizeLabel: (label) => {
return diacritic.clean(label.normalize('NFKD')).replace(/\s+/g, '_').replace(/[^0-9a-z_]/gi, '')
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@
</template>

<script>
import diacritic from 'diacritic'
import ThingPicker from '@/components/config/controls/thing-picker.vue'
import ModelPickerPopup from '@/components/model/model-picker-popup.vue'
import ChannelList from '@/components/thing/channel-list.vue'
Expand Down Expand Up @@ -275,7 +273,7 @@ export default {
if (this.createEquipment) {
this.newEquipmentItem = {
name: diacritic.clean(this.selectedThing.label).replace(/[^0-9a-z]/gi, ''),
name: this.$oh.util.normalizeLabel(this.selectedThing.label),
label: this.selectedThing.label,
tags: ['Equipment'],
type: 'Group',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import diacritic from 'diacritic'
import { Points } from '@/assets/semantics'

/**
Expand All @@ -24,13 +23,13 @@ export default (thing, channelTypes, newEquipmentItem, parentGroupsForEquipment,
for (const channel of thing.channels) {
if (channel.kind !== 'STATE') continue
const channelType = channelTypesMap.get(channel.channelTypeUID)
let newItemName = (newEquipmentItem) ? newEquipmentItem.name : diacritic.clean(thing.label).replace(/[^0-9a-z]/gi, '')
let newItemName = (newEquipmentItem) ? newEquipmentItem.name : this.$oh.util.normalizeLabel(thing.label)
newItemName += '_'
let suffix = channel.label || channel.id
if (thing.channels.filter((c) => c.label === suffix || (c.channelTypeUID && channelTypesMap[c.channelTypeUID] && channelTypesMap[c.channelTypeUID].label === suffix)).length > 1) {
suffix = channel.id.replace('#', '_').replace(/(^\w{1})|(_+\w{1})/g, letter => letter.toUpperCase())
}
newItemName += diacritic.clean(suffix).replace(/[^0-9a-z_]/gi, '')
newItemName += this.$oh.util.normalizeLabel(suffix)
const defaultTags = (channel.defaultTags.length > 0) ? channel.defaultTags : channelType.tags
const newItem = {
channel: channel,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,6 @@
</style>

<script>
import diacritic from 'diacritic'
import ConfigSheet from '@/components/config/config-sheet.vue'
import ItemPicker from '@/components/config/controls/item-picker.vue'
import ThingPicker from '@/components/config/controls/thing-picker.vue'
Expand Down Expand Up @@ -193,9 +191,9 @@ export default {
onPageAfterIn (event) {
if (!this.channel) return
this.loadProfileTypes(this.channel)
let newItemName = diacritic.clean(this.thing.label).replace(/[^0-9a-z]/gi, '')
let newItemName = this.$oh.util.normalizeLabel(this.thing.label)
newItemName += '_'
newItemName += (this.channel.label) ? diacritic.clean(this.channel.label).replace(/[^0-9a-z]/gi, '') : diacritic.clean(this.channelType.label).replace(/[^0-9a-z]/gi, '')
newItemName += this.$oh.util.normalizeLabel(this.channel.label || this.channelType.label)
const defaultTags = (this.channel.defaultTags.length > 0) ? this.channel.defaultTags : this.channelType.tags
this.$set(this, 'newItem', {
name: newItemName,
Expand Down
7 changes: 7 additions & 0 deletions bundles/org.openhab.ui/web/test/jest/__tests__/util.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import util from 'src/js/openhab/util.js'

describe('normalizeLabel', () => {
test('normalize the label to be a valid item name', () => {
expect('openHAB_3_0').toEqual(util.normalizeLabel('opénHAB? ₃?$_&.0'))
})
})

0 comments on commit c59b0c8

Please sign in to comment.