@jsonforms/material-tree-renderer
Version:
Material-based tree renderer for JSON Forms
98 lines (94 loc) • 4.1 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
/*
The MIT License
Copyright (c) 2017-2019 EclipseSource Munich
https://github.com/eclipsesource/jsonforms
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
var clone_1 = __importDefault(require("lodash/clone"));
var isEqual_1 = __importDefault(require("lodash/isEqual"));
var core_1 = require("@jsonforms/core");
var util_1 = require("../helpers/util");
exports.Types = {
TREE_DND: 'tree-master-detail-DnD'
};
/**
* The delay in milliseconds before D&D related CSS (e.g. highlighting valid drop targets) is
* applied.
*/
exports.CSS_DELAY = 30;
/**
* Moves the given data from the oldPath to the newPath by deleting the data object at the old path
* and inserting the data at the new path.
*
* Note: an item at the index of the new path is pushed behind the inserted item
*
* @param dispatch the redux dispatcher used to execute the remove and add actions
* @param data the data to move (insert at new path)
* @param oldPath the data at this path will be deleted
* @param newPath the given data will be inserted at this path
*/
exports.moveListItem = function (dispatch) { return function (data, oldPath, newPath) {
if (newPath === oldPath) {
// nothing needs to be moved
return false;
}
var oldParentPath = util_1.parentPath(oldPath);
var oldIndex = util_1.indexFromPath(oldPath);
var newParentPath = util_1.parentPath(newPath);
var newIndex = util_1.indexFromPath(newPath);
// Remove moved data from source array
dispatch(core_1.update(oldParentPath, function (array) {
// TODO clone necessary?
var clonedArray = clone_1.default(array);
clonedArray.splice(oldIndex, 1);
console.log("remove from " + oldParentPath + ", index: " + oldIndex);
return clonedArray;
}));
// Add moved data to target array
dispatch(core_1.update(newParentPath, function (array) {
if (array === undefined || array === null || array.length === 0) {
return [data];
}
// TODO clone necessary?
var clonedArray = clone_1.default(array);
clonedArray.splice(newIndex, 0, data);
console.log("add to " + newParentPath + ", index: " + newIndex);
return clonedArray;
}));
return true;
}; };
exports.mapDispatchToTreeListProps = function (dispatch) { return ({
moveListItem: exports.moveListItem(dispatch)
}); };
/**
* Returns whether the dragged item can be dropped in a list.
*
* @param containerProps The ContainmentProperties that the list can supports
* @param dragInfo The DragInfo describing the dragged item
*/
exports.canDropDraggedItem = function (containerProps, dragInfo) {
var matchingProps = containerProps.filter(function (prop) {
return isEqual_1.default(prop.schema, dragInfo.schema);
});
return matchingProps.length > 0;
};
//# sourceMappingURL=dnd.util.js.map