BxBaseIconsetLucide.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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-pdf' => 'file-text',
  72. 'file-word' => 'file-text',
  73. 'file-excel' => 'file-spreadsheet',
  74. 'fire' => 'flame',
  75. 'group' => 'users',
  76. 'google' => 'log-in', //TODO: Brand icon. Update later.
  77. 'hand-holding' => 'hand-helping',
  78. 'hand-holding-usd' => 'hand-coins',
  79. 'hashtag' => 'hash',
  80. 'helpcircle' => 'circle-question-mark',
  81. 'industry' => 'factory',
  82. 'info-circle' => 'info',
  83. 'keyround' => 'key-round',
  84. 'language' => 'languages',
  85. 'list-alt' => 'list',
  86. 'linkedin-in' => 'log-in', //TODO: Brand icon. Update later.
  87. 'lockopen' => 'lock-open',
  88. 'mail-bulk' => 'mails',
  89. 'map-marker' => 'map-pin',
  90. 'map-marker-alt' => 'map-pin',
  91. 'money-check-alt' => 'wallet-cards',
  92. 'money-bill-alt' => 'banknote',
  93. 'object-group' => 'layout-dashboard',
  94. 'paint-brush' => 'paintbrush',
  95. 'pencil-alt' => 'pencil-line',
  96. 'pencil-ruler' => 'ruler',
  97. 'photo-video' => 'image-play',
  98. 'plus-circle' => 'circle-plus',
  99. 'qrcode' => 'qr-code',
  100. 'question' => 'circle-question-mark',
  101. 'question-circle' => 'circle-question-mark',
  102. 'quote-right' => 'quote',
  103. 'remove' => 'x',
  104. 'search-location' => 'search-check',
  105. 'share-alt' => 'share-2',
  106. 'shield-alt' => 'shield',
  107. 'sign-in-alt' => 'log-in',
  108. 'sign-out-alt' => 'log-out',
  109. 'star-half-o' => 'star-half',
  110. 'stopwatch' => 'timer',
  111. 'swatchbook' => 'swatch-book',
  112. 'sync' => 'refresh-ccw',
  113. 'sync-alt' => 'refresh-ccw',
  114. 'tachometer-alt' => 'gauge',
  115. 'tasks' => 'square-check',
  116. 'th-large' => 'grid-2x2',
  117. 'thumbtack' => 'pin',
  118. 'times' => 'x',
  119. 'times-circle' => 'circle-x',
  120. 'toolbox' => 'tool-case',
  121. 'trash2' => 'trash-2',
  122. 'twitter' => 'log-in', //TODO: Brand icon. Update later.
  123. 'unlock-alt' => 'lock-open',
  124. 'user' => 'user-round',
  125. 'user-friends' => 'users',
  126. 'user-plus' => 'user-round-plus',
  127. 'user-secret' => 'user-key',
  128. 'user-slash' => 'user-x',
  129. 'user-shield' => 'user-shield', // Lucide has it now; keep the entry so the name is not remapped by a later rule
  130. 'user-tag' => 'user-star',
  131. 'user-check' => 'user-round-check',
  132. 'user-times' => 'user-x',
  133. 'users' => 'users-round',
  134. 'video-camera' => 'video',
  135. 'volume-up' => 'volume-2',
  136. 'volume-down' => 'volume-1',
  137. 'volume-mute' => 'volume-x',
  138. 'at' => 'at-sign',
  139. 'backspace' => 'delete',
  140. 'trash-alt' => 'trash',
  141. 'magic' => 'wand-sparkles',
  142. ];
  143. }
  144. public function getPreloaderJs()
  145. {
  146. return '{dir_plugins_public}lucide/|lucide.min.js';
  147. }
  148. public function getIcon($sIcon)
  149. {
  150. return bx_gen_method_name($this->_getIconName($sIcon), ['_', '-']);
  151. }
  152. public function getIconHtml($sIcon, $aAttrs = [])
  153. {
  154. $sName = $this->_getIconName($sIcon);
  155. if(!preg_match('/^[a-z0-9-]+$/', $sName))
  156. return false;
  157. if(!isset(self::$_aCacheSvg[$sName])) {
  158. $sPath = BX_DIRECTORY_PATH_PLUGINS_PUBLIC . 'lucide/icons/' . $sName . '.svg';
  159. $sSvg = file_exists($sPath) ? file_get_contents($sPath) : false;
  160. if($sSvg !== false) {
  161. $sSvg = preg_replace('/<!--.*?-->/s', '', $sSvg);
  162. $sSvg = trim(preg_replace('/\s+/', ' ', $sSvg));
  163. $sSvg = str_replace(' />', '/>', $sSvg);
  164. }
  165. self::$_aCacheSvg[$sName] = $sSvg;
  166. }
  167. $sSvg = self::$_aCacheSvg[$sName];
  168. if($sSvg === false)
  169. return false;
  170. $sClass = 'sys-icon';
  171. if(!empty($aAttrs['class']))
  172. $sClass .= ' ' . $aAttrs['class'];
  173. unset($aAttrs['class']);
  174. $sAttrs = '';
  175. foreach($aAttrs as $sKey => $sValue)
  176. $sAttrs .= ' ' . $sKey . '="' . bx_html_attribute($sValue) . '"';
  177. return preg_replace('/^<svg class="/', '<svg' . $sAttrs . ' class="' . bx_html_attribute($sClass) . ' ', $sSvg, 1);
  178. }
  179. /**
  180. * Get Lucide icon name (kebab-case) by Font Awesome or Lucide icon name.
  181. */
  182. protected function _getIconName($sIcon)
  183. {
  184. $sIcon = trim(preg_replace('/(sys-icon|far|col-\w+)/i', '', $sIcon));
  185. if(isset($this->_aMap[$sIcon]))
  186. $sIcon = $this->_aMap[$sIcon];
  187. return $sIcon;
  188. }
  189. public function getCode()
  190. {
  191. $sMap = json_encode($this->_aMap);
  192. $sCode = <<<BLAH
  193. function bx_iconset_init_lucide() {
  194. if(!window.lucide || !lucide.icons)
  195. return;
  196. const aMap = $sMap;
  197. document.querySelectorAll('i.sys-icon').forEach(el => {
  198. if(el.hasAttribute('data-lucide'))
  199. return;
  200. const sName = el.getAttribute('class').replace(/(sys-icon-bigger|sys-icon|fab|far|fas|col-\w+)/gi, '').trim().split(' ').shift();
  201. if(sName)
  202. el.setAttribute('data-lucide', aMap[sName] != undefined ? aMap[sName] : sName);
  203. else
  204. console.warn('Lucide: no icon class found on', el);
  205. });
  206. lucide.createIcons({
  207. attrs: { class: ['sys-icon'] },
  208. nameAttr: 'data-lucide'
  209. });
  210. };
  211. bx_iconset_init_lucide();
  212. if (typeof glOnProcessHtml === 'undefined')
  213. glOnProcessHtml = [];
  214. if (glOnProcessHtml instanceof Array) {
  215. glOnProcessHtml.push(function(e) {
  216. bx_iconset_init_lucide();
  217. });
  218. }
  219. BLAH;
  220. return $this->_oTemplate->_wrapInTagJsCode($sCode);
  221. }
  222. }
  223. /** @} */