OLD | NEW |
1 | 1 |
2 | 2 /** |
| 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 |
| 5 * `iron-iconset-svg` element. Multiple icons should be given distinct id's. |
| 6 * |
| 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 |
| 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. |
| 11 * |
| 12 * Example: |
| 13 * |
| 14 * <iron-iconset-svg id="my-svg-icons" iconSize="24"> |
| 15 * <svg> |
| 16 * <defs> |
| 17 * <g id="shape"> |
| 18 * <rect x="50" y="50" width="50" height="50" /> |
| 19 * <circle cx="50" cy="50" r="50" /> |
| 20 * </g> |
| 21 * </defs> |
| 22 * </svg> |
| 23 * </iron-iconset-svg> |
| 24 * |
| 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 |
| 27 * `iron-iconset` element and call the `byId` method |
| 28 * to retrieve a given iconset. To apply a particular icon inside an |
| 29 * element use the `applyIcon` method. For example: |
| 30 * |
| 31 * iconset.applyIcon(iconNode, 'car'); |
| 32 * |
| 33 * @element iron-iconset-svg |
| 34 */ |
3 Polymer({ | 35 Polymer({ |
4 | 36 |
5 is: 'iron-iconset-svg', | 37 is: 'iron-iconset-svg', |
6 | 38 |
7 properties: { | 39 properties: { |
8 | 40 |
9 /** | 41 /** |
10 * The name of the iconset. | 42 * The name of the iconset. |
11 * | 43 * |
12 * @attribute name | 44 * @attribute name |
13 * @type string | 45 * @type string |
14 * @default '' | |
15 */ | 46 */ |
16 name: { | 47 name: { |
17 type: String, | 48 type: String, |
18 observer: '_nameChanged' | 49 observer: '_nameChanged' |
19 }, | 50 }, |
20 | 51 |
21 /** | 52 /** |
22 * Array of fully-qualitifed icon names in the iconset. | 53 * Array of fully-qualitifed icon names in the iconset. |
23 */ | 54 */ |
24 iconNames: { | 55 iconNames: { |
(...skipping 16 matching lines...) Expand all Loading... |
41 }, | 72 }, |
42 | 73 |
43 /** | 74 /** |
44 * Applies an icon to the given element. | 75 * Applies an icon to the given element. |
45 * | 76 * |
46 * An svg icon is prepended to the element's shadowRoot if it exists, | 77 * An svg icon is prepended to the element's shadowRoot if it exists, |
47 * otherwise to the element itself. | 78 * otherwise to the element itself. |
48 * | 79 * |
49 * @method applyIcon | 80 * @method applyIcon |
50 * @param {Element} element Element to which the icon is applied. | 81 * @param {Element} element Element to which the icon is applied. |
51 * @param {String} icon Name of the icon to apply. | 82 * @param {string} iconName Name of the icon to apply. |
52 * @return {Element} The svg element which renders the icon. | 83 * @return {Element} The svg element which renders the icon. |
53 */ | 84 */ |
54 applyIcon: function(element, iconName) { | 85 applyIcon: function(element, iconName) { |
55 // insert svg element into shadow root, if it exists | 86 // insert svg element into shadow root, if it exists |
56 element = element.root || element; | 87 element = element.root || element; |
57 // Remove old svg element | 88 // Remove old svg element |
58 this.removeIcon(element); | 89 this.removeIcon(element); |
59 // install new svg element | 90 // install new svg element |
60 var svg = this._cloneIcon(iconName); | 91 var svg = this._cloneIcon(iconName); |
61 if (svg) { | 92 if (svg) { |
62 // TODO(sjmiles): I know, `with` is the devil ... except it isn't | 93 var pde = Polymer.dom(element); |
63 with (Polymer.dom(element)) { | 94 pde.insertBefore(svg, pde.childNodes[0]); |
64 insertBefore(svg, childNodes[0]); | |
65 } | |
66 return element._svgIcon = svg; | 95 return element._svgIcon = svg; |
67 } | 96 } |
| 97 return null; |
68 }, | 98 }, |
69 | 99 |
70 /** | 100 /** |
71 * Remove an icon from the given element by undoing the changes effected | 101 * Remove an icon from the given element by undoing the changes effected |
72 * by `applyIcon`. | 102 * by `applyIcon`. |
73 * | 103 * |
74 * @param {Element} element The element from which the icon is removed. | 104 * @param {Element} element The element from which the icon is removed. |
75 */ | 105 */ |
76 removeIcon: function(element) { | 106 removeIcon: function(element) { |
77 // Remove old svg element | 107 // Remove old svg element |
(...skipping 12 matching lines...) Expand all Loading... |
90 _nameChanged: function() { | 120 _nameChanged: function() { |
91 new Polymer.IronMeta({type: 'iconset', key: this.name, value: this}); | 121 new Polymer.IronMeta({type: 'iconset', key: this.name, value: this}); |
92 // icons (descendents) must exist a-priori | 122 // icons (descendents) must exist a-priori |
93 this._icons = this._createIconMap(); | 123 this._icons = this._createIconMap(); |
94 this.iconNames = this._getIconNames(); | 124 this.iconNames = this._getIconNames(); |
95 }, | 125 }, |
96 | 126 |
97 /** | 127 /** |
98 * Array of all icon names in this iconset. | 128 * Array of all icon names in this iconset. |
99 * | 129 * |
100 * @return {Array} Array of icon names. | 130 * @return {!Array} Array of icon names. |
101 */ | 131 */ |
102 _getIconNames: function() { | 132 _getIconNames: function() { |
103 return Object.keys(this._icons).map(function(n) { | 133 return Object.keys(this._icons).map(function(n) { |
104 return this.name + ':' + n; | 134 return this.name + ':' + n; |
105 }, this); | 135 }, this); |
106 }, | 136 }, |
107 | 137 |
108 /** | 138 /** |
109 * Create a map of child SVG elements by id. | 139 * Create a map of child SVG elements by id. |
110 * | 140 * |
111 * @return {Object} Map of id's to SVG elements. | 141 * @return {Object} Map of id's to SVG elements. |
112 */ | 142 */ |
113 _createIconMap: function() { | 143 _createIconMap: function() { |
114 // Objects chained to Object.prototype (`{}`) have members. Specifically, | 144 // Objects chained to Object.prototype (`{}`) have members. Specifically, |
115 // on FF there is a `watch` method that confuses the icon map, so we | 145 // on FF there is a `watch` method that confuses the icon map, so we |
116 // need to use a null-based object here. | 146 // need to use a null-based object here. |
117 var icons = Object.create(null); | 147 var icons = Object.create(null); |
118 Polymer.dom(this).querySelectorAll('[id]') | 148 Polymer.dom(this).querySelectorAll('[id]') |
119 .forEach(function(icon) { | 149 .forEach(function(icon) { |
120 icons[icon.id] = icon; | 150 icons[icon.id] = icon; |
121 }); | 151 }); |
122 return icons; | 152 return icons; |
123 }, | 153 }, |
124 | 154 |
125 /** | 155 /** |
126 * Produce installable clone of the SVG element matching `id` in this | 156 * Produce installable clone of the SVG element matching `id` in this |
127 * iconset, or `undefined` if there is no matching element. | 157 * iconset, or `undefined` if there is no matching element. |
128 * | 158 * |
129 * @return {Object} Returns an installable clone of the SVG element | 159 * @return {Element} Returns an installable clone of the SVG element |
130 * matching `id`. | 160 * matching `id`. |
131 */ | 161 */ |
132 _cloneIcon: function(id) { | 162 _cloneIcon: function(id) { |
133 return this._prepareSvgClone(this._icons[id], this.size); | 163 return this._prepareSvgClone(this._icons[id], this.size); |
134 }, | 164 }, |
135 | 165 |
| 166 /** |
| 167 * @param {Element} sourceSvg |
| 168 * @param {number} size |
| 169 * @return {Element} |
| 170 */ |
136 _prepareSvgClone: function(sourceSvg, size) { | 171 _prepareSvgClone: function(sourceSvg, size) { |
137 if (sourceSvg) { | 172 if (sourceSvg) { |
138 var svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg'); | 173 var svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg'); |
139 svg.setAttribute('viewBox', ['0', '0', size, size].join(' ')); | 174 svg.setAttribute('viewBox', ['0', '0', size, size].join(' ')); |
140 svg.setAttribute('preserveAspectRatio', 'xMidYMid meet'); | 175 svg.setAttribute('preserveAspectRatio', 'xMidYMid meet'); |
141 // TODO(dfreedm): `pointer-events: none` works around https://crbug.com/
370136 | 176 // TODO(dfreedm): `pointer-events: none` works around https://crbug.com/
370136 |
142 // TODO(sjmiles): inline style may not be ideal, but avoids requiring a
shadow-root | 177 // TODO(sjmiles): inline style may not be ideal, but avoids requiring a
shadow-root |
143 svg.style.cssText = 'pointer-events: none; display: block; width: 100%;
height: 100%;'; | 178 svg.style.cssText = 'pointer-events: none; display: block; width: 100%;
height: 100%;'; |
144 svg.appendChild(sourceSvg.cloneNode(true)).removeAttribute('id'); | 179 svg.appendChild(sourceSvg.cloneNode(true)).removeAttribute('id'); |
145 return svg; | 180 return svg; |
146 } | 181 } |
| 182 return null; |
147 } | 183 } |
148 | 184 |
149 }); | 185 }); |
OLD | NEW |