steps.blade.php 9.5 KB

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