AdminModule.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _react = _interopRequireDefault(require("react"));
  7. var _Types = _interopRequireDefault(require("./Types.js"));
  8. var _Logger = _interopRequireDefault(require("../systems/Logger"));
  9. var _reactFontawesome = require("@fortawesome/react-fontawesome");
  10. var _reactMaterialize = require("react-materialize");
  11. var _reactJsonView = _interopRequireDefault(require("react-json-view"));
  12. var _ModuleUtilities = require("./ModuleUtilities");
  13. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  14. function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
  15. function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
  16. function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }
  17. function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }
  18. function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }
  19. function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }
  20. function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
  21. function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  22. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  23. 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); } }
  24. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  25. function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
  26. function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
  27. function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
  28. 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 } }); if (superClass) _setPrototypeOf(subClass, superClass); }
  29. function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
  30. var AdminModule =
  31. /*#__PURE__*/
  32. function (_React$Component) {
  33. _inherits(AdminModule, _React$Component);
  34. function AdminModule(props) {
  35. var _this;
  36. _classCallCheck(this, AdminModule);
  37. _this = _possibleConstructorReturn(this, _getPrototypeOf(AdminModule).call(this, props));
  38. _this.dynamicProperties();
  39. _this.readOnly = props.readOnly;
  40. _this._mode = props.__adminmode || 'closed';
  41. if (props.withAdmin) {
  42. _this._mode = 'normal';
  43. }
  44. var disabledEvents = ['onClick'];
  45. _this.disableEvents = {};
  46. for (var i in disabledEvents) {
  47. _this.disableEvents[disabledEvents[i]] = _this._stopPropagation;
  48. }
  49. return _this;
  50. }
  51. _createClass(AdminModule, [{
  52. key: "_stopPropagation",
  53. value: function _stopPropagation(e) {
  54. e.stopPropagation();
  55. e.preventDefault();
  56. }
  57. }, {
  58. key: "_addChild",
  59. value: function _addChild(item) {
  60. console.log(item);
  61. var mod = _objectSpread({}, item, {
  62. inputs: _objectSpread({}, item.inputs),
  63. outputs: _objectSpread({}, item.outputs),
  64. children: _toConsumableArray(item.children)
  65. });
  66. mod.id = this.__genRandom();
  67. this.props.System.loadModule(mod);
  68. this.props.System.addChild(this.getId(), mod.id);
  69. this.props.System.__refresh();
  70. if (this.onChildAdd) this.onChildAdd(mod.id);
  71. }
  72. }, {
  73. key: "_removeMe",
  74. value: function _removeMe() {
  75. this.props.System.remove(this.getId());
  76. this.props.System.__refresh();
  77. }
  78. }, {
  79. key: "_connect",
  80. value: function _connect(input, output) {
  81. // output!
  82. this.props.System.connect(input, output);
  83. }
  84. }, {
  85. key: "_change",
  86. value: function _change(name, value, input) {
  87. var v = (0, _ModuleUtilities.Resolver)(input, value);
  88. this[name].update(value);
  89. this.props.System.change(this.getId(), name, v); //this.props.System.__refresh();
  90. }
  91. }, {
  92. key: "_changeID",
  93. value: function _changeID(newid) {
  94. if (newid && newid !== this.getId()) {
  95. this.props.System.rename(this.getId(), newid);
  96. this.props.System.__refresh();
  97. }
  98. }
  99. }, {
  100. key: "__factoryInput",
  101. value: function __factoryInput(input) {
  102. return (0, _ModuleUtilities.RendererInput)(input, this._change.bind(this));
  103. }
  104. }, {
  105. key: "__factoryOutput",
  106. value: function __factoryOutput(output) {
  107. return (0, _ModuleUtilities.RendererOutput)(output);
  108. }
  109. }, {
  110. key: "__renderID",
  111. value: function __renderID() {
  112. var _this2 = this;
  113. return this.updateID && !this.readOnly ? _react.default.createElement("input", {
  114. type: "text",
  115. onBlur: function onBlur(e) {
  116. return _this2._changeID(e.target.value);
  117. },
  118. defaultValue: this.__id
  119. }) : _react.default.createElement("span", {
  120. onClick: function onClick() {
  121. _this2.updateID = true;
  122. _this2.forceUpdate();
  123. }
  124. }, '' + this.__id);
  125. }
  126. }, {
  127. key: "__reorder",
  128. value: function __reorder(times) {
  129. this.props.System.reorder(this.getId(), times);
  130. this.props.System.__refresh();
  131. }
  132. }, {
  133. key: "__setAdminUIPosition",
  134. value: function __setAdminUIPosition(times) {
  135. var positions = ["top-left", "top-center", "top-right", "bottom-left", "bottom", "bottom-right"];
  136. var indx = positions.indexOf(this.properties.EditPosition);
  137. var res = this.properties.EditPosition;
  138. if (indx === -1) {
  139. res = positions[1];
  140. } else {
  141. if (indx - times >= 0 && indx - times < positions.length) res = positions[indx - times];
  142. }
  143. this.properties.EditPosition = res;
  144. }
  145. }, {
  146. key: "dynamicProperties",
  147. value: function dynamicProperties() {
  148. this.properties = {};
  149. this.properties.EditPosition = this.constructor.EditPosition;
  150. this.properties.Inputs = this.constructor.Inputs || {};
  151. this.properties.Outputs = this.constructor.Outputs || {};
  152. }
  153. }, {
  154. key: "render",
  155. value: function render() {
  156. var _this3 = this;
  157. var position = this.properties.EditPosition;
  158. var CtorInputs = this.properties.Inputs;
  159. var CtorOutputs = this.properties.Outputs;
  160. var inputs = '',
  161. outputs = ''; // analyze inputs!
  162. if (this.props.inputs) inputs = Object.keys(CtorInputs || {}).map(function (i, indx) {
  163. return _react.default.createElement("div", {
  164. key: _this3.getId() + indx
  165. }, _this3.__factoryInput(_objectSpread({}, CtorInputs[i], _this3.props.inputs[i])));
  166. });
  167. if (this.props.outputs) outputs = Object.keys(CtorOutputs || {}).map(function (i, indx) {
  168. return _react.default.createElement("div", {
  169. key: 'out' + _this3.getId() + indx
  170. }, _this3.__factoryOutput(_objectSpread({}, CtorOutputs[i], _this3.props.outputs[i])));
  171. });
  172. if (this._mode === 'closed') return '';
  173. return _react.default.createElement(_reactMaterialize.Card, _extends({
  174. className: "blue-grey darken-1 AdminModule semi Position-".concat(position, " ").concat(this._mode == "open" ? 'top' : ''),
  175. key: '666999666999Admina' + this.getId(),
  176. onMouseEnter: function onMouseEnter() {
  177. _this3._mode = 'open';
  178. _this3.forceUpdate();
  179. },
  180. onMouseLeave: function onMouseLeave() {
  181. _this3._mode = 'normal';
  182. _this3.forceUpdate();
  183. }
  184. }, this.disableEvents, {
  185. header: _react.default.createElement("div", null, _react.default.createElement("h6", null, _react.default.createElement("b", null, "ID:"), this.__renderID(), _react.default.createElement("span", null, _react.default.createElement("i", {
  186. className: "material-icons cursor small",
  187. onClick: function onClick() {
  188. return _this3.__reorder(-1);
  189. }
  190. }, "arrow_drop_up"), _react.default.createElement("i", {
  191. className: "material-icons cursor small",
  192. onClick: function onClick() {
  193. return _this3.__reorder(1);
  194. }
  195. }, "arrow_drop_down")), _react.default.createElement("span", null, _react.default.createElement("i", {
  196. className: "material-icons cursor small",
  197. onClick: function onClick() {
  198. return _this3.__setAdminUIPosition(-1);
  199. }
  200. }, "arrow_left"), _react.default.createElement("i", {
  201. className: "material-icons cursor small",
  202. onClick: function onClick() {
  203. return _this3.__setAdminUIPosition(1);
  204. }
  205. }, "arrow_right"))))
  206. }), this._mode == 'open' ? _react.default.createElement("div", null, _react.default.createElement("div", {
  207. className: "row"
  208. }, _react.default.createElement(_ModuleUtilities.InputMarker, {
  209. onDrop: function onDrop(mod) {
  210. return _this3._addChild(mod);
  211. },
  212. name: "Add child",
  213. className: "col s12"
  214. })), inputs != '' ? _react.default.createElement("div", {
  215. className: "bars"
  216. }, inputs) : '', outputs != '' ? _react.default.createElement("div", {
  217. className: "bars"
  218. }, outputs) : '', _react.default.createElement("div", null, _react.default.createElement(_ModuleUtilities.RemoveMarker, {
  219. className: "col s12",
  220. onRemove: function onRemove() {
  221. return _this3._removeMe();
  222. }
  223. }))) : '');
  224. }
  225. }]);
  226. return AdminModule;
  227. }(_react.default.Component);
  228. var _default = AdminModule;
  229. exports.default = _default;