Skip to content

Commit

Permalink
Fix empty folder for prepend (#757)
Browse files Browse the repository at this point in the history
* Move isNodeRecordWithChildren to nodeUtils; add test

* Add test

* Extract loadChildrenFromData

* Set isEmptyFolder to true when the data contains children: []

* Build

* Lint

* Changelog
  • Loading branch information
mbraak authored Oct 21, 2023
1 parent 05711c0 commit a877f57
Show file tree
Hide file tree
Showing 13 changed files with 281 additions and 141 deletions.
1 change: 1 addition & 0 deletions docs/_entries/general/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ name: changelog
#### Development version

- Issue #734: fix autoscroll issue (thanks to WriterStat)
- issue #736: fix empty folder for prepend

#### 1.7.4 (september 24 2023)

Expand Down
55 changes: 25 additions & 30 deletions lib/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getPositionName = exports.getPosition = exports.Position = exports.Node = void 0;
var _nodeUtils = require("./nodeUtils");
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
var Position = exports.Position = /*#__PURE__*/function (Position) {
Position[Position["Before"] = 1] = "Before";
Position[Position["After"] = 2] = "After";
Expand All @@ -40,12 +41,9 @@ var getPositionName = exports.getPositionName = function getPositionName(positio
var getPosition = exports.getPosition = function getPosition(name) {
return positionNames[name];
};
var isNodeRecordWithChildren = function isNodeRecordWithChildren(data) {
return _typeof(data) === "object" && "children" in data && data["children"] instanceof Array;
};
var Node = exports.Node = /*#__PURE__*/function () {
function Node() {
var o = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
var nodeData = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
var isRoot = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
var nodeClass = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : Node;
_classCallCheck(this, Node);
Expand All @@ -62,9 +60,9 @@ var Node = exports.Node = /*#__PURE__*/function () {
_defineProperty(this, "is_loading", void 0);
_defineProperty(this, "isEmptyFolder", void 0);
this.name = "";
this.isEmptyFolder = false;
this.load_on_demand = false;
this.setData(o);
this.isEmptyFolder = nodeData != null && (0, _nodeUtils.isNodeRecordWithChildren)(nodeData) && nodeData.children.length === 0;
this.setData(nodeData);
this.children = [];
this.parent = null;
if (isRoot) {
Expand All @@ -77,9 +75,9 @@ var Node = exports.Node = /*#__PURE__*/function () {
/*
Set the data of this node.
setData(string): set the name of the node
setdata(object): set attributes of the node
setData(object): set attributes of the node
Examples:
setdata('node1')
setData('node1')
setData({ name: 'node1', id: 1});
setData({ name: 'node2', id: 2, color: 'green'});
* This is an internal function; it is not in the docs
Expand Down Expand Up @@ -134,15 +132,11 @@ var Node = exports.Node = /*#__PURE__*/function () {
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var o = _step.value;
var _node = this.createNode(o);
var childData = _step.value;
var _node = this.createNode(childData);
this.addChild(_node);
if (isNodeRecordWithChildren(o)) {
if (o.children.length === 0) {
_node.isEmptyFolder = true;
} else {
_node.loadFromData(o.children);
}
if ((0, _nodeUtils.isNodeRecordWithChildren)(childData)) {
_node.loadFromData(childData.children);
}
}
} catch (err) {
Expand Down Expand Up @@ -371,9 +365,7 @@ var Node = exports.Node = /*#__PURE__*/function () {
var _node2 = this.createNode(nodeInfo);
var childIndex = this.parent.getChildIndex(this);
this.parent.addChildAtPosition(_node2, childIndex + 1);
if (isNodeRecordWithChildren(nodeInfo) && nodeInfo.children.length) {
_node2.loadFromData(nodeInfo.children);
}
_node2.loadChildrenFromData(nodeInfo);
return _node2;
}
}
Expand All @@ -386,9 +378,7 @@ var Node = exports.Node = /*#__PURE__*/function () {
var _node3 = this.createNode(nodeInfo);
var childIndex = this.parent.getChildIndex(this);
this.parent.addChildAtPosition(_node3, childIndex);
if (isNodeRecordWithChildren(nodeInfo) && nodeInfo.children.length) {
_node3.loadFromData(nodeInfo.children);
}
_node3.loadChildrenFromData(nodeInfo);
return _node3;
}
}
Expand Down Expand Up @@ -433,19 +423,15 @@ var Node = exports.Node = /*#__PURE__*/function () {
value: function append(nodeInfo) {
var node = this.createNode(nodeInfo);
this.addChild(node);
if (isNodeRecordWithChildren(nodeInfo) && nodeInfo.children.length) {
node.loadFromData(nodeInfo.children);
}
node.loadChildrenFromData(nodeInfo);
return node;
}
}, {
key: "prepend",
value: function prepend(nodeInfo) {
var node = this.createNode(nodeInfo);
this.addChildAtPosition(node, 0);
if (isNodeRecordWithChildren(nodeInfo) && nodeInfo.children.length) {
node.loadFromData(nodeInfo.children);
}
node.loadChildrenFromData(nodeInfo);
return node;
}
}, {
Expand Down Expand Up @@ -659,7 +645,7 @@ var Node = exports.Node = /*#__PURE__*/function () {
var _this2 = this;
var addNode = function addNode(nodeData) {
_this2.setData(nodeData);
if (isNodeRecordWithChildren(nodeData) && nodeData.children.length) {
if ((0, _nodeUtils.isNodeRecordWithChildren)(nodeData) && nodeData.children.length) {
addChildren(nodeData.children);
}
};
Expand Down Expand Up @@ -708,6 +694,15 @@ var Node = exports.Node = /*#__PURE__*/function () {
var nodeClass = this.getNodeClass();
return new nodeClass(nodeData);
}

// Load children data from nodeInfo if it has children
}, {
key: "loadChildrenFromData",
value: function loadChildrenFromData(nodeInfo) {
if ((0, _nodeUtils.isNodeRecordWithChildren)(nodeInfo) && nodeInfo.children.length) {
this.loadFromData(nodeInfo.children);
}
}
}]);
return Node;
}();
10 changes: 10 additions & 0 deletions lib/nodeUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.isNodeRecordWithChildren = void 0;
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
var isNodeRecordWithChildren = exports.isNodeRecordWithChildren = function isNodeRecordWithChildren(data) {
return _typeof(data) === "object" && "children" in data && data["children"] instanceof Array;
};
56 changes: 49 additions & 7 deletions lib/test/node.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,9 @@ describe("constructor", function () {
});
expect(given.node).not.toHaveProperty("id");
});
it("sets isEmptyFolder to false", function () {
expect(given.node.isEmptyFolder).toBe(false);
});
});
context("with an object with a name property", function () {
given("params", function () {
Expand Down Expand Up @@ -375,7 +378,6 @@ describe("constructor", function () {
name: "n1",
url: "/"
});
// todo: match object?
expect(given.node).not.toHaveProperty("label");
expect(given.node.children).toHaveLength(0);
expect(given.node.parent).toBeNull();
Expand All @@ -401,10 +403,23 @@ describe("constructor", function () {
};
});
it("doesn't set the children", function () {
// todo: match object?
expect(given.node.name).toBe("n1");
expect(given.node.children).toHaveLength(0);
});
it("sets isEmptyFolder to false", function () {
expect(given.node.isEmptyFolder).toBe(false);
});
});
context("when the data contains an empty children attribute", function () {
given("params", function () {
return {
name: "n1",
children: []
};
});
it("sets isEmptyFolder to true", function () {
expect(given.node.isEmptyFolder).toBe(true);
});
});
});
describe("getChildIndex", function () {
Expand Down Expand Up @@ -942,12 +957,9 @@ describe("iterate", function () {
});
});
describe("loadFromData", function () {
var given = (0, _givens["default"])();
given("tree", function () {
return new _node.Node().loadFromData(_exampleData["default"]);
});
it("creates a tree", function () {
expect(given.tree.children).toEqual([expect.objectContaining({
var tree = new _node.Node().loadFromData(_exampleData["default"]);
expect(tree.children).toEqual([expect.objectContaining({
id: 123,
intProperty: 1,
name: "node1",
Expand All @@ -970,6 +982,29 @@ describe("loadFromData", function () {
})]
})]);
});
it("sets isEmptyFolder to true for a node when it is has an empty children attribute", function () {
var data = [{
name: "test1",
children: []
}];
var tree = new _node.Node().loadFromData(data);
expect(tree.children[0].isEmptyFolder).toBe(true);
});
it("sets isEmptyFolder to false for a node when it doesn't have a children attribute", function () {
var data = [{
name: "test1"
}];
var tree = new _node.Node().loadFromData(data);
expect(tree.children[0].isEmptyFolder).toBe(false);
});
it("sets isEmptyFolder to false for a node when it has a children attribute that is not empty", function () {
var data = [{
name: "test1",
children: ["child1"]
}];
var tree = new _node.Node().loadFromData(data);
expect(tree.children[0].isEmptyFolder).toBe(false);
});
});
describe("moveNode", function () {
var given = (0, _givens["default"])();
Expand Down Expand Up @@ -1113,6 +1148,13 @@ describe("prepend", function () {
});
});
});
it("sets the isEmptyFolder attribute to true when the node data has empty children", function () {
given.node.prepend({
name: "test1",
children: []
});
expect(given.node.children[0].isEmptyFolder).toBe(true);
});
});
describe("remove", function () {
var given = (0, _givens["default"])();
Expand Down
20 changes: 20 additions & 0 deletions lib/test/nodeUtils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"use strict";

var _nodeUtils = require("../nodeUtils");
describe("isNodeRecordWithChildren", function () {
it("returns true when the data is an object with the children attribute of type array", function () {
var data = {
children: []
};
expect((0, _nodeUtils.isNodeRecordWithChildren)(data)).toBe(true);
});
it("returns when the data is an object without the children attribute", function () {
var data = {
name: "test"
};
expect((0, _nodeUtils.isNodeRecordWithChildren)(data)).toBe(false);
});
it("returns when the data is a string", function () {
expect((0, _nodeUtils.isNodeRecordWithChildren)("test")).toBe(false);
});
});
Loading

0 comments on commit a877f57

Please sign in to comment.