UNPKG

hbp-react-ui

Version:

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

160 lines (140 loc) 8.58 kB
'use strict';Object.defineProperty(exports, "__esModule", { value: true });exports.NameValueArray = exports.NameValue = undefined;var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {return typeof obj;} : function (obj) {return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;};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');function _classCallCheck(instance, Constructor) {if (!(instance instanceof Constructor)) {throw new TypeError("Cannot call a class as a function");}} /** * NameValue describes a name/value pair, in JSON format e.g. {"name": "foo", "value": "bar"}. * @class * @param {string} name A name representing the value * @param {string} value A value * @param {number} key The position of the object within an array * @returns {object} The name/value instance */var NameValue = exports.NameValue = function () { function NameValue(name, value, key) {_classCallCheck(this, NameValue); this.name = name; if (typeof value != 'undefined') this.value = value; if (typeof key != 'undefined') this.key = key; }_createClass(NameValue, [{ key: '$name', get: function get() { return this.name; }, set: function set( Name) { this.name = Name; } }, { key: '$value', get: function get() { if (typeof this.value == 'undefined') return this.name;else return this.value; }, set: function set( Value) { this.value = Value; } }, { key: '$key', get: function get() { return this.key; }, set: function set( key) { this.key = key; } }, { key: '$children', get: function get() { return this.children; }, set: function set( children) { this.children = children; } }]);return NameValue;}(); /** * NameValueArray extends Javascript's built-in 'Array' to add specific funtionality for handling arrays of 'NameValue' objects. * @class * @param {object} array Either a built-in array or a list of NameValue objects * @returns {array} The array of NameValue objects */var NameValueArray = exports.NameValueArray = function () { function NameValueArray(array) {_classCallCheck(this, NameValueArray); var items = (0, _mobx.observable)(new Array()); if (typeof array != 'undefined') { if (Array.isArray(array)) { array.map(function (item) {if ((typeof item === 'undefined' ? 'undefined' : _typeof(item)) == 'object') items.push(new NameValue(item.name, item.value, item.key));else items.push(new NameValue(item));}); } else { for (var i = 0; i < arguments.length; i++) { if (_typeof(arguments[i]) == 'object') items.push(new NameValue(arguments[i].name, arguments[i].value, arguments[i].key));else items.push(new NameValue(arguments[i])); } } } var methods = Object.getOwnPropertyNames(this.__proto__).filter(function (key) {return isNaN(parseInt(key));}); // Filter out methods for accessing elements by index for (var method in methods) { // console.log('NameValueArray: ' + methods[method]); items[methods[method]] = this[methods[method]]; } return items; // A MobX ObservableArray } /** * Create and add a single object to the array * @param {string} name An array of name/value objects in JSON format * @param {string} value An array of name/value objects in JSON format * @param {string} key An array of name/value objects in JSON format * @param {object} children An array of children */_createClass(NameValueArray, [{ key: 'add', value: function add( name, value, key, children) { this.push(new NameValue(name, value, key, children)); } /** * Add a single object to the array * @param {object} item A name/value object */ }, { key: 'addNameValue', value: function addNameValue( item) {// A name/value object this.push(item); } /** * Append the data to the array * @param {array} list An array of name/value objects in JSON format */ }, { key: 'addFromJSON', value: function addFromJSON( list) {var _this = this; // list.map(function (item, index) { _this.add(item['name'], item['value']); }); } /** * Append the alphanumeric/label/numeric-format data to the array * @param {array} list An array of objects from the Definition table */ }, { key: 'addFromDefinitionsJSON', value: function addFromDefinitionsJSON( list) {var _this2 = this; // list.map(function (item, index) { _this2.add(item['alphanumeric'], item['label'], item['numeric']); }); } /** * Remove the target object from the array * @param {object} target A name/value object to be removed */ }, { key: 'remove', value: function remove( target) { var i = -1; while ((i = this.findIndex(function (item) {return item.name == target.name && item.value == target.value;})) != -1) { this.splice(i, 1); } } /** * Return all names * @returns {array} The names */ }, { key: 'getNames', value: function getNames() { var names = this.map(function (item, index) { return item.name; }); return names; } /** * Return all values * @returns {array} The values */ }, { key: 'getValues', value: function getValues() { var values = this.map(function (item, index) { return item.value; }); return values; } /** * Find a name/value object by its unique value * @param {string} value The value to filter on * @returns {object} The name/value object, if found, otherwise undefined */ }, { key: 'findByValue', value: function findByValue( value) {// Only finds unique values var results = this.filter(function (item) {return item.value == value;}); if (results.length == 1) return results[0]; } }]);return NameValueArray;}();