@gudhub/core 1.0.57 → 1.0.61

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.
@@ -11,6 +11,57 @@ let $q = {
11
11
  }
12
12
  }
13
13
 
14
+ /*************** FAKE ANGULAR ***************/
15
+ // It's needed when we eval() angular code.
16
+
17
+ let factoryReturns = {};
18
+
19
+ let angular = {
20
+ module() {
21
+ return angular;
22
+ },
23
+ directive() {
24
+ return angular;
25
+ },
26
+ factory(name, args) {
27
+ try {
28
+ if(typeof args === 'object') {
29
+ factoryReturns[name] = args[args.length - 1]($q);
30
+ } else {
31
+ factoryReturns[name] = args($q);
32
+ }
33
+ } catch(error) {
34
+ console.log('Errror in data type: ', name);
35
+ console.log(error);
36
+ }
37
+ return angular;
38
+ },
39
+ service() {
40
+ return angular;
41
+ },
42
+ run() {
43
+ return angular;
44
+ },
45
+ filter() {
46
+ return angular;
47
+ },
48
+ controller() {
49
+ return angular;
50
+ },
51
+ value() {
52
+ return angular;
53
+ },
54
+ element() {
55
+ return angular;
56
+ },
57
+ injector() {
58
+ return angular;
59
+ },
60
+ invoke() {
61
+ return angular;
62
+ }
63
+ }
64
+
14
65
  export class GHConstructor {
15
66
  constructor() {
16
67
  this.cache = [];
@@ -23,7 +74,7 @@ export class GHConstructor {
23
74
  // If not, check if module is in cache. If not, create new instance and put loading to modules query.
24
75
  // If yes, return module from cache.
25
76
 
26
- getInstance(module_id) {
77
+ async getInstance(module_id) {
27
78
  return new Promise(async (resolve) => {
28
79
  if (this.modulesQueue[module_id]) {
29
80
  return this.modulesQueue[module_id].then(() => {
@@ -64,8 +115,8 @@ export class GHConstructor {
64
115
 
65
116
  /*************** CREATE INSTANCE ***************/
66
117
  // Get angular module and module's code.
67
- // Then parsing module's code.
68
- // Finally, creating instance of module, using functions from angular module and parsed code.
118
+ // Then eval() module's code.
119
+ // Finally, creating instance of module, using functions from angular module and evaled code.
69
120
 
70
121
  async createInstance(module_id) {
71
122
  let module_url = gudhub.storage.getModuleUrl(module_id);
@@ -78,13 +129,11 @@ export class GHConstructor {
78
129
 
79
130
  let module = await axios.get(module_url);
80
131
  module = module.data;
81
- let returningObject = this.parseModule(module, module_id);
82
- if(!returningObject) return false;
83
- let obj;
132
+
84
133
  try {
85
- eval('obj = ' + returningObject);
134
+ eval(module);
86
135
  } catch(error) {
87
- console.log(`Error in ghconstructor (${module_id})`, error);
136
+ console.error('ERROR WHILE EVAL() IN GHCONSTRUCTOR', error);
88
137
  }
89
138
 
90
139
  let result = {
@@ -141,7 +190,7 @@ export class GHConstructor {
141
190
 
142
191
  getInterpretation: function (value, field, dataType, interpretation_id, itemId, appId) {
143
192
  return new Promise(async (resolve) => {
144
- let currentDataType = obj;
193
+ let currentDataType = angularModule;
145
194
 
146
195
  let interpr_arr = await currentDataType.getInterpretation(value, field, dataType, interpretation_id, itemId, appId);
147
196
  if (interpr_arr) {
@@ -158,17 +207,36 @@ export class GHConstructor {
158
207
  });
159
208
  },
160
209
 
210
+ //*************** GET INTERPRETATION V2 ****************//
211
+
212
+ getInterpretationNew: function(value, interpretation_id, dataType, field, itemId, appId) {
213
+ return new Promise(async (resolve) => {
214
+ let currentDataType = factoryReturns[module_id];
215
+
216
+ try {
217
+ let interpr_arr = await currentDataType.getInterpretationNew(value, appId, itemId, field, dataType);
218
+ let data = interpr_arr.find((item) => item.id == interpretation_id) || interpr_arr.find((item) => item.id == 'default');
219
+
220
+ let result = await data.content()
221
+
222
+ resolve({ html: result });
223
+ } catch(error) {
224
+ console.log(`ERROR IN ${module_id}`, error);
225
+ resolve({ html: '<span>no interpretation</span>' })
226
+ }
227
+ });
228
+ },
229
+
161
230
  //*************** GET INTERPRETATED VALUE ****************//
162
231
 
163
232
  getInterpretatedValue: function (field_value, field_model, appId, itemId) {
164
233
  return new Promise(async (resolve) => {
165
- let getInterpretatedValueFunction = angularModule.getInterpretatedValue;
234
+ let getInterpretatedValueFunction = factoryReturns[module_id].getInterpretatedValue;
166
235
  if (getInterpretatedValueFunction) {
167
236
  let value = await getInterpretatedValueFunction(field_value, field_model, appId, itemId);
168
237
  resolve(value);
169
238
  } else {
170
- let value = await gudhub.getFieldValue(appId, itemId, field_model.id);
171
- resolve(value);
239
+ resolve(field_value);
172
240
  }
173
241
  });
174
242
  },
@@ -206,43 +274,4 @@ export class GHConstructor {
206
274
 
207
275
  return result;
208
276
  }
209
-
210
- /*************** PARSE MODULE ***************/
211
- // Firstly, it's find factory with provided name.
212
- // Then it's parsing factory's code, by counting open and closed brackets.
213
- // Then returns object, which originally must be returned by factory.
214
-
215
- parseModule(module, module_id) {
216
- let factoryStart = module.indexOf(`.factory('${module_id}',`);
217
- if(factoryStart === -1) {
218
- factoryStart = module.indexOf(`.factory("${module_id}",`);
219
- }
220
- if(factoryStart !== -1) {
221
- let factory = module.substring(factoryStart, module.length);
222
- let returnStart = factory.indexOf('return');
223
- let returningObject = factory.substring(returnStart, factory.length);
224
- let firstBracket = returningObject.indexOf('{');
225
- returningObject = returningObject.substring(firstBracket, returningObject.length);
226
- let openBrackets = 0;
227
- let closeBrackets = 0;
228
- let returnEnd = 0;
229
- for (let i = 0; i < returningObject.length; i++) {
230
- if (returningObject[i] === '{') {
231
- openBrackets++;
232
- }
233
- if (returningObject[i] === '}') {
234
- closeBrackets++;
235
- }
236
- if (openBrackets === closeBrackets && i >= firstBracket) {
237
- returnEnd = i;
238
- break;
239
- }
240
- }
241
- returningObject = returningObject.substring(0, returnEnd + 1);
242
- return returningObject;
243
- } else {
244
- return false;
245
- }
246
- }
247
-
248
277
  }
@@ -0,0 +1,93 @@
1
+ export class Interpritate {
2
+ constructor(gudhub) {
3
+ this.gudhub = gudhub;
4
+ }
5
+
6
+ /*********************** GET INTERPRETATION OBJ ***********************/
7
+
8
+ getInterpretationObj(fieldDataModel, defaultFieldDataModel, src, containerId) {
9
+ var currentIntrpr = {};
10
+
11
+ var defaultIntrprObj = defaultFieldDataModel.data_model.interpretation.find(function (interpritation) {
12
+ return interpritation.src == src;
13
+ }) || defaultFieldDataModel.data_model.interpretation.find(function (interpritation) {
14
+ return interpritation.src == 'table';
15
+ }) || { id: 'default' };
16
+
17
+ if (fieldDataModel.data_model && fieldDataModel.data_model.interpretation) {
18
+ // To detect interpretation we use (container_id), if there is not (container_id) we use (src)
19
+ currentIntrpr = fieldDataModel.data_model.interpretation.find(function (interpritation) {
20
+ return interpritation.src == containerId;
21
+ }) || fieldDataModel.data_model.interpretation.find(function (interpritation) {
22
+ return interpritation.src == src;
23
+ });
24
+ }
25
+ return gudhub.mergeObjects(defaultIntrprObj, currentIntrpr);
26
+ }
27
+
28
+ /*********************** GET INTERPRETATION ***********************/
29
+
30
+ getInterpretation(value, field, dataType, source, itemId, appId, containerId) {
31
+ const self = this;
32
+ return new Promise(async (resolve, reject) => {
33
+ /*-- By default we use 'data_type' from field but in case if field is undefined we use 'dataType' from attribute*/
34
+ var data_type = field && field.data_type ? field.data_type : dataType;
35
+
36
+ /*---- Constructing Data Object ----*/
37
+ if (data_type) {
38
+ /*-- if we have data_type then we construct new data object to interpritate value*/
39
+ gudhub.ghconstructor.getInstance(data_type).then(function (data) {
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) {
42
+ // console.log(result, interpretationObj)
43
+
44
+ resolve(gudhub.mergeObjects(result, interpretationObj));
45
+ }, function (error) {
46
+ reject();
47
+ });
48
+ }, function (error) { });
49
+
50
+ } else {
51
+ /*-- if we don't have data_type then we run defaultInterpretation function*/
52
+ resolve(this.getDefaultInterpretation(value, field));
53
+ }
54
+ });
55
+ }
56
+
57
+ /*********************** GET DEFAULT INTERPRETATION ***********************/
58
+
59
+ getDefaultInterpretation(value, options) {
60
+ var result = value;
61
+
62
+ if (options) {
63
+ /*-- Iterating through options from data_model to find a nave for selected*/
64
+ options.forEach(item => {
65
+ if(item.value == value && value != '') {
66
+ result = item.name;
67
+ }
68
+ })
69
+ }
70
+ return result;
71
+ }
72
+
73
+ /********************* GET INTERPRETATION BY ID *********************/
74
+
75
+ getInterpretationById(appId, itemId, fieldId, interpretationId) {
76
+ return new Promise(async (resolve) => {
77
+ let fieldValue = await this.gudhub.getFieldValue(appId, itemId, fieldId);
78
+ let fieldModel = await this.gudhub.getField(appId, fieldId);
79
+ if(fieldModel == null) {
80
+ resolve('');
81
+ }
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);
84
+ if(interpretatedValue.html == '<span>no interpretation</span>') {
85
+ resolve(fieldValue);
86
+ } else {
87
+ resolve(interpretatedValue.html);
88
+ }
89
+ });
90
+ });
91
+ }
92
+
93
+ }
@@ -405,5 +405,9 @@ export default function generateModulesList(async_modules_path, file_server_url)
405
405
  name: "vs_code",
406
406
  url: file_server_url + '/' + async_modules_path + "vs_code_data.js"
407
407
  },
408
+ {
409
+ name: "nested_list",
410
+ url: file_server_url + '/' + async_modules_path + "nested_list_data.js"
411
+ },
408
412
  ]
409
413
  }
@@ -6,6 +6,7 @@ export class Storage {
6
6
  this.users_list = [];
7
7
  this.user = {};
8
8
  this.modulesList = generateModulesList(async_modules_path, file_server_url);
9
+ this.ghComponentsPromises = [];
9
10
  }
10
11
 
11
12
  getMainStorage() {
@@ -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,147 @@
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
+ console.log('YA YA YA');
51
+ const self = this;
52
+ return new Promise(async (resolve) => {
53
+ let fieldList = await this.gudhub.getFieldModels(appId);
54
+
55
+ if (tableSettingsFieldListToView.length !== 0) {
56
+ let correctFieldList = [];
57
+ tableSettingsFieldListToView.forEach((elementFromTable, index) => {
58
+ let fieldOnPipe = fieldList.find((element) => elementFromTable.field_id == element.field_id);
59
+
60
+ if (fieldOnPipe) {
61
+ let show_field = fieldOnPipe.data_model.interpretation.find((intrpr) => intrpr.src == 'table');
62
+ if (!show_field) {
63
+ show_field = { settings: { show_field: 1 } }
64
+ }
65
+ correctFieldList.push({
66
+ field_id: fieldOnPipe.field_id,
67
+ show: elementFromTable.show,
68
+ width: elementFromTable.width ? elementFromTable.width : "150px"
69
+ })
70
+ }
71
+ if (index + 1 == tableSettingsFieldListToView.length) {
72
+ checkFiledFromPipe(correctFieldList)
73
+ }
74
+
75
+ });
76
+ } else {
77
+ checkFiledFromPipe(tableSettingsFieldListToView);
78
+ }
79
+
80
+ function checkFiledFromPipe(correctFieldList) {
81
+ let counter = 0
82
+ fieldList.forEach((elemetFromPipe, index) => {
83
+ let correctFiel = correctFieldList.find((elemet) => elemetFromPipe.field_id == elemet.field_id)
84
+ if (!correctFiel) {
85
+ self.gudhub.ghconstructor.getInstance(elemetFromPipe.data_type).then((data) => {
86
+ let template = data.getTemplate();
87
+ let show_field = template.model.data_model.interpretation.find((intrpr) => intrpr.src == 'table');
88
+ if (!show_field) {
89
+ show_field = { settings: { show_field: 1 } }
90
+ }
91
+ correctFieldList.push({
92
+ field_id: elemetFromPipe.field_id,
93
+ show: show_field.settings.show_field,
94
+ });
95
+
96
+ if (counter == fieldList.length - 1) {
97
+ resolve(correctFieldList);
98
+ }
99
+ counter++
100
+ }, function errorCallback(result) {
101
+ counter++
102
+ })
103
+ } else {
104
+ if (counter == fieldList.length - 1) {
105
+ resolve(correctFieldList);
106
+ }
107
+ counter++
108
+ }
109
+ });
110
+ }
111
+ });
112
+ }
113
+
114
+ createFieldsListToViewWithDataType(appId, tableSettingsFieldListToView) {
115
+ return new Promise(async (resolve) => {
116
+ let columnsToView = [];
117
+ let fieldList = await this.gudhub.getFieldModels(appId);
118
+
119
+ let actualFieldIds = fieldList.map((field) => +field.field_id);
120
+
121
+ columnsToView = fieldList.reduce((viewColumsList, currentField) => {
122
+ let field = viewColumsList.find((view, index) => {
123
+ if (currentField.field_id == view.field_id) {
124
+ return true;
125
+ }
126
+ return false;
127
+ });
128
+
129
+ if (!field) {
130
+ this.gudhub.ghconstructor.getInstance(currentField.data_type).then(instance => {
131
+ let template = instance.getTemplate();
132
+ let defaultIntepretation = template.model.data_model.interpretation.find((intrpr) => intrpr.src == 'table') || { settings: { show_field: 1 } };
133
+ viewColumsList.push({
134
+ field_id: currentField.field_id,
135
+ data_type: currentField.data_type,
136
+ show: defaultIntepretation.settings.show_field
137
+ });
138
+ })
139
+ }
140
+
141
+ return viewColumsList;
142
+ }, tableSettingsFieldListToView).filter((field) => actualFieldIds.indexOf(field.field_id) != -1);
143
+
144
+ resolve(columnsToView);
145
+ })
146
+ }
147
+ }
@@ -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
+ });