Copy File Definition: Process multiple objects with single api request (#3163)

Refs https://github.com/openhab/openhab-core/pull/4734.

This solves the problem with YAML format returning the `version` and
`things` (or `items`) keys for each item in multi-select requests. By
combining them into a single request, only one key will be returned at
the top.

---------

Signed-off-by: Jimmy Tanagra <jcode@tanagra.id.au>
pull/3168/head
jimtng 2025-04-24 07:01:16 +10:00 committed by GitHub
parent 19c3a42d60
commit c9c4ab6f76
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 19 additions and 45 deletions

View File

@ -30,40 +30,29 @@ export default {
get (uri, data) {
return wrapPromise(Framework7.request.promise.json(uri, data))
},
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))
getPlain (uri, data, contentType, responseType, headers) {
return wrapPromise(Framework7.request.promise({
method: 'GET',
url: uri,
data,
processData: false,
contentType: contentType || 'text/plain',
xhrFields: typeof responseType !== 'undefined' ? { responseType } : null,
headers
}))
},
post (uri, data, dataType) {
return wrapPromise(Framework7.request.promise.postJSON(uri, data, dataType))
},
postPlain (uri, data, dataType, contentType) {
postPlain (uri, data, dataType, contentType, headers) {
return wrapPromise(Framework7.request.promise({
method: 'POST',
url: uri,
data,
processData: false,
contentType: contentType || 'text/plain',
dataType: dataType || 'application/json'
dataType: dataType || 'application/json',
headers
}))
},
put (uri, data) {

View File

@ -7,22 +7,10 @@ function executeFileDefinitionCopy (vueInstance, objectType, objectTypeLabel, ob
const progressDialog = vueInstance.$f7.dialog.progress(`Loading ${objectTypeLabel} ${fileFormatLabel} definition...`)
const path = `/rest/file-format/${objectType}s`
let apiCalls = []
if (objectIds !== null) {
apiCalls = objectIds.map((id) => vueInstance.$oh.api.getPlain({
url: path + '/' + id,
headers: { accept: mediaType }
}))
} else {
apiCalls = [vueInstance.$oh.api.getPlain({
url: path,
headers: { accept: mediaType }
})]
}
Promise.all(apiCalls)
.then(definitions => {
const definition = definitions.join('\n')
const headers = { accept: mediaType }
const data = JSON.stringify(objectIds)
vueInstance.$oh.api.postPlain(path, data, 'text', 'application/json', headers)
.then(definition => {
progressDialog.close()
if (vueInstance.$clipboard(definition)) {
vueInstance.$f7.toast.create({
@ -36,7 +24,7 @@ function executeFileDefinitionCopy (vueInstance, objectType, objectTypeLabel, ob
})
.catch(error => {
progressDialog.close()
vueInstance.$f7.dialog.alert(`Error copying ${objectTypeLabel} ${fileFormatLabel} definition: ${error}`, 'Error')
vueInstance.$f7.dialog.alert(`Error loading ${objectTypeLabel} ${fileFormatLabel} definition: ${error}`, 'Error')
})
}

View File

@ -302,10 +302,7 @@ export default {
}
},
copySelected () {
// When _all_ (not just filtered) items are selected, pass null to copyFileDefinitionToClipboard
// so that it only makes one call to the backend
const selectedItems = this.selectedItems.length === this.items.length ? null : this.selectedItems
this.copyFileDefinitionToClipboard(this.ObjectType.ITEM, selectedItems)
this.copyFileDefinitionToClipboard(this.ObjectType.ITEM, this.selectedItems)
},
removeSelected () {
const vm = this