-
Notifications
You must be signed in to change notification settings - Fork 32
JavaScript
Inline JavaScript, which is used for framework plugins and widgets (e.g., slideshows, events, tabs, etc.), should be wrapped in the following code:
<script>
window.addEventListener('inlineJSReady', function() {
// Your JavaScript goes here.
}, false);
</script>
We suggest that you make use of the media hosting service MediaHub, which provides embed codes that integrate some very useful accessibly features such as searchable captions. Media hosted outside of MediaHub can still use the same player as follows.
To embed a video on your page so that it will work across browsers, use the mediaelement_wdn
plugin. To use this plugin, you will need to do the following:
- Embed the video on your page with either the
<audio>
or the<video>
element. - Give the
<audio>
or the<video>
element a class ofwdn_player
. - Load the
mediaelement_wdn
plugin with<script>WDN.initializePlugin('mediaelement_wdn');</script>
. Please note: You only need to run this script once per page. Putting it at the bottom of your content ensure that all players get loaded when ready.
There are many things to consider when it comes to accessibility and media.
- All media must have captions. UNL MediaHub makes this easy.
- Avoid auto-playing media: Media that automatically plays when a page is loaded can be confusing and unwanted.
- Keep flashes of light to a minimum. Excessive flashes and/or strobing effects can cause issues for people who are prone to seizures.
Media should be hosted on the unl.edu domain whenever possible (as opposed to YouTube, etc.) to ensure that it is available to all visitors. Some locations such as China or K–12 schools will block popular media hosting platforms such as YouTube or Vimeo. To ensure that everyone who visits your site can watch media, please host the content on unl.edu. An easy solution for hosting media at UNL is UNL MediaHub.
Button Toggles allow buttons to toggle other elements when clicked. This component is the base for other components. This component rely on specific CSS classes and does have CSS animations that you will be affected during the toggling.
Options | Data Attribute | Default value | Description |
---|---|---|---|
Controls | data-controls |
'' |
This will be the id of the element that we are going to toggle (required) |
Label On | data-label-on |
'' |
This will control the aria label for the button for when the element is off/collapsed, not needed if there is text inside the button |
Label Off | data-label-off |
'' |
This will control the aria label for the button for when the element is on/expanded, not needed if there is text inside the button |
Start Expanded | data-start-expanded |
'true' |
This will allow us to define the starting state of the toggle, this should be pared with the hidden attribute on the toggled element to avoid bad content paints before the javascript loads |
Events | Dispatches/Listens | Description |
---|---|---|
toggleButtonOn |
Dispatches | This event is dispatched when the button is toggled to the on/expanded state |
toggleButtonOff |
Dispatches | This event is dispatched when the button is toggled to the off/collapsed state |
toggleElementOn |
Dispatches | This event is dispatched when the element is toggled to the on/expanded state |
toggleElementOff |
Dispatches | This event is dispatched when the element is toggled to the off/collapsed state |
commandClose |
Listens | The component will listen for this event on both the button and toggled element, when the event is heard it will try to toggle the button off/collapsed |
commandOpen |
Listens | The component will listen for this event on both the button and toggled element, when the event is heard it will try to toggle the button on/expanded |
commandToggle |
Listens | The component will listen for this event on both the button and toggled element, when the event is heard it will try to toggle the button state |
Keys | Actions |
---|---|
space |
Toggles the state |
escape |
Tries to toggle off/collapsed |
These keys only work when focus is on the button
<!-- Starting Expanded Example -->
<div id="my-div">
My First Div
</div>
<button
class="dcf-btn-toggle dcf-btn dcf-btn-primary"
data-controls="my-div"
data-label-on="Open My First Div"
data-label-off="Close My First Div"
data-start-expanded="true"
>
Toggle My First Div (Starts On)
</button>
<!-- This is only required once per page with button-toggles -->
<script>
window.addEventListener('inlineJSReady', function() {
WDN.initializePlugin('button-toggles');
}, false);
</script>
<!-- Starting Collapsed Example -->
<div id="my-div-with-id" hidden>
My Second Div
</div>
<button
id="my-div-with-id-toggle-button"
class="dcf-btn-toggle dcf-btn dcf-btn-primary"
data-controls="my-div-with-id"
data-label-on="Open My Second Div"
data-label-off="Close My Second Div"
data-start-expanded="false"
>
Toggle My Second Div (Starts Off)
</button>
<!-- This is only required once per page with button-toggles -->
<script>
window.addEventListener('inlineJSReady', function() {
WDN.initializePlugin('button-toggles');
}, false);
</script>
Pulling data from the UNL Events system is available with a number of widgets, each with their own styling and interactions.
The events
plugin provides a vertically rendered list of upcoming events. The monthwidget
plugin provides a tabular month calendar rendering of events for the current month. The events-band
plugin provides a horizontal block grid of upcoming events. Each of these plugins work by providing a placeholder element on your page and adding a script to inject the content loaded asynchronously from the events system.
All of the events widgets are initialized via JavaScript with the WDN.initializePlugin('plugin-name', {configuration object})
function. A JavaScript object can be passed as the second parameter to customize the widgets.
For example, all of the widgets will use the main UNL calendar by default, but you can use your calendar by passing a url
option in the configuration object (the second parameter of the WDN.initializePlugin function)
. Note that the url
option has to be the base URL for your calendar (use https://events.unl.edu/yourcalendar/ and not https://events.unl.edu/yourcalendar/upcoming). For example:
<script>
window.addEventListener('inlineJSReady', function() {
WDN.initializePlugin('events', {url: 'https://events.unl.edu/yourcalendar/', limit: 5});
});
</script>
Options | Type | Default | Description |
---|---|---|---|
url |
String | 'https://events.unl.edu/' |
URL of the events calendar you'd like to pull from |
container |
Selector | '#monthwidget' |
A CSS selector for loading the widget into |
<div id="monthwidget"></div>
<script>
window.addEventListener('inlineJSReady', function() {
WDN.initializePlugin('monthwidget');
});
</script>
Options | Type | Default | Description |
---|---|---|---|
url |
String | 'https://events.unl.edu/' |
URL of the events calendar you'd like to pull from |
title |
String | '' |
Label for the More Events link |
container |
Selector | '#wdn_calendarDisplay' |
A CSS selector for loading the widget into |
limit |
Integer | 10 |
Maximum number of upcoming events to show |
rooms |
Boolean | false |
A flag to designate if event location rooms should be shown |
<div id="events"></div>
<script>
window.addEventListener('inlineJSReady', function() {
WDN.initializePlugin('events', {limit: 5});
});
</script>
Options | Type | Default | Description |
---|---|---|---|
url |
String | 'https://events.unl.edu/' |
URL of the events calendar you'd like to pull from |
title |
String | '' |
Label for the More Events link |
container |
Selector | '#events-band' |
A CSS selector for loading the widget into |
limit |
Integer | 10 |
Maximum number of upcoming events to show |
rooms |
Boolean | false |
A flag to designate if event location rooms should be shown |
<div id="events-band"></div>
<script>
window.addEventListener('inlineJSReady', function() {
WDN.initializePlugin('events-band', {limit: 6, rooms: true});
});
</script>
To load the optional brand serif font (Source Serif), apply the unl-font-serif
class to desired text, then add this snippet of JavaScript to either your page or your site's JavaScript:
<script>
window.addEventListener('inlineJSReady', function() {
WDN.initializePlugin('font-serif');
}, false);
</script>
Framework animations use GreenSock Animation Platform (GSAP) version 3.x. You can use framework classes for scroll animations or create your own custom animations by beginning with the following JavaScript:
<script>
window.addEventListener('DOMContentLoaded', function() {
require(['plugins/gsap/gsap.min'], function() {
// Custom GSAP goes here
});
}, false);
</script>
Pair the following JavaScript with CSS classes to animate elements on page scroll.
<script>
window.addEventListener('DOMContentLoaded', function() {
WDN.initializePlugin('scroll-animations');
}, false);
</script>
The UNLedu framework uses a tool called RequireJS to load JavaScript libraries and dependencies. This guide will walk you through some RequireJS basics, including how to load JavaScript dependencies safely. At first glance, this might seem needlessly complex, but it is actually pretty simple. RequireJS has some great documentation on how to use it. We won’t dive into the details here, only the basics.
Simply use the require
function. The first parameter should be an array of dependencies, and the second parameter should be an anonymous function to run after the dependencies have loaded. The parameters for the anonymous function are mapped to the first parameter of the require function.
require(['jquery'], function($) {
//place any code that relies on $ here
});
See the loading jQuery example for details on how the require function works
require(['absolute path to library', 'jquery'], function(loadedLib, $) {
//place any code that relies on $ and loadedLib here
});
So now that you're convinced that RequireJS is awesome, let's cover how to configure it to work with your dependencies that are not a part of the framework. By default, we set the baseUrl configuration value to the base JavaScript directory for framework bundled dependencies. It should be no surprise that this won't work for module dependencies that you want to host in your own directory structure. To work around this, you should set a paths configuration value for each local dependency.
require.config({
paths: {
"local-module-name": "url/to/local-module-name"
}
});
If your website or project has a lot of javascript dependencies, it might be worth configuring RequireJS to search certain paths on your server for the correct JavaScript file. See the RequireJS documentation for details on how to accomplish this.
While many JavaScript plugins and modules are adopting AMD loaders, there is a chance you will come across something that still uses the old way of relying on variables in the global scope. We’ve prepared a common jQuery dependency wrapper that can be put around these scripts so they work with the module loader.
header:
(function(factory) {
if (typeof define === 'function' && define.amd) {
// AMD
define(['jquery'], factory);
} else if (typeof module === 'object' && module.exports) {
factory(require('jquery'));
} else {
// Browser globals
factory(jQuery);
}
}(function(jQuery) {
footer:
}));