@schukai/monster 3.70.0 → 3.71.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -77,9 +77,6 @@ class SaveButton extends CustomElement {
77
77
  * @property {Object} classes The classes
78
78
  * @property {string} classes.bar The bar class
79
79
  * @property {string} classes.badge The badge class
80
- * @property {object} mapping The mapping
81
- * @property {string} mapping.data The data
82
- * @property {number} mapping.index The index
83
80
  * @property {Array} ignoreChanges The ignore changes (regex)
84
81
  * @property {Array} data The data
85
82
  * @return {Object}
@@ -105,11 +102,6 @@ class SaveButton extends CustomElement {
105
102
 
106
103
  changes: "0",
107
104
 
108
- mapping: {
109
- data: "dataset",
110
- index: 0,
111
- },
112
-
113
105
  ignoreChanges: [],
114
106
 
115
107
  data: {},
@@ -164,13 +164,6 @@ class Form extends DataSet {
164
164
  }
165
165
 
166
166
  function initDataSourceHandler() {
167
- if (!this[datasourceLinkedElementSymbol]) {
168
- return;
169
- }
170
- console.log(this[datasourceLinkedElementSymbol]);
171
- this[datasourceLinkedElementSymbol].setOption("write.responseCallback", (response) => {
172
- console.log("response!!!", response);
173
- })
174
167
 
175
168
  }
176
169
 
@@ -13,7 +13,8 @@
13
13
  */
14
14
 
15
15
  import {internalSymbol, instanceSymbol} from "../../../constants.mjs";
16
- import {isObject, isFunction} from "../../../types/is.mjs";
16
+ import {isObject, isFunction, isArray} from "../../../types/is.mjs";
17
+ import {diff} from "../../diff.mjs";
17
18
  import {Server} from "../server.mjs";
18
19
  import {WriteError} from "./restapi/writeerror.mjs";
19
20
  import {DataFetchError} from "./restapi/data-fetch-error.mjs";
@@ -30,6 +31,7 @@ const rawDataSymbol = Symbol.for(
30
31
  "@schukai/monster/data/datasource/server/restapi/rawdata",
31
32
  );
32
33
 
34
+
33
35
  /**
34
36
  * The RestAPI is a class that enables a REST API server.
35
37
  *
@@ -75,6 +77,8 @@ class RestAPI extends Server {
75
77
  * @property {Monster.Data.Datasource~exampleCallback[]} write.mapping.callback with the help of the callback, the structures can be adjusted before writing.
76
78
  * @property {Object} write.report
77
79
  * @property {String} write.report.path Path to validations
80
+ * @property {Object} write.partial
81
+ * @property {Function} write.partial.callback Callback function to be executed after the request has been completed. (obj, diffResult) => obj
78
82
  * @property {Object} write.sheathing
79
83
  * @property {Object} write.sheathing.object Object to be wrapped
80
84
  * @property {string} write.sheathing.path Path to the data
@@ -107,6 +111,10 @@ class RestAPI extends Server {
107
111
  report: {
108
112
  path: undefined,
109
113
  },
114
+
115
+ partial: {
116
+ callback: null,
117
+ }
110
118
  },
111
119
  read: {
112
120
  init: {
@@ -135,10 +143,11 @@ class RestAPI extends Server {
135
143
  if (!init["method"]) init["method"] = "GET";
136
144
 
137
145
  let callback = this.getOption("read.responseCallback");
138
- if (!callback)
146
+ if (!callback) {
139
147
  callback = (obj) => {
140
148
  this.set(this.transformServerPayload.call(this, obj));
141
149
  };
150
+ }
142
151
 
143
152
  return fetchData.call(this, init, "read", callback);
144
153
  }
@@ -189,7 +198,7 @@ function fetchData(init, key, callback) {
189
198
  .then((resp) => {
190
199
  response = resp;
191
200
 
192
- const acceptedStatus = this.getOption(`${key}.acceptedStatus`, [200]).map(Number);
201
+ const acceptedStatus = this.getOption(`${key}.acceptedStatus`, [200]).map(Number);
193
202
 
194
203
  if (acceptedStatus.indexOf(resp.status) === -1) {
195
204
  throw new DataFetchError(
@@ -12,13 +12,21 @@
12
12
  * SPDX-License-Identifier: AGPL-3.0
13
13
  */
14
14
 
15
- import { internalSymbol, instanceSymbol } from "../../constants.mjs";
16
- import { isObject } from "../../types/is.mjs";
17
- import { Datasource } from "../datasource.mjs";
18
- import { Pathfinder } from "../pathfinder.mjs";
19
- import { Pipe } from "../pipe.mjs";
15
+ import {instanceSymbol} from "../../constants.mjs";
16
+ import {isArray, isFunction, isObject} from "../../types/is.mjs";
17
+ import {Datasource} from "../datasource.mjs";
18
+ import {diff} from "../diff.mjs";
19
+ import {Pathfinder} from "../pathfinder.mjs";
20
+ import {Pipe} from "../pipe.mjs";
20
21
 
21
- export { Server };
22
+ export {Server};
23
+
24
+
25
+ /**
26
+ * @private
27
+ * @type {symbol}
28
+ */
29
+ const serverVersionSymbol = Symbol("serverVersion");
22
30
 
23
31
  /**
24
32
  * Base class for all server data sources
@@ -30,54 +38,82 @@ export { Server };
30
38
  * @summary The Server class encapsulates the access to a server datasource
31
39
  */
32
40
  class Server extends Datasource {
33
- /**
34
- * This method is called by the `instanceof` operator.
35
- * @returns {symbol}
36
- */
37
- static get [instanceSymbol]() {
38
- return Symbol.for("@schukai/monster/data/datasource/server");
39
- }
40
-
41
- /**
42
- * This prepares the data that comes from the server.
43
- * Should not be called directly.
44
- *
45
- * @private
46
- * @param {Object} payload
47
- * @returns {Object}
48
- */
49
- transformServerPayload(payload) {
50
- payload = doTransform.call(this, "read", payload);
51
-
52
- const dataPath = this.getOption("read.path");
53
- if (dataPath) {
54
- payload = new Pathfinder(payload).getVia(dataPath);
55
- }
56
-
57
- return payload;
58
- }
59
-
60
- /**
61
- * This prepares the data for writing and should not be called directly.
62
- *
63
- * @private
64
- * @param {Object} payload
65
- * @returns {Object}
66
- */
67
- prepareServerPayload(payload) {
68
- payload = doTransform.call(this, "write", payload);
69
-
70
- const sheathingObject = this.getOption("write.sheathing.object");
71
- const sheathingPath = this.getOption("write.sheathing.path");
72
-
73
- if (sheathingObject && sheathingPath) {
74
- const sub = payload;
75
- payload = sheathingObject;
76
- new Pathfinder(payload).setVia(sheathingPath, sub);
77
- }
78
-
79
- return payload;
80
- }
41
+ /**
42
+ * This method is called by the `instanceof` operator.
43
+ * @returns {symbol}
44
+ */
45
+ static get [instanceSymbol]() {
46
+ return Symbol.for("@schukai/monster/data/datasource/server");
47
+ }
48
+
49
+ /**
50
+ * This prepares the data that comes from the server.
51
+ * Should not be called directly.
52
+ *
53
+ * @private
54
+ * @param {Object} payload
55
+ * @returns {Object}
56
+ */
57
+ transformServerPayload(payload) {
58
+ payload = doTransform.call(this, "read", payload);
59
+ this[serverVersionSymbol] = payload;
60
+
61
+ const dataPath = this.getOption("read.path");
62
+ if (dataPath) {
63
+ payload = new Pathfinder(payload).getVia(dataPath);
64
+ }
65
+
66
+ return payload;
67
+ }
68
+
69
+ /**
70
+ * This prepares the data for writing and should not be called directly.
71
+ *
72
+ * @private
73
+ * @param {Object} payload
74
+ * @returns {Object}
75
+ */
76
+ prepareServerPayload(payload) {
77
+ payload = doTransform.call(this, "write", payload);
78
+ payload = doDiff.call(this, payload);
79
+
80
+ const sheathingObject = this.getOption("write.sheathing.object");
81
+ const sheathingPath = this.getOption("write.sheathing.path");
82
+
83
+ if (sheathingObject && sheathingPath) {
84
+ const sub = payload;
85
+ payload = sheathingObject;
86
+ new Pathfinder(payload).setVia(sheathingPath, sub);
87
+ }
88
+
89
+ return payload;
90
+ }
91
+ }
92
+
93
+ /**
94
+ *
95
+ * @param obj
96
+ * @returns {*}
97
+ */
98
+ function doDiff(obj) {
99
+ if (this[serverVersionSymbol] === null || this[serverVersionSymbol] === undefined) {
100
+ return obj;
101
+ }
102
+
103
+ const callback = this.getOption("write.partial.callback");
104
+ if (!isFunction(callback)) {
105
+ return obj;
106
+ }
107
+
108
+ const results = diff(this[serverVersionSymbol], obj);
109
+ if (!results) {
110
+ return obj;
111
+ }
112
+
113
+ obj = callback(obj, results);
114
+ this[serverVersionSymbol] = obj;
115
+
116
+ return obj;
81
117
  }
82
118
 
83
119
  /**
@@ -87,24 +123,32 @@ class Server extends Datasource {
87
123
  * @returns {Object}
88
124
  */
89
125
  function doTransform(type, obj) {
90
- const transformation = this.getOption(`${type}.mapping.transformer`);
91
- if (transformation !== undefined && transformation !== null) {
92
- const pipe = new Pipe(transformation);
93
- const callbacks = this.getOption(`${type}.mapping.callbacks`);
94
-
95
- if (isObject(callbacks)) {
96
- for (const key in callbacks) {
97
- if (
98
- callbacks.hasOwnProperty(key) &&
99
- typeof callbacks[key] === "function"
100
- ) {
101
- pipe.setCallback(key, callbacks[key]);
102
- }
103
- }
104
- }
105
-
106
- obj = pipe.run(obj);
107
- }
108
-
109
- return obj;
126
+ const transformation = this.getOption(`${type}.mapping.transformer`);
127
+ if (transformation !== undefined && transformation !== null) {
128
+ const pipe = new Pipe(transformation);
129
+ const callbacks = this.getOption(`${type}.mapping.callbacks`);
130
+
131
+ if (isArray(callbacks)) {
132
+ for (const callback of callbacks) {
133
+ if (typeof callback === "function") {
134
+ pipe.setCallback(callback);
135
+ }
136
+ }
137
+ }
138
+
139
+ if (isObject(callbacks)) {
140
+ for (const key in callbacks) {
141
+ if (
142
+ callbacks.hasOwnProperty(key) &&
143
+ typeof callbacks[key] === "function"
144
+ ) {
145
+ pipe.setCallback(key, callbacks[key]);
146
+ }
147
+ }
148
+ }
149
+
150
+ obj = pipe.run(obj);
151
+ }
152
+
153
+ return obj;
110
154
  }
@@ -183,4 +183,4 @@ function getOperator(a, b) {
183
183
  }
184
184
 
185
185
  return operator;
186
- }
186
+ }
@@ -6,6 +6,43 @@ import {Queue} from "../../../source/types/queue.mjs";
6
6
 
