-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #99 from amplience/feature/CRNTT-sdk-changes
Feature/crntt sdk changes
- Loading branch information
Showing
23 changed files
with
410 additions
and
68 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,50 @@ | ||
import { LOCATION_HREF } from '../constants/LocationHref'; | ||
import { APPLICATION_NAVIGATOR } from '../constants/Errors'; | ||
interface ContentItem { | ||
id?: string; | ||
} | ||
|
||
interface Event { | ||
id?: string; | ||
} | ||
|
||
interface Edition { | ||
id?: string; | ||
eventId?: string; | ||
} | ||
|
||
export interface OpenOptions { | ||
returnHref: boolean; | ||
} | ||
import { Event } from '../models/Event'; | ||
import { Edition } from '../models/Edition'; | ||
import { BaseNavigator } from './BaseNavigator'; | ||
import { OpenOptions } from '../constants/Navigation'; | ||
import { ContentItemModel } from '../models/ContentItemModel'; | ||
|
||
export class ApplicationNavigator { | ||
constructor(private readonly locationHref: string, private window: Window) {} | ||
private navigationService: BaseNavigator; | ||
|
||
constructor(locationHref: string, window: Window) { | ||
this.navigationService = new BaseNavigator(locationHref, window); | ||
} | ||
|
||
openEventsCalendar(options: Partial<OpenOptions> = {}): undefined | string { | ||
return this.processHref(`${this.getBaseHref()}/planning/events/calendar`, options); | ||
return this.navigationService.navigate(`/planning/events/calendar`, options); | ||
} | ||
|
||
openEventsTimeline(options: Partial<OpenOptions> = {}): undefined | string { | ||
return this.processHref(`${this.getBaseHref()}/planning/events/timeline`, options); | ||
return this.navigationService.navigate(`/planning/events/timeline`, options); | ||
} | ||
|
||
openEventsList(options: Partial<OpenOptions> = {}): undefined | string { | ||
return this.processHref(`${this.getBaseHref()}/planning/events/list`, options); | ||
return this.navigationService.navigate(`/planning/events/list`, options); | ||
} | ||
|
||
openEvent(event: Event, options: Partial<OpenOptions> = {}): undefined | string { | ||
return this.processHref(`${this.getBaseHref()}/planning/event/${event.id}`, options); | ||
return this.navigationService.navigate(`/planning/event/${event.id}`, options); | ||
} | ||
|
||
openEdition(edition: Edition, options: Partial<OpenOptions> = {}): undefined | string { | ||
return this.processHref( | ||
`${this.getBaseHref()}/planning/edition/${edition.eventId}/${edition.id}/`, | ||
return this.navigationService.navigate( | ||
`/planning/edition/${edition.eventId}/${edition.id}/`, | ||
options | ||
); | ||
} | ||
|
||
openContentLibrary(options: Partial<OpenOptions> = {}): undefined | string { | ||
return this.processHref(`${this.getBaseHref()}/authoring/content-items`, options); | ||
} | ||
|
||
openContentItem(id: ContentItem, options: Partial<OpenOptions> = {}): undefined | string { | ||
return this.processHref(`${this.getBaseHref()}/authoring/content-item/edit/${id.id}`, options); | ||
} | ||
|
||
private processHref(href: string, options: Partial<OpenOptions>): string | undefined { | ||
if (options.returnHref) { | ||
return href; | ||
} | ||
this.window.parent.location.href = href; | ||
return this.navigationService.navigate(`/authoring/content-items`, options); | ||
} | ||
|
||
private getBaseHref() { | ||
if (this.locationHref.indexOf(LOCATION_HREF.HASH_BANG) === -1) { | ||
throw new Error(APPLICATION_NAVIGATOR.MUST_INCLUDE_HASH_BANG); | ||
} | ||
|
||
const [baseUrl, hash] = this.locationHref.split(LOCATION_HREF.HASH_BANG); | ||
const hubName = hash.split('/')[1]; | ||
return `${baseUrl}${LOCATION_HREF.HASH_BANG}/${hubName}`; | ||
openContentItem( | ||
contentItem: Partial<ContentItemModel>, | ||
options: Partial<OpenOptions> = {} | ||
): undefined | string { | ||
return this.navigationService.navigate( | ||
`/authoring/content-item/edit/${contentItem.id}`, | ||
options | ||
); | ||
} | ||
} |
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,31 @@ | ||
import { OpenOptions } from '../constants/Navigation'; | ||
import { LOCATION_HREF } from '../constants/LocationHref'; | ||
import { APPLICATION_NAVIGATOR } from '../constants/Errors'; | ||
|
||
export class BaseNavigator { | ||
constructor(public readonly locationHref: string, public window: Window) {} | ||
|
||
navigate(templatedUri: string, options: Partial<OpenOptions> = {}) { | ||
const base = this.getBaseHref(); | ||
|
||
return this.processHref(`${base}${templatedUri}`, options); | ||
} | ||
|
||
private processHref(href: string, options: Partial<OpenOptions> = {}): string | undefined { | ||
if (options.returnHref) { | ||
return href; | ||
} | ||
this.window.parent.location.href = href; | ||
} | ||
|
||
private getBaseHref() { | ||
if (this.locationHref.indexOf(LOCATION_HREF.HASH_BANG) === -1) { | ||
throw new Error(APPLICATION_NAVIGATOR.MUST_INCLUDE_HASH_BANG); | ||
} | ||
|
||
const [baseUrl, hash] = this.locationHref.split(LOCATION_HREF.HASH_BANG); | ||
const hubName = hash.split('/')[1]; | ||
|
||
return `${baseUrl}${LOCATION_HREF.HASH_BANG}/${hubName}`; | ||
} | ||
} |
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,38 @@ | ||
import { ClientConnection } from 'message-event-channel'; | ||
import { ContentEditorApplicationNavigator } from './ContentEditorNavigator'; | ||
|
||
describe('ContentEditorNavigator', () => { | ||
let navigator: ContentEditorApplicationNavigator; | ||
let onSpy: jest.SpyInstance; | ||
let connection: ClientConnection; | ||
let localWindow: Window; | ||
|
||
beforeEach(() => { | ||
connection = new ClientConnection(); | ||
onSpy = jest.spyOn(connection, 'on'); | ||
localWindow = { | ||
parent: { | ||
location: { | ||
href: '', | ||
}, | ||
}, | ||
} as Window; | ||
navigator = new ContentEditorApplicationNavigator(connection); | ||
}); | ||
|
||
describe('editContentItem', () => { | ||
it('should request from connection', async () => { | ||
jest.spyOn(connection, 'request').mockReturnValue(Promise.resolve()); | ||
|
||
await navigator.editContentItem( | ||
'3fc3a416-574f-48df-aa29-d96fdbdb6279', | ||
'https://simple-text.com' | ||
); | ||
|
||
expect(connection.request).toBeCalledWith('navigate-to-nested', { | ||
uri: 'https://simple-text.com', | ||
id: '3fc3a416-574f-48df-aa29-d96fdbdb6279', | ||
}); | ||
}); | ||
}); | ||
}); |
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,21 @@ | ||
import { ClientConnection } from 'message-event-channel'; | ||
import { CONTENT_ITEM_NAVIGATOR } from '../constants/Events'; | ||
|
||
export class ContentEditorApplicationNavigator { | ||
constructor(private readonly connection: ClientConnection) {} | ||
|
||
/** | ||
* | ||
* @param contentItemId - content item you're wanting to navigate to | ||
* @param contentTypeUri - content type uri from a schema id | ||
* | ||
* Used to navigate to nested content item | ||
* | ||
*/ | ||
editContentItem(contentItemId: string, contentTypeUri: string) { | ||
return this.connection.request<void>(CONTENT_ITEM_NAVIGATOR.NAVIGATE_TO_NESTED, { | ||
uri: contentTypeUri, | ||
id: contentItemId, | ||
}); | ||
} | ||
} |
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,49 @@ | ||
import { CONTENT_TYPES } from '../constants/Events'; | ||
import { ClientConnection } from 'message-event-channel'; | ||
import { ContentTypeModel } from '../models/ContentType'; | ||
|
||
export class ContentTypes { | ||
constructor(private connection: ClientConnection) {} | ||
|
||
/** | ||
* | ||
* @param uri schema id / content type uri | ||
* | ||
* Used to get content type settings for a schema | ||
* | ||
* @returns [[Promise<ContentTypeModel>]] | ||
* | ||
* ### Example | ||
* | ||
* ```typescript | ||
* const contentType = sdk.contentTypes.getByUri('https://example-schema.com/blog.json') | ||
* | ||
* console.log(contentType) | ||
* ``` | ||
* @returns | ||
*/ | ||
async getByUri(uri: string): Promise<ContentTypeModel> { | ||
return this.connection.request(CONTENT_TYPES.GET_BY_URI, { uri }); | ||
} | ||
|
||
/** | ||
* | ||
* @param uri list of schema id / content type uri | ||
* | ||
* Used to get content type settings for a schema | ||
* | ||
* @returns [[Promise<ContentTypeModel[]>]] | ||
* | ||
* ### Example | ||
* | ||
* ```typescript | ||
* const contentTypes = sdk.contentTypes.getByUri(['https://example-schema.com/blog.json', 'https://example-schema.com/user.json']) | ||
* | ||
* console.log(contentTypes) | ||
* ``` | ||
* @returns | ||
*/ | ||
async getByUris(uris: string[]): Promise<ContentTypeModel[]> { | ||
return this.connection.request(CONTENT_TYPES.GET_BY_URIS, { uris }); | ||
} | ||
} |
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.