IconPickerWidget.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: ljt
  5. * DateTime: 2016/11/4 14:15
  6. * Description:
  7. */
  8. namespace backend\widgets\iconpicker;
  9. use yii\helpers\ArrayHelper;
  10. use yii\helpers\Html;
  11. use yii\helpers\Json;
  12. use yii\web\JsExpression;
  13. use yii\widgets\InputWidget;
  14. class IconPickerWidget extends InputWidget
  15. {
  16. /**
  17. * @var string $iconset - one of allowed variants
  18. * glyphicon|ionicon|fontawesome|weathericon|mapicon|octicon|typicon|elusiveicon|materialdesign
  19. */
  20. public $iconset = 'fontawesome';
  21. /**
  22. * @var array $pickerOptions additional html options for picker button
  23. */
  24. public $pickerOptions = ['class' => 'btn btn-default btn-sm'];
  25. /**
  26. * @var array $containerOptions additional html options for container
  27. */
  28. public $containerOptions = [];
  29. /**
  30. * @var array $clientOptions - iconpicker options
  31. * (will be merged with defaultOptions @see getDefaultOptions() , so you can set only overrides)
  32. * @see http://victor-valencia.github.io/bootstrap-iconpicker/
  33. **/
  34. public $clientOptions
  35. = [
  36. 'rows' => 5,
  37. 'columns' => 10,
  38. 'placement' => 'right',
  39. 'align' => 'center',
  40. 'arrowClass' => 'btn-primary',
  41. 'header' => true,
  42. 'footer' => true,
  43. 'labelHeader' => '{0} / {1}',
  44. 'labelFooter' => '{0} - {1}:[{2}]',
  45. 'search' => true,
  46. 'searchText' => 'Search icon',
  47. 'selectedClass' => 'btn-warning',
  48. 'unselectedClass' => 'btn-default',
  49. ];
  50. /**
  51. * @var JsExpression $onSelectIconCallback
  52. * @example
  53. * onSelectIconCallback=>new JsExpression('function(e){
  54. * var icon = e.icon;
  55. * icon = "some "+icon;
  56. * $('#target').val(icon);
  57. * }'),
  58. */
  59. public $onSelectIconCallback;
  60. /**
  61. * @var
  62. */
  63. private $_id;
  64. /**
  65. * @var string
  66. */
  67. private $_default = '';
  68. /**
  69. *
  70. */
  71. public function init()
  72. {
  73. if (!isset($this->options['id']) && !$this->hasModel()) {
  74. $this->options['id'] = 'iconpicker_' . $this->getId();
  75. }
  76. parent::init();
  77. $this->_id = $this->options['id'];
  78. if ($this->hasModel() && !empty($this->model->{$this->attribute})) {
  79. $this->_default = $this->pickerOptions['data-icon'] = $this->model->{$this->attribute};
  80. }
  81. if (!$this->hasModel() && !empty($this->value)) {
  82. $this->_default = $this->pickerOptions['data-icon'] = $this->value;
  83. }
  84. if (!isset($this->pickerOptions['id'])) {
  85. $this->pickerOptions['id'] = $this->_id . '_jspicker';
  86. }
  87. $this->clientOptions = ArrayHelper::merge($this->getDefaultOptions(), $this->clientOptions);
  88. $this->registerAssets();
  89. }
  90. /**
  91. * Default js-plugin options
  92. *
  93. * @return array
  94. **/
  95. protected function getDefaultOptions()
  96. {
  97. return [
  98. 'iconset' => $this->iconset,
  99. 'rows' => 5,
  100. 'columns' => 10,
  101. 'placement' => 'right',
  102. 'align' => 'center',
  103. 'arrowClass' => 'btn-primary',
  104. 'header' => true,
  105. 'footer' => true,
  106. 'labelHeader' => '{0} / {1}',
  107. 'labelFooter' => '{0} - {1}:[{2}]',
  108. 'search' => true,
  109. 'searchText' => 'SEARCH_ICON',
  110. 'selectedClass' => 'btn-warning',
  111. 'unselectedClass' => 'btn-default',
  112. ];
  113. }
  114. /**
  115. * Registers the needed assets
  116. */
  117. public function registerAssets()
  118. {
  119. $view = $this->getView();
  120. $targetId = $this->_id;
  121. $iconPickerId = $this->pickerOptions['id'];
  122. IconPickerAsset::register($this->view);
  123. $this->clientOptions = ArrayHelper::merge($this->clientOptions, [
  124. 'icon' => $this->_default,
  125. ]);
  126. $this->clientOptions = Json::encode($this->clientOptions);
  127. $js[] = <<<JS
  128. $("#{$iconPickerId}").iconpicker({$this->clientOptions});
  129. JS;
  130. $callback = null;
  131. if (!empty($this->onSelectIconCallback)) {
  132. $callback = $this->onSelectIconCallback instanceof JsExpression
  133. ? $this->onSelectIconCallback->__toString()
  134. : $this->onSelectIconCallback;
  135. }
  136. $js[] = ($callback)
  137. ? <<<JS
  138. $("#{$iconPickerId}").on('change', function(e) {
  139. var callback = {$callback};
  140. callback(e);
  141. });
  142. JS
  143. :
  144. <<<JS
  145. $("#{$iconPickerId}").on('change', function(e) {
  146. $('#$targetId').val(e.icon);
  147. });
  148. JS;
  149. $view->registerJs(implode("\n", $js));
  150. }
  151. /**
  152. * @return string bootstrap-picker button with hiddenInput field where we put selected value
  153. */
  154. public function run()
  155. {
  156. if ($this->hasModel()) {
  157. $inp = Html::activeHiddenInput($this->model, $this->attribute, $this->options);
  158. } else {
  159. $inp = Html::hiddenInput($this->name, $this->value, $this->options);
  160. }
  161. $picker = Html::button('CHOOSE_ICON', $this->pickerOptions);
  162. return Html::tag('div', $picker . $inp, $this->containerOptions);
  163. }
  164. }