steps.ext.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. require('./idealsteps');
  2. module.exports = {
  3. name: 'steps',
  4. options: {
  5. stepsContainer: '.idealsteps-container',
  6. stepsOptions: {}
  7. },
  8. methods: {
  9. // @extend
  10. _init: function() {
  11. this._buildSteps();
  12. },
  13. // @extend
  14. _validate: function() {
  15. var self = this;
  16. this._updateSteps();
  17. if ($.idealforms.hasExtension('idealAjax')) {
  18. $.each($.idealforms._requests, function(key, request) {
  19. request.done(function(){ self._updateSteps() });
  20. });
  21. }
  22. },
  23. // @extend
  24. focusFirstInvalid: function(firstInvalid) {
  25. var self = this;
  26. this.$stepsContainer.idealsteps('go', function() {
  27. return this.$steps.filter(function() {
  28. return $(this).find(firstInvalid).length;
  29. }).index();
  30. });
  31. },
  32. _buildSteps: function() {
  33. var self = this, options
  34. , hasRules = ! $.isEmptyObject(this.opts.rules)
  35. , counter = hasRules
  36. ? '<span class="counter"/>'
  37. : '<span class="counter zero">0</span>';
  38. options = $.extend({}, {
  39. buildNavItems: function(i){ return 'Step '+ (i+1) + counter }
  40. }, this.opts.stepsOptions);
  41. this.$stepsContainer = this.$form.closest(this.opts.stepsContainer).idealsteps(options);
  42. },
  43. _updateSteps: function() {
  44. var self = this;
  45. this.$stepsContainer.idealsteps('_inject', function() {
  46. var idealsteps = this;
  47. this.$navItems.each(function(i) {
  48. var invalid = idealsteps.$steps.eq(i).find(self.getInvalid()).length;
  49. $(this).find('span').text(invalid).toggleClass('zero', ! invalid);
  50. });
  51. });
  52. },
  53. // @extend
  54. addRules: function() {
  55. this.firstStep();
  56. },
  57. // @extend
  58. toggleFields: function() {
  59. this._updateSteps();
  60. },
  61. // @extend
  62. removeFields: function() {
  63. this._updateSteps();
  64. },
  65. goToStep: function(idx) {
  66. this.$stepsContainer.idealsteps('go', idx);
  67. },
  68. prevStep: function() {
  69. this.$stepsContainer.idealsteps('prev');
  70. },
  71. nextStep: function() {
  72. this.$stepsContainer.idealsteps('next');
  73. },
  74. firstStep: function() {
  75. this.$stepsContainer.idealsteps('first');
  76. },
  77. lastStep: function() {
  78. this.$stepsContainer.idealsteps('last');
  79. }
  80. }
  81. };