hbp-react-ui
Version:
A library of useful user-interface components built with React and MobX
237 lines (168 loc) • 14.6 kB
JavaScript
'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, _descriptor4; ///////////////////////////////////////////////////////////
// File : DynamicList.js
// Description :
/**
* DynamicList allows selected text to be added to a list as individual buttons. These buttons can be added manually and also deleted.
* @class DynamicList
* @property {function} onChange The event-handler for a change in value
* @property {string} path The path to specify when reporting the value
* @property {array} items A NameValueArray of the initial values
* @property {function} onAddItem The callback to handle adding a new item
* @property {object} style Styling overrides
* @property {string} description The tooltip to display
*/
// Imports :
var _react = require('react');var _react2 = _interopRequireDefault(_react);
var _reactBootstrap = require('react-bootstrap');
var _utils = require('react-bootstrap/lib/utils');
var _reactDragAndDrop = require('react-drag-and-drop');
var _mobx = require('mobx');
var _mobxReact = require('mobx-react');
var _DynamicListStyles = require('./DynamicListStyles');var _DynamicListStyles2 = _interopRequireDefault(_DynamicListStyles);
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.');}_utils.bootstrapUtils.addStyle(_reactBootstrap.Button, 'custom');
// Class Definition
var
DynamicList = (0, _mobxReact.observer)(_class = (_class2 = function (_BaseClass) {_inherits(DynamicList, _BaseClass);
// Constructor
function DynamicList(props) {_classCallCheck(this, DynamicList);
if (typeof _hbp_debug_ != 'undefined') console.log('DynamicList.constructor');var _this = _possibleConstructorReturn(this, (DynamicList.__proto__ || Object.getPrototypeOf(DynamicList)).call(this,
props));_initDefineProp(_this, 'items', _descriptor, _this);_initDefineProp(_this, 'item', _descriptor2, _this);_initDefineProp(_this, 'showEnterMetadataModal', _descriptor3, _this);_initDefineProp(_this, 'showAlert', _descriptor4, _this);
_this.items = props.items || new _NameValue.NameValueArray();
_this.item = props.item || _this.empty;
_this.style = props.style || _DynamicListStyles2.default.styleContainer();return _this;
}
// Operations
_createClass(DynamicList, [{ key: 'componentWillMount', value: function componentWillMount() {
} }, { key: 'render', value: function render()
{
return _get(DynamicList.prototype.__proto__ || Object.getPrototypeOf(DynamicList.prototype), 'render', this).call(this);
} }, { key: 'componentDidMount', value: function componentDidMount()
{
} }, { key: 'componentWillReceiveProps', value: function componentWillReceiveProps(
nextProps) {
if (typeof _hbp_debug_ != 'undefined') console.log('DynamicList.componentWillReceiveProps');
_get(DynamicList.prototype.__proto__ || Object.getPrototypeOf(DynamicList.prototype), 'componentWillReceiveProps', this).call(this, nextProps);
if (nextProps.items != this.props.items) {// Re-initialised
this.items = nextProps.items;
}
} }, { key: 'shouldComponentUpdate', value: function shouldComponentUpdate(
nextProps, nextState) {
return _get(DynamicList.prototype.__proto__ || Object.getPrototypeOf(DynamicList.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()
{
return (
_react2.default.createElement('div', { style: this.style },
_react2.default.createElement('style', { type: 'text/css' }, '.DynamicListPanel {}'),
_react2.default.createElement('style', { type: 'text/css' }, '.DynamicListPanel .panel-heading {padding: 3px 0;}'),
_react2.default.createElement('style', { type: 'text/css' }, '.DynamicListPanel .panel-body {}'),
_react2.default.createElement(_reactBootstrap.Panel, { className: 'DynamicListPanel', header: this.props.header || this.renderHeader(), bsStyle: 'info' },
_react2.default.createElement('div', null,
_react2.default.createElement(_reactDragAndDrop.Droppable, {
enabled: true,
className: 'flex-row-wrap row-single-body y_scroll',
types: ['data'],
onDrop: this.onDrop.bind(this) },
this.renderBody()))),
_react2.default.createElement(_reactBootstrap.Modal, { show: this.showEnterMetadataModal, onHide: this.close.bind(this, false) },
_react2.default.createElement(_reactBootstrap.Modal.Header, null,
_react2.default.createElement(_reactBootstrap.Modal.Title, null, 'Enter Metadata')),
_react2.default.createElement(_reactBootstrap.Modal.Body, null,
_react2.default.createElement('div', { className: 'text-center' },
_react2.default.createElement(_reactBootstrap.FormControl, {
type: 'text',
placeholder: 'type...',
onChange: this.onChange.bind(this),
autoFocus: true }))),
_react2.default.createElement(_reactBootstrap.Modal.Footer, null,
_react2.default.createElement(_reactBootstrap.Button, { onClick: this.close.bind(this, true) }, 'OK'),
_react2.default.createElement(_reactBootstrap.Button, { onClick: this.close.bind(this, false) }, 'Cancel'))),
_react2.default.createElement(_reactBootstrap.Modal, { show: this.showAlert },
_react2.default.createElement(_reactBootstrap.Modal.Header, null,
_react2.default.createElement(_reactBootstrap.Modal.Title, null, 'Error')),
_react2.default.createElement(_reactBootstrap.Modal.Body, null,
_react2.default.createElement('div', { className: 'text-center' }, 'Invalid input')),
_react2.default.createElement(_reactBootstrap.Modal.Footer, null,
_react2.default.createElement(_reactBootstrap.Button, { onClick: this.hideAlert.bind(this) }, 'OK')))));
} }, { key: 'renderHeader', value: function renderHeader()
{
var header =
_react2.default.createElement('div', { className: 'text-center' },
_react2.default.createElement('style', { type: 'text/css' }, '.btn-custom {padding: 2px 12px; background-color: #428bca; color: white;}'),
_react2.default.createElement(_reactBootstrap.Button, { bsStyle: 'custom', onClick: this.addToList.bind(this), title: this.props.description },
this.title));
return header;
} }, { key: 'renderBody', value: function renderBody()
{var _this2 = this;
return this.items.map(function (item, index) {
return (
_react2.default.createElement('div', { key: item.$value, className: 'flex-item' },
_react2.default.createElement(_reactBootstrap.Button, { onClick: _this2.removeFromList.bind(_this2, index) },
item.$name || item.$value,
_react2.default.createElement(_reactBootstrap.Glyphicon, { glyph: 'remove', style: { marginLeft: '8px' } }))));
});
} }, { key: 'onChange', value: function onChange(
event) {
console.log(event.target.value);
this.item.$value = event.target.value;
} }, { key: 'addToList', value: function addToList()
{var _this3 = this;
this.props.onAddItem(
function (name) {
if (name.length) {
if (!(name.length > 256)) {
_this3.items.push(new _NameValue.NameValue(undefined, name));
_this3.props.onChange(_this3.props.path, _this3.items.toJS());
} else {
_this3.showAlert = true;
}
} else {
return _this3.open();
}
});
} }, { key: 'removeFromList', value: function removeFromList(
index) {
this.items.splice(index, 1);
this.props.onChange(this.props.path, this.items.toJS());
} }, { key: 'open', value: function open()
{
this.showEnterMetadataModal = true;
} }, { key: 'close', value: function close(
OK) {
this.showEnterMetadataModal = false;
if (OK) {
if (this.item.$value.length) {
if (!(this.item.$value.length > 256)) {
this.items.push(new _NameValue.NameValue(undefined, this.item.$value));
this.props.onChange(this.props.path, this.items.toJS());
this.item = this.empty;
} else {
this.showAlert = true;
}
}
}
} }, { key: 'hideAlert', value: function hideAlert()
{
this.showAlert = false;
} }, { key: 'allowDrop', value: function allowDrop(
event) {
} }, { key: 'onDrop', value: function onDrop(
event) {
console.log(event.data);
var name = event.data;
this.addItem(name);
} }, { key: 'addItem', value: function addItem(
name) {
this.items.push(new _NameValue.NameValue(undefined, name));
this.props.onChange(this.props.path, this.items.toJS());
} }]);return DynamicList;}(_BaseClass3.default), (_descriptor = _applyDecoratedDescriptor(_class2.prototype, 'items', [_mobx.observable], { enumerable: true, initializer: null }), _descriptor2 = _applyDecoratedDescriptor(_class2.prototype, 'item', [_mobx.observable], { enumerable: true, initializer: null }), _descriptor3 = _applyDecoratedDescriptor(_class2.prototype, 'showEnterMetadataModal', [_mobx.observable], { enumerable: true, initializer: function initializer() {return false;} }), _descriptor4 = _applyDecoratedDescriptor(_class2.prototype, 'showAlert', [_mobx.observable], { enumerable: true, initializer: function initializer() {return false;} })), _class2)) || _class;
// Exports
exports.default = DynamicList;