-
Notifications
You must be signed in to change notification settings - Fork 9
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 #17 from Brightspace/dbatiste/focus-behavior
Dbatiste/focus behavior
- Loading branch information
Showing
9 changed files
with
191 additions
and
31 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 |
---|---|---|
|
@@ -2,3 +2,4 @@ bower_components | |
bower_components-1.x | ||
bower-1.x.json | ||
node_modules/ | ||
package-lock.json |
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,22 @@ | ||
<link rel="import" href="../polymer/polymer.html"> | ||
<script> | ||
window.D2L = window.D2L || {}; | ||
window.D2L.PolymerBehaviors = window.D2L.PolymerBehaviors || {}; | ||
|
||
/** @polymerBehavior */ | ||
D2L.PolymerBehaviors.FocusableBehavior = { | ||
|
||
/** | ||
* Applies focus to descendent with `d2l-focusable` class. | ||
*/ | ||
focus: function() { | ||
// Note: focus event will not be triggered with using Polymer 1 + shady. If necessary, | ||
// we can raise a custom event. For now, keeping this bare-bones. | ||
var elem = Polymer.dom(this.root).querySelector('.d2l-focusable'); | ||
if (!elem) return; | ||
elem.focus(); | ||
} | ||
|
||
}; | ||
|
||
</script> |
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,23 @@ | ||
<link rel="import" href="../../../polymer/polymer.html"> | ||
<link rel="import" href="../../d2l-focusable-behavior.html"> | ||
|
||
<dom-module id="d2l-focusable-component"> | ||
<template> | ||
<style> | ||
:host, a { | ||
display: block; | ||
} | ||
</style> | ||
<div> | ||
<a id="shadow1" href="javascript:void(0);" tabindex="0">Shadow 1</a> | ||
<slot></slot> | ||
<a id="shadow2" class="d2l-focusable" href="javascript:void(0);" tabindex="0">Shadow 2 (d2l-focusable)</a> | ||
</div> | ||
</template> | ||
<script> | ||
Polymer({ | ||
is: 'd2l-focusable-component', | ||
behaviors: [D2L.PolymerBehaviors.FocusableBehavior] | ||
}); | ||
</script> | ||
</dom-module> |
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,27 @@ | ||
<html> | ||
<head> | ||
<title>D2L Focusable Behavior</title> | ||
<script src="../../../webcomponentsjs/webcomponents-lite.js"></script> | ||
<link rel="stylesheet" href="../demo-styles.css"> | ||
<link rel="import" href="../../../polymer/polymer.html"> | ||
<link rel="import" href="d2l-focusable-component.html"> | ||
<style> | ||
a { | ||
display: block; | ||
} | ||
</style> | ||
</head> | ||
<body unresolved> | ||
|
||
<h1>d2l-focusable-behavior</h1> | ||
|
||
<h2>Component</h2> | ||
|
||
<button onclick="document.body.querySelector('d2l-focusable-component').focus();">Focus Component</button> | ||
|
||
<d2l-focusable-component id="wc1"> | ||
<a href="http://www.nasa.gov/topics/solarsystem/index.html">Solar System and Beyond (L2)</a> | ||
</d2l-focusable-component> | ||
|
||
</body> | ||
</html> |
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,91 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>d2l-focusable-behavior tests</title> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0"> | ||
<script src="../../webcomponentsjs/webcomponents-lite.js"></script> | ||
<script src="../../web-component-tester/browser.js"></script> | ||
<link rel="import" href="../../polymer/polymer.html"> | ||
<link rel="import" href="../d2l-focusable-behavior.html"> | ||
<link rel="import" href="../d2l-dom-focus.html"> | ||
</head> | ||
<body> | ||
|
||
<dom-module id="d2l-focusable-test"> | ||
<template> | ||
<div> | ||
<div> | ||
<a id="shadow1" href="javascript:void(0);"></a> | ||
</div> | ||
<div> | ||
<slot></slot> | ||
</div> | ||
<div> | ||
<a id="shadow2" class="d2l-focusable" href="javascript:void(0);"></a> | ||
</div> | ||
</div> | ||
</template> | ||
<script> | ||
Polymer({ | ||
is: 'd2l-focusable-test', | ||
behaviors: [D2L.PolymerBehaviors.FocusableBehavior], | ||
getShadow1: function() { return this.$.shadow1; }, | ||
getShadow2: function() { return this.$.shadow2; } | ||
}); | ||
</script> | ||
</dom-module> | ||
|
||
<test-fixture id="simpleFixture"> | ||
<template> | ||
<d2l-focusable-test id="wc1"> | ||
<a id="light1" class="d2l-focusable" href="javascript:void(0);"></a> | ||
</d2l-focusable-test> | ||
</template> | ||
</test-fixture> | ||
|
||
<script> | ||
|
||
describe('d2l-focusable-behavior', function() { | ||
|
||
var simpleFixture; | ||
|
||
beforeEach(function() { | ||
simpleFixture = fixture('simpleFixture'); | ||
}); | ||
|
||
it('does not initially have focus applied to the d2l-focusable element', function() { | ||
expect(document.activeElement).to.equal(document.body); | ||
}); | ||
|
||
it('focuses on element with d2l-focusable when focus method is called', function() { | ||
simpleFixture.focus(); | ||
expect(D2L.Dom.Focus.getComposedActiveElement()).to.equal(simpleFixture.getShadow2()); | ||
}); | ||
|
||
it('triggers the blur event when focus method is called', function(done) { | ||
var startingElement = simpleFixture.querySelector('#light1'); | ||
startingElement.focus(); | ||
startingElement.addEventListener('blur', function() { | ||
done(); | ||
}); | ||
simpleFixture.focus(); | ||
}); | ||
|
||
it('triggers the focus event when focus method is called', function(done) { | ||
if (!Polymer.Element && !Polymer.Settings.useShadow) { | ||
// Note: focus event will not be triggered with using Polymer 1 + shady. If necessary, | ||
// we can raise a custom event. For now, keeping this bare-bones. | ||
done(); | ||
} | ||
simpleFixture.addEventListener('focus', function() { | ||
done(); | ||
}); | ||
simpleFixture.focus(); | ||
}); | ||
|
||
}); | ||
|
||
</script> | ||
</body> | ||
</html> |
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