@gudhub/core 1.0.55 → 1.0.58

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,22 +115,22 @@ 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);
123
+
124
+ if(!this.angularInjector.has(module_id)) {
125
+ await this.angularInjector.get('$ocLazyLoad').load(gudhub.storage.getModuleUrl(module_id));
126
+ }
127
+
72
128
  let angularModule = await this.angularInjector.get(module_id);
129
+
73
130
  let module = await axios.get(module_url);
74
131
  module = module.data;
75
- let returningObject = this.parseModule(module, module_id);
76
- if(!returningObject) return false;
77
- let obj;
78
- try {
79
- eval('obj = ' + returningObject);
80
- } catch(error) {
81
- console.log(`Error in ghconstructor (${module_id})`, error);
82
- }
132
+
133
+ eval(module);
83
134
 
84
135
  let result = {
85
136
  type: module_id,
@@ -135,7 +186,7 @@ export class GHConstructor {
135
186
 
136
187
  getInterpretation: function (value, field, dataType, interpretation_id, itemId, appId) {
137
188
  return new Promise(async (resolve) => {
138
- let currentDataType = obj;
189
+ let currentDataType = angularModule;
139
190
 
140
191
  let interpr_arr = await currentDataType.getInterpretation(value, field, dataType, interpretation_id, itemId, appId);
141
192
  if (interpr_arr) {
@@ -152,17 +203,36 @@ export class GHConstructor {
152
203
  });
153
204
  },
154
205
 
206
+ //*************** GET INTERPRETATION V2 ****************//
207
+
208
+ getInterpretationNew: function(value, interpretation_id, dataType, field, itemId, appId) {
209
+ return new Promise(async (resolve) => {
210
+ let currentDataType = factoryReturns[module_id];
211
+
212
+ try {
213
+ let interpr_arr = await currentDataType.getInterpretationNew(value, appId, itemId, field, dataType);
214
+ let data = interpr_arr.find((item) => item.id == interpretation_id) || interpr_arr.find((item) => item.id == 'default');
215
+
216
+ let result = await data.content()
217
+
218
+ resolve({ html: result });
219
+ } catch(error) {
220
+ console.log(`ERROR IN ${module_id}`, error);
221
+ resolve({ html: '<span>no interpretation</span>' })
222
+ }
223
+ });
224
+ },
225
+
155
226
  //*************** GET INTERPRETATED VALUE ****************//
156
227
 
157
228
  getInterpretatedValue: function (field_value, field_model, appId, itemId) {
158
229
  return new Promise(async (resolve) => {
159
- let getInterpretatedValueFunction = angularModule.getInterpretatedValue;
230
+ let getInterpretatedValueFunction = factoryReturns[module_id].getInterpretatedValue;
160
231
  if (getInterpretatedValueFunction) {
161
232
  let value = await getInterpretatedValueFunction(field_value, field_model, appId, itemId);
162
233
  resolve(value);
163
234
  } else {
164
- let value = await gudhub.getFieldValue(appId, itemId, field_model.id);
165
- resolve(value);
235
+ resolve(field_value);
166
236
  }
167
237
  });
168
238
  },
@@ -200,43 +270,4 @@ export class GHConstructor {
200
270
 
201
271
  return result;
202
272
  }
203
-
204
- /*************** PARSE MODULE ***************/
205
- // Firstly, it's find factory with provided name.
206
- // Then it's parsing factory's code, by counting open and closed brackets.
207
- // Then returns object, which originally must be returned by factory.
208
-
209
- parseModule(module, module_id) {
210
- let factoryStart = module.indexOf(`.factory('${module_id}',`);
211
- if(factoryStart === -1) {
212
- factoryStart = module.indexOf(`.factory("${module_id}",`);
213
- }
214
- if(factoryStart !== -1) {
215
- let factory = module.substring(factoryStart, module.length);
216
- let returnStart = factory.indexOf('return');
217
- let returningObject = factory.substring(returnStart, factory.length);
218
- let firstBracket = returningObject.indexOf('{');
219
- returningObject = returningObject.substring(firstBracket, returningObject.length);
220
- let openBrackets = 0;
221
- let closeBrackets = 0;
222
- let returnEnd = 0;
223
- for (let i = 0; i < returningObject.length; i++) {
224
- if (returningObject[i] === '{') {
225
- openBrackets++;
226
- }
227
- if (returningObject[i] === '}') {
228
- closeBrackets++;
229
- }
230
- if (openBrackets === closeBrackets && i >= firstBracket) {
231
- returnEnd = i;
232
- break;
233
- }
234
- }
235
- returningObject = returningObject.substring(0, returnEnd + 1);
236
- return returningObject;
237
- } else {
238
- return false;
239
- }
240
- }
241
-
242
273
  }
@@ -0,0 +1,90 @@
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
+ this.gudhub.ghconstructor.getInstance(fieldModel.data_type).then(async (instance) => {
80
+ let interpretatedValue = await instance.getInterpretationNew(fieldValue, interpretationId, fieldModel.data_type, fieldModel, itemId, appId);
81
+ if(interpretatedValue.html == '<span>no interpretation</span>') {
82
+ resolve(fieldValue);
83
+ } else {
84
+ resolve(interpretatedValue.html);
85
+ }
86
+ });
87
+ });
88
+ }
89
+
90
+ }
@@ -136,14 +136,6 @@ export default function generateModulesList(async_modules_path, file_server_url)
136
136
  name: "signature",
