UNPKG

17.7 kBJavaScriptView Raw
1(function (global, factory) {
2 typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('react')) :
3 typeof define === 'function' && define.amd ? define(['exports', 'react'], factory) :
4 (global = global || self, factory(global.dataObjectConnect = {}, global.React));
5}(this, function (exports, React) { 'use strict';
6
7 React = React && React.hasOwnProperty('default') ? React['default'] : React;
8
9 function _extends() {
10 _extends = Object.assign || function (target) {
11 for (var i = 1; i < arguments.length; i++) {
12 var source = arguments[i];
13
14 for (var key in source) {
15 if (Object.prototype.hasOwnProperty.call(source, key)) {
16 target[key] = source[key];
17 }
18 }
19 }
20
21 return target;
22 };
23
24 return _extends.apply(this, arguments);
25 }
26
27 function _classCallCheck(instance, Constructor) {
28 if (!(instance instanceof Constructor)) {
29 throw new TypeError("Cannot call a class as a function");
30 }
31 }
32
33 function _defineProperties(target, props) {
34 for (var i = 0; i < props.length; i++) {
35 var descriptor = props[i];
36 descriptor.enumerable = descriptor.enumerable || false;
37 descriptor.configurable = true;
38 if ("value" in descriptor) descriptor.writable = true;
39 Object.defineProperty(target, descriptor.key, descriptor);
40 }
41 }
42
43 function _createClass(Constructor, protoProps, staticProps) {
44 if (protoProps) _defineProperties(Constructor.prototype, protoProps);
45 if (staticProps) _defineProperties(Constructor, staticProps);
46 return Constructor;
47 }
48
49 function _typeof2(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof2 = function _typeof2(obj) { return typeof obj; }; } else { _typeof2 = function _typeof2(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof2(obj); }
50
51 function _typeof(obj) {
52 if (typeof Symbol === "function" && _typeof2(Symbol.iterator) === "symbol") {
53 _typeof = function _typeof(obj) {
54 return _typeof2(obj);
55 };
56 } else {
57 _typeof = function _typeof(obj) {
58 return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : _typeof2(obj);
59 };
60 }
61
62 return _typeof(obj);
63 }
64
65 function _assertThisInitialized(self) {
66 if (self === void 0) {
67 throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
68 }
69
70 return self;
71 }
72
73 function _possibleConstructorReturn(self, call) {
74 if (call && (_typeof(call) === "object" || typeof call === "function")) {
75 return call;
76 }
77
78 return _assertThisInitialized(self);
79 }
80
81 function _getPrototypeOf(o) {
82 _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
83 return o.__proto__ || Object.getPrototypeOf(o);
84 };
85 return _getPrototypeOf(o);
86 }
87
88 function _setPrototypeOf(o, p) {
89 _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
90 o.__proto__ = p;
91 return o;
92 };
93
94 return _setPrototypeOf(o, p);
95 }
96
97 function _inherits(subClass, superClass) {
98 if (typeof superClass !== "function" && superClass !== null) {
99 throw new TypeError("Super expression must either be null or a function");
100 }
101
102 subClass.prototype = Object.create(superClass && superClass.prototype, {
103 constructor: {
104 value: subClass,
105 writable: true,
106 configurable: true
107 }
108 });
109 if (superClass) _setPrototypeOf(subClass, superClass);
110 }
111
112 var events = ["AllowDeleteChanged", "AllowUpdateChanged", "AllowInsertChanged", "SaveFailed", "PartialDataLoaded", "DataLoadFailed", "FieldChanged", "RecordCreated", "RecordRefreshed", "RecordDeleting", "RecordDeleted", "AfterSave", "BeforeLoad", "BeforeSave", "CancelEdit", "CurrentIndexChanged", "DataLoaded", "DirtyChanged"];
113 function connect(dataObject) {
114 var currentRowOnly = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
115 return function connect(WrappedComponent) {
116 function getDataObject() {
117 return typeof dataObject === "string" ? window[dataObject] : dataObject;
118 }
119
120 var connector =
121 /*#__PURE__*/
122 function (_React$Component) {
123 _inherits(connector, _React$Component);
124
125 function connector(props) {
126 var _this;
127
128 _classCallCheck(this, connector);
129
130 _this = _possibleConstructorReturn(this, _getPrototypeOf(connector).call(this, props));
131 var initialState = {};
132 var dataObject = getDataObject();
133
134 if (currentRowOnly) {
135 var _iteratorNormalCompletion = true;
136 var _didIteratorError = false;
137 var _iteratorError = undefined;
138
139 try {
140 for (var _iterator = dataObject.getFields()[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
141 var field = _step.value;
142 initialState[field.name] = null;
143 }
144 } catch (err) {
145 _didIteratorError = true;
146 _iteratorError = err;
147 } finally {
148 try {
149 if (!_iteratorNormalCompletion && _iterator.return != null) {
150 _iterator.return();
151 }
152 } finally {
153 if (_didIteratorError) {
154 throw _iteratorError;
155 }
156 }
157 }
158 } else {
159 initialState.data = [];
160 }
161
162 _this.state = Object.assign({
163 canDelete: dataObject.isDeleteAllowed(),
164 canUpdate: dataObject.isUpdateAllowed(),
165 canInsert: dataObject.isInsertAllowed(),
166 currentIndex: dataObject.getCurrentIndex(),
167 isDirty: dataObject.isDirty(),
168 isDeleting: false,
169 isLoading: dataObject.isDataLoading() === true,
170 isSaving: false,
171 loadError: null,
172 saveFailed: false
173 }, initialState);
174 _this.handleAfterSave = _this.handleAfterSave.bind(_assertThisInitialized(_this));
175 _this.handleAllowDeleteChanged = _this.handleAllowDeleteChanged.bind(_assertThisInitialized(_this));
176 _this.handleAllowInsertChanged = _this.handleAllowInsertChanged.bind(_assertThisInitialized(_this));
177 _this.handleAllowUpdateChanged = _this.handleAllowUpdateChanged.bind(_assertThisInitialized(_this));
178 _this.handleBeforeLoad = _this.handleBeforeLoad.bind(_assertThisInitialized(_this));
179 _this.handleBeforeSave = _this.handleBeforeSave.bind(_assertThisInitialized(_this));
180 _this.handleCancelEdit = _this.handleCancelEdit.bind(_assertThisInitialized(_this));
181 _this.handleCurrentIndexChanged = _this.handleCurrentIndexChanged.bind(_assertThisInitialized(_this));
182 _this.handleDataLoaded = _this.handleDataLoaded.bind(_assertThisInitialized(_this));
183 _this.handleDataLoadFailed = _this.handleDataLoadFailed.bind(_assertThisInitialized(_this));
184 _this.handleDirtyChanged = _this.handleDirtyChanged.bind(_assertThisInitialized(_this));
185 _this.handlePartialDataLoaded = _this.handlePartialDataLoaded.bind(_assertThisInitialized(_this));
186 _this.handleRecordDeleting = _this.handleRecordDeleting.bind(_assertThisInitialized(_this));
187 _this.handleRecordDeleted = _this.handleRecordDeleted.bind(_assertThisInitialized(_this));
188 _this.handleSaveFailed = _this.handleSaveFailed.bind(_assertThisInitialized(_this));
189 _this.setFieldValue = _this.setFieldValue.bind(_assertThisInitialized(_this));
190 _this.setFieldValues = _this.setFieldValues.bind(_assertThisInitialized(_this));
191 _this.updateData = _this.updateData.bind(_assertThisInitialized(_this));
192 _this.handleFieldChanged = _this.updateData;
193 _this.handleRecordCreated = _this.updateData;
194 _this.handleRecordRefreshed = _this.updateData;
195 return _this;
196 }
197
198 _createClass(connector, [{
199 key: "componentDidMount",
200 value: function componentDidMount() {
201 var dataObject = getDataObject();
202
203 for (var _i = 0, _events = events; _i < _events.length; _i++) {
204 var event = _events[_i];
205 dataObject.attachEvent("on" + event, this["handle" + event]);
206 }
207
208 this.updateData();
209 }
210 }, {
211 key: "componentWillUnmount",
212 value: function componentWillUnmount() {
213 var dataObject = getDataObject();
214
215 for (var _i2 = 0, _events2 = events; _i2 < _events2.length; _i2++) {
216 var event = _events2[_i2];
217 dataObject.detachEvent("on" + event, this["handle" + event]);
218 }
219 }
220 }, {
221 key: "cancelEdit",
222 value: function cancelEdit() {
223 getDataObject().cancelEdit();
224 }
225 }, {
226 key: "deleteRow",
227 value: function deleteRow(idx) {
228 var dataObject = getDataObject();
229 return new Promise(function (resolve) {
230 var callback = function callback(error, data) {
231 resolve({
232 data: data,
233 error: error
234 });
235 };
236
237 if (currentRowOnly) {
238 dataObject.deleteCurrentRow(callback);
239 } else {
240 dataObject.deleteRow(idx, callback);
241 }
242 });
243 }
244 }, {
245 key: "endEdit",
246 value: function endEdit(callback) {
247 var dataObject = getDataObject();
248 return new Promise(function (resolve) {
249 dataObject.endEdit(function (error, data) {
250 "function" === typeof callback && callback(error, data);
251 resolve({
252 data: data,
253 error: error
254 });
255 });
256 });
257 }
258 }, {
259 key: "updateData",
260 value: function updateData() {
261 var otherState = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
262 var dataObject = getDataObject();
263
264 if (currentRowOnly) {
265 var record = dataObject.currentRow();
266 this.setState(Object.assign(record, otherState));
267 } else {
268 var data = dataObject.getData();
269 var current = dataObject.currentRow();
270 this.setState(Object.assign({
271 current: current,
272 data: data
273 }, otherState));
274 }
275 }
276 }, {
277 key: "handleAllowDeleteChanged",
278 value: function handleAllowDeleteChanged(allowed) {
279 this.setState({
280 canDelete: allowed
281 });
282 }
283 }, {
284 key: "handleAllowUpdateChanged",
285 value: function handleAllowUpdateChanged(allowed) {
286 this.setState({
287 canUpdate: allowed
288 });
289 }
290 }, {
291 key: "handleAllowInsertChanged",
292 value: function handleAllowInsertChanged(allowed) {
293 this.setState({
294 canInsert: allowed
295 });
296 }
297 }, {
298 key: "handleSaveFailed",
299 value: function handleSaveFailed() {
300 this.setState({
301 saveFailed: true
302 });
303 }
304 }, {
305 key: "handlePartialDataLoaded",
306 value: function handlePartialDataLoaded() {
307 return null;
308 }
309 }, {
310 key: "handleDataLoadFailed",
311 value: function handleDataLoadFailed(loadError) {
312 if (loadError) {
313 this.setState({
314 isLoading: false,
315 loadError: loadError
316 });
317 } else {
318 this.setState({
319 isLoading: false
320 });
321 }
322 }
323 }, {
324 key: "handleRecordDeleting",
325 value: function handleRecordDeleting() {
326 this.setState({
327 isDeleting: true
328 });
329 }
330 }, {
331 key: "handleRecordDeleted",
332 value: function handleRecordDeleted() {
333 this.updateData({
334 isDeleting: false
335 });
336 }
337 }, {
338 key: "handleAfterSave",
339 value: function handleAfterSave() {
340 this.updateData({
341 isSaving: false
342 });
343 }
344 }, {
345 key: "handleBeforeLoad",
346 value: function handleBeforeLoad() {
347 this.setState({
348 isLoading: true
349 });
350 }
351 }, {
352 key: "handleBeforeSave",
353 value: function handleBeforeSave() {
354 this.setState({
355 isSaving: true,
356 saveFailed: false
357 });
358 }
359 }, {
360 key: "handleCancelEdit",
361 value: function handleCancelEdit() {
362 this.updateData({
363 isSaving: false
364 });
365 }
366 }, {
367 key: "handleCurrentIndexChanged",
368 value: function handleCurrentIndexChanged(currentIndex) {
369 if (currentRowOnly) {
370 this.updateData();
371 } else {
372 this.updateData();
373 this.setState({
374 currentIndex: currentIndex
375 });
376 }
377 }
378 }, {
379 key: "handleDataLoaded",
380 value: function handleDataLoaded() {
381 this.updateData({
382 isLoading: false,
383 isSaving: false,
384 isDeleting: false,
385 saveFailed: false
386 });
387 }
388 }, {
389 key: "handleDirtyChanged",
390 value: function handleDirtyChanged() {
391 this.setState({
392 isDirty: getDataObject().isDirty()
393 });
394 }
395 }, {
396 key: "refreshData",
397 value: function refreshData(callback) {
398 var dataObject = getDataObject();
399 return new Promise(function (resolve) {
400 dataObject.refreshDataSource(function (error, data) {
401 "function" === typeof callback && callback(error, data);
402 resolve({
403 data: data,
404 error: error
405 });
406 });
407 });
408 }
409 }, {
410 key: "refreshRow",
411 value: function refreshRow(callback) {
412 var dataObject = getDataObject();
413 return new Promise(function (resolve) {
414 dataObject.refreshCurrentRow(function (error, data) {
415 "function" === typeof callback && callback(error, data);
416 resolve({
417 data: data,
418 error: error
419 });
420 });
421 });
422 }
423 }, {
424 key: "setFieldValue",
425 value: function setFieldValue(name, value) {
426 getDataObject().currentRow(name, value);
427 this.updateData();
428 }
429 }, {
430 key: "setFieldValues",
431 value: function setFieldValues(fields) {
432 var dataObject = getDataObject();
433
434 for (var field in fields) {
435 if (Object.prototype.hasOwnProperty.call(fields, field)) {
436 dataObject.currentRow(field, fields[field]);
437 }
438 }
439
440 this.updateData();
441 }
442 }, {
443 key: "setCurrentIndex",
444 value: function setCurrentIndex(idx) {
445 getDataObject().setCurrentIndex(idx);
446 }
447 }, {
448 key: "setParameter",
449 value: function setParameter() {
450 var _getDataObject;
451
452 (_getDataObject = getDataObject()).setParameter.apply(_getDataObject, arguments);
453 }
454 }, {
455 key: "render",
456 value: function render() {
457 return React.createElement(WrappedComponent, _extends({}, this.state, {
458 onCancelEdit: this.cancelEdit,
459 onCurrentIndexChange: this.setCurrentIndex,
460 onDeleteRow: this.deleteRow,
461 onEndEdit: this.endEdit,
462 onFieldChange: this.setFieldValue,
463 onFieldsChange: this.setFieldValues,
464 onRefreshData: this.refreshData,
465 onRefreshRow: this.refreshRow,
466 onSetParameter: this.setParameter
467 }, this.props));
468 }
469 }]);
470
471 return connector;
472 }(React.Component);
473
474 function getDisplayName() {
475 return WrappedComponent.displayName || WrappedComponent.name || "Component";
476 }
477
478 connector.displayName = typeof dataObject === "string" ? dataObject : dataObject.getDataSourceId();
479 connector.displayName += "(".concat(getDisplayName(), ")");
480 return connector;
481 };
482 }
483 var properties = ["onCancelEdit", "onCurrentIndexChange", "onEndEdit", "onDeleteRow", "onFieldChange", "onFieldsChange", "onRefreshData", "onRefreshRow", "onSetParameter", "canDelete", "canUpdate", "canInsert", "currentIndex", "isDirty", "isDeleting", "isLoading", "isSaving", "loadError", "saveFailed"];
484 var connectRow = function connectRow(dataObject) {
485 return connect(dataObject, true);
486 };
487
488 exports.connect = connect;
489 exports.connectRow = connectRow;
490 exports.properties = properties;
491
492 Object.defineProperty(exports, '__esModule', { value: true });
493
494}));