You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

178 lines
5.4 KiB

  1. /**
  2. * This file is part of the @iconify/iconify package.
  3. *
  4. * (c) Vjacheslav Trushkin <cyberalien@gmail.com>
  5. *
  6. * For the full copyright and license information, please view the license.txt or license.gpl.txt
  7. * files that were distributed with this source code.
  8. *
  9. * Licensed under Apache 2.0 or GPL 2.0 at your option.
  10. * If derivative product is not compatible with one of licenses, you can pick one of licenses.
  11. *
  12. * @license Apache 2.0
  13. * @license GPL 2.0
  14. */
  15. /**
  16. * Plugin for FontAwesome icons
  17. */
  18. (function(Iconify) {
  19. "use strict";
  20. /**
  21. * List of FontAwesome class names that do not represent icons
  22. *
  23. * @type {[string]}
  24. */
  25. var faReserved = ['fa-lg', 'fa-fw', 'fa-ul', 'fa-li', 'fa-border', 'fa-pull-left', 'fa-pull-right', 'fa-spin', 'fa-pulse', 'fa-rotate-90', 'fa-rotate-180', 'fa-rotate-270', 'fa-flip-horizontal', 'fa-flip-vertical', 'fa-stack', 'fa-stack-1x', 'fa-stack-2x', 'fa-inverse'],
  26. rotateAttribute = Iconify.getConfig('_rotateAttribute'),
  27. flipAttribute = Iconify.getConfig('_flipAttribute'),
  28. inlineAttribute = Iconify.getConfig('_inlineModeAttribute'),
  29. i;
  30. /**
  31. * Link to stylesheet
  32. *
  33. * @type {string}
  34. */
  35. var stylesheetCDN = DOKU_TPL + '/assets/iconify/plugins/fa.css';
  36. /**
  37. * True if stylesheet has been added
  38. *
  39. * @type {boolean}
  40. */
  41. var styleAdded = false;
  42. /**
  43. * Inserts short version of FontAwesome stylesheet into DOM
  44. */
  45. function insertStylesheet() {
  46. var element = document.createElement('link');
  47. styleAdded = true;
  48. element.setAttribute('rel', 'stylesheet');
  49. element.setAttribute('type', 'text/css');
  50. element.setAttribute('href', stylesheetCDN);
  51. document.head.appendChild(element);
  52. }
  53. /**
  54. * Create finder object
  55. *
  56. * @param {string} selector
  57. * @param {string} prefix
  58. * @returns {object}
  59. */
  60. function finder(selector, prefix) {
  61. return {
  62. selector: selector,
  63. /**
  64. * Get icon name from element
  65. *
  66. * @param {Element} element
  67. * @return {string}
  68. */
  69. icon: function(element) {
  70. var item;
  71. for (var i = 0; i < element.classList.length; i++) {
  72. item = element.classList[i];
  73. if (item.slice(0, 3) === 'fa-' && faReserved.indexOf(item) === -1) {
  74. return prefix + ':' + item.slice(3);
  75. }
  76. }
  77. return '';
  78. },
  79. /**
  80. * Filter class names list, removing any FontAwesome specific classes
  81. *
  82. * @param {object} image
  83. * @param {Array|DOMTokenList} list
  84. * @return {Array}
  85. */
  86. filterClasses: function(image, list) {
  87. var results = [],
  88. transform = {
  89. rotate: 0,
  90. hFlip: false,
  91. vFlip: false
  92. };
  93. if (image.attributes === void 0) {
  94. image.attributes = Object.create(null);
  95. }
  96. for (var i = 0; i < list.length; i++) {
  97. switch (list[i]) {
  98. case 'fa-rotate-90':
  99. transform.rotate = 1;
  100. break;
  101. case 'fa-rotate-180':
  102. transform.rotate = 2;
  103. break;
  104. case 'fa-rotate-270':
  105. transform.rotate = 3;
  106. break;
  107. case 'fa-flip-horizontal':
  108. transform.hFlip = true;
  109. break;
  110. case 'fa-flip-vertical':
  111. transform.vFlip = true;
  112. break;
  113. default:
  114. results.push(list[i]);
  115. }
  116. }
  117. if (image.attributes[inlineAttribute] === void 0) {
  118. image.attributes[inlineAttribute] = true;
  119. }
  120. // Add transformation as attributes
  121. if (transform.rotate) {
  122. image.attributes[rotateAttribute] = transform.rotate;
  123. }
  124. if (transform.hFlip || transform.vFlip) {
  125. image.attributes[flipAttribute] = (transform.hFlip && transform.vFlip) ? 'horizontal vertical' : (
  126. transform.hFlip ? 'horizontal' : 'vertical'
  127. );
  128. }
  129. // Insert short version of FontAwesome stylesheet into DOM
  130. if (!styleAdded) {
  131. insertStylesheet();
  132. }
  133. return results;
  134. }
  135. };
  136. }
  137. /**
  138. * Add more reserved keywords
  139. */
  140. for (i = 2; i < 11; i ++) {
  141. faReserved.push('fa-' + i + 'x');
  142. }
  143. /**
  144. * Add finder to list of finders
  145. */
  146. Iconify.addFinder('fa', finder('.fa', 'fa'));
  147. Iconify.addFinder('fas', finder('.fas', 'fa-solid'));
  148. Iconify.addFinder('far', finder('.far', 'fa-regular'));
  149. Iconify.addFinder('fal', finder('.fal', 'fa-light')); // pro only
  150. Iconify.addFinder('fab', finder('.fab', 'fa-brands'));
  151. })(Iconify);