| OLD | NEW |
| (Empty) |
| 1 <!-- | |
| 2 @license | |
| 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.g
ithub.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/CONTRI
BUTORS.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/PATEN
TS.txt | |
| 9 --> | |
| 10 | |
| 11 <link rel="import" href="../polymer/polymer.html"> | |
| 12 <link rel="import" href="../iron-meta/iron-meta.html"> | |
| 13 | |
| 14 <script> | |
| 15 /** | |
| 16 * The `iron-iconset-svg` element allows users to define their own icon sets | |
| 17 * that contain svg icons. The svg icon elements should be children of the | |
| 18 * `iron-iconset-svg` element. Multiple icons should be given distinct id's. | |
| 19 * | |
| 20 * Using svg elements to create icons has a few advantages over traditional | |
| 21 * bitmap graphics like jpg or png. Icons that use svg are vector based so the
y | |
| 22 * are resolution independent and should look good on any device. They are | |
| 23 * stylable via css. Icons can be themed, colorized, and even animated. | |
| 24 * | |
| 25 * Example: | |
| 26 * | |
| 27 * <iron-iconset-svg id="my-svg-icons" iconSize="24"> | |
| 28 * <svg> | |
| 29 * <defs> | |
| 30 * <g id="shape"> | |
| 31 * <rect x="50" y="50" width="50" height="50" /> | |
| 32 * <circle cx="50" cy="50" r="50" /> | |
| 33 * </g> | |
| 34 * </defs> | |
| 35 * </svg> | |
| 36 * </iron-iconset-svg> | |
| 37 * | |
| 38 * This will automatically register the icon set "my-svg-icons" to the iconset | |
| 39 * database. To use these icons from within another element, make a | |
| 40 * `iron-iconset` element and call the `byId` method | |
| 41 * to retrieve a given iconset. To apply a particular icon inside an | |
| 42 * element use the `applyIcon` method. For example: | |
| 43 * | |
| 44 * iconset.applyIcon(iconNode, 'car'); | |
| 45 * | |
| 46 * @element iron-iconset-svg | |
| 47 */ | |
| 48 Polymer({ | |
| 49 | |
| 50 is: 'iron-iconset-svg', | |
| 51 | |
| 52 properties: { | |
| 53 | |
| 54 /** | |
| 55 * The name of the iconset. | |
| 56 * | |
| 57 * @attribute name | |
| 58 * @type string | |
| 59 */ | |
| 60 name: { | |
| 61 type: String, | |
| 62 observer: '_nameChanged' | |
| 63 }, | |
| 64 | |
| 65 /** | |
| 66 * Array of fully-qualitifed icon names in the iconset. | |
| 67 */ | |
| 68 iconNames: { | |
| 69 type: Array, | |
| 70 notify: true | |
| 71 }, | |
| 72 | |
| 73 /** | |
| 74 * The size of an individual icon. Note that icons must be square. | |
| 75 * | |
| 76 * @attribute iconSize | |
| 77 * @type number | |
| 78 * @default 24 | |
| 79 */ | |
| 80 size: { | |
| 81 type: Number, | |
| 82 value: 24 | |
| 83 } | |
| 84 | |
| 85 }, | |
| 86 | |
| 87 /** | |
| 88 * Applies an icon to the given element. | |
| 89 * | |
| 90 * An svg icon is prepended to the element's shadowRoot if it exists, | |
| 91 * otherwise to the element itself. | |
| 92 * | |
| 93 * @method applyIcon | |
| 94 * @param {Element} element Element to which the icon is applied. | |
| 95 * @param {string} iconName Name of the icon to apply. | |
| 96 * @return {Element} The svg element which renders the icon. | |
| 97 */ | |
| 98 applyIcon: function(element, iconName) { | |
| 99 // insert svg element into shadow root, if it exists | |
| 100 element = element.root || element; | |
| 101 // Remove old svg element | |
| 102 this.removeIcon(element); | |
| 103 // install new svg element | |
| 104 var svg = this._cloneIcon(iconName); | |
| 105 if (svg) { | |
| 106 var pde = Polymer.dom(element); | |
| 107 pde.insertBefore(svg, pde.childNodes[0]); | |
| 108 return element._svgIcon = svg; | |
| 109 } | |
| 110 return null; | |
| 111 }, | |
| 112 | |
| 113 /** | |
| 114 * Remove an icon from the given element by undoing the changes effected | |
| 115 * by `applyIcon`. | |
| 116 * | |
| 117 * @param {Element} element The element from which the icon is removed. | |
| 118 */ | |
| 119 removeIcon: function(element) { | |
| 120 // Remove old svg element | |
| 121 if (element._svgIcon) { | |
| 122 Polymer.dom(element).removeChild(element._svgIcon); | |
| 123 element._svgIcon = null; | |
| 124 } | |
| 125 }, | |
| 126 | |
| 127 /** | |
| 128 * | |
| 129 * When name is changed, either register a new iconset with the included | |
| 130 * icons, or if there are no children, set up a meta-iconset. | |
| 131 * | |
| 132 */ | |
| 133 _nameChanged: function() { | |
| 134 new Polymer.IronMeta({type: 'iconset', key: this.name, value: this}); | |
| 135 // icons (descendents) must exist a-priori | |
| 136 this._icons = this._createIconMap(); | |
| 137 this.iconNames = this._getIconNames(); | |
| 138 }, | |
| 139 | |
| 140 /** | |
| 141 * Array of all icon names in this iconset. | |
| 142 * | |
| 143 * @return {!Array} Array of icon names. | |
| 144 */ | |
| 145 _getIconNames: function() { | |
| 146 return Object.keys(this._icons).map(function(n) { | |
| 147 return this.name + ':' + n; | |
| 148 }, this); | |
| 149 }, | |
| 150 | |
| 151 /** | |
| 152 * Create a map of child SVG elements by id. | |
| 153 * | |
| 154 * @return {Object} Map of id's to SVG elements. | |
| 155 */ | |
| 156 _createIconMap: function() { | |
| 157 // Objects chained to Object.prototype (`{}`) have members. Specifically, | |
| 158 // on FF there is a `watch` method that confuses the icon map, so we | |
| 159 // need to use a null-based object here. | |
| 160 var icons = Object.create(null); | |
| 161 Polymer.dom(this).querySelectorAll('[id]') | |
| 162 .forEach(function(icon) { | |
| 163 icons[icon.id] = icon; | |
| 164 }); | |
| 165 return icons; | |
| 166 }, | |
| 167 | |
| 168 /** | |
| 169 * Produce installable clone of the SVG element matching `id` in this | |
| 170 * iconset, or `undefined` if there is no matching element. | |
| 171 * | |
| 172 * @return {Element} Returns an installable clone of the SVG element | |
| 173 * matching `id`. | |
| 174 */ | |
| 175 _cloneIcon: function(id) { | |
| 176 return this._prepareSvgClone(this._icons[id], this.size); | |
| 177 }, | |
| 178 | |
| 179 /** | |
| 180 * @param {Element} sourceSvg | |
| 181 * @param {number} size | |
| 182 * @return {Element} | |
| 183 */ | |
| 184 _prepareSvgClone: function(sourceSvg, size) { | |
| 185 if (sourceSvg) { | |
| 186 var svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg'); | |
| 187 svg.setAttribute('viewBox', ['0', '0', size, size].join(' ')); | |
| 188 svg.setAttribute('preserveAspectRatio', 'xMidYMid meet'); | |
| 189 // TODO(dfreedm): `pointer-events: none` works around https://crbug.com/
370136 | |
| 190 // TODO(sjmiles): inline style may not be ideal, but avoids requiring a
shadow-root | |
| 191 svg.style.cssText = 'pointer-events: none; display: block; width: 100%;
height: 100%;'; | |
| 192 svg.appendChild(sourceSvg.cloneNode(true)).removeAttribute('id'); | |
| 193 return svg; | |
| 194 } | |
| 195 return null; | |
| 196 } | |
| 197 | |
| 198 }); | |
| 199 </script> | |
| OLD | NEW |