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