UNPKG

webgme-engine

Version:

WebGME server and Client API without a GUI

349 lines (291 loc) 13.9 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JSDoc: Source: common/core/users/crosscuts.js</title> <script src="scripts/prettify/prettify.js"> </script> <script src="scripts/prettify/lang-css.js"> </script> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css"> <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css"> </head> <body> <div id="main"> <h1 class="page-title">Source: common/core/users/crosscuts.js</h1> <section> <article> <pre class="prettyprint source linenums"><code>/* globals define */ /** * @author pmeijer / https://github.com/pmeijer */ define([ 'common/core/constants', 'common/util/guid', 'common/core/CoreIllegalArgumentError', ], function (CORE_CONSTANTS, guid, CoreIllegalArgumentError) { 'use strict'; var CROSSCUT_REGISTRY_NAME = 'CrossCuts'; var CROSSCUT_ID_PREFIX = 'Crosscut_'; var _core = null; /** * Module for handling of crosscuts using the core. &lt;br> * To include in your module (e.g. Plugin) require via 'common/core/users/crosscut' and invoke initialize * by passing a reference to a core instance. * &lt;br> * @example * crosscuts.initialize(core); * crosscuts.getIds(); * @module crosscuts */ var exports = {}; /** * Initializes the module with a core instance. This must be called before any other method. * @param {Core} core - An instance of a Core module. */ exports.initialize = function (core) { _core = core; }; /** * * @param node * @param crosscutId * @param [memberPath] * @private */ function _ensureCrosscutExists(node, crosscutId, memberPath) { var exists = false; if (typeof memberPath === 'string') { // This ensure the crosscut exists. exports.getMemberPaths(node, crosscutId).forEach(function (mPath) { if (memberPath === mPath) { exists = true; } }); if (!exists) { throw new CoreIllegalArgumentError('Member [' + memberPath + '] does not exist in crosscut [' + crosscutId + ']'); } } else { exports.getIds(node).forEach(function (cId) { if (crosscutId === cId) { exists = true; } }); if (!exists) { throw new CoreIllegalArgumentError('Crosscut does not exist [' + crosscutId + ']'); } } } function _ensureInitialized() { if (_core === null) { throw new Error('Crosscut module has not been initialized!'); } } function _getPath(nodeOrPath) { return typeof nodeOrPath === 'string' ? nodeOrPath : _core.getPath(nodeOrPath); } /** * Returns the raw stored crosscut info at the provided node. * @param {module:Core~Node} node * @returns {object[]} */ exports.getInfo = function (node) { _ensureInitialized(); return _core.getRegistry(node, CROSSCUT_REGISTRY_NAME) || []; }; /** * Returns all titles of the crosscuts at the provided node. * @param {module:Core~Node} node - Owner of the crosscuts. * @returns {string[]} */ exports.getTitles = function (node) { return exports.getInfo(node).map(function (cInfo) { return cInfo.title; }); }; /** * Returns all the cross-cut ideas at the provided node. * @param {module:Core~Node} node - Owner of the crosscuts. * @returns {string[]} The crosscut ids. */ exports.getIds = function (node) { return exports.getInfo(node).map(function (cInfo) { return cInfo.SetID; }); }; /** * If title exists and is unique will return the id of the crosscut. If not * it will throw an exception. * @param {module:Core~Node} node - Owner of the crosscut. * @param {string} title * @returns {string} The crosscut id. */ exports.getIdFromTitle = function (node, title) { var id; exports.getInfo(node).forEach(function (cInfo) { if (cInfo.title === title) { if (typeof id === 'string') { throw new CoreIllegalArgumentError('Title [' + title + '] appears in more than one crosscut!'); } else { id = cInfo.SetID; } } }); if (typeof id !== 'string') { throw new CoreIllegalArgumentError('Title [' + title + '] does not exist among crosscuts!'); } return id; }; /** * Returns the paths to all members in the specified crosscut. * @param {module:Core~Node} node - Owner of the crosscut. * @param {string} crosscutId * @returns {string[]} Paths of members */ exports.getMemberPaths = function (node, crosscutId) { _ensureCrosscutExists(node, crosscutId); return _core.getMemberPaths(node, crosscutId); }; /** * Return the position at the cross-cuts for the member in the given crosscut at the provided node. * @param {module:Core~Node} node - Owner of the crosscut. * @param {string} crosscutId - Crosscut id to add member to. * @param {module:Core~Node|string} memberNodeOrPath - Node, or path of, member to get position of. * @returns {object} The position of the member inside the crosscut. */ exports.getMemberPosition = function (node, crosscutId, memberNodeOrPath) { _ensureInitialized(); var memberPath = _getPath(memberNodeOrPath); _ensureCrosscutExists(node, crosscutId, memberPath); return _core.getMemberRegistry(node, crosscutId, memberPath, 'position') || {x: 100, y: 100}; }; /** * Adds a new crosscut to the node. * @param {module:Core~Node} node - Owner of the new crosscut. * @param {string} title - Visible title of crosscut. * @param {number} [order] - Tab order starting from 0, if not given will be placed at end. * @returns {string} The id of the newly created crosscut. */ exports.createCrosscut = function (node, title, order) { var id = CROSSCUT_ID_PREFIX + guid(); var cInfo = exports.getInfo(node); if (typeof order === 'number') { if (order &lt; 0) { throw new CoreIllegalArgumentError('Provided order must be >= 0'); } if (cInfo.length &lt; order) { throw new CoreIllegalArgumentError('Provided order is greater than the largest possible index ' + cInfo.length + '.'); } cInfo.forEach(function (entry) { if (entry.order >= order) { entry.order += 1; } }); } else { order = cInfo.length; } cInfo.push({ SetID: id, order: order, title: title, }); _core.createSet(node, id); cInfo.sort(function (a, b) { if (a.order &lt; b.order) { return -1; } else { return 1; } }); _core.setRegistry(node, CROSSCUT_REGISTRY_NAME, cInfo); return id; }; /** * Updates the position of the member inside the specified crosscut. * @param {module:Core~Node} node - Owner of the crosscut. * @param {string} crosscutId - Id of crosscut. * @param {module:Core~Node|string} memberNodeOrPath - Node, or path of, member to update position for. * @param {object} newPosition - Position of the member inside the crosscut. */ exports.setMemberPosition = function (node, crosscutId, memberNodeOrPath, newPosition) { _ensureInitialized(); var memberPath = _getPath(memberNodeOrPath); _ensureCrosscutExists(node, crosscutId, memberPath); _core.setMemberRegistry(node, crosscutId, memberPath, 'position', newPosition); }; /** * Adds a member to the crosscut at an optionally provided position. * @param {module:Core~Node} node - Owner of the crosscut. * @param {string} crosscutId - Crosscut id to add member to. * @param {module:Core~Node} memberNode - Node that should be added to crosscut. * @param {object} [position={x:100, y:100}] - Position of the member inside crosscut. */ exports.addMember = function (node, crosscutId, memberNode, position) { _ensureCrosscutExists(node, crosscutId); position = position || {x: 100, y: 100}; _core.addMember(node, crosscutId, memberNode); exports.setMemberPosition(node, crosscutId, memberNode, position); }; /** * Deletes the crosscut from the node. * @param {module:Core~Node} node - Owner of the crosscut. * @param {string} crosscutId - Id of crosscut to delete. */ exports.deleteCrosscut = function (node, crosscutId) { var cInfo = exports.getInfo(node); var entryOrder; _ensureCrosscutExists(node, crosscutId); cInfo = cInfo.filter(function (entry) { if (entry.SetID === crosscutId) { entryOrder = entry.order; return false; } return true; }); cInfo.forEach(function (entry) { if (entry.order > entryOrder) { entry.order -= 1; } }); cInfo.sort(function (a, b) { if (a.order &lt; b.order) { return -1; } else { return 1; } }); _core.delSet(node, crosscutId); _core.setRegistry(node, CROSSCUT_REGISTRY_NAME, cInfo); }; /** * Removes the member from the specified crosscut. * @param {module:Core~Node} node - Owner of the crosscut. * @param {string} crosscutId - Id of crosscut. * @param {module:Core~Node|string} memberNodeOrPath - Node, or path of, member to delete from crosscut. */ exports.delMember = function (node, crosscutId, memberNodeOrPath) { _ensureInitialized(); var memberPath = _getPath(memberNodeOrPath); _ensureCrosscutExists(node, crosscutId, memberPath); _core.delMember(node, crosscutId, memberPath); }; return exports; }); </code></pre> </article> </section> </div> <nav> <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="Server_GMEAuth.html">Server:GMEAuth</a></li><li><a href="Server_SafeStorage.html">Server:SafeStorage</a></li><li><a href="Server_UserProject.html">Server:UserProject</a></li><li><a href="module-Core.html">Core</a></li><li><a href="module-Storage.html">Storage</a></li><li><a href="module-crosscuts.html">crosscuts</a></li><li><a href="module-serialization.html">serialization</a></li></ul><h3>Externals</h3><ul><li><a href="external-Promise.html">Promise</a></li></ul><h3>Classes</h3><ul><li><a href="AddOnBase.html">AddOnBase</a></li><li><a href="AddOnUpdateResult.html">AddOnUpdateResult</a></li><li><a href="Artifact.html">Artifact</a></li><li><a href="BlobClient.html">BlobClient</a></li><li><a href="BlobMetadata.html">BlobMetadata</a></li><li><a href="BlobRunPluginClient.html">BlobRunPluginClient</a></li><li><a href="Client.html">Client</a></li><li><a href="Core.html">Core</a></li><li><a href="ExecutorClient.html">ExecutorClient</a></li><li><a href="GMENode.html">GMENode</a></li><li><a href="GmeLogger.html">GmeLogger</a></li><li><a href="InterPluginResult.html">InterPluginResult</a></li><li><a href="JobInfo.html">JobInfo</a></li><li><a href="OutputInfo.html">OutputInfo</a></li><li><a href="PluginBase.html">PluginBase</a></li><li><a href="PluginConfig.html">PluginConfig</a></li><li><a href="PluginMessage.html">PluginMessage</a></li><li><a href="PluginNodeDescription.html">PluginNodeDescription</a></li><li><a href="PluginResult.html">PluginResult</a></li><li><a href="Project.html">Project</a></li><li><a href="ProjectInterface.html">ProjectInterface</a></li><li><a href="Server_GMEAuth-GMEAuth.html">GMEAuth</a></li><li><a href="Server_SafeStorage-SafeStorage.html">SafeStorage</a></li><li><a href="Server_UserProject-UserProject.html">UserProject</a></li><li><a href="WebsocketRouter.html">WebsocketRouter</a></li><li><a href="WebsocketRouterUser.html">WebsocketRouterUser</a></li></ul><h3>Events</h3><ul><li><a href="Client.html#event:BRANCH_CHANGED">BRANCH_CHANGED</a></li><li><a href="Client.html#event:BRANCH_CLOSED">BRANCH_CLOSED</a></li><li><a href="Client.html#event:BRANCH_OPENED">BRANCH_OPENED</a></li><li><a href="Client.html#event:BRANCH_STATUS_CHANGED">BRANCH_STATUS_CHANGED</a></li><li><a href="Client.html#event:CONNECTED_USERS_CHANGED">CONNECTED_USERS_CHANGED</a></li><li><a href="Client.html#event:NETWORK_STATUS_CHANGED">NETWORK_STATUS_CHANGED</a></li><li><a href="Client.html#event:NOTIFICATION">NOTIFICATION</a></li><li><a href="Client.html#event:PLUGIN_FINISHED">PLUGIN_FINISHED</a></li><li><a href="Client.html#event:PLUGIN_INITIATED">PLUGIN_INITIATED</a></li><li><a href="Client.html#event:PLUGIN_NOTIFICATION">PLUGIN_NOTIFICATION</a></li><li><a href="Client.html#event:PROJECT_CLOSED">PROJECT_CLOSED</a></li><li><a href="Client.html#event:PROJECT_OPENED">PROJECT_OPENED</a></li></ul><h3><a href="global.html">Global</a></h3> </nav> <br class="clear"> <footer> Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Fri Jun 21 2024 09:43:40 GMT-0400 (Eastern Daylight Time) </footer> <script> prettyPrint(); </script> <script src="scripts/linenumber.js"> </script> </body> </html>