steps.ext.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. },
  47. _buildSteps: function() {
  48. var self = this, options
  49. , hasRules = ! $.isEmptyObject(this.opts.rules)
  50. , buildNavItems = this.opts.steps.buildNavItems
  51. , counter = hasRules
  52. ? '<span class="counter"/>'
  53. : '<span class="counter zero">0</span>';
  54. if (this.opts.steps.buildNavItems) {
  55. this.opts.steps.buildNavItems = function(i) {
  56. return buildNavItems.call(self, i) + counter;
  57. };
  58. }
  59. this.$stepsContainer = this.$form
  60. .closest(this.opts.steps.container)
  61. .idealsteps(this.opts.steps);
  62. },
  63. _updateSteps: function() {
  64. var self = this;
  65. this.$stepsContainer.idealsteps('_inject', function() {
  66. var idealsteps = this;
  67. this.$navItems.each(function(i) {
  68. var invalid = idealsteps.$steps.eq(i).find(self.getInvalid()).length;
  69. $(this).find('span').text(invalid).toggleClass('zero', ! invalid);
  70. });
  71. });
  72. },
  73. // @extend
  74. addRules: function() {
  75. this.firstStep();
  76. },
  77. // @extend
  78. addFields: function(field) {
  79. field.after = this.$stepsContainer
  80. .find(this.opts.steps.step)
  81. .eq(field.appendToStep)
  82. .find('input, textarea, select')
  83. .last()[0].name;
  84. },
  85. // @extend
  86. toggleFields: function() {
  87. this._updateSteps();
  88. },
  89. // @extend
  90. removeFields: function() {
  91. this._updateSteps();
  92. },
  93. goToStep: function(idx) {
  94. this.$stepsContainer.idealsteps('go', idx);
  95. },
  96. prevStep: function() {
  97. this.$stepsContainer.idealsteps('prev');
  98. },
  99. nextStep: function() {
  100. this.$stepsContainer.idealsteps('next');
  101. },
  102. firstStep: function() {
  103. this.$stepsContainer.idealsteps('first');
  104. },
  105. lastStep: function() {
  106. this.$stepsContainer.idealsteps('last');
  107. }
  108. }
  109. };