2 Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
3 This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
4 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
5 The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
6 Code distributed by Google as part of the polymer project is also
7 subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
12 * @group Polymer Core Elements
14 * The `core-iconset` element allows users to define their own icon sets.
15 * The `src` property specifies the url of the icon image. Multiple icons may
16 * be included in this image and they may be organized into rows.
17 * The `icons` property is a space separated list of names corresponding to the
18 * icons. The names must be ordered as the icons are ordered in the icon image.
19 * Icons are expected to be square and are the size specified by the `iconSize`
20 * property. The `width` property corresponds to the width of the icon image
21 * and must be specified if icons are arranged into multiple rows in the image.
23 * All `core-iconset` elements are available for use by other `core-iconset`
24 * elements via a database keyed by id. Typically, an element author that wants
25 * to support a set of custom icons uses a `core-iconset` to retrieve
26 * and use another, user-defined iconset.
30 * <core-iconset id="my-icons" src="my-icons.png" width="96" iconSize="24"
31 * icons="location place starta stopb bus car train walk">
34 * This will automatically register the icon set "my-icons" to the iconset
35 * database. To use these icons from within another element, make a
36 * `core-iconset` element and call the `byId` method to retrieve a
37 * given iconset. To apply a particular icon to an element, use the
38 * `applyIcon` method. For example:
40 * iconset.applyIcon(iconNode, 'car');
42 * Themed icon sets are also supported. The `core-iconset` can contain child
43 * `property` elements that specify a theme with an offsetX and offsetY of the
44 * theme within the icon resource. For example.
46 * <core-iconset id="my-icons" src="my-icons.png" width="96" iconSize="24"
47 * icons="location place starta stopb bus car train walk">
48 * <property theme="special" offsetX="256" offsetY="24"></property>
51 * Then a themed icon can be applied like this:
53 * iconset.applyIcon(iconNode, 'car', 'special');
55 * @element core-iconset
61 <link rel=
"import" href=
"../core-meta/core-meta.html">
63 <polymer-element name=
"core-iconset" extends=
"core-meta" attributes=
"src width icons iconSize">
67 Polymer('core-iconset', {
70 * The URL of the iconset image.
79 * The width of the iconset image. This must only be specified if the
80 * icons are arranged into separate rows inside the image.
89 * A space separated list of names corresponding to icons in the iconset
90 * image file. This list must be ordered the same as the icon images
100 * The size of an individual icon. Note that icons must be square.
102 * @attribute iconSize
109 * The horizontal offset of the icon images in the inconset src image.
110 * This is typically used if the image resource contains additional images
111 * beside those intended for the iconset.
119 * The vertical offset of the icon images in the inconset src image.
120 * This is typically used if the image resource contains additional images
121 * beside those intended for the iconset.
130 created: function() {
137 // TODO(sorvell): ensure iconset's src is always relative to the main
139 if (this.src
&& (this.ownerDocument
!== document
)) {
140 this.src
= this.resolvePath(this.src
, this.ownerDocument
.baseURI
);
146 iconsChanged: function() {
147 var ox
= this.offsetX
;
148 var oy
= this.offsetY
;
149 this.icons
&& this.icons
.split(/\s+/g).forEach(function(name
, i
) {
150 this.iconNames
.push(name
);
151 this.iconMap
[name
] = {
155 if (ox
+ this.iconSize
< this.width
) {
164 updateThemes: function() {
165 var ts
= this.querySelectorAll('property[theme]');
166 ts
&& ts
.array().forEach(function(t
) {
167 this.themes
[t
.getAttribute('theme')] = {
168 offsetX
: parseInt(t
.getAttribute('offsetX')) || 0,
169 offsetY
: parseInt(t
.getAttribute('offsetY')) || 0
174 // TODO(ffu): support retrived by index e.g. getOffset(10);
176 * Returns an object containing `offsetX` and `offsetY` properties which
177 * specify the pixel locaion in the iconset's src file for the given
178 * `icon` and `theme`. It's uncommon to call this method. It is useful,
179 * for example, to manually position a css backgroundImage to the proper
180 * offset. It's more common to use the `applyIcon` method.
183 * @param {String|Number} icon The name of the icon or the index of the
184 * icon within in the icon image.
185 * @param {String} theme The name of the theme.
186 * @returns {Object} An object specifying the offset of the given icon
187 * within the icon resource file; `offsetX` is the horizontal offset and
188 * `offsetY` is the vertical offset. Both values are in pixel units.
190 getOffset: function(icon
, theme
) {
191 var i
= this.iconMap
[icon
];
193 var n
= this.iconNames
[Number(icon
)];
196 var t
= this.themes
[theme
];
199 offsetX
: i
.offsetX
+ t
.offsetX
,
200 offsetY
: i
.offsetY
+ t
.offsetY
207 * Applies an icon to the given element as a css background image. This
208 * method does not size the element, and it's often necessary to set
209 * the element's height and width so that the background image is visible.
212 * @param {Element} element The element to which the background is
214 * @param {String|Number} icon The name or index of the icon to apply.
215 * @param {Number} scale (optional, defaults to 1) A scaling factor
216 * with which the icon can be magnified.
217 * @return {Element} The icon element.
219 applyIcon: function(element
, icon
, scale
) {
220 var offset
= this.getOffset(icon
);
222 if (element
&& offset
) {
223 var icon
= element
._icon
|| document
.createElement('div');
224 var style
= icon
.style
;
225 style
.backgroundImage
= 'url(' + this.src
+ ')';
226 style
.backgroundPosition
= (-offset
.offsetX
* scale
+ 'px') +
227 ' ' + (-offset
.offsetY
* scale
+ 'px');
228 style
.backgroundSize
= scale
=== 1 ? 'auto' :
229 this.width
* scale
+ 'px';
230 if (icon
.parentNode
!== element
) {
231 element
.appendChild(icon
);