| 1 |
window.WpGetApi = window.WpGetApi || {}; |
| 2 |
|
| 3 |
(function (window, document, $, wpgetapi, undefined) { |
| 4 |
|
| 5 |
'use strict'; |
| 6 |
|
| 7 |
/** |
| 8 |
* Start the JS. |
| 9 |
*/ |
| 10 |
wpgetapi.onReady = function () { |
| 11 |
|
| 12 |
var $page = wpgetapi.page(); |
| 13 |
|
| 14 |
wpgetapi.functionsHelp(); |
| 15 |
|
| 16 |
$page |
| 17 |
.on( 'click', '.functions .copy-this', wpgetapi.copyThis ) |
| 18 |
.on( 'change keyup', '.field-id input', wpgetapi.functionsHelp ) |
| 19 |
.on( 'cmb2_remove_row cmb2_add_row cmb2_shift_row', wpgetapi.functionsHelp ); |
| 20 |
|
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Copy the template tag or the shortcode |
| 25 |
*/ |
| 26 |
wpgetapi.copyThis = function ( e ) { |
| 27 |
|
| 28 |
const $this = $( this ); |
| 29 |
const textToCopy = $this.prev( '.wpgetapi-copy' ).text(); |
| 30 |
|
| 31 |
navigator.clipboard.writeText(textToCopy).then( |
| 32 |
function() { |
| 33 |
/* clipboard successfully set */ |
| 34 |
$this.after( '<span class="copied">Copied</span>' ); |
| 35 |
setTimeout( |
| 36 |
function() { |
| 37 |
$( '.copied' ).remove(); |
| 38 |
}, 3000 |
| 39 |
); |
| 40 |
|
| 41 |
}, |
| 42 |
function() { |
| 43 |
/* clipboard write failed */ |
| 44 |
$this.append('Oops! Your browser doesn\'t support this.') |
| 45 |
} |
| 46 |
) |
| 47 |
|
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
/** |
| 52 |
* Adds the endpoint into the functions help section, |
| 53 |
* meaning the Template tag and the shortcode. |
| 54 |
*/ |
| 55 |
wpgetapi.functionsHelp = function ( e ) { |
| 56 |
|
| 57 |
// change, keyup, add_row etc |
| 58 |
if( e && e.type.length > 0 ) { |
| 59 |
|
| 60 |
var $this = $( this ); |
| 61 |
var $group = $this.parents( '.cmb-repeatable-grouping' ); |
| 62 |
|
| 63 |
// if we are adding a group, clear it |
| 64 |
if( e.type == 'cmb2_add_row' ) { |
| 65 |
$group = $( '.cmb-repeatable-grouping' ).last(); |
| 66 |
$group.find( '.functions .endpoint_id' ).html( '' ); |
| 67 |
} |
| 68 |
|
| 69 |
if( $group.length > 0 ) |
| 70 |
$group.find( '.functions .endpoint_id' ).html( $this.val() ); |
| 71 |
|
| 72 |
} else { |
| 73 |
|
| 74 |
$('.cmb-repeatable-grouping').each(function( index, value ) { |
| 75 |
|
| 76 |
var $group = $( this ); |
| 77 |
var endpoint_value = $group.find( '.field-id input' ).val(); |
| 78 |
$group.find( '.functions .endpoint_id' ).html( endpoint_value ); |
| 79 |
|
| 80 |
}); |
| 81 |
|
| 82 |
} |
| 83 |
|
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Gets jQuery object containing all . Caches the result. |
| 88 |
* |
| 89 |
* @since 1.0.0 |
| 90 |
* |
| 91 |
* @return {Object} jQuery object containing all. |
| 92 |
*/ |
| 93 |
wpgetapi.page = function() { |
| 94 |
if ( wpgetapi.$page ) { |
| 95 |
return wpgetapi.$page; |
| 96 |
} |
| 97 |
wpgetapi.$page = $('.wpgetapi'); |
| 98 |
return wpgetapi.$page; |
| 99 |
}; |
| 100 |
|
| 101 |
$( wpgetapi.onReady ); |
| 102 |
|
| 103 |
}(window, document, jQuery, window.WpGetApi)); |