ListModule.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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 _index = _interopRequireDefault(require("../Modules/index.js"));
  10. var _FilterModule = _interopRequireDefault(require("../Modules/FilterModule.js"));
  11. require("./ListModule.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 _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }
  15. function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }
  16. function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
  17. function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
  18. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  19. 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); } }
  20. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  21. function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
  22. function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
  23. 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); }
  24. function _superPropBase(object, property) { while (!Object.prototype.hasOwnProperty.call(object, property)) { object = _getPrototypeOf(object); if (object === null) break; } return object; }
  25. function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
  26. 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); }
  27. function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
  28. var Table = _index.default.Table,
  29. Pagination = _index.default.Pagination,
  30. Form = _index.default.Form;
  31. var Module = _modularityFront.default.Module;
  32. var pTimeout = function pTimeout(time, item) {
  33. return new Promise(function (res, rej) {
  34. setTimeout(function () {
  35. return res(item);
  36. }, time);
  37. });
  38. };
  39. var ListModule =
  40. /*#__PURE__*/
  41. function (_Modularity$Module) {
  42. _inherits(ListModule, _Modularity$Module);
  43. function ListModule(props) {
  44. var _this;
  45. _classCallCheck(this, ListModule);
  46. _this = _possibleConstructorReturn(this, _getPrototypeOf(ListModule).call(this, props));
  47. _this.state = {
  48. page: 1,
  49. data: [],
  50. totalPages: 1,
  51. showForm: false,
  52. model: {},
  53. mode: 0,
  54. formButton: "Add"
  55. };
  56. _this.service.onUpdate(function () {
  57. return _this.serviceUpdate(_this.state.page);
  58. });
  59. _this.serviceUpdate(_this.state.page);
  60. var route = _this.RoutingSystem.getRoute().url;
  61. if (route.endsWith("/add")) {
  62. pTimeout(1000).then(function () {
  63. return _this.showAddForm();
  64. });
  65. }
  66. console.log("ROUTE ", route);
  67. return _this;
  68. }
  69. _createClass(ListModule, [{
  70. key: "serviceUpdate",
  71. value: function serviceUpdate() {
  72. var _this2 = this;
  73. var page = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
  74. var Service = this.service.value;
  75. if (!Service.count) return;
  76. var perPage = this.perPage.value;
  77. return Service.count().then(function (_ref) {
  78. var _ref2 = _slicedToArray(_ref, 1),
  79. counter = _ref2[0].counter;
  80. var totalPages = parseInt(counter / perPage + 0.999);
  81. page = page <= totalPages ? page : totalPages;
  82. _this2.setState({
  83. totalPages: totalPages,
  84. page: page
  85. });
  86. return Service.getPage(page, perPage);
  87. }).then(function (data) {
  88. _this2.setState({
  89. data: data
  90. });
  91. }).catch(function (e) {
  92. return console.log(e);
  93. });
  94. }
  95. }, {
  96. key: "setFilters",
  97. value: function setFilters(filters) {
  98. this.onFilters.set(filters);
  99. }
  100. }, {
  101. key: "pageChanged",
  102. value: function pageChanged(v) {
  103. this.serviceUpdate(v);
  104. }
  105. }, {
  106. key: "update",
  107. value: function update() {
  108. var _this3 = this;
  109. var data = this.state.formData;
  110. var Service = this.service.value;
  111. if (!Service.count || !data) return;
  112. for (var i in data) {
  113. if (data[i] === "None") data[i] = null;
  114. }
  115. Service.update(data.id, data).then(function (res) {
  116. return _this3.serviceUpdate(_this3.state.page).then(function () {
  117. return _this3.setState({
  118. showForm: false
  119. });
  120. });
  121. }).catch(function (e) {
  122. return window.Materialize ? window.Materialize.toast('Can not update!', 5000) : window.M.toast({
  123. html: 'Can not update!',
  124. displayLength: 5000
  125. });
  126. });
  127. }
  128. }, {
  129. key: "add",
  130. value: function add() {
  131. var _this4 = this;
  132. var data = this.state.formData;
  133. var Service = this.service.value;
  134. if (!Service.count || !data) return;
  135. delete data.id;
  136. Service.add(data).then(function (res) {
  137. return _this4.serviceUpdate(_this4.state.page);
  138. }).then(function () {
  139. return _this4.setState({
  140. showForm: false
  141. });
  142. }).catch(function (e) {
  143. return window.Materialize ? window.Materialize.toast('Can not Add!', 5000) : window.M.toast({
  144. html: 'Can not Add!',
  145. displayLength: 5000
  146. });
  147. });
  148. }
  149. }, {
  150. key: "rowEdit",
  151. value: function rowEdit(row) {
  152. var _this5 = this;
  153. if (!row) return;
  154. var Service = this.service.value;
  155. if (Service.update && row.id) Service.update(row.id, row).then(function () {
  156. return _this5.serviceUpdate(_this5.state.page);
  157. }).catch(function (e) {
  158. window.Materialize.toast('Can not Edit!', 5000);
  159. _this5.forceUpdate();
  160. });
  161. }
  162. }, {
  163. key: "_rowEdited",
  164. value: function _rowEdited(row) {
  165. if (!row) return;
  166. var headers = this.FormHeaders.value;
  167. var mod = this.prepareForm(headers, row);
  168. this.setState({
  169. showForm: true,
  170. model: mod,
  171. mode: 1,
  172. formButton: 'Edit'
  173. });
  174. }
  175. }, {
  176. key: "_rowDeleted",
  177. value: function _rowDeleted(row) {
  178. var _this6 = this;
  179. if (!row) return;
  180. var Service = this.service.value;
  181. if (Service.delete && row.id) Service.delete(row.id).then(function () {
  182. return _this6.serviceUpdate(_this6.state.page);
  183. });
  184. }
  185. }, {
  186. key: "handleClick",
  187. value: function handleClick() {// console.log("added");
  188. }
  189. }, {
  190. key: "prepareForm",
  191. value: function prepareForm(headers) {
  192. var row = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  193. var mod = {}; // console.log(headers);
  194. for (var i in headers) {
  195. if (!headers[i]) continue; //if(headers[i].fieldName === "id") continue;
  196. mod[headers[i].Name || headers[i].fieldName] = {
  197. type: "text",
  198. placeholder: i,
  199. columns: "col s12 l6 xl4",
  200. value: row[i],
  201. filter: headers[i].filter
  202. };
  203. }
  204. return mod;
  205. }
  206. }, {
  207. key: "showAddForm",
  208. value: function showAddForm() {
  209. var headers = this.FormHeaders.value; // add Required !
  210. var mod = this.prepareForm(headers);
  211. this.setState({
  212. showForm: true,
  213. model: mod,
  214. mode: 0,
  215. formButton: 'Add'
  216. });
  217. }
  218. }, {
  219. key: "render",
  220. value: function render() {
  221. var _this7 = this;
  222. var page = this.state.page,
  223. totalPages = this.state.totalPages,
  224. data = this.state.data;
  225. var TableView = _react.default.createElement(_reactMaterialize.Row, {
  226. style: {
  227. display: 'flex'
  228. },
  229. key: 'Table'
  230. }, _react.default.createElement(_reactMaterialize.Col, {
  231. s: 2,
  232. style: this.props.filterStyle,
  233. className: this.props.filterClassName
  234. }, _react.default.createElement(_FilterModule.default, {
  235. Inputs: this.FilterInput.value,
  236. Filters: function Filters(filters) {
  237. return _this7.setFilters(filters);
  238. }
  239. })), _react.default.createElement(_reactMaterialize.Col, {
  240. s: 10
  241. }, _react.default.createElement(Table, {
  242. TableHeaders: this.TableHeaders.value,
  243. TableData: data,
  244. TableClass: this.TableClass.value,
  245. LiveEdit: this.LiveEdit.value,
  246. EditedRow: function EditedRow(v) {
  247. return _this7.rowEdit(v);
  248. },
  249. onEdit: function onEdit(v) {
  250. return _this7._rowEdited(v);
  251. },
  252. onDelete: function onDelete(v) {
  253. return _this7._rowDeleted(v);
  254. }
  255. }), _react.default.createElement(_reactMaterialize.Row, null, _react.default.createElement(_reactMaterialize.Col, {
  256. offset: "s10"
  257. }, _react.default.createElement(_reactMaterialize.Button, {
  258. onClick: function onClick() {
  259. return _this7.showAddForm();
  260. }
  261. }, "Add"))), _react.default.createElement(Pagination, {
  262. selected: page,
  263. lastPage: totalPages,
  264. onPage: function onPage(v) {
  265. return _this7.pageChanged(v);
  266. }
  267. })));
  268. var FormView = _react.default.createElement(_reactMaterialize.Row, {
  269. key: 'Form'
  270. }, _react.default.createElement(_reactMaterialize.Col, {
  271. s: 12
  272. }, _react.default.createElement(Form, {
  273. FormInputs: this.state.model,
  274. button: this.state.formButton,
  275. formData: function formData(_formData) {
  276. return _this7.setState({
  277. formData: _formData
  278. });
  279. },
  280. className: 'AdminDataForm'
  281. }), _react.default.createElement(_reactMaterialize.Button, {
  282. onClick: function onClick(data) {
  283. return _this7.state.mode ? _this7.update() : _this7.add();
  284. }
  285. }, "Save")));
  286. return [this.state.showForm ? FormView : TableView, _get(_getPrototypeOf(ListModule.prototype), "render", this).call(this)];
  287. }
  288. }]);
  289. return ListModule;
  290. }(_modularityFront.default.Module);
  291. ListModule.Inputs = {
  292. service: {
  293. type: Module.Types.Object,
  294. defaultValue: {}
  295. },
  296. perPage: {
  297. type: Module.Types.Number,
  298. defaultValue: 2
  299. },
  300. TableHeaders: {
  301. type: Module.Types.Object,
  302. defaultValue: {
  303. "Id": "id",
  304. "Name": "name",
  305. "Value": "price"
  306. }
  307. },
  308. FormHeaders: {
  309. type: Module.Types.Object,
  310. defaultValue: {
  311. "Id": "id",
  312. "Name": "name",
  313. "Value": "price"
  314. }
  315. },
  316. TableClass: {
  317. type: Module.Types.Text,
  318. defaultValue: "striped centered"
  319. },
  320. LiveEdit: {
  321. type: Module.Types.Boolean,
  322. defaultValue: false
  323. },
  324. FilterInput: {
  325. type: Module.Types.Array,
  326. arrType: Module.Types.Object
  327. }
  328. };
  329. ListModule.Outputs = {
  330. onFilters: {}
  331. };
  332. ListModule.Category = "Nikatlas";
  333. ListModule.EditPosition = "top-right";
  334. var _default = ListModule;
  335. exports.default = _default;