7
7
  describe('Diff', function () {
8
8
 
9
+ describe('test to datasets', function () {
10
+
11
+ var obj1, obj2;
12
+
13
+ beforeEach(() => {
14
+ obj1 = [
15
+ {
16
+ "id": 1,
17
+ "name": "test"
18
+ },
19
+ {
20
+ "id": 2,
21
+ "name": "test2"
22
+ }
23
+ ]
24
+
25
+ obj2 = [
26
+ {
27
+ "id": 1,
28
+ "name": "test"
29
+ },
30
+ {
31
+ "id": "3",
32
+ "name": "test2"
33
+ }
34
+ ]
35
+
36
+ });
37
+
38
+ it('should return the difference between two datasets', function () {
39
+ let d = diff(obj1, obj2);
40
+ expect(JSON.stringify(d)).is.equal('[{"operator":"update","path":["1","id"],"first":{"value":2,"type":"number"},"second":{"value":"3","type":"string"}}]');
41
+ });
42
+
43
+
44
+ })
45
+
9
46
  describe('Diff special cases', function () {
10
47
 
11
48
  var obj1, obj2;
@@ -5,12 +5,6 @@
5
5
  <title>Mocha Monster</title>
6
6
  <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
7
7
  <link rel="stylesheet" href="mocha.css"/>
8
- <script id="polyfill" src="https://polyfill.io/v3/polyfill.min.js?features=AbortController,Array.prototype.entries,Array.prototype.fill,Array.prototype.find,Array.prototype.includes,Array.prototype.keys,Array.prototype.sort,Array.prototype.values,atob,AudioContext,blissfuljs,Blob,CharacterData.prototype.nextElementSibling,CharacterData.prototype.previousElementSibling,CharacterData.prototype.remove,console,console.error,console.log,CSS.supports,CustomEvent,DocumentFragment,DocumentFragment.prototype.append,DocumentFragment.prototype.prepend,DOMRect,DOMTokenList,DOMTokenList.prototype.forEach,DOMTokenList.prototype.replace,Element.prototype.append,Element.prototype.getAttributeNames,Element.prototype.prepend,Element.prototype.remove,Element.prototype.scroll,Event,EventSource,fetch,Function.prototype.name,globalThis,HTMLDocument,HTMLPictureElement,HTMLTemplateElement,IntersectionObserver,IntersectionObserverEntry,Intl,Intl.DateTimeFormat,Intl.NumberFormat,Intl.PluralRules,Map,Math.log2,MutationObserver,Node.prototype.contains,Node.prototype.getRootNode,Node.prototype.isConnected,Node.prototype.isSameNode,NodeList.prototype.forEach,Number.isFinite,Number.isInteger,Object.assign,Object.entries,Object.freeze,Object.getOwnPropertyNames,Object.prototype.toString,Reflect,Reflect.defineProperty,Reflect.get,Reflect.getOwnPropertyDescriptor,Reflect.setPrototypeOf,requestAnimationFrame,ResizeObserver,Set,String.prototype.includes,String.prototype.matchAll,String.prototype.padStart,Symbol,Symbol.for,Symbol.hasInstance,Symbol.iterator,TextDecoder,TextEncoder,Uint16Array,Uint8Array,URLSearchParams,WeakSet"
9
- src="https://polyfill.io/v3/polyfill.min.js?features=Array.from,Array.isArray,Array.prototype.entries,Array.prototype.every,Array.prototype.fill,Array.prototype.filter,Array.prototype.find,Array.prototype.forEach,Array.prototype.includes,Array.prototype.indexOf,Array.prototype.keys,Array.prototype.lastIndexOf,Array.prototype.map,Array.prototype.reduce,Array.prototype.some,Array.prototype.sort,Array.prototype.values,ArrayBuffer,atob,Blob,console,CustomEvent,DataView,Date.prototype.toISOString,document,Document,DocumentFragment,Element,Event,fetch,Function.prototype.bind,getComputedStyle,globalThis,HTMLDocument,HTMLTemplateElement,IntersectionObserver,Intl,JSON,Map,Math.log2,MutationObserver,Number.isFinite,Number.isInteger,Object.assign,Object.defineProperty,Object.entries,Object.freeze,Object.getOwnPropertyDescriptor,Object.getOwnPropertyNames,Object.getPrototypeOf,Object.keys,Promise,Reflect,Reflect.defineProperty,Reflect.get,Reflect.getOwnPropertyDescriptor,Reflect.setPrototypeOf,requestAnimationFrame,ResizeObserver,Set,String.prototype.endsWith,String.prototype.includes,String.prototype.matchAll,String.prototype.padStart,String.prototype.startsWith,String.prototype.trim,Symbol,Symbol.for,Symbol.hasInstance,Symbol.iterator,Uint16Array,Uint8Array,URL,WeakMap,WeakSet"
10
- src="https://polyfill.io/v3/polyfill.min.js?features=Array.from,Array.isArray,Array.prototype.entries,Array.prototype.fill,Array.prototype.forEach,Array.prototype.indexOf,Array.prototype.keys,Array.prototype.lastIndexOf,Array.prototype.map,Array.prototype.reduce,Array.prototype.sort,ArrayBuffer,atob,DataView,document,DocumentFragment,Element,Event,globalThis,HTMLDocument,HTMLTemplateElement,JSON,Map,Math.log2,Number.isInteger,Object.assign,Object.defineProperty,Object.entries,Object.getOwnPropertyDescriptor,Object.getPrototypeOf,Object.keys,Promise,Reflect,Reflect.defineProperty,Reflect.get,Reflect.getOwnPropertyDescriptor,Reflect.setPrototypeOf,Set,String.prototype.endsWith,String.prototype.matchAll,String.prototype.padStart,String.prototype.startsWith,String.prototype.trim,Symbol,Symbol.iterator,WeakMap,WeakSet"
11
- crossorigin="anonymous"
12
- referrerpolicy="no-referrer"></script>
13
- <script src="https://cdn.jsdelivr.net/npm/element-internals-polyfill"></script>
14
8
 
15
9
  </head>
16
10
  <body>