sureforms-pointer.js
91 lines
| 1 | jQuery( document ).ready( function ( $ ) { |
| 2 | // Check with the server if the pointer should be shown |
| 3 | $.post( |
| 4 | sureformsPointerData.ajaxurl, |
| 5 | { |
| 6 | action: 'should_show_pointer', |
| 7 | pointer_nonce: sureformsPointerData.pointer_nonce, |
| 8 | }, |
| 9 | function ( response ) { |
| 10 | if ( ! response || ! response.show ) { |
| 11 | return; |
| 12 | } |
| 13 | |
| 14 | let $target = $( '#toplevel_page_sureforms_menu' ); |
| 15 | if ( ! $target.length ) { |
| 16 | $target = $( '#menu-plugins' ); // fallback |
| 17 | } |
| 18 | |
| 19 | const pointerContent = |
| 20 | '<h3>' + |
| 21 | response.title + |
| 22 | '</h3>' + |
| 23 | '<p>' + |
| 24 | response.content + |
| 25 | '</p>'; |
| 26 | |
| 27 | let pointerClosedBy = null; // 'cta' or 'dismiss' or null |
| 28 | |
| 29 | $target |
| 30 | .pointer( { |
| 31 | content: pointerContent, |
| 32 | position: { |
| 33 | edge: 'left', |
| 34 | align: 'center', |
| 35 | }, |
| 36 | // `_event` is unused but required positionally to access `t`. |
| 37 | buttons( _event, t ) { |
| 38 | const dismissBtn = $( |
| 39 | '<a class="close" href="#" style="margin-left:8px;"></a>' |
| 40 | ) |
| 41 | .text( response.dismiss ) |
| 42 | .on( 'click.pointer', function ( e ) { |
| 43 | e.preventDefault(); |
| 44 | pointerClosedBy = 'dismiss'; |
| 45 | t.element.pointer( 'close' ); |
| 46 | $.post( sureformsPointerData.ajaxurl, { |
| 47 | action: 'sureforms_dismiss_pointer', |
| 48 | pointer_nonce: |
| 49 | sureformsPointerData.pointer_nonce, |
| 50 | } ); |
| 51 | } ); |
| 52 | |
| 53 | const ctaBtn = $( |
| 54 | '<a class="button button-primary" href="' + |
| 55 | response.button_url + |
| 56 | '" style="margin-right:8px;">' + |
| 57 | response.button_text + |
| 58 | '</a>' |
| 59 | ).on( 'click.pointer', function () { |
| 60 | pointerClosedBy = 'cta'; |
| 61 | t.element.pointer( 'close' ); |
| 62 | $.post( sureformsPointerData.ajaxurl, { |
| 63 | action: 'sureforms_accept_cta', |
| 64 | pointer_nonce: |
| 65 | sureformsPointerData.pointer_nonce, |
| 66 | } ); |
| 67 | } ); |
| 68 | |
| 69 | // Wrap both buttons in a div and return as a jQuery object |
| 70 | return $( |
| 71 | '<div style="display:flex;justify-content:space-between;align-items:center;width:100%"></div>' |
| 72 | ) |
| 73 | .append( ctaBtn ) |
| 74 | .append( dismissBtn ); |
| 75 | }, |
| 76 | close() { |
| 77 | if ( ! pointerClosedBy ) { |
| 78 | // Only run if closed by other means (not by our buttons) |
| 79 | $.post( sureformsPointerData.ajaxurl, { |
| 80 | action: 'sureforms_dismiss_pointer', |
| 81 | pointer_nonce: |
| 82 | sureformsPointerData.pointer_nonce, |
| 83 | } ); |
| 84 | } |
| 85 | }, |
| 86 | } ) |
| 87 | .pointer( 'open' ); |
| 88 | } |
| 89 | ); |
| 90 | } ); |
| 91 |