/** * Copyright 2014 IBM Corp. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. **/ var util = require("util"); var when = require("when"); var whenNode = require('when/node'); var fs = require("fs"); var path = require("path"); var crypto = require("crypto"); var cheerio = require("cheerio"); var UglifyJS = require("uglify-js"); var events = require("../events"); var Node; var settings; function filterNodeInfo(n) { var r = { id: n.id, types: n.types, name: n.name, enabled: n.enabled } if (n.err) { r.err = n.err.toString(); } return r; } var registry = (function() { var nodeConfigCache = null; var nodeConfigs = {}; var nodeList = []; var nodeConstructors = {}; var nodeTypeToId = {}; return { addNodeSet: function(id,set) { if (!set.err) { set.types.forEach(function(t) { nodeTypeToId[t] = id; }); } nodeConfigs[id] = set; nodeList.push(id); nodeConfigCache = null; }, removeNode: function(id) { var config = nodeConfigs[id]; if (config) { delete nodeConfigs[id]; var i = nodeList.indexOf(id); if (i > -1) { nodeList.splice(i,1); } config.types.forEach(function(t) { delete nodeConstructors[t]; delete nodeTypeToId[t]; }); nodeConfigCache = null; } return filterNodeInfo(config); }, getNodeInfo: function(typeOrId) { if (nodeTypeToId[typeOrId]) { return filterNodeInfo(nodeConfigs[nodeTypeToId[typeOrId]]); } else if (nodeConfigs[typeOrId]) { return filterNodeInfo(nodeConfigs[typeOrId]); } return null; }, getNodeList: function() { return nodeList.map(function(id) { var n = nodeConfigs[id]; return filterNodeInfo(n); }); }, registerNodeConstructor: function(type,constructor) { if (nodeConstructors[type]) { throw new Error(type+" already registered"); } //TODO: Ensure type is known - but doing so will break some tests // that don't have a way to register a node template ahead // of registering the constructor util.inherits(constructor,Node); nodeConstructors[type] = constructor; events.emit("type-registered",type); }, /** * Gets all of the node template configs * @return all of the node templates in a single string */ getAllNodeConfigs: function() { if (!nodeConfigCache) { var result = ""; var script = ""; for (var i=0;i 0) { result += ''; } nodeConfigCache = result; } return nodeConfigCache; }, getNodeConfig: function(id) { var config = nodeConfigs[id]; if (config) { var result = config.config; result += ''; return result; } else { return null; } }, getNodeConstructor: function(type) { var config = nodeConfigs[nodeTypeToId[type]]; if (!config || config.enabled) { return nodeConstructors[type]; } return null; }, clear: function() { nodeConfigCache = null; nodeConfigs = {}; nodeList = []; nodeConstructors = {}; nodeTypeToId = {}; }, getTypeId: function(type) { return nodeTypeToId[type]; }, enableNodeSet: function(id) { var config = nodeConfigs[id]; if (config) { if (config.err) { throw new Error("cannot enable node with error"); } config.enabled = true; nodeConfigCache = null; } }, disableNodeSet: function(id) { var config = nodeConfigs[id]; if (config) { config.enabled = false; nodeConfigCache = null; } } } })(); function init(_settings) { Node = require("./Node"); settings = _settings; } /** * Synchronously walks the directory looking for node files. * Emits 'node-icon-dir' events for an icon dirs found * @param dir the directory to search * @return an array of fully-qualified paths to .js files */ function getNodeFiles(dir) { var result = []; var files = []; try { files = fs.readdirSync(dir); } catch(err) { return result; } files.sort(); files.forEach(function(fn) { var stats = fs.statSync(path.join(dir,fn)); if (stats.isFile()) { if (/\.js$/.test(fn)) { var valid = true; if (settings.nodesExcludes) { for (var i=0;i"; for (var j in el.attribs) { if (el.attribs.hasOwnProperty(j)) { openTag += " "+j+'="'+el.attribs[j]+'"'; } } openTag += ">"; template += openTag+$(el).text()+closeTag; } }); node.types = types; node.config = template; node.script = script; for (var i=0;i