Do not rely on the HTML file to identify where nodes are registered from

This commit is contained in:
Nick O'Leary
2016-04-28 11:23:42 +01:00
parent 47316b0fb7
commit 45ff86eae5
5 changed files with 44 additions and 16 deletions

View File

@@ -1,5 +1,5 @@
/**
* Copyright 2013, 2015 IBM Corp.
* Copyright 2013, 2016 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -33,15 +33,25 @@ var settings;
/**
* Registers a node constructor
* @param nodeSet - the nodeSet providing the node (module/set)
* @param type - the string type name
* @param constructor - the constructor function for this node type
* @param opts - optional additional options for the node
*/
function registerType(type,constructor,opts) {
function registerType(nodeSet,type,constructor,opts) {
if (typeof type !== "string") {
// This is someone calling the api directly, rather than via the
// RED object provided to a node. Log a warning
log.warn("Deprecated call to RED.runtime.nodes.registerType - node-set name must be provided as first argument");
opts = constructor;
constructor = type;
type = nodeSet;
nodeSet = "";
}
if (opts && opts.credentials) {
credentials.register(type,opts.credentials);
}
registry.registerType(type,constructor);
registry.registerType(nodeSet,type,constructor);
}
/**

View File

@@ -71,7 +71,10 @@ function createNodeApi(node) {
util: runtime.util,
version: runtime.version,
}
copyObjectProperties(runtime.nodes,red.nodes,["createNode","getNode","eachNode","registerType","addCredentials","getCredentials","deleteCredentials" ]);
copyObjectProperties(runtime.nodes,red.nodes,["createNode","getNode","eachNode","addCredentials","getCredentials","deleteCredentials" ]);
red.nodes.registerType = function(type,constructor,opts) {
runtime.nodes.registerType(node.id,type,constructor,opts);
}
copyObjectProperties(runtime.log,red.log,null,["init"]);
copyObjectProperties(runtime.settings,red.settings,null,["init","load","reset"]);
if (runtime.adminApi) {

View File

@@ -1,5 +1,5 @@
/**
* Copyright 2015 IBM Corp.
* Copyright 2015, 2016 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -347,7 +347,7 @@ function inheritNode(constructor) {
}
}
function registerNodeConstructor(type,constructor) {
function registerNodeConstructor(nodeSet,type,constructor) {
if (nodeConstructors[type]) {
throw new Error(type+" already registered");
}
@@ -358,6 +358,17 @@ function registerNodeConstructor(type,constructor) {
inheritNode(constructor);
}
var nodeSetInfo = getFullNodeInfo(nodeSet);
if (nodeSetInfo) {
if (nodeSetInfo.types.indexOf(type) === -1) {
// A type is being registered for a known set, but for some reason
// we didn't spot it when parsing the HTML file.
// Registered a type is the definitive action - not the presence
// of an edit template. Ensure it is on the list of known types.
nodeSetInfo.types.push(type);
}
}
nodeConstructors[type] = constructor;
events.emit("type-registered",type);
}