11( function ( $ ) {
22
3- $ ( document ) . ready ( function ( ) {
4- /**
5- * Submit Step form when selecting an answer
6- * Adds loading animation to empty "nextstep" div
7- * Fetch content via ajax
8- * Insert HTML and update URL
9- */
10- $ ( document ) . on ( 'change' , 'input[name=stepanswerid] ' , function ( ) {
11- var form = $ ( this ) . parents ( 'form' ) ,
12- step = form . parent ( '.step' ) ,
13- nextstep_holder = step . find ( '> .nextstep' ) ;
3+ $ ( document ) . ready ( function ( ) {
4+ /**
5+ * Submit Step form when selecting an answer
6+ * Adds loading animation to empty "nextstep" div
7+ * Fetch content via ajax
8+ * Insert HTML and update URL
9+ */
10+ $ ( document ) . on ( 'change' , 'input[name=stepanswerid] ' , function ( ) {
11+ var form = $ ( this ) . parents ( 'form' ) ,
12+ step = form . parent ( '.step' ) ,
13+ nextstep_holder = step . find ( '> .nextstep' ) ;
1414
15- nextstep_holder . html ( '<div class="spinner-holder"><div class="spinner"><span class="sr-only">loading</span></div></div>' ) ;
16- setTimeout ( function ( ) {
17- nextstep_holder . addClass ( 'loading' ) ;
18- } , 100 ) ;
15+ nextstep_holder . html ( '<div class="spinner-holder"><div class="spinner"><span class="sr-only">loading</span></div></div>' ) ;
16+ setTimeout ( function ( ) {
17+ nextstep_holder . addClass ( 'loading' ) ;
18+ } , 100 ) ;
1919
20- $ . ajax ( {
21- url : form . attr ( 'action' ) ,
22- type : form . attr ( 'method' ) ,
23- dataType : 'json' ,
24- data : form . serialize ( ) ,
25- success : function ( data ) {
26- nextstep_holder . addClass ( 'new-content-loaded' ) ;
27- nextstep_holder . html ( data . html ) ;
28- window . history . pushState ( null , null , data . nexturl ) ;
29- } ,
30- error : function ( xhr , err ) {
31- nextstep_holder . html ( xhr . responseText ) ;
32- }
33- } ) . always ( function ( ) {
34- setTimeout ( function ( ) {
35- nextstep_holder . removeClass ( 'loading new-content-loaded' ) ;
36- } , 100 ) ;
37- } ) ;
38- } ) ;
20+ $ . ajax ( {
21+ url : form . attr ( 'action' ) ,
22+ type : form . attr ( 'method' ) ,
23+ dataType : 'json' ,
24+ data : form . serialize ( ) ,
25+ success : function ( data ) {
26+ nextstep_holder . addClass ( 'new-content-loaded' ) ;
27+ nextstep_holder . html ( data . html ) ;
28+ window . history . pushState ( null , null , data . nexturl ) ;
29+ } ,
30+ error : function ( xhr , err ) {
31+ nextstep_holder . html ( xhr . responseText ) ;
32+ }
33+ } ) . always ( function ( ) {
34+ setTimeout ( function ( ) {
35+ nextstep_holder . removeClass ( 'loading new-content-loaded' ) ;
36+ } , 100 ) ;
37+ } ) ;
38+ } ) ;
3939
40- /**
41- * Handles the restart button
42- * Empties all subsequent steps then
43- * Scroll back to first step then
44- * Reset url to original page url
45- */
46- $ ( document ) . on ( 'click' , 'button[data-action="restart-tree"]' , function ( ) {
47- var button = $ ( this ) ,
48- firststep = button . parents ( '.step--first' ) ,
49- radio = firststep . find ( 'input[type="radio"]' ) ,
50- tree = firststep . parents ( '.ElementDecisionTree' ) ;
40+ /**
41+ * Handles the restart button
42+ * Empties all subsequent steps then
43+ * Scroll back to first step then
44+ * Reset url to original page url
45+ */
46+ $ ( document ) . on ( 'click' , 'button[data-action="restart-tree"]' , function ( ) {
47+ var button = $ ( this ) ,
48+ firststep = button . parents ( '.step--first' ) ,
49+ radio = firststep . find ( 'input[type="radio"]' ) ,
50+ tree = firststep . parents ( '.ElementDecisionTree' ) ;
5151
52- if ( firststep ) {
53- firststep . find ( '.nextstep' ) . fadeOut ( function ( ) {
54- $ ( this ) . html ( '' ) . show ( ) ;
55- radio . removeAttr ( 'checked' ) ;
56- $ ( 'html, body' ) . animate ( {
57- scrollTop : $ ( tree ) . offset ( ) . top - 150
58- } , 500 ) ;
59- window . history . pushState ( null , null , button . data ( 'target' ) ) ;
60- } ) ;
61- }
62- } ) ;
52+ if ( firststep ) {
53+ firststep . find ( '.nextstep' ) . fadeOut ( function ( ) {
54+ $ ( this ) . html ( '' ) . show ( ) ;
55+ radio . removeAttr ( 'checked' ) ;
6356
64- } ) ;
57+ if ( $ ( tree ) . length > 0 ) {
58+ $ ( 'html, body' ) . animate ( {
59+ scrollTop : $ ( tree ) . offset ( ) . top - 150
60+ } , 500 ) ;
61+ }
62+
63+ window . history . pushState ( null , null , button . data ( 'target' ) ) ;
64+ } ) ;
65+ }
66+ } ) ;
67+ } ) ;
6568} ) ( jQuery ) ;
0 commit comments