|
44 | 44 | } ).text( text ); |
45 | 45 | } |
46 | 46 |
|
| 47 | + /** |
| 48 | + * Insert a notice element before the form and scroll it into view. |
| 49 | + * |
| 50 | + * @param {jQuery} $el - The notice element to insert. |
| 51 | + */ |
| 52 | + function insertNoticeBeforeForm( $el ) { |
| 53 | + $el.insertBefore( $( '.scg-form' ) ); |
| 54 | + |
| 55 | + const offset = $el.offset(); |
| 56 | + if ( offset?.top ) { |
| 57 | + $( 'html, body' ).animate( { scrollTop: Math.max( 0, offset.top - 50 ) }, 300 ); |
| 58 | + } |
| 59 | + } |
| 60 | + |
47 | 61 | /** |
48 | 62 | * Send a single batch AJAX request and return the jQuery promise. |
49 | 63 | * |
|
335 | 349 |
|
336 | 350 | const $el = createElement( 'div', { class: 'notice notice-error scg-error-message' } ); |
337 | 351 | $el.append( createElement( 'p' ).text( message ) ); |
338 | | - $el.insertBefore( $( '.scg-form' ) ); |
339 | | - |
340 | | - const offset = $el.offset(); |
341 | | - if ( offset?.top ) { |
342 | | - $( 'html, body' ).animate( { scrollTop: Math.max( 0, offset.top - 50 ) }, 300 ); |
343 | | - } |
| 352 | + insertNoticeBeforeForm( $el ); |
344 | 353 |
|
345 | 354 | setTimeout( () => $el.fadeOut( 400, () => $el.remove() ), 5000 ); |
346 | 355 | }, |
|
355 | 364 |
|
356 | 365 | const $el = createElement( 'div', { class: 'notice notice-success is-dismissible' } ); |
357 | 366 | $el.append( createElement( 'p' ).text( message ) ); |
358 | | - $( '.scg-form' ).before( $el ); |
359 | | - |
360 | | - const offset = $el.offset(); |
361 | | - if ( offset?.top ) { |
362 | | - $( 'html, body' ).animate( { scrollTop: Math.max( 0, offset.top - 50 ) }, 300 ); |
363 | | - } |
| 367 | + insertNoticeBeforeForm( $el ); |
364 | 368 | }, |
365 | 369 | }; |
366 | 370 |
|
|
0 commit comments