-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rhydhux.js
1410 lines (1098 loc) · 52.7 KB
/
Rhydhux.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const Rhydux = (function() {
let default_config= {
tagSeparator: "--slashRhydux-",
maxTagNameLength: 1000,
}
function generateRandomString(prefix="") {
return prefix + Math.random().toString(36).substring(2, 15) + "-" + Math.random().toString(36).substring(2, 15); // can contain only letters and numbers
}
function lerpFunction(value, inputMin=0, inputMaz=1, outputMin=0, outputMax=1) {
return outputMin + (outputMax - outputMin) * (value - inputMin) / (inputMaz - inputMin);
}
function TagTree() {
// two basics tree paths : systemTags and regularTags
this.systemTags = {}
this.regularTags = {}
// tag tree specifications:
// all tags are separated with --slashRhydux- when representing a nested tag
}
const moveChildrenTagsToParent = (parentTag, newParentTag, childrenTags, tagType) => {
for (const childTag in childrenTags) {
oldPath = childTag.fullTagPath
childTag.fullTagPath = newParentTag.fullTagPath + default_config.tagSeparator + childTag.tagName;
// updated the key of the child tag itself
this.tagTree[tagType][childTag.fullTagPath] = childTag;
delete this.tagTree[tagType][oldPath];
// got to each object and update the tag path
for (const objectID in childTag.objects) {
this.objects[objectID].tags[childTag.fullTagPath] = childTag;
// delete the old tag path
delete this.objects[objectID].tags[oldPath];
}
}
}
// Rhydux object
let RhyduxData = {}
function RhyduxDB(options) {
// id is generated randomly as a alphanumeric string of with time
const randomString = generateRandomString(); // can contain only letters and numbers
this.id = options && options.id ? options.id : "rhyduxDB--" + randomString + "--" + Date.now().toString(36);
this.options = options;
this.tagTree = new TagTree();
this.objects = {}
this.operations = {
getMainTagType: (tagPath) => {
var tag = this.tagTree.systemTags[tagPath];
if (tag) {
return "systemTag";
}
tag = this.tagTree.regularTags[tagPath];
if (tag) {
return "regularTag";
}
return null;
},
createSystemTag: (tagName="System Tag", parentTagPath=null, userCanAssign=true, additionalProperties={}) => {
var shortNameTrimmed = ""
if (tagName) {
shortNameTrimmed = tagName.split(" ").map(word => word.trim()).join("").toLowerCase();
}
// tag name cannot have the forbidden words [regularTag, systemTag, separator variables]
if (shortNameTrimmed.includes("regulartag") || shortNameTrimmed.includes("systemtag")) {
throw new Error("Tag name cannot have the forbidden words [regularTag, systemTag]");
}
// tag name cannot have default_config.tagSeparator
if (shortNameTrimmed.includes(default_config.tagSeparator)) {
throw new Error("Tag name cannot have the separator for this database: " + default_config.tagSeparator);
}
// tag name cannot be longer than default_config.maxTagNameLength
if (tagName.length > default_config.maxTagNameLength) {
throw new Error("Tag name cannot be longer than " + default_config.maxTagNameLength + " characters");
}
if (parentTagPath) {
const parentTag = this.tagTree.systemTags[parentTagPath];
if (!parentTag) {
throw new Error("Parent tag not found, OR is not a system tag");
}
}
const randomString = generateRandomString(); // can contain only letters and numbers
const systemTagID = "systemTag--" + shortNameTrimmed + "---" + randomString + "--" + Date.now().toString(36);
const fullTagPath = (parentTagPath ? parentTagPath + default_config.tagSeparator : "") + systemTagID;
const createdTime = Date.now();
const objects = {}
const systemTag = {
...additionalProperties,
tagName,
id: systemTagID,
fullTagPath,
createdTime,
lastModifiedTime: createdTime,
colorHue: "none",
objects,
expanded: false,
userCanAssign,
visibleInNavigation: true,
editable: false,
fontAwesomeIcon: "tag",
linkedProperties: {
userProps: {},
systemProps: {},
},
};
this.tagTree.systemTags[fullTagPath] = systemTag;
return systemTag;
},
createRegularTag: (tagName="Regular Tag", parentTagPath=null, additionalProperties={}) => {
var shortNameTrimmed = ""
if (tagName) {
shortNameTrimmed = tagName.split(" ").map(word => word.trim()).join("").toLowerCase();
}
if (parentTagPath) {
const parentTag = this.tagTree.regularTags[parentTagPath];
if (!parentTag) {
throw new Error("Parent tag not found, OR is not a regular tag");
}
}
const randomString = generateRandomString(); // can contain only letters and numbers
const regularTagID = "regularTag--" + shortNameTrimmed + "---" + randomString + "--" + Date.now().toString(36);
const fullTagPath = (parentTagPath ? parentTagPath + default_config.tagSeparator : "") + regularTagID;
const createdTime = Date.now();
const objects = {}
const regularTag = {
...additionalProperties,
tagName,
id: regularTagID,
fullTagPath,
createdTime,
lastModifiedTime: createdTime,
colorHue: "none",
objects,
expanded: false,
userCanAssign: true,
editable: true,
visibleInNavigation: true,
fontAwesomeIcon: "tag",
linkedProperties: {
userProps: {},
systemProps: {},
},
};
this.tagTree.regularTags[fullTagPath] = regularTag;
return regularTag;
},
getSystemTag: (tagName) => {
return this.tagTree.systemTags[tagName];
},
getRegularTag: (tagName) => {
return this.tagTree.regularTags[tagName];
},
getTag: (tagPath) => {
if (!tagPath) {
return null;
}
var tagType = this.operations.getMainTagType(tagPath);
var tagType = tagType + "s"
return this.tagTree[tagType][tagPath];
},
deleteTag: (tagPath) => {
// check if the tag is a system tag or a regular tag
var tagType = this.operations.getMainTagType(tagPath);
if (!tagType) {
throw new Error("Tag not found");
}
var tagType = tagType + "s"
for (const childTag in this.tagTree[tagType]) {
if (childTag.startsWith(tagPath)) {
// remove the tag from the objects
for (const objectID in this.objects) {
delete this.objects[objectID].tags[childTag];
}
delete this.tagTree[tagType][childTag];
}
}
delete this.tagTree[tagType][tagPath];
return;
},
setExpanded: (tagPath, expanded) => {
// get type
var tagType = this.operations.getMainTagType(tagPath);
var tagType = tagType + "s"
this.tagTree[tagType][tagPath].expanded = expanded;
},
editTagName: (tagPath, newTagName="") => {
var tagType = this.operations.getMainTagType(tagPath);
var tagType = tagType + "s"
this.tagTree[tagType][tagPath].tagName = newTagName;
},
editTagProp: (tagPath, propName, propValue) => {
var tagType = this.operations.getMainTagType(tagPath);
var tagType = tagType + "s"
this.tagTree[tagType][tagPath][propName] = propValue;
},
createObject: (objectName="Object", props={}) => {
const randomString = generateRandomString(); // can contain only letters and numbers
const objectID = "object--" + objectName + "---" + randomString + "--" + Date.now().toString(36);
const createdTime = Date.now();
const objectTags = {}
const object = {
props,
objectName,
id: objectID,
createdTime,
lastModifiedTime: createdTime,
movedToTrashTime: null,
tags: objectTags,
userProps : {
"placeholder": "value"
},
systemProps: {
"placeholder": "value"
},
};
this.objects[objectID] = object;
return object;
},
changeObject: (objectID, newProps) => {
this.objects[objectID].props = newProps;
this.objects[objectID].lastModifiedTime = Date.now();
},
changeObjectProp: (objectID, propName, propValue) => {
this.objects[objectID].props[propName] = propValue;
this.objects[objectID].lastModifiedTime = Date.now();
},
getSystemPropByName: (propName) => {
var returningSystemProp = null
// get system property tag key first
var systemPropertyTagKey = null;
for (const tagPath in this.tagTree.systemTags) {
if (this.tagTree.systemTags[tagPath].tagName === "System Property") {
systemPropertyTagKey = tagPath;
break;
}
}
Object.keys(this.objects).forEach(objectID => {
// check if the object has the tag 'System Property'
// console.log(this.objects[objectID].tags[systemPropertyTagKey])
if (this.objects[objectID].tags[systemPropertyTagKey]) {
// check if the object previewTitle is propName
// console.log(this.objects[objectID].props.previewTitle, propName)
if (this.objects[objectID].props.previewTitle == propName) {
returningSystemProp = objectID;
}
}
});
return returningSystemProp;
},
changeObjectVisibleSystemProp: (objectID, propName, propValue, subPropKey=null) => {
if (subPropKey) {
if (this.objects[objectID].systemProps[propName]) {
this.objects[objectID].systemProps[propName][subPropKey] = propValue;
}
else {
this.objects[objectID].systemProps[propName] = {}
this.objects[objectID].systemProps[propName][subPropKey] = propValue;
}
} else {
this.objects[objectID].systemProps[propName] = propValue;
}
this.objects[objectID].lastModifiedTime = Date.now();
},
readObjectProp: (objectID, propName) => {
return this.objects[objectID].props[propName];
},
deleteObject: (objectID) => {
// handle the tags first
for (const tagPath in this.objects[objectID].tags) {
delete this.tagTree[this.operations.getMainTagType(tagPath) + "s"][tagPath].objects[objectID];
}
delete this.objects[objectID];
},
moveToTrash: (objectID) => {
// add trash tag if available
if (this.trashTag) {
// handle the other tags first
// for (const tagPath in this.objects[objectID].tags) {
// delete this.tagTree[this.operations.getMainTagType(tagPath) + "s"][tagPath].objects[objectID];
// }
// set object moved to trash time
this.objects[objectID].movedToTrashTime = Date.now();
this.operations.addTagToObject(objectID, this.trashTag);
}
},
getObject: (objectID) => {
return this.objects[objectID];
},
addTagToObject: (objectID, tagPath) => {
const object = this.objects[objectID];
var tagType = this.operations.getMainTagType(tagPath);
var tagType = tagType + "s"
object.tags[tagPath] = this.tagTree[tagType][tagPath];
// add the object id
this.tagTree[tagType][tagPath].objects[objectID] = {objectID, objectName: object.objectName};
return object;
},
removeTagFromObject: (objectID, tagPath) => {
const tagType = this.operations.getMainTagType(tagPath) + "s"
const object = this.objects[objectID];
delete object.tags[tagPath];
// remove the object id
delete this.tagTree[tagType][tagPath].objects[objectID];
},
getObjectTags: (objectID) => {
const object = this.objects[objectID];
return object.tags;
},
getObjects: (tagPath, filterBySearchTerm=null) => {
if (filterBySearchTerm == "") {
filterBySearchTerm = null;
}
returningObjects = {}
if (tagPath == null) {
// return all objects
for (const objectID in this.objects) {
// check if has the trash tag
if (this.trashTag && this.objects[objectID].tags[this.trashTag] && this.trashTag !== tagPath) {
continue;
}
if (filterBySearchTerm) {
// check preview title and preview description
if (this.objects[objectID].props.previewTitle.toLowerCase().includes(filterBySearchTerm.toLowerCase()) || this.objects[objectID].props.previewDescription.toLowerCase().includes(filterBySearchTerm.toLowerCase())) {
returningObjects[objectID] = {
path: null,
object: this.objects[objectID],
}
}
}
else {
returningObjects[objectID] = {
path: null,
object: this.objects[objectID],
}
}
}
return returningObjects;
}
// get main tag type
var tagType = this.operations.getMainTagType(tagPath);
var tagType = tagType + "s"
// get all paths that start with the tagPath
var paths = Object.keys(this.tagTree[tagType]).filter(path => path.startsWith(tagPath));
for (const path of paths) {
objectsFound = this.tagTree[tagType][path].objects;
for (const objectID in objectsFound) {
// check if has the trash tag
if (this.trashTag && this.objects[objectID].tags[this.trashTag] && this.trashTag !== tagPath) {
continue;
}
if (filterBySearchTerm) {
// check preview title and preview description
if (this.objects[objectID].props.previewTitle.toLowerCase().includes(filterBySearchTerm.toLowerCase()) || this.objects[objectID].props.previewDescription.toLowerCase().includes(filterBySearchTerm.toLowerCase())) {
returningObjects[objectID] = {
path: null,
object: this.objects[objectID],
}
}
}
else {
returningObjects[objectID] = {
path: null,
object: this.objects[objectID],
}
}
}
}
// this.tagTree[tagType][tagPath].objects
// console.log("******************************")
// console.log("tagPath", tagPath)
// console.log("returningObjects", returningObjects)
// console.log(this.tagTree)
// console.log("******************************")
return returningObjects;
},
getObjectsPaginated: (tagPath, perPage = 20, pageNumber = 0) => {
returningObjects = {}
// get main tag type
var tagType = this.operations.getMainTagType(tagPath);
var tagType = tagType + "s"
// get all paths that start with the tagPath
var paths = Object.keys(this.tagTree[tagType]).filter(path => path.startsWith(tagPath));
for (const path of paths) {
objectsFound = this.tagTree[tagType][path].objects;
for (const objectID in objectsFound) {
returningObjects[objectID] = {
path: path,
object: objectsFound[objectID],
}
}
}
// get only those on the page
var objectKeys = Object.keys(returningObjects);
var paginatedObjects = {}
for (let i = pageNumber * perPage; i < (pageNumber + 1) * perPage; i++) {
if (objectKeys[i]) {
paginatedObjects[objectKeys[i]] = returningObjects[objectKeys[i]];
}
}
return paginatedObjects;
},
sortObjects: (objectsInvolved, type="descending") => {
var sortedObjects = {}
// sort by lastModifiedTime
var sortable = [];
for (const objectID in objectsInvolved) {
var lastModifiedTime = this.operations.getObject(objectID).lastModifiedTime;
sortable.push([objectID, lastModifiedTime]);
}
if (type === "ascending") {
sortable.sort(function(a, b) {
return a[1] - b[1];
});
} else {
sortable.sort(function(a, b) {
return b[1] - a[1];
});
}
for (const object of sortable) {
sortedObjects[object[0]] = objectsInvolved[object[0]];
}
return sortedObjects;
},
paginatedObjectsList: (objectsInvolved, perPage = 30, pageNumber = 0) => {
var objectKeys = Object.keys(objectsInvolved);
var paginatedObjects = {}
for (let i = pageNumber * perPage; i < (pageNumber + 1) * perPage; i++) {
if (objectKeys[i]) {
paginatedObjects[objectKeys[i]] = objectsInvolved[objectKeys[i]];
}
}
return paginatedObjects;
},
getChildrenTags: (tagPath) => {
var tagType = this.operations.getMainTagType(tagPath);
var tagType = tagType + "s"
var childrenTags = {}
for (const childTag in this.tagTree[tagType]) {
if (childTag.startsWith(tagPath) && childTag !== tagPath) {
childrenTags[childTag] = this.tagTree[tagType][childTag];
}
}
return childrenTags;
},
getImmediateChildrenTags: (tagPath, filterBySearchTerm=null) => {
if (filterBySearchTerm == "") {
filterBySearchTerm = null;
}
var tagType = this.operations.getMainTagType(tagPath);
var tagType = tagType + "s"
var childrenTags = {}
for (const childTag in this.tagTree[tagType]) {
var childTagLevel = childTag.split(default_config.tagSeparator).length
var tagLevel = tagPath.split(default_config.tagSeparator).length
if (childTag.startsWith(tagPath) && childTag !== tagPath && childTagLevel == tagLevel + 1) {
if (filterBySearchTerm) {
if (this.tagTree[tagType][childTag].tagName.toLowerCase().includes(filterBySearchTerm.toLowerCase())) {
childrenTags[childTag] = this.tagTree[tagType][childTag];
}
}
else {
childrenTags[childTag] = this.tagTree[tagType][childTag];
}
}
}
return childrenTags;
},
getImmediateParentTag: (tagPath) => {
var tagType = this.operations.getMainTagType(tagPath);
var tagType = tagType + "s"
var tagLevel = tagPath.split(default_config.tagSeparator).length
var parentTagPath = tagPath.split(default_config.tagSeparator).slice(0, tagLevel - 1).join(default_config.tagSeparator);
if (parentTagPath) {
return parentTagPath;
}
return null;
},
getAncestorsOfTag: (tagPath) => {
var tagType = this.operations.getMainTagType(tagPath);
var tagType = tagType + "s"
var tagPathParts = tagPath.split(default_config.tagSeparator);
var ancestors = []
if (tagPathParts.length == 1) {
return ancestors;
}
for (let i = 0; i < tagPathParts.length - 1; i++) {
var path = tagPathParts.slice(0, i + 1).join(default_config.tagSeparator);
ancestors.push(path);
}
return ancestors;
},
getHighestLevelTags: (filterBySearchTerm=null) => {
if (filterBySearchTerm == "") {
filterBySearchTerm = null;
}
var highestLevelTags = []
for (const tagPath in this.tagTree.systemTags) {
if (tagPath.split(default_config.tagSeparator).length == 1) {
if (filterBySearchTerm) {
if (this.tagTree.systemTags[tagPath].tagName.toLowerCase().includes(filterBySearchTerm.toLowerCase())) {
highestLevelTags.push(tagPath)
}
}
else {
highestLevelTags.push(tagPath)
}
}
}
for (const tagPath in this.tagTree.regularTags) {
if (tagPath.split(default_config.tagSeparator).length == 1) {
if (filterBySearchTerm) {
if (this.tagTree.regularTags[tagPath].tagName.toLowerCase().includes(filterBySearchTerm.toLowerCase())) {
highestLevelTags.push(tagPath)
}
}
else {
highestLevelTags.push(tagPath)
}
}
}
return highestLevelTags;
},
moveTag: (tagPath, newParentTagPath) => {
// check if the tag is a system tag or a regular tag
var tagType = "systemTags"
if (this.tagTree.systemTags[tagPath]) {
tagType = "systemTags"
} else if (this.tagTree.regularTags[tagPath]) {
tagType = "regularTags"
}
// check if the new parent tag is a system tag or a regular tag
var newParentTagType = "systemTags"
if (this.tagTree.systemTags[newParentTagPath]) {
newParentTagType = "systemTags"
} else if (this.tagTree.regularTags[newParentTagPath]) {
newParentTagType = "regularTags"
}
// tags must be of the same type
if (tagType !== newParentTagType) {
throw new Error("Tags must be of the same type");
}
// you cannot move a tag under itself
if (tagPath === newParentTagPath) {
throw new Error("You cannot move a tag under itself");
}
// or move parent under child
// console.log("movingTagPath", tagPath)
// console.log("newParentTag : ", newParentTagPath)
if (newParentTagPath.startsWith(tagPath)) {
console.log("You cannot move a parent tag under its child")
throw new Error("You cannot move a parent tag under its child");
}
var tagLevel = tagPath.split(default_config.tagSeparator).length
var newParentTagLevel = newParentTagPath.split(default_config.tagSeparator).length
// console.log(tagLevel, newParentTagLevel)
if (tagPath.startsWith(newParentTagPath + default_config.tagSeparator) && tagLevel == newParentTagLevel + 1) {
console.log("You cannot move a tag under its immediate parent")
throw new Error("You cannot move a tag under its immediate parent");
}
// adjust all paths of tags according to the move
var tag = this.tagTree[tagType][tagPath];
var parentTag = this.tagTree[tagType][newParentTagPath];
// console.log(tag)
// console.log(parentTag)
const oldPath = tag.fullTagPath;
const newPath = parentTag.fullTagPath + default_config.tagSeparator + tag.id;
// replace all oldPath with newPath in all tags and objects
for (const tag in this.tagTree[tagType]) {
if (tag.startsWith(oldPath)) {
// new path here
subPath = tag.split(oldPath)[1];
tagNewPath = newPath + subPath;
this.tagTree[tagType][tagNewPath] = this.tagTree[tagType][tag];
this.tagTree[tagType][tagNewPath].fullTagPath = tagNewPath;
for (const objectID in this.objects) {
const object = this.objects[objectID];
if (object.tags[tag]) {
object.tags[tagNewPath] = object.tags[tag];
object.tags[tagNewPath].fullTagPath = tagNewPath;
delete object.tags[tag];
}
}
// delete the old path
delete this.tagTree[tagType][tag];
}
}
// tag.fullTagPath = newPath;
},
getAncestryString: (tagPath) => {
var tagType = this.operations.getMainTagType(tagPath);
var tagType = tagType + "s"
var tagPathParts = tagPath.split(default_config.tagSeparator);
var ancestryPaths = []
if (tagPathParts.length == 1) {
return "";
}
for (let i = 0; i < tagPathParts.length - 1; i++) {
var path = tagPathParts.slice(0, i + 1).join(default_config.tagSeparator);
ancestryPaths.push(path);
}
var ancestryString = ""
for (const path of ancestryPaths) {
if (this.tagTree[tagType][path]) {
ancestryString += this.tagTree[tagType][path].tagName + "/";
}
}
return ancestryString;
},
searchForObject: (searchQuery, searchProps=[], maxResults=100) => {
// search for object in the database
var searchResults = []
for (const objectID in this.objects) {
const object = this.objects[objectID];
if (object.objectName.includes(searchQuery)) {
searchResults.push(object);
if (searchResults.length >= maxResults) {
return searchResults;
}
break;
}
for (const searchProp of searchProps) {
if (object.props[searchProp] && typeof object.props[searchProp] === "string") {
if (object.props[searchProp].includes(searchQuery)) {
searchResults.push(object);
if (searchResults.length >= maxResults) {
return searchResults;
}
break;
}
}
}
}
return searchResults;
},
searchForTag: (searchQuery, searchProps=[], maxResults=100) => {
// search for tag in the database
var searchQuery = searchQuery.toLowerCase();
var searchResults = []
for (const tagPath in this.tagTree.regularTags) {
const tag = this.tagTree.regularTags[tagPath];
if (tag.tagName.toLowerCase().includes(searchQuery)) {
searchResults.push(tag);
if (searchResults.length >= maxResults) {
return searchResults;
}
}
for (const searchProp of searchProps) {
if (tag[searchProp] && typeof tag[searchProp] === "string") {
if (tag[searchProp].toLowerCase().includes(searchQuery)) {
searchResults.push(tag);
if (searchResults.length >= maxResults) {
return searchResults;
}
break;
}
}
}
}
for (const tagPath in this.tagTree.systemTags) {
const tag = this.tagTree.systemTags[tagPath];
if (tag.tagName.toLowerCase().includes(searchQuery)) {
searchResults.push(tag);
if (searchResults.length >= maxResults) {
return searchResults;
}
}
for (const searchProp of searchProps) {
if (tag[searchProp] && typeof tag[searchProp] === "string") {
if (tag[searchProp].toLowerCase().includes(searchQuery)) {
searchResults.push(tag);
if (searchResults.length >= maxResults) {
return searchResults;
}
break;
}
}
}
}
return searchResults;
},
objectHasTag : (objectID, tagPath) => {
return this.objects[objectID].tags[tagPath] ? true : false;
},
objectHasRegularTagName: (objectID, tagName) => {
// get tagPath
var searchingTagPath = null;
var tagID = null;
for (const tagPath in this.tagTree.regularTags) {
if (this.tagTree.regularTags[tagPath].tagName === tagName) {
searchingTagPath = tagPath;
tagID = this.tagTree.regularTags[tagPath].id;
break;
}
}
if (!searchingTagPath) {
return false;
}
for (const tagPath in this.objects[objectID].tags) {
if (tagPath === searchingTagPath) {
return tagPath;
}
if (tagPath.split(default_config.tagSeparator).includes(tagID)) {
return tagPath;
}
}
return false;
},
searchForTagForObject: (searchQuery, objectKey, searchProps=[], maxResults=100) => {
// search for tag in the database
var searchQuery = searchQuery.toLowerCase();
var searchResults = []
// get object and its tags
const object = this.objects[objectKey];
const objectTags = object.tags;
for (const tagPath in this.tagTree.regularTags) {
if (objectTags[tagPath]) {
continue
}
const tag = this.tagTree.regularTags[tagPath];
if (tag.tagName.toLowerCase().includes(searchQuery)) {
searchResults.push(tag);
if (searchResults.length >= maxResults) {
return searchResults;
}
}
for (const searchProp of searchProps) {
if (tag[searchProp] && typeof tag[searchProp] === "string") {
if (tag[searchProp].toLowerCase().includes(searchQuery)) {
searchResults.push(tag);
if (searchResults.length >= maxResults) {
return searchResults;
}
break;
}
}
}
}
for (const tagPath in this.tagTree.systemTags) {
const tag = this.tagTree.systemTags[tagPath];
if (objectTags[tagPath]) {
continue
}
if (tag.tagName.toLowerCase().includes(searchQuery)) {
if (tag.userCanAssign == true) {
searchResults.push(tag);
}
if (searchResults.length >= maxResults) {
return searchResults;
}
}
for (const searchProp of searchProps) {
if (tag[searchProp] && typeof tag[searchProp] === "string") {
if (tag[searchProp].toLowerCase().includes(searchQuery)) {
if (tag.userCanAssign == true) {
searchResults.push(tag);
}
if (searchResults.length >= maxResults) {
return searchResults;
}
break;
}
}
}
}
return searchResults;
},
isInTrash: (objectID) => {
// console.log(this.trashTag, this.objects[objectID].tags)
if (this.trashTag && this.objects[objectID].tags[this.trashTag]) {
return true;
}
return false;
},
objectHasSystemTagName: (objectID, tagName) => {
// get tagPath
var searchingTagPath = null;
var tagID = null;
for (const tagPath in this.tagTree.systemTags) {
if (this.tagTree.systemTags[tagPath].tagName === tagName) {
searchingTagPath = tagPath;
tagID = this.tagTree.systemTags[tagPath].id;
break;
}
}
if (!searchingTagPath) {
return false;
}
for (const tagPath in this.objects[objectID].tags) {