PageRenderTime 25ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/ninja-forms-multi-part/assets/js/front-end/models/partCollection.js

https://bitbucket.org/djmdigital/total-auto-care-wordpress
JavaScript | 110 lines | 69 code | 18 blank | 23 comment | 19 complexity | 2ba903590d5ebe6f916a8772708f0c79 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.0, 0BSD
  1. define( [ 'models/partModel' ], function( PartModel ) {
  2. var collection = Backbone.Collection.extend( {
  3. model: PartModel,
  4. currentElement: false,
  5. initialize: function( models, options ){
  6. this.formModel = options.formModel;
  7. },
  8. getElement: function() {
  9. /*
  10. * If we haven't set an element yet, set it to the first one.
  11. */
  12. if ( ! this.currentElement ) {
  13. this.setElement( this.at( 0 ), true );
  14. }
  15. return this.currentElement;
  16. },
  17. setElement: function( model, silent ) {
  18. silent = silent || false;
  19. /*
  20. * If we have part errors and aren't updating silently, check for part errors.
  21. */
  22. if ( ! silent ) {
  23. if ( this.partErrors() ) return;
  24. }
  25. this.currentElement = model;
  26. if ( ! silent ) {
  27. this.trigger( 'change:part', this );
  28. nfRadio.channel( 'nfMP' ).trigger( 'change:part', this );
  29. }
  30. },
  31. // check for errors on Next click if we have validate parts on
  32. setNextElement: function( model, silent ) {
  33. silent = silent || false;
  34. /*
  35. * If we have part errors and aren't updating silently, check for part errors.
  36. */
  37. if ( ! silent ) {
  38. if ( this.partErrors() ) return;
  39. }
  40. this.currentElement = model;
  41. if ( ! silent ) {
  42. this.trigger( 'change:part', this );
  43. nfRadio.channel( 'nfMP' ).trigger( 'change:part', this );
  44. }
  45. },
  46. // We don't want to stop user from moving back in the form if there are errors
  47. setPreviousElement: function( model, silent ) {
  48. silent = silent || false;
  49. this.currentElement = model;
  50. if ( ! silent ) {
  51. this.trigger( 'change:part', this );
  52. nfRadio.channel( 'nfMP' ).trigger( 'change:part', this );
  53. }
  54. },
  55. next: function (){
  56. /*
  57. * If this isn't the last visible part, move forward.
  58. */
  59. if ( this.getVisibleParts().length - 1 != this.getVisibleParts().indexOf( this.getElement() ) ) {
  60. this.setNextElement( this.getVisibleParts()[ this.getVisibleParts().indexOf( this.getElement() ) + 1 ] );
  61. }
  62. return this;
  63. },
  64. previous: function() {
  65. /*
  66. * If this isn't the first visible part, move backward.
  67. */
  68. if ( 0 != this.getVisibleParts().indexOf( this.getElement() ) ) {
  69. this.setPreviousElement( this.getVisibleParts()[ this.getVisibleParts().indexOf( this.getElement() ) - 1 ] );
  70. }
  71. return this;
  72. },
  73. partErrors: function() {
  74. if ( 'undefined' == typeof this.formModel.get( 'settings' ).mp_validate || 0 == this.formModel.get( 'settings' ).mp_validate ) return false;
  75. /*
  76. * Check to see if our parts have any errors.
  77. */
  78. this.currentElement.validateFields();
  79. return this.currentElement.get( 'errors' );
  80. },
  81. validateFields: function() {
  82. /*
  83. * call validateFields on each visible part
  84. */
  85. _.each( this.getVisibleParts(), function( partModel ) { partModel.validateFields(); } );
  86. },
  87. getVisibleParts: function() {
  88. return this.where( { visible: true } );
  89. }
  90. } );
  91. return collection;
  92. } );