BxBaseIconsetLucide.php 8.7 KB

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