| 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 |
wpgetapi.tooltips(); |
| 16 |
|
| 17 |
$page |
| 18 |
.on( 'click', '.functions .copy-this', wpgetapi.copyThis ) |
| 19 |
.on( 'change keyup', '.field-id input', wpgetapi.functionsHelp ) |
| 20 |
.on( 'cmb2_remove_row cmb2_add_row cmb2_shift_row', wpgetapi.functionsHelp ); |
| 21 |
|
| 22 |
} |
| 23 |
|
| 24 |
|
| 25 |
/** |
| 26 |
* Do the tooltips |
| 27 |
*/ |
| 28 |
wpgetapi.tooltips = function () { |
| 29 |
$( ".wrap.wpgetapi .dashicons-editor-help" ).tooltip({ |
| 30 |
items: "[data-tip]", |
| 31 |
content: function () { |
| 32 |
return $(this).data("tip"); |
| 33 |
}, |
| 34 |
classes: { |
| 35 |
"ui-tooltip": "wpgetapi-tooltip" |
| 36 |
}, |
| 37 |
position: { my: "left+10 center", at: "right center" }, |
| 38 |
close: function (event, ui) { |
| 39 |
ui.tooltip.hover( |
| 40 |
function () { |
| 41 |
$(this).stop(true).fadeTo(400, 1); |
| 42 |
}, |
| 43 |
function () { |
| 44 |
$(this).fadeOut("400", function () { |
| 45 |
$(this).remove(); |
| 46 |
}) |
| 47 |
}); |
| 48 |
} |
| 49 |
}); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Copy the template tag or the shortcode |
| 54 |
*/ |
| 55 |
wpgetapi.copyThis = function ( e ) { |
| 56 |
|
| 57 |
const $this = $( this ); |
| 58 |
const textToCopy = $this.prev( '.wpgetapi-copy' ).text(); |
| 59 |
|
| 60 |
navigator.clipboard.writeText(textToCopy).then( |
| 61 |
function() { |
| 62 |
/* clipboard successfully set */ |
| 63 |
$this.after( '<span class="copied">Copied</span>' ); |
| 64 |
setTimeout( |
| 65 |
function() { |
| 66 |
$( '.copied' ).remove(); |
| 67 |
}, 3000 |
| 68 |
); |
| 69 |
|
| 70 |
}, |
| 71 |
function() { |
| 72 |
/* clipboard write failed */ |
| 73 |
$this.append('Oops! Your browser doesn\'t support this.') |
| 74 |
} |
| 75 |
) |
| 76 |
|
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
/** |
| 81 |
* Adds the endpoint into the functions help section, |
| 82 |
* meaning the Template tag and the shortcode. |
| 83 |
*/ |
| 84 |
wpgetapi.functionsHelp = function ( e ) { |
| 85 |
|
| 86 |
// change, keyup, add_row etc |
| 87 |
if( e && e.type.length > 0 ) { |
| 88 |
|
| 89 |
var $this = $( this ); |
| 90 |
var $group = $this.parents( '.cmb-repeatable-grouping' ); |
| 91 |
|
| 92 |
// if we are adding a group, clear it |
| 93 |
if( e.type == 'cmb2_add_row' ) { |
| 94 |
$group = $( '.cmb-repeatable-grouping' ).last(); |
| 95 |
$group.find( '.functions .endpoint_id' ).html( '' ); |
| 96 |
} |
| 97 |
|
| 98 |
if( $group.length > 0 ) |
| 99 |
$group.find( '.functions .endpoint_id' ).html( $this.val() ); |
| 100 |
|
| 101 |
} else { |
| 102 |
|
| 103 |
$('.cmb-repeatable-grouping').each(function( index, value ) { |
| 104 |
|
| 105 |
var $group = $( this ); |
| 106 |
var endpoint_value = $group.find( '.field-id input' ).val(); |
| 107 |
$group.find( '.functions .endpoint_id' ).html( endpoint_value ); |
| 108 |
|
| 109 |
}); |
| 110 |
|
| 111 |
} |
| 112 |
|
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Gets jQuery object containing all . Caches the result. |
| 117 |
* |
| 118 |
* @since 1.0.0 |
| 119 |
* |
| 120 |
* @return {Object} jQuery object containing all. |
| 121 |
*/ |
| 122 |
wpgetapi.page = function() { |
| 123 |
if ( wpgetapi.$page ) { |
| 124 |
return wpgetapi.$page; |
| 125 |
} |
| 126 |
wpgetapi.$page = $('.wpgetapi'); |
| 127 |
return wpgetapi.$page; |
| 128 |
}; |
| 129 |
|
| 130 |
$( wpgetapi.onReady ); |
| 131 |
|
| 132 |
}(window, document, jQuery, window.WpGetApi)); |