]> git.proxmox.com Git - proxmox-widget-toolkit.git/blame - data/reader/JsonObject.js
use eslint and execute as check target
[proxmox-widget-toolkit.git] / data / reader / JsonObject.js
CommitLineData
0bb29d35 1/* A reader to store a single JSON Object (hash) into a storage.
13fc756d 2 * Also accepts an array containing a single hash.
0bb29d35
DM
3 *
4 * So it can read:
5 *
13fc756d 6 * example1: {data1: "xyz", data2: "abc"}
0bb29d35
DM
7 * returns [{key: "data1", value: "xyz"}, {key: "data2", value: "abc"}]
8 *
13fc756d 9 * example2: [ {data1: "xyz", data2: "abc"} ]
0bb29d35
DM
10 * returns [{key: "data1", value: "xyz"}, {key: "data2", value: "abc"}]
11 *
12 * If you set 'readArray', the reader expexts the object as array:
13 *
14 * example3: [ { key: "data1", value: "xyz", p2: "cde" }, { key: "data2", value: "abc", p2: "efg" }]
15 * returns [{key: "data1", value: "xyz", p2: "cde}, {key: "data2", value: "abc", p2: "efg"}]
16 *
17 * Note: The records can contain additional properties (like 'p2' above) when you use 'readArray'
18 *
19 * Additional feature: specify allowed properties with default values with 'rows' object
20 *
05a977a2 21 * let rows = {
0bb29d35
DM
22 * memory: {
23 * required: true,
24 * defaultValue: 512
25 * }
26 * }
27 *
28 */
29
30Ext.define('Proxmox.data.reader.JsonObject', {
31 extend: 'Ext.data.reader.Json',
01031528 32 alias: 'reader.jsonobject',
13fc756d 33
0bb29d35
DM
34 readArray: false,
35
36 rows: undefined,
37
38 constructor: function(config) {
05a977a2 39 let me = this;
0bb29d35
DM
40
41 Ext.apply(me, config || {});
42
43 me.callParent([config]);
44 },
45
46 getResponseData: function(response) {
05a977a2 47 let me = this;
0bb29d35 48
05a977a2 49 let data = [];
0bb29d35 50 try {
05a977a2 51 let result = Ext.decode(response.responseText);
0bb29d35 52 // get our data items inside the server response
05a977a2 53 let root = result[me.getRootProperty()];
0bb29d35
DM
54
55 if (me.readArray) {
05a977a2 56 let rec_hash = {};
0bb29d35
DM
57 Ext.Array.each(root, function(rec) {
58 if (Ext.isDefined(rec.key)) {
59 rec_hash[rec.key] = rec;
60 }
61 });
62
63 if (me.rows) {
64 Ext.Object.each(me.rows, function(key, rowdef) {
05a977a2 65 let rec = rec_hash[key];
0bb29d35
DM
66 if (Ext.isDefined(rec)) {
67 if (!Ext.isDefined(rec.value)) {
68 rec.value = rowdef.defaultValue;
69 }
70 data.push(rec);
71 } else if (Ext.isDefined(rowdef.defaultValue)) {
01031528 72 data.push({ key: key, value: rowdef.defaultValue });
0bb29d35 73 } else if (rowdef.required) {
01031528 74 data.push({ key: key, value: undefined });
0bb29d35
DM
75 }
76 });
77 } else {
78 Ext.Array.each(root, function(rec) {
79 if (Ext.isDefined(rec.key)) {
80 data.push(rec);
81 }
82 });
83 }
13fc756d 84 } else {
05a977a2 85 let org_root = root;
0bb29d35
DM
86
87 if (Ext.isArray(org_root)) {
05a977a2 88 if (root.length === 1) {
0bb29d35
DM
89 root = org_root[0];
90 } else {
91 root = {};
92 }
93 }
94
95 if (me.rows) {
96 Ext.Object.each(me.rows, function(key, rowdef) {
97 if (Ext.isDefined(root[key])) {
01031528 98 data.push({ key: key, value: root[key] });
0bb29d35 99 } else if (Ext.isDefined(rowdef.defaultValue)) {
01031528 100 data.push({ key: key, value: rowdef.defaultValue });
0bb29d35 101 } else if (rowdef.required) {
01031528 102 data.push({ key: key, value: undefined });
0bb29d35
DM
103 }
104 });
105 } else {
106 Ext.Object.each(root, function(key, value) {
01031528 107 data.push({ key: key, value: value });
0bb29d35
DM
108 });
109 }
110 }
01031528 111 } catch (ex) {
0bb29d35
DM
112 Ext.Error.raise({
113 response: response,
114 json: response.responseText,
115 parseError: ex,
01031528 116 msg: 'Unable to parse the JSON returned by the server: ' + ex.toString(),
0bb29d35
DM
117 });
118 }
119
120 return data;
01031528 121 },
0bb29d35
DM
122});
123