-
Notifications
You must be signed in to change notification settings - Fork 633
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
Make window.kolibri
object available to Hashi iframe context
#7942
Merged
jonboiser
merged 10 commits into
learningequality:develop
from
marcellamaki:custom-channels-working-branch
Apr 15, 2021
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
97a7cfd
(WIP) Add basic window.kolibri configuration but no kolibri.js functions
marcellamaki c794cb1
(WIP) Fetch content node data and return to getContentNodeById
marcellamaki e3bdf6b
(WIP) Add additional functions to kolibri for context and navigation …
marcellamaki 8466bde
(WIP) Add first pass pseudocode to getOrUpdateContext function and up…
marcellamaki ce2e2d5
(WIP) Move helper functions to kolibri.js
marcellamaki 3d3b06b
Update tests for hashi-kolibri and code cleanup
marcellamaki 8af99b6
Remove helper functions from kolibri.js to be managed in later PR thr…
marcellamaki 6fb6f83
Add additional tests for getContext and fix data property to be set a…
marcellamaki 1afd9dc
Fix kolibri-hashi tests for MVP API
marcellamaki fa7f25f
Update tests
marcellamaki File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
/** | ||
* This class offers an API interface for interacting directly with the Kolibri app | ||
* that the HTML5 app is embedded within | ||
*/ | ||
import BaseShim from './baseShim'; | ||
import Mediator from './mediator'; | ||
import { events, nameSpace } from './hashiBase'; | ||
|
||
/** | ||
* Type definition for Language metadata | ||
* @typedef {Object} Language | ||
* @property {string} id - an IETF language tag | ||
* @property {string} lang_code - the ISO 639‑1 language code | ||
* @property {string} lang_subcode - the regional identifier | ||
* @property {string} lang_name - the name of the language in that language | ||
* @property {('ltr'|'rtl'|)} lang_direction - Direction of the language's script, | ||
* top to bottom is not supported currently | ||
*/ | ||
|
||
/** | ||
* Type definition for ContentNode metadata | ||
* @typedef {Object} ContentNode | ||
* @property {string} id - unique id of the ContentNode | ||
* @property {string} channel_id - unique channel_id of the channel that the ContentNode is in | ||
* @property {string} content_id - identifier that is common across all instances of this resource | ||
* @property {string} title - A title that summarizes this ContentNode for the user | ||
* @property {string} description - detailed description of the ContentNode | ||
* @property {string} author - author of the ContentNode | ||
* @property {string} thumbnail_url - URL for the thumbnail for this ContentNode, | ||
* this may be any valid URL format including base64 encoded or blob URL | ||
* @property {boolean} available - Whether the ContentNode has all necessary files for rendering | ||
* @property {boolean} coach_content - Whether the ContentNode is intended only for coach users | ||
* @property {Language} lang - The primary language of the ContentNode | ||
* @property {string} license_description - The description of the license, which may be localized | ||
* @property {string} license_name - The human readable name of the license, localized | ||
* @property {string} license_owner - The name of the person or organization that holds copyright | ||
* @property {number} num_coach_contents - Number of coach contents that are descendants of this | ||
* @property {string} parent - The unique id of the parent of this ContentNode | ||
* @property {number} sort_order - The order of display for this node in its channel | ||
* if depth recursion was not deep enough | ||
*/ | ||
|
||
/** | ||
* Type definition for pagination object | ||
* @typedef {Object} PageResult | ||
* @property {number} page - the page that this pagination object represents | ||
* @property {number} pageSize - the page size for this pagination object | ||
* @property {ContentNode[]} results - the array of ContentNodes for this page | ||
*/ | ||
|
||
/** | ||
* Type definition for Theme options | ||
* properties TBD | ||
* @typedef {Object} Theme | ||
*/ | ||
|
||
/** | ||
* Type definition for NavigationContext | ||
* This can have arbitrary properties as defined | ||
* by the navigating app that it uses to resume its state | ||
* Should be able to be encoded down to <1600 characters using | ||
* an encoding function something like 'encode context' above | ||
* @typedef {Object} NavigationContext | ||
* @property {string} node_id - The current node_id that is being displayed, | ||
* custom apps should handle this as it may be used to | ||
* generate links externally to jump to this state | ||
*/ | ||
|
||
export default class Kolibri extends BaseShim { | ||
constructor(mediator) { | ||
super(mediator); | ||
this.data = {}; | ||
this.nameSpace = 'kolibri'; | ||
this.mediator = new Mediator(window.parent); | ||
} | ||
|
||
encodeContext(context) { | ||
return encodeURI(Object.entries(context).map(([k, v]) => `${k}:${v}`)); | ||
} | ||
|
||
iframeInitialize(contentWindow) { | ||
this.__setShimInterface(); | ||
Object.defineProperty(contentWindow, this.nameSpace, { | ||
value: this.shim, | ||
configurable: true, | ||
}); | ||
} | ||
|
||
__setShimInterface() { | ||
const self = this; | ||
|
||
class Shim { | ||
/* | ||
* Method to query contentnodes from Kolibri and return | ||
* an array of matching metadata | ||
* @param {Object} options - The different options to filter by | ||
* @param {string=} options.parent - id of the parent node to filter by, or 'self' | ||
* @param {string[]} options.ids - an array of ids to filter by | ||
* @param {number} [options.page=1] - which page to return from the result set | ||
* @param {number} [options.pageSize=50] - the page size for pagination | ||
* @return {Promise<PageResult>} - a Promise that resolves to an array of ContentNodes | ||
*/ | ||
getContentByFilter(options) { | ||
return self.mediator.sendMessageAwaitReply({ | ||
event: events.DATAREQUESTED, | ||
data: { options, dataType: 'Collection' }, | ||
nameSpace, | ||
}); | ||
} | ||
/* | ||
* Method to query a single contentnode from Kolibri and return | ||
* a metadata object | ||
* @param {string} id - id of the ContentNode | ||
* @return {Promise<ContentNode>} - a Promise that resolves to a ContentNode | ||
*/ | ||
getContentById(id) { | ||
return self.mediator.sendMessageAwaitReply({ | ||
event: events.DATAREQUESTED, | ||
data: { id, dataType: 'Model' }, | ||
nameSpace, | ||
}); | ||
} | ||
/* | ||
* Method to search for contentnodes on Kolibri and return | ||
* an array of matching metadata | ||
* @param {Object} options - The different options to search by | ||
* @param {string=} options.keyword - search term for key word search | ||
* @param {string=} options.under - id of topic to search under, or 'self' | ||
* @param {number} [options.page=1] - which page to return from the result set | ||
* @param {number} [options.pageSize=50] - the page size for pagination | ||
* @return {Promise<PageResult>} - a Promise that resolves to an array of ContentNodes | ||
*/ | ||
searchContent() {} | ||
|
||
/* | ||
* Method to set a default theme for any content rendering initiated by this app | ||
* @param {Theme} options - The different options for custom themeing | ||
* @return {Promise} - a Promise that resolves when the theme has been applied | ||
*/ | ||
themeRenderer() {} | ||
|
||
/* | ||
* Method to allow navigation to or rendering of a specific node | ||
* has optional parameter context that can update the URL for a custom context. | ||
* When this is called for a resource node in the custom navigation context | ||
* this will launch a renderer overlay to maintain the current state, and update the | ||
* query parameters for the URL of the custom context to indicate the change | ||
* If called for a topic in a custom context or outside of a custom context | ||
* this will simply prompt navigation to that node in Kolibri. | ||
* @param {string} nodeId - id of the parent node to navigate to | ||
* @param {NavigationContext=} context - optional context describing the state update | ||
* if node_id is missing from the context, it will be automatically filled in by this method | ||
* @return {Promise} - a Promise that resolves when the navigation has completed | ||
*/ | ||
navigateTo(nodeId, context) { | ||
return self.mediator.sendMessageAwaitReply({ | ||
event: events.NAVIGATETO, | ||
data: { nodeId, context }, | ||
nameSpace, | ||
}); | ||
} | ||
|
||
/* | ||
* Method to allow updating of stored state in the URL | ||
* @param {NavigationContext} context - context describing the state update | ||
* @return {Promise} - a Promise that resolves when the context has been updated | ||
*/ | ||
updateContext(context) { | ||
return self.mediator.sendMessageAwaitReply({ | ||
event: events.CONTEXT, | ||
data: { context }, | ||
nameSpace, | ||
}); | ||
} | ||
|
||
/* | ||
* Method to request the current context state | ||
* @return {Promise<NavigationContext>} - a Promise that resolves | ||
* when the context has been updated | ||
*/ | ||
getContext() { | ||
return self.mediator.sendMessageAwaitReply({ | ||
event: events.CONTEXT, | ||
data: {}, | ||
nameSpace, | ||
}); | ||
} | ||
} | ||
this.shim = new Shim(); | ||
return this.shim; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import uuidv4 from 'uuid/v4'; | ||
|
||
/* | ||
* This class manages all message listening and sending from the postMessage | ||
* layer. All interfaces that need to message via the postMessage layer should | ||
|
@@ -50,6 +52,38 @@ class Mediator { | |
this.remote.postMessage(message, '*'); | ||
} | ||
|
||
// a function to manage messages for kolibri.js, | ||
// when most messages require a response, to minimize redundancy | ||
sendMessageAwaitReply({ event, data, nameSpace }) { | ||
return new Promise((resolve, reject) => { | ||
const msgId = uuidv4(); | ||
function handler(message) { | ||
if (message.message_id === msgId && message.type === 'response') { | ||
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. Once we've received the corresponding message, would be good to unregister the message handler for cleanup. |
||
this.removeMessageHandler({ | ||
nameSpace, | ||
event: 'datareturned', | ||
callback: handler, | ||
}); | ||
if (message.status === 'success') { | ||
console.log(message.data); | ||
return resolve(message.data); | ||
} else if (message.status === 'failure' && message.err) { | ||
return reject(message.err); | ||
} | ||
// Otherwise something unspecified happened | ||
return reject(); | ||
} | ||
} | ||
this.registerMessageHandler({ | ||
nameSpace, | ||
event: 'datareturned', | ||
callback: handler, | ||
}); | ||
data.message_id = msgId; | ||
this.sendMessage({ event, data, nameSpace }); | ||
}); | ||
} | ||
|
||
registerMessageHandler({ event, nameSpace, callback } = {}) { | ||
if ( | ||
typeof callback !== 'function' || | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This looks very familiar....