Skip to content

Commit

Permalink
Track connected uib node by ID not just url to better handle uib rena…
Browse files Browse the repository at this point in the history
…mes.
  • Loading branch information
TotallyInformation committed Mar 11, 2023
1 parent 09fce63 commit d25e562
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 9 deletions.
59 changes: 51 additions & 8 deletions src/editor/uib-sender/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
/** Node's background color @constant {string} paletteColor */
const paletteColor = '#E6E0F8'

/** Copy of all deployed uibuilder node instances */
let uibInstances = null

/** Get all of the current uibuilder URL's */
function getUrls() {
$.ajax({
Expand All @@ -25,12 +28,15 @@
'cmd': 'listinstances',
},
success: function(instances) {
// console.log('>>>>', instances)
console.log('>> Instances >>', instances, Object.entries(instances) )

uibInstances = instances

Object.keys(instances).forEach( (val, i, arr) => {
Object.keys(instances).forEach( (key, i, arr) => {
$('#node-input-url').append($('<option>', {
value: instances[val],
text: instances[val],
value: instances[key],
text: instances[key],
'data-id': key,
}))
})

Expand All @@ -43,6 +49,8 @@
* @param {*} node A node instance as seen from the Node-RED Editor
*/
function onEditPrepare(node) {
console.log('uib-sender onEditPrepare ', node)

// initial checkbox states
if (!node.passthrough) node.passthrough = false
$('#node-input-passthrough')
Expand Down Expand Up @@ -73,23 +81,55 @@

// Deal with the url
getUrls()
// $('#node-input-url')
// .on('change', function() {
// console.log('>>>>', this.value)
// })

if ( node.uibId in uibInstances ) {
// console.log( 'uibuilder node ID is known', node.url, node.uibId)
// We know the ID, always look up the latest name
const chkUrl = uibInstances[node.uibId]
if (chkUrl !== node.url) { // The url for this node changed so force a re-deploy
RED.nodes.dirty(true)
node.changed = true
}
node.url = uibInstances[node.uibId]
} else if ( Object.values(uibInstances).includes(node.url) ) {
// console.log( 'We didnt know the id but we found the url', node.url, node.uibId, Object.keys(uibInstances)[Object.values(uibInstances).indexOf(node.url)])
// We didn't know the ID but we know the last url so set the ID - always force a redeploy in this case
node.uibId = uibInstances[Object.keys(uibInstances)[Object.values(uibInstances).indexOf(node.url)]]
RED.nodes.dirty(true)
node.changed = true
} else {
// console.log( 'Neither id nor url found', node.url, node.uibId)
node.valid = false
}

$('#node-input-url')
.on('change', function() {
node.uibId = Object.keys(uibInstances)[Object.values(uibInstances).indexOf(this.value)]
// console.log('>> URL Change >>', this.value, node.uibId, Object.keys(uibInstances)[Object.values(uibInstances).indexOf(this.value)])
})

if ( node.url && node.url.length > 0 ) {
$(`#node-input-url option[value="${node.url}"]`).prop('selected', true)
$('#node-input-url').val(node.url)
}

} // ----- end of onEditPrepare() ----- //

/** When node type is added to the palette
* Which happens on load of the Editor
* @param {*} node -
*/
function onPaletteAdd(node) {
// console.log('uib-sender onPaletteAdd ', node)
} // ---- End of onPaletteAdd ---- //

// @ts-ignore
RED.nodes.registerType(moduleName, {
category: paletteCategory,
color: paletteColor,
defaults: {
url: { value: '', required: true },
uibId: { value: '' }, // ID of selected uibuilder instance
name: { value: '' },
topic: { value: '' },
passthrough: { value: false },
Expand All @@ -107,6 +147,9 @@

oneditprepare: function() { onEditPrepare(this) },

/** When this node is added to the palette - happens on (re)load of Editor as well */
onpaletteadd: function() { onPaletteAdd(this) },

}) // ---- End of registerType() ---- //

}())
2 changes: 1 addition & 1 deletion src/editor/uib-sender/panel.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div aria-label="uibuilder URL to associate with" class="form-row" title="uibuilder URL to associate with">
<label for="node-input-url"><i class="fa fa-globe"></i> URL</label>
<select id="node-input-url">
<option value="">--choose a uibuilder instance--</option>
<option value="" name="">--choose a uibuilder instance--</option>
</select>
</div>

Expand Down
1 change: 1 addition & 0 deletions typedefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@
* passthrough: boolean;
* return: boolean;
* url: string;
* uibId: string;
* }} senderNode1
*/

Expand Down

0 comments on commit d25e562

Please sign in to comment.