-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added navigation on page * Removed useless log, added docs to navigator component * Fixed duplicated variables, some changes in navigation functions, changed pages.twig and navigator.twig * Added flatArray model, changed navigation functions * Replaced page footer style to page.pcss * Fixed generating flat array, when pages remove * Removed useless generating * Renamed flatArray model to pagesFlatArray, updated descriptions, renamed generate to regenerate, removed hardcoded key name in cache * Changed styles naming and added margin for navigation * Added ability to change nesting in flat array, fixed BEM * Updated nesting parameter, fixed BEM * Changed navigator component by passing objects, removed navigator wrapper * Style navigator renamed to navigator__item * Update src/backend/models/pagesFlatArray.ts Co-authored-by: Peter Savchenko <[email protected]> * Renamed navigator__item to navigator_item * Deleted wrappers from navigator buttons, removed page__footer Co-authored-by: Peter Savchenko <[email protected]>
- Loading branch information
Showing
15 changed files
with
352 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
import Page from './page'; | ||
import PageOrder from './pageOrder'; | ||
import NodeCache from 'node-cache'; | ||
|
||
// Create cache for flat array | ||
const cache = new NodeCache({ stdTTL: 120 }); | ||
|
||
const cacheKey = 'pagesFlatArray'; | ||
|
||
/** | ||
* Element for pagesFlatArray | ||
*/ | ||
export interface PagesFlatArrayData { | ||
/** | ||
* Page id | ||
*/ | ||
id: string; | ||
|
||
/** | ||
* Page parent id | ||
*/ | ||
parentId?: string; | ||
|
||
/** | ||
* id of parent with parent id '0' | ||
*/ | ||
rootId: string; | ||
|
||
/** | ||
* Page level in sidebar view | ||
*/ | ||
level: number; | ||
|
||
/** | ||
* Page title | ||
*/ | ||
title: string; | ||
|
||
/** | ||
* Page uri | ||
*/ | ||
uri?: string; | ||
} | ||
|
||
/** | ||
* @class PagesFlatArray model - flat array of pages, which are ordered like in sidebar | ||
*/ | ||
class PagesFlatArray { | ||
/** | ||
* Returns pages flat array | ||
* | ||
* @param nestingLimit - number of flat array nesting, set null to dismiss the restriction, default nesting 2 | ||
* @returns {Promise<Array<PagesFlatArrayData>>} | ||
*/ | ||
public static async get(nestingLimit: number | null = 2): Promise<Array<PagesFlatArrayData>> { | ||
// Get flat array from cache | ||
let arr = cache.get(cacheKey) as Array<PagesFlatArrayData>; | ||
|
||
// Check is flat array consists in cache | ||
if (!arr) { | ||
arr = await this.regenerate(); | ||
} | ||
|
||
if (!nestingLimit) { | ||
return arr; | ||
} | ||
|
||
return arr.filter( (item) => item.level < nestingLimit ); | ||
} | ||
|
||
/** | ||
* Generates new flat array, saves it to cache, returns it | ||
* Calls, when there is no pages flat array data in cache or when page or pageOrder data updates | ||
* | ||
* @returns {Promise<Array<PagesFlatArrayData>>} | ||
*/ | ||
public static async regenerate(): Promise<Array<PagesFlatArrayData>> { | ||
const pages = await Page.getAll(); | ||
const pagesOrders = await PageOrder.getAll(); | ||
|
||
let arr = new Array<PagesFlatArrayData>(); | ||
|
||
// Get root order | ||
const rootOrder = pagesOrders.find( order => order.page == '0' ); | ||
|
||
// Check is root order is not empty | ||
if (!rootOrder) { | ||
return []; | ||
} | ||
|
||
for (const pageId of rootOrder.order) { | ||
arr = arr.concat(this.getChildrenFlatArray(pageId, 0, pages, | ||
pagesOrders)); | ||
} | ||
|
||
// Save generated flat array to cache | ||
cache.set(cacheKey, arr); | ||
|
||
return arr; | ||
} | ||
|
||
/** | ||
* Returns previous page | ||
* | ||
* @param pageId - page id | ||
* @returns {Promise<PagesFlatArrayData | undefined>} | ||
*/ | ||
public static async getPageBefore(pageId: string): Promise<PagesFlatArrayData | undefined> { | ||
const arr = await this.get(); | ||
|
||
const pageIndex = arr.findIndex( (item) => item.id == pageId); | ||
|
||
// Check if index is not the first | ||
if (pageIndex && pageIndex > 0) { | ||
// Return previous element from array | ||
return arr[pageIndex - 1]; | ||
} else { | ||
return; | ||
} | ||
} | ||
|
||
/** | ||
* Returns next page | ||
* | ||
* @param pageId - page id | ||
* @returns {Promise<PagesFlatArrayData | undefined>} | ||
*/ | ||
public static async getPageAfter(pageId: string): Promise<PagesFlatArrayData | undefined> { | ||
const arr = await this.get(); | ||
|
||
const pageIndex = arr.findIndex( (item) => item.id == pageId ); | ||
|
||
// Check if index is not the last | ||
if (pageIndex < arr.length -1) { | ||
// Return next element from array | ||
return arr[pageIndex + 1]; | ||
} else { | ||
return; | ||
} | ||
} | ||
|
||
/** | ||
* Returns child pages array | ||
* | ||
* @param pageId - parent page id | ||
* @param level - page level in sidebar | ||
* @param pages - all pages | ||
* @param orders - all page orders | ||
* @returns {Promise<Array<PagesFlatArrayData>>} | ||
*/ | ||
private static getChildrenFlatArray(pageId: string, level: number, | ||
pages: Array<Page>, orders: Array<PageOrder>): Array<PagesFlatArrayData> { | ||
let arr: Array<PagesFlatArrayData> = new Array<PagesFlatArrayData>(); | ||
|
||
const page = pages.find( item => item._id == pageId ); | ||
|
||
// Add element to child array | ||
if (page) { | ||
arr.push( { | ||
id: page._id!, | ||
level: level, | ||
parentId: page._parent, | ||
rootId: '0', | ||
title: page.title!, | ||
uri: page.uri, | ||
} ); | ||
} | ||
|
||
const order = orders.find(item => item.page == pageId); | ||
|
||
if (order) { | ||
for (const childPageId of order.order) { | ||
arr = arr.concat(this.getChildrenFlatArray(childPageId, level + 1, | ||
pages, orders)); | ||
} | ||
} | ||
|
||
return arr; | ||
} | ||
} | ||
|
||
export default PagesFlatArray; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.