| 1 |
jQuery( document ).ready(function ($) { |
| 2 |
|
| 3 |
/** |
| 4 |
* Show save button |
| 5 |
*/ |
| 6 |
$( '.inline-text-shortcode' ).bind('input propertychange', function( e ) { |
| 7 |
|
| 8 |
let id = $( this ).data( 'id' ); |
| 9 |
|
| 10 |
sh_cd_save_button_reset( id ); |
| 11 |
|
| 12 |
$( '#sh-cd-save-button-' + id ).removeClass( 'sh-cd-hide' ); |
| 13 |
|
| 14 |
}); |
| 15 |
|
| 16 |
/** |
| 17 |
* Show inline form |
| 18 |
*/ |
| 19 |
$( '.button-add-inline' ).on( 'click', function( e ) { |
| 20 |
$( '#sh-cd-add-inline' ).toggleClass( 'sh-cd-hide' ); |
| 21 |
}); |
| 22 |
|
| 23 |
/** |
| 24 |
* Save inline form |
| 25 |
*/ |
| 26 |
$( '#sh-cd-add-button' ).on( 'click', function( e ) { |
| 27 |
|
| 28 |
if ( '1' == sh_cd['premium'] ) { |
| 29 |
|
| 30 |
$( '#sh-cd-add-button' ).html('<i class="fas fa-spinner fa-spin"></i>' ); |
| 31 |
|
| 32 |
let data = {}; |
| 33 |
data['content'] = $( '#sh-cd-add-inline-text' ).val(); |
| 34 |
data['slug'] = $( '#sh-cd-add-inline-slug' ).val(); |
| 35 |
data['multisite'] = $( '#sh-cd-add-inline-global' ).is(':checked'); |
| 36 |
data['enabled'] = $( '#sh-cd-add-inline-enabled' ).is(':checked'); |
| 37 |
|
| 38 |
sh_cd_post_data_to_WP( 'add_shortcode', data, sh_cd_handle_add_shortcode ); |
| 39 |
|
| 40 |
} else { |
| 41 |
sh_cd_promo(); |
| 42 |
} |
| 43 |
}); |
| 44 |
|
| 45 |
/** |
| 46 |
* Reset Add button state |
| 47 |
*/ |
| 48 |
$( '#sh-cd-add-inline-text, #sh-cd-add-inline-slug, #sh-cd-add-inline-global, #sh-cd-add-inline-enabled' ).bind('input propertychange', function( e ) { |
| 49 |
$( '#sh-cd-add-button' ).html('<i class="fas fa-save"></i> ' + sh_cd[ 'text-add' ]) |
| 50 |
}); |
| 51 |
|
| 52 |
/** |
| 53 |
* Handle add of shortcode |
| 54 |
* @param response |
| 55 |
* @param data |
| 56 |
*/ |
| 57 |
function sh_cd_handle_add_shortcode( response, data ) { |
| 58 |
|
| 59 |
if ( 1 == response.ok ) { |
| 60 |
|
| 61 |
if ( $( '#sh-cd-add-inline-clear' ).is(':checked') ) { |
| 62 |
$( '#sh-cd-add-inline-text' ).val( '' ); |
| 63 |
$( '#sh-cd-add-inline-slug' ).val( '' ); |
| 64 |
$( '#sh-cd-add-inline-global' ).prop( "checked", false ) |
| 65 |
$( '#sh-cd-add-inline-enabled' ).prop( "checked", false ) |
| 66 |
|
| 67 |
$( '#sh-cd-add-button' ).html('<i class="fas fa-check"></i> ' + sh_cd[ 'text-saved' ]); |
| 68 |
} |
| 69 |
|
| 70 |
$( '#sh-cd-add-inline-results' ).removeClass( 'sh-cd-hide' ); |
| 71 |
|
| 72 |
let text = $( '#sh-cd-add-inline-results span' ).text(); |
| 73 |
|
| 74 |
if ( '' !== text ) { |
| 75 |
text += ', '; |
| 76 |
} |
| 77 |
|
| 78 |
text += ' [sv slug="' + response.shortcode.slug + '"]'; |
| 79 |
|
| 80 |
$( '#sh-cd-add-inline-results span' ).text( text ); |
| 81 |
|
| 82 |
} else { |
| 83 |
alert( response.error_message ); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Save inline shortcode changes |
| 89 |
*/ |
| 90 |
$( '.sh-cd-inline-save-button' ).on( 'click', function( e ) { |
| 91 |
|
| 92 |
if ( '1' == sh_cd['premium'] ) { |
| 93 |
|
| 94 |
let data = {}; |
| 95 |
data['id'] = $( this ).data( 'id' ); |
| 96 |
data['content'] = $( '#sh-cd-text-area-' + data['id'] ).val(); |
| 97 |
|
| 98 |
$( '#sh-cd-save-button-' + data['id'] ).html('<i class="fas fa-spinner fa-spin"></i>' ); |
| 99 |
|
| 100 |
sh_cd_post_data_to_WP( 'update_shortcode', data, sh_cd_handle_update_shortcode ); |
| 101 |
|
| 102 |
} else { |
| 103 |
sh_cd_promo(); |
| 104 |
} |
| 105 |
}); |
| 106 |
|
| 107 |
/** |
| 108 |
* Delete shortcode |
| 109 |
*/ |
| 110 |
$( '.delete-shortcode' ).on( 'click', function( e ) { |
| 111 |
|
| 112 |
if ( false === confirm( sh_cd[ 'text-delete-confirm' ] ) ) { |
| 113 |
return; |
| 114 |
} |
| 115 |
|
| 116 |
let data = {}; |
| 117 |
data['id'] = $( this ).data( 'id' ); |
| 118 |
|
| 119 |
$( '#' + $( this ).attr( 'id' ) + ' i' ).removeClass( 'fa-trash-alt' ).addClass( 'fa-spinner fa-spin' ); |
| 120 |
|
| 121 |
sh_cd_post_data_to_WP( 'delete_shortcode', data, sh_cd_handle_delete_shortcode ); |
| 122 |
|
| 123 |
}); |
| 124 |
|
| 125 |
/** |
| 126 |
* Handle deleting of shortcode |
| 127 |
* @param response |
| 128 |
* @param data |
| 129 |
*/ |
| 130 |
function sh_cd_handle_delete_shortcode( response, data ) { |
| 131 |
|
| 132 |
if ( 1 == response.ok ) { |
| 133 |
$( '#sh-cd-row-' + response.id ).remove(); |
| 134 |
} else { |
| 135 |
$( '#sc-cd-delete-' + response.id + ' i' ).addClass( 'fa-trash-alt' ).removeClass( 'fa-spinner fa-spin' ); |
| 136 |
alert( sh_cd[ 'text-error' ] ); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Toggle shortcode status |
| 142 |
*/ |
| 143 |
$( '.toggle-disable' ).on( 'click', function( e ) { |
| 144 |
|
| 145 |
if ( '1' == sh_cd['premium'] ) { |
| 146 |
|
| 147 |
let data = {}; |
| 148 |
data['id'] = $( this ).data( 'id' ); |
| 149 |
|
| 150 |
$( '#' + $( this ).attr( 'id' ) + ' i' ).removeClass( 'fa-check' ).removeClass( 'fa-times' ).addClass( 'fa-spinner fa-spin' ); |
| 151 |
|
| 152 |
sh_cd_post_data_to_WP( 'toggle_status', data, sh_cd_handle_toggle_disable ); |
| 153 |
|
| 154 |
} else { |
| 155 |
sh_cd_promo(); |
| 156 |
} |
| 157 |
}); |
| 158 |
|
| 159 |
/** |
| 160 |
* Toggle status of a shortcode |
| 161 |
* @param response |
| 162 |
* @param data |
| 163 |
*/ |
| 164 |
function sh_cd_handle_toggle_disable( response, data ) { |
| 165 |
|
| 166 |
if ( 1 == response.ok ) { |
| 167 |
|
| 168 |
let element_id = '#sc-cd-toggle-' + response.id + ' i'; |
| 169 |
|
| 170 |
$( element_id ).removeClass( 'fa-spinner fa-spin' ) |
| 171 |
|
| 172 |
if ( 1 == response.status ) { |
| 173 |
$( element_id ).removeClass( 'fa-check' ); |
| 174 |
$( element_id ).addClass( 'fa-times' ); |
| 175 |
} else { |
| 176 |
$( element_id ).removeClass( 'fa-times' ); |
| 177 |
$( element_id ).addClass( 'fa-check' ); |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Toggle multisite status |
| 184 |
*/ |
| 185 |
$( '.toggle-multisite' ).on( 'click', function( e ) { |
| 186 |
|
| 187 |
if ( '1' == sh_cd['premium'] ) { |
| 188 |
|
| 189 |
let data = {}; |
| 190 |
data['id'] = $( this ).data( 'id' ); |
| 191 |
|
| 192 |
$( '#' + $( this ).attr( 'id' ) + ' i' ).removeClass( 'fa-check' ).removeClass( 'fa-times' ).addClass( 'fa-spinner fa-spin' ); |
| 193 |
|
| 194 |
sh_cd_post_data_to_WP( 'toggle_multisite', data, sh_cd_handle_toggle_multisite ); |
| 195 |
|
| 196 |
} else { |
| 197 |
sh_cd_promo(); |
| 198 |
} |
| 199 |
}); |
| 200 |
|
| 201 |
/** |
| 202 |
* Toggle multiside of a shortcode |
| 203 |
* @param response |
| 204 |
* @param data |
| 205 |
*/ |
| 206 |
function sh_cd_handle_toggle_multisite( response, data ) { |
| 207 |
|
| 208 |
if ( 1 == response.ok ) { |
| 209 |
|
| 210 |
let element_id = '#sc-cd-multisite-' + response.id + ' i'; |
| 211 |
|
| 212 |
$( element_id ).removeClass( 'fa-spinner fa-spin' ) |
| 213 |
|
| 214 |
if ( 0 == response.multisite ) { |
| 215 |
$( element_id ).removeClass( 'fa-check' ); |
| 216 |
$( element_id ).addClass( 'fa-times' ); |
| 217 |
} else { |
| 218 |
$( element_id ).removeClass( 'fa-times' ); |
| 219 |
$( element_id ).addClass( 'fa-check' ); |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* Toggle status of a shortcode |
| 226 |
* @param response |
| 227 |
* @param data |
| 228 |
*/ |
| 229 |
function sh_cd_handle_update_shortcode( response, data ) { |
| 230 |
if ( 1 == response.ok ) { |
| 231 |
sh_cd_save_button_success( response.id ); |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* Set save button to success |
| 237 |
* @param i |
| 238 |
*/ |
| 239 |
function sh_cd_save_button_success( i ) { |
| 240 |
$( '#sh-cd-save-button-' + i ).html('<i class="fas fa-check"></i> ' + sh_cd[ 'text-saved' ]); |
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
* Set save button to save |
| 245 |
* @param i |
| 246 |
*/ |
| 247 |
function sh_cd_save_button_reset( i ) { |
| 248 |
$( '#sh-cd-save-button-' + i ).html('<i class="fas fa-save"></i> ' + sh_cd[ 'text-save' ]); |
| 249 |
} |
| 250 |
|
| 251 |
/** |
| 252 |
* Post Data to ajax handler |
| 253 |
* |
| 254 |
* @param action |
| 255 |
* @param data |
| 256 |
* @param callback |
| 257 |
*/ |
| 258 |
function sh_cd_post_data_to_WP( action, data, callback ) { |
| 259 |
|
| 260 |
var post_data = {}; |
| 261 |
post_data['action'] = action; |
| 262 |
post_data['security'] = sh_cd['security']; |
| 263 |
|
| 264 |
var post_data = obj3 = $.extend( post_data, data ); |
| 265 |
|
| 266 |
$.post( ajaxurl, post_data, function( response, post_data ) { |
| 267 |
callback && callback( response, post_data ); |
| 268 |
}); |
| 269 |
} |
| 270 |
|
| 271 |
/** |
| 272 |
* Show Promo stuff |
| 273 |
*/ |
| 274 |
function sh_cd_promo() { |
| 275 |
sh_cd_show_upgrade_buttons(); |
| 276 |
} |
| 277 |
|
| 278 |
/** |
| 279 |
* Show upgrade buttons |
| 280 |
*/ |
| 281 |
function sh_cd_show_upgrade_buttons() { |
| 282 |
$( '.sh-cd-upgrade-button' ).removeClass( 'sh-cd-hide' ) |
| 283 |
} |
| 284 |
|
| 285 |
}); |
| 286 |
|