3.0.2 • Published 4 months ago

@pearson-components/app-header v3.0.2

Weekly downloads
95
License
PEARSON PROPRIETA...
Repository
github
Last release
4 months ago

Deprecated support

Updated to support Node 20 in v3

Usage

If you are consuming version 1.0.0+ of app-header, you must consume the Elements SDK, as it is a required dependency. It is looking for the /fonts assets in the root of your served application.

Assumption: You have Node v4+ and npm 2+ installed.

> npm i --save @pearson-components/app-header

Bundle (Recommended)

The javascript bundle is available in /node_modules/@pearson-components/app-header/build/dist.app-header.js.

Add the following script include to your web page:

	<script src="path/to/dist.app-header.js"></script>

Please see the demo code for examples of how to dispatch the event to initialize the app-header.

Note this example is using CustomEvent, which must be poly-filled for IE.

document.dispatchEvent(new CustomEvent('o.DOMContentLoaded', {
    detail: {
      element: element,
      config: config
    }
  }));

The config property options are outlined further down in this README.

CommonJS

This method requires a web bundler, such as webpack or browserify.

After installing the app-header from npm, define the target element and configuration options.

var AppHeader = require('@pearson-components/app-header');
var appHeader = new AppHeader(element, options);

Via CDN

This component is published to NPM and is therefore available via unkpg.com. This allows teams to be able to consume the component through a simple script include. Below is an example which allows a basic App-Header to appear on the page.

<html>
  <head>
    <script type="text/javascript" src="https://unpkg.com/@pearson-components/app-header@2.1.0/build/dist.app-header.js"></script>
    <script>
      document.addEventListener('DOMContentLoaded', function() {
        var element = document.getElementsByTagName('body');
        var config = {
          session: 'session',
          user: {givenName: 'Bender'},
          mode: 'Basic',
          onLogin: function () { alert('You signed in'); },
          onLogout: function () { alert('Buh-bye'); }
        };

        document.dispatchEvent(new CustomEvent('o.DOMContentLoaded', {
          detail: {
            element: element,
            config: config
          }
        }));
      });
    </script>
  </head>
  <body>
    <script>
      if (document.body.children[0].nodeName !== 'svg') {
        var pe_ajax = new XMLHttpRequest();
        pe_ajax.open("GET", "https://unpkg.com/pearson-elements@1.14.9/dist/icons/p-icons-sprite-1.1.svg", true);
        pe_ajax.responseType = "document";
        pe_ajax.onload = function(e) {
          document.body.insertBefore(
            pe_ajax.responseXML.documentElement,
            document.body.childNodes[0]
          );
        }
        pe_ajax.send();
      }

      document.dispatchEvent(new CustomEvent('DOMContentLoaded'));
    </script>
  </body>
</html>

Configuration

Configuration properties may be passed as an object argument (preferred) to the constructor or defined in a configuration block on the page:

<script data-o-app-header-config type="application/json">
  {
  	"consoleBaseUrl": "https://...",
  	"session": "Session"
  }
</script>

Refer to the options object for a list of properties.

Integration

Refer to the Integration document, which details use with piSession.js and context-based mode switching.

API

Constructor

new AppHeader([element], [options])

Creates a new instance of AppHeader.

  • element: an HTMLElement or selector string. The selected element will be replaced with the header element unless it is document.body, in which case the header will be preprended to the body content.
  • options: an object with the following properties:
