steps.blade.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. @if($showHeader)
  2. <div class="box-header with-border">
  3. <h3 class="box-title" style="line-height:30px">{!! $form->title() !!}</h3>
  4. <div class="pull-right">{!! $form->renderTools() !!}</div>
  5. </div>
  6. @endif
  7. <div class="box-body">
  8. @if($steps->count())
  9. <div class="fields-group dcat-step-box" style="padding: {{ $steps->getOption('padding') }};max-width: {{ $steps->getOption('width') }}">
  10. <ul class="dcat-step-horizontal dcat-step-label-horizontal dcat-step ">
  11. @foreach($steps->all() as $step)
  12. <li class="dcat-step-item">
  13. <a href="#{{ $step->getElementId() }}" class="dcat-step-item-container">
  14. <div class="dcat-step-line"></div>
  15. <div class="dcat-step-icons">
  16. <span class="dcat-step-icon" data-index="{{ $step->index() }}">{{ $step->index() + 1 }}</span>
  17. </div>
  18. <div class="dcat-step-content">
  19. <div class="dcat-step-title">{!! $step->title() !!}</div>
  20. <div class="dcat-step-desc"> {{ $step->description() }} </div>
  21. </div>
  22. </a>
  23. </li>
  24. @endforeach
  25. <li class="dcat-step-item">
  26. <a href="#{{ $steps->done()->getElementId() }}" class="dcat-step-item-container">
  27. <div class="dcat-step-line"></div>
  28. <div class="dcat-step-icons">
  29. <span class="dcat-step-icon" data-index="{{ $steps->count() }}"> {{ $steps->count() + 1 }} </span>
  30. </div>
  31. <div class="dcat-step-content">
  32. <div class="dcat-step-title">{{ $steps->done()->title() }}</div>
  33. <div class="dcat-step-desc"></div>
  34. </div>
  35. </a>
  36. </li>
  37. </ul>
  38. <div class="dcat-step-form">
  39. {!! $steps->build() !!}
  40. <div id="{{ $steps->done()->getElementId() }}" class="dcat-done-step" style="display: none;">
  41. </div>
  42. </div>
  43. </div>
  44. @endif
  45. </div>
  46. @foreach($form->hiddenFields() as $field)
  47. {!! $field->render() !!}
  48. @endforeach
  49. <input type="hidden" class="current-step-input" name="{{ Dcat\Admin\Form\Step\Builder::CURRENT_VALIDATION_STEP }}" />
  50. <input type="hidden" class="all-steps-input" name="{{ Dcat\Admin\Form\Step\Builder::ALL_STEPS }}" />
  51. <input type="hidden" name="_token" value="{{ csrf_token() }}">
  52. @php
  53. $lastStep = $step;
  54. @endphp
  55. <script>
  56. Dcat.ready(function () {
  57. var form = $('#{{ $form->getElementId() }}'),
  58. box = form.find('.dcat-step-box'),
  59. stepInput = form.find('.current-step-input'),
  60. allStepInput = form.find('.all-steps-input'),
  61. smartWizard,
  62. isSubmitting;
  63. var submitBtn = $('<button style="margin-left: 10px"></button>')
  64. .text('{{ trans('admin.submit') }}')
  65. .addClass('btn btn-primary step-submit-btn disabled d-none')
  66. .on('click', function(){
  67. var $t = $(this);
  68. if ($t.hasClass('disabled') || isSubmitting) {
  69. return false;
  70. }
  71. form.validator('validate');
  72. if (form.find('.has-error').length > 0) {
  73. return false;
  74. }
  75. allStepInput.val("1");
  76. stepInput.val("");
  77. $t.buttonLoading().removeClass('waves-effect');
  78. isSubmitting = 1;
  79. // 提交完整表单
  80. submit(function (state, data) {
  81. $t.buttonLoading(false);
  82. isSubmitting = 0;
  83. if (state) {
  84. if (data) {
  85. form.find('.dcat-done-step').html(data);
  86. }
  87. smartWizard.next();
  88. toggleBtn();
  89. }
  90. });
  91. return false;
  92. });
  93. smartWizard = box.smartWizard({
  94. selected: {{ $steps->getOption('selected') }},
  95. transitionEffect: 'fade',
  96. useURLhash: false,
  97. keyNavigation: false,
  98. showStepURLhash: false,
  99. autoAdjustHeight: false,
  100. lang: {
  101. next: '{!! trans('admin.next_step') !!}',
  102. previous: '{!! trans('admin.prev_step') !!}'
  103. },
  104. toolbarSettings: {
  105. toolbarPosition: 'bottom',
  106. toolbarExtraButtons: [submitBtn,],
  107. toolbarButtonPosition: 'left'
  108. },
  109. anchorSettings: {
  110. removeDoneStepOnNavigateBack: true,
  111. enableAnchorOnDoneStep: false,
  112. },
  113. }).on('leaveStep', function (e, tab, idx, direction) {
  114. @if ($leaving = $steps->getOption('leaving'))
  115. var callbacks = [];
  116. @foreach($leaving as $fun)
  117. callbacks.push({!! $fun !!});
  118. @endforeach
  119. return callListeners(callbacks, buildArgs(e, tab, idx, direction));
  120. @endif
  121. }).on('showStep', function (e, tab, idx, direction) {
  122. @if ($shown = $steps->getOption('shown'))
  123. var callbacks = [];
  124. @foreach($shown as $fun)
  125. callbacks.push({!! $fun !!});
  126. @endforeach
  127. return callListeners(callbacks, buildArgs(e, tab, idx, direction));
  128. @endif
  129. });
  130. @if ($steps->getOption('leaving') || $steps->getOption('shown'))
  131. // 执行回调函数
  132. function callListeners(func, args) {
  133. for (var i in func) {
  134. if (func[i](args) === false) {
  135. return false;
  136. }
  137. }
  138. }
  139. // 获取步骤表单
  140. function getForm(idx) {
  141. return box.find('.dcat-step-form [data-toggle="validator"]').eq(idx);
  142. }
  143. // 构建参数
  144. function buildArgs(e, tab, idx, direction) {
  145. return {
  146. event: e,
  147. tab: tab,
  148. index: idx,
  149. direction: direction,
  150. form: getForm(idx),
  151. getFrom: function (idx) {
  152. return getForm(idx)
  153. },
  154. formArray: getForm(idx).formToArray(),
  155. getFormArray: function (idx) {
  156. return getForm(idx).formToArray();
  157. }
  158. };
  159. }
  160. @endif
  161. smartWizard = smartWizard.data('smartWizard');
  162. // 上一步
  163. var prev = box.find('.sw-btn-prev').click(function (e) {
  164. e.preventDefault();
  165. if (smartWizard.steps.index(this) !== smartWizard.current_index) {
  166. smartWizard.prev();
  167. }
  168. toggleBtn();
  169. });
  170. // 下一步
  171. var next = box.find('.sw-btn-next').click(function (e) {
  172. e.preventDefault();
  173. if ($(this).hasClass('disabled') || isSubmitting) {
  174. return false;
  175. }
  176. var stepForm = form.find('.sw-container [data-toggle="validator"]').eq(smartWizard.current_index);
  177. stepForm.validator('validate');
  178. if (stepForm.find('.has-error').length > 0) {
  179. return false;
  180. }
  181. var self = this;
  182. $(self).buttonLoading().removeClass('waves-effect');
  183. isSubmitting = 1;
  184. // 发送表单到服务器进行验证
  185. stepInput.val(smartWizard.current_index);
  186. submit(function (state) {
  187. $(self).buttonLoading(false);
  188. isSubmitting = 0;
  189. if (state) {
  190. // 表单验证成功
  191. if (smartWizard.steps.index(self) !== smartWizard.current_index) {
  192. smartWizard.next();
  193. }
  194. toggleBtn();
  195. }
  196. });
  197. });
  198. // 提交表单
  199. function submit(after) {
  200. Dcat.Form({
  201. form: form,
  202. after: function (state, b, c, d) {
  203. after(state, b, c, d);
  204. if (state) {
  205. return false;
  206. }
  207. }
  208. });
  209. }
  210. // 按钮显示隐藏切换
  211. function toggleBtn() {
  212. var last = {{ $lastStep->index() }},
  213. sbm = box.find('.step-submit-btn');
  214. if (smartWizard.current_index == last) {
  215. sbm.removeClass('disabled d-none');
  216. next.hide();
  217. prev.show();
  218. } else {
  219. sbm.addClass('disabled d-none');
  220. if (smartWizard.current_index !== 0) {
  221. prev.show();
  222. } else {
  223. prev.hide();
  224. }
  225. if (smartWizard.current_index != (last + 1)) {
  226. next.show()
  227. }
  228. }
  229. if (smartWizard.current_index == (last + 1)) {
  230. box.find('.sw-btn-group').remove()
  231. }
  232. }
  233. toggleBtn();
  234. });
  235. </script>