HasElementNames.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace Encore\Admin\Grid;
  3. trait HasElementNames
  4. {
  5. /**
  6. * Grid name.
  7. *
  8. * @var string
  9. */
  10. protected $name;
  11. /**
  12. * HTML element names.
  13. *
  14. * @var array
  15. */
  16. protected $elementNames = [
  17. 'grid_row' => 'grid-row',
  18. 'grid_select_all' => 'grid-select-all',
  19. 'grid_per_page' => 'grid-per-pager',
  20. 'grid_batch' => 'grid-batch',
  21. 'export_selected' => 'export-selected',
  22. 'selected_rows' => 'selectedRows',
  23. ];
  24. /**
  25. * Set name to grid.
  26. *
  27. * @param string $name
  28. *
  29. * @return $this
  30. */
  31. public function setName($name)
  32. {
  33. $this->name = $name;
  34. $this->model()->setPerPageName("{$name}_{$this->model()->getPerPageName()}");
  35. $this->getFilter()->setName($name);
  36. return $this;
  37. }
  38. /**
  39. * Get name of grid.
  40. *
  41. * @return string
  42. */
  43. public function getName()
  44. {
  45. return $this->name;
  46. }
  47. /**
  48. * @return string
  49. */
  50. public function getGridRowName()
  51. {
  52. return $this->elementNameWithPrefix('grid_row');
  53. }
  54. /**
  55. * @return string
  56. */
  57. public function getSelectAllName()
  58. {
  59. return $this->elementNameWithPrefix('grid_select_all');
  60. }
  61. /**
  62. * @return string
  63. */
  64. public function getPerPageName()
  65. {
  66. return $this->elementNameWithPrefix('grid_per_page');
  67. }
  68. /**
  69. * @return string
  70. */
  71. public function getGridBatchName()
  72. {
  73. return $this->elementNameWithPrefix('grid_batch');
  74. }
  75. /**
  76. * @return string
  77. */
  78. public function getExportSelectedName()
  79. {
  80. return $this->elementNameWithPrefix('export_selected');
  81. }
  82. /**
  83. * @return string
  84. */
  85. public function getSelectedRowsName()
  86. {
  87. $elementName = $this->elementNames['selected_rows'];
  88. if ($this->name) {
  89. return sprintf('%s%s', $this->name, ucfirst($elementName));
  90. }
  91. return $elementName;
  92. }
  93. /**
  94. * @return string
  95. */
  96. protected function elementNameWithPrefix($name)
  97. {
  98. $elementName = $this->elementNames[$name];
  99. if ($this->name) {
  100. return sprintf('%s-%s', $this->name, $elementName);
  101. }
  102. return $elementName;
  103. }
  104. }