@@ -460,6 +460,24 @@ $.entwine('ss', function($) {
460
460
// default to first button if none given - simulates browser behaviour
461
461
if ( ! button ) button = this . find ( '.btn-toolbar :submit:first' ) ;
462
462
463
+ // set button to "submitting" state
464
+ $ ( button ) . addClass ( 'btn--loading loading' ) ;
465
+ $ ( button ) . prop ( 'disabled' , true ) ;
466
+
467
+ if ( $ ( button ) . is ( 'button' ) ) {
468
+ $ ( button ) . append (
469
+ $ (
470
+ '<div class="btn__loading-icon">' +
471
+ '<span class="btn__circle btn__circle--1"></span>' +
472
+ '<span class="btn__circle btn__circle--2"></span>' +
473
+ '<span class="btn__circle btn__circle--3"></span>' +
474
+ '</div>'
475
+ )
476
+ ) ;
477
+
478
+ $ ( button ) . css ( $ ( button ) . outerWidth ( ) + 'px' ) ;
479
+ }
480
+
463
481
var beforeSubmitFormEventData = {
464
482
// array of promises that must resolve({success:true}) before the form is submitted
465
483
// result of each promise must be an object of
@@ -470,6 +488,14 @@ $.entwine('ss', function($) {
470
488
} ;
471
489
form . trigger ( 'beforesubmitform' , beforeSubmitFormEventData ) ;
472
490
491
+ var clearButton = function ( ) {
492
+ $ ( button ) . removeClass ( 'btn--loading loading' ) ;
493
+ $ ( button ) . prop ( 'disabled' , false ) ;
494
+ $ ( button ) . find ( '.btn__loading-icon' ) . remove ( ) ;
495
+ $ ( button ) . css ( 'width' , 'auto' ) ;
496
+ $ ( button ) . text ( $ ( button ) . data ( 'original-text' ) ) ;
497
+ }
498
+
473
499
Promise . all ( beforeSubmitFormEventData . promises ) . then ( function ( results ) {
474
500
475
501
let success = true ;
@@ -499,38 +525,15 @@ $.entwine('ss', function($) {
499
525
}
500
526
} ) ;
501
527
}
528
+ clearButton ( ) ;
502
529
return false ;
503
530
}
504
531
505
532
self . trigger ( 'submitform' , { form : form , button : button } ) ;
506
533
507
- // set button to "submitting" state
508
- $ ( button ) . addClass ( 'btn--loading loading' ) ;
509
- $ ( button ) . prop ( 'disabled' , true ) ;
510
-
511
- if ( $ ( button ) . is ( 'button' ) ) {
512
-
513
- $ ( button ) . append ( $ (
514
- '<div class="btn__loading-icon">' +
515
- '<span class="btn__circle btn__circle--1"></span>' +
516
- '<span class="btn__circle btn__circle--2"></span>' +
517
- '<span class="btn__circle btn__circle--3"></span>' +
518
- '</div>' ) ) ;
519
-
520
- $ ( button ) . css ( $ ( button ) . outerWidth ( ) + 'px' ) ;
521
- }
522
-
523
534
// validate if required
524
535
var validationResult = form . validate ( ) ;
525
536
526
- var clearButton = function ( ) {
527
- $ ( button ) . removeClass ( 'btn--loading loading' ) ;
528
- $ ( button ) . prop ( 'disabled' , false ) ;
529
- $ ( button ) . find ( '.btn__loading-icon' ) . remove ( ) ;
530
- $ ( button ) . css ( 'width' , 'auto' ) ;
531
- $ ( button ) . text ( $ ( button ) . data ( 'original-text' ) ) ;
532
- }
533
-
534
537
if ( typeof validationResult !== 'undefined' && ! validationResult ) {
535
538
statusMessage ( "Validation failed." , "bad" ) ;
536
539
clearButton ( ) ;
@@ -572,7 +575,10 @@ $.entwine('ss', function($) {
572
575
newContentEls . filter ( 'form' ) . trigger ( 'aftersubmitform' , { status : status , xhr : xhr , formData : formData } ) ;
573
576
}
574
577
} , ajaxOptions ) ) ;
575
- } ) ;
578
+ } ) . catch ( function ( ) {
579
+ // Handle errors from the promises
580
+ clearButton ( ) ;
581
+ } ) ; ;
576
582
577
583
return false ;
578
584
} ,
0 commit comments