/** * 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 = {}; return { addNodeSet: function(id,set) { nodeConfigs[id] = set; nodeList.push(id); }, getNodeSet: function(id) { return nodeConfigs[id]; }, 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"); } 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'; return result; } else { return null; } }, getNodeConstructor: function(type) { return nodeConstructors[type]; }, clear: function() { nodeConfigCache = null; nodeConfigs = {}; nodeList = []; nodeConstructors = {}; } } })(); 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; registry.addNodeSet(id,node); return node; } /** * Loads all palette nodes * @param defaultNodesDir optional parameter, when set, it overrides the default * location of nodeFiles - used by the tests * @return a promise that resolves on completion of loading */ function load(defaultNodesDir) { return when.promise(function(resolve,reject) { // Find all of the nodes to load var nodeFiles; if(defaultNodesDir) { nodeFiles = getNodeFiles(path.resolve(defaultNodesDir)); } else { nodeFiles = getNodeFiles(__dirname+"/../../nodes"); } if (settings.nodesDir) { var dir = settings.nodesDir; if (typeof settings.nodesDir == "string") { dir = [dir]; } for (var i=0;i