@builttocreate/engine-utils 2.8.0-beta.8 → 2.8.0-beta.9

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.
@@ -161,44 +161,97 @@ var getCleanedJoyDoc = exports.getCleanedJoyDoc = function getCleanedJoyDoc(doc)
161
161
  * Remove orphaned fields from doc
162
162
  */
163
163
 
164
- var removeOrphanedFieldsFromJoydoc = exports.removeOrphanedFieldsFromJoydoc = function removeOrphanedFieldsFromJoydoc(doc) {
165
- var nextDoc = _objectSpread({}, doc);
164
+ // export const removeOrphanedFieldsFromJoydoc = (doc) => {
166
165
 
167
- /**
168
- * Logic below removes orphaned fields (ie fields without fieldPositions)
169
- */
166
+ // const nextDoc = { ...doc };
170
167
 
171
- nextDoc.fields = nextDoc.fields.filter(function (field) {
172
- var isOrphanedField = true;
173
- nextDoc.files.forEach(function (file) {
174
- var primaryViewPages = file.pages;
168
+ // /**
169
+ // * Logic below removes orphaned fields (ie fields without fieldPositions)
170
+ // */
175
171
 
176
- // Check if field is associated with any field positions in primary view pages
177
- var isFieldAssociatedWithPrimaryView = primaryViewPages.some(function (page) {
178
- return page.fieldPositions.some(function (fieldPosition) {
179
- return fieldPosition.field === field._id;
180
- });
172
+ // nextDoc.fields = nextDoc.fields.filter((field) => {
173
+
174
+ // let isOrphanedField = true;
175
+
176
+ // nextDoc.files.forEach((file) => {
177
+
178
+ // const primaryViewPages = file.pages;
179
+
180
+ // // Check if field is associated with any field positions in primary view pages
181
+ // const isFieldAssociatedWithPrimaryView = primaryViewPages.some((page) => {
182
+ // return page.fieldPositions.some((fieldPosition) => fieldPosition.field === field._id);
183
+ // });
184
+
185
+ // // Check if field is associated with other views
186
+ // let isFieldAssociatedWithAnotherView;
187
+
188
+ // if (file.views && file.views.length > 0) {
189
+
190
+ // isFieldAssociatedWithAnotherView = file.views.some((view) => {
191
+ // return view.pages.some((page) => {
192
+ // return page.fieldPositions.some((fieldPosition) => fieldPosition.field === field._id);
193
+ // });
194
+ // });
195
+
196
+ // }
197
+
198
+ // if (isFieldAssociatedWithPrimaryView || isFieldAssociatedWithAnotherView) {
199
+ // isOrphanedField = false;
200
+ // }
201
+
202
+ // });
203
+
204
+ // if (!isOrphanedField) return field;
205
+
206
+ // return null;
207
+
208
+ // });
209
+
210
+ // return nextDoc;
211
+
212
+ // };
213
+
214
+ var removeOrphanedFieldsFromJoydoc = exports.removeOrphanedFieldsFromJoydoc = function removeOrphanedFieldsFromJoydoc(doc) {
215
+ var _nextDoc$files;
216
+ var nextDoc = _objectSpread({}, doc);
217
+ var associatedFieldIdLookup = {};
218
+ nextDoc === null || nextDoc === void 0 ? void 0 : (_nextDoc$files = nextDoc.files) === null || _nextDoc$files === void 0 ? void 0 : _nextDoc$files.forEach(function (file) {
219
+ var _file$views;
220
+ /**
221
+ * Primary view
222
+ */
223
+ file.pages.forEach(function (page) {
224
+ page.fieldPositions.forEach(function (fieldPosition) {
225
+ return associatedFieldIdLookup[fieldPosition.field] = true;
181
226
  });
227
+ });
182
228
 
183
- // Check if field is associated with other views
184
- var isFieldAssociatedWithAnotherView;
185
- if (file.views && file.views.length > 0) {
186
- isFieldAssociatedWithAnotherView = file.views.some(function (view) {
187
- return view.pages.some(function (page) {
188
- return page.fieldPositions.some(function (fieldPosition) {
189
- return fieldPosition.field === field._id;
190
- });
191
- });
229
+ /**
230
+ * Alternative view
231
+ */
232
+ (_file$views = file.views) === null || _file$views === void 0 ? void 0 : _file$views.forEach(function (view) {
233
+ var _view$pages;
234
+ view === null || view === void 0 ? void 0 : (_view$pages = view.pages) === null || _view$pages === void 0 ? void 0 : _view$pages.forEach(function (page) {
235
+ page === null || page === void 0 ? void 0 : page.fieldPositions.forEach(function (fieldPosition) {
236
+ return associatedFieldIdLookup[fieldPosition.field] = true;
192
237
  });
193
- }
194
- if (isFieldAssociatedWithPrimaryView || isFieldAssociatedWithAnotherView) {
195
- isOrphanedField = false;
196
- }
238
+ });
197
239
  });
198
- if (!isOrphanedField) return field;
199
- return null;
200
240
  });
201
- return nextDoc;
241
+
242
+ /**
243
+ * Logic below removes orphaned fields (ie fields without fieldPositions)
244
+ */
245
+ var validFields = [];
246
+ var deletedFields = [];
247
+ nextDoc.fields.forEach(function (field) {
248
+ if (associatedFieldIdLookup[field._id]) validFields.push(field);else deletedFields.push(field);
249
+ });
250
+ nextDoc.fields = validFields;
251
+ return {
252
+ doc: nextDoc,
253
+ deletedFields: deletedFields
254
+ };
202
255
  };
203
256
 
204
257
  /**
@@ -590,8 +643,8 @@ var duplicateDocumentPage = exports.duplicateDocumentPage = function duplicateDo
590
643
  * @returns {Object}
591
644
  */
592
645
  var getMobileViewFromFile = exports.getMobileViewFromFile = function getMobileViewFromFile(file) {
593
- var _file$views;
594
- var mobileViewIndex = file !== null && file !== void 0 && file.views && (file === null || file === void 0 ? void 0 : (_file$views = file.views) === null || _file$views === void 0 ? void 0 : _file$views.length) > 0 ? file.views.findIndex(function (view) {
646
+ var _file$views2;
647
+ var mobileViewIndex = file !== null && file !== void 0 && file.views && (file === null || file === void 0 ? void 0 : (_file$views2 = file.views) === null || _file$views2 === void 0 ? void 0 : _file$views2.length) > 0 ? file.views.findIndex(function (view) {
595
648
  return view.type === _FileViews["default"].mobile;
596
649
  }) : -1;
597
650
  if (mobileViewIndex !== -1) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@builttocreate/engine-utils",
3
- "version": "2.8.0-beta.8",
3
+ "version": "2.8.0-beta.9",
4
4
  "description": "Utility library for common logic shared across web and mobile",
5
5
  "main": "dist/index.js",
6
6
  "files": [