Autonomous click event being blocked (I can't find the reason)

I'll explain.

When I click on A (Zwave) Node in my sidebar, it loads in all its endpoints (from an Ajax get), and what I do - is auto click the first one, after they are loaded.

What is strange, is that the click is being handled with no issue

Here is the code that loads in all Endpoints, and activates the first one.

Runtime.Post('DRIVER', 'getValueDB', [selectedNode.nodeId])
    .then((data) => {
        if (data.callSuccess) {
            data = data.response[0];

            const groupByEP = function(xs) {
                return xs.reduce(function(rv, x) {
                    (rv[x.valueId.endpoint] = rv[x.valueId.endpoint] || []).push(x);
                    return rv;
                }, {});
            };

            const EPGroups = groupByEP(data.values);
            const EPIDs = Object.keys(EPGroups);

            EPIDs.forEach((E) => {
                const EP = E === '0' ? 'Root' : `EP${E}`;
                const Button = $(`<div data-endpoint="${E}">${EP}</div>`);
                Button.click(() => {
                    listCCs(EPGroups[E]);
                    $('#zwjs-endpoint-list > div').removeAttr('selected');
                    $(`#zwjs-endpoint-list > div[data-endpoint="${E}"]`).attr('selected', 'selected');
                });
                $('#zwjs-endpoint-list').append(Button);
            });

            /* >>>> HERE <<<< */
            $('#zwjs-endpoint-list > div[data-endpoint="0"]').trigger('click');
        } else {
            alert(data.response);
        }
    })
    .catch((Error) => {
        alert(Error.message);
    });

Looking at the browser log, and digging into the stack trace, its this line.

$('#zwjs-endpoint-list > div[data-endpoint="0"]').trigger('click');

Now, this does trigger another routine (and it runs fine) - and it does not do anything with media (not even any Ajax calls).

Im struggling to understand why my browser (Yes @TotallyInformation - Safari :wink: ) complains - but before I blame Apple - am I missing something? - as nothing bad is happening.

I do note - the stack is listing the Node RED editor (vendor) a lot - is that itself doing any media things that I am not aware ?

For completeness, here is listCCs
(Open is triggered by interaction, and results in a delayed click elsewhere, and is not called upon - form the Endpoint click)

// EP Selected
	const listCCs = (Collection) => {
		const groupByCC = function (xs) {
			return xs.reduce(function (rv, x) {
				(rv[x.valueId.commandClass] = rv[x.valueId.commandClass] || []).push(x);
				return rv;
			}, {});
		};

		const CCGroups = groupByCC(Collection);
		const CCGroupIDs = Object.keys(CCGroups);

		const Items = [];

		CCGroupIDs.forEach((CCID) => {
			const Name = CCGroups[CCID][0].valueId.commandClassName;
			const Item = {
				element: `<div><span class="zwjs-cc-id">0x${parseInt(CCID).toString(16).padStart(2, '0').toUpperCase()}</span> - ${Name}</div>`,
				children: [],
				parent: true
			};

			CCGroups[CCID].forEach((V) => {
				const getCurrentValue = (value) => {
					let Display;
					if (value !== undefined) {
						if (typeof value === 'object' && !Array.isArray(value)) {
							Display = '(Complex)';
						} else {
							Display = `${value} ${V.metadata.unit || ''}`;
						}
						return `<span class="zwjs-cc-value" id="zwjs-value-${getValueUpdateHash(V.valueId)}" style="padding:1px;float:right;color:rgb(46, 145, 205); min-width:80px">${Display}</span>`;
					} else {
						return '';
					}
				};

				SelectedNodeVIDs[getValueUpdateHash(V.valueId)] = {
					metadata: V.metadata,
					valueId: V.valueId,
					currentValue: V.currentValue
				};

				const sItem = {
					element: `<div style="width:100%; margin-right:30px">${V.metadata.label} ${getCurrentValue(V.currentValue)}</div>`,
					icon: V.metadata.writeable ? 'fa fa-pencil' : '',
					parent: false,
					valueInfo: SelectedNodeVIDs[getValueUpdateHash(V.valueId)]
				};

				Item.children.push(sItem);
			});

			Items.push(Item);
		});

		$('#zwjs-cc-list').treeList('empty');
		$('#zwjs-cc-list').treeList('data', Items);
		$('#zwjs-cc-list').on('treelistselect', function (event, item) {
			if (Object.keys(item).length < 1 || item.parent === true) {
				return;
			}
			CloseTray();
			const Options = {
				width: 700,
				title: 'Value Management',
				buttons: [
					{
						id: 'zwjs-tray-about',
						text: 'About Zwave JS',
						click: function () {
							CloseTray();
						}
					},
					{
						id: 'zwjs-tray-close',
						text: 'Close',
						click: function () {
							CloseTray();
						}
					}
				],
				open: function (tray) {
					const trayBody = tray.find('.red-ui-tray-body, .editor-tray-body');

					let Property;
					if (typeof item.valueInfo.valueId.property === 'number') {
						Property = `0x${parseInt(item.valueInfo.valueId.property).toString(16).padStart(2, '0').toUpperCase()}`;
					} else {
						Property = item.valueInfo.valueId.property;
					}

					if (item.valueInfo.valueId.propertyKey) {
						if (typeof item.valueInfo.valueId.propertyKey === 'number') {
							Property += ` / 0x${parseInt(item.valueInfo.valueId.propertyKey).toString(16).padStart(2, '0').toUpperCase()}`;
						} else {
							Property += ` / ${item.valueInfo.valueId.propertyKey}`;
						}
					}

					const State = {
						ccId: `0x${parseInt(item.valueInfo.valueId.commandClass).toString(16).padStart(2, '0').toUpperCase()}`,
						ccName: item.valueInfo.valueId.commandClassName,
						valueLabel: item.valueInfo.metadata.label,
						nodeId: selectedNode.nodeId,
						property: Property,
						editInfo: {
							valueLabel: item.valueInfo.metadata.label,
							valueId: item.valueInfo.valueId,
							writeable: item.valueInfo.metadata.writeable,
							currentValue: item.valueInfo.currentValue,
							type: item.valueInfo.metadata.type,
							states: item.valueInfo.metadata.states,
							allowManualEntry:
								item.valueInfo.metadata.allowManualEntry !== undefined
									? item.valueInfo.metadata.allowManualEntry
									: item.valueInfo.metadata.writeable
						}
					};

					State.examples = {
						valueLabel: item.valueInfo.metadata.label,
						nocmd: {
							payload: {
								cmd: {
									api: 'VALUE'
								},
								cmdProperties: {
									nodeId: selectedNode.nodeId,
									valueId: { ...item.valueInfo.valueId }
								}
							}
						},
						cmd: {
							topic: selectedNode.nodeId,
							valueId: { ...item.valueInfo.valueId }
						}
					};

					delete State.examples.cmd.valueId.commandClassName;
					delete State.examples.cmd.valueId.propertyName;
					delete State.examples.cmd.valueId.propertyKeyName;
					delete State.examples.nocmd.payload.cmdProperties.valueId.commandClassName;
					delete State.examples.nocmd.payload.cmdProperties.valueId.propertyName;
					delete State.examples.nocmd.payload.cmdProperties.valueId.propertyKeyName;

					if (item.valueInfo.metadata.writeable) {
						State.examples.nocmd.payload.cmd.method = 'setValue | getValue';
						State.examples.nocmd.payload.cmdProperties.value = item.valueInfo.currentValue;
						State.examples.cmd.payload = item.valueInfo.currentValue;
					} else {
						State.examples.nocmd.payload.cmd.method = 'getValue';
					}

					trayBody.append(TPL_ValueManagement(State));

					setTimeout(() => {
						$('.zwjs-tray-menu > div[default]').trigger('click');
					}, 250);
				}
			};
			RED.tray.show(Options);
		});
	};

I still don't know why this was occurring, so decided to not do the click thing. -and the error is gone.
Strange! as I thought unless its causing media, it should be allowed

plus its not happening anywhere else with auto clicks

EPIDs.forEach((E) => {
    const EP = E === '0' ? 'Root' : `EP${E}`;
    const Button = $(`<div data-endpoint="${E}">${EP}</div>`);
    Button.click(() => {
        listCCs(EPGroups[E]);
        $('#zwjs-endpoint-list > div').removeAttr('selected');
        $(`#zwjs-endpoint-list > div[data-endpoint="${E}"]`).attr('selected', 'selected');
    });
    $('#zwjs-endpoint-list').append(Button);
});

//$('#zwjs-endpoint-list > div[data-endpoint="0"]').trigger('click');
listCCs(EPGroups['0'])
$(`#zwjs-endpoint-list > div[data-endpoint="0"]`).attr('selected', 'selected');

Personally, I wouldn't try to fool the browser that way. You never really know when you will fall foul of browsers trapping such things due to security concerns.

I would make the click function a separate function, assign it to the button click but then call it manually to trigger correctly.

1 Like

Yup - thats what I ended up doing - luckily, the "trick-click" (trademark pending :grinning_face_with_smiling_eyes:) - is very rare in my code base, so quite easy to address