BxBaseIconsetLucide.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaBaseView UNA Base Representation Classes
  7. * @{
  8. */
  9. /**
  10. * Basic Lucide iconset representation.
  11. * @see BxDolIconset
  12. */
  13. class BxBaseIconsetLucide extends BxBaseIconset
  14. {
  15. protected $_aMap;
  16. protected static $_aCacheSvg = [];
  17. public function __construct ($aObject, $oTemplate)
  18. {
  19. parent::__construct ($aObject, $oTemplate);
  20. $this->_aMap = [
  21. 'ad' => 'circle-star',
  22. 'address-book' => 'contact-round',
  23. 'address-card' => 'id-card',
  24. 'angle-double-down' => 'chevrons-down',
  25. 'angle-double-left' => 'chevrons-left',
  26. 'angle-double-right' => 'chevrons-right',
  27. 'arrows-alt' => 'move',
  28. 'bars' => 'menu',
  29. 'book-reader' => 'book-open-text',
  30. 'buffer' => 'layers',
  31. 'bullhorn' => 'megaphone',
  32. 'calendar-alt' => 'calendar-days',
  33. 'calendar-day' => 'calendar-days',
  34. 'camera-retro' => 'camera',
  35. 'caret-right' => 'chevron-right',
  36. 'cart-arrow-down' => 'shopping-cart',
  37. 'cart-plus' => 'shopping-cart',
  38. 'cc-stripe' => 'credit-card',
  39. 'certificate' => 'award',
  40. 'check-circle' => 'circle-check',
  41. 'check-double' => 'check-check',
  42. 'cog' => 'settings',
  43. 'cogs' => 'settings',
  44. 'comment' => 'message-circle',
  45. 'comment-alt' => 'message-square',
  46. 'comment-dots' => 'message-square-dashed',
  47. 'comments' => 'messages-square',
  48. 'desktop' => 'monitor',
  49. 'donate' => 'hand-coins',
  50. 'ellipsis-h' => 'ellipsis',
  51. 'ellipsis-v' => 'ellipsis-vertical',
  52. 'envelope' => 'mail',
  53. 'envelope-open' => 'mail-open',
  54. 'envelope-open-text' => 'mail-open',
  55. 'exchange-alt' => 'repeat',
  56. 'exclamation-circle' => 'circle-alert',
  57. 'exclamation-triangle' => 'triangle-alert',
  58. 'fa-book' => 'book-text',
  59. 'fa-bookmark' => 'bookmark',
  60. 'fa-certificate' => 'badge',
  61. 'fa-check-circle' => 'circle-check',
  62. 'fa-grip-horizontal' => 'layout-grid',
  63. 'fa-smile' => 'smile',
  64. 'fa-thumbs-up' => 'thumbs-up',
  65. 'facebook-square' => 'log-in', //TODO: Brand icon. Update later.
  66. 'file-alt' => 'file',
  67. 'file-alt' => 'file-text',
  68. 'file-import' => 'file-input',
  69. 'file-export' => 'file-output',
  70. 'file-invoice' => 'receipt',
  71. 'file-word' => 'file-text',
  72. 'fire' => 'flame',
  73. 'group' => 'users',
  74. 'google' => 'log-in', //TODO: Brand icon. Update later.
  75. 'hand-holding-usd' => 'hand-coins',
  76. 'hashtag' => 'hash',
  77. 'helpcircle' => 'circle-question-mark',
  78. 'industry' => 'factory',
  79. 'info-circle' => 'info',
  80. 'keyround' => 'key-round',
  81. 'language' => 'languages',
  82. 'list-alt' => 'list',
  83. 'linkedin-in' => 'log-in', //TODO: Brand icon. Update later.
  84. 'lockopen' => 'lock-open',
  85. 'mail-bulk' => 'mails',
  86. 'map-marker' => 'map-pin',
  87. 'map-marker-alt' => 'map-pin',
  88. 'money-check-alt' => 'wallet-cards',
  89. 'object-group' => 'layout-dashboard',
  90. 'pencil-alt' => 'pencil-line',
  91. 'pencil-ruler' => 'ruler',
  92. 'photo-video' => 'image-play',
  93. 'plus-circle' => 'circle-plus',
  94. 'qrcode' => 'qr-code',
  95. 'question' => 'circle-question-mark',
  96. 'question-circle' => 'circle-question-mark',
  97. 'quote-right' => 'quote',
  98. 'remove' => 'x',
  99. 'search-location' => 'search-check',
  100. 'share-alt' => 'share-2',
  101. 'shield-alt' => 'shield',
  102. 'sign-in-alt' => 'log-in',
  103. 'sign-out-alt' => 'log-out',
  104. 'star-half-o' => 'star-half',
  105. 'stopwatch' => 'timer',
  106. 'swatchbook' => 'swatch-book',
  107. 'sync' => 'refresh-ccw',
  108. 'sync-alt' => 'refresh-ccw',
  109. 'tachometer-alt' => 'gauge',
  110. 'tasks' => 'square-check',
  111. 'th-large' => 'grid-2x2',
  112. 'thumbtack' => 'pin',
  113. 'times' => 'x',
  114. 'times-circle' => 'circle-x',
  115. 'toolbox' => 'tool-case',
  116. 'trash2' => 'trash-2',
  117. 'twitter' => 'log-in', //TODO: Brand icon. Update later.
  118. 'unlock-alt' => 'lock-open',
  119. 'user' => 'user-round',
  120. 'user-friends' => 'users',
  121. 'user-plus' => 'user-round-plus',
  122. 'user-secret' => 'user-key',
  123. 'user-slash' => 'user-x',
  124. 'user-shield' => 'user-shield', // Lucide has it now; keep the entry so the name is not remapped by a later rule
  125. 'user-check' => 'user-round-check',
  126. 'user-times' => 'user-x',
  127. 'users' => 'users-round',
  128. 'video-camera' => 'video',
  129. 'at' => 'at-sign',
  130. 'backspace' => 'delete',
  131. 'trash-alt' => 'trash',
  132. 'magic' => 'wand-sparkles',
  133. ];
  134. }
  135. public function getPreloaderJs()
  136. {
  137. return '{dir_plugins_public}lucide/|lucide.min.js';
  138. }
  139. public function getIcon($sIcon)
  140. {
  141. return bx_gen_method_name($this->_getIconName($sIcon), ['_', '-']);
  142. }
  143. public function getIconHtml($sIcon, $aAttrs = [])
  144. {
  145. $sName = $this->_getIconName($sIcon);
  146. if(!preg_match('/^[a-z0-9-]+$/', $sName))
  147. return false;
  148. if(!isset(self::$_aCacheSvg[$sName])) {
  149. $sPath = BX_DIRECTORY_PATH_PLUGINS_PUBLIC . 'lucide/icons/' . $sName . '.svg';
  150. $sSvg = file_exists($sPath) ? file_get_contents($sPath) : false;
  151. if($sSvg !== false) {
  152. $sSvg = preg_replace('/<!--.*?-->/s', '', $sSvg);
  153. $sSvg = trim(preg_replace('/\s+/', ' ', $sSvg));
  154. $sSvg = str_replace(' />', '/>', $sSvg);
  155. }
  156. self::$_aCacheSvg[$sName] = $sSvg;
  157. }
  158. $sSvg = self::$_aCacheSvg[$sName];
  159. if($sSvg === false)
  160. return false;
  161. $sClass = 'sys-icon';
  162. if(!empty($aAttrs['class']))
  163. $sClass .= ' ' . $aAttrs['class'];
  164. unset($aAttrs['class']);
  165. $sAttrs = '';
  166. foreach($aAttrs as $sKey => $sValue)
  167. $sAttrs .= ' ' . $sKey . '="' . bx_html_attribute($sValue) . '"';
  168. return preg_replace('/^<svg class="/', '<svg' . $sAttrs . ' class="' . bx_html_attribute($sClass) . ' ', $sSvg, 1);
  169. }
  170. /**
  171. * Get Lucide icon name (kebab-case) by Font Awesome or Lucide icon name.
  172. */
  173. protected function _getIconName($sIcon)
  174. {
  175. $sIcon = trim(preg_replace('/(sys-icon|far|col-\w+)/i', '', $sIcon));
  176. if(isset($this->_aMap[$sIcon]))
  177. $sIcon = $this->_aMap[$sIcon];
  178. return $sIcon;
  179. }
  180. public function getCode()
  181. {
  182. $sMap = json_encode($this->_aMap);
  183. $sCode = <<<BLAH
  184. function bx_iconset_init_lucide() {
  185. if(!window.lucide || !lucide.icons)
  186. return;
  187. const aMap = $sMap;
  188. document.querySelectorAll('i.sys-icon').forEach(el => {
  189. if(el.hasAttribute('data-lucide'))
  190. return;
  191. const sName = el.getAttribute('class').replace(/(sys-icon-bigger|sys-icon|fab|far|fas|col-\w+)/gi, '').trim().split(' ').shift();
  192. if(sName)
  193. el.setAttribute('data-lucide', aMap[sName] != undefined ? aMap[sName] : sName);
  194. else
  195. console.warn('Lucide: no icon class found on', el);
  196. });
  197. lucide.createIcons({
  198. attrs: { class: ['sys-icon'] },
  199. nameAttr: 'data-lucide'
  200. });
  201. };
  202. bx_iconset_init_lucide();
  203. if (typeof glOnProcessHtml === 'undefined')
  204. glOnProcessHtml = [];
  205. if (glOnProcessHtml instanceof Array) {
  206. glOnProcessHtml.push(function(e) {
  207. bx_iconset_init_lucide();
  208. });
  209. }
  210. BLAH;
  211. return $this->_oTemplate->_wrapInTagJsCode($sCode);
  212. }
  213. }
  214. /** @} */