icon.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  1. /*!
  2. * AngularJS Material Design
  3. * https://github.com/angular/material
  4. * @license MIT
  5. * v1.1.8-master-aba7b2b
  6. */
  7. goog.provide('ngmaterial.components.icon');
  8. goog.require('ngmaterial.core');
  9. /**
  10. * @ngdoc module
  11. * @name material.components.icon
  12. * @description
  13. * Icon
  14. */
  15. angular.module('material.components.icon', ['material.core']);
  16. angular
  17. .module('material.components.icon')
  18. .directive('mdIcon', ['$mdIcon', '$mdTheming', '$mdAria', '$sce', mdIconDirective]);
  19. /**
  20. * @ngdoc directive
  21. * @name mdIcon
  22. * @module material.components.icon
  23. *
  24. * @restrict E
  25. *
  26. * @description
  27. * The `md-icon` directive makes it easier to use vector-based icons in your app (as opposed to
  28. * raster-based icons types like PNG). The directive supports both icon fonts and SVG icons.
  29. *
  30. * Icons should be considered view-only elements that should not be used directly as buttons; instead nest a `<md-icon>`
  31. * inside a `md-button` to add hover and click features.
  32. *
  33. * ### Icon fonts
  34. * Icon fonts are a technique in which you use a font where the glyphs in the font are
  35. * your icons instead of text. Benefits include a straightforward way to bundle everything into a
  36. * single HTTP request, simple scaling, easy color changing, and more.
  37. *
  38. * `md-icon` lets you consume an icon font by letting you reference specific icons in that font
  39. * by name rather than character code.
  40. *
  41. * ### SVG
  42. * For SVGs, the problem with using `<img>` or a CSS `background-image` is that you can't take
  43. * advantage of some SVG features, such as styling specific parts of the icon with CSS or SVG
  44. * animation.
  45. *
  46. * `md-icon` makes it easier to use SVG icons by *inlining* the SVG into an `<svg>` element in the
  47. * document. The most straightforward way of referencing an SVG icon is via URL, just like a
  48. * traditional `<img>`. `$mdIconProvider`, as a convenience, lets you _name_ an icon so you can
  49. * reference it by name instead of URL throughout your templates.
  50. *
  51. * Additionally, you may not want to make separate HTTP requests for every icon, so you can bundle
  52. * your SVG icons together and pre-load them with $mdIconProvider as an icon set. An icon set can
  53. * also be given a name, which acts as a namespace for individual icons, so you can reference them
  54. * like `"social:cake"`.
  55. *
  56. * When using SVGs, both external SVGs (via URLs) or sets of SVGs [from icon sets] can be
  57. * easily loaded and used. When using font-icons, developers must follow three (3) simple steps:
  58. *
  59. * <ol>
  60. * <li>Load the font library. e.g.<br/>
  61. * `<link href="https://fonts.googleapis.com/icon?family=Material+Icons"
  62. * rel="stylesheet">`
  63. * </li>
  64. * <li>
  65. * Use either (a) font-icon class names or (b) a fontset and a font ligature to render the font glyph by
  66. * using its textual name _or_ numerical character reference. Note that `material-icons` is the default fontset when
  67. * none is specified.
  68. * </li>
  69. * <li> Use any of the following templates: <br/>
  70. * <ul>
  71. * <li>`<md-icon md-font-icon="classname"></md-icon>`</li>
  72. * <li>`<md-icon md-font-set="font library classname or alias">textual_name</md-icon>`</li>
  73. * <li>`<md-icon> numerical_character_reference </md-icon>`</li>
  74. * <li>`<md-icon ng_bind="'textual_name'"></md-icon>`</li>
  75. * <li>`<md-icon ng-bind="scopeVariable"></md-icon>`</li>
  76. * </ul>
  77. * </li>
  78. * </ol>
  79. *
  80. * Full details for these steps can be found:
  81. *
  82. * <ul>
  83. * <li>http://google.github.io/material-design-icons/</li>
  84. * <li>http://google.github.io/material-design-icons/#icon-font-for-the-web</li>
  85. * </ul>
  86. *
  87. * The Material Design icon style <code>.material-icons</code> and the icon font references are published in
  88. * Material Design Icons:
  89. *
  90. * <ul>
  91. * <li>https://design.google.com/icons/</li>
  92. * <li>https://design.google.com/icons/#ic_accessibility</li>
  93. * </ul>
  94. *
  95. * ### Localization
  96. *
  97. * Because an `md-icon` element's text content is not intended to be translated, it is recommended to declare the text
  98. * content for an `md-icon` element in its start tag. Instead of using the HTML text content, consider using `ng-bind`
  99. * with a scope variable or literal string.
  100. *
  101. * Examples:
  102. *
  103. * <ul>
  104. * <li>`<md-icon ng-bind="myIconVariable"></md-icon>`</li>
  105. * <li>`<md-icon ng-bind="'menu'"></md-icon>`
  106. * </ul>
  107. *
  108. * <h2 id="material_design_icons">Material Design Icons</h2>
  109. * Using the Material Design Icon-Selector, developers can easily and quickly search for a Material Design font-icon and
  110. * determine its textual name and character reference code. Click on any icon to see the slide-up information
  111. * panel with details regarding a SVG download or information on the font-icon usage.
  112. *
  113. * <a href="https://www.google.com/design/icons/#ic_accessibility" target="_blank" style="border-bottom:none;">
  114. * <img src="https://cloud.githubusercontent.com/assets/210413/7902490/fe8dd14c-0780-11e5-98fb-c821cc6475e6.png"
  115. * aria-label="Material Design Icon-Selector" style="max-width:75%;padding-left:10%">
  116. * </a>
  117. *
  118. * <span class="image_caption">
  119. * Click on the image above to link to the
  120. * <a href="https://design.google.com/icons/#ic_accessibility" target="_blank">Material Design Icon-Selector</a>.
  121. * </span>
  122. *
  123. * @param {string} md-font-icon String name of CSS icon associated with the font-face will be used
  124. * to render the icon. Requires the fonts and the named CSS styles to be preloaded.
  125. * @param {string} md-font-set CSS style name associated with the font library; which will be assigned as
  126. * the class for the font-icon ligature. This value may also be an alias that is used to lookup the classname;
  127. * internally use `$mdIconProvider.fontSet(<alias>)` to determine the style name.
  128. * @param {string} md-svg-src String URL (or expression) used to load, cache, and display an
  129. * external SVG.
  130. * @param {string} md-svg-icon md-svg-icon String name used for lookup of the icon from the internal cache;
  131. * interpolated strings or expressions may also be used. Specific set names can be used with
  132. * the syntax `<set name>:<icon name>`.<br/><br/>
  133. * To use icon sets, developers are required to pre-register the sets using the `$mdIconProvider` service.
  134. * @param {string=} aria-label Labels icon for accessibility. If an empty string is provided, icon
  135. * will be hidden from accessibility layer with `aria-hidden="true"`. If there's no aria-label on the icon
  136. * nor a label on the parent element, a warning will be logged to the console.
  137. * @param {string=} alt Labels icon for accessibility. If an empty string is provided, icon
  138. * will be hidden from accessibility layer with `aria-hidden="true"`. If there's no alt on the icon
  139. * nor a label on the parent element, a warning will be logged to the console.
  140. *
  141. * @usage
  142. * When using SVGs:
  143. * <hljs lang="html">
  144. *
  145. * <!-- Icon ID; may contain optional icon set prefix; icons must registered using $mdIconProvider -->
  146. * <md-icon md-svg-icon="social:android" aria-label="android " ></md-icon>
  147. *
  148. * <!-- Icon urls; may be preloaded in templateCache -->
  149. * <md-icon md-svg-src="/android.svg" aria-label="android " ></md-icon>
  150. * <md-icon md-svg-src="{{ getAndroid() }}" aria-label="android " ></md-icon>
  151. *
  152. * </hljs>
  153. *
  154. * Use the <code>$mdIconProvider</code> to configure your application with
  155. * svg iconsets.
  156. *
  157. * <hljs lang="js">
  158. * angular.module('appSvgIconSets', ['ngMaterial'])
  159. * .controller('DemoCtrl', function($scope) {})
  160. * .config(function($mdIconProvider) {
  161. * $mdIconProvider
  162. * .iconSet('social', 'img/icons/sets/social-icons.svg', 24)
  163. * .defaultIconSet('img/icons/sets/core-icons.svg', 24);
  164. * });
  165. * </hljs>
  166. *
  167. *
  168. * When using Font Icons with classnames:
  169. * <hljs lang="html">
  170. *
  171. * <md-icon md-font-icon="android" aria-label="android" ></md-icon>
  172. * <md-icon class="icon_home" aria-label="Home" ></md-icon>
  173. *
  174. * </hljs>
  175. *
  176. * When using Material Font Icons with ligatures:
  177. * <hljs lang="html">
  178. * <!--
  179. * For Material Design Icons
  180. * The class '.material-icons' is auto-added if a style has NOT been specified
  181. * since `material-icons` is the default fontset. So your markup:
  182. * -->
  183. * <md-icon> face </md-icon>
  184. * <!-- becomes this at runtime: -->
  185. * <md-icon md-font-set="material-icons"> face </md-icon>
  186. * <!-- If the fontset does not support ligature names, then we need to use the ligature unicode.-->
  187. * <md-icon> &#xE87C; </md-icon>
  188. * <!-- The class '.material-icons' must be manually added if other styles are also specified-->
  189. * <md-icon class="material-icons md-light md-48"> face </md-icon>
  190. * </hljs>
  191. *
  192. * When using other Font-Icon libraries:
  193. *
  194. * <hljs lang="js">
  195. * // Specify a font-icon style alias
  196. * angular.config(function($mdIconProvider) {
  197. * $mdIconProvider.fontSet('md', 'material-icons');
  198. * });
  199. * </hljs>
  200. *
  201. * <hljs lang="html">
  202. * <md-icon md-font-set="md">favorite</md-icon>
  203. * </hljs>
  204. *
  205. */
  206. function mdIconDirective($mdIcon, $mdTheming, $mdAria, $sce) {
  207. return {
  208. restrict: 'E',
  209. link : postLink
  210. };
  211. /**
  212. * Directive postLink
  213. * Supports embedded SVGs, font-icons, & external SVGs
  214. */
  215. function postLink(scope, element, attr) {
  216. $mdTheming(element);
  217. var lastFontIcon = attr.mdFontIcon;
  218. var lastFontSet = $mdIcon.fontSet(attr.mdFontSet);
  219. prepareForFontIcon();
  220. attr.$observe('mdFontIcon', fontIconChanged);
  221. attr.$observe('mdFontSet', fontIconChanged);
  222. // Keep track of the content of the svg src so we can compare against it later to see if the
  223. // attribute is static (and thus safe).
  224. var originalSvgSrc = element[0].getAttribute(attr.$attr.mdSvgSrc);
  225. // If using a font-icon, then the textual name of the icon itself
  226. // provides the aria-label.
  227. var attrName = attr.$normalize(attr.$attr.mdSvgIcon || attr.$attr.mdSvgSrc || '');
  228. /* Provide a default accessibility role of img */
  229. if (!attr.role) {
  230. $mdAria.expect(element, 'role', 'img');
  231. /* manually update attr variable */
  232. attr.role = 'img';
  233. }
  234. /* Don't process ARIA if already valid */
  235. if ( attr.role === "img" && !attr.ariaHidden && !$mdAria.hasAriaLabel(element) ) {
  236. var iconName;
  237. if (attr.alt) {
  238. /* Use alt text by default if available */
  239. $mdAria.expect(element, 'aria-label', attr.alt);
  240. } else if ($mdAria.parentHasAriaLabel(element, 2)) {
  241. /* Parent has ARIA so we will assume it will describe the image */
  242. $mdAria.expect(element, 'aria-hidden', 'true');
  243. } else if (iconName = (attr.mdFontIcon || attr.mdSvgIcon || element.text())) {
  244. /* Use icon name as aria-label */
  245. $mdAria.expect(element, 'aria-label', iconName);
  246. } else {
  247. /* No label found */
  248. $mdAria.expect(element, 'aria-hidden', 'true');
  249. }
  250. }
  251. if (attrName) {
  252. // Use either pre-configured SVG or URL source, respectively.
  253. attr.$observe(attrName, function(attrVal) {
  254. element.empty();
  255. if (attrVal) {
  256. $mdIcon(attrVal)
  257. .then(function(svg) {
  258. element.empty();
  259. element.append(svg);
  260. });
  261. }
  262. });
  263. }
  264. function prepareForFontIcon() {
  265. if (!attr.mdSvgIcon && !attr.mdSvgSrc) {
  266. if (attr.mdFontIcon) {
  267. element.addClass('md-font ' + attr.mdFontIcon);
  268. }
  269. element.addClass(lastFontSet);
  270. }
  271. }
  272. function fontIconChanged() {
  273. if (!attr.mdSvgIcon && !attr.mdSvgSrc) {
  274. if (attr.mdFontIcon) {
  275. element.removeClass(lastFontIcon);
  276. element.addClass(attr.mdFontIcon);
  277. lastFontIcon = attr.mdFontIcon;
  278. }
  279. var fontSet = $mdIcon.fontSet(attr.mdFontSet);
  280. if (lastFontSet !== fontSet) {
  281. element.removeClass(lastFontSet);
  282. element.addClass(fontSet);
  283. lastFontSet = fontSet;
  284. }
  285. }
  286. }
  287. }
  288. }
  289. MdIconService['$inject'] = ["config", "$templateRequest", "$q", "$log", "$mdUtil", "$sce"];angular
  290. .module('material.components.icon')
  291. .constant('$$mdSvgRegistry', {
  292. 'mdTabsArrow': 'data:image/svg+xml;base64,PHN2ZyB2ZXJzaW9uPSIxLjEiIHg9IjBweCIgeT0iMHB4IiB2aWV3Qm94PSIwIDAgMjQgMjQiPjxnPjxwb2x5Z29uIHBvaW50cz0iMTUuNCw3LjQgMTQsNiA4LDEyIDE0LDE4IDE1LjQsMTYuNiAxMC44LDEyICIvPjwvZz48L3N2Zz4=',
  293. 'mdClose': 'data:image/svg+xml;base64,PHN2ZyB2ZXJzaW9uPSIxLjEiIHg9IjBweCIgeT0iMHB4IiB2aWV3Qm94PSIwIDAgMjQgMjQiPjxnPjxwYXRoIGQ9Ik0xOSA2LjQxbC0xLjQxLTEuNDEtNS41OSA1LjU5LTUuNTktNS41OS0xLjQxIDEuNDEgNS41OSA1LjU5LTUuNTkgNS41OSAxLjQxIDEuNDEgNS41OS01LjU5IDUuNTkgNS41OSAxLjQxLTEuNDEtNS41OS01LjU5eiIvPjwvZz48L3N2Zz4=',
  294. 'mdCancel': 'data:image/svg+xml;base64,PHN2ZyB2ZXJzaW9uPSIxLjEiIHg9IjBweCIgeT0iMHB4IiB2aWV3Qm94PSIwIDAgMjQgMjQiPjxnPjxwYXRoIGQ9Ik0xMiAyYy01LjUzIDAtMTAgNC40Ny0xMCAxMHM0LjQ3IDEwIDEwIDEwIDEwLTQuNDcgMTAtMTAtNC40Ny0xMC0xMC0xMHptNSAxMy41OWwtMS40MSAxLjQxLTMuNTktMy41OS0zLjU5IDMuNTktMS40MS0xLjQxIDMuNTktMy41OS0zLjU5LTMuNTkgMS40MS0xLjQxIDMuNTkgMy41OSAzLjU5LTMuNTkgMS40MSAxLjQxLTMuNTkgMy41OSAzLjU5IDMuNTl6Ii8+PC9nPjwvc3ZnPg==',
  295. 'mdMenu': 'data:image/svg+xml;base64,PHN2ZyB2ZXJzaW9uPSIxLjEiIHg9IjBweCIgeT0iMHB4IiB2aWV3Qm94PSIwIDAgMjQgMjQiPjxwYXRoIGQ9Ik0zLDZIMjFWOEgzVjZNMywxMUgyMVYxM0gzVjExTTMsMTZIMjFWMThIM1YxNloiIC8+PC9zdmc+',
  296. 'mdToggleArrow': 'data:image/svg+xml;base64,PHN2ZyB2ZXJzaW9uPSIxLjEiIHg9IjBweCIgeT0iMHB4IiB2aWV3Qm94PSIwIDAgNDggNDgiPjxwYXRoIGQ9Ik0yNCAxNmwtMTIgMTIgMi44MyAyLjgzIDkuMTctOS4xNyA5LjE3IDkuMTcgMi44My0yLjgzeiIvPjxwYXRoIGQ9Ik0wIDBoNDh2NDhoLTQ4eiIgZmlsbD0ibm9uZSIvPjwvc3ZnPg==',
  297. 'mdCalendar': 'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIyNCIgaGVpZ2h0PSIyNCIgdmlld0JveD0iMCAwIDI0IDI0Ij48cGF0aCBkPSJNMTkgM2gtMVYxaC0ydjJIOFYxSDZ2Mkg1Yy0xLjExIDAtMS45OS45LTEuOTkgMkwzIDE5YzAgMS4xLjg5IDIgMiAyaDE0YzEuMSAwIDItLjkgMi0yVjVjMC0xLjEtLjktMi0yLTJ6bTAgMTZINVY4aDE0djExek03IDEwaDV2NUg3eiIvPjwvc3ZnPg==',
  298. 'mdChecked': 'data:image/svg+xml;base64,PHN2ZyB2ZXJzaW9uPSIxLjEiIHg9IjBweCIgeT0iMHB4IiB2aWV3Qm94PSIwIDAgMjQgMjQiPjxnPjxwYXRoIGQ9Ik05IDE2LjE3TDQuODMgMTJsLTEuNDIgMS40MUw5IDE5IDIxIDdsLTEuNDEtMS40MXoiLz48L2c+PC9zdmc+'
  299. })
  300. .provider('$mdIcon', MdIconProvider);
  301. /**
  302. * @ngdoc service
  303. * @name $mdIconProvider
  304. * @module material.components.icon
  305. *
  306. * @description
  307. * `$mdIconProvider` is used only to register icon IDs with URLs. These configuration features allow
  308. * icons and icon sets to be pre-registered and associated with source URLs **before** the `<md-icon />`
  309. * directives are compiled.
  310. *
  311. * If using font-icons, the developer is responsible for loading the fonts.
  312. *
  313. * If using SVGs, loading of the actual svg files are deferred to on-demand requests and are loaded
  314. * internally by the `$mdIcon` service using the `$templateRequest` service. When an SVG is
  315. * requested by name/ID, the `$mdIcon` service searches its registry for the associated source URL;
  316. * that URL is used to on-demand load and parse the SVG dynamically.
  317. *
  318. * The `$templateRequest` service expects the icons source to be loaded over trusted URLs.<br/>
  319. * This means, when loading icons from an external URL, you have to trust the URL in the `$sceDelegateProvider`.
  320. *
  321. * <hljs lang="js">
  322. * app.config(function($sceDelegateProvider) {
  323. * $sceDelegateProvider.resourceUrlWhitelist([
  324. * // Adding 'self' to the whitelist, will allow requests from the current origin.
  325. * 'self',
  326. * // Using double asterisks here, will allow all URLs to load.
  327. * // We recommend to only specify the given domain you want to allow.
  328. * '**'
  329. * ]);
  330. * });
  331. * </hljs>
  332. *
  333. * Read more about the [$sceDelegateProvider](https://docs.angularjs.org/api/ng/provider/$sceDelegateProvider).
  334. *
  335. * **Notice:** Most font-icons libraries do not support ligatures (for example `fontawesome`).<br/>
  336. * In such cases you are not able to use the icon's ligature name - Like so:
  337. *
  338. * <hljs lang="html">
  339. * <md-icon md-font-set="fa">fa-bell</md-icon>
  340. * </hljs>
  341. *
  342. * You should instead use the given unicode, instead of the ligature name.
  343. *
  344. * <p ng-hide="true"> ##// Notice we can't use a hljs element here, because the characters will be escaped.</p>
  345. * ```html
  346. * <md-icon md-font-set="fa">&#xf0f3</md-icon>
  347. * ```
  348. *
  349. * All unicode ligatures are prefixed with the `&#x` string.
  350. *
  351. * @usage
  352. * <hljs lang="js">
  353. * app.config(function($mdIconProvider) {
  354. *
  355. * // Configure URLs for icons specified by [set:]id.
  356. *
  357. * $mdIconProvider
  358. * .defaultFontSet( 'fa' ) // This sets our default fontset className.
  359. * .defaultIconSet('my/app/icons.svg') // Register a default set of SVG icons
  360. * .iconSet('social', 'my/app/social.svg') // Register a named icon set of SVGs
  361. * .icon('android', 'my/app/android.svg') // Register a specific icon (by name)
  362. * .icon('work:chair', 'my/app/chair.svg'); // Register icon in a specific set
  363. * });
  364. * </hljs>
  365. *
  366. * SVG icons and icon sets can be easily pre-loaded and cached using either (a) a build process or (b) a runtime
  367. * **startup** process (shown below):
  368. *
  369. * <hljs lang="js">
  370. * app.config(function($mdIconProvider) {
  371. *
  372. * // Register a default set of SVG icon definitions
  373. * $mdIconProvider.defaultIconSet('my/app/icons.svg')
  374. *
  375. * })
  376. * .run(function($templateRequest){
  377. *
  378. * // Pre-fetch icons sources by URL and cache in the $templateCache...
  379. * // subsequent $templateRequest calls will look there first.
  380. *
  381. * var urls = [ 'imy/app/icons.svg', 'img/icons/android.svg'];
  382. *
  383. * angular.forEach(urls, function(url) {
  384. * $templateRequest(url);
  385. * });
  386. *
  387. * });
  388. *
  389. * </hljs>
  390. *
  391. * > <b>Note:</b> The loaded SVG data is subsequently cached internally for future requests.
  392. *
  393. */
  394. /**
  395. * @ngdoc method
  396. * @name $mdIconProvider#icon
  397. *
  398. * @description
  399. * Register a source URL for a specific icon name; the name may include optional 'icon set' name prefix.
  400. * These icons will later be retrieved from the cache using `$mdIcon( <icon name> )`
  401. *
  402. * @param {string} id Icon name/id used to register the icon
  403. * @param {string} url specifies the external location for the data file. Used internally by
  404. * `$templateRequest` to load the data or as part of the lookup in `$templateCache` if pre-loading
  405. * was configured.
  406. * @param {number=} viewBoxSize Sets the width and height the icon's viewBox.
  407. * It is ignored for icons with an existing viewBox. Default size is 24.
  408. *
  409. * @returns {obj} an `$mdIconProvider` reference; used to support method call chains for the API
  410. *
  411. * @usage
  412. * <hljs lang="js">
  413. * app.config(function($mdIconProvider) {
  414. *
  415. * // Configure URLs for icons specified by [set:]id.
  416. *
  417. * $mdIconProvider
  418. * .icon('android', 'my/app/android.svg') // Register a specific icon (by name)
  419. * .icon('work:chair', 'my/app/chair.svg'); // Register icon in a specific set
  420. * });
  421. * </hljs>
  422. *
  423. */
  424. /**
  425. * @ngdoc method
  426. * @name $mdIconProvider#iconSet
  427. *
  428. * @description
  429. * Register a source URL for a 'named' set of icons; group of SVG definitions where each definition
  430. * has an icon id. Individual icons can be subsequently retrieved from this cached set using
  431. * `$mdIcon(<icon set name>:<icon name>)`
  432. *
  433. * @param {string} id Icon name/id used to register the iconset
  434. * @param {string} url specifies the external location for the data file. Used internally by
  435. * `$templateRequest` to load the data or as part of the lookup in `$templateCache` if pre-loading
  436. * was configured.
  437. * @param {number=} viewBoxSize Sets the width and height of the viewBox of all icons in the set.
  438. * It is ignored for icons with an existing viewBox. All icons in the icon set should be the same size.
  439. * Default value is 24.
  440. *
  441. * @returns {obj} an `$mdIconProvider` reference; used to support method call chains for the API
  442. *
  443. *
  444. * @usage
  445. * <hljs lang="js">
  446. * app.config(function($mdIconProvider) {
  447. *
  448. * // Configure URLs for icons specified by [set:]id.
  449. *
  450. * $mdIconProvider
  451. * .iconSet('social', 'my/app/social.svg') // Register a named icon set
  452. * });
  453. * </hljs>
  454. *
  455. */
  456. /**
  457. * @ngdoc method
  458. * @name $mdIconProvider#defaultIconSet
  459. *
  460. * @description
  461. * Register a source URL for the default 'named' set of icons. Unless explicitly registered,
  462. * subsequent lookups of icons will failover to search this 'default' icon set.
  463. * Icon can be retrieved from this cached, default set using `$mdIcon(<name>)`
  464. *
  465. * @param {string} url specifies the external location for the data file. Used internally by
  466. * `$templateRequest` to load the data or as part of the lookup in `$templateCache` if pre-loading
  467. * was configured.
  468. * @param {number=} viewBoxSize Sets the width and height of the viewBox of all icons in the set.
  469. * It is ignored for icons with an existing viewBox. All icons in the icon set should be the same size.
  470. * Default value is 24.
  471. *
  472. * @returns {obj} an `$mdIconProvider` reference; used to support method call chains for the API
  473. *
  474. * @usage
  475. * <hljs lang="js">
  476. * app.config(function($mdIconProvider) {
  477. *
  478. * // Configure URLs for icons specified by [set:]id.
  479. *
  480. * $mdIconProvider
  481. * .defaultIconSet( 'my/app/social.svg' ) // Register a default icon set
  482. * });
  483. * </hljs>
  484. *
  485. */
  486. /**
  487. * @ngdoc method
  488. * @name $mdIconProvider#defaultFontSet
  489. *
  490. * @description
  491. * When using Font-Icons, AngularJS Material assumes the the Material Design icons will be used and automatically
  492. * configures the default font-set == 'material-icons'. Note that the font-set references the font-icon library
  493. * class style that should be applied to the `<md-icon>`.
  494. *
  495. * Configuring the default means that the attributes
  496. * `md-font-set="material-icons"` or `class="material-icons"` do not need to be explicitly declared on the
  497. * `<md-icon>` markup. For example:
  498. *
  499. * `<md-icon> face </md-icon>`
  500. * will render as
  501. * `<span class="material-icons"> face </span>`, and
  502. *
  503. * `<md-icon md-font-set="fa"> face </md-icon>`
  504. * will render as
  505. * `<span class="fa"> face </span>`
  506. *
  507. * @param {string} name of the font-library style that should be applied to the md-icon DOM element
  508. *
  509. * @usage
  510. * <hljs lang="js">
  511. * app.config(function($mdIconProvider) {
  512. * $mdIconProvider.defaultFontSet( 'fa' );
  513. * });
  514. * </hljs>
  515. *
  516. */
  517. /**
  518. * @ngdoc method
  519. * @name $mdIconProvider#fontSet
  520. *
  521. * @description
  522. * When using a font set for `<md-icon>` you must specify the correct font classname in the `md-font-set`
  523. * attribute. If the fonset className is really long, your markup may become cluttered... an easy
  524. * solution is to define an `alias` for your fontset:
  525. *
  526. * @param {string} alias of the specified fontset.
  527. * @param {string} className of the fontset.
  528. *
  529. * @usage
  530. * <hljs lang="js">
  531. * app.config(function($mdIconProvider) {
  532. * // In this case, we set an alias for the `material-icons` fontset.
  533. * $mdIconProvider.fontSet('md', 'material-icons');
  534. * });
  535. * </hljs>
  536. *
  537. */
  538. /**
  539. * @ngdoc method
  540. * @name $mdIconProvider#defaultViewBoxSize
  541. *
  542. * @description
  543. * While `<md-icon />` markup can also be style with sizing CSS, this method configures
  544. * the default width **and** height used for all icons; unless overridden by specific CSS.
  545. * The default sizing is (24px, 24px).
  546. * @param {number=} viewBoxSize Sets the width and height of the viewBox for an icon or an icon set.
  547. * All icons in a set should be the same size. The default value is 24.
  548. *
  549. * @returns {obj} an `$mdIconProvider` reference; used to support method call chains for the API
  550. *
  551. * @usage
  552. * <hljs lang="js">
  553. * app.config(function($mdIconProvider) {
  554. *
  555. * // Configure URLs for icons specified by [set:]id.
  556. *
  557. * $mdIconProvider
  558. * .defaultViewBoxSize(36) // Register a default icon size (width == height)
  559. * });
  560. * </hljs>
  561. *
  562. */
  563. var config = {
  564. defaultViewBoxSize: 24,
  565. defaultFontSet: 'material-icons',
  566. fontSets: []
  567. };
  568. function MdIconProvider() {
  569. }
  570. MdIconProvider.prototype = {
  571. icon: function(id, url, viewBoxSize) {
  572. if (id.indexOf(':') == -1) id = '$default:' + id;
  573. config[id] = new ConfigurationItem(url, viewBoxSize);
  574. return this;
  575. },
  576. iconSet: function(id, url, viewBoxSize) {
  577. config[id] = new ConfigurationItem(url, viewBoxSize);
  578. return this;
  579. },
  580. defaultIconSet: function(url, viewBoxSize) {
  581. var setName = '$default';
  582. if (!config[setName]) {
  583. config[setName] = new ConfigurationItem(url, viewBoxSize);
  584. }
  585. config[setName].viewBoxSize = viewBoxSize || config.defaultViewBoxSize;
  586. return this;
  587. },
  588. defaultViewBoxSize: function(viewBoxSize) {
  589. config.defaultViewBoxSize = viewBoxSize;
  590. return this;
  591. },
  592. /**
  593. * Register an alias name associated with a font-icon library style ;
  594. */
  595. fontSet: function fontSet(alias, className) {
  596. config.fontSets.push({
  597. alias: alias,
  598. fontSet: className || alias
  599. });
  600. return this;
  601. },
  602. /**
  603. * Specify a default style name associated with a font-icon library
  604. * fallback to Material Icons.
  605. *
  606. */
  607. defaultFontSet: function defaultFontSet(className) {
  608. config.defaultFontSet = !className ? '' : className;
  609. return this;
  610. },
  611. defaultIconSize: function defaultIconSize(iconSize) {
  612. config.defaultIconSize = iconSize;
  613. return this;
  614. },
  615. $get: ['$templateRequest', '$q', '$log', '$mdUtil', '$sce', function($templateRequest, $q, $log, $mdUtil, $sce) {
  616. return MdIconService(config, $templateRequest, $q, $log, $mdUtil, $sce);
  617. }]
  618. };
  619. /**
  620. * Configuration item stored in the Icon registry; used for lookups
  621. * to load if not already cached in the `loaded` cache
  622. */
  623. function ConfigurationItem(url, viewBoxSize) {
  624. this.url = url;
  625. this.viewBoxSize = viewBoxSize || config.defaultViewBoxSize;
  626. }
  627. /**
  628. * @ngdoc service
  629. * @name $mdIcon
  630. * @module material.components.icon
  631. *
  632. * @description
  633. * The `$mdIcon` service is a function used to lookup SVG icons.
  634. *
  635. * @param {string} id Query value for a unique Id or URL. If the argument is a URL, then the service will retrieve the icon element
  636. * from its internal cache or load the icon and cache it first. If the value is not a URL-type string, then an ID lookup is
  637. * performed. The Id may be a unique icon ID or may include an iconSet ID prefix.
  638. *
  639. * For the **id** query to work properly, this means that all id-to-URL mappings must have been previously configured
  640. * using the `$mdIconProvider`.
  641. *
  642. * @returns {angular.$q.Promise} A promise that gets resolved to a clone of the initial SVG DOM element; which was
  643. * created from the SVG markup in the SVG data file. If an error occurs (e.g. the icon cannot be found) the promise
  644. * will get rejected.
  645. *
  646. * @usage
  647. * <hljs lang="js">
  648. * function SomeDirective($mdIcon) {
  649. *
  650. * // See if the icon has already been loaded, if not
  651. * // then lookup the icon from the registry cache, load and cache
  652. * // it for future requests.
  653. * // NOTE: ID queries require configuration with $mdIconProvider
  654. *
  655. * $mdIcon('android').then(function(iconEl) { element.append(iconEl); });
  656. * $mdIcon('work:chair').then(function(iconEl) { element.append(iconEl); });
  657. *
  658. * // Load and cache the external SVG using a URL
  659. *
  660. * $mdIcon('img/icons/android.svg').then(function(iconEl) {
  661. * element.append(iconEl);
  662. * });
  663. * };
  664. * </hljs>
  665. *
  666. * > <b>Note:</b> The `<md-icon>` directive internally uses the `$mdIcon` service to query, loaded,
  667. * and instantiate SVG DOM elements.
  668. */
  669. /* ngInject */
  670. function MdIconService(config, $templateRequest, $q, $log, $mdUtil, $sce) {
  671. var iconCache = {};
  672. var svgCache = {};
  673. var urlRegex = /[-\w@:%+.~#?&//=]{2,}\.[a-z]{2,4}\b(\/[-\w@:%+.~#?&//=]*)?/i;
  674. var dataUrlRegex = /^data:image\/svg\+xml[\s*;\w\-=]*?(base64)?,(.*)$/i;
  675. Icon.prototype = {clone: cloneSVG, prepare: prepareAndStyle};
  676. getIcon.fontSet = findRegisteredFontSet;
  677. // Publish service...
  678. return getIcon;
  679. /**
  680. * Actual $mdIcon service is essentially a lookup function
  681. */
  682. function getIcon(id) {
  683. id = id || '';
  684. // If the "id" provided is not a string, the only other valid value is a $sce trust wrapper
  685. // over a URL string. If the value is not trusted, this will intentionally throw an error
  686. // because the user is attempted to use an unsafe URL, potentially opening themselves up
  687. // to an XSS attack.
  688. if (!angular.isString(id)) {
  689. id = $sce.getTrustedUrl(id);
  690. }
  691. // If already loaded and cached, use a clone of the cached icon.
  692. // Otherwise either load by URL, or lookup in the registry and then load by URL, and cache.
  693. if (iconCache[id]) {
  694. return $q.when(transformClone(iconCache[id]));
  695. }
  696. if (urlRegex.test(id) || dataUrlRegex.test(id)) {
  697. return loadByURL(id).then(cacheIcon(id));
  698. }
  699. if (id.indexOf(':') == -1) {
  700. id = '$default:' + id;
  701. }
  702. var load = config[id] ? loadByID : loadFromIconSet;
  703. return load(id)
  704. .then(cacheIcon(id));
  705. }
  706. /**
  707. * Lookup registered fontSet style using its alias...
  708. * If not found,
  709. */
  710. function findRegisteredFontSet(alias) {
  711. var useDefault = angular.isUndefined(alias) || !(alias && alias.length);
  712. if (useDefault) return config.defaultFontSet;
  713. var result = alias;
  714. angular.forEach(config.fontSets, function(it) {
  715. if (it.alias == alias) result = it.fontSet || result;
  716. });
  717. return result;
  718. }
  719. function transformClone(cacheElement) {
  720. var clone = cacheElement.clone();
  721. var cacheSuffix = '_cache' + $mdUtil.nextUid();
  722. // We need to modify for each cached icon the id attributes.
  723. // This is needed because SVG id's are treated as normal DOM ids
  724. // and should not have a duplicated id.
  725. if (clone.id) clone.id += cacheSuffix;
  726. angular.forEach(clone.querySelectorAll('[id]'), function(item) {
  727. item.id += cacheSuffix;
  728. });
  729. return clone;
  730. }
  731. /**
  732. * Prepare and cache the loaded icon for the specified `id`
  733. */
  734. function cacheIcon(id) {
  735. return function updateCache(icon) {
  736. iconCache[id] = isIcon(icon) ? icon : new Icon(icon, config[id]);
  737. return iconCache[id].clone();
  738. };
  739. }
  740. /**
  741. * Lookup the configuration in the registry, if !registered throw an error
  742. * otherwise load the icon [on-demand] using the registered URL.
  743. *
  744. */
  745. function loadByID(id) {
  746. var iconConfig = config[id];
  747. return loadByURL(iconConfig.url).then(function(icon) {
  748. return new Icon(icon, iconConfig);
  749. });
  750. }
  751. /**
  752. * Loads the file as XML and uses querySelector( <id> ) to find
  753. * the desired node...
  754. */
  755. function loadFromIconSet(id) {
  756. var setName = id.substring(0, id.lastIndexOf(':')) || '$default';
  757. var iconSetConfig = config[setName];
  758. return !iconSetConfig ? announceIdNotFound(id) : loadByURL(iconSetConfig.url).then(extractFromSet);
  759. function extractFromSet(set) {
  760. var iconName = id.slice(id.lastIndexOf(':') + 1);
  761. var icon = set.querySelector('#' + iconName);
  762. return icon ? new Icon(icon, iconSetConfig) : announceIdNotFound(id);
  763. }
  764. function announceIdNotFound(id) {
  765. var msg = 'icon ' + id + ' not found';
  766. $log.warn(msg);
  767. return $q.reject(msg || id);
  768. }
  769. }
  770. /**
  771. * Load the icon by URL (may use the $templateCache).
  772. * Extract the data for later conversion to Icon
  773. */
  774. function loadByURL(url) {
  775. /* Load the icon from embedded data URL. */
  776. function loadByDataUrl(url) {
  777. var results = dataUrlRegex.exec(url);
  778. var isBase64 = /base64/i.test(url);
  779. var data = isBase64 ? window.atob(results[2]) : results[2];
  780. return $q.when(angular.element(data)[0]);
  781. }
  782. /* Load the icon by URL using HTTP. */
  783. function loadByHttpUrl(url) {
  784. return $q(function(resolve, reject) {
  785. // Catch HTTP or generic errors not related to incorrect icon IDs.
  786. var announceAndReject = function(err) {
  787. var msg = angular.isString(err) ? err : (err.message || err.data || err.statusText);
  788. $log.warn(msg);
  789. reject(err);
  790. },
  791. extractSvg = function(response) {
  792. if (!svgCache[url]) {
  793. svgCache[url] = angular.element('<div>').append(response)[0].querySelector('svg');
  794. }
  795. resolve(svgCache[url]);
  796. };
  797. $templateRequest(url, true).then(extractSvg, announceAndReject);
  798. });
  799. }
  800. return dataUrlRegex.test(url)
  801. ? loadByDataUrl(url)
  802. : loadByHttpUrl(url);
  803. }
  804. /**
  805. * Check target signature to see if it is an Icon instance.
  806. */
  807. function isIcon(target) {
  808. return angular.isDefined(target.element) && angular.isDefined(target.config);
  809. }
  810. /**
  811. * Define the Icon class
  812. */
  813. function Icon(el, config) {
  814. if (el && el.tagName != 'svg') {
  815. el = angular.element('<svg xmlns="http://www.w3.org/2000/svg">').append(el.cloneNode(true))[0];
  816. }
  817. // Inject the namespace if not available...
  818. if (!el.getAttribute('xmlns')) {
  819. el.setAttribute('xmlns', "http://www.w3.org/2000/svg");
  820. }
  821. this.element = el;
  822. this.config = config;
  823. this.prepare();
  824. }
  825. /**
  826. * Prepare the DOM element that will be cached in the
  827. * loaded iconCache store.
  828. */
  829. function prepareAndStyle() {
  830. var viewBoxSize = this.config ? this.config.viewBoxSize : config.defaultViewBoxSize;
  831. angular.forEach({
  832. 'fit': '',
  833. 'height': '100%',
  834. 'width': '100%',
  835. 'preserveAspectRatio': 'xMidYMid meet',
  836. 'viewBox': this.element.getAttribute('viewBox') || ('0 0 ' + viewBoxSize + ' ' + viewBoxSize),
  837. 'focusable': false // Disable IE11s default behavior to make SVGs focusable
  838. }, function(val, attr) {
  839. this.element.setAttribute(attr, val);
  840. }, this);
  841. }
  842. /**
  843. * Clone the Icon DOM element.
  844. */
  845. function cloneSVG() {
  846. // If the element or any of its children have a style attribute, then a CSP policy without
  847. // 'unsafe-inline' in the style-src directive, will result in a violation.
  848. return this.element.cloneNode(true);
  849. }
  850. }
  851. ngmaterial.components.icon = angular.module("material.components.icon");