From ac8270603cd9a63de6d32d11c95043d29e07e8bf Mon Sep 17 00:00:00 2001 From: canbax Date: Fri, 11 Feb 2022 15:38:22 +0300 Subject: [PATCH] #47 optional ignored elements --- README.md | 3 ++- cytoscape-grid-guide.js | 56 ++++++++++++++++++++++++++++++++++------- src/alignment.js | 4 ++- src/helper.js | 14 +++++++++++ src/index.js | 3 ++- src/resize.js | 10 ++++++++ src/snap_during_drag.js | 5 +++- src/snap_on_release.js | 6 +++++ 8 files changed, 88 insertions(+), 13 deletions(-) create mode 100644 src/helper.js diff --git a/README.md b/README.md index b89e855..9633d0d 100644 --- a/README.md +++ b/README.md @@ -113,7 +113,8 @@ var options = { }, // Parent Padding - parentSpacing: -1 // -1 to set paddings of parents to gridSpacing + parentSpacing: -1, // -1 to set paddings of parents to gridSpacing + ignoredElems: '' // a cytoscape.js selector (string) or a cytoscape.js collection }; ``` diff --git a/cytoscape-grid-guide.js b/cytoscape-grid-guide.js index c3352ac..571e103 100644 --- a/cytoscape-grid-guide.js +++ b/cytoscape-grid-guide.js @@ -996,12 +996,14 @@ function createRBTree(compare) { return new RedBlackTree(compare || defaultCompare, null) } },{}],2:[function(require,module,exports){ +const h = require("./helper"); + module.exports = function (cytoscape, cy, apiRegistered) { // Needed because parent nodes cannot be moved in Cytoscape.js < v3.2 function moveTopDown(node, dx, dy) { var nodes = node.union(node.descendants()); - + nodes = h.removeIgnored(nodes); nodes.filter(":childless").positions(function (node, i) { if(typeof node === "number") { node = i; @@ -1151,7 +1153,7 @@ module.exports = function (cytoscape, cy, apiRegistered) { }; -},{}],3:[function(require,module,exports){ +},{"./helper":8}],3:[function(require,module,exports){ var debounce = (function(){ /** @@ -2805,6 +2807,22 @@ module.exports = function (opts, cy, debounce) { }; },{"functional-red-black-tree":1}],8:[function(require,module,exports){ +/** + * @param {} elems collection of cytoscape elements + * returns the new collection that does not contain ignored + */ +module.exports = { + removeIgnored: (elems) => { + const cy = elems.cy(); + const ignored = cy.scratch("_gridGuide").options.ignoredElems; + if (!ignored) { + return elems; + } + return elems.not(ignored); + }, +}; + +},{}],9:[function(require,module,exports){ ;(function(){ 'use strict'; // registers the extension on a cytoscape lib ref @@ -2859,7 +2877,8 @@ module.exports = function (opts, cy, debounce) { }, // Parent Padding - parentSpacing: -1 // -1 to set paddings of parents to gridSpacing + parentSpacing: -1, // -1 to set paddings of parents to gridSpacing + ignoredElems: '' // a cytoscape.js selector (string) or a cytoscape.js collection }; var _snapOnRelease = require("./snap_on_release"); var _snapToGridDuringDrag = require("./snap_during_drag"); @@ -2941,7 +2960,7 @@ module.exports = function (opts, cy, debounce) { })(); -},{"./alignment":2,"./debounce":3,"./draw_grid":4,"./events_controller":5,"./extend":6,"./guidelines":7,"./parentPadding":9,"./resize":10,"./snap_during_drag":11,"./snap_on_release":12}],9:[function(require,module,exports){ +},{"./alignment":2,"./debounce":3,"./draw_grid":4,"./events_controller":5,"./extend":6,"./guidelines":7,"./parentPadding":10,"./resize":11,"./snap_during_drag":12,"./snap_on_release":13}],10:[function(require,module,exports){ module.exports = function (opts, cy) { var options = opts; @@ -2978,7 +2997,9 @@ module.exports = function (opts, cy) { setPaddingOfParent: setPaddingOfParent }; }; -},{}],10:[function(require,module,exports){ +},{}],11:[function(require,module,exports){ +const h = require("./helper"); + module.exports = function (gridSpacing) { @@ -2994,6 +3015,10 @@ module.exports = function (gridSpacing) { }; function resizeNode(node) { + node = h.removeIgnored(node); + if (node.length < 1) { + return; + } var width = node.width(); var height = node.height(); @@ -3015,6 +3040,10 @@ module.exports = function (gridSpacing) { } function recoverNodeDimensions(node) { + node = h.removeIgnored(node); + if (node.length < 1) { + return; + } var oldSizes = getScratch(node).resize; if (oldSizes) node.style({ @@ -3033,7 +3062,9 @@ module.exports = function (gridSpacing) { }; }; -},{}],11:[function(require,module,exports){ +},{"./helper":8}],12:[function(require,module,exports){ +const h = require("./helper"); + module.exports = function (cy, snap) { var snapToGridDuringDrag = {}; @@ -3056,6 +3087,7 @@ module.exports = function (cy, snap) { draggedNodes = e.cy.$(":selected"); else draggedNodes = cyTarget; + draggedNodes = h.removeIgnored( draggedNodes); startPos = e.position || e.cyPosition; @@ -3092,7 +3124,7 @@ module.exports = function (cy, snap) { if (dist.x != 0 || dist.y != 0) { attachedNode.unlock(); var nodes = draggedNodes.union(draggedNodes.descendants()); - + nodes = h.removeIgnored(nodes); nodes.filter(":childless").positions(function (node, i) { if(typeof node === "number") { node = i; @@ -3116,7 +3148,9 @@ module.exports = function (cy, snap) { }; -},{}],12:[function(require,module,exports){ +},{"./helper":8}],13:[function(require,module,exports){ +const h = require("./helper"); + module.exports = function (cy, gridSpacing, gridSpacingOffset) { var snap = { }; @@ -3149,10 +3183,12 @@ module.exports = function (cy, gridSpacing, gridSpacingOffset) { var pos = node.position(); var newPos = snap.snapPos(pos); + node = h.removeIgnored(node); node.position(newPos); }; snap.snapNodesTopDown = function (nodes) { + nodes = h.removeIgnored(nodes); // getTOpMostNodes -> nodes cy.startBatch(); nodes.union(nodes.descendants()).filter(":childless").positions(function (node, i) { @@ -3173,12 +3209,14 @@ module.exports = function (cy, gridSpacing, gridSpacingOffset) { else nodes = cyTarget; + nodes = h.removeIgnored(nodes); snap.snapNodesTopDown(nodes); }; snap.recoverSnapNode = function (node) { + node = h.removeIgnored(node); var snapScratch = getScratch(node).snap; if (snapScratch) { node.position(snapScratch.oldPos); @@ -3193,4 +3231,4 @@ module.exports = function (cy, gridSpacing, gridSpacingOffset) { }; -},{}]},{},[8]); +},{"./helper":8}]},{},[9]); diff --git a/src/alignment.js b/src/alignment.js index dc23364..b393359 100644 --- a/src/alignment.js +++ b/src/alignment.js @@ -1,9 +1,11 @@ +const h = require("./helper"); + module.exports = function (cytoscape, cy, apiRegistered) { // Needed because parent nodes cannot be moved in Cytoscape.js < v3.2 function moveTopDown(node, dx, dy) { var nodes = node.union(node.descendants()); - + nodes = h.removeIgnored(nodes); nodes.filter(":childless").positions(function (node, i) { if(typeof node === "number") { node = i; diff --git a/src/helper.js b/src/helper.js new file mode 100644 index 0000000..eae4985 --- /dev/null +++ b/src/helper.js @@ -0,0 +1,14 @@ +/** + * @param {} elems collection of cytoscape elements + * returns the new collection that does not contain ignored + */ +module.exports = { + removeIgnored: (elems) => { + const cy = elems.cy(); + const ignored = cy.scratch("_gridGuide").options.ignoredElems; + if (!ignored) { + return elems; + } + return elems.not(ignored); + }, +}; diff --git a/src/index.js b/src/index.js index 81474f6..8cd66a6 100644 --- a/src/index.js +++ b/src/index.js @@ -52,7 +52,8 @@ }, // Parent Padding - parentSpacing: -1 // -1 to set paddings of parents to gridSpacing + parentSpacing: -1, // -1 to set paddings of parents to gridSpacing + ignoredElems: '' // a cytoscape.js selector (string) or a cytoscape.js collection }; var _snapOnRelease = require("./snap_on_release"); var _snapToGridDuringDrag = require("./snap_during_drag"); diff --git a/src/resize.js b/src/resize.js index 1a2af65..ab9b9d7 100644 --- a/src/resize.js +++ b/src/resize.js @@ -1,3 +1,5 @@ +const h = require("./helper"); + module.exports = function (gridSpacing) { @@ -13,6 +15,10 @@ module.exports = function (gridSpacing) { }; function resizeNode(node) { + node = h.removeIgnored(node); + if (node.length < 1) { + return; + } var width = node.width(); var height = node.height(); @@ -34,6 +40,10 @@ module.exports = function (gridSpacing) { } function recoverNodeDimensions(node) { + node = h.removeIgnored(node); + if (node.length < 1) { + return; + } var oldSizes = getScratch(node).resize; if (oldSizes) node.style({ diff --git a/src/snap_during_drag.js b/src/snap_during_drag.js index b97b4a3..66d13b4 100644 --- a/src/snap_during_drag.js +++ b/src/snap_during_drag.js @@ -1,3 +1,5 @@ +const h = require("./helper"); + module.exports = function (cy, snap) { var snapToGridDuringDrag = {}; @@ -20,6 +22,7 @@ module.exports = function (cy, snap) { draggedNodes = e.cy.$(":selected"); else draggedNodes = cyTarget; + draggedNodes = h.removeIgnored( draggedNodes); startPos = e.position || e.cyPosition; @@ -56,7 +59,7 @@ module.exports = function (cy, snap) { if (dist.x != 0 || dist.y != 0) { attachedNode.unlock(); var nodes = draggedNodes.union(draggedNodes.descendants()); - + nodes = h.removeIgnored(nodes); nodes.filter(":childless").positions(function (node, i) { if(typeof node === "number") { node = i; diff --git a/src/snap_on_release.js b/src/snap_on_release.js index 08bcbf2..ca3e5e8 100644 --- a/src/snap_on_release.js +++ b/src/snap_on_release.js @@ -1,3 +1,5 @@ +const h = require("./helper"); + module.exports = function (cy, gridSpacing, gridSpacingOffset) { var snap = { }; @@ -30,10 +32,12 @@ module.exports = function (cy, gridSpacing, gridSpacingOffset) { var pos = node.position(); var newPos = snap.snapPos(pos); + node = h.removeIgnored(node); node.position(newPos); }; snap.snapNodesTopDown = function (nodes) { + nodes = h.removeIgnored(nodes); // getTOpMostNodes -> nodes cy.startBatch(); nodes.union(nodes.descendants()).filter(":childless").positions(function (node, i) { @@ -54,12 +58,14 @@ module.exports = function (cy, gridSpacing, gridSpacingOffset) { else nodes = cyTarget; + nodes = h.removeIgnored(nodes); snap.snapNodesTopDown(nodes); }; snap.recoverSnapNode = function (node) { + node = h.removeIgnored(node); var snapScratch = getScratch(node).snap; if (snapScratch) { node.position(snapScratch.oldPos);