-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
adds method to sort all documents and pages #101
base: main
Are you sure you want to change the base?
Changes from all commits
47cdd53
e7e239b
b5ca144
bdfe0ae
14ce415
cc664da
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,9 +33,11 @@ module.exports = self => { | |
|
||
const hasRelatedTypes = !!relatedTypes.length; | ||
|
||
const docs = (await self.getDocs(req, ids, hasRelatedTypes, manager, reporting)) | ||
const docs = (await self.getDocs(req, ids, manager, reporting)) | ||
.map((doc) => self.apos.util.clonePermanent(doc)); | ||
|
||
if (!hasRelatedTypes) { | ||
self.sortDocs(docs); | ||
return self.exportFile( | ||
req, | ||
reporting, | ||
|
@@ -55,6 +57,8 @@ module.exports = self => { | |
}); | ||
} | ||
|
||
self.sortDocs(allDocs); | ||
|
||
if (!format.includeAttachments) { | ||
return self.exportFile( | ||
req, | ||
|
@@ -101,10 +105,48 @@ module.exports = self => { | |
); | ||
}, | ||
|
||
sortDocs(docs) { | ||
docs.sort((a, b) => { | ||
if (a.aposMode === 'draft' && b.aposMode === 'published') { | ||
return -1; | ||
} | ||
if (a.aposMode === 'published' && b.aposMode === 'draft') { | ||
return 1; | ||
} | ||
if (!self.apos.page.isPage(a) && !self.apos.page.isPage(b)) { | ||
return 0; | ||
} | ||
if (self.apos.page.isPage(a) && !self.apos.page.isPage(b)) { | ||
return -1; | ||
} | ||
if (!self.apos.page.isPage(a) && self.apos.page.isPage(b)) { | ||
return 1; | ||
} | ||
if (a.level > b.level) { | ||
return 1; | ||
} | ||
if (a.level < b.level) { | ||
return -1; | ||
} | ||
if (a.rank < b.rank) { | ||
return -1; | ||
} | ||
if (a.rank > b.rank) { | ||
return 1; | ||
} | ||
return 0; | ||
}); | ||
}, | ||
|
||
// Get docs via their manager in order to populate them | ||
// so that we can retrieve their relationships IDs later, | ||
// and to let the manager handle permissions. | ||
async getDocs(req, docsIds, includeRelationships, manager, reporting) { | ||
async getDocs(req, docsIds, manager, reporting) { | ||
// For BC, used to accept hasRelatedTypes as third param | ||
if (arguments.length === 5) { | ||
manager = arguments[3]; | ||
reporting = arguments[4]; | ||
} | ||
if (!docsIds.length) { | ||
return []; | ||
} | ||
|
@@ -121,7 +163,7 @@ module.exports = self => { | |
$in: draftIds | ||
} | ||
}) | ||
.relationships(includeRelationships) | ||
.relationships(false) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unneeded since we get related docs manually There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. BC issue: someone may have overridden this method, so please check for 5 arguments and if there are five, accept them that way too, ignoring the redundant parameter. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Riiiight BC, always forget that. Would be so good to have a public API, and internal methods that shouldn't be updated by users. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done! |
||
.toArray(); | ||
|
||
docs.push(...draftDocs); | ||
|
@@ -133,7 +175,7 @@ module.exports = self => { | |
$in: publishedIds | ||
} | ||
}) | ||
.relationships(includeRelationships) | ||
.relationships(false) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unneeded since we get related docs manually There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoiding the redundant call should be a nice little perf win then. |
||
.toArray(); | ||
|
||
docs.push(...publishedDocs); | ||
|
@@ -225,7 +267,7 @@ module.exports = self => { | |
if (!field.withType && !fieldValue) { | ||
continue; | ||
} | ||
if (field.withType && relatedTypes && !relatedTypesIncludes(field.withType)) { | ||
if (field.withType && relatedTypes && !self.relatedTypesIncludes(field.withType, relatedTypes)) { | ||
continue; | ||
} | ||
if (field.withType && !self.canExport(req, field.withType)) { | ||
|
@@ -290,16 +332,16 @@ module.exports = self => { | |
}); | ||
} | ||
} | ||
}, | ||
|
||
function relatedTypesIncludes(name) { | ||
if ([ '@apostrophecms/any-page-type', '@apostrophecms/page' ].includes(name)) { | ||
return relatedTypes.some(type => { | ||
const module = self.apos.modules[type]; | ||
return self.apos.instanceOf(module, '@apostrophecms/page-type'); | ||
}); | ||
} | ||
return relatedTypes.includes(name); | ||
relatedTypesIncludes(name, relatedTypes) { | ||
if ([ '@apostrophecms/any-page-type', '@apostrophecms/page' ].includes(name)) { | ||
return relatedTypes.some(type => { | ||
const module = self.apos.modules[type]; | ||
return self.apos.instanceOf(module, '@apostrophecms/page-type'); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @boutell I can move this function in a utils file if you think it makes no sense to attach it to self (which I would agree with I think). |
||
} | ||
return relatedTypes.includes(name); | ||
}, | ||
|
||
async getRelatedDocsFromRichTextWidget(req, { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we do not get relationships whatever if
export related docs
has been checked or no, we don't need this param anymore.