PropertyTypeDescription
consoleBaseUrlstringThe consuming application base URL (default: https://console.pearson.com)
linksobjectLinks in the user dropdown. Refer to the Links section for the default list
modestringThe mode. Refer to the Modes section for a list of supported modes and options.
company_logostringdefault: Pearson logo
app_logostringThe application logo
registerbooleanTo change 'Sign In' to 'Sign in or Register'
var AppHeader = require('@pearson-components/app-header');
var appHeader = new AppHeader({ mode: 'Signed Out', showLoginControls: false });

Logo

Pass the url's for the property "company_logo" and "app_logo" as an object, and they will replace the default Pearson logo (preferred).

  • company_logo: a string with the url for the company logo image.
  • app_logo : a string with the url for the application logo image.

When only the application logo is added, the default Pearson logo will be added along with the application logo image.

The height property of both logos should be 37px.

'Sign In'

If the 'Sign In' wording needs to be changed to 'Sign in or Register' the boolean property register is passed as true

Default register is false

Methods

setMode(mode, [options])

Sets the mode. Refer to the Modes section for a list of supported modes and options.

  • mode: a string that indicates the header mode.
  • options: an object containing the mode options.

Links object

By default the links object looks like:

Link NameURL
home{consoleBaseUrl}/console/home
myAccount{consoleBaseUrl}/account/manage/account
termsOfUse{consoleBaseUrl}/account/manage/tos
privacyPolicy{consoleBaseUrl}/account/manage/privacy

Modes

The application header supports multiple modes that can be configured depending on the type and state of the application.

Mode: Signed Out

Use this mode when the user is anonymous.

new AppHeader({ mode: 'Signed Out', showLoginControls: false });

Options

PropertyTypeDescription
showLoginControlsBooleanIf true, show the 'Sign In' nav item. Default: true.
onLoginFunction or stringA callback function that will be called when the user clicks the Sign In nav item. If this value is a string, the component will look in the global scope for a function with a matching name.

Mode: Basic

Use this mode when the user is authenticated.

new AppHeader({
	mode: 'Basic',
	user: {
		givenName: 'John'
	},
	courseItems: [
		{ text: 'Physics', href: 'https://example.com/physics' },
		{ text: 'Chemistry', href: 'https://example.com/physics' }
	]
});

Options

PropertyTypeDescription
userObjectThe user data.
user.givenNamestringThe user's given name.
courseItemsArray<Object>A collection of course menu items.
courseItems.textstringThe menu item text.
courseItems.hrefstringThe menu item url.
courseItems.onClickFunctionIf provided, the function will be called when the user clicks the menu item.
onLogoutFunction or stringA callback function that will be called when the user clicks the Sign Out menu item. If this value is a string, the component will look in the global scope for a function with a matching name.

Mode: Course

Use this mode when the user is authenticated and is in an application that adheres to the course format.

new AppHeader({
	mode: 'Course',
	user: {
		givenName: 'John'
	},
	courseNav: {
		heading: { text: 'Physics', href: 'https://example.com/physics' },
		items: [
			{ text: 'Performance', href: 'https://example.com/performance' },
			{ text: 'Assessments', href: 'https://example.com/assessments' }
		]
	}
});

Options

PropertyTypeDescription
userObjectThe user data.
user.givenNamestringThe user's given name.
courseNavObjectAn object containing options for course navigation menu items.
courseNav.headingObjectAn object representing the current course.
courseNav.heading.textstringThe heading menu item text.
courseNav.heading.hrefstringThe heading menu item url.
courseNav.itemsArray<Object>The course navigation menu items.
courseNav.items.textstringThe menu item text.
courseNav.items.hrefstringThe menu item url.
courseNav.items.activeBooleanRepresents the active area of the course. If true, the menu item will be disabled. Default: false.
onLogoutFunction or stringA callback function that will be called when the user clicks the Sign Out menu item. If this value is a string, the component will look in the global scope for a function with a matching name.

Mode: Integration

Use this mode when the application is configured as an integration with a non-Pearson application or web propertyfor example, a third party LMS.

new AppHeader({ mode: 'Integration' });

Events

Event NameDescription
oAppHeader.help.toggleFires when the Help nav item is clicked.
oAppHeader.loginFires when the Sign In nav item is clicked.
oAppHeader.logoutFires when the Sign Out menu item is clicked.
document.addEventListener('oAppHeader.help.toggle', function (e) {
	// Do stuff
});

z-index

By default, the header's z-index property is set to 1000. This value can be changed by setting the $o-app-header-z-index SASS variable.

Changing the header.template

If you make changes to the header.template file, the template generator needs to be run (manually).

node bin/gen-template srcFile endFile

The endFile here is template.js, meaning if you made a change in header.template, your command is

node bin/gen-template src/html/header.template src/js/template.js

Browser support

Browser versions that do not support WeakMap must use a polyfill, for example via the polyfill service:

<script src="https://cdn.polyfill.io/v1/polyfill.min.js?features=default,WeakMap"></script>

Contributing

Please review the guidelines for contributing before getting started.

Toolchain

Recommendation: If you are using different node versions on your machine, use nvm to manage them.

Demo

The following npm script will build the component, fire up a webpack dev server at localhost:8080/demo, and hot reload any saved changes to the source without having to refresh the browser.

npm install
npm run dev

Test

The following npm script will execute the unit tests.

npm test

How do I debug?

Source maps are enabled for the webpack dev server. Using Chrome dev tools - open the "Sources" tab, navigate to top/webpack://./, and you will find the original source files for which you can set breakpoints in Chrome's debugger.

License

This software is published by Pearson Education under the MIT license.

3.0.2

4 months ago

2.5.0

5 years ago

2.3.1

5 years ago

2.3.0

5 years ago

2.2.2

6 years ago

2.2.1

6 years ago

2.2.0

6 years ago

2.1.2

7 years ago

2.1.1

7 years ago

2.1.0

7 years ago

2.0.15

7 years ago

2.0.14

7 years ago

2.0.13

7 years ago

2.0.12

7 years ago

2.0.11

7 years ago

2.0.10

7 years ago

2.0.9

7 years ago

2.0.8

7 years ago

2.0.7

8 years ago

2.0.6

8 years ago

2.0.5

8 years ago

2.0.4

8 years ago

2.0.3

8 years ago

2.0.2

8 years ago

2.0.1

8 years ago

1.1.2

9 years ago

1.1.1

9 years ago

1.1.0

9 years ago

1.0.1

9 years ago

1.0.0

9 years ago

0.1.7

9 years ago

0.1.6

9 years ago

0.1.5

9 years ago

0.1.4

9 years ago