frc-ui
Version:
React Web UI
517 lines (418 loc) • 19.5 kB
JavaScript
;
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _react = _interopRequireDefault(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _Item = _interopRequireDefault(require("./Item"));
var _types = require("./types");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
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 _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
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 _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, 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 _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
var __rest = void 0 && (void 0).__rest || function (s, e) {
var t = {};
for (var p in s) {
if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
}
if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
}
return t;
};
var Tree = /*#__PURE__*/function (_React$PureComponent) {
_inherits(Tree, _React$PureComponent);
var _super = _createSuper(Tree);
function Tree(props) {
var _this;
_classCallCheck(this, Tree);
_this = _super.call(this, props);
_this.showColorKeys = [];
_this.type = _types.Scroll_Type.ALL;
_this.showCount = 0;
_this.resize = function () {
var _this$props = _this.props,
rowHeight = _this$props.rowHeight,
fixedTopBottom = _this$props.fixedTopBottom;
var _this$state = _this.state,
tops = _this$state.tops,
options = _this$state.options,
bottoms = _this$state.bottoms;
if (_this.wrap && fixedTopBottom) {
var height = _this.wrap.clientHeight;
var width = _this.wrap.clientWidth;
if (width !== _this.state.width || height !== _this.state.height) {
_this.setState({
width: width,
height: height
});
}
var length = (options || []).length + (tops || []).length + (bottoms || []).length;
var type;
if (length * rowHeight < height) {
type = _types.Scroll_Type.ALL;
} else {
type = _types.Scroll_Type.CONTAINER;
}
if (type !== _this.type || _this.showCount !== (options || []).length) {
_this.showCount = (options || []).length;
_this.type = type;
var opts = _this.update(_this.getOptions());
_this.setState(opts);
}
}
};
_this.getProps = function (prop) {
return _this.props[prop];
};
_this.saveRef = function (name) {
return function (ele) {
_this[name] = ele;
};
};
_this.update = function () {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
var rowHeight = _this.props.rowHeight;
var height = _this.state.height;
if (height && options.length * rowHeight > height) {
_this.type = _types.Scroll_Type.CONTAINER;
} else {
_this.type = _types.Scroll_Type.ALL;
}
if (_this.type !== _types.Scroll_Type.CONTAINER) {
return {
tops: [],
bottoms: [],
options: options
};
}
var tops = [],
opts = [],
bottoms = [];
var start = _types.Insert_Type.TOP;
for (var i = 0; i < options.length; i++) {
var o = options[i];
if (o.index === 0) {
if (start === _types.Insert_Type.TOP) {
tops.push(o);
} else {
start = _types.Insert_Type.BOTTOM;
bottoms.push(o);
}
} else {
start = _types.Insert_Type.MIDDLE;
opts.push(o);
}
}
return {
tops: tops,
options: opts,
bottoms: bottoms
};
};
_this.onChange = function (option) {
return function (type, flag) {
var _this$props2 = _this.props,
rowHeight = _this$props2.rowHeight,
onChange = _this$props2.onChange,
onExpand = _this$props2.onExpand;
var height = _this.state.height;
var selectedKeys = _this.state.selectedKeys || [];
var expandedKeys = _this.state.expandedKeys || [];
var value = option.value;
var keys;
switch (type) {
case 'check':
keys = selectedKeys;
if (flag) {
if (keys.indexOf(value) < 0) {
keys = [].concat(_toConsumableArray(selectedKeys), [value]);
}
} else {
keys = selectedKeys.filter(function (v) {
return v !== value;
});
}
_this.showColorKeys = _this.getSelectedPath(keys);
_this.setState({
selectedKeys: keys
}, function () {
if (typeof onChange === 'function') {
onChange(keys || [], option);
}
});
break;
case 'expand':
keys = expandedKeys;
if (flag) {
if (keys.indexOf(value) < 0) {
keys = [].concat(_toConsumableArray(expandedKeys), [value]);
}
} else {
var cKeys = _this.expandKeys(value);
keys = keys.filter(function (v) {
return cKeys.indexOf(v) < 0;
});
}
var options = _this.getOptions(keys);
if (height && options.length * rowHeight > height) {
var childKeys = _this.getChildKeys(option.index === 0 ? option.value : option.parent);
keys = keys.filter(function (k) {
return childKeys.indexOf(k) > -1;
});
}
options = _this.getOptions(keys);
var opts = _this.update(options);
_this.setState(Object.assign({
expandedKeys: keys
}, opts), function () {
if (typeof onExpand === 'function') {
onExpand(keys || [], option);
}
});
break;
}
};
};
_this.getChildKeys = function (value) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _this.props.options;
var keys = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
var flag = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
for (var i = 0; i < options.length; i++) {
var option = options[i];
if (option.value === value || flag) {
keys.push(option.value);
var children = option.children || [];
if (children.length > 0) {
keys = _this.getChildKeys(value, children, keys, true);
}
}
}
return keys;
};
_this.expandKeys = function (value) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _this.props.options;
var keys = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
var found = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
options.forEach(function (o) {
var children = o.children || [];
if (children.length > 0) {
if (o.value === value || found) {
keys.push(o.value);
}
keys = _this.expandKeys(value, children, keys, o.value === value || found);
}
});
return keys;
};
_this.getSelectedPath = function (selectedKeys) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _this.props.options;
var keys = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
options.forEach(function (o) {
var children = o.children || [];
if (children.length > 0) {
_this.getSelectedPath(selectedKeys, children, keys);
}
if (selectedKeys.indexOf(o.value) > -1 || children.some(function (c) {
return keys.indexOf(c.value) > -1;
})) {
keys.push(o.value);
}
});
return keys;
};
_this.getOptions = function () {
var expandedKeys = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _this.state.expandedKeys || [];
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _this.props.options;
var opt = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
var index = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 0;
var parentExpanded = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : true;
var parent = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : '';
options = options || [];
var expandPlace = options.some(function (o) {
return o.children && o.children.length > 0;
});
options.forEach(function (o) {
var children = o.children,
other = __rest(o, ["children"]);
var expandEnable = children && children.length > 0;
var one = Object.assign(Object.assign({
expandEnable: expandEnable
}, other), {
index: index,
expandPlace: expandPlace,
parent: parent
});
var expanded = expandEnable && expandedKeys.indexOf(o.value) > -1;
if (index === 0) parent = o.value;
if (parentExpanded) {
opt.push(one);
}
if (expandEnable) {
_this.getOptions(expandedKeys, children || [], opt, index + 1, expanded, parent);
}
});
return opt;
};
_this.getStyle = function (type) {
var rowHeight = _this.props.rowHeight;
var opts = _this.state[type.toString()];
var style = {};
if (opts.length <= 0) {
style.display = 'none';
return style;
}
style.height = opts.length * rowHeight;
return style;
};
_this.renderItem = function (type) {
var _this$props3 = _this.props,
prefix = _this$props3.prefix,
rowHeight = _this$props3.rowHeight,
clickRow = _this$props3.clickRow;
var options = _this.state[type.toString()];
var _this$state2 = _this.state,
selectedKeys = _this$state2.selectedKeys,
expandedKeys = _this$state2.expandedKeys,
width = _this$state2.width;
return options.map(function (o) {
return /*#__PURE__*/_react["default"].createElement(_Item["default"], {
key: "".concat(o.value, "-").concat(Math.random()),
prefix: prefix,
option: o,
clickRow: clickRow,
parentWidth: width || 100,
rowHeight: rowHeight || 22,
hasSelected: _this.showColorKeys.indexOf(o.value) > -1,
expanded: (expandedKeys || []).indexOf(o.value) > -1,
checked: (selectedKeys || []).indexOf(o.value) > -1,
onChange: _this.onChange(o)
});
});
};
_this.showColorKeys = _this.getSelectedPath(props.selectedKeys || [], props.options);
_this.state = {
selectedKeys: props.selectedKeys || [],
expandedKeys: props.expandedKeys || [],
tops: [],
options: [],
bottoms: [],
width: 100,
height: 100
};
return _this;
}
_createClass(Tree, [{
key: "componentWillMount",
value: function componentWillMount() {
this.setState({
options: this.getOptions()
});
}
}, {
key: "componentDidMount",
value: function componentDidMount() {
if (this.wrap) {
this.resize();
this.wrap.addEventListener('resize', this.resize);
}
window.addEventListener('resize', this.resize);
}
}, {
key: "componentWillReceiveProps",
value: function componentWillReceiveProps(nextProps) {
var state = {};
if (nextProps.selectedKeys !== this.props.selectedKeys) {
state.selectedKeys = nextProps.selectedKeys || [];
this.showColorKeys = this.getSelectedPath(state.selectedKeys, nextProps.options);
}
if (nextProps.expandedKeys !== this.props.expandedKeys) {
state.expandedKeys = nextProps.expandedKeys || [];
}
if (nextProps.options !== this.props.options) {
state.tops = [];
state.bottoms = [];
state.options = this.getOptions(state.expandedKeys || this.state.expandedKeys, nextProps.options);
state = Object.assign(Object.assign({}, state), this.update(state.options));
if (Object.prototype.hasOwnProperty.call(state, 'selectedKeys')) {
this.showColorKeys = this.getSelectedPath(state.selectedKeys || [], nextProps.options);
} else {
this.showColorKeys = this.getSelectedPath(this.state.selectedKeys || [], nextProps.options);
}
} else {
if (!!state.expandedKeys) {
state.options = this.getOptions(state.expandedKeys);
state = Object.assign(Object.assign({}, state), this.update(state.options));
}
}
if (Object.keys(state).length > 0) {
this.setState(state, this.resize);
}
}
}, {
key: "render",
value: function render() {
var _this$props4 = this.props,
className = _this$props4.className,
prefix = _this$props4.prefix;
var cls = (0, _classnames["default"])("".concat(prefix), className);
var fixedTop = "".concat(prefix, "-fixed-top");
var fixedBottom = "".concat(prefix, "-fixed-bottom");
var topStyle = this.getStyle(_types.Render_Type.tops);
var bottomStyle = this.getStyle(_types.Render_Type.bottoms);
var containerStyle = {};
var innerStyle = {};
if (this.type === _types.Scroll_Type.CONTAINER) {
innerStyle.height = '100%';
innerStyle.overflowY = 'hidden';
innerStyle.paddingTop = topStyle.height || 0;
innerStyle.paddingBottom = bottomStyle.height || 0;
containerStyle.overflowY = 'auto';
containerStyle.height = '100%';
}
return /*#__PURE__*/_react["default"].createElement("div", {
className: cls,
ref: this.saveRef('wrap')
}, /*#__PURE__*/_react["default"].createElement("div", {
className: "".concat(prefix, "-inner"),
style: innerStyle
}, /*#__PURE__*/_react["default"].createElement("div", {
className: fixedTop,
style: topStyle
}, this.renderItem(_types.Render_Type.tops)), /*#__PURE__*/_react["default"].createElement("div", {
className: "".concat(prefix, "-container"),
style: containerStyle
}, this.renderItem(_types.Render_Type.options)), /*#__PURE__*/_react["default"].createElement("div", {
className: fixedBottom,
style: bottomStyle
}, this.renderItem(_types.Render_Type.bottoms))));
}
}]);
return Tree;
}(_react["default"].PureComponent);
Tree.defaultProps = {
prefix: 'swc-tree',
options: [],
selectedKeys: [],
expandedKeys: [],
rowHeight: 22,
fixedTopBottom: true,
clickRow: true
};
var _default = Tree;
exports["default"] = _default;