steps.ext.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. require('./idealsteps');
  2. module.exports = {
  3. name: 'steps',
  4. options: {
  5. steps: {
  6. container: '.idealsteps-container',
  7. nav: '.idealsteps-nav',
  8. navItems: 'li',
  9. buildNavItems: function(i) {
  10. return this.opts.steps.i18n.step +' '+ (i+1);
  11. },
  12. wrap: '.idealsteps-wrap',
  13. step: '.idealsteps-step',
  14. activeClass: 'idealsteps-step-active',
  15. before: $.noop,
  16. after: $.noop,
  17. fadeSpeed: 0,
  18. i18n: {
  19. step: 'Step'
  20. }
  21. }
  22. },
  23. methods: {
  24. // @extend
  25. _init: function() {
  26. this._buildSteps();
  27. },
  28. // @extend
  29. _validate: function() {
  30. var self = this;
  31. this._updateSteps();
  32. if (this._hasExtension('ajax')) {
  33. $.each($.idealforms._requests, function(key, request) {
  34. request.done(function(){ self._updateSteps() });
  35. });
  36. }
  37. },
  38. // @extend
  39. focusFirstInvalid: function(firstInvalid) {
  40. var self = this;
  41. this.$stepsContainer.idealsteps('go', function() {
  42. return this.$steps.filter(function() {
  43. return $(this).find(firstInvalid).length;
  44. }).index();
  45. });
  46. setTimeout(function(){ $(firstInvalid).focus() }, this.opts.steps.fadeSpeed);
  47. },
  48. _buildSteps: function() {
  49. var self = this, options
  50. , hasRules = ! $.isEmptyObject(this.opts.rules)
  51. , buildNavItems = this.opts.steps.buildNavItems
  52. , counter = hasRules
  53. ? '<span class="counter"/>'
  54. : '<span class="counter zero">0</span>';
  55. if (this.opts.steps.buildNavItems) {
  56. this.opts.steps.buildNavItems = function(i) {
  57. return buildNavItems.call(self, i) + counter;
  58. };
  59. }
  60. this.$stepsContainer = this.$form
  61. .closest(this.opts.steps.container)
  62. .idealsteps(this.opts.steps);
  63. },
  64. _updateSteps: function() {
  65. var self = this;
  66. this.$stepsContainer.idealsteps('_inject', function() {
  67. var idealsteps = this;
  68. this.$navItems.each(function(i) {
  69. var invalid = idealsteps.$steps.eq(i).find(self.getInvalid()).length;
  70. $(this).find('span').text(invalid).toggleClass('zero', ! invalid);
  71. });
  72. });
  73. },
  74. // @extend
  75. addRules: function() {
  76. this.firstStep();
  77. },
  78. // @extend
  79. addFields: function(field) {
  80. var $steps = this.$stepsContainer.find(this.opts.steps.step);
  81. field.after = $steps
  82. .eq(field.appendToStep || $steps.length-1)
  83. .find('input, select, textarea')
  84. .last()[0].name;
  85. },
  86. // @extend
  87. toggleFields: function() {
  88. this._updateSteps();
  89. },
  90. // @extend
  91. removeFields: function() {
  92. this._updateSteps();
  93. },
  94. goToStep: function(idx) {
  95. this.$stepsContainer.idealsteps('go', idx);
  96. },
  97. prevStep: function() {
  98. this.$stepsContainer.idealsteps('prev');
  99. },
  100. nextStep: function() {
  101. this.$stepsContainer.idealsteps('next');
  102. },
  103. firstStep: function() {
  104. this.$stepsContainer.idealsteps('first');
  105. },
  106. lastStep: function() {
  107. this.$stepsContainer.idealsteps('last');
  108. }
  109. }
  110. };