be_forms_wizard.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Document : be_forms_wizard.js
  3. * Author : pixelcave
  4. * Description: Custom JS code used in Form Wizard Page
  5. */
  6. var BeFormWizard = function() {
  7. // Init Wizard defaults
  8. var initWizardDefaults = function(){
  9. jQuery.fn.bootstrapWizard.defaults.tabClass = 'nav nav-tabs';
  10. jQuery.fn.bootstrapWizard.defaults.nextSelector = '[data-wizard="next"]';
  11. jQuery.fn.bootstrapWizard.defaults.previousSelector = '[data-wizard="prev"]';
  12. jQuery.fn.bootstrapWizard.defaults.firstSelector = '[data-wizard="first"]';
  13. jQuery.fn.bootstrapWizard.defaults.lastSelector = '[data-wizard="lsat"]';
  14. jQuery.fn.bootstrapWizard.defaults.finishSelector = '[data-wizard="finish"]';
  15. jQuery.fn.bootstrapWizard.defaults.backSelector = '[data-wizard="back"]';
  16. };
  17. // Init simple wizard, for more examples you can check out https://github.com/VinceG/twitter-bootstrap-wizard
  18. var initWizardSimple = function(){
  19. jQuery('.js-wizard-simple').bootstrapWizard({
  20. onTabShow: function(tab, navigation, index) {
  21. var percent = ((index + 1) / navigation.find('li').length) * 100;
  22. // Get progress bar
  23. var progress = navigation.parents('.block').find('[data-wizard="progress"] > .progress-bar');
  24. // Update progress bar if there is one
  25. if (progress.length) {
  26. progress.css({ width: percent + 1 + '%' });
  27. }
  28. }
  29. });
  30. };
  31. // Init wizards with validation, for more examples you can check out https://github.com/VinceG/twitter-bootstrap-wizard
  32. // and https://github.com/jzaefferer/jquery-validation
  33. var initWizardValidation = function(){
  34. // Get forms
  35. var formClassic = jQuery('.js-wizard-validation-classic-form');
  36. var formMaterial = jQuery('.js-wizard-validation-material-form');
  37. // Prevent forms from submitting on enter key press
  38. formClassic.add(formMaterial).on('keyup keypress', function (e) {
  39. var code = e.keyCode || e.which;
  40. if (code === 13) {
  41. e.preventDefault();
  42. return false;
  43. }
  44. });
  45. // Init form validation on classic wizard form
  46. var validatorClassic = formClassic.validate({
  47. errorClass: 'invalid-feedback animated fadeInDown',
  48. errorElement: 'div',
  49. errorPlacement: function(error, e) {
  50. jQuery(e).parents('.form-group').append(error);
  51. },
  52. highlight: function(e) {
  53. jQuery(e).closest('.form-group').removeClass('is-invalid').addClass('is-invalid');
  54. },
  55. success: function(e) {
  56. jQuery(e).closest('.form-group').removeClass('is-invalid');
  57. jQuery(e).remove();
  58. },
  59. rules: {
  60. 'wizard-validation-classic-firstname': {
  61. required: true,
  62. minlength: 2
  63. },
  64. 'wizard-validation-classic-lastname': {
  65. required: true,
  66. minlength: 2
  67. },
  68. 'wizard-validation-classic-email': {
  69. required: true,
  70. email: true
  71. },
  72. 'wizard-validation-classic-bio': {
  73. required: true,
  74. minlength: 5
  75. },
  76. 'wizard-validation-classic-location': {
  77. required: true
  78. },
  79. 'wizard-validation-classic-skills': {
  80. required: true
  81. },
  82. 'wizard-validation-classic-terms': {
  83. required: true
  84. }
  85. },
  86. messages: {
  87. 'wizard-validation-classic-firstname': {
  88. required: 'Please enter a firstname',
  89. minlength: 'Your firtname must consist of at least 2 characters'
  90. },
  91. 'wizard-validation-classic-lastname': {
  92. required: 'Please enter a lastname',
  93. minlength: 'Your lastname must consist of at least 2 characters'
  94. },
  95. 'wizard-validation-classic-email': 'Please enter a valid email address',
  96. 'wizard-validation-classic-bio': 'Let us know a few thing about yourself',
  97. 'wizard-validation-classic-skills': 'Please select a skill!',
  98. 'wizard-validation-classic-terms': 'You must agree to the service terms!'
  99. }
  100. });
  101. // Init form validation on material wizard form
  102. var validatorMaterial = formMaterial.validate({
  103. errorClass: 'invalid-feedback animated fadeInDown',
  104. errorElement: 'div',
  105. errorPlacement: function(error, e) {
  106. jQuery(e).parents('.form-group').append(error);
  107. },
  108. highlight: function(e) {
  109. jQuery(e).closest('.form-group').removeClass('is-invalid').addClass('is-invalid');
  110. },
  111. success: function(e) {
  112. jQuery(e).closest('.form-group').removeClass('is-invalid');
  113. jQuery(e).remove();
  114. },
  115. rules: {
  116. 'wizard-validation-material-firstname': {
  117. required: true,
  118. minlength: 2
  119. },
  120. 'wizard-validation-material-lastname': {
  121. required: true,
  122. minlength: 2
  123. },
  124. 'wizard-validation-material-email': {
  125. required: true,
  126. email: true
  127. },
  128. 'wizard-validation-material-bio': {
  129. required: true,
  130. minlength: 5
  131. },
  132. 'wizard-validation-material-location': {
  133. required: true
  134. },
  135. 'wizard-validation-material-skills': {
  136. required: true
  137. },
  138. 'wizard-validation-material-terms': {
  139. required: true
  140. }
  141. },
  142. messages: {
  143. 'wizard-validation-material-firstname': {
  144. required: 'Please enter a firstname',
  145. minlength: 'Your firtname must consist of at least 2 characters'
  146. },
  147. 'wizard-validation-material-lastname': {
  148. required: 'Please enter a lastname',
  149. minlength: 'Your lastname must consist of at least 2 characters'
  150. },
  151. 'wizard-validation-material-email': 'Please enter a valid email address',
  152. 'wizard-validation-material-bio': 'Let us know a few thing about yourself',
  153. 'wizard-validation-material-skills': 'Please select a skill!',
  154. 'wizard-validation-material-terms': 'You must agree to the service terms!'
  155. }
  156. });
  157. // Init classic wizard with validation
  158. jQuery('.js-wizard-validation-classic').bootstrapWizard({
  159. tabClass: '',
  160. onTabShow: function(tab, navigation, index) {
  161. var percent = ((index + 1) / navigation.find('li').length) * 100;
  162. // Get progress bar
  163. var progress = navigation.parents('.block').find('[data-wizard="progress"] > .progress-bar');
  164. // Update progress bar if there is one
  165. if (progress.length) {
  166. progress.css({ width: percent + 1 + '%' });
  167. }
  168. },
  169. onNext: function(tab, navigation, index) {
  170. if( !formClassic.valid() ) {
  171. validatorClassic.focusInvalid();
  172. return false;
  173. }
  174. },
  175. onTabClick: function(tab, navigation, index) {
  176. jQuery('a', navigation).blur();
  177. return false;
  178. }
  179. });
  180. // Init wizard with validation
  181. jQuery('.js-wizard-validation-material').bootstrapWizard({
  182. tabClass: '',
  183. onTabShow: function(tab, navigation, index) {
  184. var percent = ((index + 1) / navigation.find('li').length) * 100;
  185. // Get progress bar
  186. var progress = navigation.parents('.block').find('[data-wizard="progress"] > .progress-bar');
  187. // Update progress bar if there is one
  188. if (progress.length) {
  189. progress.css({ width: percent + 1 + '%' });
  190. }
  191. },
  192. onNext: function(tab, navigation, index) {
  193. if( !formMaterial.valid() ) {
  194. validatorMaterial.focusInvalid();
  195. return false;
  196. }
  197. },
  198. onTabClick: function(tab, navigation, index) {
  199. jQuery('a', navigation).blur();
  200. return false;
  201. }
  202. });
  203. };
  204. return {
  205. init: function () {
  206. // Init Wizard Defaults
  207. initWizardDefaults();
  208. // Init Form Simple Wizard
  209. initWizardSimple();
  210. // Init Form Validation Wizard
  211. initWizardValidation();
  212. }
  213. };
  214. }();
  215. // Initialize when page loads
  216. jQuery(function(){ BeFormWizard.init(); });