steps.blade.php 9.7 KB

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