137
137
  url: file_server_url + '/' + async_modules_path + "signature_data.js"
138
138
  },
139
- {
140
- name: "schedule",
141
- url: file_server_url + '/' + async_modules_path + "schedule_data.js"
142
- },
143
- {
144
- name: "auto_schedule",
145
- url: file_server_url + '/' + async_modules_path + "auto_schedule_data.js"
146
- },
147
139
  {
148
140
  name: "sendEmail",
149
141
  url: file_server_url + '/' + async_modules_path + "send_email_data.js"
@@ -413,5 +405,9 @@ export default function generateModulesList(async_modules_path, file_server_url)
413
405
  name: "vs_code",
414
406
  url: file_server_url + '/' + async_modules_path + "vs_code_data.js"
415
407
  },
408
+ {
409
+ name: "nested_list",
410
+ url: file_server_url + '/' + async_modules_path + "nested_list_data.js"
411
+ },
416
412
  ]
417
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() {
@@ -139,8 +139,8 @@ export class Utils {
139
139
  return mergeChunks(chunks);
140
140
  }
141
141
 
142
- jsonConstructor(scheme, items, variables){
143
- return compiler(scheme, items, this, variables)
142
+ jsonConstructor(scheme, item, variables){
143
+ return compiler(scheme, item, this, variables)
144
144
  }
145
145
 
146
146
  compareAppsItemsLists(items_list1, items_list2, callback) {
@@ -1,5 +1,7 @@
1
1
  import should from "should";
2
+ // import add from "date-fns/subYears/index.js";
2
3
  import {GudHub} from '../../gudhub.js';
4
+ import { subYears } from "date-fns";
3
5
 
4
6
  describe("GET DATE", function () {
5
7
  const gudhub = new GudHub();
@@ -73,6 +75,15 @@ describe("GET DATE", function () {
73
75
  result.should.equal(false);
74
76
  });
75
77
 
78
+ it('CHECK IF DATE IN THIS WEEK / Compare this wednesday with wednesday two years ago', function(){
79
+ let day = gudhub.getDate('this_wednesday');
80
+ let dayTwoYearsBefore = subYears(day, 2);
81
+
82
+ // console.log(dayTwoYearsBefore);
83
+ let result = gudhub.checkRecurringDate(dayTwoYearsBefore, 'week');
84
+ result.should.equal(true);
85
+ });
86
+
76
87
  it("CHECK IF DATE IN THIS MONTH", function() {
77
88
  let today = new Date();
78
89
  let result = gudhub.checkRecurringDate(today, 'month');
@@ -83,4 +94,5 @@ describe("GET DATE", function () {
83
94
  result = gudhub.checkRecurringDate(twoMonthsLater, 'month');
84
95
  result.should.equal(false);
85
96
  });
97
+
86
98
  });
@@ -63,6 +63,8 @@ function mergeObject(target, source, optionsArgument) {
63
63
  }
64
64
 
65
65
  function deepmerge(target, source, optionsArgument) {
66
+ target = target !== undefined ? target : {};
67
+ source = source !== undefined ? source : {};
66
68
  var array = Array.isArray(source);
67
69
  var options = optionsArgument || { arrayMerge: defaultArrayMerge }
68
70
  var arrayMerge = options.arrayMerge || defaultArrayMerge
@@ -36,16 +36,12 @@ export function makeNestedList(arr, id, parent_id, children_property, priority_p
36
36
  }
37
37
 
38
38
  function sortChildrensByPriority(unsorted_array) {
39
- unsorted_array.forEach(item => {
40
- if(item[children]) {
41
- item[children].sort((a, b) => a[priority] - b[priority]);
42
- item[children].forEach(child => {
43
- if(child[children]) {
44
- sortChildrensByPriority(child[children]);
45
- }
46
- })
47
- }
48
- });
39
+ unsorted_array.sort( (a, b) => a[priority] - b[priority])
40
+ unsorted_array.forEach(element => {
41
+ if(element[children]) {
42
+ sortChildrensByPriority(element[children])
43
+ }
44
+ })
49
45
  }
50
46
 
51
47
  if(priority) {
@@ -24,6 +24,11 @@ describe("NESTED LIST", function () {
24
24
  nestedList[0].custom_children_array_name[1].title.should.equal('Child 1');
25
25
  })
26
26
 
27
+ it('Should sort nested list parents by priority', function () {
28
+ let nestedList = gudhub.makeNestedList(parentInput, 'id', 'parent_id', 'children', 'priority');
29
+ nestedList[0].title.should.equal('Nineth');
30
+ })
31
+
27
32
  });
28
33
 
29
34
  let input = [
@@ -145,4 +150,36 @@ let complicatedInput = [
145
150
  parent_id: "26553.2884361",
146
151
  title: "Child of second parent"
147
152
  }
153
+ ]
154
+ let parentInput = [
155
+ {
156
+ "id": "27026.2937695",
157
+ "parent_id": "",
158
+ "title": "First",
159
+ "priority": "1"
160
+ },
161
+ {
162
+ "id": "27026.2937708",
163
+ "parent_id": "",
164
+ "title": "Second",
165
+ "priority": "4"
166
+ },
167
+ {
168
+ "id": "27026.3009942",
169
+ "parent_id": "",
170
+ "title": "Eighth",
171
+ "priority": "3"
172
+ },
173
+ {
174
+ "id": "27026.3009943",
175
+ "parent_id": "",
176
+ "title": "Tenth",
177
+ "priority": "2"
178
+ },
179
+ {
180
+ "id": "27026.3108354",
181
+ "parent_id": "",
182
+ "title": "Nineth",
183
+ "priority": "0"
184
+ }
148
185
  ]
package/GUDHUB/gudhub.js CHANGED
@@ -12,6 +12,7 @@ import { FieldProcessor } from "./FieldProcessor/FieldProcessor.js";
12
12
  import { FileManager } from "./FileManager/FileManager.js";
13
13
  import { ChunksManager } from "./ChunksManager/ChunksManager.js";
14
14
  import { DocumentManager } from "./DocumentManager/DocumentManager.js";
15
+ import { Interpritate } from './GHConstructor/interpritate.js'
15
16
  import { IS_WEB } from "./consts.js";
16
17
 
17
18
  export class GudHub {
@@ -28,6 +29,7 @@ export class GudHub {
28
29
  }
29
30
  ) {
30
31
  this.ghconstructor = new GHConstructor();
32
+ this.interpritate = new Interpritate(this);
31
33
  this.pipeService = new PipeService();
32
34
  this.storage = new Storage(options.async_modules_path, options.file_server_url);
33
35
  this.util = new Utils(this);
@@ -148,6 +150,26 @@ export class GudHub {
148
150
  return this.ghconstructor.initAngularInjector(injector);
149
151
  }
150
152
 
153
+ /******************* INTERPRITATE *******************/
154
+
155
+ getInterpretation(value, field, dataType, source, itemId, appId, containerId) {
156
+ return this.interpritate.getInterpretation(
157
+ value,
158
+ field,
159
+ dataType,
160
+ source,
161
+ itemId,
162
+ appId,
163
+ containerId
164
+ );
165
+ }
166
+
167
+ /******************* GET INTERPRETATION BY ID *******************/
168
+
169
+ getInterpretationById(appId, itemId, fieldId, interpretationId) {
170
+ return this.interpritate.getInterpretationById(appId, itemId, fieldId, interpretationId);
171
+ }
172
+
151
173
  //============ FILTER ==========//
152
174
  filter(items, filter_list) {
153
175
  return this.util.filter(items, filter_list);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gudhub/core",
3
- "version": "1.0.55",
3
+ "version": "1.0.58",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {