SplitButton.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Ui\Component\Control;
  7. /**
  8. * Class SplitButton
  9. *
  10. * @method string getTitle
  11. * @method string getLabel
  12. * @method string getButtonClass
  13. * @method string getId
  14. * @method string getClass
  15. * @method string getDataAttribute
  16. * @method string getStyle
  17. * @method string getDisabled
  18. * @method array getOptions
  19. * @method string getIdHard
  20. */
  21. class SplitButton extends Button
  22. {
  23. /**
  24. * {@inheritdoc}
  25. */
  26. protected function getTemplatePath()
  27. {
  28. return 'Magento_Ui::control/button/split.phtml';
  29. }
  30. /**
  31. * Retrieve <div> wrapper attributes html
  32. *
  33. * @return string
  34. */
  35. public function getAttributesHtml()
  36. {
  37. $classes = [];
  38. if (!($title = $this->getTitle())) {
  39. $title = $this->getLabel();
  40. }
  41. if ($this->hasSplit()) {
  42. $classes[] = 'actions-split';
  43. }
  44. if ($this->getClass()) {
  45. $classes[] = $this->getClass();
  46. }
  47. return $this->attributesToHtml(['title' => $title, 'class' => join(' ', $classes)]);
  48. }
  49. /**
  50. * Retrieve button attributes html
  51. *
  52. * @return string
  53. */
  54. public function getButtonAttributesHtml()
  55. {
  56. $disabled = $this->getDisabled() ? 'disabled' : '';
  57. $classes = ['action-default', 'primary'];
  58. if (!($title = $this->getTitle())) {
  59. $title = $this->getLabel();
  60. }
  61. if ($this->getButtonClass()) {
  62. $classes[] = $this->getButtonClass();
  63. }
  64. if ($disabled) {
  65. $classes[] = $disabled;
  66. }
  67. $attributes = [
  68. 'id' => $this->getId() . '-button',
  69. 'title' => $title,
  70. 'class' => join(' ', $classes),
  71. 'disabled' => $disabled,
  72. 'style' => $this->getStyle(),
  73. ];
  74. if (($idHard = $this->getIdHard())) {
  75. $attributes['id'] = $idHard;
  76. }
  77. //TODO perhaps we need to skip data-mage-init when disabled="disabled"
  78. if (($dataAttribute = $this->getDataAttribute())) {
  79. $this->getDataAttributes($dataAttribute, $attributes);
  80. }
  81. $html = $this->attributesToHtml($attributes);
  82. $html .= $this->getUiId();
  83. return $html;
  84. }
  85. /**
  86. * Retrieve toggle button attributes html
  87. *
  88. * @return string
  89. */
  90. public function getToggleAttributesHtml()
  91. {
  92. $disabled = $this->getDisabled() ? 'disabled' : '';
  93. $classes = ['action-toggle', 'primary'];
  94. if (!($title = $this->getTitle())) {
  95. $title = $this->getLabel();
  96. }
  97. if (($currentClass = $this->getClass())) {
  98. $classes[] = $currentClass;
  99. }
  100. if ($disabled) {
  101. $classes[] = $disabled;
  102. }
  103. $attributes = ['title' => $title, 'class' => join(' ', $classes), 'disabled' => $disabled];
  104. $this->getDataAttributes(['mage-init' => '{"dropdown": {}}', 'toggle' => 'dropdown'], $attributes);
  105. $html = $this->attributesToHtml($attributes);
  106. $html .= $this->getUiId('dropdown');
  107. return $html;
  108. }
  109. /**
  110. * Retrieve options attributes html
  111. *
  112. * @param string $key
  113. * @param array $option
  114. * @return string
  115. * @SuppressWarnings(PHPMD.NPathComplexity)
  116. */
  117. public function getOptionAttributesHtml($key, $option)
  118. {
  119. $disabled = !empty($option['disabled']) ? 'disabled' : '';
  120. $title = isset($option['title']) ? $option['title'] : $option['label'];
  121. $classes = ['item'];
  122. if (!empty($option['default'])) {
  123. $classes[] = 'item-default';
  124. }
  125. if ($disabled) {
  126. $classes[] = $disabled;
  127. }
  128. $attributes = $this->prepareOptionAttributes($option, $title, $classes, $disabled);
  129. $html = $this->attributesToHtml($attributes);
  130. $html .= $this->getUiId(isset($option['id']) ? $option['id'] : 'item' . '-' . $key);
  131. return $html;
  132. }
  133. /**
  134. * Prepare option attributes
  135. *
  136. * @param array $option
  137. * @param string $title
  138. * @param string $classes
  139. * @param string $disabled
  140. * @return array
  141. * @SuppressWarnings(PHPMD.NPathComplexity)
  142. */
  143. protected function prepareOptionAttributes($option, $title, $classes, $disabled)
  144. {
  145. $attributes = [
  146. 'id' => isset($option['id']) ? $this->getId() . '-' . $option['id'] : '',
  147. 'title' => $title,
  148. 'class' => join(' ', $classes),
  149. 'onclick' => isset($option['onclick']) ? $option['onclick'] : '',
  150. 'style' => isset($option['style']) ? $option['style'] : '',
  151. 'disabled' => $disabled,
  152. ];
  153. if (!empty($option['id_hard'])) {
  154. $attributes['id'] = $option['id_hard'];
  155. }
  156. if (isset($option['data_attribute'])) {
  157. $this->getDataAttributes($option['data_attribute'], $attributes);
  158. }
  159. return $attributes;
  160. }
  161. /**
  162. * Checks if the button needs actions-split functionality
  163. *
  164. * If this function returns false then split button will be rendered as simple button
  165. *
  166. * @return bool
  167. */
  168. public function hasSplit()
  169. {
  170. return $this->hasData('has_split') ? (bool)$this->getData('has_split') : true;
  171. }
  172. /**
  173. * Add data attributes to $attributes array
  174. *
  175. * @param array $data
  176. * @param array &$attributes
  177. * @return void
  178. */
  179. protected function getDataAttributes($data, &$attributes)
  180. {
  181. foreach ($data as $key => $attr) {
  182. $attributes['data-' . $key] = is_scalar($attr) ? $attr : json_encode($attr);
  183. }
  184. }
  185. }