UNPKG

hbp-react-ui

Version:

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

59 lines (49 loc) 3.3 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 _mobx = require('mobx'); var _NameValue = require('./NameValue');var _NameValue2 = _interopRequireDefault(_NameValue);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}function _classCallCheck(instance, Constructor) {if (!(instance instanceof Constructor)) {throw new TypeError("Cannot call a class as a function");}}var NameValueArray = function () { function NameValueArray(array) {_classCallCheck(this, NameValueArray); if (typeof array == 'undefined') { this.items = (0, _mobx.observable)(new Array()); // Create a new one } else { this.items = (0, _mobx.observable)(array); // Wrap an existing one } }_createClass(NameValueArray, [{ key: 'add', value: function add( name, value, key) { this.items.push(new _NameValue2.default(name, value, key)); } }, { key: 'addNameValue', value: function addNameValue( item) {// A name-value object this.items.push(item); } }, { key: 'addFromJSON', value: function addFromJSON( data) {var _this = this; // An array of name-value objects in JSON format data.map(function (item, index) { _this.add(item['name'], item['value']); }); } }, { key: 'addFromDefinitionsJSON', value: function addFromDefinitionsJSON( data) {var _this2 = this; // An array of objects from the Definition table data.map(function (item, index) { _this2.add(item['alphanumeric'], item['label'], item['numeric']); }); } }, { key: 'remove', value: function remove( object) { var i = -1; while ((i = this.items.findIndex(function (item) {return item.name == object.name && item.value == object.value;})) != -1) { this.items.splice(i, 1); } } }, { key: 'getNames', value: function getNames() { var names = this.items.map(function (item, index) { return item.name; }); return names; } }, { key: 'getValues', value: function getValues() { var values = this.items.map(function (item, index) { return item.value; }); return values; } }, { key: 'findByValue', value: function findByValue( value) {// Only finds unique values var results = this.items.filter(function (item) {return item.value == value;}); if (results.length == 1) return results[0]; } }]);return NameValueArray;}();exports.default = NameValueArray;