@builttocreate/engine-utils 2.3.2 → 2.4.0
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.
- package/dist/joyDocHelper.js +85 -19
- package/package.json +1 -1
package/dist/joyDocHelper.js
CHANGED
|
@@ -393,38 +393,104 @@ var duplicateDocumentPage = function duplicateDocumentPage(doc, fileId, pageId)
|
|
|
393
393
|
(_nextDoc$fields = nextDoc.fields) === null || _nextDoc$fields === void 0 ? void 0 : _nextDoc$fields.forEach(function (field) {
|
|
394
394
|
if (field.file === fileId) fieldLookup[field._id] = field;
|
|
395
395
|
});
|
|
396
|
-
|
|
396
|
+
/**
|
|
397
|
+
* Step 1: Duplicate fields associated with target page
|
|
398
|
+
*/
|
|
399
|
+
|
|
400
|
+
var fieldIdsToDuplicate = []; //Step 1.1 Get all field Ids from primary page
|
|
401
|
+
|
|
402
|
+
var targetPageIndex = nextDoc.files[fileIndex].pages.findIndex(function (page) {
|
|
403
|
+
return page._id === pageId;
|
|
404
|
+
});
|
|
405
|
+
if (targetPageIndex !== -1) nextDoc.files[fileIndex].pages[targetPageIndex].fieldPositions.forEach(function (fieldPosition) {
|
|
406
|
+
return fieldIdsToDuplicate.push(fieldPosition.field);
|
|
407
|
+
}); //Step 1.2 Get all field Ids from view page
|
|
408
|
+
|
|
409
|
+
if (nextDoc.files[fileIndex].views && nextDoc.files[fileIndex].views.length > 0) {
|
|
410
|
+
nextDoc.files[fileIndex].views.forEach(function (view) {
|
|
411
|
+
var viewPageIndex = view.pages.findIndex(function (page) {
|
|
412
|
+
return page._id === pageId;
|
|
413
|
+
});
|
|
397
414
|
|
|
398
|
-
|
|
415
|
+
if (viewPageIndex !== -1) {
|
|
416
|
+
view.pages[viewPageIndex].fieldPositions.forEach(function (fieldPosition) {
|
|
417
|
+
if (fieldIdsToDuplicate.indexOf(fieldPosition.field) === -1) fieldIdsToDuplicate.push(fieldPosition.field);
|
|
418
|
+
});
|
|
419
|
+
}
|
|
420
|
+
});
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
var newFieldsLookupByOldId = {};
|
|
424
|
+
var duplicatedFields = [];
|
|
425
|
+
fieldIdsToDuplicate.forEach(function (fieldId) {
|
|
399
426
|
/**
|
|
400
427
|
* Why do we check if a new field has already been created?
|
|
401
428
|
*
|
|
402
429
|
* That is because multiple field positions can be associated with the same
|
|
403
430
|
* field. If a field has already been created by a previous field position
|
|
404
|
-
* that is linked to the same field as the current target then
|
|
405
|
-
*
|
|
431
|
+
* that is linked to the same field as the current target then do not create
|
|
432
|
+
* again.
|
|
406
433
|
*/
|
|
407
|
-
if (newFieldsLookupByOldId[
|
|
434
|
+
if (newFieldsLookupByOldId[fieldId]) return;
|
|
435
|
+
var field = fieldLookup[fieldId];
|
|
408
436
|
|
|
409
437
|
var duplicateField = _objectSpread(_objectSpread({}, field), {}, {
|
|
410
438
|
_id: (0, _generateObjectId["default"])()
|
|
411
439
|
});
|
|
412
440
|
|
|
413
|
-
|
|
414
|
-
changelogInfo.fields.push(duplicateField);
|
|
441
|
+
duplicatedFields.push(duplicateField);
|
|
415
442
|
/**
|
|
416
443
|
* Add to lookup so it can be used with other associated field positions
|
|
417
444
|
*/
|
|
418
445
|
|
|
419
446
|
newFieldsLookupByOldId[field._id] = duplicateField;
|
|
420
|
-
|
|
421
|
-
};
|
|
447
|
+
});
|
|
422
448
|
/**
|
|
423
|
-
* Step
|
|
449
|
+
* Step 2: Update newly duplicated fields logic properties
|
|
424
450
|
*
|
|
425
|
-
*
|
|
451
|
+
* This step can only be done after all fields have been properly duplicated.
|
|
452
|
+
*
|
|
453
|
+
* We only need to update logic conditions that referenced the target page
|
|
454
|
+
* that we are duplicating. We do this by checking the logic.condition[x].page
|
|
455
|
+
* property of the condition.
|
|
426
456
|
*/
|
|
427
457
|
|
|
458
|
+
duplicatedFields = duplicatedFields.map(function (duplicatedField) {
|
|
459
|
+
if (!duplicatedField.logic || !duplicatedField.logic.conditions || duplicatedField.logic.conditions.length < 1) return duplicatedField;
|
|
460
|
+
|
|
461
|
+
var nextField = _objectSpread(_objectSpread({}, duplicatedField), {}, {
|
|
462
|
+
logic: _objectSpread({}, duplicatedField.logic)
|
|
463
|
+
});
|
|
464
|
+
|
|
465
|
+
nextField.logic.conditions = nextField.logic.conditions.map(function (condition) {
|
|
466
|
+
/**
|
|
467
|
+
* 1. Only update conditions associated with the current page being duplicated.
|
|
468
|
+
* 2. Only update conditions associated with a field that was duplicated.
|
|
469
|
+
*/
|
|
470
|
+
if (condition.page === pageId && newFieldsLookupByOldId[condition.field]) {
|
|
471
|
+
return _objectSpread(_objectSpread({}, condition), {}, {
|
|
472
|
+
page: newPageId,
|
|
473
|
+
field: newFieldsLookupByOldId[condition.field]._id
|
|
474
|
+
});
|
|
475
|
+
} else {
|
|
476
|
+
return condition;
|
|
477
|
+
}
|
|
478
|
+
});
|
|
479
|
+
return nextField;
|
|
480
|
+
});
|
|
481
|
+
/**
|
|
482
|
+
* Step 3: Add newly duplicated fields changelogs and update doucment
|
|
483
|
+
*/
|
|
484
|
+
|
|
485
|
+
duplicatedFields.forEach(function (duplicateField) {
|
|
486
|
+
changelogInfo.fields.push(duplicateField);
|
|
487
|
+
nextDoc.fields.push(duplicateField);
|
|
488
|
+
});
|
|
489
|
+
/**
|
|
490
|
+
* Step 4: Update Primary View Page Order
|
|
491
|
+
*
|
|
492
|
+
* IMPORTANT NOTE: Page order update must go before page object creation.
|
|
493
|
+
*/
|
|
428
494
|
|
|
429
495
|
var pageOrder = getPageOrder(nextDoc.files[fileIndex].pageOrder, nextDoc.files[fileIndex].pages);
|
|
430
496
|
var primaryPageOrderIndex = pageOrder.findIndex(function (id) {
|
|
@@ -434,7 +500,7 @@ var duplicateDocumentPage = function duplicateDocumentPage(doc, fileId, pageId)
|
|
|
434
500
|
nextDoc.files[fileIndex].pageOrder = pageOrder;
|
|
435
501
|
nextDoc.files[fileIndex].pageOrder.splice(primaryPageOrderTargetIndex, 0, newPageId);
|
|
436
502
|
/**
|
|
437
|
-
* Step
|
|
503
|
+
* Step 5: Generate Primary View Page
|
|
438
504
|
*/
|
|
439
505
|
|
|
440
506
|
var primaryPageIndex = nextDoc.files[fileIndex].pages.findIndex(function (page) {
|
|
@@ -445,20 +511,20 @@ var duplicateDocumentPage = function duplicateDocumentPage(doc, fileId, pageId)
|
|
|
445
511
|
fieldPositions: []
|
|
446
512
|
}, pageDefaults);
|
|
447
513
|
nextDoc.files[fileIndex].pages[primaryPageIndex].fieldPositions.forEach(function (fieldPosition) {
|
|
448
|
-
var newField =
|
|
514
|
+
var newField = newFieldsLookupByOldId[fieldPosition.field];
|
|
449
515
|
newPrimaryPage.fieldPositions.push(_objectSpread(_objectSpread({}, fieldPosition), {}, {
|
|
450
516
|
field: newField._id
|
|
451
517
|
}));
|
|
452
518
|
});
|
|
453
519
|
nextDoc.files[fileIndex].pages.splice(primaryPageIndex + 1, 0, newPrimaryPage);
|
|
454
520
|
/**
|
|
455
|
-
* Step
|
|
521
|
+
* Step 6: Update Primary Page Changelog
|
|
456
522
|
*/
|
|
457
523
|
|
|
458
524
|
changelogInfo.primaryPages.targetIndex = primaryPageOrderTargetIndex;
|
|
459
525
|
changelogInfo.primaryPages.page = newPrimaryPage;
|
|
460
526
|
/**
|
|
461
|
-
* Step
|
|
527
|
+
* Step 7: Update Views
|
|
462
528
|
*/
|
|
463
529
|
|
|
464
530
|
if (nextDoc.files[fileIndex].views && nextDoc.files[fileIndex].views.length > 0) {
|
|
@@ -470,7 +536,7 @@ var duplicateDocumentPage = function duplicateDocumentPage(doc, fileId, pageId)
|
|
|
470
536
|
});
|
|
471
537
|
if (viewPageIndex === -1) return view;
|
|
472
538
|
/**
|
|
473
|
-
* Step
|
|
539
|
+
* Step 7.1 Update View Page Order.
|
|
474
540
|
*
|
|
475
541
|
* IMPORTANT NOTE: Page order update must go before page object creation.
|
|
476
542
|
*/
|
|
@@ -483,7 +549,7 @@ var duplicateDocumentPage = function duplicateDocumentPage(doc, fileId, pageId)
|
|
|
483
549
|
nextView.pageOrder = nextViewPageOrder;
|
|
484
550
|
nextView.pageOrder.splice(nextViewTargetPageOrderIndex, 0, newPageId);
|
|
485
551
|
/**
|
|
486
|
-
* Step
|
|
552
|
+
* Step 7.2 Update View Pages
|
|
487
553
|
*/
|
|
488
554
|
|
|
489
555
|
var newViewPage = _objectSpread(_objectSpread({}, nextView.pages[viewPageIndex]), {}, {
|
|
@@ -492,14 +558,14 @@ var duplicateDocumentPage = function duplicateDocumentPage(doc, fileId, pageId)
|
|
|
492
558
|
}, pageDefaults);
|
|
493
559
|
|
|
494
560
|
nextView.pages[viewPageIndex].fieldPositions.forEach(function (fieldPosition) {
|
|
495
|
-
var newField =
|
|
561
|
+
var newField = newFieldsLookupByOldId[fieldPosition.field];
|
|
496
562
|
newViewPage.fieldPositions.push(_objectSpread(_objectSpread({}, fieldPosition), {}, {
|
|
497
563
|
field: newField._id
|
|
498
564
|
}));
|
|
499
565
|
});
|
|
500
566
|
nextView.pages.splice(viewPageIndex + 1, 0, newViewPage);
|
|
501
567
|
/**
|
|
502
|
-
* Step
|
|
568
|
+
* Step 7.3 Add View Page Changelog Info
|
|
503
569
|
*/
|
|
504
570
|
|
|
505
571
|
changelogInfo.viewPages.push({
|