cx 22.4.1 → 22.4.2

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cx",
3
- "version": "22.4.1",
3
+ "version": "22.4.2",
4
4
  "description": "Advanced JavaScript UI framework for admin and dashboard applications with ready to use grid, form and chart components.",
5
5
  "main": "index.js",
6
6
  "jsnext:main": "src/index.js",
package/src/core.d.ts CHANGED
@@ -29,6 +29,8 @@ declare namespace Cx {
29
29
 
30
30
  type AccessorChain<M> = {
31
31
  toString(): string;
32
+ valueOf(): string;
33
+ nameOf(): string;
32
34
  } & {
33
35
  [prop in keyof M]: AccessorChain<M[prop]>;
34
36
  };
@@ -1,38 +1,38 @@
1
- import {sorter} from './comparer';
2
- import assert from 'assert';
1
+ import { sorter } from "./comparer";
2
+ import assert from "assert";
3
3
 
4
- describe('comparer', function() {
5
- describe('sorter', function() {
6
- it('orders by numeric values ASC', function () {
4
+ describe("comparer", function () {
5
+ describe("sorter", function () {
6
+ it("orders by numeric values ASC", function () {
7
7
  let records = [
8
- {value: 3, id: 1},
9
- {value: -1, id: 2},
10
- {value: 2, id: 3},
11
- {value: null, id: 4},
12
- {value: null, id: 5},
8
+ { value: 3, id: 1 },
9
+ { value: -1, id: 2 },
10
+ { value: 2, id: 3 },
11
+ { value: null, id: 4 },
12
+ { value: null, id: 5 },
13
13
  ];
14
14
 
15
- let sort = sorter([{value: {bind: 'value'}, direction: 'ASC'}]);
15
+ let sort = sorter([{ value: { bind: "value" }, direction: "ASC" }]);
16
16
 
17
17
  let sorted = sort(records);
18
18
 
19
- assert.equal(sorted[0].value, null);
20
- assert.equal(sorted[1].value, null);
21
- assert.equal(sorted[2].value, -1);
22
- assert.equal(sorted[3].value, 2);
23
- assert.equal(sorted[4].value, 3);
19
+ assert.equal(sorted[0].value, -1);
20
+ assert.equal(sorted[1].value, 2);
21
+ assert.equal(sorted[2].value, 3);
22
+ assert.equal(sorted[3].value, null);
23
+ assert.equal(sorted[4].value, null);
24
24
  });
25
25
 
26
- it('orders by numeric values DESC', function () {
26
+ it("orders by numeric values DESC", function () {
27
27
  let records = [
28
- {value: 3, id: 1},
29
- {value: 1, id: 2},
30
- {value: -2, id: 3},
31
- {value: null, id: 4},
32
- {value: null, id: 5},
28
+ { value: 3, id: 1 },
29
+ { value: 1, id: 2 },
30
+ { value: -2, id: 3 },
31
+ { value: null, id: 4 },
32
+ { value: null, id: 5 },
33
33
  ];
34
34
 
35
- let sort = sorter([{value: {bind: 'value'}, direction: 'DESC'}]);
35
+ let sort = sorter([{ value: { bind: "value" }, direction: "DESC" }]);
36
36
 
37
37
  let sorted = sort(records);
38
38
 
@@ -43,14 +43,14 @@ describe('comparer', function() {
43
43
  assert.equal(sorted[4].value, null);
44
44
  });
45
45
 
46
- it('supports field', function () {
46
+ it("supports field", function () {
47
47
  let records = [
48
- {value: 3, id: 1},
49
- {value: 1, id: 2},
50
- {value: 2, id: 3}
48
+ { value: 3, id: 1 },
49
+ { value: 1, id: 2 },
50
+ { value: 2, id: 3 },
51
51
  ];
52
52
 
53
- let sort = sorter([{field: 'value', direction: 'ASC'}]);
53
+ let sort = sorter([{ field: "value", direction: "ASC" }]);
54
54
  let sorted = sort(records);
55
55
  assert.equal(sorted[0].value, 1);
56
56
  assert.equal(sorted[1].value, 2);
@@ -1,21 +1,43 @@
1
+ const emptyFn = () => {};
2
+
1
3
  export function createAccessorModelProxy(chain = "") {
2
- const proxy = new Proxy(() => {}, {
3
- get: (target, name) => {
4
- if (name === "isAccessorChain") return true;
5
- if (typeof name !== "string") return this;
6
- if (name === "toString" || name === "valueOf") return proxy;
4
+ let lastOp = null;
5
+ let lastName = null;
6
+
7
+ const proxy = new Proxy(emptyFn, {
8
+ get: (_, name) => {
9
+ if (typeof name !== "string") return proxy;
10
+
11
+ switch (name) {
12
+ case "isAccessorChain":
13
+ return true;
14
+
15
+ case "toString":
16
+ case "valueOf":
17
+ case "nameOf":
18
+ lastOp = name;
19
+ return proxy;
20
+ }
7
21
 
8
22
  let newChain = chain;
9
23
  if (newChain.length > 0) newChain += ".";
10
24
  newChain += name;
25
+ lastName = name;
11
26
  return createAccessorModelProxy(newChain);
12
27
  },
13
28
 
14
29
  apply() {
15
- return chain;
30
+ switch (lastOp) {
31
+ case "nameOf":
32
+ if (lastName != null) return lastName;
33
+ const lastDotIndex = chain.lastIndexOf(".");
34
+ return lastDotIndex > 0 ? chain.substring(lastDotIndex + 1) : chain;
35
+
36
+ default:
37
+ return chain;
38
+ }
16
39
  },
17
40
  });
18
- proxy.isAccessorChain = true;
19
41
  return proxy;
20
42
  }
21
43
 
@@ -5,19 +5,33 @@ interface Model {
5
5
  firstName: string;
6
6
  address: {
7
7
  city: string;
8
+ streetNumber: number;
8
9
  };
9
10
  }
10
11
 
11
12
  describe("createAccessorModelProxy", () => {
12
13
  it("generates correct paths", () => {
13
- var model = createAccessorModelProxy<Model>();
14
+ let model = createAccessorModelProxy<Model>();
14
15
  assert.strictEqual(model.firstName.toString(), "firstName");
15
16
  assert.strictEqual(model.address.toString(), "address");
16
17
  assert.strictEqual(model.address.city.toString(), "address.city");
17
18
  });
18
19
 
19
20
  it("can be used in string templates", () => {
20
- var model = createAccessorModelProxy<Model>();
21
+ let model = createAccessorModelProxy<Model>();
21
22
  assert.strictEqual("address.city", `${model.address.city}`);
23
+ assert.strictEqual("address.city", "" + model.address.city);
24
+ assert.strictEqual("address.city.suffix", model.address.city + ".suffix");
25
+ });
26
+
27
+ it("nameOf returns name of the last prop ", () => {
28
+ let model = createAccessorModelProxy<Model>();
29
+ assert.strictEqual(model.firstName.nameOf(), "firstName");
30
+ assert.strictEqual(model.address.nameOf(), "address");
31
+ assert.strictEqual(model.address.city.nameOf(), "city");
32
+
33
+ let { streetNumber, city } = model.address;
34
+ assert.strictEqual(streetNumber.nameOf(), "streetNumber");
35
+ assert.strictEqual(city.nameOf(), "city");
22
36
  });
23
37
  });
@@ -18,6 +18,26 @@ export class Controller<D = any> {
18
18
  store: View<D>;
19
19
  widget: any;
20
20
 
21
+ addTrigger<V1>(name: string, args: [Cx.AccessorChain<V1>], callback: (v1: V1) => void, autoRun?: boolean): void;
22
+ addTrigger<V1, V2>(
23
+ name: string,
24
+ args: [Cx.AccessorChain<V1>, Cx.AccessorChain<V2>],
25
+ callback: (v1: V1, v2: V2) => void,
26
+ autoRun?: boolean
27
+ ): void;
28
+ addTrigger<V1, V2, V3>(
29
+ name: string,
30
+ args: [Cx.AccessorChain<V1>, Cx.AccessorChain<V2>, Cx.AccessorChain<V3>],
31
+ callback: (v1: V1, v2: V2, v3: V3) => void,
32
+ autoRun?: boolean
33
+ ): void;
34
+ addTrigger<V1, V2, V3, V4>(
35
+ name: string,
36
+ args: [Cx.AccessorChain<V1>, Cx.AccessorChain<V2>, Cx.AccessorChain<V3>, Cx.AccessorChain<V4>],
37
+ callback: (v1: V1, v2: V2, v3: V3, v4: V4) => void,
38
+ autoRun?: boolean
39
+ ): void;
40
+
21
41
  addTrigger(
22
42
  name: string,
23
43
  args: (string | Cx.AccessorChain<any>)[],
@@ -25,7 +45,24 @@ export class Controller<D = any> {
25
45
  autoRun?: boolean
26
46
  ): void;
27
47
 
28
- addComputable(name: string, args: string[], callback: (...args) => any): void;
48
+ addComputable<V1, R>(path: Cx.AccessorChain<R>, args: [Cx.AccessorChain<V1>], callback: (v1: V1) => R): void;
49
+ addComputable<V1, V2, R>(
50
+ path: Cx.AccessorChain<R>,
51
+ args: [Cx.AccessorChain<V1>, Cx.AccessorChain<V2>],
52
+ callback: (v1: V1, v2: V2) => R
53
+ ): void;
54
+ addComputable<V1, V2, V3, R>(
55
+ path: Cx.AccessorChain<R>,
56
+ args: [Cx.AccessorChain<V1>, Cx.AccessorChain<V2>, Cx.AccessorChain<V3>],
57
+ callback: (v1: V1, v2: V2, v3: V3) => R
58
+ ): void;
59
+ addComputable<V1, V2, V3, V4, R>(
60
+ path: Cx.AccessorChain<R>,
61
+ args: [Cx.AccessorChain<V1>, Cx.AccessorChain<V2>, Cx.AccessorChain<V3>, Cx.AccessorChain<V4>],
62
+ callback: (v1: V1, v2: V2, v3: V3, v4: V4) => R
63
+ ): void;
64
+
65
+ addComputable(path: string, args: (string | Cx.AccessorChain<any>)[], callback: (...args) => any): void;
29
66
 
30
67
  removeTrigger(name: string): void;
31
68