From d6519450bba86e27d5baec90c8bebc620513ff6a Mon Sep 17 00:00:00 2001 From: jimtng <2554958+jimtng@users.noreply.github.com> Date: Tue, 11 Mar 2025 00:16:10 +1000 Subject: [PATCH] Inbox: Add `Copy DSL Definition` (to clipboard) button & Add selection count (#3021) Refs https://github.com/openhab/openhab-core/pull/4569. This allows us to quickly create the Thing inside a .things file from the discovered Inbox items. It works when clicking a single inbox entry, and also when multiple entries are selected. It also adds a count for the number of the selected Things. --------- Also-by: Florian Hotze Signed-off-by: Jimmy Tanagra --- .../org.openhab.ui/web/src/js/openhab/api.js | 31 +++++-- .../settings/things/add/choose-thing-type.vue | 3 +- .../settings/things/inbox/inbox-list.vue | 92 +++++++++++++++---- .../settings/things/thing-inbox-mixin.js | 26 ++++++ 4 files changed, 125 insertions(+), 27 deletions(-) diff --git a/bundles/org.openhab.ui/web/src/js/openhab/api.js b/bundles/org.openhab.ui/web/src/js/openhab/api.js index 620bdb4c9a..c25a9567c4 100644 --- a/bundles/org.openhab.ui/web/src/js/openhab/api.js +++ b/bundles/org.openhab.ui/web/src/js/openhab/api.js @@ -30,15 +30,28 @@ export default { get (uri, data) { return wrapPromise(Framework7.request.promise.json(uri, data)) }, - getPlain (uri, data, contentType, responseType) { - return wrapPromise(Framework7.request.promise({ - method: 'GET', - url: uri, - data, - processData: false, - contentType: contentType || 'text/plain', - xhrFields: typeof responseType !== 'undefined' ? { responseType } : null - })) + getPlain (uri_or_parameters, data, contentType, responseType) { + let parameters = {} + if (typeof uri_or_parameters === 'string') { + parameters = { + url: uri_or_parameters, + method: 'GET', + data, + processData: false, + contentType: contentType || 'text/plain', + xhrFields: typeof responseType !== 'undefined' ? { responseType } : null + } + } else if (typeof uri_or_parameters === 'object') { + parameters = { + contentType: 'text/plain', + processData: false, + method: 'GET', + ...uri_or_parameters + } + } else { + throw new Error('Invalid parameters') + } + return wrapPromise(Framework7.request.promise(parameters)) }, post (uri, data, dataType) { return wrapPromise(Framework7.request.promise.postJSON(uri, data, dataType)) diff --git a/bundles/org.openhab.ui/web/src/pages/settings/things/add/choose-thing-type.vue b/bundles/org.openhab.ui/web/src/pages/settings/things/add/choose-thing-type.vue index db7d2fa99a..2eb6bf1c99 100644 --- a/bundles/org.openhab.ui/web/src/pages/settings/things/add/choose-thing-type.vue +++ b/bundles/org.openhab.ui/web/src/pages/settings/things/add/choose-thing-type.vue @@ -244,7 +244,8 @@ export default { } ], [ - this.entryActionsAddAsThingButton(entry, this.loadInbox) + this.entryActionsAddAsThingButton(entry, this.loadInbox), + this.entryActionsCopyThingDefinitionButton(entry) ] ] }) diff --git a/bundles/org.openhab.ui/web/src/pages/settings/things/inbox/inbox-list.vue b/bundles/org.openhab.ui/web/src/pages/settings/things/inbox/inbox-list.vue index 119e4075e4..6041c789cf 100644 --- a/bundles/org.openhab.ui/web/src/pages/settings/things/inbox/inbox-list.vue +++ b/bundles/org.openhab.ui/web/src/pages/settings/things/inbox/inbox-list.vue @@ -17,23 +17,55 @@ - -  Remove {{ selectedItems.length }} - - -  Ignore {{ selectedItems.length }} - - -  Approve {{ selectedItems.length }} - +
+ +  Remove + + +  Ignore + + +  Unignore + + +  Approve + + + + + +
{{ selectedItems.length }} selected
-
+
+
@@ -47,7 +79,7 @@ - {{ inboxCount }} entries + {{ inboxCount }} entries
@@ -264,6 +296,7 @@ export default { ], [ this.entryActionsAddAsThingButton(entry, this.load), + this.entryActionsCopyThingDefinitionButton(entry), { text: (!ignored) ? 'Ignore' : 'Unignore', color: (!ignored) ? 'orange' : 'blue', @@ -389,6 +422,8 @@ export default { }, performActionOnSelection (action) { let progressMessage, successMessage, promises + let navigateToThingsPage = false + let clearSelectionAndReload = true switch (action) { case 'delete': progressMessage = 'Removing Inbox Entries...' @@ -399,6 +434,7 @@ export default { progressMessage = 'Approving Inbox Entries...' successMessage = `${this.selectedItems.length} entries approved` promises = this.filterSelectedItems().map((e) => this.$oh.api.postPlain('/rest/inbox/' + e.thingUID + '/approve', e.label)) + navigateToThingsPage = true break case 'ignore': progressMessage = 'Ignoring Inbox Entries...' @@ -410,22 +446,44 @@ export default { successMessage = `${this.selectedItems.length} entries unignored` promises = this.filterSelectedItems().map((e) => this.$oh.api.postPlain('/rest/inbox/' + e.thingUID + '/unignore')) break + case 'copy': + progressMessage = 'Copying Inbox Entries...' + successMessage = `${this.selectedItems.length} entries copied to clipboard` + promises = this.filterSelectedItems().map((e) => this.$oh.api.getPlain({ + url: '/rest/file-format/things/' + e.thingUID, + headers: { accept: 'text/vnd.openhab.dsl.thing' } + })) + + promises = [Promise.all(promises).then((data) => { + if (this.$clipboard(data.join('\n'))) { + Promise.resolve() + } else { + Promise.reject('Failed to copy to clipboard') + } + })] + clearSelectionAndReload = false + break } let dialog = this.$f7.dialog.progress(progressMessage) - Promise.all(promises).then((data) => { + Promise.all(promises).then(() => { this.$f7.toast.create({ text: successMessage, destroyOnClose: true, closeTimeout: 2000 }).open() + const searchFor = this.selectedItems.join(',') + if (clearSelectionAndReload) this.selectedItems = [] dialog.close() - this.$f7router.navigate('/settings/things/', { - props: { - searchFor: this.selectedItems.join(',') - } - }) + if (clearSelectionAndReload) this.load() + if (navigateToThingsPage) { + this.$f7router.navigate('/settings/things/', { + props: { + searchFor + } + }) + } }).catch((err) => { dialog.close() this.load() diff --git a/bundles/org.openhab.ui/web/src/pages/settings/things/thing-inbox-mixin.js b/bundles/org.openhab.ui/web/src/pages/settings/things/thing-inbox-mixin.js index 745aced8c9..05891994e5 100644 --- a/bundles/org.openhab.ui/web/src/pages/settings/things/thing-inbox-mixin.js +++ b/bundles/org.openhab.ui/web/src/pages/settings/things/thing-inbox-mixin.js @@ -1,3 +1,7 @@ +import Vue from 'vue' +import Clipboard from 'v-clipboard' +Vue.use(Clipboard) + import ThingMixin from '@/components/thing/thing-mixin' export default { @@ -115,6 +119,28 @@ export default { }).open() } } + }, + entryActionsCopyThingDefinitionButton (entry) { + return { + text: 'Copy DSL Definition', + color: 'blue', + bold: true, + onClick: () => { + const headers = { accept: 'text/vnd.openhab.dsl.thing' } + this.$oh.api.getPlain({ + url: '/rest/file-format/things/' + entry.thingUID, + headers: { accept: 'text/vnd.openhab.dsl.thing' } + }).then(definition => { + if (this.$clipboard(definition)) { + this.$f7.toast.create({ + text: `DSL Thing definition for '${entry.thingUID}' copied to clipboard`, + destroyOnClose: true, + closeTimeout: 2000 + }).open() + } + }) + } + } } } }