BxBaseIconsetLucide.php 8.5 KB

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