steps.blade.php 8.4 KB

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