@gudhub/core 1.0.59 → 1.0.63

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.
@@ -188,33 +188,12 @@ export class GHConstructor {
188
188
 
189
189
  //*************** GET INTERPRETATION ****************//
190
190
 
191
- getInterpretation: function (value, field, dataType, interpretation_id, itemId, appId) {
192
- return new Promise(async (resolve) => {
193
- let currentDataType = angularModule;
194
-
195
- let interpr_arr = await currentDataType.getInterpretation(value, field, dataType, interpretation_id, itemId, appId);
196
- if (interpr_arr) {
197
- let data = interpr_arr.find((item) => item.id == interpretation_id) || interpr_arr.find((item) => item.id == 'default');
198
-
199
- resolve({
200
- html: data.html
201
- })
202
- } else {
203
- resolve({
204
- html: '<span>error(from constructor)</span>'
205
- })
206
- }
207
- });
208
- },
209
-
210
- //*************** GET INTERPRETATION V2 ****************//
211
-
212
- getInterpretationNew: function(value, interpretation_id, dataType, field, itemId, appId) {
191
+ getInterpretation: function(value, interpretation_id, dataType, field, itemId, appId) {
213
192
  return new Promise(async (resolve) => {
214
193
  let currentDataType = factoryReturns[module_id];
215
194
 
216
195
  try {
217
- let interpr_arr = await currentDataType.getInterpretationNew(value, appId, itemId, field, dataType);
196
+ let interpr_arr = await currentDataType.getInterpretation(value, appId, itemId, field, dataType);
218
197
  let data = interpr_arr.find((item) => item.id == interpretation_id) || interpr_arr.find((item) => item.id == 'default');
219
198
 
220
199
  let result = await data.content()
@@ -38,7 +38,7 @@ export class Interpritate {
38
38
  /*-- if we have data_type then we construct new data object to interpritate value*/
39
39
  gudhub.ghconstructor.getInstance(data_type).then(function (data) {
40
40
  var interpretationObj = self.getInterpretationObj(field, data.getTemplate().model, source, containerId);
41
- data.getInterpretationNew(value, interpretationObj.id, data_type, field, itemId, appId).then(function (result) {
41
+ data.getInterpretation(value, interpretationObj.id, data_type, field, itemId, appId).then(function (result) {
42
42
  // console.log(result, interpretationObj)
43
43
 
44
44
  resolve(gudhub.mergeObjects(result, interpretationObj));
@@ -80,7 +80,7 @@ export class Interpritate {
80
80
  resolve('');
81
81
  }
82
82
  this.gudhub.ghconstructor.getInstance(fieldModel.data_type).then(async (instance) => {
83
- let interpretatedValue = await instance.getInterpretationNew(fieldValue, interpretationId, fieldModel.data_type, fieldModel, itemId, appId);
83
+ let interpretatedValue = await instance.getInterpretation(fieldValue, interpretationId, fieldModel.data_type, fieldModel, itemId, appId);
84
84
  if(interpretatedValue.html == '<span>no interpretation</span>') {
85
85
  resolve(fieldValue);
86
86
  } else {
@@ -0,0 +1,52 @@
1
+ export default class ItemsSelection {
2
+ constructor(gudhub) {
3
+ this.gudhub = gudhub;
4
+ this.selectedItems = {}
5
+ }
6
+
7
+ async selectItems(appId, items) {
8
+ if(!this.selectedItems.hasOwnProperty(appId)) {
9
+ this.selectedItems[appId] = {
10
+ items: [],
11
+ app: []
12
+ }
13
+
14
+ let app = await this.gudhub.getApp(appId);
15
+ this.selectedItems[appId].app = app;
16
+ }
17
+
18
+ let itemObject = this.selectedItems[appId];
19
+
20
+ let selectedItemsIds = itemObject.items.map(obj => obj.item_id);
21
+
22
+ items.forEach(item => {
23
+ if(selectedItemsIds.indexOf(item.item_id) != -1) {
24
+ itemObject.items.splice(selectedItemsIds.indexOf(item.item_id), 1);
25
+ } else {
26
+ itemObject.items[itemObject.items.length] = item;
27
+ }
28
+ });
29
+
30
+ }
31
+
32
+ getSelectedItems(appId) {
33
+ return this.selectedItems[appId] ? this.selectedItems[appId] : { items: [], app: [] };
34
+ }
35
+
36
+ clearSelectedItems(appId) {
37
+ if(this.selectedItems[appId]) {
38
+ this.selectedItems[appId].items = [];
39
+ }
40
+ }
41
+
42
+ isItemSelected(appId, itemId) {
43
+ let array = this.getSelectedItems(appId);
44
+ if(array !== null) {
45
+ array = array.items.map(obj => obj.item_id);
46
+ return array.indexOf(itemId) != -1;
47
+ } else {
48
+ return false;
49
+ }
50
+ }
51
+
52
+ }
@@ -0,0 +1,62 @@
1
+ import should from "should";
2
+ import {GudHub} from '../../gudhub.js';
3
+
4
+ describe("ITEMS SELECTION", async function() {
5
+ const auth_key = 'Z/lxMHLenEaQTvPjW5U6c3jBDwWFYZrh2F9Kxa3fbt8drvabS2u2lXQ2zI+SRmic';
6
+ const gudhub = new GudHub(auth_key);
7
+
8
+ const appId = 16259;
9
+
10
+ const items = [
11
+ {
12
+ "item_id": 1,
13
+ "last_update": 1123669745123,
14
+ "fields": []
15
+ },
16
+ {
17
+ "item_id": 2,
18
+ "last_update": 1985625103278,
19
+ "fields": []
20
+ },
21
+ {
22
+ "item_id": 3,
23
+ "last_update": 1614725478960,
24
+ "fields": []
25
+ },
26
+ ]
27
+
28
+ // ORDER OF TEST IS IMPORTANT!
29
+
30
+ it('SELECT AND GET SELECTED ITEM', async function() {
31
+ await gudhub.util.selectItems(appId, [items[0]]);
32
+
33
+ let selectedItems = gudhub.util.getSelectedItems(appId);
34
+
35
+ selectedItems.items.length.should.equals(1);
36
+ });
37
+
38
+ it('CHECK IF ITEM SELECTED', async function() {
39
+ let isItemSelected = gudhub.util.isItemSelected(appId, 1);
40
+ isItemSelected.should.equals(true);
41
+
42
+ isItemSelected = gudhub.util.isItemSelected(appId, 2);
43
+ isItemSelected.should.equals(false);
44
+ });
45
+
46
+ it('CLEAR SELECTED ITEMS', async function() {
47
+ gudhub.util.clearSelectedItems(appId);
48
+
49
+ let selectedItems = gudhub.util.getSelectedItems(appId);
50
+
51
+ selectedItems.items.length.should.equals(0);
52
+ });
53
+
54
+ it('SELECT MULTIPLE ITEMS', async function() {
55
+ await gudhub.util.selectItems(appId, items);
56
+
57
+ let selectedItems = gudhub.util.getSelectedItems(appId);
58
+
59
+ selectedItems.items.length.should.equals(3);
60
+ })
61
+
62
+ });
@@ -0,0 +1,146 @@
1
+ export default class FieldsOperator {
2
+ constructor(gudhub) {
3
+ this.gudhub = gudhub;
4
+ }
5
+
6
+ mergeFieldLists(fieldsToView, fieldList) {
7
+ let fieldsToViewCopy = JSON.parse(JSON.stringify(fieldsToView));
8
+ let fieldListCopy = JSON.parse(JSON.stringify(fieldList));
9
+
10
+ let existingFieldsIds = fieldListCopy.map(field => field.field_id);
11
+
12
+ if (!fieldListCopy.length) {
13
+ return [];
14
+ }
15
+
16
+ return fieldListCopy.reduce((viewArray, currentField) => {
17
+
18
+ let positionInViewArray = null;
19
+ let props = viewArray.find((view, index) => {
20
+ if (currentField.field_id == view.field_id) {
21
+ positionInViewArray = index;
22
+ return true;
23
+ }
24
+ return false;
25
+ });
26
+
27
+ if (!props) {
28
+
29
+ let defaultIntrpr = currentField.data_model && currentField.data_model.interpretation ? currentField.data_model.interpretation.find((intrpr) => {
30
+ return intrpr.src == 'table';
31
+ }) || { settings: { show_field: 1 } } : { settings: { show_field: 1 } };
32
+
33
+ if (defaultIntrpr.settings.show_field) {
34
+ let firstMerge = this.gudhub.util.mergeObjects({ show: 1 }, defaultIntrpr);
35
+ let secondMerge = this.gudhub.util.mergeObjects(firstMerge, currentField);
36
+ viewArray.push(secondMerge);
37
+ }
38
+
39
+ } else {
40
+ viewArray[positionInViewArray] = this.gudhub.util.mergeObjects(viewArray[positionInViewArray], currentField);
41
+ }
42
+
43
+ return viewArray;
44
+ }, fieldsToViewCopy).filter(function (field) {
45
+ return field.show && existingFieldsIds.indexOf(field.field_id) != -1;
46
+ });
47
+ };
48
+
49
+ createFieldsListToView(appId, tableSettingsFieldListToView) {
50
+ const self = this;
51
+ return new Promise(async (resolve) => {
52
+ let fieldList = await this.gudhub.getFieldModels(appId);
53
+
54
+ if (tableSettingsFieldListToView.length !== 0) {
55
+ let correctFieldList = [];
56
+ tableSettingsFieldListToView.forEach((elementFromTable, index) => {
57
+ let fieldOnPipe = fieldList.find((element) => elementFromTable.field_id == element.field_id);
58
+
59
+ if (fieldOnPipe) {
60
+ let show_field = fieldOnPipe.data_model.interpretation.find((intrpr) => intrpr.src == 'table');
61
+ if (!show_field) {
62
+ show_field = { settings: { show_field: 1 } }
63
+ }
64
+ correctFieldList.push({
65
+ field_id: fieldOnPipe.field_id,
66
+ show: elementFromTable.show,
67
+ width: elementFromTable.width ? elementFromTable.width : "150px"
68
+ })
69
+ }
70
+ if (index + 1 == tableSettingsFieldListToView.length) {
71
+ checkFiledFromPipe(correctFieldList)
72
+ }
73
+
74
+ });
75
+ } else {
76
+ checkFiledFromPipe(tableSettingsFieldListToView);
77
+ }
78
+
79
+ function checkFiledFromPipe(correctFieldList) {
80
+ let counter = 0
81
+ fieldList.forEach((elemetFromPipe, index) => {
82
+ let correctFiel = correctFieldList.find((elemet) => elemetFromPipe.field_id == elemet.field_id)
83
+ if (!correctFiel) {
84
+ self.gudhub.ghconstructor.getInstance(elemetFromPipe.data_type).then((data) => {
85
+ let template = data.getTemplate();
86
+ let show_field = template.model.data_model.interpretation.find((intrpr) => intrpr.src == 'table');
87
+ if (!show_field) {
88
+ show_field = { settings: { show_field: 1 } }
89
+ }
90
+ correctFieldList.push({
91
+ field_id: elemetFromPipe.field_id,
92
+ show: show_field.settings.show_field,
93
+ });
94
+
95
+ if (counter == fieldList.length - 1) {
96
+ resolve(correctFieldList);
97
+ }
98
+ counter++
99
+ }, function errorCallback(result) {
100
+ counter++
101
+ })
102
+ } else {
103
+ if (counter == fieldList.length - 1) {
104
+ resolve(correctFieldList);
105
+ }
106
+ counter++
107
+ }
108
+ });
109
+ }
110
+ });
111
+ }
112
+
113
+ createFieldsListToViewWithDataType(appId, tableSettingsFieldListToView) {
114
+ return new Promise(async (resolve) => {
115
+ let columnsToView = [];
116
+ let fieldList = await this.gudhub.getFieldModels(appId);
117
+
118
+ let actualFieldIds = fieldList.map((field) => +field.field_id);
119
+
120
+ columnsToView = fieldList.reduce((viewColumsList, currentField) => {
121
+ let field = viewColumsList.find((view, index) => {
122
+ if (currentField.field_id == view.field_id) {
123
+ return true;
124
+ }
125
+ return false;
126
+ });
127
+
128
+ if (!field) {
129
+ this.gudhub.ghconstructor.getInstance(currentField.data_type).then(instance => {
130
+ let template = instance.getTemplate();
131
+ let defaultIntepretation = template.model.data_model.interpretation.find((intrpr) => intrpr.src == 'table') || { settings: { show_field: 1 } };
132
+ viewColumsList.push({
133
+ field_id: currentField.field_id,
134
+ data_type: currentField.data_type,
135
+ show: defaultIntepretation.settings.show_field
136
+ });
137
+ })
138
+ }
139
+
140
+ return viewColumsList;
141
+ }, tableSettingsFieldListToView).filter((field) => actualFieldIds.indexOf(field.field_id) != -1);
142
+
143
+ resolve(columnsToView);
144
+ })
145
+ }
146
+ }
@@ -0,0 +1,38 @@
1
+ import should from "should";
2
+ import {GudHub} from '../../gudhub.js';
3
+
4
+ import table_field_list from './mocks/table_field_list.js';
5
+ import table_fields_to_view from './mocks/table_fields_to_view.js';
6
+
7
+ import online_inventory_field_list from './mocks/online_inventory_field_list.js';
8
+ import online_inventory_fields_to_view from './mocks/online_inventory_fields_to_view.js';
9
+
10
+ import table_settings_field_list_to_view from './mocks/table_settings_field_list_to_view.js';
11
+
12
+ describe("MERGE FIELDS", async function() {
13
+ const auth_key = 'Z/lxMHLenEaQTvPjW5U6c3jBDwWFYZrh2F9Kxa3fbt8drvabS2u2lXQ2zI+SRmic';
14
+ const gudhub = new GudHub(auth_key);
15
+
16
+ it("MERGE FIELDS LIST", async function () {
17
+ let fieldsToView = table_fields_to_view;
18
+ let fieldList = table_field_list;
19
+ let result = gudhub.util.mergeFieldLists(fieldsToView, fieldList);
20
+ result[0].field_id.should.equals(190);
21
+ result[1].data_model.interpretation.length.should.equals(1);
22
+
23
+ fieldsToView = online_inventory_fields_to_view;
24
+ fieldList = online_inventory_field_list;
25
+ result = gudhub.util.mergeFieldLists(fieldsToView, fieldList);
26
+ result[0].field_id.should.equals(192954);
27
+ result[2].data_model.interpretation.length.should.equals(6);
28
+ });
29
+
30
+ it("CREATES FIELDS LIST TO VIEW", async function () {
31
+ let tableSettingsFieldListToView = table_settings_field_list_to_view;
32
+ let appId = 16259;
33
+ let result = await gudhub.util.createFieldsListToView(appId, tableSettingsFieldListToView);
34
+ result.length.should.equals(12);
35
+ result[1].field_id.should.equals(254055);
36
+ });
37
+
38
+ });