海南旅游SAAS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
3.4 KiB

  1. <?php
  2. namespace App\Admin\Extensions\Form;
  3. use Dcat\Admin\Form\Field\Radio;
  4. use Dcat\Admin\Widgets\Radio as WidgetRadio;
  5. /**
  6. * 由于信息收集表单中,hasMany中的radio存在多个时出现问题,故重写覆盖
  7. * Class Radio2
  8. * @package App\Admin\Extensions\Form
  9. */
  10. class Radio2 extends Radio
  11. {
  12. protected $view = 'admin::form.radio';
  13. /**
  14. * {@inheritdoc}
  15. */
  16. public function render()
  17. {
  18. if ($this->options instanceof \Closure) {
  19. $this->options(
  20. $this->options->call($this->values(), $this->value(), $this)
  21. );
  22. }
  23. $this->addCascadeScript();
  24. $radio = WidgetRadio::make($this->getElementName(), $this->options, $this->style);
  25. if ($this->attributes['disabled'] ?? false) {
  26. $radio->disable();
  27. }
  28. $radio
  29. ->inline($this->inline)
  30. ->check($this->value())
  31. ->class($this->getElementClassString())
  32. ->size($this->size);
  33. $this->addVariables([
  34. 'radio' => $radio,
  35. ]);
  36. return parent::render();
  37. }
  38. /**
  39. * Add cascade scripts to contents.
  40. *
  41. * @return string
  42. */
  43. protected function getCascadeScript()
  44. {
  45. if (empty($this->conditions)) {
  46. return;
  47. }
  48. $cascadeGroups = collect($this->conditions)->map(function ($condition) {
  49. return [
  50. 'class' => $this->getCascadeClass($condition['value'], $condition['operator']),
  51. 'operator' => $condition['operator'],
  52. 'value' => $condition['value'],
  53. ];
  54. })->toJson();
  55. return <<<JS
  56. (function () {
  57. var compare = function (a, b, o) {
  58. if (! $.isArray(b)) {
  59. return operator_table[o](a, b)
  60. }
  61. if (o === '!=') {
  62. var result = true;
  63. for (var i in b) {
  64. if (! operator_table[o](a, b[i])) {
  65. result = false;
  66. break;
  67. }
  68. }
  69. return result;
  70. }
  71. for (var i in b) {
  72. if (operator_table[o](a, b[i])) {
  73. return true;
  74. }
  75. }
  76. };
  77. var operator_table = {
  78. '=': function(a, b) {
  79. if ($.isArray(a) && $.isArray(b)) {
  80. return $(a).not(b).length === 0 && $(b).not(a).length === 0;
  81. }
  82. return String(a) === String(b);
  83. },
  84. '>': function(a, b) {
  85. return a > b;
  86. },
  87. '<': function(a, b) {
  88. return a < b;
  89. },
  90. '>=': function(a, b) { return a >= b; },
  91. '<=': function(a, b) { return a <= b; },
  92. '!=': function(a, b) {
  93. return ! operator_table['='](a, b);
  94. },
  95. 'in': function(a, b) { return Dcat.helpers.inObject(a, String(b), true); },
  96. 'notIn': function(a, b) { return ! Dcat.helpers.inObject(a, String(b), true); },
  97. 'has': function(a, b) { return Dcat.helpers.inObject(b, String(b), true); },
  98. };
  99. var cascade_groups = {$cascadeGroups}, event = '{$this->cascadeEvent}';
  100. \$this.on(event, function (e) {
  101. {$this->getFormFrontValue()}
  102. cascade_groups.forEach(function (event) {
  103. var group = $(e.target).closest('.form-group.form-field').next('div.cascade-group.'+event.class);
  104. if (compare(checked, event.value, event.operator)) {
  105. group.removeClass('d-none');
  106. } else {
  107. group.addClass('d-none');
  108. }
  109. });
  110. }).trigger(event);
  111. })();
  112. JS;
  113. }
  114. /**
  115. * @return string
  116. */
  117. protected function getFormFrontValue()
  118. {
  119. return <<<JS
  120. var checked = $('{$this->getElementClassSelector()}:checked').val();
  121. JS;
  122. }
  123. }