UNPKG

hbp-react-ui

Version:

A library of useful user-interface components built with React and MobX

121 lines (87 loc) 9.05 kB
'use strict';Object.defineProperty(exports, "__esModule", { value: true });exports.default = undefined;var _createClass = 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);}}return function (Constructor, protoProps, staticProps) {if (protoProps) defineProperties(Constructor.prototype, protoProps);if (staticProps) defineProperties(Constructor, staticProps);return Constructor;};}();var _get = function get(object, property, receiver) {if (object === null) object = Function.prototype;var desc = Object.getOwnPropertyDescriptor(object, property);if (desc === undefined) {var parent = Object.getPrototypeOf(object);if (parent === null) {return undefined;} else {return get(parent, property, receiver);}} else if ("value" in desc) {return desc.value;} else {var getter = desc.get;if (getter === undefined) {return undefined;}return getter.call(receiver);}};var _class, _desc, _value, _class2, _descriptor, _descriptor2, _descriptor3; /////////////////////////////////////////////////////////// // File : Tree.js // Description : /** * Tree displays an expandable/collapsible hierarchical tree and allows nodes to be selected. * @class Tree * @property {function} onChange The event-handler for each selection * @property {string} path The path to specify when reporting the value * @property {object} data A JSON object representing the hierarchy to be displayed, with each node in the form {name, value, children: []} * @property {object} style Styling overrides * @property {string} description The tooltip to display */ // Imports : var _react = require('react');var _react2 = _interopRequireDefault(_react); var _mobx = require('mobx'); var _mobxReact = require('mobx-react'); var _TreeStyles = require('./TreeStyles');var _TreeStyles2 = _interopRequireDefault(_TreeStyles); var _BaseClass2 = require('./BaseClass');var _BaseClass3 = _interopRequireDefault(_BaseClass2); var _NameValue = require('./NameValue');function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}function _initDefineProp(target, property, descriptor, context) {if (!descriptor) return;Object.defineProperty(target, property, { enumerable: descriptor.enumerable, configurable: descriptor.configurable, writable: descriptor.writable, value: descriptor.initializer ? descriptor.initializer.call(context) : void 0 });}function _classCallCheck(instance, Constructor) {if (!(instance instanceof Constructor)) {throw new TypeError("Cannot call a class as a function");}}function _possibleConstructorReturn(self, call) {if (!self) {throw new ReferenceError("this hasn't been initialised - super() hasn't been called");}return call && (typeof call === "object" || typeof call === "function") ? call : self;}function _inherits(subClass, superClass) {if (typeof superClass !== "function" && superClass !== null) {throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);}subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } });if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;}function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) {var desc = {};Object['ke' + 'ys'](descriptor).forEach(function (key) {desc[key] = descriptor[key];});desc.enumerable = !!desc.enumerable;desc.configurable = !!desc.configurable;if ('value' in desc || desc.initializer) {desc.writable = true;}desc = decorators.slice().reverse().reduce(function (desc, decorator) {return decorator(target, property, desc) || desc;}, desc);if (context && desc.initializer !== void 0) {desc.value = desc.initializer ? desc.initializer.call(context) : void 0;desc.initializer = undefined;}if (desc.initializer === void 0) {Object['define' + 'Property'](target, property, desc);desc = null;}return desc;}function _initializerWarningHelper(descriptor, context) {throw new Error('Decorating class property failed. Please ensure that transform-class-properties is enabled.');} // Class Definition var Tree = (0, _mobxReact.observer)(_class = (_class2 = function (_BaseClass) {_inherits(Tree, _BaseClass); // Constructor function Tree(props) {_classCallCheck(this, Tree); if (typeof _hbp_debug_ != 'undefined') console.log('Tree.constructor');var _this = _possibleConstructorReturn(this, (Tree.__proto__ || Object.getPrototypeOf(Tree)).call(this, props));_initDefineProp(_this, 'data', _descriptor, _this);_initDefineProp(_this, 'class', _descriptor2, _this);_initDefineProp(_this, 'displayChildren', _descriptor3, _this); _this.data = props.data || {}; _this.style = props.style || _TreeStyles2.default.styleContainer();return _this; } // Operations _createClass(Tree, [{ key: 'componentWillMount', value: function componentWillMount() { } }, { key: 'render', value: function render() {var _this2 = this; var name = this.data.name; var value = this.data.value; var children = undefined; if (this.data.children != undefined) { children = this.data.children.map(function (child, index) { return ( _react2.default.createElement(Tree, { path: _this2.props.path, data: child, onChange: _this2.props.onChange, key: index })); }); } return ( _react2.default.createElement('li', { style: { listStyleType: 'none' } }, children != undefined && _react2.default.createElement('i', { className: this.class, style: { marginRight: '0.5em' }, onClick: this.toggle.bind(this) }), _react2.default.createElement('a', { onClick: this.onChange.bind(this, name, value), style: { cursor: 'pointer', textDecoration: 'none', color: 'inherit' } }, name), children != undefined && _react2.default.createElement('ul', { style: Object.assign({}, { marginLeft: '1em' }, this.displayChildren) }, children))); } }, { key: 'componentDidMount', value: function componentDidMount() { } }, { key: 'componentWillReceiveProps', value: function componentWillReceiveProps( nextProps) { if (typeof _hbp_debug_ != 'undefined') console.log('Tree.componentWillReceiveProps'); _get(Tree.prototype.__proto__ || Object.getPrototypeOf(Tree.prototype), 'componentWillReceiveProps', this).call(this, nextProps); if (nextProps.data != this.props.data) {// Re-initialised this.data = nextProps.data; } } }, { key: 'shouldComponentUpdate', value: function shouldComponentUpdate( nextProps, nextState) { return _get(Tree.prototype.__proto__ || Object.getPrototypeOf(Tree.prototype), 'shouldComponentUpdate', this).call(this); } }, { key: 'componentWillUpdate', value: function componentWillUpdate( nextProps, nextState) { } }, { key: 'componentDidUpdate', value: function componentDidUpdate( prevProps, prevState) { } }, { key: 'componentWillUnmount', value: function componentWillUnmount() { } }, { key: 'componentDidCatch', value: function componentDidCatch( error, info) { } }, { key: 'renderContainer', value: function renderContainer() { } }, { key: 'renderHeader', value: function renderHeader() { } }, { key: 'renderBody', value: function renderBody() { } }, { key: 'onChange', value: function onChange( name, value) { this.props.onChange(this.props.path, new _NameValue.NameValue(name, value)); } }, { key: 'toggle', value: function toggle() { this.class = this.class == 'glyphicon glyphicon-plus' ? 'glyphicon glyphicon-minus' : 'glyphicon glyphicon-plus'; this.displayChildren.display = this.displayChildren.display == 'none' ? 'block' : 'none'; } }]);return Tree;}(_BaseClass3.default), (_descriptor = _applyDecoratedDescriptor(_class2.prototype, 'data', [_mobx.observable], { enumerable: true, initializer: null }), _descriptor2 = _applyDecoratedDescriptor(_class2.prototype, 'class', [_mobx.observable], { enumerable: true, initializer: function initializer() {return 'glyphicon glyphicon-plus';} }), _descriptor3 = _applyDecoratedDescriptor(_class2.prototype, 'displayChildren', [_mobx.observable], { enumerable: true, initializer: function initializer() {return { display: 'none' };} })), _class2)) || _class; // Exports exports.default = Tree;