Table.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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 _reactMoment = _interopRequireDefault(require("react-moment"));
  10. require("./Table.css");
  11. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  12. function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
  13. function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
  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 _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  16. 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); } }
  17. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  18. function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
  19. function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
  20. 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); }
  21. function _superPropBase(object, property) { while (!Object.prototype.hasOwnProperty.call(object, property)) { object = _getPrototypeOf(object); if (object === null) break; } return object; }
  22. function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
  23. 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); }
  24. function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
  25. var Module = _modularityFront.default.Module;
  26. var TableModule =
  27. /*#__PURE__*/
  28. function (_Modularity$Module) {
  29. _inherits(TableModule, _Modularity$Module);
  30. function TableModule(props) {
  31. var _this;
  32. _classCallCheck(this, TableModule);
  33. _this = _possibleConstructorReturn(this, _getPrototypeOf(TableModule).call(this, props));
  34. _this.state = {};
  35. _this.EditedTable.set(_this.TableData.value);
  36. _this.TableData.onUpdate(function (e) {
  37. return _this.forceUpdate();
  38. });
  39. return _this;
  40. }
  41. _createClass(TableModule, [{
  42. key: "change",
  43. value: function change(data, row_id) {
  44. console.log("event fid with", row_id);
  45. this.EditedRow.set(data);
  46. var updated_data = this.TableData.value;
  47. updated_data[row_id] = data;
  48. this.EditedTable.set(updated_data);
  49. this.forceUpdate();
  50. }
  51. }, {
  52. key: "row_edited",
  53. value: function row_edited(data, row_id) {
  54. this.onEdit.set(data);
  55. }
  56. }, {
  57. key: "row_deleted",
  58. value: function row_deleted(data, row_id) {
  59. this.onDelete.set(data);
  60. }
  61. }, {
  62. key: "resolve",
  63. value: function resolve(value, header) {
  64. if (header && _typeof(header) === "object") {
  65. switch (header.type) {
  66. case 'date':
  67. return _react.default.createElement(_reactMoment.default, {
  68. format: "HH:mm DD/MM/YYYY"
  69. }, value);
  70. default:
  71. return value;
  72. }
  73. }
  74. return value;
  75. }
  76. }, {
  77. key: "render",
  78. value: function render() {
  79. var _this2 = this;
  80. var headers;
  81. var data;
  82. headers = Object.keys(this.TableHeaders.value || this.TableData.value && this.TableData.value.length > 0 && this.TableData.value[0] || {}).map(function (key, index) {
  83. return _react.default.createElement("th", {
  84. key: index
  85. }, key);
  86. });
  87. data = (this.TableData.value || []).map(function (item, ind) {
  88. return _react.default.createElement("tr", {
  89. key: '' + item.id + item.price + item.quantity
  90. }, Object.keys(_this2.TableHeaders.value || {}).map(function (header, index) {
  91. var input = _this2.TableHeaders.value[header];
  92. if (header === 'image') {
  93. return _react.default.createElement("td", {
  94. key: header
  95. }, _react.default.createElement("img", {
  96. src: item[input.field || input]
  97. }));
  98. } else if (input === '-index-' && item.id) {
  99. return _react.default.createElement("td", {
  100. key: item.id + 'index'
  101. }, ind + 1);
  102. } else {
  103. var field = input.field || input;
  104. var value = _this2.resolve(item[field], input);
  105. if (value && _typeof(value) === "object") {
  106. value = value.name || value[0] || value;
  107. } else if (value) {
  108. if (input.precision && !isNaN(value)) {
  109. value = value.toFixed(input.precision);
  110. }
  111. if (input.suffix) {
  112. value = value + input.suffix;
  113. }
  114. if (input.prefix) {
  115. value = input.prefix + value;
  116. }
  117. }
  118. return _react.default.createElement("td", {
  119. key: header
  120. }, _react.default.createElement(Editable, {
  121. row_id: ind,
  122. value: value,
  123. header_key: input.field || input,
  124. onChange: function onChange(e, i) {
  125. return _this2.change(e, i);
  126. },
  127. row: item,
  128. edit: _this2.LiveEdit.value
  129. }));
  130. }
  131. }), _this2.props.onEdit ? _react.default.createElement(EditButton, {
  132. row_id: ind,
  133. row: item,
  134. row_edited: function row_edited(e) {
  135. return _this2.row_edited(e);
  136. }
  137. }) : null, _this2.props.onDelete ? _react.default.createElement(DeleteButton, {
  138. row_id: ind,
  139. row: item,
  140. row_deleted: function row_deleted(e) {
  141. return _this2.row_deleted(e);
  142. }
  143. }) : null, _this2.props.onView ? _react.default.createElement(ViewButton, {
  144. row_id: ind,
  145. row: item,
  146. view_row: function view_row(e) {
  147. return _this2.props.onView(e);
  148. }
  149. }) : null);
  150. });
  151. return _react.default.createElement("div", null, _get(_getPrototypeOf(TableModule.prototype), "render", this).call(this), data.length ? _react.default.createElement("table", {
  152. className: this.TableClass.value + ""
  153. }, _react.default.createElement("thead", null, _react.default.createElement("tr", null, headers)), _react.default.createElement("tbody", null, data)) : _react.default.createElement("div", {
  154. className: "NothingHere"
  155. }, " Nothing Here! "));
  156. }
  157. }]);
  158. return TableModule;
  159. }(_modularityFront.default.Module);
  160. TableModule.Inputs = {
  161. LiveEdit: {
  162. type: Module.Types.Boolean,
  163. defaultValue: false
  164. },
  165. TableHeaders: {
  166. type: Module.Types.Object,
  167. defaultValue: {
  168. "Name": "key1",
  169. "Age": "key2",
  170. "Value": "key3"
  171. }
  172. },
  173. TableData: {
  174. type: Module.Types.Array,
  175. defaultValue: [{
  176. "key1": "Natalie",
  177. "key2": "20",
  178. "key3": "120"
  179. }, {
  180. "key1": "Somer",
  181. "key2": "10 y",
  182. "key3": "1000"
  183. }, {
  184. "key1": "Panos",
  185. "key2": "3y",
  186. "key3": "500"
  187. }],
  188. arrType: Module.Types.Object
  189. },
  190. TableClass: {
  191. type: Module.Types.Text,
  192. defaultValue: "striped centered"
  193. }
  194. };
  195. var Editable =
  196. /*#__PURE__*/
  197. function (_React$Component) {
  198. _inherits(Editable, _React$Component);
  199. function Editable(props) {
  200. var _this3;
  201. _classCallCheck(this, Editable);
  202. _this3 = _possibleConstructorReturn(this, _getPrototypeOf(Editable).call(this, props));
  203. _this3.state = {
  204. value: _this3.props.value,
  205. LiveEdit: false
  206. };
  207. return _this3;
  208. }
  209. _createClass(Editable, [{
  210. key: "edit",
  211. value: function edit(e) {
  212. e.preventDefault();
  213. this.setState({
  214. LiveEdit: true
  215. });
  216. }
  217. }, {
  218. key: "handleSubmit",
  219. value: function handleSubmit(e) {
  220. e.preventDefault();
  221. var key = this.props.header_key;
  222. var output = this.props.row;
  223. output[key] = e.target.value;
  224. this.props.onChange(output, this.props.row_id);
  225. }
  226. }, {
  227. key: "_handleKeyPress",
  228. value: function _handleKeyPress(e) {
  229. if (e.key === 'Enter') {
  230. this.handleSubmit(e);
  231. this.setState({
  232. LiveEdit: false
  233. });
  234. }
  235. }
  236. }, {
  237. key: "blur",
  238. value: function blur(e) {
  239. this.handleSubmit(e);
  240. this.setState({
  241. LiveEdit: false
  242. });
  243. }
  244. }, {
  245. key: "render",
  246. value: function render() {
  247. var _this4 = this;
  248. var _this$props = this.props,
  249. onBlur = _this$props.onBlur,
  250. onChange = _this$props.onChange,
  251. value = _this$props.value,
  252. props = _objectWithoutProperties(_this$props, ["onBlur", "onChange", "value"]);
  253. return this.state.LiveEdit ? _react.default.createElement("input", {
  254. style: {
  255. width: "auto"
  256. },
  257. value: this.state.value,
  258. onChange: function onChange(e) {
  259. return _this4.setState({
  260. value: e.target.value
  261. });
  262. },
  263. onBlur: function onBlur(e) {
  264. return _this4.blur(e);
  265. },
  266. onKeyPress: function onKeyPress(e) {
  267. return _this4._handleKeyPress(e);
  268. }
  269. }) : _react.default.createElement("div", {
  270. onClick: function onClick(e) {
  271. return _this4.props.edit && _this4.edit(e);
  272. }
  273. }, this.state.value);
  274. }
  275. }]);
  276. return Editable;
  277. }(_react.default.Component);
  278. var EditButton =
  279. /*#__PURE__*/
  280. function (_React$Component2) {
  281. _inherits(EditButton, _React$Component2);
  282. function EditButton(props) {
  283. _classCallCheck(this, EditButton);
  284. return _possibleConstructorReturn(this, _getPrototypeOf(EditButton).call(this, props));
  285. }
  286. _createClass(EditButton, [{
  287. key: "_editRow",
  288. value: function _editRow(e) {
  289. e.preventDefault();
  290. this.props.row_edited(this.props.row, this.props.row_id);
  291. }
  292. }, {
  293. key: "render",
  294. value: function render() {
  295. var _this5 = this;
  296. return _react.default.createElement("td", {
  297. style: {
  298. width: "60px"
  299. }
  300. }, _react.default.createElement(_reactMaterialize.Button, {
  301. onClick: function onClick(e) {
  302. return _this5._editRow(e);
  303. },
  304. className: "btn-floating"
  305. }, _react.default.createElement(_reactMaterialize.Icon, {
  306. tiny: true
  307. }, "edit")));
  308. }
  309. }]);
  310. return EditButton;
  311. }(_react.default.Component);
  312. var DeleteButton =
  313. /*#__PURE__*/
  314. function (_React$Component3) {
  315. _inherits(DeleteButton, _React$Component3);
  316. function DeleteButton(props) {
  317. _classCallCheck(this, DeleteButton);
  318. return _possibleConstructorReturn(this, _getPrototypeOf(DeleteButton).call(this, props));
  319. }
  320. _createClass(DeleteButton, [{
  321. key: "_DeleteRow",
  322. value: function _DeleteRow(e) {
  323. e.preventDefault();
  324. this.props.row_deleted(this.props.row, this.props.row_id);
  325. }
  326. }, {
  327. key: "render",
  328. value: function render() {
  329. var _this6 = this;
  330. return _react.default.createElement("td", {
  331. style: {
  332. width: "60px"
  333. }
  334. }, _react.default.createElement(_reactMaterialize.Button, {
  335. onClick: function onClick(e) {
  336. return _this6._DeleteRow(e);
  337. },
  338. className: "btn-floating"
  339. }, _react.default.createElement(_reactMaterialize.Icon, {
  340. tiny: true
  341. }, "delete")));
  342. }
  343. }]);
  344. return DeleteButton;
  345. }(_react.default.Component);
  346. var ViewButton =
  347. /*#__PURE__*/
  348. function (_React$Component4) {
  349. _inherits(ViewButton, _React$Component4);
  350. function ViewButton(props) {
  351. _classCallCheck(this, ViewButton);
  352. return _possibleConstructorReturn(this, _getPrototypeOf(ViewButton).call(this, props));
  353. }
  354. _createClass(ViewButton, [{
  355. key: "render",
  356. value: function render() {
  357. var _this7 = this;
  358. return _react.default.createElement("td", {
  359. style: {
  360. width: "60px"
  361. }
  362. }, _react.default.createElement(_reactMaterialize.Button, {
  363. onClick: function onClick(e) {
  364. return _this7.props.view_row(_this7.props.row);
  365. },
  366. className: "btn-floating"
  367. }, _react.default.createElement(_reactMaterialize.Icon, {
  368. tiny: true
  369. }, "remove_red_eye")));
  370. }
  371. }]);
  372. return ViewButton;
  373. }(_react.default.Component);
  374. TableModule.Outputs = {
  375. EditedTable: {
  376. type: Module.Types.Array,
  377. arrType: Module.Types.Object
  378. },
  379. EditedRow: {
  380. type: Module.Types.Object
  381. },
  382. onEdit: {
  383. type: Module.Types.Object
  384. },
  385. onDelete: {
  386. type: Module.Types.Object
  387. }
  388. };
  389. TableModule.Category = "sxoinas";
  390. TableModule.EditPosition = "center";
  391. var _default = TableModule;
  392. exports.default = _default;