3 Polymer('core-iconset', {
6 * The URL of the iconset image.
15 * The width of the iconset image. This must only be specified if the
16 * icons are arranged into separate rows inside the image.
25 * A space separated list of names corresponding to icons in the iconset
26 * image file. This list must be ordered the same as the icon images
36 * The size of an individual icon. Note that icons must be square.
45 * The horizontal offset of the icon images in the inconset src image.
46 * This is typically used if the image resource contains additional images
47 * beside those intended for the iconset.
55 * The vertical offset of the icon images in the inconset src image.
56 * This is typically used if the image resource contains additional images
57 * beside those intended for the iconset.
73 // TODO(sorvell): ensure iconset's src is always relative to the main
75 if (this.src && (this.ownerDocument !== document)) {
76 this.src = this.resolvePath(this.src, this.ownerDocument.baseURI);
82 iconsChanged: function() {
83 var ox = this.offsetX;
84 var oy = this.offsetY;
85 this.icons && this.icons.split(/\s+/g).forEach(function(name, i) {
86 this.iconNames.push(name);
87 this.iconMap[name] = {
91 if (ox + this.iconSize < this.width) {
100 updateThemes: function() {
101 var ts = this.querySelectorAll('property[theme]');
102 ts && ts.array().forEach(function(t) {
103 this.themes[t.getAttribute('theme')] = {
104 offsetX: parseInt(t.getAttribute('offsetX')) || 0,
105 offsetY: parseInt(t.getAttribute('offsetY')) || 0
110 // TODO(ffu): support retrived by index e.g. getOffset(10);
112 * Returns an object containing `offsetX` and `offsetY` properties which
113 * specify the pixel locaion in the iconset's src file for the given
114 * `icon` and `theme`. It's uncommon to call this method. It is useful,
115 * for example, to manually position a css backgroundImage to the proper
116 * offset. It's more common to use the `applyIcon` method.
119 * @param {String|Number} icon The name of the icon or the index of the
120 * icon within in the icon image.
121 * @param {String} theme The name of the theme.
122 * @returns {Object} An object specifying the offset of the given icon
123 * within the icon resource file; `offsetX` is the horizontal offset and
124 * `offsetY` is the vertical offset. Both values are in pixel units.
126 getOffset: function(icon, theme) {
127 var i = this.iconMap[icon];
129 var n = this.iconNames[Number(icon)];
132 var t = this.themes[theme];
135 offsetX: i.offsetX + t.offsetX,
136 offsetY: i.offsetY + t.offsetY
143 * Applies an icon to the given element as a css background image. This
144 * method does not size the element, and it's often necessary to set
145 * the element's height and width so that the background image is visible.
148 * @param {Element} element The element to which the background is
150 * @param {String|Number} icon The name or index of the icon to apply.
151 * @param {Number} scale (optional, defaults to 1) A scaling factor
152 * with which the icon can be magnified.
153 * @return {Element} The icon element.
155 applyIcon: function(element, icon, scale) {
156 var offset = this.getOffset(icon);
158 if (element && offset) {
159 var icon = element._icon || document.createElement('div');
160 var style = icon.style;
161 style.backgroundImage = 'url(' + this.src + ')';
162 style.backgroundPosition = (-offset.offsetX * scale + 'px') +
163 ' ' + (-offset.offsetY * scale + 'px');
164 style.backgroundSize = scale === 1 ? 'auto' :
165 this.width * scale + 'px';
166 if (icon.parentNode !== element) {
167 element.appendChild(icon);