CrudListPackage.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 _modularityFront = _interopRequireDefault(require("modularity-front"));
  8. var _reactMaterialize = require("react-materialize");
  9. var _ListModule = _interopRequireDefault(require("../HigherModules/ListModule.js"));
  10. var _CrudService = _interopRequireDefault(require("../Services/CrudService.js"));
  11. require("./CrudListPackage.css");
  12. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  13. 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); }
  14. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  15. 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); } }
  16. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  17. function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
  18. function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
  19. function _get(target, property, receiver) { if (typeof Reflect !== "undefined" && Reflect.get) { _get = Reflect.get; } else { _get = function _get(target, property, receiver) { var base = _superPropBase(target, property); if (!base) return; var desc = Object.getOwnPropertyDescriptor(base, property); if (desc.get) { return desc.get.call(receiver); } return desc.value; }; } return _get(target, property, receiver || target); }
  20. function _superPropBase(object, property) { while (!Object.prototype.hasOwnProperty.call(object, property)) { object = _getPrototypeOf(object); if (object === null) break; } return object; }
  21. function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
  22. 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); }
  23. function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
  24. var Module = _modularityFront.default.Module;
  25. var CrudListPackage =
  26. /*#__PURE__*/
  27. function (_Modularity$Module) {
  28. _inherits(CrudListPackage, _Modularity$Module);
  29. function CrudListPackage(props) {
  30. var _this;
  31. _classCallCheck(this, CrudListPackage);
  32. _this = _possibleConstructorReturn(this, _getPrototypeOf(CrudListPackage).call(this, props));
  33. _this.state = {
  34. service: null
  35. };
  36. _this.headers = _this.TableHeaders.value;
  37. var table = _this.TableName.value;
  38. if (_this.RouterDependable.value) {
  39. // Search route for table
  40. var route = _this.RoutingSystem.getRoute();
  41. var regex = /\/[A-Za-z]+\/?/g;
  42. var found = route.url.match(regex);
  43. if (found.length) {
  44. table = found[0].replace('/', '');
  45. }
  46. }
  47. var propertiesService = {
  48. Table: table,
  49. Token: _this.Token.value,
  50. Service: function Service(service) {
  51. return _this.updateService(service);
  52. }
  53. };
  54. _this.back_service = new _CrudService.default(propertiesService);
  55. _this.TableName.onUpdate(function (v) {
  56. _this.back_service.Table.update(v);
  57. _this.forceUpdate();
  58. });
  59. _this.Token.onUpdate(function (v) {
  60. _this.back_service.Token.update(v);
  61. _this.forceUpdate();
  62. });
  63. return _this;
  64. }
  65. _createClass(CrudListPackage, [{
  66. key: "updateService",
  67. value: function updateService(service) {
  68. if (service) this.calculateHeaders(service);
  69. }
  70. }, {
  71. key: "setFilters",
  72. value: function setFilters(filters) {
  73. if (filters) {
  74. this.back_service.Filters.update(filters);
  75. }
  76. this.filters = filters;
  77. this.forceUpdate();
  78. }
  79. }, {
  80. key: "buildFilters",
  81. value: function buildFilters(headers) {
  82. var filters = [];
  83. for (var i in headers) {
  84. if (headers[i].filter) {
  85. filters.push(headers[i].filter);
  86. }
  87. }
  88. return filters;
  89. }
  90. }, {
  91. key: "calculateHeaders",
  92. value: function calculateHeaders(service, test) {
  93. var _this2 = this;
  94. service.model().then(function (model) {
  95. var headers = {};
  96. var formHeaders = {};
  97. for (var name in model) {
  98. if (model[name].disabled) continue;
  99. var f = {
  100. Name: model[name].Name,
  101. type: model[name].type,
  102. required: !model[name].nullable,
  103. fieldName: name,
  104. filter: model[name].Filter
  105. };
  106. if (!name.includes("_id") && !(model[name].Filter && model[name].Filter.type === "editor" || model[name].type === "timestamp")) headers[model[name].Name || name] = f;
  107. formHeaders[model[name].Name || name] = f;
  108. }
  109. _this2.formHeaders = formHeaders;
  110. _this2.headers = headers;
  111. return headers;
  112. }).then(function (headers) {
  113. if (!_this2.filters) _this2.filters = _this2.buildFilters(headers);
  114. _this2.forceUpdate();
  115. });
  116. }
  117. }, {
  118. key: "render",
  119. value: function render() {
  120. var _this3 = this;
  121. var service = this.back_service.Service.value;
  122. return _react.default.createElement("div", null, _react.default.createElement(_ListModule.default, {
  123. TableHeaders: this.headers,
  124. TableClass: "striped centered",
  125. LiveEdit: this.LiveEdit.value,
  126. FormHeaders: this.formHeaders,
  127. perPage: this.perPage.value,
  128. service: service,
  129. filterStyle: {},
  130. filterClassName: "grey lighten-5",
  131. FilterInput: this.filters,
  132. onFilters: function onFilters(filters) {
  133. return _this3.setFilters(filters);
  134. }
  135. }), _get(_getPrototypeOf(CrudListPackage.prototype), "render", this).call(this));
  136. }
  137. }]);
  138. return CrudListPackage;
  139. }(_modularityFront.default.Module);
  140. CrudListPackage.Inputs = {
  141. RouterDependable: {
  142. type: Module.Types.Boolean,
  143. defaultValue: false
  144. },
  145. TableName: {
  146. type: Module.Types.Text,
  147. defaultValue: "products"
  148. },
  149. perPage: {
  150. type: Module.Types.Number,
  151. defaultValue: 2
  152. },
  153. TableHeaders: {
  154. type: Module.Types.Object,
  155. defaultValue: {
  156. "Id": "id",
  157. "Name": "name",
  158. "Value": "price"
  159. }
  160. },
  161. LiveEdit: {
  162. type: Module.Types.Boolean,
  163. defaultValue: false
  164. },
  165. Token: {
  166. type: Module.Types.Text,
  167. defaultValue: '123123'
  168. }
  169. };
  170. CrudListPackage.Outputs = {};
  171. CrudListPackage.Category = "NikatlasPackages";
  172. CrudListPackage.EditPosition = "center";
  173. var _default = CrudListPackage;
  174. exports.default = _default;