Skip to content

Commit

Permalink
Added initial files from consolidated repo
Browse files Browse the repository at this point in the history
  • Loading branch information
robbear committed May 8, 2015
1 parent 73a9eb4 commit 30d1887
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 2 deletions.
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2015 Component Kitchen, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# basic-keyboard
Aspect which manages the keyboard focus and keydown handling for a component collective. This aspect ensures that its only the outermost aspect in a collective that is listening for keyboard events.
The organization of these web component files is tuned for the component's
inclusion in other projects via [Bower](http://bower.io). If you'd like to run
this component's demo on your own machine, please see these
[instructions](https://github.com/basic-web-components/components-dev/wiki/Running-Basic-Web-Component-demos).
92 changes: 92 additions & 0 deletions basic-keyboard.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<!--
Aspect which manages the keyboard focus and keydown handling for a component
collective.
This aspect ensures that its only the outermost aspect in a collective that is
listening for keyboard events.
@element basic-keyboard
-->

<link rel="import" href="../basic-aspect/basic-aspect.html">

<script>
Polymer(mixin(BasicAspectMixin, {

contribute: {

/*
* When the collective changes, stop listening for keyboard events on
* whichever aspect was previously the outermost aspect, and start listening
* to keyboard events on whichever aspect is now the new outermost aspect.
*/
// TODO: Do we need to start/stop listening when attached/detached, or is
// that handled automatically?
collectiveChanged: function() {

var outermost = this.collective.outermostAttached;
if (outermost === this._previousOutermostAspect) {
// Should already be listening to events on the outermost aspect.
return;
}

if (this._previousOutermostAspect) {
// Clean up the previous aspect that was handling the keyboard.

if (this._previousTabIndex) {
// Restore previous tab index.
this._previousOutermostAspect.setAttribute('tabIndex', this._previousTabIndex);
} else {
// Aspect didn't have a tab index before, so remove it.
this._previousOutermostAspect.removeAttribute('tabIndex');
}

// Stop listening to events the previous outermost aspect.
this._previousOutermostAspect.removeEventListener('keydown', this._keydownHandler);
}

if (outermost.getAttribute('tabIndex')) {
// Leave existing tab index in place.
this._previousTabIndex = null;
} else {
// Make new outermost aspect focusable.
this._previousTabIndex = outermost.getAttribute('tabIndex');
outermost.setAttribute('tabIndex', 0);
}

// Start listening to events on the new outermost aspect.
if (!this._keydownHandler) {
this._keydownHandler = this._keydown.bind(this);
}
outermost.addEventListener('keydown', this._keydownHandler);

this._previousOutermostAspect = outermost;
}

},

is: 'basic-keyboard',

// Default keydown handler will be overwritten by collective method.
keydown: function(event) {},

// ready: function() {
// // TODO: Uncomment this when we inherit again.
// // this.super();
// this.addEventListener('click', function() {
// this.focus();
// });
// },

_keydown: function(event) {
return this.keydown(event);
},

_keydownHandler: null,

_previousOutermostAspect: null,

_previousTabIndex: null

}));
</script>
24 changes: 24 additions & 0 deletions bower.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "basic-keyboard",
"description": "Aspect which manages the keyboard focus and keydown handling for a component collective. This aspect ensures that its only the outermost aspect in a collective that is listening for keyboard events.",
"version": "0.6.0-preview",
"license": "MIT",
"main": "basic-keyboard.html",
"dependencies": {
"basic-aspect": "basic-web-components/basic-aspect#master",
"iron-icons": "PolymerElements/iron-icons",
"polymer": "Polymer/polymer#^0.8.0-rc.7"
},
"devDependencies": {
"basic-framed-content": "basic-web-components/basic-framed-content#master",
"globalize": "0.x",
"web-component-tester": "*"
},
"keywords": [
"basic-web-components"
],
"ignore": [
"**/.*",
"/test/"
]
}
51 changes: 51 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<!DOCTYPE html>
<html>
<head>

<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>basic-keyboard</title>

<script src="../bower_components/webcomponentsjs/webcomponents-lite.js"></script>
<link rel="import" href="../basic-aspect/basic-aspect.html">
<link rel="import" href="basic-keyboard.html">

<style>
body,
button,
input {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 15px;
}

body {
margin: 0;
padding: 2em; /* Until we use basic-framed-content again */
}
</style>

<script>
var keyboardLogAspect = {
contribute: {
keydown: function(event) {
console.log("keydown " + event.which);
}
}
};

document.addEventListener('WebComponentsReady', function() {
new Collective(keyboardLogAspect, document.querySelector('basic-aspect'));
});
</script>

</head>

<body>
<basic-aspect id="outer" target="child">
<basic-keyboard id="inner">
This element handles keyboard events.
</basic-keyboard>
</basic-aspect>
</body>

</html>

0 comments on commit 30d1887

Please sign in to comment.