Icon.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace Dcat\Admin\Form\Field;
  3. class Icon extends Text
  4. {
  5. public static $js = '@fontawesome-iconpicker';
  6. public static $css = '@fontawesome-iconpicker';
  7. public function render()
  8. {
  9. $this->addScript();
  10. $this->prepend("<i class='fa {$this->value()}'>&nbsp;</i>")
  11. ->defaultAttribute('autocomplete', 'off')
  12. ->defaultAttribute('style', 'width: 160px;flex:none');
  13. return parent::render();
  14. }
  15. protected function addScript()
  16. {
  17. $this->script = <<<JS
  18. setTimeout(function () {
  19. var field = $('{$this->getElementClassSelector()}'),
  20. parent = field.parents('.form-field'),
  21. showIcon = function (icon) {
  22. parent.find('.input-group-prepend .input-group-text').html('<i class="' + icon + '"></i>');
  23. };
  24. field.iconpicker({placement:'bottomLeft', animation: false});
  25. parent.find('.iconpicker-item').on('click', function (e) {
  26. showIcon($(this).find('i').attr('class'));
  27. });
  28. field.on('keyup', function (e) {
  29. var val = $(this).val();
  30. if (val.indexOf('fa-') !== -1) {
  31. if (val.indexOf('fa ') === -1) {
  32. val = 'fa ' + val;
  33. }
  34. }
  35. showIcon(val);
  36. })
  37. }, 1);
  38. JS;
  39. }
  40. }