3
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
4
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
5
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
6
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
7
Code distributed by Google as part of the polymer project is also
8
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
11
`<iron-label>` provides a version of the `<label>` element that works with Custom Elements as well as native elements.
13
All text in the `iron-label` will be applied to the target element as a screen-reader accessible description.
15
There are three ways to use `iron-label` to target an element:
17
1. place an element inside iron-label and some text:
21
<paper-button>button</paper-button>
24
2. place some elements inside iron-label and target one with the `iron-label-target` attribute.
25
The other elements will provide the label for that element
26
Note: This is not necessary if the element you want to label is the first
27
element child of iron-label:
30
<span>Label for the Button</span>
31
<paper-button iron-label-target>button</paper-button>
35
<paper-button>button</paper-button>
36
<span>Label for the Button</span>
39
3. Set the `for` attribute on the `iron-label` element with the id of the target
40
element in the same document or ShadowRoot:
42
<iron-label for="foo">
43
Context for the button with the "foo" class"
45
<paper-button id="foo">Far away button</paper-button>
47
All taps on the `iron-label` will be forwarded to the "target" element.
52
@homepage polymer.github.io
54
<link rel="import" href="../polymer/polymer.html">
56
<dom-module id="iron-label">
58
Polymer.IronLabel = Polymer({
67
* An ID reference to another element that needs to be
68
* labelled by this `iron-label` element.
73
reflectToAttribute: true,
74
observer: '_forChanged'
82
attached: function() {
87
this._generateLabelId();
90
// generate a unique id for this element
91
_generateLabelId: function() {
93
var id = 'iron-label-' + Polymer.IronLabel._labelNumber++;
94
Polymer.dom(this).setAttribute('id', id);
98
_findTarget: function() {
101
var scope = Polymer.dom(this).getOwnerRoot();
102
return Polymer.dom(scope).querySelector('#' + this.for);
104
// explicit internal target
105
var el = Polymer.dom(this).querySelector('[iron-label-target]');
107
// implicit internal target
108
el = Polymer.dom(this).firstElementChild;
114
_tapHandler: function(ev) {
115
if (!this._forElement) {
118
var target = Polymer.dom(ev).localTarget;
119
if (target === this._forElement) {
122
this._forElement.focus();
123
this._forElement.click();
126
_applyLabelledBy: function() {
127
if (this._forElement) {
128
Polymer.dom(this._forElement).setAttribute('aria-labelledby', this.id);
132
_forChanged: function() {
133
if (this._forElement) {
134
Polymer.dom(this._forElement).removeAttribute('aria-labelledby');
136
this._forElement = this._findTarget();
137
this._applyLabelledBy();
141
// global counter for unique label ids
142
Polymer.IronLabel._labelNumber = 0;